1 50 package com.lowagie.text.rtf.style; 51 52 import java.awt.Color ; 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 57 import com.lowagie.text.Element; 58 import com.lowagie.text.Font; 59 import com.lowagie.text.rtf.RtfBasicElement; 60 import com.lowagie.text.rtf.document.RtfDocument; 61 import com.lowagie.text.rtf.text.RtfParagraph; 62 63 74 public class RtfParagraphStyle extends RtfFont { 75 76 79 public static final byte[] ALIGN_LEFT = "\\ql".getBytes(); 80 83 public static final byte[] ALIGN_RIGHT = "\\qr".getBytes(); 84 87 public static final byte[] ALIGN_CENTER = "\\qc".getBytes(); 88 91 public static final byte[] ALIGN_JUSTIFY = "\\qj".getBytes(); 92 95 public static final byte[] FIRST_LINE_INDENT = "\\fi".getBytes(); 96 99 public static final byte[] INDENT_LEFT = "\\li".getBytes(); 100 103 public static final byte[] INDENT_RIGHT = "\\ri".getBytes(); 104 107 public static final byte[] KEEP_TOGETHER = "\\keep".getBytes(); 108 111 public static final byte[] KEEP_TOGETHER_WITH_NEXT = "\\keepn".getBytes(); 112 115 public static final byte[] SPACING_AFTER = "\\sa".getBytes(); 116 119 public static final byte[] SPACING_BEFORE = "\\sb".getBytes(); 120 121 124 public static final RtfParagraphStyle STYLE_NORMAL = new RtfParagraphStyle("Normal", "Arial", 12, Font.NORMAL, Color.black); 125 128 public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle("heading 1", "Normal"); 129 132 public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle("heading 2", "Normal"); 133 136 public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle("heading 3", "Normal"); 137 138 141 static { 142 STYLE_HEADING_1.setSize(16); 143 STYLE_HEADING_1.setStyle(Font.BOLD); 144 STYLE_HEADING_2.setSize(14); 145 STYLE_HEADING_2.setStyle(Font.BOLDITALIC); 146 STYLE_HEADING_3.setSize(13); 147 STYLE_HEADING_3.setStyle(Font.BOLD); 148 } 149 150 155 private static final int MODIFIED_NONE = 0; 156 159 private static final int MODIFIED_ALIGNMENT = 1; 160 163 private static final int MODIFIED_INDENT_LEFT = 2; 164 167 private static final int MODIFIED_INDENT_RIGHT = 4; 168 171 private static final int MODIFIED_SPACING_BEFORE = 8; 172 175 private static final int MODIFIED_SPACING_AFTER = 16; 176 179 private static final int MODIFIED_FONT_NAME = 32; 180 183 private static final int MODIFIED_FONT_SIZE = 64; 184 187 private static final int MODIFIED_FONT_STYLE = 128; 188 191 private static final int MODIFIED_FONT_COLOR = 256; 192 195 private static final int MODIFIED_LINE_LEADING = 512; 196 199 private static final int MODIFIED_KEEP_TOGETHER = 1024; 200 203 private static final int MODIFIED_KEEP_TOGETHER_WITH_NEXT = 2048; 204 205 208 private int alignment = Element.ALIGN_LEFT; 209 212 private int firstLineIndent = 0; 213 216 private int indentLeft = 0; 217 220 private int indentRight = 0; 221 224 private int spacingBefore = 0; 225 228 private int spacingAfter = 0; 229 232 private int lineLeading = 0; 233 236 private boolean keepTogether = false; 237 240 private boolean keepTogetherWithNext = false; 241 244 private String styleName = ""; 245 248 private String basedOnName = null; 249 252 private RtfParagraphStyle baseStyle = null; 253 256 private int modified = MODIFIED_NONE; 257 260 private int styleNumber = -1; 261 262 271 public RtfParagraphStyle(String styleName, String fontName, int fontSize, int fontStyle, Color fontColor) { 272 super(null, new RtfFont(fontName, fontSize, fontStyle, fontColor)); 273 this.styleName = styleName; 274 } 275 276 282 public RtfParagraphStyle(String styleName, String basedOnName) { 283 super(null, new Font()); 284 this.styleName = styleName; 285 this.basedOnName = basedOnName; 286 } 287 288 296 public RtfParagraphStyle(RtfDocument doc, RtfParagraphStyle style) { 297 super(doc, style); 298 this.document = doc; 299 this.styleName = style.getStyleName(); 300 this.alignment = style.getAlignment(); 301 this.indentLeft = (int) (style.getIndentLeft() * RtfBasicElement.TWIPS_FACTOR); 302 this.indentRight = (int) (style.getIndentRight() * RtfBasicElement.TWIPS_FACTOR); 303 this.spacingBefore = (int) (style.getSpacingBefore() * RtfBasicElement.TWIPS_FACTOR); 304 this.spacingAfter = (int) (style.getSpacingAfter() * RtfBasicElement.TWIPS_FACTOR); 305 this.lineLeading = (int) (style.getLineLeading() * RtfBasicElement.TWIPS_FACTOR); 306 this.keepTogether = style.getKeepTogether(); 307 this.keepTogetherWithNext = style.getKeepTogetherWithNext(); 308 this.basedOnName = style.basedOnName; 309 this.modified = style.modified; 310 this.styleNumber = style.getStyleNumber(); 311 312 if(this.document != null) { 313 setRtfDocument(this.document); 314 } 315 } 316 317 322 public String getStyleName() { 323 return this.styleName; 324 } 325 326 331 public String getBasedOnName() { 332 return this.basedOnName; 333 } 334 335 340 public int getAlignment() { 341 return this.alignment; 342 } 343 344 349 public void setAlignment(int alignment) { 350 this.modified = this.modified | MODIFIED_ALIGNMENT; 351 this.alignment = alignment; 352 } 353 354 359 public int getFirstLineIndent() { 360 return this.firstLineIndent; 361 } 362 363 369 public void setFirstLineIndent(int firstLineIndent) { 370 this.firstLineIndent = firstLineIndent; 371 } 372 373 378 public int getIndentLeft() { 379 return this.indentLeft; 380 } 381 382 387 public void setIndentLeft(int indentLeft) { 388 this.modified = this.modified | MODIFIED_INDENT_LEFT; 389 this.indentLeft = indentLeft; 390 } 391 392 397 public int getIndentRight() { 398 return this.indentRight; 399 } 400 401 406 public void setIndentRight(int indentRight) { 407 this.modified = this.modified | MODIFIED_INDENT_RIGHT; 408 this.indentRight = indentRight; 409 } 410 411 416 public int getSpacingBefore() { 417 return this.spacingBefore; 418 } 419 420 425 public void setSpacingBefore(int spacingBefore) { 426 this.modified = this.modified | MODIFIED_SPACING_BEFORE; 427 this.spacingBefore = spacingBefore; 428 } 429 430 435 public int getSpacingAfter() { 436 return this.spacingAfter; 437 } 438 439 444 public void setSpacingAfter(int spacingAfter) { 445 this.modified = this.modified | MODIFIED_SPACING_AFTER; 446 this.spacingAfter = spacingAfter; 447 } 448 449 454 public void setFontName(String fontName) { 455 this.modified = this.modified | MODIFIED_FONT_NAME; 456 super.setFontName(fontName); 457 } 458 459 464 public void setSize(float fontSize) { 465 this.modified = this.modified | MODIFIED_FONT_SIZE; 466 super.setSize(fontSize); 467 } 468 469 474 public void setStyle(int fontStyle) { 475 this.modified = this.modified | MODIFIED_FONT_STYLE; 476 super.setStyle(fontStyle); 477 } 478 479 484 public void setColor(Color color) { 485 this.modified = this.modified | MODIFIED_FONT_COLOR; 486 super.setColor(color); 487 } 488 489 494 public int getLineLeading() { 495 return this.lineLeading; 496 } 497 498 503 public void setLineLeading(int lineLeading) { 504 this.lineLeading = lineLeading; 505 this.modified = this.modified | MODIFIED_LINE_LEADING; 506 } 507 508 514 public boolean getKeepTogether() { 515 return this.keepTogether; 516 } 517 518 524 public void setKeepTogether(boolean keepTogether) { 525 this.keepTogether = keepTogether; 526 this.modified = this.modified | MODIFIED_KEEP_TOGETHER; 527 } 528 529 535 public boolean getKeepTogetherWithNext() { 536 return this.keepTogetherWithNext; 537 } 538 539 545 public void setKeepTogetherWithNext(boolean keepTogetherWithNext) { 546 this.keepTogetherWithNext = keepTogetherWithNext; 547 this.modified = this.modified | MODIFIED_KEEP_TOGETHER_WITH_NEXT; 548 } 549 550 555 public void handleInheritance() { 556 if(this.basedOnName != null && this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName) != null) { 557 this.baseStyle = this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName); 558 this.baseStyle.handleInheritance(); 559 if(!((this.modified & MODIFIED_ALIGNMENT) == MODIFIED_ALIGNMENT)) { 560 this.alignment = this.baseStyle.getAlignment(); 561 } 562 if(!((this.modified & MODIFIED_INDENT_LEFT) == MODIFIED_INDENT_LEFT)) { 563 this.indentLeft = this.baseStyle.getIndentLeft(); 564 } 565 if(!((this.modified & MODIFIED_INDENT_RIGHT) == MODIFIED_INDENT_RIGHT)) { 566 this.indentRight = this.baseStyle.getIndentRight(); 567 } 568 if(!((this.modified & MODIFIED_SPACING_BEFORE) == MODIFIED_SPACING_BEFORE)) { 569 this.spacingBefore = this.baseStyle.getSpacingBefore(); 570 } 571 if(!((this.modified & MODIFIED_SPACING_AFTER) == MODIFIED_SPACING_AFTER)) { 572 this.spacingAfter = this.baseStyle.getSpacingAfter(); 573 } 574 if(!((this.modified & MODIFIED_FONT_NAME) == MODIFIED_FONT_NAME)) { 575 setFontName(this.baseStyle.getFontName()); 576 } 577 if(!((this.modified & MODIFIED_FONT_SIZE) == MODIFIED_FONT_SIZE)) { 578 setSize(this.baseStyle.getFontSize()); 579 } 580 if(!((this.modified & MODIFIED_FONT_STYLE) == MODIFIED_FONT_STYLE)) { 581 setStyle(this.baseStyle.getFontStyle()); 582 } 583 if(!((this.modified & MODIFIED_FONT_COLOR) == MODIFIED_FONT_COLOR)) { 584 setColor(this.baseStyle.getColor()); 585 } 586 if(!((this.modified & MODIFIED_LINE_LEADING) == MODIFIED_LINE_LEADING)) { 587 setLineLeading(this.baseStyle.getLineLeading()); 588 } 589 if(!((this.modified & MODIFIED_KEEP_TOGETHER) == MODIFIED_KEEP_TOGETHER)) { 590 setKeepTogether(this.baseStyle.getKeepTogether()); 591 } 592 if(!((this.modified & MODIFIED_KEEP_TOGETHER_WITH_NEXT) == MODIFIED_KEEP_TOGETHER_WITH_NEXT)) { 593 setKeepTogetherWithNext(this.baseStyle.getKeepTogetherWithNext()); 594 } 595 } 596 } 597 598 603 private byte[] writeParagraphSettings() { 604 ByteArrayOutputStream result = new ByteArrayOutputStream (); 605 try { 606 if(this.keepTogether) { 607 result.write(RtfParagraphStyle.KEEP_TOGETHER); 608 } 609 if(this.keepTogetherWithNext) { 610 result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT); 611 } 612 switch (alignment) { 613 case Element.ALIGN_LEFT: 614 result.write(RtfParagraphStyle.ALIGN_LEFT); 615 break; 616 case Element.ALIGN_RIGHT: 617 result.write(RtfParagraphStyle.ALIGN_RIGHT); 618 break; 619 case Element.ALIGN_CENTER: 620 result.write(RtfParagraphStyle.ALIGN_CENTER); 621 break; 622 case Element.ALIGN_JUSTIFIED: 623 case Element.ALIGN_JUSTIFIED_ALL: 624 result.write(RtfParagraphStyle.ALIGN_JUSTIFY); 625 break; 626 } 627 result.write(FIRST_LINE_INDENT); 628 result.write(intToByteArray(this.firstLineIndent)); 629 result.write(RtfParagraphStyle.INDENT_LEFT); 630 result.write(intToByteArray(indentLeft)); 631 result.write(RtfParagraphStyle.INDENT_RIGHT); 632 result.write(intToByteArray(indentRight)); 633 if(this.spacingBefore > 0) { 634 result.write(RtfParagraphStyle.SPACING_BEFORE); 635 result.write(intToByteArray(this.spacingBefore)); 636 } 637 if(this.spacingAfter > 0) { 638 result.write(RtfParagraphStyle.SPACING_AFTER); 639 result.write(intToByteArray(this.spacingAfter)); 640 } 641 if(this.lineLeading > 0) { 642 result.write(RtfParagraph.LINE_SPACING); 643 result.write(intToByteArray(this.lineLeading)); 644 } 645 } catch(IOException ioe) { 646 ioe.printStackTrace(); 647 } 648 return result.toByteArray(); 649 } 650 651 655 public byte[] writeDefinition() { 656 ByteArrayOutputStream result = new ByteArrayOutputStream (); 657 try { 658 writeDefinition(result); 659 } catch(IOException ioe) { 660 ioe.printStackTrace(); 661 } 662 return result.toByteArray(); 663 } 664 667 public void writeDefinition(final OutputStream result) throws IOException 668 { 669 result.write("{".getBytes()); 670 result.write("\\style".getBytes()); 671 result.write("\\s".getBytes()); 672 result.write(intToByteArray(this.styleNumber)); 673 result.write(RtfBasicElement.DELIMITER); 674 result.write(writeParagraphSettings()); 675 result.write(super.writeBegin()); 676 result.write(RtfBasicElement.DELIMITER); 677 result.write(this.styleName.getBytes()); 678 result.write(";".getBytes()); 679 result.write("}".getBytes()); 680 if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) { 681 result.write('\n'); 682 } 683 } 684 685 688 public byte[] writeBegin() { 689 ByteArrayOutputStream result = new ByteArrayOutputStream (); 690 try { 691 result.write("\\s".getBytes()); 692 result.write(intToByteArray(this.styleNumber)); 693 result.write(writeParagraphSettings()); 694 } catch(IOException ioe) { 695 ioe.printStackTrace(); 696 } 697 return result.toByteArray(); 698 } 699 700 704 public byte[] writeEnd() { 705 return new byte[0]; 706 } 707 708 712 public byte[] write() 713 { 714 return(new byte[0]); 715 } 716 719 public void writeContent(final OutputStream out) throws IOException 720 { 721 } 722 723 727 public boolean equals(Object o) { 728 if(!(o instanceof RtfParagraphStyle)) { 729 return false; 730 } 731 RtfParagraphStyle paragraphStyle = (RtfParagraphStyle) o; 732 boolean result = this.getStyleName().equals(paragraphStyle.getStyleName()); 733 return result; 734 } 735 736 739 public int hashCode() { 740 return this.styleName.hashCode(); 741 } 742 743 748 private int getStyleNumber() { 749 return this.styleNumber; 750 } 751 752 757 protected void setStyleNumber(int styleNumber) { 758 this.styleNumber = styleNumber; 759 } 760 } 761 | Popular Tags |