KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: FontSetup.java,v 1.3.2.6 2003/02/25 14:39:34 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.awt;
52
53 // FOP
54
import org.apache.fop.messaging.MessageHandler;
55 import org.apache.fop.layout.FontInfo;
56 import org.apache.fop.configuration.Configuration;
57 import org.apache.fop.configuration.FontTriplet;
58 import org.apache.fop.apps.FOPException;
59
60 // Java
61
import java.awt.Graphics2D JavaDoc;
62 import java.util.List JavaDoc;
63 import java.awt.RenderingHints JavaDoc ;
64 import java.net.URL JavaDoc;
65
66 /**
67  * sets up the AWT fonts. It is similar to
68  * org.apache.fop.render.pdf.FontSetup.
69  * Assigns the font (with metrics) to internal names like "F1" and
70  * assigns family-style-weight triplets to the fonts
71  */

72 public class FontSetup {
73
74     /** Regular */
75     private static int normal = java.awt.Font.PLAIN;
76     /** Bold */
77     private static int bold = java.awt.Font.BOLD;
78     /** Italic */
79     private static int italic = java.awt.Font.ITALIC;
80     /** BoldItalic */
81     private static int bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC;
82
83     /**
84      * sets up the font info object.
85      *
86      * adds metrics for basic fonts and useful family-style-weight
87      * triplets for lookup
88      *
89      * @param fontInfo the font info object to set up
90      * @param parent needed, since a live AWT component is needed
91      * to get a valid java.awt.FontMetrics object
92      */

93     public static void setup(FontInfo fontInfo, Graphics2D JavaDoc graphics)
94         throws FOPException {
95
96         FontMetricsMapper metric;
97
98         MessageHandler.logln("setting up fonts");
99
100         // Nov 18, 2002 - [aml/rlc] eliminates layout problems at various scaling
101

102         graphics.setRenderingHint (RenderingHints.KEY_FRACTIONALMETRICS,
103                                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);
104
105
106         /*
107          * available java fonts are:
108          * Serif - bold, normal, italic, bold-italic
109          * SansSerif - bold, normal, italic, bold-italic
110          * MonoSpaced - bold, normal, italic, bold-italic
111          */

112
113         metric = new FontMetricsMapper("SansSerif", normal, graphics);
114         // --> goes to F1
115
fontInfo.addMetrics("F1", metric);
116         metric = new FontMetricsMapper("SansSerif", italic, graphics);
117         // --> goes to F2
118
fontInfo.addMetrics("F2", metric);
119         metric = new FontMetricsMapper("SansSerif", bold, graphics);
120         // --> goes to F3
121
fontInfo.addMetrics("F3", metric);
122         metric = new FontMetricsMapper("SansSerif", bolditalic, graphics);
123         // --> goes to F4
124
fontInfo.addMetrics("F4", metric);
125
126
127         metric = new FontMetricsMapper("Serif", normal, graphics);
128         // --> goes to F5
129
fontInfo.addMetrics("F5", metric);
130         metric = new FontMetricsMapper("Serif", italic, graphics);
131         // --> goes to F6
132
fontInfo.addMetrics("F6", metric);
133         metric = new FontMetricsMapper("Serif", bold, graphics);
134         // --> goes to F7
135
fontInfo.addMetrics("F7", metric);
136         metric = new FontMetricsMapper("Serif", bolditalic, graphics);
137         // --> goes to F8
138
fontInfo.addMetrics("F8", metric);
139
140         metric = new FontMetricsMapper("MonoSpaced", normal, graphics);
141         // --> goes to F9
142
fontInfo.addMetrics("F9", metric);
143         metric = new FontMetricsMapper("MonoSpaced", italic, graphics);
144         // --> goes to F10
145
fontInfo.addMetrics("F10", metric);
146         metric = new FontMetricsMapper("MonoSpaced", bold, graphics);
147         // --> goes to F11
148
fontInfo.addMetrics("F11", metric);
149         metric = new FontMetricsMapper("MonoSpaced", bolditalic, graphics);
150         // --> goes to F12
151
fontInfo.addMetrics("F12", metric);
152
153         metric = new FontMetricsMapper("Symbol", bolditalic, graphics);
154         // --> goes to F13 and F14
155
fontInfo.addMetrics("F13", metric);
156         fontInfo.addMetrics("F14", metric);
157
158         // Custom type 1 fonts step 1/2
159
// fontInfo.addMetrics("F15", new OMEP());
160
// fontInfo.addMetrics("F16", new GaramondLightCondensed());
161
// fontInfo.addMetrics("F17", new BauerBodoniBoldItalic());
162

163         /* any is treated as serif */
164         fontInfo.addFontProperties("F5", "any", "normal", "normal");
165         fontInfo.addFontProperties("F6", "any", "italic", "normal");
166         fontInfo.addFontProperties("F6", "any", "oblique", "normal");
167         fontInfo.addFontProperties("F7", "any", "normal", "bold");
168         fontInfo.addFontProperties("F8", "any", "italic", "bold");
169         fontInfo.addFontProperties("F8", "any", "oblique", "bold");
170
171         fontInfo.addFontProperties("F1", "sans-serif", "normal", "normal");
172         fontInfo.addFontProperties("F2", "sans-serif", "oblique", "normal");
173         fontInfo.addFontProperties("F2", "sans-serif", "italic", "normal");
174         fontInfo.addFontProperties("F3", "sans-serif", "normal", "bold");
175         fontInfo.addFontProperties("F4", "sans-serif", "oblique", "bold");
176         fontInfo.addFontProperties("F4", "sans-serif", "italic", "bold");
177         fontInfo.addFontProperties("F5", "serif", "normal", "normal");
178         fontInfo.addFontProperties("F6", "serif", "oblique", "normal");
179         fontInfo.addFontProperties("F6", "serif", "italic", "normal");
180         fontInfo.addFontProperties("F7", "serif", "normal", "bold");
181         fontInfo.addFontProperties("F8", "serif", "oblique", "bold");
182         fontInfo.addFontProperties("F8", "serif", "italic", "bold");
183         fontInfo.addFontProperties("F9", "monospace", "normal", "normal");
184         fontInfo.addFontProperties("F10", "monospace", "oblique", "normal");
185         fontInfo.addFontProperties("F10", "monospace", "italic", "normal");
186         fontInfo.addFontProperties("F11", "monospace", "normal", "bold");
187         fontInfo.addFontProperties("F12", "monospace", "oblique", "bold");
188         fontInfo.addFontProperties("F12", "monospace", "italic", "bold");
189
190         fontInfo.addFontProperties("F1", "Helvetica", "normal", "normal");
191         fontInfo.addFontProperties("F2", "Helvetica", "oblique", "normal");
192         fontInfo.addFontProperties("F2", "Helvetica", "italic", "normal");
193         fontInfo.addFontProperties("F3", "Helvetica", "normal", "bold");
194         fontInfo.addFontProperties("F4", "Helvetica", "oblique", "bold");
195         fontInfo.addFontProperties("F4", "Helvetica", "italic", "bold");
196         fontInfo.addFontProperties("F5", "Times", "normal", "normal");
197         fontInfo.addFontProperties("F6", "Times", "oblique", "normal");
198         fontInfo.addFontProperties("F6", "Times", "italic", "normal");
199         fontInfo.addFontProperties("F7", "Times", "normal", "bold");
200         fontInfo.addFontProperties("F8", "Times", "oblique", "bold");
201         fontInfo.addFontProperties("F8", "Times", "italic", "bold");
202         fontInfo.addFontProperties("F9", "Courier", "normal", "normal");
203         fontInfo.addFontProperties("F10", "Courier", "oblique", "normal");
204         fontInfo.addFontProperties("F10", "Courier", "italic", "normal");
205         fontInfo.addFontProperties("F11", "Courier", "normal", "bold");
206         fontInfo.addFontProperties("F12", "Courier", "oblique", "bold");
207         fontInfo.addFontProperties("F12", "Courier", "italic", "bold");
208         fontInfo.addFontProperties("F13", "Symbol", "normal", "normal");
209         fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", "normal");
210
211         // Custom type 1 fonts step 2/2
212
// fontInfo.addFontProperties("F15", "OMEP", "normal", "normal");
213
// fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", "normal");
214
// fontInfo.addFontProperties("F17", "BauerBodoni", "italic", "bold");
215

216         /* for compatibility with PassiveTex */
217         fontInfo.addFontProperties("F5", "Times-Roman", "normal", "normal");
218         fontInfo.addFontProperties("F6", "Times-Roman", "oblique", "normal");
219         fontInfo.addFontProperties("F6", "Times-Roman", "italic", "normal");
220         fontInfo.addFontProperties("F7", "Times-Roman", "normal", "bold");
221         fontInfo.addFontProperties("F8", "Times-Roman", "oblique", "bold");
222         fontInfo.addFontProperties("F8", "Times-Roman", "italic", "bold");
223         fontInfo.addFontProperties("F5", "Times Roman", "normal", "normal");
224         fontInfo.addFontProperties("F6", "Times Roman", "oblique", "normal");
225         fontInfo.addFontProperties("F6", "Times Roman", "italic", "normal");
226         fontInfo.addFontProperties("F7", "Times Roman", "normal", "bold");
227         fontInfo.addFontProperties("F8", "Times Roman", "oblique", "bold");
228         fontInfo.addFontProperties("F8", "Times Roman", "italic", "bold");
229         fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter",
230                                    "normal", "normal");
231
232         /* Add configured fonts */
233         addConfiguredFonts(fontInfo, 15, graphics);
234     }
235
236     /**
237      * Add fonts from configuration file starting with
238      * internalnames F<num>
239      * @param fontInfo FontInfo from Configuration
240      * @param num starting index
241      * @param graphics Graphics2D to work on
242      * @throws FOPException in case of an error during font setup
243      */

