1 50 51 package com.lowagie.text; 52 53 import java.awt.Color ; 54 55 import com.lowagie.text.html.Markup; 56 import com.lowagie.text.pdf.BaseFont; 57 58 72 73 public class Font implements Comparable { 74 75 77 78 public static final int COURIER = 0; 79 80 81 public static final int HELVETICA = 1; 82 83 84 public static final int TIMES_ROMAN = 2; 85 86 87 public static final int SYMBOL = 3; 88 89 90 public static final int ZAPFDINGBATS = 4; 91 92 94 95 public static final int NORMAL = 0; 96 97 98 public static final int BOLD = 1; 99 100 101 public static final int ITALIC = 2; 102 103 104 public static final int UNDERLINE = 4; 105 106 107 public static final int STRIKETHRU = 8; 108 109 110 public static final int BOLDITALIC = BOLD | ITALIC; 111 112 114 115 public static final int UNDEFINED = -1; 116 117 118 public static final int DEFAULTSIZE = 12; 119 120 122 123 private int family = UNDEFINED; 124 125 126 private float size = UNDEFINED; 127 128 129 private int style = UNDEFINED; 130 131 132 private Color color = null; 133 134 135 private BaseFont baseFont = null; 136 137 139 145 public Font(Font other) { 146 this.family = other.family; 147 this.size = other.size; 148 this.style = other.style; 149 this.color = other.color; 150 this.baseFont = other.baseFont; 151 } 152 153 165 166 public Font(int family, float size, int style, Color color) { 167 this.family = family; 168 this.size = size; 169 this.style = style; 170 this.color = color; 171 } 172 173 185 186 public Font(BaseFont bf, float size, int style, Color color) { 187 this.baseFont = bf; 188 this.size = size; 189 this.style = style; 190 this.color = color; 191 } 192 193 203 public Font(BaseFont bf, float size, int style) { 204 this(bf, size, style, null); 205 } 206 207 215 public Font(BaseFont bf, float size) { 216 this(bf, size, UNDEFINED, null); 217 } 218 219 225 public Font(BaseFont bf) { 226 this(bf, UNDEFINED, UNDEFINED, null); 227 } 228 229 239 240 public Font(int family, float size, int style) { 241 this(family, size, style, null); 242 } 243 244 252 253 public Font(int family, float size) { 254 this(family, size, UNDEFINED, null); 255 } 256 257 263 264 public Font(int family) { 265 this(family, UNDEFINED, UNDEFINED, null); 266 } 267 268 271 272 public Font() { 273 this(UNDEFINED, UNDEFINED, UNDEFINED, null); 274 } 275 276 278 285 public int compareTo(Object object) { 286 if (object == null) { 287 return -1; 288 } 289 Font font; 290 try { 291 font = (Font) object; 292 if (baseFont != null && !baseFont.equals(font.getBaseFont())) { 293 return -2; 294 } 295 if (this.family != font.getFamily()) { 296 return 1; 297 } 298 if (this.size != font.getSize()) { 299 return 2; 300 } 301 if (this.style != font.getStyle()) { 302 return 3; 303 } 304 if (this.color == null) { 305 if (font.color == null) { 306 return 0; 307 } 308 return 4; 309 } 310 if (font.color == null) { 311 return 4; 312 } 313 if (this.color.equals(font.getColor())) { 314 return 0; 315 } 316 return 4; 317 } catch (ClassCastException cce) { 318 return -3; 319 } 320 } 321 322 324 329 public int getFamily() { 330 return family; 331 } 332 333 338 public String getFamilyname() { 339 String tmp = "unknown"; 340 switch (getFamily()) { 341 case Font.COURIER: 342 return FontFactory.COURIER; 343 case Font.HELVETICA: 344 return FontFactory.HELVETICA; 345 case Font.TIMES_ROMAN: 346 return FontFactory.TIMES_ROMAN; 347 case Font.SYMBOL: 348 return FontFactory.SYMBOL; 349 case Font.ZAPFDINGBATS: 350 return FontFactory.ZAPFDINGBATS; 351 default: 352 if (baseFont != null) { 353 String [][] names = baseFont.getFamilyFontName(); 354 for (int i = 0; i < names.length; i++) { 355 if ("0".equals(names[i][2])) { 356 return names[i][3]; 357 } 358 if ("1033".equals(names[i][2])) { 359 tmp = names[i][3]; 360 } 361 if ("".equals(names[i][2])) { 362 tmp = names[i][3]; 363 } 364 } 365 } 366 } 367 return tmp; 368 } 369 370 377 public void setFamily(String family) { 378 this.family = getFamilyIndex(family); 379 } 380 381 389 public static int getFamilyIndex(String family) { 390 if (family.equalsIgnoreCase(FontFactory.COURIER)) { 391 return COURIER; 392 } 393 if (family.equalsIgnoreCase(FontFactory.HELVETICA)) { 394 return HELVETICA; 395 } 396 if (family.equalsIgnoreCase(FontFactory.TIMES_ROMAN)) { 397 return TIMES_ROMAN; 398 } 399 if (family.equalsIgnoreCase(FontFactory.SYMBOL)) { 400 return SYMBOL; 401 } 402 if (family.equalsIgnoreCase(FontFactory.ZAPFDINGBATS)) { 403 return ZAPFDINGBATS; 404 } 405 return UNDEFINED; 406 } 407 408 410 415 public float getSize() { 416 return size; 417 } 418 419 426 public float getCalculatedSize() { 427 float s = this.size; 428 if (s == UNDEFINED) { 429 s = DEFAULTSIZE; 430 } 431 return s; 432 } 433 434 441 public float getCalculatedLeading(float linespacing) { 442 return linespacing * getCalculatedSize(); 443 } 444 445 451 public void setSize(float size) { 452 this.size = size; 453 } 454 455 457 462 public int getStyle() { 463 return style; 464 } 465 466 473 public int getCalculatedStyle() { 474 int style = this.style; 475 if (style == UNDEFINED) { 476 style = NORMAL; 477 } 478 if (baseFont != null) 479 return style; 480 if (family == SYMBOL || family == ZAPFDINGBATS) 481 return style; 482 else 483 return style & (~BOLDITALIC); 484 } 485 486 491 public boolean isBold() { 492 if (style == UNDEFINED) { 493 return false; 494 } 495 return (style & BOLD) == BOLD; 496 } 497 498 503 public boolean isItalic() { 504 if (style == UNDEFINED) { 505 return false; 506 } 507 return (style & ITALIC) == ITALIC; 508 } 509 510 515 public boolean isUnderlined() { 516 if (style == UNDEFINED) { 517 return false; 518 } 519 return (style & UNDERLINE) == UNDERLINE; 520 } 521 522 527 public boolean isStrikethru() { 528 if (style == UNDEFINED) { 529 return false; 530 } 531 return (style & STRIKETHRU) == STRIKETHRU; 532 } 533 534 540 public void setStyle(int style) { 541 if (this.style == UNDEFINED) 542 this.style = NORMAL; 543 this.style |= style; 544 } 545 546 553 public void setStyle(String style) { 554 if (this.style == UNDEFINED) 555 this.style = NORMAL; 556 this.style |= getStyleValue(style); 557 } 558 559 567 public static int getStyleValue(String style) { 568 int s = 0; 569 if (style.indexOf(Markup.CSS_VALUE_NORMAL) != -1) { 570 s |= NORMAL; 571 } 572 if (style.indexOf(Markup.CSS_VALUE_BOLD) != -1) { 573 s |= BOLD; 574 } 575 if (style.indexOf(Markup.CSS_VALUE_ITALIC) != -1) { 576 s |= ITALIC; 577 } 578 if (style.indexOf(Markup.CSS_VALUE_OBLIQUE) != -1) { 579 s |= ITALIC; 580 } 581 if (style.indexOf(Markup.CSS_VALUE_UNDERLINE) != -1) { 582 s |= UNDERLINE; 583 } 584 if (style.indexOf(Markup.CSS_VALUE_LINETHROUGH) != -1) { 585 s |= STRIKETHRU; 586 } 587 return s; 588 } 589 590 592 597 public Color getColor() { 598 return color; 599 } 600 601 607 608 public void setColor(Color color) { 609 this.color = color; 610 } 611 612 622 public void setColor(int red, int green, int blue) { 623 this.color = new Color (red, green, blue); 624 } 625 626 628 633 public BaseFont getBaseFont() { 634 return baseFont; 635 } 636 637 647 public BaseFont getCalculatedBaseFont(boolean specialEncoding) { 648 if (baseFont != null) 649 return baseFont; 650 int style = this.style; 651 if (style == UNDEFINED) { 652 style = NORMAL; 653 } 654 String fontName = BaseFont.HELVETICA; 655 String encoding = BaseFont.WINANSI; 656 BaseFont cfont = null; 657 switch (family) { 658 case COURIER: 659 switch (style & BOLDITALIC) { 660 case BOLD: 661 fontName = BaseFont.COURIER_BOLD; 662 break; 663 case ITALIC: 664 fontName = BaseFont.COURIER_OBLIQUE; 665 break; 666 case BOLDITALIC: 667 fontName = BaseFont.COURIER_BOLDOBLIQUE; 668 break; 669 default: 670 fontName = BaseFont.COURIER; 672 break; 673 } 674 break; 675 case TIMES_ROMAN: 676 switch (style & BOLDITALIC) { 677 case BOLD: 678 fontName = BaseFont.TIMES_BOLD; 679 break; 680 case ITALIC: 681 fontName = BaseFont.TIMES_ITALIC; 682 break; 683 case BOLDITALIC: 684 fontName = BaseFont.TIMES_BOLDITALIC; 685 break; 686 default: 687 case NORMAL: 688 fontName = BaseFont.TIMES_ROMAN; 689 break; 690 } 691 break; 692 case SYMBOL: 693 fontName = BaseFont.SYMBOL; 694 if (specialEncoding) 695 encoding = BaseFont.SYMBOL; 696 break; 697 case ZAPFDINGBATS: 698 fontName = BaseFont.ZAPFDINGBATS; 699 if (specialEncoding) 700 encoding = BaseFont.ZAPFDINGBATS; 701 break; 702 default: 703 case Font.HELVETICA: 704 switch (style & BOLDITALIC) { 705 case BOLD: 706 fontName = BaseFont.HELVETICA_BOLD; 707 break; 708 case ITALIC: 709 fontName = BaseFont.HELVETICA_OBLIQUE; 710 break; 711 case BOLDITALIC: 712 fontName = BaseFont.HELVETICA_BOLDOBLIQUE; 713 break; 714 default: 715 case NORMAL: 716 fontName = BaseFont.HELVETICA; 717 break; 718 } 719 break; 720 } 721 try { 722 cfont = BaseFont.createFont(fontName, encoding, false); 723 } catch (Exception ee) { 724 throw new ExceptionConverter(ee); 725 } 726 return cfont; 727 } 728 729 730 732 739 public boolean isStandardFont() { 740 return (family == UNDEFINED && size == UNDEFINED && style == UNDEFINED 741 && color == null && baseFont == null); 742 } 743 744 752 public Font difference(Font font) { 753 float dSize = font.size; 755 if (dSize == UNDEFINED) { 756 dSize = this.size; 757 } 758 int dStyle = UNDEFINED; 760 int style1 = this.style; 761 int style2 = font.getStyle(); 762 if (style1 != UNDEFINED || style2 != UNDEFINED) { 763 if (style1 == UNDEFINED) 764 style1 = 0; 765 if (style2 == UNDEFINED) 766 style2 = 0; 767 dStyle = style1 | style2; 768 } 769 Color dColor = font.color; 771 if (dColor == null) { 772 dColor = this.color; 773 } 774 if (font.baseFont != null) { 776 return new Font(font.baseFont, dSize, dStyle, dColor); 777 } 778 if (font.getFamily() != UNDEFINED) { 779 return new Font(font.family, dSize, dStyle, dColor); 780 } 781 if (this.baseFont != null) { 782 if (dStyle == style1) { 783 return new Font(this.baseFont, dSize, dStyle, dColor); 784 } else { 785 return FontFactory.getFont(this.getFamilyname(), dSize, dStyle, 786 dColor); 787 } 788 } 789 return new Font(this.family, dSize, dStyle, dColor); 790 } 791 792 794 800 public int family() { 801 return getFamily(); 802 } 803 804 810 public float size() { 811 return getSize(); 812 } 813 814 822 public float leading(float linespacing) { 823 return getCalculatedLeading(linespacing); 824 } 825 826 832 public int style() { 833 return getStyle(); 834 } 835 836 842 public Color color() { 843 return getColor(); 844 } 845 } | Popular Tags |