1 7 8 package java.awt; 9 10 import java.awt.Graphics2D ; 11 import java.awt.font.FontRenderContext ; 12 import java.awt.font.LineMetrics ; 13 import java.awt.geom.Rectangle2D ; 14 import java.text.CharacterIterator ; 15 16 79 public abstract class FontMetrics implements java.io.Serializable { 80 81 static { 82 83 Toolkit.loadLibraries(); 84 if (!GraphicsEnvironment.isHeadless()) { 85 initIDs(); 86 } 87 } 88 89 97 protected Font font; 98 99 102 private static final long serialVersionUID = 1681126225205050147L; 103 104 111 protected FontMetrics(Font font) { 112 this.font = font; 113 } 114 115 121 public Font getFont() { 122 return font; 123 } 124 125 137 public int getLeading() { 138 return 0; 139 } 140 141 150 public int getAscent() { 151 return font.getSize(); 152 } 153 154 165 public int getDescent() { 166 return 0; 167 } 168 169 182 public int getHeight() { 183 return getLeading() + getAscent() + getDescent(); 184 } 185 186 194 public int getMaxAscent() { 195 return getAscent(); 196 } 197 198 206 public int getMaxDescent() { 207 return getDescent(); 208 } 209 210 218 @Deprecated 219 public int getMaxDecent() { 220 return getMaxDescent(); 221 } 222 223 233 public int getMaxAdvance() { 234 return -1; 235 } 236 237 258 public int charWidth(int codePoint) { 259 if (!Character.isValidCodePoint(codePoint)) { 260 codePoint = 0xffff; } 262 263 if (codePoint < 256) { 264 return getWidths()[codePoint]; 265 } else { 266 char[] buffer = new char[2]; 267 int len = Character.toChars(codePoint, buffer, 0); 268 return charsWidth(buffer, 0, len); 269 } 270 } 271 272 292 public int charWidth(char ch) { 293 if (ch < 256) { 294 return getWidths()[ch]; 295 } 296 char data[] = {ch}; 297 return charsWidth(data, 0, 1); 298 } 299 300 323 public int stringWidth(String str) { 324 int len = str.length(); 325 char data[] = new char[len]; 326 str.getChars(0, len, data, 0); 327 return charsWidth(data, 0, len); 328 } 329 330 349 public int charsWidth(char data[], int off, int len) { 350 return stringWidth(new String (data, off, len)); 351 } 352 353 371 public int bytesWidth(byte data[], int off, int len) { 372 return stringWidth(new String (data, 0, off, len)); 373 } 374 375 386 public int[] getWidths() { 387 int widths[] = new int[256]; 388 for (char ch = 0 ; ch < 256 ; ch++) { 389 widths[ch] = charWidth(ch); 390 } 391 return widths; 392 } 393 394 406 public boolean hasUniformLineMetrics() { 407 return font.hasUniformLineMetrics(); 408 } 409 410 419 public LineMetrics getLineMetrics( String str, Graphics context) { 420 return font.getLineMetrics(str, myFRC(context)); 421 } 422 423 434 public LineMetrics getLineMetrics( String str, 435 int beginIndex, int limit, 436 Graphics context) { 437 return font.getLineMetrics(str, beginIndex, limit, myFRC(context)); 438 } 439 440 451 public LineMetrics getLineMetrics(char [] chars, 452 int beginIndex, int limit, 453 Graphics context) { 454 return font.getLineMetrics( 455 chars, beginIndex, limit, myFRC(context)); 456 } 457 458 470 public LineMetrics getLineMetrics(CharacterIterator ci, 471 int beginIndex, int limit, 472 Graphics context) { 473 return font.getLineMetrics(ci, beginIndex, limit, myFRC(context)); 474 } 475 476 487 public Rectangle2D getStringBounds( String str, Graphics context) { 488 return font.getStringBounds(str, myFRC(context)); 489 } 490 491 504 public Rectangle2D getStringBounds( String str, 505 int beginIndex, int limit, 506 Graphics context) { 507 return font.getStringBounds(str, beginIndex, limit, 508 myFRC(context)); 509 } 510 511 527 public Rectangle2D getStringBounds( char [] chars, 528 int beginIndex, int limit, 529 Graphics context) { 530 return font.getStringBounds(chars, beginIndex, limit, 531 myFRC(context)); 532 } 533 534 547 public Rectangle2D getStringBounds(CharacterIterator ci, 548 int beginIndex, int limit, 549 Graphics context) { 550 return font.getStringBounds(ci, beginIndex, limit, 551 myFRC(context)); 552 } 553 554 562 public Rectangle2D getMaxCharBounds(Graphics context) { 563 return font.getMaxCharBounds(myFRC(context)); 564 } 565 566 private FontRenderContext myFRC(Graphics context) { 567 if (context instanceof Graphics2D ) { 568 return ((Graphics2D )context).getFontRenderContext(); 569 } 570 return new FontRenderContext (null, false, false); 571 } 572 573 574 581 public String toString() { 582 return getClass().getName() + 583 "[font=" + getFont() + 584 "ascent=" + getAscent() + 585 ", descent=" + getDescent() + 586 ", height=" + getHeight() + "]"; 587 } 588 589 592 private static native void initIDs(); 593 } 594 | Popular Tags |