KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > html > style > FontStyleRenderState


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 package org.lobobrowser.html.style;
23
24 import java.awt.Font JavaDoc;
25 import java.awt.FontMetrics JavaDoc;
26 import java.awt.Toolkit JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 public class FontStyleRenderState extends RenderStateDelegator {
31     private final int style;
32     
33     public FontStyleRenderState(RenderState prevRenderState, int style) {
34         super(prevRenderState);
35         this.style = style;
36     }
37
38     private Font JavaDoc iFont;
39     
40     public Font JavaDoc getFont() {
41         Font JavaDoc f = this.iFont;
42         if(f != null) {
43             return f;
44         }
45         Font JavaDoc parentFont = this.prevRenderState.getFont();
46         f = parentFont.deriveFont(this.style | parentFont.getStyle());
47         this.iFont = f;
48         return f;
49     }
50
51     private FontMetrics JavaDoc iFontMetrics;
52     
53     public FontMetrics JavaDoc getFontMetrics() {
54         FontMetrics JavaDoc fm = this.iFontMetrics;
55         if(fm == null) {
56             //TODO getFontMetrics deprecated. How to get text width?
57
fm = Toolkit.getDefaultToolkit().getFontMetrics(this.getFont());
58             this.iFontMetrics = fm;
59         }
60         return fm;
61     }
62
63     public void invalidate() {
64         this.prevRenderState.invalidate();
65         this.iFont = null;
66         this.iFontMetrics = null;
67         Map JavaDoc map = this.iWordInfoMap;
68         if(map != null) {
69             map.clear();
70         }
71     }
72
73     Map JavaDoc iWordInfoMap = null;
74     
75     public final WordInfo getWordInfo(String JavaDoc word) {
76         // Expected to be called only in the GUI (rendering) thread.
77
// No synchronization necessary.
78
Map JavaDoc map = this.iWordInfoMap;
79         if(map == null) {
80             map = new HashMap JavaDoc(1);
81             this.iWordInfoMap = map;
82         }
83         WordInfo wi = (WordInfo) map.get(word);
84         if(wi != null) {
85             return wi;
86         }
87         wi = new WordInfo();
88         FontMetrics JavaDoc fm = this.getFontMetrics();
89         wi.fontMetrics = fm;
90         wi.ascentPlusLeading = fm.getAscent() + fm.getLeading();
91         wi.descent = fm.getDescent();
92         wi.height = fm.getHeight();
93         wi.width = fm.stringWidth(word);
94         map.put(word, wi);
95         return wi;
96     }
97 }
98
Popular Tags