1 51 package org.apache.fop.render.awt; 52 53 import org.apache.fop.messaging.MessageHandler; 55 56 import java.io.InputStream ; 58 import java.net.URL ; 59 import java.awt.Font ; 60 import java.awt.Graphics2D ; 61 import java.awt.geom.Rectangle2D ; 62 import java.awt.FontMetrics ; 63 import java.awt.font.TextLayout ; 64 import java.util.Map ; 65 66 78 79 public class AWTFontMetrics { 80 81 84 public static final int FONT_SIZE = 1; 85 86 90 public static final int FONT_FACTOR = (1000 * 1000) / FONT_SIZE; 91 92 95 private int width[] = null; 96 97 100 private int xHeight = 0; 101 102 107 private Font f1 = null; 109 112 private String family = ""; 113 114 117 private int style = 0; 118 119 122 private float size = 0; 123 124 127 private FontMetrics fmt = null; 128 129 132 Graphics2D graphics; 133 134 137 private Map embedFontList = null; 138 139 142 private Map fontCache = null; 143 144 150 public AWTFontMetrics(Graphics2D graphics) { 151 this.graphics = graphics; 152 } 153 154 161 public int getAscender(String family, int style, int size) { 162 setFont(family, style, size); 163 165 int realAscent = fmt.getAscent() 168 - (fmt.getDescent() + fmt.getLeading()); 169 return FONT_FACTOR * realAscent; 170 } 171 172 173 176 public int getCapHeight(String family, int style, int size) { 177 return getAscender(family, style, size); 180 } 181 182 189 public int getDescender(String family, int style, int size) { 190 setFont(family, style, size); 191 return (-1 * FONT_FACTOR * fmt.getDescent()); 192 } 193 194 201 public int getXHeight(String family, int style, int size) { 202 setFont(family, style, size); 203 return (int)(FONT_FACTOR * xHeight); 204 } 205 206 214 public int width(int i, String family, int style, int size) { 215 int w; 216 setFont(family, style, size); 217 218 221 char [] ac = new char [1]; 222 ac [0] = (char)i; 223 224 double dWidth = fmt.getStringBounds (ac, 0, 1, graphics).getWidth() * FONT_FACTOR; 225 226 228 232 if (i <=32) { 233 dWidth = dWidth * 1.4; 234 } 235 236 return (int) dWidth; 237 } 238 239 245 public int[] getWidths(String family, int style, int size) { 246 int i; 247 248 if (width == null) { 249 width = new int[256]; 250 } 251 setFont(family, style, size); 252 for (i = 0; i < 256; i++) { 253 width[i] = FONT_FACTOR * fmt.charWidth(i); 254 } 255 return width; 256 } 257 258 266 private boolean setFont(String family, int style, int size) { 267 boolean changed = false; 268 Rectangle2D rect; 269 TextLayout layout; 270 int s = (int)(size / 1000f); 271 272 if (f1 == null) { 273 f1 = createFont(family, style, s); 274 fmt = graphics.getFontMetrics(f1); 275 changed = true; 276 } else { 277 if ((this.style != style) ||!this.family.equals(family) 278 || this.size != s) { 279 if (family.equals(this.family)) { 280 f1 = f1.deriveFont(style, (float)s); 281 } else 282 f1 = createFont(family, style, s); 283 fmt = graphics.getFontMetrics(f1); 284 changed = true; 285 } 286 } 288 if (changed) { 289 layout = new TextLayout ("m", f1, graphics.getFontRenderContext()); 290 rect = layout.getBounds(); 291 xHeight = (int)rect.getHeight(); 292 } 293 this.family = family; 295 this.style = style; 296 this.size = s; 297 return changed; 298 } 299 300 306 public void setEmbedFont(String family, int style, URL fontURL) { 307 if (embedFontList == null) 308 embedFontList = new java.util.HashMap (); 309 embedFontList.put(family+style, fontURL); 310 } 311 312 318 public java.awt.Font createFont(String family, int style, int size) { 319 URL fontURL = null; 320 if (embedFontList != null) 321 fontURL = (URL )embedFontList.get(family+style); 322 if (fontURL == null) 323 return new Font (family, style, size); 324 if (fontCache == null) 326 fontCache = new java.util.HashMap (); 327 Font cachedFont = (Font )fontCache.get(fontURL.toExternalForm()); 328 if (cachedFont == null) { 329 InputStream fontStream = null; 331 try { 332 MessageHandler.logln("Create embedded AWT font from stream "+fontURL.toExternalForm()); 333 fontStream = fontURL.openStream(); 334 cachedFont = Font.createFont(Font.TRUETYPE_FONT, fontStream); 337 } catch(Throwable th) { 338 MessageHandler.error("Failed to create embedded AWT font "+ 339 fontURL.toExternalForm() + ": " + th.toString()); 340 cachedFont = new Font ("Dialog", style, size); 343 } finally { 344 if (fontStream != null) 345 try { fontStream.close(); } catch(Exception ex) {} 346 } 347 fontCache.put(fontURL.toExternalForm(), cachedFont); 348 } 349 Font font = cachedFont.deriveFont(style, (float)size); 350 return font; 351 } 352 353 365 public java.awt.Font getFont(String family, int style, int size) { 366 Font f; 367 368 setFont(family, style, size); 369 return f1; 370 378 } 379 380 } 381 382 383 384 385 386 387 | Popular Tags |