KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > java2d > 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 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.java2d;
21
22 // FOP
23
import org.apache.commons.logging.Log;
24 import org.apache.commons.logging.LogFactory;
25 import org.apache.fop.fonts.FontInfo;
26 import org.apache.fop.fonts.Font;
27 import org.apache.fop.fonts.FontTriplet;
28
29 // Java
30
import java.awt.Graphics2D JavaDoc;
31 import java.awt.GraphicsEnvironment JavaDoc;
32 import java.util.Set JavaDoc;
33
34 /**
35  * Sets up the Java2D/AWT fonts. It is similar to
36  * org.apache.fop.render.fonts.FontSetup.
37  * Assigns the font (with metrics) to internal names like "F1" and
38  * assigns family-style-weight triplets to the fonts.
39  */

40 public class FontSetup {
41
42     /** logging instance */
43     protected static Log log = LogFactory.getLog(FontSetup.class);
44     
45     private static final int LAST_PREDEFINED_FONT_NUMBER = 14;
46
47     private static final Set JavaDoc HARDCODED_FONT_NAMES;
48     
49     static {
50         HARDCODED_FONT_NAMES = new java.util.HashSet JavaDoc();
51         HARDCODED_FONT_NAMES.add("any");
52         HARDCODED_FONT_NAMES.add("sans-serif");
53         HARDCODED_FONT_NAMES.add("serif");
54         HARDCODED_FONT_NAMES.add("monospace");
55         
56         HARDCODED_FONT_NAMES.add("Helvetica");
57         HARDCODED_FONT_NAMES.add("Times");
58         HARDCODED_FONT_NAMES.add("Courier");
59         HARDCODED_FONT_NAMES.add("Symbol");
60         HARDCODED_FONT_NAMES.add("ZapfDingbats");
61         HARDCODED_FONT_NAMES.add("Times Roman");
62         HARDCODED_FONT_NAMES.add("Times-Roman");
63         HARDCODED_FONT_NAMES.add("Computer-Modern-Typewriter");
64     }
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 graphics needed for acces to font metrics
74      */

75     public static void setup(FontInfo fontInfo, Graphics2D JavaDoc graphics) {
76         FontMetricsMapper metric;
77         int normal, bold, bolditalic, italic;
78
79         /*
80          * available java fonts are:
81          * Serif - bold, normal, italic, bold-italic
82          * SansSerif - bold, normal, italic, bold-italic
83          * MonoSpaced - bold, normal, italic, bold-italic
84          */

85         normal = java.awt.Font.PLAIN;
86         bold = java.awt.Font.BOLD;
87         italic = java.awt.Font.ITALIC;
88         bolditalic = java.awt.Font.BOLD + java.awt.Font.ITALIC;
89
90         metric = new FontMetricsMapper("SansSerif", normal, graphics);
91         // --> goes to F1
92
fontInfo.addMetrics("F1", metric);
93         metric = new FontMetricsMapper("SansSerif", italic, graphics);
94         // --> goes to F2
95
fontInfo.addMetrics("F2", metric);
96         metric = new FontMetricsMapper("SansSerif", bold, graphics);
97         // --> goes to F3
98
fontInfo.addMetrics("F3", metric);
99         metric = new FontMetricsMapper("SansSerif", bolditalic, graphics);
100         // --> goes to F4
101
fontInfo.addMetrics("F4", metric);
102
103
104         metric = new FontMetricsMapper("Serif", normal, graphics);
105         // --> goes to F5
106
fontInfo.addMetrics("F5", metric);
107         metric = new FontMetricsMapper("Serif", italic, graphics);
108         // --> goes to F6
109
fontInfo.addMetrics("F6", metric);
110         metric = new FontMetricsMapper("Serif", bold, graphics);
111         // --> goes to F7
112
fontInfo.addMetrics("F7", metric);
113         metric = new FontMetricsMapper("Serif", bolditalic, graphics);
114         // --> goes to F8
115
fontInfo.addMetrics("F8", metric);
116
117         metric = new FontMetricsMapper("MonoSpaced", normal, graphics);
118         // --> goes to F9
119
fontInfo.addMetrics("F9", metric);
120         metric = new FontMetricsMapper("MonoSpaced", italic, graphics);
121         // --> goes to F10
122
fontInfo.addMetrics("F10", metric);
123         metric = new FontMetricsMapper("MonoSpaced", bold, graphics);
124         // --> goes to F11
125
fontInfo.addMetrics("F11", metric);
126         metric = new FontMetricsMapper("MonoSpaced", bolditalic, graphics);
127         // --> goes to F12
128
fontInfo.addMetrics("F12", metric);
129
130         metric = new FontMetricsMapper("Serif", normal, graphics);
131         //"Symbol" doesn't seem to work here, but "Serif" does the job just fine. *shrug*
132
// --> goes to F13 and F14
133
fontInfo.addMetrics("F13", metric);
134         fontInfo.addMetrics("F14", metric);
135
136         // Custom type 1 fonts step 1/2
137
// fontInfo.addMetrics("F15", new OMEP());
138
// fontInfo.addMetrics("F16", new GaramondLightCondensed());
139
// fontInfo.addMetrics("F17", new BauerBodoniBoldItalic());
140

141         /* any is treated as serif */
142         fontInfo.addFontProperties("F5", "any", "normal", Font.NORMAL);
143         fontInfo.addFontProperties("F6", "any", "italic", Font.NORMAL);
144         fontInfo.addFontProperties("F6", "any", "oblique", Font.NORMAL);
145         fontInfo.addFontProperties("F7", "any", "normal", Font.BOLD);
146         fontInfo.addFontProperties("F8", "any", "italic", Font.BOLD);
147         fontInfo.addFontProperties("F8", "any", "oblique", Font.BOLD);
148
149         fontInfo.addFontProperties("F1", "sans-serif", "normal", Font.NORMAL);
150         fontInfo.addFontProperties("F2", "sans-serif", "oblique", Font.NORMAL);
151         fontInfo.addFontProperties("F2", "sans-serif", "italic", Font.NORMAL);
152         fontInfo.addFontProperties("F3", "sans-serif", "normal", Font.BOLD);
153         fontInfo.addFontProperties("F4", "sans-serif", "oblique", Font.BOLD);
154         fontInfo.addFontProperties("F4", "sans-serif", "italic", Font.BOLD);
155         fontInfo.addFontProperties("F5", "serif", "normal", Font.NORMAL);
156         fontInfo.addFontProperties("F6", "serif", "oblique", Font.NORMAL);
157         fontInfo.addFontProperties("F6", "serif", "italic", Font.NORMAL);
158         fontInfo.addFontProperties("F7", "serif", "normal", Font.BOLD);
159         fontInfo.addFontProperties("F8", "serif", "oblique", Font.BOLD);
160         fontInfo.addFontProperties("F8", "serif", "italic", Font.BOLD);
161         fontInfo.addFontProperties("F9", "monospace", "normal", Font.NORMAL);
162         fontInfo.addFontProperties("F10", "monospace", "oblique", Font.NORMAL);
163         fontInfo.addFontProperties("F10", "monospace", "italic", Font.NORMAL);
164         fontInfo.addFontProperties("F11", "monospace", "normal", Font.BOLD);
165         fontInfo.addFontProperties("F12", "monospace", "oblique", Font.BOLD);
166         fontInfo.addFontProperties("F12", "monospace", "italic", Font.BOLD);
167
168         fontInfo.addFontProperties("F1", "Helvetica", "normal", Font.NORMAL);
169         fontInfo.addFontProperties("F2", "Helvetica", "oblique", Font.NORMAL);
170         fontInfo.addFontProperties("F2", "Helvetica", "italic", Font.NORMAL);
171         fontInfo.addFontProperties("F3", "Helvetica", "normal", Font.BOLD);
172         fontInfo.addFontProperties("F4", "Helvetica", "oblique", Font.BOLD);
173         fontInfo.addFontProperties("F4", "Helvetica", "italic", Font.BOLD);
174         fontInfo.addFontProperties("F5", "Times", "normal", Font.NORMAL);
175         fontInfo.addFontProperties("F6", "Times", "oblique", Font.NORMAL);
176         fontInfo.addFontProperties("F6", "Times", "italic", Font.NORMAL);
177         fontInfo.addFontProperties("F7", "Times", "normal", Font.BOLD);
178         fontInfo.addFontProperties("F8", "Times", "oblique", Font.BOLD);
179         fontInfo.addFontProperties("F8", "Times", "italic", Font.BOLD);
180         fontInfo.addFontProperties("F9", "Courier", "normal", Font.NORMAL);
181         fontInfo.addFontProperties("F10", "Courier", "oblique", Font.NORMAL);
182         fontInfo.addFontProperties("F10", "Courier", "italic", Font.NORMAL);
183         fontInfo.addFontProperties("F11", "Courier", "normal", Font.BOLD);
184         fontInfo.addFontProperties("F12", "Courier", "oblique", Font.BOLD);
185         fontInfo.addFontProperties("F12", "Courier", "italic", Font.BOLD);
186         fontInfo.addFontProperties("F13", "Symbol", "normal", Font.NORMAL);
187         fontInfo.addFontProperties("F14", "ZapfDingbats", "normal", Font.NORMAL);
188
189         // Custom type 1 fonts step 2/2
190
// fontInfo.addFontProperties("F15", "OMEP", "normal", FontInfo.NORMAL);
191
// fontInfo.addFontProperties("F16", "Garamond-LightCondensed", "normal", FontInfo.NORMAL);
192
// fontInfo.addFontProperties("F17", "BauerBodoni", "italic", FontInfo.BOLD);
193

194         /* for compatibility with PassiveTex */
195         fontInfo.addFontProperties("F5", "Times-Roman", "normal", Font.NORMAL);
196         fontInfo.addFontProperties("F6", "Times-Roman", "oblique", Font.NORMAL);
197         fontInfo.addFontProperties("F6", "Times-Roman", "italic", Font.NORMAL);
198         fontInfo.addFontProperties("F7", "Times-Roman", "normal", Font.BOLD);
199         fontInfo.addFontProperties("F8", "Times-Roman", "oblique", Font.BOLD);
200         fontInfo.addFontProperties("F8", "Times-Roman", "italic", Font.BOLD);
201         fontInfo.addFontProperties("F5", "Times Roman", "normal", Font.NORMAL);
202         fontInfo.addFontProperties("F6", "Times Roman", "oblique", Font.NORMAL);
203         fontInfo.addFontProperties("F6", "Times Roman", "italic", Font.NORMAL);
204         fontInfo.addFontProperties("F7", "Times Roman", "normal", Font.BOLD);
205         fontInfo.addFontProperties("F8", "Times Roman", "oblique", Font.BOLD);
206         fontInfo.addFontProperties("F8", "Times Roman", "italic", Font.BOLD);
207         fontInfo.addFontProperties("F9", "Computer-Modern-Typewriter",
208                                    "normal", Font.NORMAL);
209         
210         configureInstalledAWTFonts(fontInfo, graphics, LAST_PREDEFINED_FONT_NUMBER + 1);
211     }
212
213     private static void configureInstalledAWTFonts(FontInfo fontInfo, Graphics2D JavaDoc graphics,
214             int startNumber) {
215         int num = startNumber;
216         GraphicsEnvironment JavaDoc env = GraphicsEnvironment.getLocalGraphicsEnvironment();
217         String JavaDoc[] allFontFamilies = env.getAvailableFontFamilyNames();
218         for (int i = 0; i < allFontFamilies.length; i++) {
219             String JavaDoc family = allFontFamilies[i];
220             if (HARDCODED_FONT_NAMES.contains(family)) {
221                 continue; //skip
222
}
223
224             if (log.isDebugEnabled()) {
225                 log.debug("Registering: " + family);
226             }
227             
228             //Java does not give info about what variants of a font is actually supported, so
229
//we simply register all the basic variants. If we use GraphicsEnvironment.getAllFonts()
230
//we don't get reliable info whether a font is italic or bold or both.
231
int fontStyle;
232             fontStyle = java.awt.Font.PLAIN;
233             registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
234             num++;
235
236             fontStyle = java.awt.Font.ITALIC;
237             registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
238             num++;
239
240             fontStyle = java.awt.Font.BOLD;
241             registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
242             num++;
243
244             fontStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC;
245             registerFontTriplet(fontInfo, family, fontStyle, "F" + num, graphics);
246             num++;
247         }
248     }
249
250     private static void registerFontTriplet(FontInfo fontInfo, String JavaDoc family, int fontStyle,
251             String JavaDoc fontKey, Graphics2D JavaDoc graphics) {
252         FontMetricsMapper metric = new FontMetricsMapper(family, fontStyle, graphics);
253         fontInfo.addMetrics(fontKey, metric);
254         
255         int weight = Font.NORMAL;
256         if ((fontStyle & java.awt.Font.BOLD) != 0) {
257             weight = Font.BOLD;
258         }
259         String JavaDoc style = "normal";
260         if ((fontStyle & java.awt.Font.ITALIC) != 0) {
261             style = "italic";
262         }
263         FontTriplet triplet = FontInfo.createFontKey(family, style, weight);
264         fontInfo.addFontProperties(fontKey, triplet);
265     }
266     
267 }
268
269
Popular Tags