KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > util > gui > FontFactory


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 /*
22  * Created on Apr 17, 2005
23  */

24 package org.lobobrowser.util.gui;
25
26 import java.util.*;
27 import java.awt.*;
28 import java.util.logging.*;
29
30 import org.lobobrowser.util.Objects;
31
32 /**
33  * @author J. H. S.
34  */

35 public class FontFactory {
36     private static final Logger logger = Logger.getLogger(FontFactory.class.getName());
37     private static final boolean loggableInfo = logger.isLoggable(Level.INFO);
38     private static final FontFactory instance = new FontFactory();
39     private final Set fontFamilies = new HashSet();
40     private final Map fontMap = new HashMap();
41     
42     /**
43      *
44      */

45     private FontFactory() {
46         boolean liflag = loggableInfo;
47         String JavaDoc[] ffns = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
48         Set fontFamilies = this.fontFamilies;
49         synchronized(this) {
50             for(int i = 0; i < ffns.length; i++) {
51                 if(liflag) {
52                     logger.info("FontFactory(): family=" + ffns[i]);
53                 }
54                 fontFamilies.add(ffns[i].toLowerCase());
55             }
56         }
57     }
58     
59     public static final FontFactory getInstance() {
60         return instance;
61     }
62
63     private final Map registeredFonts = new HashMap(0);
64     
65     /**
66      * Registers a font family. It does not close the stream provided.
67      * Fonts should be registered before the renderer has a chance to
68      * cache document font specifications.
69      * @param fontName The name of a font as it would appear in a font-family specification.
70      * @param fontFormat Should be {@link Font#TRUETYPE_FONT}.
71      */

72     public void registerFont(String JavaDoc fontName, int fontFormat, java.io.InputStream JavaDoc fontStream) throws java.awt.FontFormatException JavaDoc, java.io.IOException JavaDoc {
73         Font f = Font.createFont(fontFormat, fontStream);
74         synchronized(this) {
75             this.registeredFonts.put(fontName.toLowerCase(), f);
76         }
77     }
78     
79     /**
80      * Unregisters a font previously registered with {@link #registerFont(String, int, java.io.InputStream)}.
81      * @param fontName The font name to be removed.
82      */

83     public void unregisterFont(String JavaDoc fontName) {
84         synchronized(this) {
85             this.registeredFonts.remove(fontName.toLowerCase());
86         }
87     }
88     
89     public Font getFont(String JavaDoc fontFamily, String JavaDoc fontStyle, String JavaDoc fontVariant, String JavaDoc fontWeight, float fontSize) {
90         FontKey key = new FontKey(fontFamily, fontStyle, fontVariant, fontWeight, fontSize);
91         synchronized(this) {
92             Font font = (Font) this.fontMap.get(key);
93             if(font == null) {
94                 font = this.createFont(key);
95                 this.fontMap.put(key, font);
96             }
97             return font;
98         }
99     }
100     
101     private final Font createFont(FontKey key) {
102         String JavaDoc fontNames = key.fontFamily;
103         String JavaDoc fontFam = null;
104         Set fontFamilies = this.fontFamilies;
105         Map registeredFonts = this.registeredFonts;
106         Font baseFont = null;
107         if(fontNames != null) {
108             StringTokenizer tok = new StringTokenizer(fontNames, ",");
109             while(tok.hasMoreTokens()) {
110                 fontFam = tok.nextToken().trim();
111                 String JavaDoc fontFamTL = fontFam.toLowerCase();
112                 if(registeredFonts.containsKey(fontFamTL)) {
113                     baseFont = (Font) registeredFonts.get(fontFamTL);
114                     break;
115                 }
116                 else if(fontFamilies.contains(fontFamTL)) {
117                     break;
118                 }
119             }
120         }
121         int fontStyle = Font.PLAIN;
122         if("italic".equalsIgnoreCase(key.fontStyle)) {
123             fontStyle |= Font.ITALIC;
124         }
125         if("bold".equalsIgnoreCase(key.fontWeight) || "bolder".equalsIgnoreCase(key.fontWeight)) {
126             fontStyle |= Font.BOLD;
127         }
128         if(baseFont != null) {
129             return baseFont.deriveFont(fontStyle, key.fontSize);
130         }
131         else {
132             return new Font(fontFam, fontStyle,(int) Math.round(key.fontSize));
133         }
134     }
135     
136     private static class FontKey {
137         public final String JavaDoc fontFamily;
138         public final String JavaDoc fontStyle;
139         public final String JavaDoc fontVariant;
140         public final String JavaDoc fontWeight;
141         public final float fontSize;
142         
143         
144         /**
145          * @param fontFamily
146          * @param fontStyle
147          * @param fontVariant
148          * @param fontWeight
149          * @param fontSize
150          */

151         public FontKey(final String JavaDoc fontFamily, final String JavaDoc fontStyle,
152                 final String JavaDoc fontVariant, final String JavaDoc fontWeight,
153                 final float fontSize) {
154             this.fontFamily = fontFamily;
155             this.fontStyle = fontStyle;
156             this.fontVariant = fontVariant;
157             this.fontWeight = fontWeight;
158             this.fontSize = fontSize;
159         }
160         
161         public boolean equals(Object JavaDoc other) {
162             if(!(other instanceof FontKey)) {
163                 return false;
164             }
165             FontKey ors = (FontKey) other;
166             // fontSize is primitive
167
return this.fontSize == ors.fontSize &&
168                    Objects.equals(this.fontFamily,ors.fontFamily) &&
169                    Objects.equals(this.fontStyle,ors.fontStyle) &&
170                    Objects.equals(this.fontWeight,ors.fontWeight) &&
171                    Objects.equals(this.fontVariant,ors.fontVariant);
172         }
173         
174         public int hashCode() {
175             String JavaDoc ff = this.fontFamily;
176             if(ff == null) {
177                 ff = "";
178             }
179             String JavaDoc fw = this.fontWeight;
180             if(fw == null) {
181                 fw = "";
182             }
183             String JavaDoc fs = this.fontStyle;
184             if(fs == null) {
185                 fs = "";
186             }
187             return ff.hashCode() ^
188                    fw.hashCode() ^
189                    fs.hashCode() ^
190                    (int) this.fontSize;
191         }
192         
193         public String JavaDoc toString() {
194             return "FontKey[family=" + this.fontFamily + ",size=" + this.fontSize + ",style=" + this.fontStyle + ",weight=" + this.fontWeight + ",variant=" + this.fontVariant + "]";
195         }
196     }
197 }
Popular Tags