KickJava   Java API By Example, From Geeks To Geeks.

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


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.Font JavaDoc;
21 import java.awt.Shape JavaDoc;
22 import java.awt.font.FontRenderContext JavaDoc;
23 import java.awt.font.GlyphMetrics JavaDoc;
24 import java.awt.font.GlyphVector JavaDoc;
25 import java.awt.font.TextAttribute JavaDoc;
26 import java.awt.geom.AffineTransform JavaDoc;
27 import java.awt.geom.Point2D JavaDoc;
28 import java.awt.geom.Rectangle2D JavaDoc;
29 import java.text.AttributedCharacterIterator JavaDoc;
30 import java.text.CharacterIterator JavaDoc;
31 import java.text.StringCharacterIterator JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Map JavaDoc;
34
35 import org.apache.batik.gvt.text.ArabicTextHandler;
36
37
38 /**
39  * This is a wrapper class for a java.awt.Font instance.
40  *
41  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
42  * @version $Id: AWTGVTFont.java,v 1.15 2004/08/18 07:14:35 vhardy Exp $
43  */

44 public class AWTGVTFont implements GVTFont {
45
46     protected Font JavaDoc awtFont;
47     protected float size;
48     protected float scale;
49
50     /**
51      * Creates a new AWTGVTFont that wraps the given Font.
52      *
53      * @param font The font object to wrap.
54      */

55     public AWTGVTFont(Font JavaDoc font) {
56         this.size = font.getSize2D();
57         this.awtFont = font.deriveFont(FONT_SIZE);
58         this.scale = size/awtFont.getSize2D();
59         initializeFontCache(awtFont);
60     }
61
62     /**
63      * Creates a new AWTGVTFont that wraps the given Font.
64      *
65      * @param font The font object to wrap.
66      * @param scale The scale factor to apply to font...
67      */

68     public AWTGVTFont(Font JavaDoc font, float scale) {
69         this.size = font.getSize2D()*scale;
70         this.awtFont = font.deriveFont(FONT_SIZE);
71         this.scale = size/awtFont.getSize2D();
72         initializeFontCache(awtFont);
73     }
74
75     /**
76      * Creates a new AWTGVTFont with the specified attributes.
77      *
78      * @param attributes Contains attributes of the font to create.
79      */

80     public AWTGVTFont(Map JavaDoc attributes) {
81         Float JavaDoc sz = (Float JavaDoc)attributes.get(TextAttribute.SIZE);
82         if (sz != null) {
83             this.size = sz.floatValue();
84             attributes.put(TextAttribute.SIZE, new Float JavaDoc(FONT_SIZE));
85             this.awtFont = new Font JavaDoc(attributes);
86         } else {
87             this.awtFont = new Font JavaDoc(attributes);
88             this.size = awtFont.getSize2D();
89         }
90         this.scale = size/awtFont.getSize2D();
91         initializeFontCache(awtFont);
92     }
93
94     /**
95      * Creates a new AWTGVTFont from the specified name, style and point size.
96      *
97      * @param name The name of the new font.
98      * @param style The required font style.
99      * @param size The required font size.
100      */

101     public AWTGVTFont(String JavaDoc name, int style, int size) {
102         this.awtFont = new Font JavaDoc(name, style, (int)FONT_SIZE);
103         this.size = size;
104         this.scale = size/awtFont.getSize2D();
105         initializeFontCache(awtFont);
106     }
107
108     /**
109      * Checks if this font can display the specified character.
110      *
111      * @param c The character to check.
112      * @return Whether or not the character can be displayed.
113      */

114     public boolean canDisplay(char c) {
115         return awtFont.canDisplay(c);
116     }
117
118     /**
119      * Indicates whether or not this font can display the characters in the
120      * specified text starting at start and ending at limit.
121      *
122      * @param text An array containing the characters to check.
123      * @param start The index of the first character to check.
124      * @param limit The index of the last character to check.
125      *
126      * @return The index of the first char this font cannot display. Will be
127      * -1 if it can display all characters in the specified range.
128      */

129     public int canDisplayUpTo(char[] text, int start, int limit) {
130         return awtFont.canDisplayUpTo(text, start, limit);
131     }
132
133     /**
134      * Indicates whether or not this font can display the the characters in
135      * the specified CharacterIterator starting at start and ending at limit.
136      */

137     public int canDisplayUpTo(CharacterIterator JavaDoc iter, int start, int limit) {
138         return awtFont.canDisplayUpTo(iter, start, limit);
139     }
140
141     /**
142      * Indicates whether or not this font can display a specified String.
143      */

144     public int canDisplayUpTo(String JavaDoc str) {
145         return awtFont.canDisplayUpTo(str);
146     }
147
148     /**
149      * Returns a new GlyphVector object created with the specified array of
150      * characters and the specified FontRenderContext.
151      */

152     public GVTGlyphVector createGlyphVector(FontRenderContext JavaDoc frc,
153                                             char[] chars) {
154
155         StringCharacterIterator JavaDoc sci =
156             new StringCharacterIterator JavaDoc(new String JavaDoc(chars));
157         GlyphVector JavaDoc gv = awtFont.createGlyphVector(frc, chars);
158         return new AWTGVTGlyphVector(gv, this, scale, sci);
159     }
160
161     /**
162      * Returns a new GlyphVector object created with the specified
163      * CharacterIterator and the specified FontRenderContext.
164      */

165     public GVTGlyphVector createGlyphVector(FontRenderContext JavaDoc frc,
166                                             CharacterIterator JavaDoc ci) {
167
168         if (ci instanceof AttributedCharacterIterator JavaDoc) {
169             AttributedCharacterIterator JavaDoc aci = (AttributedCharacterIterator JavaDoc)ci;
170             if (ArabicTextHandler.containsArabic(aci)) {
171                 String JavaDoc str = ArabicTextHandler.createSubstituteString(aci);
172
173                 return createGlyphVector(frc, str);
174             }
175         }
176         GlyphVector JavaDoc gv = awtFont.createGlyphVector(frc, ci);
177         return new AWTGVTGlyphVector(gv, this, scale, ci);
178     }
179
180     /**
181      * Returns a new GlyphVector object created with the specified integer
182      * array and the specified FontRenderContext.
183      */

184     public GVTGlyphVector createGlyphVector(FontRenderContext JavaDoc frc,
185                                             int[] glyphCodes,
186                                             CharacterIterator JavaDoc ci) {
187         return new AWTGVTGlyphVector
188             (awtFont.createGlyphVector(frc, glyphCodes),
189              this, scale, ci);
190     }
191
192     /**
193      * Returns a new GlyphVector object created with the specified String and
194      * the specified FontRenderContext.
195      */

196     public GVTGlyphVector createGlyphVector(FontRenderContext JavaDoc frc, String JavaDoc str)
197     {
198
199         StringCharacterIterator JavaDoc sci = new StringCharacterIterator JavaDoc(str);
200
201         return new AWTGVTGlyphVector
202             (awtFont.createGlyphVector(frc, str), this, scale, sci);
203     }
204
205     /**
206      * Creates a new Font object by replicating the current Font object and
207      * applying a new size to it.
208      */

209     public GVTFont deriveFont(float size) {
210         return new AWTGVTFont(awtFont, size/this.size);
211     }
212
213     /**
214      * Returns a LineMetrics object created with the specified arguments.
215      */

216     public GVTLineMetrics getLineMetrics(char[] chars,
217                                          int beginIndex,
218                                          int limit,
219                                          FontRenderContext JavaDoc frc) {
220         return new GVTLineMetrics
221             (awtFont.getLineMetrics(chars, beginIndex, limit, frc), scale);
222     }
223
224     /**
225      * Returns a GVTLineMetrics object created with the specified arguments.
226      */

227     public GVTLineMetrics getLineMetrics(CharacterIterator JavaDoc ci,
228                                          int beginIndex,
229                                          int limit,
230                                          FontRenderContext JavaDoc frc) {
231         return new GVTLineMetrics
232             (awtFont.getLineMetrics(ci, beginIndex, limit, frc), scale);
233     }
234
235     /**
236      * Returns a GVTLineMetrics object created with the specified String and
237      * FontRenderContext.
238      */

239     public GVTLineMetrics getLineMetrics(String JavaDoc str, FontRenderContext JavaDoc frc) {
240         return new GVTLineMetrics(awtFont.getLineMetrics(str, frc), scale);
241     }
242
243     /**
244      * Returns a GVTLineMetrics object created with the specified arguments.
245      */

246     public GVTLineMetrics getLineMetrics(String JavaDoc str,
247                                          int beginIndex,
248                                          int limit,
249                                          FontRenderContext JavaDoc frc) {
250         return new GVTLineMetrics
251             (awtFont.getLineMetrics(str, beginIndex, limit, frc), scale);
252     }
253
254     /**
255      * Returns the size of this font.
256      */

257     public float getSize() {
258         return size;
259     }
260
261     /**
262      * Returns the horizontal kerning value for this glyph pair.
263      */

264     public float getHKern(int glyphCode1, int glyphCode2) {
265         return 0f;
266     }
267
268     /**
269      * Returns the vertical kerning value for this glyph pair.
270      */

271     public float getVKern(int glyphCode1, int glyphCode2) {
272         return 0f;
273     }
274
275     /////////////////////////////////////////////////////////////////////////
276

277     public static final float FONT_SIZE = 48f;
278
279     /**
280      * Returns the geometry of the specified character. This method also put
281      * the in cache the geometry associated to the specified character if
282      * needed.
283      */

284     public static
285         AWTGlyphGeometryCache.Value getGlyphGeometry(AWTGVTFont font,
286                                                      char c,
287                                                      GlyphVector JavaDoc gv,
288                                                      int glyphIndex,
289                                                      Point2D JavaDoc glyphPos) {
290
291         AWTGlyphGeometryCache glyphCache =
292             (AWTGlyphGeometryCache)fontCache.get(font.awtFont);
293
294         AWTGlyphGeometryCache.Value v = glyphCache.get(c);
295         if (v == null) {
296             Shape JavaDoc outline = gv.getGlyphOutline(glyphIndex);
297             GlyphMetrics JavaDoc metrics = gv.getGlyphMetrics(glyphIndex);
298             Rectangle2D JavaDoc gmB = metrics.getBounds2D();
299             if (AWTGVTGlyphVector.outlinesPositioned()) {
300                 AffineTransform JavaDoc tr = AffineTransform.getTranslateInstance
301                     (-glyphPos.getX(), -glyphPos.getY());
302                 outline = tr.createTransformedShape(outline);
303             }
304             v = new AWTGlyphGeometryCache.Value(outline, gmB);
305             //System.out.println("put "+font.awtFont+" "+c);
306
glyphCache.put(c, v);
307         }
308         return v;
309     }
310
311     //
312
// static cache for AWTGVTFont
313
//
314

315     static Map JavaDoc fontCache = new HashMap JavaDoc(11);
316
317     static void initializeFontCache(Font JavaDoc awtFont) {
318         if (!fontCache.containsKey(awtFont)) {
319             fontCache.put(awtFont, new AWTGlyphGeometryCache());
320         }
321     }
322
323     static void putAWTGVTFont(AWTGVTFont font) {
324         fontCache.put(font.awtFont, font);
325     }
326
327     static AWTGVTFont getAWTGVTFont(Font JavaDoc awtFont) {
328         return (AWTGVTFont)fontCache.get(awtFont);
329     }
330
331 }
332
333
Popular Tags