source: trunk/docx4j/src/main/java/org/docx4j/fonts/Mapper.java @ 871

Revision 871, 5.4 KB checked in by jharrop, 3 years ago (diff)

Get rid of System.out.println (mostly).

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.Collections;
23import java.util.HashMap;
24import java.util.Map;
25
26import org.apache.log4j.Logger;
27
28/**
29 * Maps font names used in the document to
30 * fonts physically available
31 * on the system.
32 *
33 * So, a mapper per document.
34 * If fonts are added to the document
35 * (ie fonts introduced into use)
36 * then the mapper should be updated
37 * to include a mapping for the
38 * new font.
39 *
40 * There are 2 implementations:
41 *
42 * - IndentityPlusMapper, which is best
43 *   where most of the fonts used in the
44 *   document are physically present
45 *   on the system
46 *   
47 * - BestMatchingMapper, useful on
48 *   Linux and OSX systems on which
49 *   Microsoft fonts have not been
50 *   installed.
51 *   
52 * Whichever one you use, you can
53 * add/remove mappings programmatically
54 * to customise to your needs.
55 *
56 * @author jharrop
57 *
58 */
59public abstract class Mapper {
60       
61       
62        protected static Logger log = Logger.getLogger(Mapper.class);
63
64        public Mapper() {
65                super();
66        }
67       
68        protected final static Map<String, PhysicalFont> fontMappings;
69        public Map<String, PhysicalFont> getFontMappings() {
70                return fontMappings;
71        }       
72       
73        public final static String FONT_FALLBACK = "Times New Roman"; 
74
75       
76        static {
77                fontMappings = Collections.synchronizedMap(new HashMap<String, PhysicalFont>());
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 abstract void populateFontMappings(Map documentFontNames, 
89                        org.docx4j.wml.Fonts wmlFonts ) throws Exception;
90       
91       
92        // For Xalan
93        public static String getSubstituteFontXsltExtension(Mapper s, String documentStyleId, String bolditalic, boolean fontFamilyStack) {
94               
95                return s.getSubstituteFontXsltExtension(documentStyleId, bolditalic, fontFamilyStack);
96        }
97       
98        public String getSubstituteFontXsltExtension(String documentStyleId, 
99                        String bolditalic, boolean fontFamilyStack) {
100               
101                log.debug("Trying to insert HTML font-family value for " + documentStyleId);
102                               
103                if (documentStyleId==null) {
104                        log.error("passed null documentStyleId");
105                        return "nullInputToExtension";
106                }
107
108               
109               
110                PhysicalFont physicalFont = (PhysicalFont)fontMappings.get((documentStyleId));
111                if (physicalFont==null) {
112
113                        log.error("No mapping for: " + documentStyleId);
114                        return Mapper.FONT_FALLBACK;
115                } else {
116
117                        // iTextFontResolver wants a font family name
118                        // Until such time as we get this from FOP,
119                        // use the following heuristic..
120                       
121                        String fontFamily = physicalFont.getName();
122                       
123                        if (fontFamily.startsWith("Britannic")) { // special case
124                                return fontFamily;
125                        }
126                        if (fontFamily.endsWith(" Demibold" ) ) {
127                                fontFamily = fontFamily.substring(0, fontFamily.length() - 9);
128                        }
129                        if (fontFamily.endsWith(" Oblique" ) ) {
130                                fontFamily = fontFamily.substring(0, fontFamily.length() - 8);
131                        }
132                        if (fontFamily.endsWith(" Italic" ) ) {
133                                fontFamily = fontFamily.substring(0, fontFamily.length() - 7);
134                        }
135                        if (fontFamily.endsWith(" Bold" ) ) {
136                                fontFamily = fontFamily.substring(0, fontFamily.length() - 5);
137                        }
138                        // NB, in that order, it handles " Bold Italic" and "Bold Oblique" as well.
139                        log.debug("Mapping " + documentStyleId + " to " + physicalFont.getName());
140                       
141                        /* On my Windows box, the following are passed
142                         * to ITextFontResolver, but still not found in its
143                         * _fontFamilies map:
144                         *
145                         *      DejaVu Sans ExtraLight
146                         *      Lucida Sans Demibold
147                         *      Lucida Sans Regular
148                         *      Lucida Bright Demibold
149                         *      Lucida Sans Demibold Roman
150                         *      Lucida Fax Regular
151                         *      Lucida Fax Demibold
152                         */
153                       
154                        return fontFamily;
155                       
156                }
157               
158//              log.info(documentStyleId + " -> " + physicalFont.getName() );
159//             
160//              if (fontFamilyStack) {
161//                     
162//                      // TODO - if this is an HTML document intended
163//                      // for viewing in a web browser, we need to add a
164//                      // font-family cascade (since the true type font
165//                      // specified for PDF purposes won't necessarily be
166//                      // present on web browser's system).
167//                     
168//                      // The easiest way to do it might be to just
169//                      // see whether the substitute font is serif or
170//                      // not, and add cascade entries accordingly.
171//                     
172//                      // If we matched it via FontSubstitutions.xml,
173//                      // maybe that file contains an HTML match as well?
174//                     
175//                      // Either way, this stuff should be worked out in
176//                      // populateFontMappings, and added to the
177//                      // FontMapping objects.
178//                     
179//                      return physicalFont.getName();
180//              } else {
181//                      return physicalFont.getName();
182//              }
183               
184                /*
185                 * We want to return eg "Times New Roman"
186                 * or "Arial Unicode MS" here, ie _with spaces_, since that is
187                 * what xhtmlrender's org.xhtmlrenderer.pdf.ITextFontResolver sets up.
188                 *
189                 *
190                 */
191               
192        }
193       
194}
Note: See TracBrowser for help on using the repository browser.