1 17 18 19 20 package org.apache.fop.render.java2d; 21 22 import java.awt.Font ; 24 import java.awt.Graphics2D ; 25 import java.awt.geom.Rectangle2D ; 26 import java.awt.FontMetrics ; 27 import java.awt.font.LineMetrics ; 28 import java.awt.font.TextAttribute ; 29 import java.awt.font.TextLayout ; 30 import java.util.Map ; 31 32 44 public class Java2DFontMetrics { 45 46 49 public static final int FONT_SIZE = 1; 50 51 55 public static final int FONT_FACTOR = (1000 * 1000) / FONT_SIZE; 56 57 60 private int[] width = null; 61 62 65 private int xHeight = 0; 66 67 70 private int ascender = 0; 71 72 75 private int descender = 0; 76 77 82 private Font f1 = null; 84 87 private String family = ""; 88 89 92 private int style = 0; 93 94 97 private float size = 0; 98 99 102 private FontMetrics fmt = null; 103 104 105 private LineMetrics lineMetrics; 106 107 110 private Graphics2D graphics; 111 112 117 public Java2DFontMetrics(Graphics2D graphics) { 118 this.graphics = graphics; 119 } 120 121 129 public int getMaxAscent(String family, int style, int size) { 130 setFont(family, style, size); 131 return Math.round(lineMetrics.getAscent() * FONT_FACTOR); 132 } 133 134 142 public int getAscender(String family, int style, int size) { 143 setFont(family, style, size); 144 return ascender * 1000; 145 146 165 170 } 171 172 173 180 public int getCapHeight(String family, int style, int size) { 181 return getAscender(family, style, size); 184 } 185 186 194 public int getDescender(String family, int style, int size) { 195 setFont(family, style, size); 196 return descender * 1000; 197 } 198 199 207 public int getXHeight(String family, int style, int size) { 208 setFont(family, style, size); 209 return xHeight * 1000; 210 } 211 212 221 public int width(int i, String family, int style, int size) { 222 int w; 223 setFont(family, style, size); 224 w = internalCharWidth(i) * 1000; 225 return w; 226 } 227 228 private int internalCharWidth(int i) { 229 char[] ch = {(char)i}; 231 Rectangle2D rect = fmt.getStringBounds(ch, 0, 1, this.graphics); 232 return (int)Math.round(rect.getWidth() * 1000); 233 } 234 235 243 public int[] getWidths(String family, int style, int size) { 244 int i; 245 246 if (width == null) { 247 width = new int[256]; 248 } 249 setFont(family, style, size); 250 for (i = 0; i < 256; i++) { 251 width[i] = 1000 * internalCharWidth(i); 252 } 253 return width; 254 } 255 256 private Font getBaseFont(String family, int style, float size) { 257 Map atts = new java.util.HashMap (); 258 atts.put(TextAttribute.FAMILY, family); 259 if ((style & Font.BOLD) != 0) { 260 atts.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD); 261 } 262 if ((style & Font.ITALIC) != 0) { 263 atts.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE); 264 } 265 atts.put(TextAttribute.SIZE, new Float (size)); return new Font (atts); 267 } 268 269 278 private boolean setFont(String family, int style, int size) { 279 boolean changed = false; 280 float s = size / 1000f; 281 282 if (f1 == null) { 283 f1 = getBaseFont(family, style, s); 284 fmt = graphics.getFontMetrics(f1); 285 changed = true; 286 } else { 287 if ((this.style != style) || !this.family.equals(family) 288 || this.size != s) { 289 if (family.equals(this.family)) { 290 f1 = f1.deriveFont(style, s); 291 } else { 292 f1 = getBaseFont(family, style, s); 293 } 294 fmt = graphics.getFontMetrics(f1); 295 changed = true; 296 } 297 } 299 if (changed) { 300 TextLayout layout = new TextLayout ("x", f1, graphics.getFontRenderContext()); 302 Rectangle2D rect = layout.getBounds(); 303 xHeight = (int)Math.round(-rect.getY() * 1000); 304 305 layout = new TextLayout ("d", f1, graphics.getFontRenderContext()); 307 rect = layout.getBounds(); 308 ascender = (int)Math.round(-rect.getY() * 1000); 309 310 layout = new TextLayout ("p", f1, graphics.getFontRenderContext()); 312 rect = layout.getBounds(); 313 descender = (int)Math.round((rect.getY() + rect.getHeight()) * -1000); 314 315 lineMetrics = f1.getLineMetrics("", graphics.getFontRenderContext()); 317 } 318 this.family = family; 320 this.style = style; 321 this.size = s; 322 return changed; 323 } 324 325 326 338 public java.awt.Font getFont(String family, int style, int size) { 339 setFont(family, style, size); 340 return f1; 341 349 } 350 351 359 public boolean hasChar(String family, int style, int size, char c) { 360 setFont(family, style, size); 361 return f1.canDisplay(c); 362 } 363 364 } 365 366 367 368 369 370 371 | Popular Tags |