KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > gvt > font > FontFamilyResolver


1 /*
2
3    Copyright 2001-2003 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.batik.gvt.font;
19
20 import java.awt.GraphicsEnvironment JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.StringTokenizer JavaDoc;
26 import java.util.Vector JavaDoc;
27
28 /**
29  * The is a utility class that is used for resolving UnresolvedFontFamilies.
30  *
31  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
32  * @version $Id: FontFamilyResolver.java,v 1.14 2005/03/27 08:58:34 cam Exp $
33  */

34 public class FontFamilyResolver {
35
36     /**
37      * The default font. This will be used when no font families can
38      * be resolved for a particular text chunck/run.
39      */

40     public final static AWTFontFamily defaultFont =
41         new AWTFontFamily("SansSerif");
42
43     /**
44      * List of all available fonts on the current system, plus a few common
45      * alternatives.
46      */

47     protected final static Map JavaDoc fonts = new HashMap JavaDoc();
48
49     protected final static Vector JavaDoc awtFontFamilies = new Vector JavaDoc();
50     protected final static Vector JavaDoc awtFonts = new Vector JavaDoc();
51
52     /**
53      * This sets up the list of available fonts.
54      */

55     static {
56         fonts.put("sans-serif", "SansSerif");
57         fonts.put("serif", "Serif");
58         fonts.put("times", "Serif");
59         fonts.put("times new roman", "Serif");
60         fonts.put("cursive", "Dialog");
61         fonts.put("fantasy", "Symbol");
62         fonts.put("monospace", "Monospaced");
63         fonts.put("monospaced", "Monospaced");
64         fonts.put("courier", "Monospaced");
65
66         //
67
// Load all fonts. Work around
68
//
69

70         GraphicsEnvironment JavaDoc env;
71         env = GraphicsEnvironment.getLocalGraphicsEnvironment();
72         String JavaDoc fontNames[] = env.getAvailableFontFamilyNames();
73
74         int nFonts = fontNames != null ? fontNames.length : 0;
75         for(int i=0; i<nFonts; i++){
76             fonts.put(fontNames[i].toLowerCase(), fontNames[i]);
77
78             // also add the font name with the spaces removed
79
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(fontNames[i]);
80             String JavaDoc fontNameWithoutSpaces = "";
81             while (st.hasMoreTokens()) {
82                 fontNameWithoutSpaces += st.nextToken();
83             }
84             fonts.put(fontNameWithoutSpaces.toLowerCase(), fontNames[i]);
85
86             // also add the font name with spaces replaced by dashes
87
String JavaDoc fontNameWithDashes = fontNames[i].replace(' ', '-');
88             if (!fontNameWithDashes.equals(fontNames[i])) {
89                fonts.put(fontNameWithDashes.toLowerCase(), fontNames[i]);
90             }
91         }
92
93         // first add the default font
94
awtFontFamilies.add(defaultFont);
95         awtFonts.add(new AWTGVTFont(defaultFont.getFamilyName(), 0, 12));
96
97         Collection JavaDoc fontValues = fonts.values();
98         Iterator JavaDoc iter = fontValues.iterator();
99         while(iter.hasNext()) {
100             String JavaDoc fontFamily = (String JavaDoc)iter.next();
101             AWTFontFamily awtFontFamily = new AWTFontFamily(fontFamily);
102             awtFontFamilies.add(awtFontFamily);
103             AWTGVTFont font = new AWTGVTFont(fontFamily, 0, 12);
104             awtFonts.add(font);
105         }
106
107     }
108
109     /**
110      * This keeps track of all the resolved font families. This is to hopefully
111      * reduce the number of font family objects used.
112      */

113     protected static Map JavaDoc resolvedFontFamilies;
114
115     /**
116      * Looks up a font family name and returns the platform name
117      * for the font.
118      *
119      * @param familyName The Font Family name to resolve
120      *
121      * @return The platform name for the font or null if it can't be found.
122      */

123     public static String JavaDoc lookup(String JavaDoc familyName) {
124         return (String JavaDoc)fonts.get(familyName.toLowerCase());
125     }
126
127     /**
128      * Resolves a font family name into a GVTFontFamily. If the font
129      * family cannot be resolved then null will be returned.
130      *
131      * @param familyName The Font Family name to resolve
132      *
133      * @return A resolved GVTFontFamily or null if the font family could not
134      * be resolved.
135      */

136     public static GVTFontFamily resolve(String JavaDoc familyName) {
137         if (resolvedFontFamilies == null) {
138             resolvedFontFamilies = new HashMap JavaDoc();
139         }
140
141         // first see if this font family has already been resolved
142
GVTFontFamily resolvedFF =
143             (GVTFontFamily)resolvedFontFamilies.get(familyName.toLowerCase());
144
145         if (resolvedFF == null) { // hasn't been resolved yet
146
// try to find a matching family name in the list of
147
// available fonts
148
String JavaDoc awtFamilyName = (String JavaDoc)fonts.get(familyName.toLowerCase());
149             if (awtFamilyName != null) {
150                 resolvedFF = new AWTFontFamily(awtFamilyName);
151             }
152
153             resolvedFontFamilies.put(familyName.toLowerCase(), resolvedFF);
154         }
155
156         // if (resolvedFF != null) {
157
// System.out.println("resolved " + fontFamily.getFamilyName() +
158
// " to " + resolvedFF.getFamilyName());
159
// } else {
160
// System.out.println("could not resolve " +
161
// fontFamily.getFamilyName());
162
// }
163
return resolvedFF;
164     }
165
166     /**
167      * Resolves an UnresolvedFontFamily into a GVTFontFamily. If the font
168      * family cannot be resolved then null will be returned.
169      *
170      * @param fontFamily The UnresolvedFontFamily to resolve
171      *
172      * @return A resolved GVTFontFamily or null if the font family could not
173      * be resolved.
174      */

175     public static GVTFontFamily resolve(UnresolvedFontFamily fontFamily) {
176
177         return resolve(fontFamily.getFamilyName());
178     }
179
180     public static GVTFontFamily getFamilyThatCanDisplay(char c) {
181         for (int i = 0; i < awtFontFamilies.size(); i++) {
182             AWTFontFamily fontFamily = (AWTFontFamily)awtFontFamilies.get(i);
183             AWTGVTFont font = (AWTGVTFont)awtFonts.get(i);
184             if (font.canDisplay(c) && fontFamily.getFamilyName().indexOf("Song") == -1) {
185                 // the awt font for "MS Song" doesn't display chinese glyphs correctly
186
return fontFamily;
187             }
188         }
189
190         return null;
191     }
192
193 }
194
Popular Tags