KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > fonts > FontSetup


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: FontSetup.java 474387 2006-11-13 16:28:13Z jeremias $ */
19
20 package org.apache.fop.fonts;
21
22 // FOP (base 14 fonts)
23
import org.apache.fop.fonts.base14.Helvetica;
24 import org.apache.fop.fonts.base14.HelveticaBold;
25 import org.apache.fop.fonts.base14.HelveticaOblique;
26 import org.apache.fop.fonts.base14.HelveticaBoldOblique;
27 import org.apache.fop.fonts.base14.TimesRoman;
28 import org.apache.fop.fonts.base14.TimesBold;
29 import org.apache.fop.fonts.base14.TimesItalic;
30 import org.apache.fop.fonts.base14.TimesBoldItalic;
31 import org.apache.fop.fonts.base14.Courier;
32 import org.apache.fop.fonts.base14.CourierBold;
33 import org.apache.fop.fonts.base14.CourierOblique;
34 import org.apache.fop.fonts.base14.CourierBoldOblique;
35 import org.apache.fop.fonts.base14.Symbol;
36 import org.apache.fop.fonts.base14.ZapfDingbats;
37
38 // commons logging
39
import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 // Avalon
43
import org.apache.avalon.framework.configuration.Configuration;
44 import org.apache.avalon.framework.configuration.ConfigurationException;
45
46 // Java
47
import java.util.List JavaDoc;
48
49 import javax.xml.transform.Source JavaDoc;
50 import javax.xml.transform.stream.StreamSource JavaDoc;
51
52 /**
53  * Default fonts for FOP application; currently this uses PDF's fonts
54  * by default.
55  *
56  * Assigns the font (with metrics) to internal names like "F1" and
57  * assigns family-style-weight triplets to the fonts
58  */

