1 18 package org.apache.batik.gvt.font; 19 20 import java.awt.Graphics2D ; 21 import java.awt.Paint ; 22 import java.awt.Shape ; 23 import java.awt.Stroke ; 24 import java.awt.font.GlyphMetrics ; 25 import java.awt.geom.AffineTransform ; 26 import java.awt.geom.GeneralPath ; 27 import java.awt.geom.Point2D ; 28 import java.awt.geom.Rectangle2D ; 29 import java.util.Vector ; 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 43 public class Glyph { 44 45 private String unicode; 46 private Vector names; 47 private String orientation; 48 private String arabicForm; 49 private String lang; 50 private Point2D horizOrigin; 51 private Point2D vertOrigin; 52 private float horizAdvX; 53 private float vertAdvY; 54 private int glyphCode; 55 private AffineTransform transform; 56 private Point2D.Float position; 57 private GVTGlyphMetrics metrics; 58 59 private Shape outline; private Rectangle2D bounds; 62 private TextPaintInfo tpi; 63 private TextPaintInfo cacheTPI; 64 private Shape dShape; 65 private GraphicsNode glyphChildrenNode; 66 67 68 71 public Glyph(String unicode, Vector names, 72 String orientation, String arabicForm, String lang, 73 Point2D horizOrigin, Point2D vertOrigin, float horizAdvX, 74 float vertAdvY, int glyphCode, 75 TextPaintInfo tpi, 76 Shape dShape, GraphicsNode glyphChildrenNode) { 77 78 if (unicode == null) { 79 throw new IllegalArgumentException (); 80 } 81 if (horizOrigin == null) { 82 throw new IllegalArgumentException (); 83 } 84 if (vertOrigin == null) { 85 throw new IllegalArgumentException (); 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 (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 113 public String getUnicode() { 114 return unicode; 115 } 116 117 122 public Vector getNames() { 123 return names; 124 } 125 126 134 public String getOrientation() { 135 return orientation; 136 } 137 138 144 public String getArabicForm() { 145 return arabicForm; 146 } 147 148 153 public String getLang() { 154 return lang; 155 } 156 157 162 public Point2D getHorizOrigin() { 163 return horizOrigin; 164 } 165 166 171 public Point2D getVertOrigin() { 172 return vertOrigin; 173 } 174 175 180 public float getHorizAdvX() { 181 return horizAdvX; 182 } 183 184 189 public float getVertAdvY() { 190 return vertAdvY; 191 } 192 193 199 public int getGlyphCode() { 200 return glyphCode; 201 } 202 203 208 public AffineTransform getTransform() { 209 return transform; 210 } 211 212 217 public void setTransform(AffineTransform transform) { 218 this.transform = transform; 219 outline = null; 220 bounds = null; 221 } 222 223 228 public Point2D getPosition() { 229 return position; 230 } 231 232 237 public void setPosition(Point2D position) { 238 this.position.x = (float)position.getX(); 239 this.position.y = (float)position.getY(); 240 outline = null; 241 bounds = null; 242 } 243 244 249 public GVTGlyphMetrics getGlyphMetrics() { 250 if (metrics == null) { 251 Rectangle2D gb = getGeometryBounds(); 252 253 metrics = new GVTGlyphMetrics 254 (getHorizAdvX(), getVertAdvY(), 255 new Rectangle2D.Double (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 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 getGeometryBounds() { 283 return getOutline().getBounds2D(); 284 } 285 286 public Rectangle2D getBounds2D() { 287 if ((bounds != null) && 289 TextPaintInfo.equivilent(tpi, cacheTPI)) 290 return bounds; 291 292 AffineTransform tr = 293 AffineTransform.getTranslateInstance(position.getX(), 294 position.getY()); 295 if (transform != null) { 296 tr.concatenate(transform); 297 } 298 299 Rectangle2D 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 s = tpi.strokeStroke.createStrokedShape(dShape); 306 Rectangle2D 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 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 319 (position.getX(), position.getY(), 0, 0); 320 321 cacheTPI = new TextPaintInfo(tpi); 322 return bounds; 323 } 324 325 331 public Shape getOutline() { 332 if (outline == null) { 333 AffineTransform tr = 334 AffineTransform.getTranslateInstance(position.getX(), 335 position.getY()); 336 if (transform != null) { 337 tr.concatenate(transform); 338 } 339 Shape glyphChildrenOutline = null; 340 if (glyphChildrenNode != null) { 341 glyphChildrenOutline = glyphChildrenNode.getOutline(); 342 } 343 GeneralPath glyphOutline = null; 344 if (dShape != null && glyphChildrenOutline != null) { 345 glyphOutline = new GeneralPath (dShape); 346 glyphOutline.append(glyphChildrenOutline, false); 347 } else if (dShape != null && glyphChildrenOutline == null) { 348 glyphOutline = new GeneralPath (dShape); 349 } else if (dShape == null && glyphChildrenOutline != null) { 350 glyphOutline = new GeneralPath (glyphChildrenOutline); 351 } else { 352 glyphOutline = new GeneralPath (); 354 } 355 outline = tr.createTransformedShape(glyphOutline); 356 } 357 return outline; 358 } 359 360 365 public void draw(Graphics2D graphics2D) { 366 AffineTransform tr = 367 AffineTransform.getTranslateInstance(position.getX(), 368 position.getY()); 369 if (transform != null) { 370 tr.concatenate(transform); 371 } 372 373 if ((dShape != null) && (tpi != null)) { 375 Shape tShape = tr.createTransformedShape(dShape); 376 if (tpi.fillPaint != null) { 377 graphics2D.setPaint(tpi.fillPaint); 378 graphics2D.fill(tShape); 379 } 380 381 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 if (glyphChildrenNode != null) { 391 glyphChildrenNode.setTransform(tr); 392 glyphChildrenNode.paint(graphics2D); 393 } 394 } 395 } 396 397 | Popular Tags |