KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joshy > html > css > FontResolverTest


1 package org.joshy.html.css;
2
3 import java.awt.*;
4 import org.joshy.html.Context;
5 import org.joshy.u;
6 import java.util.HashMap JavaDoc;
7
8 public class FontResolverTest extends FontResolver {
9     String JavaDoc[] available_fonts;
10     HashMap JavaDoc instance_hash;
11     HashMap JavaDoc available_fonts_hash;
12     public FontResolverTest() {
13         GraphicsEnvironment gfx = GraphicsEnvironment.getLocalGraphicsEnvironment();
14         String JavaDoc[] available_fonts = gfx.getAvailableFontFamilyNames();
15         //u.p("available fonts =");
16
//u.p(available_fonts);
17
instance_hash = new HashMap JavaDoc();
18         
19         // preload the font map with the font names as keys
20
// don't add the actual font objects because that would be a waste of memory
21
// we will only add them once we need to use them
22
// put empty strings in instead
23
available_fonts_hash = new HashMap JavaDoc();
24         for(int i=0; i<available_fonts.length; i++) {
25             available_fonts_hash.put(available_fonts[i],new String JavaDoc());
26         }
27         
28         // preload sans, serif, and monospace into the available font hash
29
available_fonts_hash.put("Serif",new Font("Serif",Font.PLAIN,1));
30         available_fonts_hash.put("SansSerif",new Font("SansSerif",Font.PLAIN,1));
31         //u.p("put in sans serif");
32
available_fonts_hash.put("Monospaced",new Font("Monospaced",Font.PLAIN,1));
33     }
34     
35     protected Font createFont(Font root_font, float size, String JavaDoc weight, String JavaDoc style) {
36         int font_const = Font.PLAIN;
37         if(weight != null && weight.equals("bold")) {
38             font_const = font_const | Font.BOLD;
39         }
40         if(style != null && style.equals("italic")) {
41             font_const = font_const | Font.ITALIC;
42         }
43         
44         Font fnt = root_font.deriveFont(font_const,size);
45         return fnt;
46     }
47     
48     protected String JavaDoc getFontInstanceHashName(String JavaDoc name, float size, String JavaDoc weight, String JavaDoc style) {
49         return name + "-" + size + "-" + weight + "-" + style;
50     }
51     
52     protected Font resolveFont(Context c, String JavaDoc font, float size, String JavaDoc weight, String JavaDoc style) {
53         // normalize the font name
54
if(font.equals("serif")) {
55             font = "Serif";
56         }
57         if(font.equals("sans-serif")) {
58             font = "SansSerif";
59         }
60         if(font.equals("monospace")) {
61             font = "Monospaced";
62         }
63         
64         
65         // assemble a font instance hash name
66
String JavaDoc font_instance_name = getFontInstanceHashName(font,size,weight,style);
67         //u.p("looking for font: " + font_instance_name);
68
// check if the font instance exists in the hash table
69
if(instance_hash.containsKey(font_instance_name)) {
70             // if so then return it
71
return (Font) instance_hash.get(font_instance_name);
72         }
73         
74         //u.p("font lookup failed");
75
//u.p("searching for : " + font + " " + size + " " + weight + " " + style);
76

77         // if not then
78
// does the font exist
79
if(available_fonts_hash.containsKey(font)) {
80             Object JavaDoc value = available_fonts_hash.get(font);
81             // have we actually allocated the root font object yet?
82
Font root_font = null;
83             if(value instanceof Font) {
84                 root_font = (Font)value;
85             } else {
86                 root_font = new Font(font,Font.PLAIN,1);
87                 available_fonts_hash.put(font,root_font);
88             }
89             
90             // now that we have a root font, we need to create the correct version of it
91
Font fnt = createFont(root_font,size,weight,style);
92             
93             // add the font to the hash so we don't have to do this again
94
instance_hash.put(font_instance_name,fnt);
95             return fnt;
96         }
97         
98         // we didn't find any possible matching font, so just return null
99
return null;
100     }
101     
102     public Font resolveFont(Context c, String JavaDoc[] families, float size, String JavaDoc weight, String JavaDoc style) {
103         
104         // for each font family
105
if(families != null) {
106             for(int i=0; i<families.length; i++) {
107                 Font font = resolveFont(c,families[i],size,weight,style);
108                 if(font != null) { return font; }
109             }
110         }
111         
112         // if we get here then no font worked, so just return default sans
113
//u.p("pulling out: -" + available_fonts_hash.get("SansSerif") + "-");
114
try {
115         Font fnt = createFont((Font)available_fonts_hash.get("SansSerif"),size,weight,style);
116         instance_hash.put(getFontInstanceHashName("SansSerif",size,weight,style),fnt);
117         //u.p("subbing in base sans : " + fnt);
118
return fnt;
119         } catch (Exception JavaDoc ex) {
120             u.p("exception: " + ex);
121             return c.getGraphics().getFont();
122         }
123
124     }
125 }
126
127
Popular Tags