59 public class FontSetup {
60
61     /**
62      * logging instance
63      */

64     protected static Log log = LogFactory.getLog("org.apache.fop.fonts");
65     
66     /**
67      * Sets up the font info object.
68      *
69      * Adds metrics for basic fonts and useful family-style-weight
70      * triplets for lookup.
71      *
72      * @param fontInfo the font info object to set up
73      * @param embedList a list of EmbedFontInfo objects
74      * @param resolver the font resolver
75      */

76     public static void setup(FontInfo fontInfo, List JavaDoc embedList, FontResolver resolver) {
77         setup(fontInfo, embedList, resolver, false);
78     }
79
80     /**
81      * Sets up the font info object.
82      *
83      * Adds metrics for basic fonts and useful family-style-weight
84      * triplets for lookup.
85      *
86      * @param fontInfo the font info object to set up
87      * @param embedList a list of EmbedFontInfo objects
88      * @param resolver the font resolver
89      * @param enableBase14Kerning true if kerning should be enabled for base 14 fonts
90      */

91     public static void setup(FontInfo fontInfo, List JavaDoc embedList, FontResolver resolver,
92             boolean enableBase14Kerning) {
93
94         fontInfo.addMetrics("F1", new Helvetica(enableBase14Kerning));
95         fontInfo.addMetrics("F2", new HelveticaOblique(enableBase14Kerning));
96         fontInfo.addMetrics("F3", new HelveticaBold(enableBase14Kerning));
97         fontInfo.addMetrics("F4", new HelveticaBoldOblique(enableBase14Kerning));
98         fontInfo.addMetrics("F5", new TimesRoman(enableBase14Kerning));
99         fontInfo.addMetrics("F6", new TimesItalic(enableBase14Kerning));
100         fontInfo.addMetrics("F7", new TimesBold(enableBase14Kerning));
101         fontInfo.addMetrics("F8", new TimesBoldItalic(enableBase14Kerning));
102         fontInfo.addMetrics("F9", new Courier(enableBase14Kerning));
103         fontInfo.addMetrics("F10", new CourierOblique(enableBase14Kerning));
104         fontInfo.addMetrics("F11", new CourierBold(enableBase14Kerning));
105         fontInfo.addMetrics("F12", new CourierBoldOblique(enableBase14Kerning));
106         fontInfo.addMetrics("F13", new Symbol());
107         fontInfo.addMetrics("F14", new ZapfDingbats());
108
109         // Custom type 1 fonts step 1/2
110
// fontInfo.addMetrics("F15", new OMEP());
111
// fontInfo.addMetrics("F16", new GaramondLightCondensed());
112
// fontInfo.addMetrics("F17", new BauerBodoniBoldItalic());
113

114         /* any is treated as serif */
115         fontInfo.addFontProperties("F5", "any", "normal", Font.NORMAL);
116         fontInfo.addFontProperties("F6", "any", "italic", Font.NORMAL);
117         fontInfo.addFontProperties("F6", "any", "oblique", Font.NORMAL);
118         fontInfo.addFontProperties("F7", "any", "normal", Font.BOLD);
119         fontInfo.addFontProperties("F8", "any", "italic", Font.BOLD);
120         fontInfo.addFontProperties("F8", "any", "oblique", Font.BOLD);
121
122         fontInfo.addFontProperties("F1", "sans-serif", "normal", Font.NORMAL);
123         fontInfo.addFontProperties("F2", "sans-serif", "oblique", Font.NORMAL);
124         fontInfo.addFontProperties("F2", "sans-serif", "italic", Font.NORMAL);
125         fontInfo.addFontProperties("F3", "sans-serif", "normal", Font.BOLD);
126         fontInfo.addFontProperties("F4", "sans-serif", "oblique", Font.BOLD);
127         fontInfo.addFontProperties("F4", "sans-serif", "italic", Font.BOLD);
128         fontInfo.addFontProperties("F5", "serif", "normal", Font.NORMAL);
129         fontInfo.addFontProperties("F6", "serif", "oblique", Font.NORMAL);
130         fontInfo.addFontProperties("F6", "serif", "italic", Font.NORMAL);
131         fontInfo.addFontProperties("F7", "serif", "normal", Font.BOLD);
132         fontInfo.addFontProperties("F8", "serif", "oblique", Font.BOLD);
133         fontInfo.addFontProperties("F8", "serif", "italic", Font.BOLD);
134         fontInfo.addFontProperties("F9", "monospace", "normal", Font.NORMAL);
135         fontInfo.addFontProperties("F10", "monospace", "oblique", Font.NORMAL);
136         fontInfo.addFontProperties("F10", "monospace", "italic", Font.NORMAL);
137         fontInfo.addFontProperties("F11", "monospace", "normal", Font.BOLD);
138         fontInfo.addFontProperties("F12", "monospace", "oblique", Font.BOLD);
139         fontInfo.addFontProperties("F12", "monospace", "italic", Font.BOLD);
140
141         fontInfo.addFontProperties("F1", "Helvetica", "normal", Font.NORMAL);
142         fontInfo.addFontProperties("F2", "Helvetica", "oblique", Font.NORMAL);
143         fontInfo.addFontProperties("F2", "Helvetica", "italic", Font.NORMAL);
144         fontInfo.addFontProperties("F3", "Helvetica", "normal", Font.BOLD);
145         fontInfo.addFontProperties("F4", "Helvetica", "oblique", Font.BOLD);
146         fontInfo.addFontProperties("F4", "Helvetica", "italic", Font.BOLD);
147         fontInfo.addFontProperties("F5", "Times", "normal", Font.NORMAL);
148         fontInfo.addFontProperties("F6", "Times", "oblique", Font.NORMAL);
149         fontInfo.addFontProperties("F6", "Times", "italic", Font.NORMAL);
150         fontInfo.addFontProperties("F7", "Times", "normal", Font.BOLD);
151         fontInfo.addFontProperties("F8", "Times", "oblique", Font.BOLD);
152         fontInfo.addFontProperties("F8", "Times", "italic", Font.BOLD);
153         fontInfo.addFontProperties("F9", "Courier", "normal", Font.NORMAL);
154         fontInfo.addFontProperties("F10", "Courier", "oblique", Font.NORMAL);
155         fontInfo.addFontProperties("F10", "Courier", "italic", Font.NORMAL);
156         fontInfo.addFontProperties("F11", "Courier", "normal", Font.BOLD);
157         fontInfo.addFontProperties("F12", "Courier", "oblique", Font.BOLD);
158         fontInfo.addFontProperties("F12", "Courier", "italic", Font.BOLD);
159         fontInfo.addFontProperties("F13", "Symbol", "normal", Font.NORMAL);
160         fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", Font.NORMAL);
161
162         // Custom type 1 fonts step 2/2
163
// fontInfo.addFontProperties("F15", "OMEP", "normal", FontInfo.NORMAL);
164
// fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", FontInfo.NORMAL);
165
// fontInfo.addFontProperties("F17", "BauerBodoni", "italic", FontInfo.BOLD);
166

167         /* for compatibility with PassiveTex */
168         fontInfo.addFontProperties("F5", "Times-Roman", "normal", Font.NORMAL);
169         fontInfo.addFontProperties("F6", "Times-Roman", "oblique", Font.NORMAL);
170         fontInfo.addFontProperties("F6", "Times-Roman", "italic", Font.NORMAL);
171         fontInfo.addFontProperties("F7", "Times-Roman", "normal", Font.BOLD);
172         fontInfo.addFontProperties("F8", "Times-Roman", "oblique", Font.BOLD);
173         fontInfo.addFontProperties("F8", "Times-Roman", "italic", Font.BOLD);
174         fontInfo.addFontProperties("F5", "Times Roman", "normal", Font.NORMAL);
175         fontInfo.addFontProperties("F6", "Times Roman", "oblique", Font.NORMAL);
176         fontInfo.addFontProperties("F6", "Times Roman", "italic", Font.NORMAL);
177         fontInfo.addFontProperties("F7", "Times Roman", "normal", Font.BOLD);
178         fontInfo.addFontProperties("F8", "Times Roman", "oblique", Font.BOLD);
179         fontInfo.addFontProperties("F8", "Times Roman", "italic", Font.BOLD);
180         fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter",
181                                    "normal", Font.NORMAL);
182
183         /* Add configured fonts */
184         addConfiguredFonts(fontInfo, embedList, 15, resolver);
185     }
186
187     /**
188      * Add fonts from configuration file starting with internal name F<num>.
189      * @param fontInfo the font info object to set up
190      * @param fontInfoList a list of EmbedFontInfo objects
191      * @param num starting index for internal font numbering
192      * @param resolver the font resolver
193      */

