KickJava   Java API By Example, From Geeks To Geeks.

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


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