001: /*
002: * $Id: Paragraph.java 2748 2007-05-12 15:11:48Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text;
052:
053: /**
054: * A <CODE>Paragraph</CODE> is a series of <CODE>Chunk</CODE>s and/or <CODE>Phrases</CODE>.
055: * <P>
056: * A <CODE>Paragraph</CODE> has the same qualities of a <CODE>Phrase</CODE>, but also
057: * some additional layout-parameters:
058: * <UL>
059: * <LI>the indentation
060: * <LI>the alignment of the text
061: * </UL>
062: *
063: * Example:
064: * <BLOCKQUOTE><PRE>
065: * <STRONG>Paragraph p = new Paragraph("This is a paragraph",
066: * FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));</STRONG>
067: * </PRE></BLOCKQUOTE>
068: *
069: * @see Element
070: * @see Phrase
071: * @see ListItem
072: */
073:
074: public class Paragraph extends Phrase {
075:
076: // constants
077: private static final long serialVersionUID = 7852314969733375514L;
078:
079: // membervariables
080:
081: /** The alignment of the text. */
082: protected int alignment = Element.ALIGN_UNDEFINED;
083:
084: /** The text leading that is multiplied by the biggest font size in the line. */
085: protected float multipliedLeading = 0;
086:
087: /** The indentation of this paragraph on the left side. */
088: protected float indentationLeft;
089:
090: /** The indentation of this paragraph on the right side. */
091: protected float indentationRight;
092:
093: /** Holds value of property firstLineIndent. */
094: private float firstLineIndent = 0;
095:
096: /** The spacing before the paragraph. */
097: protected float spacingBefore;
098:
099: /** The spacing after the paragraph. */
100: protected float spacingAfter;
101:
102: /** Holds value of property extraParagraphSpace. */
103: private float extraParagraphSpace = 0;
104:
105: /** Does the paragraph has to be kept together on 1 page. */
106: protected boolean keeptogether = false;
107:
108: // constructors
109:
110: /**
111: * Constructs a <CODE>Paragraph</CODE>.
112: */
113: public Paragraph() {
114: super ();
115: }
116:
117: /**
118: * Constructs a <CODE>Paragraph</CODE> with a certain leading.
119: *
120: * @param leading the leading
121: */
122: public Paragraph(float leading) {
123: super (leading);
124: }
125:
126: /**
127: * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Chunk</CODE>.
128: *
129: * @param chunk a <CODE>Chunk</CODE>
130: */
131: public Paragraph(Chunk chunk) {
132: super (chunk);
133: }
134:
135: /**
136: * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Chunk</CODE>
137: * and a certain leading.
138: *
139: * @param leading the leading
140: * @param chunk a <CODE>Chunk</CODE>
141: */
142: public Paragraph(float leading, Chunk chunk) {
143: super (leading, chunk);
144: }
145:
146: /**
147: * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>.
148: *
149: * @param string a <CODE>String</CODE>
150: */
151: public Paragraph(String string) {
152: super (string);
153: }
154:
155: /**
156: * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>
157: * and a certain <CODE>Font</CODE>.
158: *
159: * @param string a <CODE>String</CODE>
160: * @param font a <CODE>Font</CODE>
161: */
162: public Paragraph(String string, Font font) {
163: super (string, font);
164: }
165:
166: /**
167: * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>String</CODE>
168: * and a certain leading.
169: *
170: * @param leading the leading
171: * @param string a <CODE>String</CODE>
172: */
173: public Paragraph(float leading, String string) {
174: super (leading, string);
175: }
176:
177: /**
178: * Constructs a <CODE>Paragraph</CODE> with a certain leading, <CODE>String</CODE>
179: * and <CODE>Font</CODE>.
180: *
181: * @param leading the leading
182: * @param string a <CODE>String</CODE>
183: * @param font a <CODE>Font</CODE>
184: */
185: public Paragraph(float leading, String string, Font font) {
186: super (leading, string, font);
187: }
188:
189: /**
190: * Constructs a <CODE>Paragraph</CODE> with a certain <CODE>Phrase</CODE>.
191: *
192: * @param phrase a <CODE>Phrase</CODE>
193: */
194: public Paragraph(Phrase phrase) {
195: super (phrase);
196: if (phrase instanceof Paragraph) {
197: Paragraph p = (Paragraph) phrase;
198: setAlignment(p.alignment);
199: setLeading(phrase.getLeading(), p.multipliedLeading);
200: setIndentationLeft(p.getIndentationLeft());
201: setIndentationRight(p.getIndentationRight());
202: setFirstLineIndent(p.getFirstLineIndent());
203: setSpacingAfter(p.spacingAfter());
204: setSpacingBefore(p.spacingBefore());
205: setExtraParagraphSpace(p.getExtraParagraphSpace());
206: }
207: }
208:
209: // implementation of the Element-methods
210:
211: /**
212: * Gets the type of the text element.
213: *
214: * @return a type
215: */
216: public int type() {
217: return Element.PARAGRAPH;
218: }
219:
220: // methods
221:
222: /**
223: * Adds an <CODE>Object</CODE> to the <CODE>Paragraph</CODE>.
224: *
225: * @param o object the object to add.
226: * @return true is adding the object succeeded
227: */
228: public boolean add(Object o) {
229: if (o instanceof List) {
230: List list = (List) o;
231: list.setIndentationLeft(list.getIndentationLeft()
232: + indentationLeft);
233: list.setIndentationRight(indentationRight);
234: return super .add(list);
235: } else if (o instanceof Image) {
236: super .addSpecial(o);
237: return true;
238: } else if (o instanceof Paragraph) {
239: super .add(o);
240: super .add(Chunk.NEWLINE);
241: return true;
242: }
243: return super .add(o);
244: }
245:
246: // setting the membervariables
247:
248: /**
249: * Sets the alignment of this paragraph.
250: *
251: * @param alignment the new alignment
252: */
253: public void setAlignment(int alignment) {
254: this .alignment = alignment;
255: }
256:
257: /**
258: * Sets the alignment of this paragraph.
259: *
260: * @param alignment the new alignment as a <CODE>String</CODE>
261: */
262: public void setAlignment(String alignment) {
263: if (ElementTags.ALIGN_CENTER.equalsIgnoreCase(alignment)) {
264: this .alignment = Element.ALIGN_CENTER;
265: return;
266: }
267: if (ElementTags.ALIGN_RIGHT.equalsIgnoreCase(alignment)) {
268: this .alignment = Element.ALIGN_RIGHT;
269: return;
270: }
271: if (ElementTags.ALIGN_JUSTIFIED.equalsIgnoreCase(alignment)) {
272: this .alignment = Element.ALIGN_JUSTIFIED;
273: return;
274: }
275: if (ElementTags.ALIGN_JUSTIFIED_ALL.equalsIgnoreCase(alignment)) {
276: this .alignment = Element.ALIGN_JUSTIFIED_ALL;
277: return;
278: }
279: this .alignment = Element.ALIGN_LEFT;
280: }
281:
282: /**
283: * @see com.lowagie.text.Phrase#setLeading(float)
284: */
285: public void setLeading(float fixedLeading) {
286: this .leading = fixedLeading;
287: this .multipliedLeading = 0;
288: }
289:
290: /**
291: * Sets the variable leading. The resultant leading will be
292: * multipliedLeading*maxFontSize where maxFontSize is the
293: * size of the bigest font in the line.
294: * @param multipliedLeading the variable leading
295: */
296: public void setMultipliedLeading(float multipliedLeading) {
297: this .leading = 0;
298: this .multipliedLeading = multipliedLeading;
299: }
300:
301: /**
302: * Sets the leading fixed and variable. The resultant leading will be
303: * fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
304: * size of the bigest font in the line.
305: * @param fixedLeading the fixed leading
306: * @param multipliedLeading the variable leading
307: */
308: public void setLeading(float fixedLeading, float multipliedLeading) {
309: this .leading = fixedLeading;
310: this .multipliedLeading = multipliedLeading;
311: }
312:
313: /**
314: * Sets the indentation of this paragraph on the left side.
315: *
316: * @param indentation the new indentation
317: */
318: public void setIndentationLeft(float indentation) {
319: this .indentationLeft = indentation;
320: }
321:
322: /**
323: * Sets the indentation of this paragraph on the right side.
324: *
325: * @param indentation the new indentation
326: */
327: public void setIndentationRight(float indentation) {
328: this .indentationRight = indentation;
329: }
330:
331: /**
332: * Setter for property firstLineIndent.
333: * @param firstLineIndent New value of property firstLineIndent.
334: */
335: public void setFirstLineIndent(float firstLineIndent) {
336: this .firstLineIndent = firstLineIndent;
337: }
338:
339: /**
340: * Sets the spacing before this paragraph.
341: *
342: * @param spacing the new spacing
343: */
344: public void setSpacingBefore(float spacing) {
345: this .spacingBefore = spacing;
346: }
347:
348: /**
349: * Sets the spacing after this paragraph.
350: *
351: * @param spacing the new spacing
352: */
353: public void setSpacingAfter(float spacing) {
354: this .spacingAfter = spacing;
355: }
356:
357: /**
358: * Indicates that the paragraph has to be kept together on one page.
359: *
360: * @param keeptogether true of the paragraph may not be split over 2 pages
361: */
362: public void setKeepTogether(boolean keeptogether) {
363: this .keeptogether = keeptogether;
364: }
365:
366: /**
367: * Checks if this paragraph has to be kept together on one page.
368: *
369: * @return true if the paragraph may not be split over 2 pages.
370: */
371: public boolean getKeepTogether() {
372: return keeptogether;
373: }
374:
375: // methods to retrieve information
376:
377: /**
378: * Gets the alignment of this paragraph.
379: *
380: * @return alignment
381: */
382: public int getAlignment() {
383: return alignment;
384: }
385:
386: /**
387: * Gets the variable leading
388: * @return the leading
389: */
390: public float getMultipliedLeading() {
391: return multipliedLeading;
392: }
393:
394: /**
395: * Gets the total leading.
396: * This method is based on the assumption that the
397: * font of the Paragraph is the font of all the elements
398: * that make part of the paragraph. This isn't necessarily
399: * true.
400: * @return the total leading (fixed and multiplied)
401: */
402: public float getTotalLeading() {
403: float m = font == null ? Font.DEFAULTSIZE * multipliedLeading
404: : font.getCalculatedLeading(multipliedLeading);
405: if (m > 0 && !hasLeading()) {
406: return m;
407: }
408: return getLeading() + m;
409: }
410:
411: /**
412: * Gets the indentation of this paragraph on the left side.
413: *
414: * @return the indentation
415: */
416: public float getIndentationLeft() {
417: return indentationLeft;
418: }
419:
420: /**
421: * Gets the indentation of this paragraph on the right side.
422: *
423: * @return the indentation
424: */
425: public float getIndentationRight() {
426: return indentationRight;
427: }
428:
429: /**
430: * Getter for property firstLineIndent.
431: * @return Value of property firstLineIndent.
432: */
433: public float getFirstLineIndent() {
434: return this .firstLineIndent;
435: }
436:
437: /**
438: * Gets the spacing before this paragraph.
439: *
440: * @return the spacing
441: */
442: public float spacingBefore() {
443: return spacingBefore;
444: }
445:
446: /**
447: * Gets the spacing after this paragraph.
448: *
449: * @return the spacing
450: */
451: public float spacingAfter() {
452: return spacingAfter;
453: }
454:
455: /**
456: * Getter for property extraParagraphSpace.
457: * @return Value of property extraParagraphSpace.
458: */
459: public float getExtraParagraphSpace() {
460: return this .extraParagraphSpace;
461: }
462:
463: /**
464: * Setter for property extraParagraphSpace.
465: * @param extraParagraphSpace New value of property extraParagraphSpace.
466: */
467: public void setExtraParagraphSpace(float extraParagraphSpace) {
468: this .extraParagraphSpace = extraParagraphSpace;
469: }
470:
471: // deprecated stuff
472:
473: /**
474: * Returns a <CODE>Paragraph</CODE> that has been constructed taking in account
475: * the value of some <VAR>attributes</VAR>.
476: *
477: * @param attributes Some attributes
478: * @deprecated use ElementFactory.getParagraph(attributes)
479: */
480: public Paragraph(java.util.Properties attributes) {
481: this (com.lowagie.text.factories.ElementFactory
482: .getParagraph(attributes));
483: }
484:
485: /**
486: * Gets the alignment of this paragraph.
487: *
488: * @return alignment
489: * @deprecated Use {@link #getAlignment()} instead
490: */
491: public int alignment() {
492: return getAlignment();
493: }
494:
495: /**
496: * Gets the indentation of this paragraph on the left side.
497: *
498: * @return the indentation
499: * @deprecated Use {@link #getIndentationLeft()} instead
500: */
501: public float indentationLeft() {
502: return getIndentationLeft();
503: }
504:
505: /**
506: * Gets the indentation of this paragraph on the right side.
507: *
508: * @return the indentation
509: * @deprecated Use {@link #getIndentationRight()} instead
510: */
511: public float indentationRight() {
512: return getIndentationRight();
513: }
514: }
|