1 /*
2 * $Id: Paragraph.java 3469 2008-05-27 11:25:17Z 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 if (size() > 0) {
241 Chunk tmp = ((Chunk) getChunks().get(size() - 1));
242 super.add(new Chunk("\n", tmp.getFont()));
243 }
244 else {
245 super.add(Chunk.NEWLINE);
246 }
247 return true;
248 }
249 return super.add(o);
250 }
251
252 // setting the membervariables
253
254 /**
255 * Sets the alignment of this paragraph.
256 *
257 * @param alignment the new alignment
258 */
259 public void setAlignment(int alignment) {
260 this.alignment = alignment;
261 }
262
263 /**
264 * Sets the alignment of this paragraph.
265 *
266 * @param alignment the new alignment as a <CODE>String</CODE>
267 */
268 public void setAlignment(String alignment) {
269 if (ElementTags.ALIGN_CENTER.equalsIgnoreCase(alignment)) {
270 this.alignment = Element.ALIGN_CENTER;
271 return;
272 }
273 if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(alignment)) {
274 this.alignment = Element.ALIGN_RIGHT;
275 return;
276 }
277 if (ElementTags.ALIGN_JUSTIFIED.equalsIgnoreCase(alignment)) {
278 this.alignment = Element.ALIGN_JUSTIFIED;
279 return;
280 }
281 if (ElementTags.ALIGN_JUSTIFIED_ALL.equalsIgnoreCase(alignment)) {
282 this.alignment = Element.ALIGN_JUSTIFIED_ALL;
283 return;
284 }
285 this.alignment = Element.ALIGN_LEFT;
286 }
287
288 /**
289 * @see com.lowagie.text.Phrase#setLeading(float)
290 */
291 public void setLeading(float fixedLeading) {
292 this.leading = fixedLeading;
293 this.multipliedLeading = 0;
294 }
295
296 /**
297 * Sets the variable leading. The resultant leading will be
298 * multipliedLeading*maxFontSize where maxFontSize is the
299 * size of the biggest font in the line.
300 * @param multipliedLeading the variable leading
301 */
302 public void setMultipliedLeading(float multipliedLeading) {
303 this.leading = 0;
304 this.multipliedLeading = multipliedLeading;
305 }
306
307 /**
308 * Sets the leading fixed and variable. The resultant leading will be
309 * fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
310 * size of the biggest font in the line.
311 * @param fixedLeading the fixed leading
312 * @param multipliedLeading the variable leading
313 */
314 public void setLeading(float fixedLeading, float multipliedLeading) {
315 this.leading = fixedLeading;
316 this.multipliedLeading = multipliedLeading;
317 }
318
319 /**
320 * Sets the indentation of this paragraph on the left side.
321 *
322 * @param indentation the new indentation
323 */
324 public void setIndentationLeft(float indentation) {
325 this.indentationLeft = indentation;
326 }
327
328 /**
329 * Sets the indentation of this paragraph on the right side.
330 *
331 * @param indentation the new indentation
332 */
333 public void setIndentationRight(float indentation) {
334 this.indentationRight = indentation;
335 }
336
337 /**
338 * Setter for property firstLineIndent.
339 * @param firstLineIndent New value of property firstLineIndent.
340 */
341 public void setFirstLineIndent(float firstLineIndent) {
342 this.firstLineIndent = firstLineIndent;
343 }
344
345 /**
346 * Sets the spacing before this paragraph.
347 *
348 * @param spacing the new spacing
349 */
350 public void setSpacingBefore(float spacing) {
351 this.spacingBefore = spacing;
352 }
353
354 /**
355 * Sets the spacing after this paragraph.
356 *
357 * @param spacing the new spacing
358 */
359 public void setSpacingAfter(float spacing) {
360 this.spacingAfter = spacing;
361 }
362
363 /**
364 * Indicates that the paragraph has to be kept together on one page.
365 *
366 * @param keeptogether true of the paragraph may not be split over 2 pages
367 */
368 public void setKeepTogether(boolean keeptogether) {
369 this.keeptogether = keeptogether;
370 }
371
372 /**
373 * Checks if this paragraph has to be kept together on one page.
374 *
375 * @return true if the paragraph may not be split over 2 pages.
376 */
377 public boolean getKeepTogether() {
378 return keeptogether;
379 }
380
381 // methods to retrieve information
382
383 /**
384 * Gets the alignment of this paragraph.
385 *
386 * @return alignment
387 */
388 public int getAlignment() {
389 return alignment;
390 }
391
392 /**
393 * Gets the variable leading
394 * @return the leading
395 */
396 public float getMultipliedLeading() {
397 return multipliedLeading;
398 }
399
400 /**
401 * Gets the total leading.
402 * This method is based on the assumption that the
403 * font of the Paragraph is the font of all the elements
404 * that make part of the paragraph. This isn't necessarily
405 * true.
406 * @return the total leading (fixed and multiplied)
407 */
408 public float getTotalLeading() {
409 float m = font == null ?
410 Font.DEFAULTSIZE * multipliedLeading : font.getCalculatedLeading(multipliedLeading);
411 if (m > 0 && !hasLeading()) {
412 return m;
413 }
414 return getLeading() + m;
415 }
416
417 /**
418 * Gets the indentation of this paragraph on the left side.
419 *
420 * @return the indentation
421 */
422 public float getIndentationLeft() {
423 return indentationLeft;
424 }
425
426 /**
427 * Gets the indentation of this paragraph on the right side.
428 *
429 * @return the indentation
430 */
431 public float getIndentationRight() {
432 return indentationRight;
433 }
434
435 /**
436 * Getter for property firstLineIndent.
437 * @return Value of property firstLineIndent.
438 */
439 public float getFirstLineIndent() {
440 return this.firstLineIndent;
441 }
442
443 /**
444 * Gets the spacing before this paragraph.
445 *
446 * @return the spacing
447 */
448 public float spacingBefore() {
449 return spacingBefore;
450 }
451
452 /**
453 * Gets the spacing after this paragraph.
454 *
455 * @return the spacing
456 */
457 public float spacingAfter() {
458 return spacingAfter;
459 }
460
461 /**
462 * Getter for property extraParagraphSpace.
463 * @return Value of property extraParagraphSpace.
464 */
465 public float getExtraParagraphSpace() {
466 return this.extraParagraphSpace;
467 }
468
469 /**
470 * Setter for property extraParagraphSpace.
471 * @param extraParagraphSpace New value of property extraParagraphSpace.
472 */
473 public void setExtraParagraphSpace(float extraParagraphSpace) {
474 this.extraParagraphSpace = extraParagraphSpace;
475 }
476
477 }