Ignore:
Timestamp:
03/22/08 03:51:50 (4 years ago)
Author:
jharrop
Message:

Embed just the fonts used in the document, plus their bold, italic and bold italic friends.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/packages/WordprocessingMLPackage.java

    r217 r218  
    351351                //     possibly via an extension function in the XSLT 
    352352                if (fontSubstituter==null) { 
     353                        log.debug("Creating new Substituter."); 
    353354                        setFontSubstituter(new Substituter()); 
     355                } else { 
     356                        log.debug("Using existing Substituter."); 
    354357                } 
    355358                xformer.setParameter("substituterInstance", fontSubstituter); 
     
    436439                                 
    437440                Map fontMappings = fontSubstituter.getFontMappings(); 
    438                 Iterator fontMappingsIterator = fontMappings.entrySet().iterator(); 
    439             while (fontMappingsIterator.hasNext()) { 
     441                Map fontsInUse = this.getMainDocumentPart().fontsInUse(); 
     442                Iterator fontMappingsIterator = fontsInUse.entrySet().iterator(); 
     443                while (fontMappingsIterator.hasNext()) { 
    440444                Map.Entry pairs = (Map.Entry)fontMappingsIterator.next(); 
    441445                if(pairs.getKey()==null) { 
     
    445449                 
    446450                String fontName = (String)pairs.getKey(); 
    447                 Substituter.FontMapping fm = (Substituter.FontMapping)pairs.getValue(); 
     451                embed(renderer, Substituter.normalise(fontName), fontMappings);          
     452                // For any font we embed, also embed the bold, italic, and bold italic substitute 
     453                // .. at present, we can't tell which of these forms are actually used, so add them all 
     454                embed(renderer, Substituter.normalise(fontName + Substituter.BOLD), fontMappings); 
     455                embed(renderer, Substituter.normalise(fontName + Substituter.ITALIC), fontMappings); 
     456                embed(renderer, Substituter.normalise(fontName + Substituter.BOLD_ITALIC), fontMappings); 
    448457                 
    449                         if (fm.getPhysicalFont()!=null) { 
    450                                 try { 
    451                                         if (fm.getPhysicalFont().getEmbeddedFile().endsWith(".pfb")) { 
    452                                                  
    453 //                                              String afm = fm.getPhysicalFont().getEmbeddedFile().substring(5, fm.getPhysicalFont().getEmbeddedFile().length()-4 ) + ".afm";  // drop the 'file:' 
    454                                                 String afm = FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()); 
    455                                                 afm = afm.substring(0, afm.length()-4 ) + ".afm";  // drop the 'file:' 
    456                                                 log.info("Looking for: " + afm); 
    457                                                  
    458                                                 // Given the check in substituter, we expect to find one or the other. 
    459                                                 File f = new File(afm); 
    460                                         if (f.exists()) {                                
    461                                                 log.info("Got it"); 
    462                                                 renderer.getFontResolver().addFont(afm, BaseFont.CP1252, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()));  // drop the 'file:'      
    463                                                         log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
    464                                         } else { 
    465                                                 // Should we be doing afm first, or pfm? 
    466                                                         String pfm = FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()); 
    467                                                         pfm = pfm.substring(0, pfm.length()-4 ) + ".pfm";  // drop the 'file:' 
    468                                                         log.info("Looking for: " + pfm); 
    469                                                         f = new File(pfm); 
    470                                                 if (f.exists()) {                                
    471                                                         log.info("Got it"); 
    472                                                         renderer.getFontResolver().addFont(pfm, BaseFont.CP1252, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile() ));  // drop the 'file:' 
    473                                                                 log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
    474                                                 } else { 
    475                                                         // Shouldn't happen. 
    476                                                         log.error("Couldn't find afm or pfm corresponding to " + fm.getPhysicalFont().getEmbeddedFile()); 
    477                                                 } 
    478                                         } 
    479                                         } else {                                 
    480                                                 renderer.getFontResolver().addFont(FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()), true); 
    481                                                 log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
    482                                         } 
    483                                 } catch (java.io.IOException e) { 
    484                                  
    485                                 /*  
    486                                  * [AWT-EventQueue-0] INFO  packages.WordprocessingMLPackage - Substituting symbol with standardsymbolsl from file:/usr/share/fonts/type1/gsfonts/s050000l.pfb  
    487 java.io.IOException: Unsupported font type 
    488         at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:199) 
    489          
    490         .pfb not supported, even with iText 2.0.8 
    491          
    492                                  */ 
    493                                         e.printStackTrace(); 
    494                                         log.warn("Shouldn't happen - should have been detected upstream ... " +  e.getMessage() + ": " + fm.getPhysicalFont().getEmbeddedFile());  
    495                                 } catch (Exception e) { 
    496                                         e.printStackTrace(); 
    497                                         log.error("Shouldn't happen - should have been detected upstream ... " + e.getMessage());  
    498                                 } 
    499                         } else { 
    500                                 log.warn("Can't addFont for: " + fontName);  
    501                         } 
    502458            } 
    503459                 
     
    519475                renderer.createPDF(os); 
    520476                 
     477        } 
     478        /** 
     479         * @param renderer 
     480         * @param fontName 
     481         * @param fm 
     482         */ 
     483        private void embed(org.xhtmlrenderer.pdf.ITextRenderer renderer, 
     484                        String fontName, Map fontMappings) { 
     485                Substituter.FontMapping fm = (Substituter.FontMapping)fontMappings.get( fontName ); 
     486                 
     487                if (fm == null) { 
     488                        log.warn("No mapping found for: " + fontName); 
     489                } else if (fm.getPhysicalFont()!=null) { 
     490                        try { 
     491                                if (fm.getPhysicalFont().getEmbeddedFile().endsWith(".pfb")) { 
     492                                         
     493//                                              String afm = fm.getPhysicalFont().getEmbeddedFile().substring(5, fm.getPhysicalFont().getEmbeddedFile().length()-4 ) + ".afm";  // drop the 'file:' 
     494                                        String afm = FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()); 
     495                                        afm = afm.substring(0, afm.length()-4 ) + ".afm";  // drop the 'file:' 
     496                                        log.info("Looking for: " + afm); 
     497                                         
     498                                        // Given the check in substituter, we expect to find one or the other. 
     499                                        File f = new File(afm); 
     500                                if (f.exists()) {                                
     501                                        log.info("Got it"); 
     502                                        renderer.getFontResolver().addFont(afm, BaseFont.CP1252, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()));  // drop the 'file:'      
     503                                                log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
     504                                } else { 
     505                                        // Should we be doing afm first, or pfm? 
     506                                                String pfm = FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()); 
     507                                                pfm = pfm.substring(0, pfm.length()-4 ) + ".pfm";  // drop the 'file:' 
     508                                                log.info("Looking for: " + pfm); 
     509                                                f = new File(pfm); 
     510                                        if (f.exists()) {                                
     511                                                log.info("Got it"); 
     512                                                renderer.getFontResolver().addFont(pfm, BaseFont.CP1252, true, FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile() ));  // drop the 'file:' 
     513                                                        log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
     514                                        } else { 
     515                                                // Shouldn't happen. 
     516                                                log.error("Couldn't find afm or pfm corresponding to " + fm.getPhysicalFont().getEmbeddedFile()); 
     517                                        } 
     518                                } 
     519                                } else {                                 
     520                                        renderer.getFontResolver().addFont(FontUtils.pathFromURL(fm.getPhysicalFont().getEmbeddedFile()), true); 
     521                                        log.info("Substituting " + fontName + " with embedding " + fm.getPhysicalFont().getFamilyName() + " from " + fm.getPhysicalFont().getEmbeddedFile() ); 
     522                                } 
     523                        } catch (java.io.IOException e) { 
     524                         
     525                        /*  
     526                         * [AWT-EventQueue-0] INFO  packages.WordprocessingMLPackage - Substituting symbol with standardsymbolsl from file:/usr/share/fonts/type1/gsfonts/s050000l.pfb  
     527java.io.IOException: Unsupported font type 
     528at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:199) 
     529 
     530.pfb not supported, even with iText 2.0.8 
     531 
     532                         */ 
     533                                e.printStackTrace(); 
     534                                log.warn("Shouldn't happen - should have been detected upstream ... " +  e.getMessage() + ": " + fm.getPhysicalFont().getEmbeddedFile());  
     535                        } catch (Exception e) { 
     536                                e.printStackTrace(); 
     537                                log.error("Shouldn't happen - should have been detected upstream ... " + e.getMessage());  
     538                        } 
     539                } else { 
     540                        log.warn("Can't addFont for: " + fontName);  
     541                } 
    521542        }        
    522543         
Note: See TracChangeset for help on using the changeset viewer.