source: trunk/docx4j/src/main/java/org/docx4j/fonts/IdentityPlusMapper.java @ 1555

Revision 1555, 10.7 KB checked in by jharrop, 11 months ago (diff)

Apply Dave Brown's "assorted patches" of 28 April, as described at
 http://dev.plutext.org/forums/docx-java-f6/assorted-patches-t712.html

Line 
1/*
2 *  Copyright 2007-2008, Plutext Pty Ltd.
3 *   
4 *  This file is part of docx4j.
5
6    docx4j is licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18
19 */
20package org.docx4j.fonts;
21
22import java.util.Iterator;
23import java.util.Map;
24
25import org.apache.log4j.Logger;
26import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
27import org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart;
28
29/**
30 *
31 * Maps font names used in the document to
32 * corresponding fonts physically available
33 * on the system.
34 *
35 * This mapper automatically maps
36 * document fonts for which the exact
37 * font is physically available.  Think
38 * of this as an identity mapping.  For
39 * this reason, it will work best on
40 * Windows, or a system on which
41 * Microsoft fonts have been installed.
42 *
43 * You can manually add your own
44 * additional mappings if you wish.
45 *
46 * @author jharrop
47 *
48 */
49public class IdentityPlusMapper extends Mapper {
50       
51       
52        protected static Logger log = Logger.getLogger(IdentityPlusMapper.class);
53
54        public IdentityPlusMapper() {
55                super();
56               
57                //log.debug(System.getProperty("os.name")); // eg Linux
58                //log.debug(System.getProperty("os.arch")); // eg i386
59               
60                if (System.getProperty("os.name").toLowerCase().indexOf("windows")<0) {
61                        log.warn("WARNING! SubstituterWindowsPlatformImpl works best " +
62                                        "on Windows.  To get good results on other platforms, you'll probably  " +
63                                        "need to have installed Windows fonts.");
64                }
65               
66        }
67       
68        static {
69               
70                try {
71                       
72                        PhysicalFonts.discoverPhysicalFonts();
73                       
74                } catch (Exception exc) {
75                        throw new RuntimeException(exc);
76                }
77        }
78
79       
80        /**
81         * Populate the fontMappings object. We make an entry for each
82         * of the documentFontNames.
83         *
84         * @param documentFontNames - the fonts used in the document
85         * @param wmlFonts - the content model for the fonts part
86         * @throws Exception
87         */
88        public void populateFontMappings(Map documentFontNames, org.docx4j.wml.Fonts wmlFonts ) throws Exception {
89                               
90                /* org.docx4j.wml.Fonts fonts is obtained as follows:
91                 *
92                 *     FontTablePart fontTablePart= wordMLPackage.getMainDocumentPart().getFontTablePart();
93                 *     org.docx4j.wml.Fonts fonts = (org.docx4j.wml.Fonts)fontTablePart.getJaxbElement();
94                 *     
95                 * If the document doesn't have a font table,
96                 *     
97                 *              org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart fontTable
98                 *                      = new org.docx4j.openpackaging.parts.WordprocessingML.FontTablePart();
99                 *              fontTable.unmarshalDefaultFonts();
100                 */ 
101               
102                //  We need to make a map out of it.
103//              List<Fonts.Font> fontList = wmlFonts.getFont();
104//              Map<String, Fonts.Font> fontsInFontTable = new HashMap<String, Fonts.Font>();
105//              for (Fonts.Font font : fontList ) {
106//                      fontsInFontTable.put( normalise(font.getName()), font );
107//              }
108               
109
110                Iterator documentFontMapIterator = documentFontNames.entrySet().iterator();
111            while (documentFontMapIterator.hasNext()) {
112                Map.Entry pairs = (Map.Entry)documentFontMapIterator.next();
113               
114                if(pairs.getKey()==null) {
115                        log.info("Skipped null key");
116                        continue;
117//                      pairs = (Map.Entry)documentFontMapIterator.next();
118                }
119               
120                String documentFontname = (String)pairs.getKey();
121                log.debug("Document font: " + documentFontname);
122               
123                if ( PhysicalFonts.getPhysicalFonts().get(documentFontname)!=null ) {
124                       
125                        // An identity mapping; that is all
126                        // this class knows how to do!
127                        fontMappings.put(documentFontname,                                       
128                                                        PhysicalFonts.getPhysicalFonts().get(documentFontname) );                                               
129                } else {
130                       
131                        log.warn("- - No physical font for: " + documentFontname);
132                }
133            }                   
134               
135               
136//              // Iterate through the physical fonts, since their key is their
137//              // postscript name (non-normalised).  This way, we can do a single
138//              // pass.
139//              Iterator physicalFontMapIterator = physicalFontMap.entrySet().iterator();
140//          while (physicalFontMapIterator.hasNext()) {
141//              Map.Entry pairs = (Map.Entry)physicalFontMapIterator.next();
142//             
143//              if(pairs.getKey()==null) {
144//                      log.info("Skipped null key");
145//                      pairs = (Map.Entry)physicalFontMapIterator.next();
146//              }
147//             
148//              String physicalFontName = (String)pairs.getKey();
149//                      //log.debug("\n\n" + physicalFontName);         
150//              //String normalisedFontName = normalise(physicalFontName);
151//                      String fontPath = ((PhysicalFont)pairs.getValue()).getEmbeddedFile();
152//                      String physicalFilename = fontPath.substring( fontPath.lastIndexOf("/") +1).toLowerCase();
153//                     
154//                      String msFontName = filenamesToMsFontNames.get(physicalFilename);
155//                     
156//                      if (msFontName!=null) {
157//                     
158////                            String msFontName = font.getName();
159//                             
160//                              String baseform = msFontName;
161//                              if (msFontName.indexOf(SEPARATOR)>0) {
162//                                      baseform = msFontName.substring(0, msFontName.indexOf(SEPARATOR));
163//                              }
164//                             
165//                      // Add it to the mapping if it is present in the document
166//                      if ( documentFontNames.get(baseform)!=null ) {
167//                             
168//                                      // for now, if the baseform is used,
169//                              // we say bold, italic, and bolditalic are as well
170//                             
171//                              if (pairs.getValue()==null) {
172//                                      log.debug("Handle that");
173//                              } else {
174//                                      fontMappings.put((msFontName),
175//                                                      new FontMapping(msFontName, (PhysicalFont)pairs.getValue() ) );
176//                                      log.info("Added mapping for: " + (msFontName));                                 
177//                              }
178//                             
179//                      } else {
180//                              log.debug("Ignoring physical font " + msFontName
181//                                              + ((PhysicalFont)pairs.getValue()).getEmbeddedFile() );
182//                             
183//                      }
184//                      } else {
185//                             
186//                              log.info("Unknown font: " + physicalFontName + "(" + physicalFilename);
187//                             
188//                      }
189//                             
190//          }
191               
192        }
193
194
195//      public static class PhysicalFontFamily {
196//
197//              String familyName; // For example: Times New Roman
198//              public String getFamilyName() {
199//                      return familyName;
200//              }
201//
202//              PhysicalFontFamily(String familyName) {
203//                      this.familyName = familyName;
204//              }
205//
206//              // We want this, so that when were are searching panose space
207//              // for bold, bolditalic, italic, we can restrict the search
208//              // to this list
209//              Map<String, PhysicalFont> physicalFonts = new HashMap<String, PhysicalFont> ();
210//              void addFont(PhysicalFont physicalFont){
211//                      physicalFonts.put(physicalFont.getName(), physicalFont);
212//              }
213//             
214//              Map<String, PhysicalFont> getPhysicalFonts() {
215//                      return physicalFonts;
216//              }
217//             
218//      }
219       
220       
221        public static void main(String[] args) throws Exception {
222
223                //String inputfilepath = "/home/dev/workspace/docx4j/sample-docs/Word2007-fonts.docx";
224//              String inputfilepath = "C:\\Documents and Settings\\Jason Harrop\\workspace\\docx4j-2009\\sample-docs\\Word2007-fonts.docx";
225                //String inputfilepath = "/home/jharrop/workspace200711/docx4j-001/sample-docs/fonts-modesOfApplication.docx";
226                //String inputfilepath = "/home/jharrop/workspace200711/docx4all/sample-docs/TargetFeatureSet.docx"; //docx4all-fonts.docx";
227                String inputfilepath = "C:\\Documents and Settings\\Jason Harrop\\My Documents\\Downloads\\AUMS-easy.docx";
228               
229                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(inputfilepath));
230                               
231                FontTablePart fontTablePart= wordMLPackage.getMainDocumentPart().getFontTablePart();           
232                org.docx4j.wml.Fonts fonts = (org.docx4j.wml.Fonts)fontTablePart.getJaxbElement();             
233       
234                IdentityPlusMapper s = new IdentityPlusMapper();
235                               
236                ///////////////
237                // Go through the FontsTable, and see what we have filenames for.
238//              for (Fonts.Font font : fontList ) {
239//                      String fontName =  font.getName();
240//                      MicrosoftFonts.Font msFontInfo = (MicrosoftFonts.Font)msFontsFilenames.get(fontName);
241//                      if (msFontInfo!=null) {
242//                              System.out.println( fontName + " at " + msFontInfo.getFilename() );                             
243//                      } else {
244//                              System.out.println( "? " + fontName );                                                         
245//                      }
246//              }
247               
248                //panoseDebugReportOnMicrosoftFonts( fonts );
249               
250                s.populateFontMappings(wordMLPackage.getMainDocumentPart().fontsInUse(), fonts );
251        }
252       
253//      private static void panoseDebugReportOnPhysicalFonts( Map<String, PhysicalFont>physicalFontMap ) {
254//              Iterator fontIterator = physicalFontMap.entrySet().iterator();
255//          while (fontIterator.hasNext()) {
256//              Map.Entry pairs = (Map.Entry)fontIterator.next();
257//             
258//              if(pairs.getKey()==null) {
259//                      log.info("Skipped null key");
260//                      if (pairs.getValue()!=null) {
261//                              log.error(((PhysicalFont)pairs.getValue()).getEmbeddedFile());
262//                      }
263//                     
264//                      if (fontIterator.hasNext() ) {
265//                              pairs = (Map.Entry)fontIterator.next();
266//                      } else {
267//                              return;
268//                      }
269//              }
270//             
271//              String fontName = (String)pairs.getKey();
272//
273//                      PhysicalFont pf = (PhysicalFont)pairs.getValue();
274//                     
275//                      org.apache.fop.fonts.Panose fopPanose = pf.getPanose();
276//                     
277//                              if (fopPanose == null ) {
278//                                      System.out.println(fontName + " .. lacks Panose!");                                     
279//                              } else if (fopPanose!=null ) {
280//                                      System.out.println(fontName + " .. " + fopPanose);
281//                              }
282////                                    long pd = fopPanose.difference(nfontInfo.getPanose().getPanoseArray());
283////                                            System.out.println(".. panose distance: " + pd);                                       
284//          }
285//      }
286
287//      private static void panoseDebugReportOnMicrosoftFonts(org.docx4j.wml.Fonts wmlFonts ) {
288//                             
289//              List<Fonts.Font> fontList = wmlFonts.getFont();
290//              for (Fonts.Font font : fontList ) {
291//                     
292//                      org.docx4j.wml.FontPanose wmlFontPanoseForDocumentFont =
293//                              wmlFontPanoseForDocumentFont = font.getPanose1();
294//                     
295//                      org.apache.fop.fonts.Panose documentFontPanose = null;
296//                      if (wmlFontPanoseForDocumentFont!=null && wmlFontPanoseForDocumentFont.getVal()!=null ) {
297//                              try {
298//                                      documentFontPanose = org.apache.fop.fonts.Panose.makeInstance(wmlFontPanoseForDocumentFont.getVal() );
299//                                     
300//                                      System.out.println( font.getName() + documentFontPanose);
301//                                     
302//                              } catch (IllegalArgumentException e) {                                 
303//                                      log.error(e.getMessage());
304//                                      // For example:
305//                                      // Illegal Panose Array: Invalid value 10 > 8 in position 5 of [ 4 2 7 5 4 10 2 6 7 2 ]
306//                              }
307//                              //log.debug(".. " + fopPanose.toString() );                                     
308//                             
309//                      } else {
310//                              log.debug(".. no panose info!!!");                                                                                                                     
311//                      }
312//                     
313//          }
314//      }
315       
316       
317}
Note: See TracBrowser for help on using the repository browser.