2
* $Id: RtfParagraphStyle.java,v 1.5 2006/08/07 10:42:35 blowagie Exp $
5
* Copyright 2001, 2002, 2003, 2004 by Mark Hall
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* (the "License"); you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the License.
15
* The Original Code is 'iText, a free JAVA-PDF library'.
17
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19
* All Rights Reserved.
20
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
23
* Contributor(s): all the names of the contributors are added in the source code
26
* Alternatively, the contents of this file may be used under the terms of the
27
* LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
28
* provisions of LGPL are applicable instead of those above. If you wish to
29
* allow use of your version of this file only under the terms of the LGPL
30
* License and not to allow others to use your version of this file under
31
* the MPL, indicate your decision by deleting the provisions above and
32
* replace them with the notice and other provisions required by the LGPL.
33
* If you do not delete the provisions above, a recipient may use your version
34
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36
* This library is free software; you can redistribute it and/or modify it
37
* under the terms of the MPL as stated above or under the terms of the GNU
38
* Library General Public License as published by the Free Software Foundation;
39
* either version 2 of the License, or any later version.
41
* This library is distributed in the hope that it will be useful, but WITHOUT
42
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46
* If you didn't download this code from the following link, you should check if
47
* you aren't using an obsolete version:
48
* http://www.lowagie.com/iText/
50
package com.lowagie.text.rtf.style;
52
import java.awt.Color;
53
import java.io.ByteArrayOutputStream;
54
import java.io.IOException;
56
import com.lowagie.text.Element;
57
import com.lowagie.text.Font;
58
import com.lowagie.text.rtf.RtfBasicElement;
59
import com.lowagie.text.rtf.document.RtfDocument;
60
import com.lowagie.text.rtf.text.RtfParagraph;
63
* The RtfParagraphStyle stores all style/formatting attributes of a RtfParagraph.
64
* Additionally it also supports the style name system available in RTF. The RtfParagraphStyle
65
* is a Font and can thus be used as such. To use the stylesheet functionality
66
* it needs to be set as the font of a Paragraph. Otherwise it will work like a
67
* RtfFont. It also supports inheritance of styles.
69
* @version $Revision: 1.5 $
70
* @author Mark Hall (mhall@edu.uni-klu.ac.at)
72
public class RtfParagraphStyle extends RtfFont {
75
* Constant for left alignment
77
public static final byte[] ALIGN_LEFT = "\\ql".getBytes();
79
* Constant for right alignment
81
public static final byte[] ALIGN_RIGHT = "\\qr".getBytes();
83
* Constant for center alignment
85
public static final byte[] ALIGN_CENTER = "\\qc".getBytes();
87
* Constant for justified alignment
89
public static final byte[] ALIGN_JUSTIFY = "\\qj".getBytes();
91
* Constant for left indentation
93
public static final byte[] INDENT_LEFT = "\\li".getBytes();
95
* Constant for right indentation
97
public static final byte[] INDENT_RIGHT = "\\ri".getBytes();
99
* Constant for keeping the paragraph together on one page
101
public static final byte[] KEEP_TOGETHER = "\\keep".getBytes();
103
* Constant for keeping the paragraph toghether with the next one on one page
105
public static final byte[] KEEP_TOGETHER_WITH_NEXT = "\\keepn".getBytes();
107
* Constant for the space after the paragraph.
109
public static final byte[] SPACING_AFTER = "\\sa".getBytes();
111
* Constant for the space before the paragraph.
113
public static final byte[] SPACING_BEFORE = "\\sb".getBytes();
116
* The NORMAL/STANDARD style.
118
public static final RtfParagraphStyle STYLE_NORMAL = new RtfParagraphStyle("Normal", "Arial", 12, Font.NORMAL, Color.black);
120
* The style for level 1 headings.
122
public static final RtfParagraphStyle STYLE_HEADING_1 = new RtfParagraphStyle("heading 1", "Normal");
124
* The style for level 2 headings.
126
public static final RtfParagraphStyle STYLE_HEADING_2 = new RtfParagraphStyle("heading 2", "Normal");
128
* The style for level 3 headings.
130
public static final RtfParagraphStyle STYLE_HEADING_3 = new RtfParagraphStyle("heading 3", "Normal");
133
* Initialises the properties of the styles.
136
STYLE_HEADING_1.setSize(16);
137
STYLE_HEADING_1.setStyle(Font.BOLD);
138
STYLE_HEADING_2.setSize(14);
139
STYLE_HEADING_2.setStyle(Font.BOLDITALIC);
140
STYLE_HEADING_3.setSize(13);
141
STYLE_HEADING_3.setStyle(Font.BOLD);
145
* No modification has taken place when compared to the RtfParagraphStyle this RtfParagraphStyle
146
* is based on. These modification markers are used to determine what needs to be
147
* inherited and what not from the parent RtfParagraphStyle.
149
private static final int MODIFIED_NONE = 0;
151
* The alignment has been modified.
153
private static final int MODIFIED_ALIGNMENT = 1;
155
* The left indentation has been modified.
157
private static final int MODIFIED_INDENT_LEFT = 2;
159
* The right indentation has been modified.
161
private static final int MODIFIED_INDENT_RIGHT = 4;
163
* The spacing before a paragraph has been modified.
165
private static final int MODIFIED_SPACING_BEFORE = 8;
167
* The spacing after a paragraph has been modified.
169
private static final int MODIFIED_SPACING_AFTER = 16;
171
* The font name has been modified.
173
private static final int MODIFIED_FONT_NAME = 32;
175
* The font style has been modified.
177
private static final int MODIFIED_FONT_SIZE = 64;
179
* The font size has been modified.
181
private static final int MODIFIED_FONT_STYLE = 128;
183
* The font colour has been modified.
185
private static final int MODIFIED_FONT_COLOR = 256;
187
* The line leading has been modified.
189
private static final int MODIFIED_LINE_LEADING = 512;
191
* The paragraph keep together setting has been modified.
193
private static final int MODIFIED_KEEP_TOGETHER = 1024;
195
* The paragraph keep together with next setting has been modified.
197
private static final int MODIFIED_KEEP_TOGETHER_WITH_NEXT = 2048;
200
* The alignment of the paragraph.
202
private int alignment = Element.ALIGN_LEFT;
204
* The left indentation of the paragraph.
206
private int indentLeft = 0;
208
* The right indentation of the paragraph.
210
private int indentRight = 0;
212
* The spacing before a paragraph.
214
private int spacingBefore = 0;
216
* The spacing after a paragraph.
218
private int spacingAfter = 0;
220
* The line leading of the paragraph.
222
private int lineLeading = 0;
224
* Whether this RtfParagraph must stay on one page.
226
private boolean keepTogether = false;
228
* Whether this RtfParagraph must stay on the same page as the next paragraph.
230
private boolean keepTogetherWithNext = false;
232
* The name of this RtfParagraphStyle.
234
private String styleName = "";
236
* The name of the RtfParagraphStyle this RtfParagraphStyle is based on.
238
private String basedOnName = null;
240
* The RtfParagraphStyle this RtfParagraphStyle is based on.
242
private RtfParagraphStyle baseStyle = null;
244
* Which properties have been modified when compared to the base style.
246
private int modified = MODIFIED_NONE;
248
* The number of this RtfParagraphStyle in the stylesheet list.
250
private int styleNumber = -1;
253
* Constructs a new RtfParagraphStyle with the given attributes.
255
* @param styleName The name of this RtfParagraphStyle.
256
* @param fontName The name of the font to use for this RtfParagraphStyle.
257
* @param fontSize The size of the font to use for this RtfParagraphStyle.
258
* @param fontStyle The style of the font to use for this RtfParagraphStyle.
259
* @param fontColor The colour of the font to use for this RtfParagraphStyle.
261
public RtfParagraphStyle(String styleName, String fontName, int fontSize, int fontStyle, Color fontColor) {
262
super(null, new RtfFont(fontName, fontSize, fontStyle, fontColor));
263
this.styleName = styleName;
267
* Constructs a new RtfParagraphStyle that is based on an existing RtfParagraphStyle.
269
* @param styleName The name of this RtfParagraphStyle.
270
* @param basedOnName The name of the RtfParagraphStyle this RtfParagraphStyle is based on.
272
public RtfParagraphStyle(String styleName, String basedOnName) {
273
super(null, new Font());
274
this.styleName = styleName;
275
this.basedOnName = basedOnName;
279
* Constructs a RtfParagraphStyle from another RtfParagraphStyle.
283
* @param doc The RtfDocument this RtfParagraphStyle belongs to.
284
* @param style The RtfParagraphStyle to copy settings from.
286
public RtfParagraphStyle(RtfDocument doc, RtfParagraphStyle style) {
289
this.styleName = style.getStyleName();
290
this.alignment = style.getAlignment();
291
this.indentLeft = (int) (style.getIndentLeft() * RtfBasicElement.TWIPS_FACTOR);
292
this.indentRight = (int) (style.getIndentRight() * RtfBasicElement.TWIPS_FACTOR);
293
this.spacingBefore = (int) (style.getSpacingBefore() * RtfBasicElement.TWIPS_FACTOR);
294
this.spacingAfter = (int) (style.getSpacingAfter() * RtfBasicElement.TWIPS_FACTOR);
295
this.lineLeading = (int) (style.getLineLeading() * RtfBasicElement.TWIPS_FACTOR);
296
this.keepTogether = style.getKeepTogether();
297
this.keepTogetherWithNext = style.getKeepTogetherWithNext();
298
this.basedOnName = style.basedOnName;
299
this.modified = style.modified;
300
this.styleNumber = style.getStyleNumber();
302
if(this.document != null) {
303
setRtfDocument(this.document);
308
* Gets the name of this RtfParagraphStyle.
310
* @return The name of this RtfParagraphStyle.
312
public String getStyleName() {
313
return this.styleName;
317
* Gets the name of the RtfParagraphStyle this RtfParagraphStyle is based on.
319
* @return The name of the base RtfParagraphStyle.
321
public String getBasedOnName() {
322
return this.basedOnName;
326
* Gets the alignment of this RtfParagraphStyle.
328
* @return The alignment of this RtfParagraphStyle.
330
public int getAlignment() {
331
return this.alignment;
335
* Sets the alignment of this RtfParagraphStyle.
337
* @param alignment The alignment to use.
339
public void setAlignment(int alignment) {
340
this.modified = this.modified | MODIFIED_ALIGNMENT;
341
this.alignment = alignment;
345
* Gets the left indentation of this RtfParagraphStyle.
347
* @return The left indentation of this RtfParagraphStyle.
349
public int getIndentLeft() {
350
return this.indentLeft;
354
* Sets the left indentation of this RtfParagraphStyle.
356
* @param indentLeft The left indentation to use.
358
public void setIndentLeft(int indentLeft) {
359
this.modified = this.modified | MODIFIED_INDENT_LEFT;
360
this.indentLeft = indentLeft;
364
* Gets the right indentation of this RtfParagraphStyle.
366
* @return The right indentation of this RtfParagraphStyle.
368
public int getIndentRight() {
369
return this.indentRight;
373
* Sets the right indentation of this RtfParagraphStyle.
375
* @param indentRight The right indentation to use.
377
public void setIndentRight(int indentRight) {
378
this.modified = this.modified | MODIFIED_INDENT_RIGHT;
379
this.indentRight = indentRight;
383
* Gets the space before the paragraph of this RtfParagraphStyle..
385
* @return The space before the paragraph.
387
public int getSpacingBefore() {
388
return this.spacingBefore;
392
* Sets the space before the paragraph of this RtfParagraphStyle.
394
* @param spacingBefore The space before to use.
396
public void setSpacingBefore(int spacingBefore) {
397
this.modified = this.modified | MODIFIED_SPACING_BEFORE;
398
this.spacingBefore = spacingBefore;
402
* Gets the space after the paragraph of this RtfParagraphStyle.
404
* @return The space after the paragraph.
406
public int getSpacingAfter() {
407
return this.spacingAfter;
411
* Sets the space after the paragraph of this RtfParagraphStyle.
413
* @param spacingAfter The space after to use.
415
public void setSpacingAfter(int spacingAfter) {
416
this.modified = this.modified | MODIFIED_SPACING_AFTER;
417
this.spacingAfter = spacingAfter;
421
* Sets the font name of this RtfParagraphStyle.
423
* @param fontName The font name to use
425
public void setFontName(String fontName) {
426
this.modified = this.modified | MODIFIED_FONT_NAME;
427
super.setFontName(fontName);
431
* Sets the font size of this RtfParagraphStyle.
433
* @param fontSize The font size to use.
435
public void setSize(float fontSize) {
436
this.modified = this.modified | MODIFIED_FONT_SIZE;
437
super.setSize(fontSize);
441
* Sets the font style of this RtfParagraphStyle.
443
* @param fontStyle The font style to use.
445
public void setStyle(int fontStyle) {
446
this.modified = this.modified | MODIFIED_FONT_STYLE;
447
super.setStyle(fontStyle);
451
* Sets the colour of this RtfParagraphStyle.
453
* @param color The Color to use.
455
public void setColor(Color color) {
456
this.modified = this.modified | MODIFIED_FONT_COLOR;
457
super.setColor(color);
461
* Gets the line leading of this RtfParagraphStyle.
463
* @return The line leading of this RtfParagraphStyle.
465
public int getLineLeading() {
466
return this.lineLeading;
470
* Sets the line leading of this RtfParagraphStyle.
472
* @param lineLeading The line leading to use.
474
public void setLineLeading(int lineLeading) {
475
this.lineLeading = lineLeading;
476
this.modified = this.modified | MODIFIED_LINE_LEADING;
480
* Gets whether the lines in the paragraph should be kept together in
481
* this RtfParagraphStyle.
483
* @return Whether the lines in the paragraph should be kept together.
485
public boolean getKeepTogether() {
486
return this.keepTogether;
490
* Sets whether the lines in the paragraph should be kept together in
491
* this RtfParagraphStyle.
493
* @param keepTogether Whether the lines in the paragraph should be kept together.
495
public void setKeepTogether(boolean keepTogether) {
496
this.keepTogether = keepTogether;
497
this.modified = this.modified | MODIFIED_KEEP_TOGETHER;
501
* Gets whether the paragraph should be kept toggether with the next in
502
* this RtfParagraphStyle.
504
* @return Whether the paragraph should be kept together with the next.
506
public boolean getKeepTogetherWithNext() {
507
return this.keepTogetherWithNext;
511
* Sets whether the paragraph should be kept together with the next in
512
* this RtfParagraphStyle.
514
* @param keepTogetherWithNext Whether the paragraph should be kept together with the next.
516
public void setKeepTogetherWithNext(boolean keepTogetherWithNext) {
517
this.keepTogetherWithNext = keepTogetherWithNext;
518
this.modified = this.modified | MODIFIED_KEEP_TOGETHER_WITH_NEXT;
522
* Handles the inheritance of paragraph style settings. All settings that
523
* have not been modified will be inherited from the base RtfParagraphStyle.
524
* If this RtfParagraphStyle is not based on another one, then nothing happens.
526
public void handleInheritance() {
527
if(this.basedOnName != null && this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName) != null) {
528
this.baseStyle = this.document.getDocumentHeader().getRtfParagraphStyle(this.basedOnName);
529
this.baseStyle.handleInheritance();
530
if(!((this.modified & MODIFIED_ALIGNMENT) == MODIFIED_ALIGNMENT)) {
531
this.alignment = this.baseStyle.getAlignment();
533
if(!((this.modified & MODIFIED_INDENT_LEFT) == MODIFIED_INDENT_LEFT)) {
534
this.indentLeft = this.baseStyle.getIndentLeft();
536
if(!((this.modified & MODIFIED_INDENT_RIGHT) == MODIFIED_INDENT_RIGHT)) {
537
this.indentRight = this.baseStyle.getIndentRight();
539
if(!((this.modified & MODIFIED_SPACING_BEFORE) == MODIFIED_SPACING_BEFORE)) {
540
this.spacingBefore = this.baseStyle.getSpacingBefore();
542
if(!((this.modified & MODIFIED_SPACING_AFTER) == MODIFIED_SPACING_AFTER)) {
543
this.spacingAfter = this.baseStyle.getSpacingAfter();
545
if(!((this.modified & MODIFIED_FONT_NAME) == MODIFIED_FONT_NAME)) {
546
setFontName(this.baseStyle.getFontName());
548
if(!((this.modified & MODIFIED_FONT_SIZE) == MODIFIED_FONT_SIZE)) {
549
setSize(this.baseStyle.getFontSize());
551
if(!((this.modified & MODIFIED_FONT_STYLE) == MODIFIED_FONT_STYLE)) {
552
setStyle(this.baseStyle.getFontStyle());
554
if(!((this.modified & MODIFIED_FONT_COLOR) == MODIFIED_FONT_COLOR)) {
555
setColor(this.baseStyle.color());
557
if(!((this.modified & MODIFIED_LINE_LEADING) == MODIFIED_LINE_LEADING)) {
558
setLineLeading(this.baseStyle.getLineLeading());
560
if(!((this.modified & MODIFIED_KEEP_TOGETHER) == MODIFIED_KEEP_TOGETHER)) {
561
setKeepTogether(this.baseStyle.getKeepTogether());
563
if(!((this.modified & MODIFIED_KEEP_TOGETHER_WITH_NEXT) == MODIFIED_KEEP_TOGETHER_WITH_NEXT)) {
564
setKeepTogetherWithNext(this.baseStyle.getKeepTogetherWithNext());
570
* Writes the settings of this RtfParagraphStyle.
572
* @return A byte array with the settings of this RtfParagraphStyle.
574
private byte[] writeParagraphSettings() {
575
ByteArrayOutputStream result = new ByteArrayOutputStream();
577
if(this.keepTogether) {
578
result.write(RtfParagraphStyle.KEEP_TOGETHER);
580
if(this.keepTogetherWithNext) {
581
result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
584
case Element.ALIGN_LEFT:
585
result.write(RtfParagraphStyle.ALIGN_LEFT);
587
case Element.ALIGN_RIGHT:
588
result.write(RtfParagraphStyle.ALIGN_RIGHT);
590
case Element.ALIGN_CENTER:
591
result.write(RtfParagraphStyle.ALIGN_CENTER);
593
case Element.ALIGN_JUSTIFIED:
594
case Element.ALIGN_JUSTIFIED_ALL:
595
result.write(RtfParagraphStyle.ALIGN_JUSTIFY);
598
result.write(RtfParagraphStyle.INDENT_LEFT);
599
result.write(intToByteArray(indentLeft));
600
result.write(RtfParagraphStyle.INDENT_RIGHT);
601
result.write(intToByteArray(indentRight));
602
if(this.spacingBefore > 0) {
603
result.write(RtfParagraphStyle.SPACING_BEFORE);
604
result.write(intToByteArray(this.spacingBefore));
606
if(this.spacingAfter > 0) {
607
result.write(RtfParagraphStyle.SPACING_AFTER);
608
result.write(intToByteArray(this.spacingAfter));
610
if(this.lineLeading > 0) {
611
result.write(RtfParagraph.LINE_SPACING);
612
result.write(intToByteArray(this.lineLeading));
614
} catch(IOException ioe) {
615
ioe.printStackTrace();
617
return result.toByteArray();
621
* Writes the definition of this RtfParagraphStyle for the stylesheet list.
623
public byte[] writeDefinition() {
624
ByteArrayOutputStream result = new ByteArrayOutputStream();
626
result.write("{".getBytes());
627
result.write("\\style".getBytes());
628
result.write("\\s".getBytes());
629
result.write(intToByteArray(this.styleNumber));
630
result.write(RtfBasicElement.DELIMITER);
631
result.write(writeParagraphSettings());
632
result.write(super.writeBegin());
633
result.write(RtfBasicElement.DELIMITER);
634
result.write(this.styleName.getBytes());
635
result.write(";".getBytes());
636
result.write("}".getBytes());
637
if(this.document.getDocumentSettings().isOutputDebugLineBreaks()) {
640
} catch(IOException ioe) {
641
ioe.printStackTrace();
643
return result.toByteArray();
647
* Writes the start information of this RtfParagraphStyle.
649
public byte[] writeBegin() {
650
ByteArrayOutputStream result = new ByteArrayOutputStream();
652
result.write("\\s".getBytes());
653
result.write(intToByteArray(this.styleNumber));
654
result.write(writeParagraphSettings());
655
} catch(IOException ioe) {
656
ioe.printStackTrace();
658
return result.toByteArray();
663
* @return An empty byte array.
665
public byte[] writeEnd() {
671
* @return An empty byte array.
673
public byte[] write() {
678
* Tests whether two RtfParagraphStyles are equal. Equality
679
* is determined via the name.
681
public boolean equals(Object o) {
682
if(o == null || !(o instanceof RtfParagraphStyle)) {
685
RtfParagraphStyle paragraphStyle = (RtfParagraphStyle) o;
686
boolean result = this.getStyleName().equals(paragraphStyle.getStyleName());
691
* Gets the hash code of this RtfParagraphStyle.
693
public int hashCode() {
694
return this.styleName.hashCode();
698
* Gets the number of this RtfParagraphStyle in the stylesheet list.
700
* @return The number of this RtfParagraphStyle in the stylesheet list.
702
private int getStyleNumber() {
703
return this.styleNumber;
707
* Sets the number of this RtfParagraphStyle in the stylesheet list.
709
* @param styleNumber The number to use.
711
protected void setStyleNumber(int styleNumber) {
712
this.styleNumber = styleNumber;