KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pdf > FontSetup


1 /*
2  * $Id: FontSetup.java,v 1.13.2.4 2003/02/25 14:58:08 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.render.pdf;
52
53 // FOP
54
import org.apache.fop.render.pdf.fonts.*;
55 import org.apache.fop.messaging.MessageHandler;
56 import org.apache.fop.layout.FontInfo;
57 import org.apache.fop.layout.FontDescriptor;
58 import org.apache.fop.pdf.PDFDocument;
59 import org.apache.fop.pdf.PDFResources;
60 import org.apache.fop.configuration.Configuration;
61 import org.apache.fop.configuration.FontTriplet;
62 import org.apache.fop.apps.FOPException;
63
64 // Java
65
import java.util.Iterator JavaDoc;
66 import java.util.Map JavaDoc;
67 import java.util.List JavaDoc;
68 import java.net.URL JavaDoc;
69
70 /**
71  * sets up the PDF fonts.
72  *
73  * Assigns the font (with metrics) to internal names like "F1" and
74  * assigns family-style-weight triplets to the fonts
75  */

76 public class FontSetup {
77
78     /**
79      * sets up the font info object.
80      *
81      * adds metrics for basic fonts and useful family-style-weight
82      * triplets for lookup
83      *
84      * @param fontInfo the font info object to set up
85      */

86     public static void setup(FontInfo fontInfo) throws FOPException {
87         MessageHandler.logln("setting up fonts");
88
89         fontInfo.addMetrics("F1", new Helvetica());
90         fontInfo.addMetrics("F2", new HelveticaOblique());
91         fontInfo.addMetrics("F3", new HelveticaBold());
92         fontInfo.addMetrics("F4", new HelveticaBoldOblique());
93         fontInfo.addMetrics("F5", new TimesRoman());
94         fontInfo.addMetrics("F6", new TimesItalic());
95         fontInfo.addMetrics("F7", new TimesBold());
96         fontInfo.addMetrics("F8", new TimesBoldItalic());
97         fontInfo.addMetrics("F9", new Courier());
98         fontInfo.addMetrics("F10", new CourierOblique());
99         fontInfo.addMetrics("F11", new CourierBold());
100         fontInfo.addMetrics("F12", new CourierBoldOblique());
101         fontInfo.addMetrics("F13", new Symbol());
102         fontInfo.addMetrics("F14", new ZapfDingbats());
103
104         // Custom type 1 fonts step 1/2
105
// fontInfo.addMetrics("F15", new OMEP());
106
// fontInfo.addMetrics("F16", new GaramondLightCondensed());
107
// fontInfo.addMetrics("F17", new BauerBodoniBoldItalic());
108

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

162         /* for compatibility with PassiveTex */
163         fontInfo.addFontProperties("F5", "Times-Roman", "normal", "normal");
164         fontInfo.addFontProperties("F6", "Times-Roman", "oblique", "normal");
165         fontInfo.addFontProperties("F6", "Times-Roman", "italic", "normal");
166         fontInfo.addFontProperties("F7", "Times-Roman", "normal", "bold");
167         fontInfo.addFontProperties("F8", "Times-Roman", "oblique", "bold");
168         fontInfo.addFontProperties("F8", "Times-Roman", "italic", "bold");
169         fontInfo.addFontProperties("F5", "Times Roman", "normal", "normal");
170         fontInfo.addFontProperties("F6", "Times Roman", "oblique", "normal");
171         fontInfo.addFontProperties("F6", "Times Roman", "italic", "normal");
172         fontInfo.addFontProperties("F7", "Times Roman", "normal", "bold");
173         fontInfo.addFontProperties("F8", "Times Roman", "oblique", "bold");
174         fontInfo.addFontProperties("F8", "Times Roman", "italic", "bold");
175         fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter",
176                                    "normal", "normal");
177
178         /* Add configured fonts */
179         addConfiguredFonts(fontInfo, 15);
180     }
181
182     /**
183      * Add fonts from configuration file starting with
184      * internalnames F<num>
185      */

186     public static void addConfiguredFonts(FontInfo fontInfo, int num)
187         throws FOPException {
188
189         String JavaDoc internalName = null;
190         FontReader reader = null;
191
192         List JavaDoc fontInfos = Configuration.getFonts();
193         if (fontInfos == null)
194             return;
195
196         for (int i = 0; i < fontInfos.size(); i++) {
197             org.apache.fop.configuration.FontInfo configFontInfo =
198                 (org.apache.fop.configuration.FontInfo)fontInfos.get(i);
199
200             try {
201                 URL JavaDoc metricsFile = configFontInfo.getMetricsFile();
202                 if (metricsFile != null) {
203                     internalName = "F" + num;
204                     num++;
205                     /*
206                     reader = new FontReader(metricsFile);
207                     reader.useKerning(configFontInfo.getKerning());
208                     reader.setFontEmbedPath(configFontInfo.getEmbedFile());
209                     fontInfo.addMetrics(internalName, reader.getFont());
210                     */

211                     LazyFont font = new LazyFont(configFontInfo.getEmbedFile(),
212                                                  metricsFile,
213                                                  configFontInfo.getKerning());
214                     fontInfo.addMetrics(internalName, font);
215
216                     List JavaDoc triplets = configFontInfo.getFontTriplets();
217                     for (int j = 0; j < triplets.size(); j++) {
218                         FontTriplet triplet = (FontTriplet)triplets.get(j);
219
220                         fontInfo.addFontProperties(internalName,
221                                                    triplet.getName(),
222                                                    triplet.getStyle(),
223                                                    triplet.getWeight());
224                     }
225                 }
226             } catch (Exception JavaDoc ex) {
227                 MessageHandler.error("Failed to read a font metrics file: "
228                                       + ex.getMessage());
229             }
230         }
231     }
232
233     /**
234      * add the fonts in the font info to the PDF document
235      *
236      * @param doc PDF document to add fonts to
237      * @param fontInfo font info object to get font information from
238      */

239     public static void addToResources(PDFDocument doc, FontInfo fontInfo) {
240         Map JavaDoc fonts = fontInfo.getUsedFonts();
241         Iterator JavaDoc e = fonts.keySet().iterator();
242         PDFResources resources = doc.getResources();
243         while (e.hasNext()) {
244             String JavaDoc f = (String JavaDoc)e.next();
245             Font font = (Font)fonts.get(f);
246             FontDescriptor desc = null;
247             if (font instanceof FontDescriptor) {
248                 desc = (FontDescriptor)font;
249             }
250             resources.addFont(doc.makeFont(f, font.fontName(),
251                                            font.encoding(), font, desc));
252         }
253     }
254
255 }
256
Popular Tags