1 18 package org.apache.batik.gvt.font; 19 20 import java.awt.Font ; 21 import java.awt.Shape ; 22 import java.awt.font.FontRenderContext ; 23 import java.awt.font.GlyphMetrics ; 24 import java.awt.font.GlyphVector ; 25 import java.awt.font.TextAttribute ; 26 import java.awt.geom.AffineTransform ; 27 import java.awt.geom.Point2D ; 28 import java.awt.geom.Rectangle2D ; 29 import java.text.AttributedCharacterIterator ; 30 import java.text.CharacterIterator ; 31 import java.text.StringCharacterIterator ; 32 import java.util.HashMap ; 33 import java.util.Map ; 34 35 import org.apache.batik.gvt.text.ArabicTextHandler; 36 37 38 44 public class AWTGVTFont implements GVTFont { 45 46 protected Font awtFont; 47 protected float size; 48 protected float scale; 49 50 55 public AWTGVTFont(Font 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 68 public AWTGVTFont(Font 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 80 public AWTGVTFont(Map attributes) { 81 Float sz = (Float )attributes.get(TextAttribute.SIZE); 82 if (sz != null) { 83 this.size = sz.floatValue(); 84 attributes.put(TextAttribute.SIZE, new Float (FONT_SIZE)); 85 this.awtFont = new Font (attributes); 86 } else { 87 this.awtFont = new Font (attributes); 88 this.size = awtFont.getSize2D(); 89 } 90 this.scale = size/awtFont.getSize2D(); 91 initializeFontCache(awtFont); 92 } 93 94 101 public AWTGVTFont(String name, int style, int size) { 102 this.awtFont = new Font (name, style, (int)FONT_SIZE); 103 this.size = size; 104 this.scale = size/awtFont.getSize2D(); 105 initializeFontCache(awtFont); 106 } 107 108 114 public boolean canDisplay(char c) { 115 return awtFont.canDisplay(c); 116 } 117 118 129 public int canDisplayUpTo(char[] text, int start, int limit) { 130 return awtFont.canDisplayUpTo(text, start, limit); 131 } 132 133 137 public int canDisplayUpTo(CharacterIterator iter, int start, int limit) { 138 return awtFont.canDisplayUpTo(iter, start, limit); 139 } 140 141 144 public int canDisplayUpTo(String str) { 145 return awtFont.canDisplayUpTo(str); 146 } 147 148 152 public GVTGlyphVector createGlyphVector(FontRenderContext frc, 153 char[] chars) { 154 155 StringCharacterIterator sci = 156 new StringCharacterIterator (new String (chars)); 157 GlyphVector gv = awtFont.createGlyphVector(frc, chars); 158 return new AWTGVTGlyphVector(gv, this, scale, sci); 159 } 160 161 165 public GVTGlyphVector createGlyphVector(FontRenderContext frc, 166 CharacterIterator ci) { 167 168 if (ci instanceof AttributedCharacterIterator ) { 169 AttributedCharacterIterator aci = (AttributedCharacterIterator )ci; 170 if (ArabicTextHandler.containsArabic(aci)) { 171 String str = ArabicTextHandler.createSubstituteString(aci); 172 173 return createGlyphVector(frc, str); 174 } 175 } 176 GlyphVector gv = awtFont.createGlyphVector(frc, ci); 177 return new AWTGVTGlyphVector(gv, this, scale, ci); 178 } 179 180 184 public GVTGlyphVector createGlyphVector(FontRenderContext frc, 185 int[] glyphCodes, 186 CharacterIterator ci) { 187 return new AWTGVTGlyphVector 188 (awtFont.createGlyphVector(frc, glyphCodes), 189 this, scale, ci); 190 } 191 192 196 public GVTGlyphVector createGlyphVector(FontRenderContext frc, String str) 197 { 198 199 StringCharacterIterator sci = new StringCharacterIterator (str); 200 201 return new AWTGVTGlyphVector 202 (awtFont.createGlyphVector(frc, str), this, scale, sci); 203 } 204 205 209 public GVTFont deriveFont(float size) { 210 return new AWTGVTFont(awtFont, size/this.size); 211 } 212 213 216 public GVTLineMetrics getLineMetrics(char[] chars, 217 int beginIndex, 218 int limit, 219 FontRenderContext frc) { 220 return new GVTLineMetrics 221 (awtFont.getLineMetrics(chars, beginIndex, limit, frc), scale); 222 } 223 224 227 public GVTLineMetrics getLineMetrics(CharacterIterator ci, 228 int beginIndex, 229 int limit, 230 FontRenderContext frc) { 231 return new GVTLineMetrics 232 (awtFont.getLineMetrics(ci, beginIndex, limit, frc), scale); 233 } 234 235 239 public GVTLineMetrics getLineMetrics(String str, FontRenderContext frc) { 240 return new GVTLineMetrics(awtFont.getLineMetrics(str, frc), scale); 241 } 242 243 246 public GVTLineMetrics getLineMetrics(String str, 247 int beginIndex, 248 int limit, 249 FontRenderContext frc) { 250 return new GVTLineMetrics 251 (awtFont.getLineMetrics(str, beginIndex, limit, frc), scale); 252 } 253 254 257 public float getSize() { 258 return size; 259 } 260 261 264 public float getHKern(int glyphCode1, int glyphCode2) { 265 return 0f; 266 } 267 268 271 public float getVKern(int glyphCode1, int glyphCode2) { 272 return 0f; 273 } 274 275 277 public static final float FONT_SIZE = 48f; 278 279 284 public static 285 AWTGlyphGeometryCache.Value getGlyphGeometry(AWTGVTFont font, 286 char c, 287 GlyphVector gv, 288 int glyphIndex, 289 Point2D 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 outline = gv.getGlyphOutline(glyphIndex); 297 GlyphMetrics metrics = gv.getGlyphMetrics(glyphIndex); 298 Rectangle2D gmB = metrics.getBounds2D(); 299 if (AWTGVTGlyphVector.outlinesPositioned()) { 300 AffineTransform tr = AffineTransform.getTranslateInstance 301 (-glyphPos.getX(), -glyphPos.getY()); 302 outline = tr.createTransformedShape(outline); 303 } 304 v = new AWTGlyphGeometryCache.Value(outline, gmB); 305 glyphCache.put(c, v); 307 } 308 return v; 309 } 310 311 315 static Map fontCache = new HashMap (11); 316 317 static void initializeFontCache(Font 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 awtFont) { 328 return (AWTGVTFont)fontCache.get(awtFont); 329 } 330 331 } 332 333 | Popular Tags |