KickJava   Java API By Example, From Geeks To Geeks.

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


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.Graphics2D JavaDoc;
21 import java.awt.Paint JavaDoc;
22 import java.awt.Shape JavaDoc;
23 import java.awt.Stroke JavaDoc;
24 import java.awt.font.GlyphMetrics JavaDoc;
25 import java.awt.geom.AffineTransform JavaDoc;
26 import java.awt.geom.GeneralPath JavaDoc;
27 import java.awt.geom.Point2D JavaDoc;
28 import java.awt.geom.Rectangle2D JavaDoc;
29 import java.util.Vector JavaDoc;
30
31 import org.apache.batik.gvt.GraphicsNode;
32 import org.apache.batik.gvt.text.ArabicTextHandler;
33 import org.apache.batik.gvt.text.TextPaintInfo;
34
35
36 /**
37  * A Glyph describes a graphics node with some specific glyph rendering
38  * attributes.
39  *
40  * @author <a HREF="mailto:bella.robinson@cmis.csiro.au">Bella Robinson</a>
41  * @version $Id: Glyph.java,v 1.17 2005/03/27 08:58:34 cam Exp $
42  */

43 public class Glyph {
44
45     private String JavaDoc unicode;
46     private Vector JavaDoc names;
47     private String JavaDoc orientation;
48     private String JavaDoc arabicForm;
49     private String JavaDoc lang;
50     private Point2D JavaDoc horizOrigin;
51     private Point2D JavaDoc vertOrigin;
52     private float horizAdvX;
53     private float vertAdvY;
54     private int glyphCode;
55     private AffineTransform JavaDoc transform;
56     private Point2D.Float JavaDoc position;
57     private GVTGlyphMetrics metrics;
58
59     private Shape JavaDoc outline; // cache the glyph outline
60
private Rectangle2D JavaDoc bounds; // cache the glyph bounds
61

62     private TextPaintInfo tpi;
63     private TextPaintInfo cacheTPI;
64     private Shape JavaDoc dShape;
65     private GraphicsNode glyphChildrenNode;
66
67
68     /**
69      * Constructs a Glyph with the specified parameters.
70      */

71     public Glyph(String JavaDoc unicode, Vector JavaDoc names,
72                  String JavaDoc orientation, String JavaDoc arabicForm, String JavaDoc lang,
73                  Point2D JavaDoc horizOrigin, Point2D JavaDoc vertOrigin, float horizAdvX,
74                  float vertAdvY, int glyphCode,
75                  TextPaintInfo tpi,
76                  Shape JavaDoc dShape, GraphicsNode glyphChildrenNode) {
77
78         if (unicode == null) {
79             throw new IllegalArgumentException JavaDoc();
80         }
81         if (horizOrigin == null) {
82             throw new IllegalArgumentException JavaDoc();
83         }
84         if (vertOrigin == null) {
85             throw new IllegalArgumentException JavaDoc();
86         }
87
88         this.unicode = unicode;
89         this.names = names;
90         this.orientation = orientation;
91         this.arabicForm = arabicForm;
92         this.lang = lang;
93         this.horizOrigin = horizOrigin;
94         this.vertOrigin = vertOrigin;
95         this.horizAdvX = horizAdvX;
96         this.vertAdvY = vertAdvY;
97         this.glyphCode = glyphCode;
98         this.position = new Point2D.Float JavaDoc(0,0);
99         this.outline = null;
100         this.bounds = null;
101
102
103         this.tpi = tpi;
104         this.dShape = dShape;
105         this.glyphChildrenNode = glyphChildrenNode;
106     }
107
108     /**
109      * Returns the unicode char or chars this glyph represents.
110      *
111      * @return The glyphs unicode value.
112      */

113     public String JavaDoc getUnicode() {
114         return unicode;
115     }
116
117     /**
118      * Returns the names of this glyph.
119      *
120      * @return The glyph names.
121      */

122     public Vector JavaDoc getNames() {
123         return names;
124     }
125
126     /**
127      * Returns the orientation of this glyph.
128      * Indicates what inline-progression-direction this glyph
129      * can be used in. Should be either "h" for horizontal only, "v" for vertical
130      * only, or empty which indicates that the glpyh can be use in both.
131      *
132      * @return The glyph orientation.
133      */

134     public String JavaDoc getOrientation() {
135         return orientation;
136     }
137
138     /**
139      * Returns which of the four possible arabic forms this glyph represents.
140      * This is only used for arabic glyphs.
141      *
142      * @return The glyphs arabic form.
143      */

144     public String JavaDoc getArabicForm() {
145         return arabicForm;
146     }
147
148     /**
149      * Returns a comma separated list of languages this glyph can be used in.
150      *
151      * @return The glyph languages.
152      */

153     public String JavaDoc getLang() {
154         return lang;
155     }
156
157     /**
158      * Returns the horizontal origin of this glyph.
159      *
160      * @return The horizontal origin.
161      */

162     public Point2D JavaDoc getHorizOrigin() {
163         return horizOrigin;
164     }
165
166     /**
167      * Returns the vertical origin of this glyph.
168      *
169      * @return The vertical origin.
170      */

171     public Point2D JavaDoc getVertOrigin() {
172         return vertOrigin;
173     }
174
175     /**
176      * Returns the horizontal advance value.
177      *
178      * @return This glyph's horizontal advance.
179      */

180     public float getHorizAdvX() {
181         return horizAdvX;
182     }
183
184     /**
185      * Returns the vertical advance value.
186      *
187      * @return the glyph's vertical advance.
188      */

189     public float getVertAdvY() {
190         return vertAdvY;
191     }
192
193     /**
194      * Returns the glyphs unique code with resect to its font. This will be
195      * the index into the font's list of glyphs.
196      *
197      * @return The glyph's unique code.
198      */

199     public int getGlyphCode() {
200         return glyphCode;
201     }
202
203     /**
204      * Returns the glpyh's transform.
205      *
206      * @return The glyph's transform.
207      */

208     public AffineTransform JavaDoc getTransform() {
209         return transform;
210     }
211
212     /**
213      * Sets the transform to be applied to this glyph.
214      *
215      * @param transform The transform to set.
216      */

217     public void setTransform(AffineTransform JavaDoc transform) {
218         this.transform = transform;
219         outline = null;
220         bounds = null;
221     }
222
223     /**
224      * Returns the position of this glyph.
225      *
226      * @return The glyph's position.
227      */

228     public Point2D JavaDoc getPosition() {
229         return position;
230     }
231
232     /**
233      * Sets the position of the glyph.
234      *
235      * @param position The new glyph position.
236      */

237     public void setPosition(Point2D JavaDoc position) {
238         this.position.x = (float)position.getX();
239         this.position.y = (float)position.getY();
240         outline = null;
241         bounds = null;
242     }
243
244     /**
245      * Returns the metrics of this Glyph if it is used in a horizontal layout.
246      *
247      * @return The glyph metrics.
248      */

249     public GVTGlyphMetrics getGlyphMetrics() {
250         if (metrics == null) {
251             Rectangle2D JavaDoc gb = getGeometryBounds();
252             
253             metrics = new GVTGlyphMetrics
254                 (getHorizAdvX(), getVertAdvY(),
255                  new Rectangle2D.Double JavaDoc(gb.getX()-position.getX(),
256                                         gb.getY()-position.getY(),
257                                         gb.getWidth(),gb.getHeight()),
258                  GlyphMetrics.COMPONENT);
259         }
260         return metrics;
261     }
262
263
264     /**
265      * Returns the metics of this Glyph with the specified kerning value
266      * applied.
267      *
268      * @param hkern The horizontal kerning value to apply when calculating
269      * the glyph metrics.
270      * @param vkern The horizontal vertical value to apply when calculating
271      * the glyph metrics.
272      * @return The kerned glyph metics
273      */

274     public GVTGlyphMetrics getGlyphMetrics(float hkern, float vkern) {
275         return new GVTGlyphMetrics(getHorizAdvX() - hkern,
276                                    getVertAdvY() - vkern,
277                                    getGeometryBounds(),
278                                    GlyphMetrics.COMPONENT);
279
280     }
281
282     public Rectangle2D JavaDoc getGeometryBounds() {
283         return getOutline().getBounds2D();
284     }
285
286     public Rectangle2D JavaDoc getBounds2D() {
287         // Check if the TextPaintInfo has changed...
288
if ((bounds != null) &&
289             TextPaintInfo.equivilent(tpi, cacheTPI))
290             return bounds;
291
292         AffineTransform JavaDoc tr =
293             AffineTransform.getTranslateInstance(position.getX(),
294                                                  position.getY());
295         if (transform != null) {
296             tr.concatenate(transform);
297         }
298
299         Rectangle2D JavaDoc bounds = null;
300         if ((dShape != null) && (tpi != null)) {
301             if (tpi.fillPaint != null)
302                 bounds = tr.createTransformedShape(dShape).getBounds2D();
303
304             if ((tpi.strokeStroke != null) && (tpi.strokePaint != null)) {
305                 Shape JavaDoc s = tpi.strokeStroke.createStrokedShape(dShape);
306                 Rectangle2D JavaDoc r = tr.createTransformedShape(s).getBounds2D();
307                 if (bounds == null) bounds = r;
308                 else bounds = r.createUnion(bounds);
309             }
310         }
311
312         if (glyphChildrenNode != null) {
313             Rectangle2D JavaDoc r = glyphChildrenNode.getTransformedBounds(tr);
314             if (bounds == null) bounds = r;
315             else bounds = r.createUnion(bounds);
316         }
317         if (bounds == null)
318             bounds = new Rectangle2D.Double JavaDoc
319                 (position.getX(), position.getY(), 0, 0);
320
321         cacheTPI = new TextPaintInfo(tpi);
322         return bounds;
323     }
324
325     /**
326      * Returns the outline of this glyph. This will be positioned correctly and
327      * any glyph transforms will have been applied.
328      *
329      * @return the outline of this glyph.
330      */

331     public Shape JavaDoc getOutline() {
332         if (outline == null) {
333             AffineTransform JavaDoc tr =
334         AffineTransform.getTranslateInstance(position.getX(),
335                              position.getY());
336             if (transform != null) {
337                 tr.concatenate(transform);
338             }
339             Shape JavaDoc glyphChildrenOutline = null;
340             if (glyphChildrenNode != null) {
341                 glyphChildrenOutline = glyphChildrenNode.getOutline();
342             }
343             GeneralPath JavaDoc glyphOutline = null;
344             if (dShape != null && glyphChildrenOutline != null) {
345                 glyphOutline = new GeneralPath JavaDoc(dShape);
346                 glyphOutline.append(glyphChildrenOutline, false);
347             } else if (dShape != null && glyphChildrenOutline == null) {
348                 glyphOutline = new GeneralPath JavaDoc(dShape);
349             } else if (dShape == null && glyphChildrenOutline != null) {
350                 glyphOutline = new GeneralPath JavaDoc(glyphChildrenOutline);
351             } else {
352                 // must be a whitespace glyph, return an empty shape
353
glyphOutline = new GeneralPath JavaDoc();
354             }
355             outline = tr.createTransformedShape(glyphOutline);
356         }
357         return outline;
358     }
359
360     /**
361      * Draws this glyph.
362      *
363      * @param graphics2D The Graphics2D object to draw to.
364      */

365     public void draw(Graphics2D JavaDoc graphics2D) {
366         AffineTransform JavaDoc tr =
367         AffineTransform.getTranslateInstance(position.getX(),
368                          position.getY());
369         if (transform != null) {
370             tr.concatenate(transform);
371         }
372
373         // paint the dShape first
374
if ((dShape != null) && (tpi != null)) {
375             Shape JavaDoc tShape = tr.createTransformedShape(dShape);
376             if (tpi.fillPaint != null) {
377                 graphics2D.setPaint(tpi.fillPaint);
378                 graphics2D.fill(tShape);
379             }
380
381             // check if we need to draw the outline of this glyph
382
if (tpi.strokeStroke != null && tpi.strokePaint != null) {
383                 graphics2D.setStroke(tpi.strokeStroke);
384                 graphics2D.setPaint(tpi.strokePaint);
385                 graphics2D.draw(tShape);
386             }
387         }
388
389         // paint the glyph children nodes
390
if (glyphChildrenNode != null) {
391             glyphChildrenNode.setTransform(tr);
392             glyphChildrenNode.paint(graphics2D);
393         }
394     }
395 }
396
397
Popular Tags