1 50 51 package com.lowagie.text.rtf.style; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.IOException ; 56 import java.io.OutputStream ; 57 58 import com.lowagie.text.Font; 59 import com.lowagie.text.rtf.RtfExtendedElement; 60 import com.lowagie.text.rtf.document.RtfDocument; 61 62 76 public class RtfFont extends Font implements RtfExtendedElement { 77 80 private static final byte[] FONT_FAMILY = "\\froman".getBytes(); 81 84 private static final byte[] FONT_CHARSET = "\\fcharset".getBytes(); 85 88 public static final byte[] FONT_SIZE = "\\fs".getBytes(); 89 92 private static final byte[] FONT_BOLD = "\\b".getBytes(); 93 96 private static final byte[] FONT_ITALIC = "\\i".getBytes(); 97 100 private static final byte[] FONT_UNDERLINE = "\\ul".getBytes(); 101 104 private static final byte[] FONT_STRIKETHROUGH = "\\strike".getBytes(); 105 108 private static final byte[] FONT_DOUBLE_STRIKETHROUGH = "\\striked".getBytes(); 109 112 private static final byte[] FONT_SHADOW = "\\shad".getBytes(); 113 116 private static final byte[] FONT_OUTLINE = "\\outl".getBytes(); 117 120 private static final byte[] FONT_EMBOSSED = "\\embo".getBytes(); 121 124 private static final byte[] FONT_ENGRAVED = "\\impr".getBytes(); 125 128 private static final byte[] FONT_HIDDEN = "\\v".getBytes(); 129 130 133 public static final int STYLE_NONE = 0; 134 137 public static final int STYLE_BOLD = 1; 138 141 public static final int STYLE_ITALIC = 2; 142 145 public static final int STYLE_UNDERLINE = 4; 146 149 public static final int STYLE_STRIKETHROUGH = 8; 150 153 public static final int STYLE_DOUBLE_STRIKETHROUGH = 16; 154 157 public static final int STYLE_SHADOW = 32; 158 161 public static final int STYLE_OUTLINE = 64; 162 165 public static final int STYLE_EMBOSSED = 128; 166 169 public static final int STYLE_ENGRAVED = 256; 170 173 public static final int STYLE_HIDDEN = 512; 174 175 178 private String fontName = "Times New Roman"; 179 182 private int fontSize = 10; 183 186 private int fontStyle = STYLE_NONE; 187 190 private int fontNumber = 0; 191 194 private RtfColor color = null; 195 198 private int charset = 0; 199 202 protected RtfDocument document = null; 203 204 210 public RtfFont(String fontName) { 211 super(Font.UNDEFINED, Font.UNDEFINED, Font.UNDEFINED, null); 212 this.fontName = fontName; 213 } 214 215 222 public RtfFont(String fontName, float size) { 223 super(Font.UNDEFINED, size, Font.UNDEFINED, null); 224 this.fontName = fontName; 225 } 226 227 235 public RtfFont(String fontName, float size, int style) { 236 super(Font.UNDEFINED, size, style, null); 237 this.fontName = fontName; 238 } 239 240 249 public RtfFont(String fontName, float size, int style, Color color) { 250 super(Font.UNDEFINED, size, style, color); 251 this.fontName = fontName; 252 } 253 254 264 public RtfFont(String fontName, float size, int style, Color color, int charset) { 265 this(fontName, size, style, color); 266 this.charset = charset; 267 } 268 269 275 protected RtfFont(RtfDocument doc, int fontNumber) { 276 this.document = doc; 277 this.fontNumber = fontNumber; 278 color = new RtfColor(doc, 0, 0, 0); 279 } 280 281 286 public RtfFont(RtfDocument doc, Font font) { 287 this.document = doc; 288 if(font != null) { 289 if(font instanceof RtfFont) { 290 this.fontName = ((RtfFont) font).getFontName(); 291 this.charset = ((RtfFont) font).getCharset(); 292 } else { 293 setToDefaultFamily(font.getFamilyname()); 294 } 295 if(font.getBaseFont() != null) { 296 String [][] fontNames = font.getBaseFont().getFullFontName(); 297 for(int i = 0; i < fontNames.length; i++) { 298 if(fontNames[i][2].equals("0")) { 299 this.fontName = fontNames[i][3]; 300 break; 301 } else if(fontNames[i][2].equals("1033") || fontNames[i][2].equals("")) { 302 this.fontName = fontNames[i][3]; 303 } 304 } 305 } 306 307 setSize(font.getSize()); 308 setStyle(font.getStyle()); 309 setColor(font.getColor()); 310 } 311 312 if(this.fontName.equalsIgnoreCase("unknown")) { 313 return; 314 } 315 316 if(document != null) { 317 setRtfDocument(document); 318 } 319 } 320 321 327 public byte[] writeDefinition() { 328 ByteArrayOutputStream result = new ByteArrayOutputStream (); 329 try { 330 writeDefinition(result); 331 } catch(IOException ioe) { 332 ioe.printStackTrace(); 333 } 334 return result.toByteArray(); 335 } 336 337 340 public void writeDefinition(final OutputStream result) throws IOException 341 { 342 result.write(FONT_FAMILY); 343 result.write(FONT_CHARSET); 344 result.write(intToByteArray(charset)); 345 result.write(DELIMITER); 346 document.filterSpecialChar(result, fontName, true, false); 348 } 349 350 355 public byte[] writeBegin() { 356 ByteArrayOutputStream result = new ByteArrayOutputStream (); 357 try { 358 if(this.fontNumber != Font.UNDEFINED) { 359 result.write(RtfFontList.FONT_NUMBER); 360 result.write(intToByteArray(fontNumber)); 361 } 362 if(this.fontSize != Font.UNDEFINED) { 363 result.write(FONT_SIZE); 364 result.write(intToByteArray(fontSize * 2)); 365 } 366 if(this.fontStyle != UNDEFINED) { 367 if((fontStyle & STYLE_BOLD) == STYLE_BOLD) { 368 result.write(FONT_BOLD); 369 } 370 if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) { 371 result.write(FONT_ITALIC); 372 } 373 if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) { 374 result.write(FONT_UNDERLINE); 375 } 376 if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) { 377 result.write(FONT_STRIKETHROUGH); 378 } 379 if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) { 380 result.write(FONT_HIDDEN); 381 } 382 if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) { 383 result.write(FONT_DOUBLE_STRIKETHROUGH); 384 result.write(intToByteArray(1)); 385 } 386 if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) { 387 result.write(FONT_SHADOW); 388 } 389 if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) { 390 result.write(FONT_OUTLINE); 391 } 392 if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) { 393 result.write(FONT_EMBOSSED); 394 } 395 if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) { 396 result.write(FONT_ENGRAVED); 397 } 398 } 399 if(color != null) { 400 result.write(color.writeBegin()); 401 } 402 } catch(IOException ioe) { 403 ioe.printStackTrace(); 404 } 405 return result.toByteArray(); 406 } 407 408 413 public byte[] writeEnd() { 414 ByteArrayOutputStream result = new ByteArrayOutputStream (); 415 try { 416 if(this.fontStyle != UNDEFINED) { 417 if((fontStyle & STYLE_BOLD) == STYLE_BOLD) { 418 result.write(FONT_BOLD); 419 result.write(intToByteArray(0)); 420 } 421 if((fontStyle & STYLE_ITALIC) == STYLE_ITALIC) { 422 result.write(FONT_ITALIC); 423 result.write(intToByteArray(0)); 424 } 425 if((fontStyle & STYLE_UNDERLINE) == STYLE_UNDERLINE) { 426 result.write(FONT_UNDERLINE); 427 result.write(intToByteArray(0)); 428 } 429 if((fontStyle & STYLE_STRIKETHROUGH) == STYLE_STRIKETHROUGH) { 430 result.write(FONT_STRIKETHROUGH); 431 result.write(intToByteArray(0)); 432 } 433 if((fontStyle & STYLE_HIDDEN) == STYLE_HIDDEN) { 434 result.write(FONT_HIDDEN); 435 result.write(intToByteArray(0)); 436 } 437 if((fontStyle & STYLE_DOUBLE_STRIKETHROUGH) == STYLE_DOUBLE_STRIKETHROUGH) { 438 result.write(FONT_DOUBLE_STRIKETHROUGH); 439 result.write(intToByteArray(0)); 440 } 441 if((fontStyle & STYLE_SHADOW) == STYLE_SHADOW) { 442 result.write(FONT_SHADOW); 443 result.write(intToByteArray(0)); 444 } 445 if((fontStyle & STYLE_OUTLINE) == STYLE_OUTLINE) { 446 result.write(FONT_OUTLINE); 447 result.write(intToByteArray(0)); 448 } 449 if((fontStyle & STYLE_EMBOSSED) == STYLE_EMBOSSED) { 450 result.write(FONT_EMBOSSED); 451 result.write(intToByteArray(0)); 452 } 453 if((fontStyle & STYLE_ENGRAVED) == STYLE_ENGRAVED) { 454 result.write(FONT_ENGRAVED); 455 result.write(intToByteArray(0)); 456 } 457 } 458 } catch(IOException ioe) { 459 ioe.printStackTrace(); 460 } 461 return result.toByteArray(); 462 } 463 464 469 public byte[] write() { 470 return new byte[0]; 471 } 472 475 public void writeContent(OutputStream out) throws IOException 476 { 477 } 478 479 486 public boolean equals(Object obj) { 487 if(!(obj instanceof RtfFont)) { 488 return false; 489 } 490 RtfFont font = (RtfFont) obj; 491 boolean result = true; 492 result = result & this.fontName.equals(font.getFontName()); 493 494 return result; 495 } 496 497 504 public int hashCode() { 505 return (this.fontName + this.fontSize + "-" + this.fontStyle).hashCode(); 506 } 507 508 513 public String getFontName() { 514 return this.fontName; 515 } 516 517 522 protected void setFontName(String fontName) { 523 this.fontName = fontName; 524 if(document != null) { 525 this.fontNumber = document.getDocumentHeader().getFontNumber(this); 526 } 527 } 528 529 532 public String getFamilyname() { 533 return this.fontName; 534 } 535 536 539 public void setFamily(String family){ 540 super.setFamily(family); 541 setToDefaultFamily(family); 542 } 543 544 549 private void setToDefaultFamily(String familyname){ 550 switch (Font.getFamilyIndex(familyname)) { 551 case Font.COURIER: 552 this.fontName = "Courier"; 553 break; 554 case Font.HELVETICA: 555 this.fontName = "Arial"; 556 break; 557 case Font.SYMBOL: 558 this.fontName = "Symbol"; 559 this.charset = 2; 560 break; 561 case Font.TIMES_ROMAN: 562 this.fontName = "Times New Roman"; 563 break; 564 case Font.ZAPFDINGBATS: 565 this.fontName = "Windings"; 566 break; 567 default: 568 this.fontName = familyname; 569 } 570 } 571 572 577 public int getFontSize() { 578 return this.fontSize; 579 } 580 581 584 public void setSize(float size){ 585 super.setSize(size); 586 this.fontSize = (int) getSize(); 587 } 588 589 594 public int getFontStyle() { 595 return this.fontStyle; 596 } 597 598 601 public void setStyle(int style){ 602 super.setStyle(style); 603 this.fontStyle = getStyle(); 604 } 605 606 609 public void setStyle(String style) { 610 super.setStyle(style); 611 fontStyle = getStyle(); 612 } 613 614 619 public int getCharset() { 620 return charset; 621 } 622 623 628 public void setCharset(int charset) { 629 this.charset = charset; 630 } 631 632 637 public int getFontNumber() { 638 return fontNumber; 639 } 640 641 646 public void setRtfDocument(RtfDocument doc) { 647 this.document = doc; 648 if(document != null) { 649 this.fontNumber = document.getDocumentHeader().getFontNumber(this); 650 } 651 if(this.color != null) { 652 this.color.setRtfDocument(this.document); 653 } 654 } 655 656 660 public void setInTable(boolean inTable) { 661 } 662 663 667 public void setInHeader(boolean inHeader) { 668 } 669 670 673 public void setColor(Color color) { 674 super.setColor(color); 675 if(color != null) { 676 this.color = new RtfColor(document, color); 677 } else { 678 this.color = null; 679 } 680 } 681 682 685 public void setColor(int red, int green, int blue) { 686 super.setColor(red,green,blue); 687 this.color = new RtfColor(document, red, green, blue); 688 } 689 690 697 protected byte[] intToByteArray(int i) { 698 return Integer.toString(i).getBytes(); 699 } 700 701 708 public Font difference(Font font) { 709 String dFamilyname = font.getFamilyname(); 710 if(dFamilyname == null || dFamilyname.trim().equals("") || dFamilyname.trim().equalsIgnoreCase("unknown")) { 711 dFamilyname = this.fontName; 712 } 713 714 float dSize = font.getSize(); 715 if(dSize == Font.UNDEFINED) { 716 dSize = this.getSize(); 717 } 718 719 int dStyle = Font.UNDEFINED; 720 if(this.getStyle() != Font.UNDEFINED && font.getStyle() != Font.UNDEFINED) { 721 dStyle = this.getStyle() | font.getStyle(); 722 } else if(this.getStyle() != Font.UNDEFINED) { 723 dStyle = this.getStyle(); 724 } else if(font.getStyle() != Font.UNDEFINED) { 725 dStyle = font.getStyle(); 726 } 727 728 Color dColor = font.getColor(); 729 if(dColor == null) { 730 dColor = this.getColor(); 731 } 732 733 int dCharset = this.charset; 734 if(font instanceof RtfFont) { 735 dCharset = ((RtfFont) font).getCharset(); 736 } 737 738 return new RtfFont(dFamilyname, dSize, dStyle, dColor, dCharset); 739 } 740 } 741 | Popular Tags |