1 /*
2 * $Id: Paragraph.java 3668 2009-02-01 09:08:50Z blowagie $
3 *
4 * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * (the "License"); you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the License.
13 *
14 * The Original Code is 'iText, a free JAVA-PDF library'.
15 *
16 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18 * All Rights Reserved.
19 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21 *
22 * Contributor(s): all the names of the contributors are added in the source code
23 * where applicable.
24 *
25 * Alternatively, the contents of this file may be used under the terms of the
26 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
27 * provisions of LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the LGPL
29 * License and not to allow others to use your version of this file under
30 * the MPL, indicate your decision by deleting the provisions above and
31 * replace them with the notice and other provisions required by the LGPL.
32 * If you do not delete the provisions above, a recipient may use your version
33 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34 *
35 * This library is free software; you can redistribute it and/or modify it
36 * under the terms of the MPL as stated above or under the terms of the GNU
37 * Library General Public License as published by the Free Software Foundation;
38 * either version 2 of the License, or any later version.
39 *
40 * This library is distributed in the hope that it will be useful, but WITHOUT
41 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43 * details.
44 *
45 * If you didn't download this code from the following link, you should check if
46 * you aren't using an obsolete version:
47 * http://www.lowagie.com/iText/
48 */
49
50 package com.lowagie.text;
51
52 /**
53 * A <CODE>Paragraph</CODE> is a series of <CODE>Chunk</CODE>s and/or <CODE>Phrases</CODE>.
54 * <P>
55 * A <CODE>Paragraph</CODE> has the same qualities of a <CODE>Phrase</CODE>, but also
56 * some additional layout-parameters:
57 * <UL>
58 * <LI>the indentation
59 * <LI>the alignment of the text
60 * </UL>
61 *
62 * Example:
63 * <BLOCKQUOTE><PRE>
64 * <STRONG>Paragraph p = new Paragraph("This is a paragraph",
65 * FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));</STRONG>
66 * </PRE></BLOCKQUOTE>
67 *
68 * @see Element
69 * @see Phrase
70 * @see ListItem
71 */
72
73 public class Paragraph extends Phrase {
74
75 // constants
76 private static final long serialVersionUID = 7852314969733375514L;
77
78 // membervariables
79
80 /** The alignment of the text. */
81 protected int alignment = Element.ALIGN_UNDEFINED;
82
83 /** The text leading that is multiplied by the biggest font size in the line. */
84 protected float multipliedLeading = 0;
85
86 /** The indentation of this paragraph on the left side. */
87 protected float indentationLeft;
88
89 /** The indentation of this paragraph on the right side. */
90 protected float indentationRight;
91
92 /** Holds value of property firstLineIndent. */
93 private float firstLineIndent = 0;
94
95 /** The spacing before the paragraph. */
96 protected float spacingBefore;
97
98 /** The spacing after the paragraph. */
99 protected float spacingAfter;
100
101 /** Holds value of property extraParagraphSpace. */
102 private float extraParagraphSpace = 0;
103
104 /** Does the paragraph has to be kept together on 1 page. */
105 protected boolean keeptogether = false;
106
107 // constructors
108
109 /**
110 * Constructs a <CODE>Paragraph</CODE>.
111 */
112 public Paragraph() {
113 super();
114 }
115
116 /**
117 * Constructs a <CODE>Paragraph</CODE> with a certain leading.
118 *
119 * @param leading the leading
120 */
121 public Paragraph(float leading) {
122 super(leading);
123 }
124
125 /**
126 * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Chunk</CODE>.
127 *
128 * @param chunk a <CODE>Chunk</CODE>
129 */
130 public Paragraph(Chunk chunk) {
131 super(chunk);
132 }
133
134 /**
135 * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Chunk</CODE>
136 * and a certain leading.
137 *
138 * @param leading the leading
139 * @param chunk a <CODE>Chunk</CODE>
140 */
141 public Paragraph(float leading, Chunk chunk) {
142 super(leading, chunk);
143 }
144
145 /**
146 * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>.
147 *
148 * @param string a <CODE>String</CODE>
149 */
150 public Paragraph(String string) {
151 super(string);
152 }
153
154 /**
155 * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>
156 * and a certain <CODE>Font</CODE>.
157 *
158 * @param string a <CODE>String</CODE>
159 * @param font a <CODE>Font</CODE>
160 */
161 public Paragraph(String string, Font font) {
162 super(string, font);
163 }
164
165 /**
166 * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>
167 * and a certain leading.
168 *
169 * @param leading the leading
170 * @param string a <CODE>String</CODE>
171 */
172 public Paragraph(float leading, String string) {
173 super(leading, string);
174 }
175
176 /**
177 * Constructs a <CODE>Paragraph</CODE> with a certain leading, <CODE>String</CODE>
178 * and <CODE>Font</CODE>.
179 *
180 * @param leading the leading
181 * @param string a <CODE>String</CODE>
182 * @param font a <CODE>Font</CODE>
183 */
184 public Paragraph(float leading, String string, Font font) {
185 super(leading, string, font);
186 }
187
188 /**
189 * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Phrase</CODE>.
190 *
191 * @param phrase a <CODE>Phrase</CODE>
192 */
193 public Paragraph(Phrase phrase) {
194 super(phrase);
195 if (phrase instanceof Paragraph) {
196 Paragraph p = (Paragraph)phrase;
197 setAlignment(p.alignment);
198 setLeading(phrase.getLeading(), p.multipliedLeading);
199 setIndentationLeft(p.getIndentationLeft());
200 setIndentationRight(p.getIndentationRight());
201 setFirstLineIndent(p.getFirstLineIndent());
202 setSpacingAfter(p.spacingAfter());
203 setSpacingBefore(p.spacingBefore());
204 setExtraParagraphSpace(p.getExtraParagraphSpace());
205 }
206 }
207
208 // implementation of the Element-methods
209
210 /**
211 * Gets the type of the text element.
212 *
213 * @return a type
214 */
215 public int type() {
216 return Element.PARAGRAPH;
217 }
218
219 // methods
220
221 /**
222 * Adds an <CODE>Object</CODE> to the <CODE>Paragraph</CODE>.
223 *
224 * @param o object the object to add.
225 * @return true is adding the object succeeded
226 */
227 public boolean add(Object o) {
228 if (o instanceof List) {
229 List list = (List) o;
230 list.setIndentationLeft(list.getIndentationLeft() + indentationLeft);
231 list.setIndentationRight(indentationRight);
232 return super.add(list);
233 }
234 else if (o instanceof Image) {
235 super.addSpecial(o);
236 return true;
237 }
238 else if (o instanceof Paragraph) {
239 super.add(o);
240 java.util.List chunks = getChunks();
241 if (!chunks.isEmpty()) {
242 Chunk tmp = ((Chunk) chunks.get(chunks.size() - 1));
243 super.add(new Chunk("\n", tmp.getFont()));
244 }
245 else {
246 super.add(Chunk.NEWLINE);
247 }
248 return true;
249 }
250 return super.add(o);
251 }
252
253 // setting the membervariables
254
255 /**
256 * Sets the alignment of this paragraph.
257 *
258 * @param alignment the new alignment
259 */
260 public void setAlignment(int alignment) {
261 this.alignment = alignment;
262 }
263
264 /**
265 * Sets the alignment of this paragraph.
266 *
267 * @param alignment the new alignment as a <CODE>String</CODE>
268 */
269 public void setAlignment(String alignment) {
270 if (ElementTags.ALIGN_CENTER.equalsIgnoreCase(alignment)) {
271 this.alignment = Element.ALIGN_CENTER;
272 return;
273 }
274 if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(alignment)) {
275 this.alignment = Element.ALIGN_RIGHT;
276 return;
277 }
278 if (ElementTags.ALIGN_JUSTIFIED.equalsIgnoreCase(alignment)) {
279 this.alignment = Element.ALIGN_JUSTIFIED;
280 return;
281 }
282 if (ElementTags.ALIGN_JUSTIFIED_ALL.equalsIgnoreCase(alignment)) {
283 this.alignment = Element.ALIGN_JUSTIFIED_ALL;
284 return;
285 }
286 this.alignment = Element.ALIGN_LEFT;
287 }
288
289 /**
290 * @see com.lowagie.text.Phrase#setLeading(float)
291 */
292 public void setLeading(float fixedLeading) {
293 this.leading = fixedLeading;
294 this.multipliedLeading = 0;
295 }
296
297 /**
298 * Sets the variable leading. The resultant leading will be
299 * multipliedLeading*maxFontSize where maxFontSize is the
300 * size of the biggest font in the line.
301 * @param multipliedLeading the variable leading
302 */
303 public void setMultipliedLeading(float multipliedLeading) {
304 this.leading = 0;
305 this.multipliedLeading = multipliedLeading;
306 }
307
308 /**
309 * Sets the leading fixed and variable. The resultant leading will be
310 * fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
311 * size of the biggest font in the line.
312 * @param fixedLeading the fixed leading
313 * @param multipliedLeading the variable leading
314 */
315 public void setLeading(float fixedLeading, float multipliedLeading) {
316 this.leading = fixedLeading;
317 this.multipliedLeading = multipliedLeading;
318 }
319
320 /**
321 * Sets the indentation of this paragraph on the left side.
322 *
323 * @param indentation the new indentation
324 */
325 public void setIndentationLeft(float indentation) {
326 this.indentationLeft = indentation;
327 }
328
329 /**
330 * Sets the indentation of this paragraph on the right side.
331 *
332 * @param indentation the new indentation
333 */
334 public void setIndentationRight(float indentation) {
335 this.indentationRight = indentation;
336 }
337
338 /**
339 * Setter for property firstLineIndent.
340 * @param firstLineIndent New value of property firstLineIndent.
341 */
342 public void setFirstLineIndent(float firstLineIndent) {
343 this.firstLineIndent = firstLineIndent;
344 }
345
346 /**
347 * Sets the spacing before this paragraph.
348 *
349 * @param spacing the new spacing
350 */
351 public void setSpacingBefore(float spacing) {
352 this.spacingBefore = spacing;
353 }
354
355 /**
356 * Sets the spacing after this paragraph.
357 *
358 * @param spacing the new spacing
359 */
360 public void setSpacingAfter(float spacing) {
361 this.spacingAfter = spacing;
362 }
363
364 /**
365 * Indicates that the paragraph has to be kept together on one page.
366 *
367 * @param keeptogether true of the paragraph may not be split over 2 pages
368 */
369 public void setKeepTogether(boolean keeptogether) {
370 this.keeptogether = keeptogether;
371 }
372
373 /**
374 * Checks if this paragraph has to be kept together on one page.
375 *
376 * @return true if the paragraph may not be split over 2 pages.
377 */
378 public boolean getKeepTogether() {
379 return keeptogether;
380 }
381
382 // methods to retrieve information
383
384 /**
385 * Gets the alignment of this paragraph.
386 *
387 * @return alignment
388 */
389 public int getAlignment() {
390 return alignment;
391 }
392
393 /**
394 * Gets the variable leading
395 * @return the leading
396 */
397 public float getMultipliedLeading() {
398 return multipliedLeading;
399 }
400
401 /**
402 * Gets the total leading.
403 * This method is based on the assumption that the
404 * font of the Paragraph is the font of all the elements
405 * that make part of the paragraph. This isn't necessarily
406 * true.
407 * @return the total leading (fixed and multiplied)
408 */
409 public float getTotalLeading() {
410 float m = font == null ?
411 Font.DEFAULTSIZE * multipliedLeading : font.getCalculatedLeading(multipliedLeading);
412 if (m > 0 && !hasLeading()) {
413 return m;
414 }
415 return getLeading() + m;
416 }
417
418 /**
419 * Gets the indentation of this paragraph on the left side.
420 *
421 * @return the indentation
422 */
423 public float getIndentationLeft() {
424 return indentationLeft;
425 }
426
427 /**
428 * Gets the indentation of this paragraph on the right side.
429 *
430 * @return the indentation
431 */
432 public float getIndentationRight() {
433 return indentationRight;
434 }
435
436 /**
437 * Getter for property firstLineIndent.
438 * @return Value of property firstLineIndent.
439 */
440 public float getFirstLineIndent() {
441 return this.firstLineIndent;
442 }
443
444 /**
445 * Gets the spacing before this paragraph.
446 * @return the spacing
447 * @since 2.1.5
448 */
449 public float getSpacingBefore() {
450 return spacingBefore;
451 }
452
453 /**
454 * Gets the spacing after this paragraph.
455 * @return the spacing
456 * @since 2.1.5
457 */
458 public float getSpacingAfter() {
459 return spacingAfter;
460 }
461
462 /**
463 * Getter for property extraParagraphSpace.
464 * @return Value of property extraParagraphSpace.
465 */
466 public float getExtraParagraphSpace() {
467 return this.extraParagraphSpace;
468 }
469
470 /**
471 * Setter for property extraParagraphSpace.
472 * @param extraParagraphSpace New value of property extraParagraphSpace.
473 */
474 public void setExtraParagraphSpace(float extraParagraphSpace) {
475 this.extraParagraphSpace = extraParagraphSpace;
476 }
477
478 // scheduled for removal
479
480 /**
481 * Gets the spacing before this paragraph.
482 *
483 * @return the spacing
484 * @deprecated As of iText 2.1.5, replaced by {@link #getSpacingBefore()},
485 * scheduled for removal at 2.3.0
486 */
487 public float spacingBefore() {
488 return getSpacingBefore();
489 }
490
491 /**
492 * Gets the spacing after this paragraph.
493 *
494 * @return the spacing
495 * @deprecated As of iText 2.1.5, replaced by {@link #getSpacingAfter()},
496 * scheduled for removal at 2.3.0
497 */
498 public float spacingAfter() {
499 return spacingAfter;
500 }
501
502 }
503