244     public static void addConfiguredFonts(
245                              FontInfo fontInfo, int num, Graphics2D JavaDoc graphics)
246                              throws FOPException {
247         FontMetricsMapper metric;
248         String JavaDoc internalName = null;
249
250         List JavaDoc fontInfos = Configuration.getFonts();
251         if (fontInfos == null)
252             return;
253
254         for (int i = 0; i < fontInfos.size(); i++) {
255             org.apache.fop.configuration.FontInfo configFontInfo =
256                 (org.apache.fop.configuration.FontInfo)fontInfos.get(i);
257
258             try {
259                 URL JavaDoc metricsFile = configFontInfo.getMetricsFile();
260                 if (metricsFile != null) {
261                     internalName = "F" + num;
262                     num++;
263
264                     List JavaDoc triplets = configFontInfo.getFontTriplets();
265                     for (int j = 0; j < triplets.size(); j++) {
266                         FontTriplet triplet = (FontTriplet)triplets.get(j);
267                         boolean embed = configFontInfo.getEmbedFile() != null;
268                         // if embed font is not specified, use system "Dialog"
269
// logical font name for each Locale.
270
String JavaDoc family = embed ? triplet.getName() : "Dialog";
271                         metric = new FontMetricsMapper(family,
272                                                        getFontMetrics(triplet),
273                                                        graphics);
274                         if (embed)
275                             metric.setEmbedFont(configFontInfo.getEmbedFile());
276                         fontInfo.addMetrics(internalName, metric);
277                         fontInfo.addFontProperties(internalName,
278                                                    triplet.getName(),
279                                                    triplet.getStyle(),
280                                                    triplet.getWeight());
281                     }
282                 }
283             } catch (Exception JavaDoc ex) {
284                 MessageHandler.error("Failed to read font metrics file: "
285                                      + ex.getMessage());
286             }
287         }
288     }
289
290     /**
291      * Return configured font metrics value.
292      *
293      * @param triplet FontTriplet to analyze
294      * @return value indicating font style
295      */

296     private static int getFontMetrics(FontTriplet triplet) {
297         boolean isBold = ("bold".equalsIgnoreCase(triplet.getWeight()));
298         boolean isItalic = ("italic".equalsIgnoreCase(triplet.getStyle()));
299         if (isBold && isItalic) {
300             return bolditalic;
301         } else if (isBold) {
302             return bold;
303         } else if (isItalic) {
304             return italic;
305         }
306         return normal;
307     }
308 }
309
310
311
312
313
314
315
316
317
318
319
Popular Tags