194     public static void addConfiguredFonts(FontInfo fontInfo, List JavaDoc fontInfoList
195                                         , int num, FontResolver resolver) {
196         if (fontInfoList == null) {
197             return; //No fonts to process
198
}
199
200         if (resolver == null) {
201             //Ensure that we have minimal font resolution capabilities
202
resolver = createMinimalFontResolver();
203         }
204         
205         String JavaDoc internalName = null;
206         //FontReader reader = null;
207

208         for (int i = 0; i < fontInfoList.size(); i++) {
209             EmbedFontInfo configFontInfo = (EmbedFontInfo) fontInfoList.get(i);
210
211             String JavaDoc metricsFile = configFontInfo.getMetricsFile();
212             internalName = "F" + num;
213             num++;
214             /*
215             reader = new FontReader(metricsFile);
216             reader.useKerning(configFontInfo.getKerning());
217             reader.setFontEmbedPath(configFontInfo.getEmbedFile());
218             fontInfo.addMetrics(internalName, reader.getFont());
219             */

220             LazyFont font = new LazyFont(configFontInfo.getEmbedFile(),
221                                          metricsFile,
222                                          configFontInfo.getKerning(),
223                                          resolver);
224             fontInfo.addMetrics(internalName, font);
225
226             List JavaDoc triplets = configFontInfo.getFontTriplets();
227             for (int c = 0; c < triplets.size(); c++) {
228                 FontTriplet triplet = (FontTriplet) triplets.get(c);
229
230                 if (log.isDebugEnabled()) {
231                     log.debug("Registering: " + triplet + " under " + internalName);
232                 }
233                 fontInfo.addFontProperties(internalName, triplet);
234             }
235         }
236     }
237
238     /** @return a new FontResolver to be used by the font subsystem */
239     private static FontResolver createMinimalFontResolver() {
240         return new FontResolver() {
241
242             /** @see org.apache.fop.fonts.FontResolver#resolve(java.lang.String) */
243             public Source JavaDoc resolve(String JavaDoc href) {
244                 //Minimal functionality here
245
return new StreamSource JavaDoc(href);
246             }
247             
248         };
249     }
250     /**
251      * Builds a list of EmbedFontInfo objects for use with the setup() method.
252      * @param cfg Configuration object
253      * @return List the newly created list of fonts
254      * @throws ConfigurationException if something's wrong with the config data
255      */

256     public static List JavaDoc buildFontListFromConfiguration(Configuration cfg)
257             throws ConfigurationException {
258         List JavaDoc fontList = new java.util.ArrayList JavaDoc();
259         Configuration[] font = cfg.getChild("fonts").getChildren("font");
260         for (int i = 0; i < font.length; i++) {
261             Configuration[] triple = font[i].getChildren("font-triplet");
262             List JavaDoc tripleList = new java.util.ArrayList JavaDoc();
263             for (int j = 0; j < triple.length; j++) {
264                 int weight = FontUtil.parseCSS2FontWeight(triple[j].getAttribute("weight"));
265                 tripleList.add(FontInfo.createFontKey(triple[j].getAttribute("name"),
266                                                triple[j].getAttribute("style"),
267                                                weight));
268             }
269
270             EmbedFontInfo efi;
271             efi = new EmbedFontInfo(font[i].getAttribute("metrics-url", null),
272                                     font[i].getAttributeAsBoolean("kerning", false),
273                                     tripleList, font[i].getAttribute("embed-url", null));
274
275             if (log.isDebugEnabled()) {
276                 log.debug("Adding font " + efi.getEmbedFile()
277                           + ", metric file " + efi.getMetricsFile());
278                 for (int j = 0; j < tripleList.size(); ++j) {
279                     FontTriplet triplet = (FontTriplet) tripleList.get(j);
280                     log.debug("Font triplet "
281                               + triplet.getName() + ", "
282                               + triplet.getStyle() + ", "
283                               + triplet.getWeight());
284                 }
285             }
286
287             fontList.add(efi);
288         }
289         return fontList;
290     }
291 }
292
293
Popular Tags