1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
package com.lowagie.text.rtf.style;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.rtf.RtfBasicElement;
import com.lowagie.text.rtf.document.RtfDocument;
import com.lowagie.text.rtf.text.RtfParagraph;
/**
* The RtfParagraphStyle stores all style/formatting attributes of a RtfParagraph.
* Additionally it also supports the style name system available in RTF. The RtfParagraphStyle
* is a Font and can thus be used as such. To use the stylesheet functionality
* it needs to be set as the font of a Paragraph. Otherwise it will work like a
* RtfFont. It also supports inheritance of styles.
*
* @version $Revision: 1.4 $
* @author Mark Hall (mhall@edu.uni-klu.ac.at)
*/
public class RtfParagraphStyle extends RtfFont {
/**
* Constant for left alignment
*/
public static final byte[] ALIGN_LEFT = "\\ql".getBytes();
/**
* Constant for right alignment
*/
public static final byte[] ALIGN_RIGHT = "\\qr".getBytes();
/**
* Constant for center alignment
*/
public static final byte[] ALIGN_CENTER = "\\qc".getBytes();
/**
* Constant for justified alignment
*/
public static final byte[] ALIGN_JUSTIFY = "\\qj".getBytes();
/**
* Constant for left indentation
*/
public static final byte[] INDENT_LEFT = "\\li".getBytes();
/**
* Constant for right indentation
*/
public static final byte[] INDENT_RIGHT = "\\ri".getBytes();
/**
* Constant for keeping the paragraph together on one page
*/
public static final byte[] KEEP_TOGETHER = "\\keep".getBytes();
/**
* Constant for keeping the paragraph toghether with the next one on one page
*/
public static final byte[] KEEP_TOGETHER_WITH_NEXT = "\\keepn".getBytes();
/**
* Constant for the space after the paragraph.
*/
public static final byte[] SPACING_AFTER = "\\sa".getBytes();
/**
* Constant for the space before the paragraph.
*/
public static final byte[] SPACING_BEFORE = "\\sb".getBytes();
/**
* The NORMAL/STANDARD style.
*/
public static final RtfParagraphStyle STYLE_NORMAL = new RtfParagraphStyle("Normal", "Arial", 12, Font.NORMAL, Color.black);
/**
* The style for level 1 headings.
*/
public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle("heading 1", "Normal");
/**
* The style for level 2 headings.
*/
public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle("heading 2", "Normal");
/**
* The style for level 3 headings.
*/
public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle("heading 3", "Normal");
/**
* Initialises the properties of the styles.
*/
static {
STYLE_HEADING_1.setSize(16);
STYLE_HEADING_1.setStyle(Font.BOLD);
STYLE_HEADING_2.setSize(14);
STYLE_HEADING_2.setStyle(Font.BOLDITALIC);
STYLE_HEADING_3.setSize(13);
STYLE_HEADING_3.setStyle(Font.BOLD);
}
/**
* No modification has taken place when compared to the RtfParagraphStyle this RtfParagraphStyle
* is based on. These modification markers are used to determine what needs to be
* inherited and what not from the parent RtfParagraphStyle.
*/
private static final int MODIFIED_NONE = 0;
/**
* The alignment has been modified.
*/
private static final int MODIFIED_ALIGNMENT = 1;
/**
* The left indentation has been modified.
*/
private static final int MODIFIED_INDENT_LEFT = 2;
/**
* The right indentation has been modified.
*/
private static final int MODIFIED_INDENT_RIGHT = 4;
/**
* The spacing before a paragraph has been modified.
*/
private static final int MODIFIED_SPACING_BEFORE = 8;
/**
* The spacing after a paragraph has been modified.
*/
private static final int MODIFIED_SPACING_AFTER = 16;
/**
* The font name has been modified.
*/
private static final int MODIFIED_FONT_NAME = 32;
/**
* The font style has been modified.
*/
private static final int MODIFIED_FONT_SIZE = 64;
/**
* The font size has been modified.
*/
private static final int MODIFIED_FONT_STYLE = 128;
/**
* The font colour has been modified.
*/
private static final int MODIFIED_FONT_COLOR = 256;
/**
* The line leading has been modified.
*/
private static final int MODIFIED_LINE_LEADING = 512;
/**
* The paragraph keep together setting has been modified.
*/
private static final int MODIFIED_KEEP_TOGETHER = 1024;
/**
* The paragraph keep together with next setting has been modified.
*/
private static final int MODIFIED_KEEP_TOGETHER_WITH_NEXT = 2048;
/**
* The alignment of the paragraph.
*/
private int alignment = Element.ALIGN_LEFT;
/**
* The left indentation of the paragraph.
*/
private int indentLeft = 0;
/**
* The right indentation of the paragraph.
*/
private int indentRight = 0;
/**
* The spacing before a paragraph.
*/
private int spacingBefore = 0;
/**
* The spacing after a paragraph.
*/
private int spacingAfter = 0;
/**
* The line leading of the paragraph.
*/
private int lineLeading = 0;
/**
* Whether this RtfParagraph must stay on one page.
*/
private boolean keepTogether = false;
/**
* Whether this RtfParagraph must stay on the same page as the next paragraph.
*/
private boolean keepTogetherWithNext = false;
/**
* The name of this RtfParagraphStyle.
*/
private String styleName = "";
/**
* The name of the RtfParagraphStyle this RtfParagraphStyle is based on.
*/
private String basedOnName = null;
/**
* The RtfParagraphStyle this RtfParagraphStyle is based on.
*/
private RtfParagraphStyle baseStyle = null;
/**
* Which properties have been modified when compared to the base style.
*/
private int modified = MODIFIED_NONE;
/**
* The number of this RtfParagraphStyle in the stylesheet list.
*/
private int styleNumber = -1;
/**
* Constructs a new RtfParagraphStyle with the given attributes.
*
* @param styleName The name of this RtfParagraphStyle.
* @param fontName The name of the font to use for this RtfParagraphStyle.
* @param fontSize The size of the font to use for this RtfParagraphStyle.
* @param fontStyle The style of the font to use for this RtfParagraphStyle.
* @param fontColor The colour of the font to use for this RtfParagraphStyle.
*/
public RtfParagraphStyle(String styleName, String fontName, int fontSize, int fontStyle, Color fontColor) {
super(null, new RtfFont(fontName, fontSize, fontStyle, fontColor));
this.styleName = styleName;
}
/**
* Constructs a new RtfParagraphStyle that is based on an existing RtfParagraphStyle.
*
* @param styleName The name of this RtfParagraphStyle.
* @param basedOnName The name of the RtfParagraphStyle this RtfParagraphStyle is based on.
*/
public RtfParagraphStyle(String styleName, String basedOnName) {
super(null, new Font());
this.styleName = styleName;
this.basedOnName = basedOnName;
}
/**
* Constructs a RtfParagraphStyle from another RtfParagraphStyle.
*
* INTERNAL USE ONLY
*
* @param doc The RtfDocument this RtfParagraphStyle belongs to.
* @param style The RtfParagraphStyle to copy settings from.
*/
public RtfParagraphStyle(RtfDocument doc, RtfParagraphStyle style) {
super(doc, style);
this.document = doc;
this.styleName = style.getStyleName();
this.alignment = style.getAlignment();
this.indentLeft = (int) (style.getIndentLeft() * RtfBasicElement.TWIPS_FACTOR);
this.indentRight = (int) (style.getIndentRight() * RtfBasicElement.TWIPS_FACTOR);
this.spacingBefore = (int) (style.getSpacingBefore() * RtfBasicElement.TWIPS_FACTOR);
this.spacingAfter = (int) (style.getSpacingAfter() * RtfBasicElement.TWIPS_FACTOR);
this.lineLeading = (int) (style.getLineLeading() * RtfBasicElement.TWIPS_FACTOR);
this.keepTogether = style.getKeepTogether();
this.keepTogetherWithNext = style.getKeepTogetherWithNext();
this.basedOnName = style.basedOnName;
this.modified = style.modified;
this.styleNumber = style.getStyleNumber();
if(this.document != null) {
setRtfDocument(this.document);
}
}
/**
* Gets the name of this RtfParagraphStyle.
*
* @return The name of this RtfParagraphStyle.
*/
public String getStyleName() {
return this.styleName;
}
/**
* Gets the name of the RtfParagraphStyle this RtfParagraphStyle is based on.
*
* @return The name of the base RtfParagraphStyle.
*/
public String getBasedOnName() {
return this.basedOnName;
}
/**
* Gets the alignment of this RtfParagraphStyle.
*
* @return The alignment of this RtfParagraphStyle.
*/
public int getAlignment() {
return this.alignment;
}
/**
* Sets the alignment of this RtfParagraphStyle.
*
* @param alignment The alignment to use.
*/
public void setAlignment(int alignment) {
this.modified = this.modified | MODIFIED_ALIGNMENT;
this.alignment = alignment;
}
/**
* Gets the left indentation of this RtfParagraphStyle.
*
* @return The left indentation of this RtfParagraphStyle.
*/
public int getIndentLeft() {
return this.indentLeft;
}
/**
* Sets the left indentation of this RtfParagraphStyle.
*
* @param indentLeft The left indentation to use.
*/
public void setIndentLeft(int indentLeft) {
this.modified = this.modified | MODIFIED_INDENT_LEFT;
this.indentLeft = indentLeft;
}
/**
* Gets the right indentation of this RtfParagraphStyle.
*
* @return The right indentation of this RtfParagraphStyle.
*/
public int getIndentRight() {
return this.indentRight;
}
/**
* Sets the right indentation of this RtfParagraphStyle.
*
* @param indentRight The right indentation to use.
*/
public void setIndentRight(int indentRight) {
this.modified = this.modified | MODIFIED_INDENT_RIGHT;
this.indentRight = indentRight;
}
/**
* Gets the space before the paragraph of this RtfParagraphStyle..
*
* @return The space before the paragraph.
*/
public int getSpacingBefore() {
return this.spacingBefore;
}
/**
* Sets the space before the paragraph of this RtfParagraphStyle.
*
* @param spacingBefore The space before to use.
*/
public void setSpacingBefore(int spacingBefore) {
this.modified = this.modified | MODIFIED_SPACING_BEFORE;
this.spacingBefore = spacingBefore;
}
/**
* Gets the space after the paragraph of this RtfParagraphStyle.
*
* @return The space after the paragraph.
*/
public int getSpacingAfter() {
return this.spacingAfter;
}
/**
* Sets the space after the paragraph of this RtfParagraphStyle.
*
* @param spacingAfter The space after to use.
*/
public void setSpacingAfter(int spacingAfter) {
this.modified = this.modified | MODIFIED_SPACING_AFTER;
this.spacingAfter = spacingAfter;
}
/**
* Sets the font name of this RtfParagraphStyle.
*
* @param fontName The font name to use
*/
public void setFontName(String fontName) {
this.modified = this.modified | MODIFIED_FONT_NAME;
super.setFontName(fontName);
}
/**
* Sets the font size of this RtfParagraphStyle.
*
* @param fontSize The font size to use.
*/
public void setSize(float fontSize) {
this.modified = this.modified | MODIFIED_FONT_SIZE;
super.setSize(fontSize);
}
/**
* Sets the font style of this RtfParagraphStyle.
*
* @param fontStyle The font style to use.
*/
public void setStyle(int fontStyle) {
this.modified = this.modified | MODIFIED_FONT_STYLE;
super.setStyle(fontStyle);
}
/**
* Sets the colour of this RtfParagraphStyle.
*
* @param color The Color to use.
*/
public void setColor(Color color) {
this.modified = this.modified | MODIFIED_FONT_COLOR;
super.setColor(color);
}
/**
* Gets the line leading of this RtfParagraphStyle.
*
* @return The line leading of this RtfParagraphStyle.
*/
public int getLineLeading() {
return this.lineLeading;
}
/**
* Sets the line leading of this RtfParagraphStyle.
*
* @param lineLeading The line leading to use.
*/
public void setLineLeading(int lineLeading) {
this.lineLeading = lineLeading;
this.modified = this.modified | MODIFIED_LINE_LEADING;
}
/**
* Gets whether the lines in the paragraph should be kept together in
* this RtfParagraphStyle.
*
* @return Whether the lines in the paragraph should be kept together.
*/
public boolean getKeepTogether() {
return this.keepTogether;
}
/**
* Sets whether the lines in the paragraph should be kept together in
* this RtfParagraphStyle.
*
* @param keepTogether Whether the lines in the paragraph should be kept together.
*/
public void setKeepTogether(boolean keepTogether) {
this.keepTogether = keepTogether;
this.modified = this.modified | MODIFIED_KEEP_TOGETHER;
}
/**
* Gets whether the paragraph should be kept toggether with the next in
* this RtfParagraphStyle.
*
* @return Whether the paragraph should be kept together with the next.
*/
public boolean getKeepTogetherWithNext() {
return this.keepTogetherWithNext;
}
/**
* Sets whether the paragraph should be kept together with the next in
* this RtfParagraphStyle.
*
* @param keepTogetherWithNext Whether the paragraph should be kept together with the next.
*/
public void setKeepTogetherWithNext(boolean keepTogetherWithNext) {
this.keepTogetherWithNext = keepTogetherWithNext;
this.modified = this.modified | MODIFIED_KEEP_TOGETHER_WITH_NEXT;
}
/**
* Handles the inheritance of paragraph style settings. All settings that
* have not been modified will be inherited from the base RtfParagraphStyle.
* If this RtfParagraphStyle is not based on another one, then nothing happens.
*/
public void handleInheritance() {
if(this.basedOnName != null && this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName) != null) {
this.baseStyle = this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName);
this.baseStyle.handleInheritance();
if(!((this.modified & MODIFIED_ALIGNMENT) == MODIFIED_ALIGNMENT)) {
this.alignment = this.baseStyle.getAlignment();
}
if(!((this.modified & MODIFIED_INDENT_LEFT) == MODIFIED_INDENT_LEFT)) {
this.indentLeft = this.baseStyle.getIndentLeft();
}
if(!((this.modified & MODIFIED_INDENT_RIGHT) == MODIFIED_INDENT_RIGHT)) {
this.indentRight = this.baseStyle.getIndentRight();
}
if(!((this.modified & MODIFIED_SPACING_BEFORE) == MODIFIED_SPACING_BEFORE)) {
this.spacingBefore = this.baseStyle.getSpacingBefore();
}
if(!((this.modified & MODIFIED_SPACING_AFTER) == MODIFIED_SPACING_AFTER)) {
this.spacingAfter = this.baseStyle.getSpacingAfter();
}
if(!((this.modified & MODIFIED_FONT_NAME) == MODIFIED_FONT_NAME)) {
setFontName(this.baseStyle.getFontName());
}
if(!((this.modified & MODIFIED_FONT_SIZE) == MODIFIED_FONT_SIZE)) {
setSize(this.baseStyle.getFontSize());
}
if(!((this.modified & MODIFIED_FONT_STYLE) == MODIFIED_FONT_STYLE)) {
setStyle(this.baseStyle.getFontStyle());
}
if(!((this.modified & MODIFIED_FONT_COLOR) == MODIFIED_FONT_COLOR)) {
setColor(this.baseStyle.color());
}
if(!((this.modified & MODIFIED_LINE_LEADING) == MODIFIED_LINE_LEADING)) {
setLineLeading(this.baseStyle.getLineLeading());
}
if(!((this.modified & MODIFIED_KEEP_TOGETHER) == MODIFIED_KEEP_TOGETHER)) {
setKeepTogether(this.baseStyle.getKeepTogether());
}
if(!((this.modified & MODIFIED_KEEP_TOGETHER_WITH_NEXT) == MODIFIED_KEEP_TOGETHER_WITH_NEXT)) {
setKeepTogetherWithNext(this.baseStyle.getKeepTogetherWithNext());
}
}
}
/**
* Writes the settings of this RtfParagraphStyle.
*
* @return A byte array with the settings of this RtfParagraphStyle.
*/
private byte[] writeParagraphSettings() {
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
if(this.keepTogether) {
result.write(RtfParagraphStyle.KEEP_TOGETHER);
}
if(this.keepTogetherWithNext) {
result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
}
switch (alignment) {
case Element.ALIGN_LEFT:
result.write(RtfParagraphStyle.ALIGN_LEFT);
break;
case Element.ALIGN_RIGHT:
result.write(RtfParagraphStyle.ALIGN_RIGHT);
break;
case Element.ALIGN_CENTER:
result.write(RtfParagraphStyle.ALIGN_CENTER);
break;
case Element.ALIGN_JUSTIFIED:
case Element.ALIGN_JUSTIFIED_ALL:
result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
break;
}
result.write(RtfParagraphStyle.INDENT_LEFT);
result.write(intToByteArray(indentLeft));
result.write(RtfParagraphStyle.INDENT_RIGHT);
result.write(intToByteArray(indentRight));
if(this.spacingBefore > 0) {
result.write(RtfParagraphStyle.SPACING_BEFORE);
result.write(intToByteArray(this.spacingBefore));
}
if(this.spacingAfter > 0) {
result.write(RtfParagraphStyle.SPACING_AFTER);
result.write(intToByteArray(this.spacingAfter));
}
if(this.lineLeading > 0) {
result.write(RtfParagraph.LINE_SPACING);
result.write(intToByteArray(this.lineLeading));
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
return result.toByteArray();
}
/**
* Writes the definition of this RtfParagraphStyle for the stylesheet list.
*/
public byte[] writeDefinition() {
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
result.write("{".getBytes());
result.write("\\style".getBytes());
result.write("\\s".getBytes());
result.write(intToByteArray(this.styleNumber));
result.write(RtfBasicElement.DELIMITER);
result.write(writeParagraphSettings());
result.write(super.writeBegin());
result.write(RtfBasicElement.DELIMITER);
result.write(this.styleName.getBytes());
result.write(";".getBytes());
result.write("}".getBytes());
if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) {
result.write('\n');
}
} catch(IOException ioe) {
ioe.printStackTrace();
}
return result.toByteArray();
}
/**
* Writes the start information of this RtfParagraphStyle.
*/
public byte[] writeBegin() {
ByteArrayOutputStream result = new ByteArrayOutputStream();
try {
result.write("\\s".getBytes());
result.write(intToByteArray(this.styleNumber));
result.write(writeParagraphSettings());
} catch(IOException ioe) {
ioe.printStackTrace();
}
return result.toByteArray();
}
/**
* Unused
* @return An empty byte array.
*/
public byte[] writeEnd() {
return new byte[0];
}
/**
* Unused
* @return An empty byte array.
*/
public byte[] write() {
return new byte[0];
}
/**
* Tests whether two RtfParagraphStyles are equal. Equality
* is determined via the name.
*/
public boolean equals(Object o) {
if(o == null || !(o instanceof RtfParagraphStyle)) {
return false;
}
RtfParagraphStyle paragraphStyle = (RtfParagraphStyle) o;
boolean result = this.getStyleName().equals(paragraphStyle.getStyleName());
return result;
}
/**
* Gets the hash code of this RtfParagraphStyle.
*/
public int hashCode() {
return this.styleName.hashCode();
}
/**
* Gets the number of this RtfParagraphStyle in the stylesheet list.
*
* @return The number of this RtfParagraphStyle in the stylesheet list.
*/
private int getStyleNumber() {
return this.styleNumber;
}
/**
* Sets the number of this RtfParagraphStyle in the stylesheet list.
*
* @param styleNumber The number to use.
*/
protected void setStyleNumber(int styleNumber) {
this.styleNumber = styleNumber;
}
}
|