001: /*
002: * $Id: PdfPCell.java 2366 2006-09-14 23:10:58Z xlv $
003: * $Name$
004: *
005: * Copyright 2001, 2002 Paulo Soares
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.pdf;
052:
053: import com.lowagie.text.Chunk;
054: import com.lowagie.text.Element;
055: import com.lowagie.text.Image;
056: import com.lowagie.text.Phrase;
057: import com.lowagie.text.Rectangle;
058: import com.lowagie.text.pdf.events.PdfPCellEventForwarder;
059:
060: /** A cell in a PdfPTable.
061: */
062:
063: public class PdfPCell extends Rectangle {
064:
065: private ColumnText column = new ColumnText(null);
066:
067: /** Holds value of property verticalAlignment. */
068: private int verticalAlignment = Element.ALIGN_TOP;
069:
070: /** Holds value of property paddingLeft. */
071: private float paddingLeft = 2;
072:
073: /** Holds value of property paddingLeft. */
074: private float paddingRight = 2;
075:
076: /** Holds value of property paddingTop. */
077: private float paddingTop = 2;
078:
079: /** Holds value of property paddingBottom. */
080: private float paddingBottom = 2;
081:
082: /** Holds value of property fixedHeight. */
083: private float fixedHeight = 0;
084:
085: /** Holds value of property noWrap. */
086: private boolean noWrap = false;
087:
088: /** Holds value of property table. */
089: private PdfPTable table;
090:
091: /** Holds value of property minimumHeight. */
092: private float minimumHeight;
093:
094: /** Holds value of property colspan. */
095: private int colspan = 1;
096:
097: /** Holds value of property image. */
098: private Image image;
099:
100: /** Holds value of property cellEvent. */
101: private PdfPCellEvent cellEvent;
102:
103: /** Holds value of property useDescender. */
104: private boolean useDescender;
105:
106: /** Increases padding to include border if true */
107: private boolean useBorderPadding = false;
108:
109: /** The text in the cell. */
110: protected Phrase phrase;
111:
112: /** Constructs an empty <CODE>PdfPCell</CODE>.
113: * The default padding is 2.
114: */
115: public PdfPCell() {
116: super (0, 0, 0, 0);
117: borderWidth = 0.5f;
118: border = BOX;
119: column.setLeading(0, 1);
120: }
121:
122: /** Constructs a <CODE>PdfPCell</CODE> with a <CODE>Phrase</CODE>.
123: * The default padding is 2.
124: * @param phrase the text
125: */
126: public PdfPCell(Phrase phrase) {
127: super (0, 0, 0, 0);
128: borderWidth = 0.5f;
129: border = BOX;
130: column.addText(this .phrase = phrase);
131: column.setLeading(0, 1);
132: }
133:
134: /** Constructs a <CODE>PdfPCell</CODE> with an <CODE>Image</CODE>.
135: * The default padding is 0.
136: * @param image the <CODE>Image</CODE>
137: */
138: public PdfPCell(Image image) {
139: super (0, 0, 0, 0);
140: borderWidth = 0.5f;
141: border = BOX;
142: column
143: .addText(this .phrase = new Phrase(
144: new Chunk(image, 0, 0)));
145: column.setLeading(0, 1);
146: setPadding(0);
147: }
148:
149: /** Constructs a <CODE>PdfPCell</CODE> with an <CODE>Image</CODE>.
150: * The default padding is 0.25 for a border width of 0.5.
151: * @param image the <CODE>Image</CODE>
152: * @param fit <CODE>true</CODE> to fit the image to the cell
153: */
154: public PdfPCell(Image image, boolean fit) {
155: super (0, 0, 0, 0);
156: if (fit) {
157: borderWidth = 0.5f;
158: border = BOX;
159: this .image = image;
160: column.setLeading(0, 1);
161: setPadding(borderWidth / 2);
162: } else {
163: borderWidth = 0.5f;
164: border = BOX;
165: column.addText(this .phrase = new Phrase(new Chunk(image, 0,
166: 0)));
167: column.setLeading(0, 1);
168: setPadding(0);
169: }
170: }
171:
172: /** Constructs a <CODE>PdfPCell</CODE> with a <CODE>PdfPtable</CODE>.
173: * This constructor allows nested tables.
174: * The default padding is 0.
175: * @param table The <CODE>PdfPTable</CODE>
176: */
177: public PdfPCell(PdfPTable table) {
178: super (0, 0, 0, 0);
179: borderWidth = 0.5f;
180: border = BOX;
181: column.setLeading(0, 1);
182: setPadding(0);
183: this .table = table;
184: table.setWidthPercentage(100);
185: table.setExtendLastRow(true);
186: column.addElement(table);
187: }
188:
189: /** Constructs a deep copy of a <CODE>PdfPCell</CODE>.
190: * @param cell the <CODE>PdfPCell</CODE> to duplicate
191: */
192: public PdfPCell(PdfPCell cell) {
193: super (cell.llx, cell.lly, cell.urx, cell.ury);
194: cloneNonPositionParameters(cell);
195: verticalAlignment = cell.verticalAlignment;
196: paddingLeft = cell.paddingLeft;
197: paddingRight = cell.paddingRight;
198: paddingTop = cell.paddingTop;
199: paddingBottom = cell.paddingBottom;
200: phrase = cell.phrase;
201: fixedHeight = cell.fixedHeight;
202: minimumHeight = cell.minimumHeight;
203: noWrap = cell.noWrap;
204: colspan = cell.colspan;
205: if (cell.table != null)
206: table = new PdfPTable(cell.table);
207: image = Image.getInstance(cell.image);
208: cellEvent = cell.cellEvent;
209: useDescender = cell.useDescender;
210: column = ColumnText.duplicate(cell.column);
211: useBorderPadding = cell.useBorderPadding;
212: rotation = cell.rotation;
213: }
214:
215: /**
216: * Adds an iText element to the cell.
217: * @param element
218: */
219: public void addElement(Element element) {
220: if (table != null) {
221: table = null;
222: column.setText(null);
223: }
224: column.addElement(element);
225: }
226:
227: /** Gets the <CODE>Phrase</CODE> from this cell.
228: * @return the <CODE>Phrase</CODE>
229: */
230: public Phrase getPhrase() {
231: return phrase;
232: }
233:
234: /** Sets the <CODE>Phrase</CODE> for this cell.
235: * @param phrase the <CODE>Phrase</CODE>
236: */
237: public void setPhrase(Phrase phrase) {
238: table = null;
239: image = null;
240: column.setText(this .phrase = phrase);
241: }
242:
243: /** Gets the horizontal alignment for the cell.
244: * @return the horizontal alignment for the cell
245: */
246: public int getHorizontalAlignment() {
247: return column.getAlignment();
248: }
249:
250: /** Sets the horizontal alignment for the cell. It could be
251: * <CODE>Element.ALIGN_CENTER</CODE> for example.
252: * @param horizontalAlignment The horizontal alignment
253: */
254: public void setHorizontalAlignment(int horizontalAlignment) {
255: column.setAlignment(horizontalAlignment);
256: }
257:
258: /** Gets the vertical alignment for the cell.
259: * @return the vertical alignment for the cell
260: */
261: public int getVerticalAlignment() {
262: return verticalAlignment;
263: }
264:
265: /** Sets the vertical alignment for the cell. It could be
266: * <CODE>Element.ALIGN_MIDDLE</CODE> for example.
267: * @param verticalAlignment The vertical alignment
268: */
269: public void setVerticalAlignment(int verticalAlignment) {
270: if (table != null)
271: table
272: .setExtendLastRow(verticalAlignment == Element.ALIGN_TOP);
273: this .verticalAlignment = verticalAlignment;
274: }
275:
276: /** Gets the effective left padding. This will include
277: * the left border width if {@link #isUseBorderPadding()} is true.
278: * @return effective value of property paddingLeft.
279: */
280: public float getEffectivePaddingLeft() {
281: return paddingLeft
282: + (isUseBorderPadding() ? (getBorderWidthLeft() / (isUseVariableBorders() ? 1f
283: : 2f))
284: : 0);
285: }
286:
287: /**
288: * @return Value of property paddingLeft.
289: */
290: public float getPaddingLeft() {
291: return paddingLeft;
292: }
293:
294: /**
295: * Setter for property paddingLeft.
296: * @param paddingLeft New value of property paddingLeft.
297: */
298: public void setPaddingLeft(float paddingLeft) {
299: this .paddingLeft = paddingLeft;
300: }
301:
302: /** Gets the effective right padding. This will include
303: * the right border width if {@link #isUseBorderPadding()} is true.
304: * @return effective value of property paddingRight.
305: */
306: public float getEffectivePaddingRight() {
307: return paddingRight
308: + (isUseBorderPadding() ? (getBorderWidthRight() / (isUseVariableBorders() ? 1f
309: : 2f))
310: : 0);
311: }
312:
313: /**
314: * Getter for property paddingRight.
315: * @return Value of property paddingRight.
316: */
317: public float getPaddingRight() {
318: return paddingRight;
319: }
320:
321: /**
322: * Setter for property paddingRight.
323: * @param paddingRight New value of property paddingRight.
324: */
325: public void setPaddingRight(float paddingRight) {
326: this .paddingRight = paddingRight;
327: }
328:
329: /** Gets the effective top padding. This will include
330: * the top border width if {@link #isUseBorderPadding()} is true.
331: * @return effective value of property paddingTop.
332: */
333: public float getEffectivePaddingTop() {
334: return paddingTop
335: + (isUseBorderPadding() ? (getBorderWidthTop() / (isUseVariableBorders() ? 1f
336: : 2f))
337: : 0);
338: }
339:
340: /**
341: * Getter for property paddingTop.
342: * @return Value of property paddingTop.
343: */
344: public float getPaddingTop() {
345: return paddingTop;
346: }
347:
348: /**
349: * Setter for property paddingTop.
350: * @param paddingTop New value of property paddingTop.
351: */
352: public void setPaddingTop(float paddingTop) {
353: this .paddingTop = paddingTop;
354: }
355:
356: /** Gets the effective bottom padding. This will include
357: * the bottom border width if {@link #isUseBorderPadding()} is true.
358: * @return effective value of property paddingBottom.
359: */
360: public float getEffectivePaddingBottom() {
361: return paddingBottom
362: + (isUseBorderPadding() ? (getBorderWidthBottom() / (isUseVariableBorders() ? 1f
363: : 2f))
364: : 0);
365: }
366:
367: /**
368: * Getter for property paddingBottom.
369: * @return Value of property paddingBottom.
370: */
371: public float getPaddingBottom() {
372: return paddingBottom;
373: }
374:
375: /**
376: * Setter for property paddingBottom.
377: * @param paddingBottom New value of property paddingBottom.
378: */
379: public void setPaddingBottom(float paddingBottom) {
380: this .paddingBottom = paddingBottom;
381: }
382:
383: /**
384: * Sets the padding of the contents in the cell (space between content and border).
385: * @param padding
386: */
387: public void setPadding(float padding) {
388: paddingBottom = padding;
389: paddingTop = padding;
390: paddingLeft = padding;
391: paddingRight = padding;
392: }
393:
394: /**
395: * If true, then effective padding will include border widths
396: * @return true if effective padding includes border widths
397: */
398: public boolean isUseBorderPadding() {
399: return useBorderPadding;
400: }
401:
402: /**
403: * Adjusts effective padding to include border widths.
404: * @param use adjust effective padding if true
405: */
406: public void setUseBorderPadding(boolean use) {
407: useBorderPadding = use;
408: }
409:
410: /**
411: * Sets the leading fixed and variable. The resultant leading will be
412: * fixedLeading+multipliedLeading*maxFontSize where maxFontSize is the
413: * size of the bigest font in the line.
414: * @param fixedLeading the fixed leading
415: * @param multipliedLeading the variable leading
416: */
417: public void setLeading(float fixedLeading, float multipliedLeading) {
418: column.setLeading(fixedLeading, multipliedLeading);
419: }
420:
421: /**
422: * Gets the fixed leading
423: * @return the leading
424: */
425: public float getLeading() {
426: return column.getLeading();
427: }
428:
429: /**
430: * Gets the variable leading
431: * @return the leading
432: */
433: public float getMultipliedLeading() {
434: return column.getMultipliedLeading();
435: }
436:
437: /**
438: * Sets the first paragraph line indent.
439: * @param indent the indent
440: */
441: public void setIndent(float indent) {
442: column.setIndent(indent);
443: }
444:
445: /**
446: * Gets the first paragraph line indent.
447: * @return the indent
448: */
449: public float getIndent() {
450: return column.getIndent();
451: }
452:
453: /**
454: * Gets the extra space between paragraphs.
455: * @return the extra space between paragraphs
456: */
457: public float getExtraParagraphSpace() {
458: return column.getExtraParagraphSpace();
459: }
460:
461: /**
462: * Sets the extra space between paragraphs.
463: * @param extraParagraphSpace the extra space between paragraphs
464: */
465: public void setExtraParagraphSpace(float extraParagraphSpace) {
466: column.setExtraParagraphSpace(extraParagraphSpace);
467: }
468:
469: /**
470: * Getter for property fixedHeight.
471: * @return Value of property fixedHeight.
472: */
473: public float getFixedHeight() {
474: return fixedHeight;
475: }
476:
477: /**
478: * Setter for property fixedHeight.
479: * @param fixedHeight New value of property fixedHeight.
480: */
481: public void setFixedHeight(float fixedHeight) {
482: this .fixedHeight = fixedHeight;
483: minimumHeight = 0;
484: }
485:
486: /**
487: * Getter for property noWrap.
488: * @return Value of property noWrap.
489: */
490: public boolean isNoWrap() {
491: return noWrap;
492: }
493:
494: /**
495: * Setter for property noWrap.
496: * @param noWrap New value of property noWrap.
497: */
498: public void setNoWrap(boolean noWrap) {
499: this .noWrap = noWrap;
500: }
501:
502: /**
503: * Getter for property table.
504: * @return Value of property table.
505: */
506: PdfPTable getTable() {
507: return table;
508: }
509:
510: void setTable(PdfPTable table) {
511: this .table = table;
512: column.setText(null);
513: image = null;
514: if (table != null) {
515: table
516: .setExtendLastRow(verticalAlignment == Element.ALIGN_TOP);
517: column.addElement(table);
518: table.setWidthPercentage(100);
519: }
520: }
521:
522: /** Getter for property minimumHeight.
523: * @return Value of property minimumHeight.
524: */
525: public float getMinimumHeight() {
526: return minimumHeight;
527: }
528:
529: /** Setter for property minimumHeight.
530: * @param minimumHeight New value of property minimumHeight.
531: */
532: public void setMinimumHeight(float minimumHeight) {
533: this .minimumHeight = minimumHeight;
534: fixedHeight = 0;
535: }
536:
537: /** Getter for property colspan.
538: * @return Value of property colspan.
539: */
540: public int getColspan() {
541: return colspan;
542: }
543:
544: /** Setter for property colspan.
545: * @param colspan New value of property colspan.
546: */
547: public void setColspan(int colspan) {
548: this .colspan = colspan;
549: }
550:
551: /**
552: * Sets the following paragraph lines indent.
553: * @param indent the indent
554: */
555: public void setFollowingIndent(float indent) {
556: column.setFollowingIndent(indent);
557: }
558:
559: /**
560: * Gets the following paragraph lines indent.
561: * @return the indent
562: */
563: public float getFollowingIndent() {
564: return column.getFollowingIndent();
565: }
566:
567: /**
568: * Sets the right paragraph lines indent.
569: * @param indent the indent
570: */
571: public void setRightIndent(float indent) {
572: column.setRightIndent(indent);
573: }
574:
575: /**
576: * Gets the right paragraph lines indent.
577: * @return the indent
578: */
579: public float getRightIndent() {
580: return column.getRightIndent();
581: }
582:
583: /** Gets the space/character extra spacing ratio for
584: * fully justified text.
585: * @return the space/character extra spacing ratio
586: */
587: public float getSpaceCharRatio() {
588: return column.getSpaceCharRatio();
589: }
590:
591: /** Sets the ratio between the extra word spacing and the extra character spacing
592: * when the text is fully justified.
593: * Extra word spacing will grow <CODE>spaceCharRatio</CODE> times more than extra character spacing.
594: * If the ratio is <CODE>PdfWriter.NO_SPACE_CHAR_RATIO</CODE> then the extra character spacing
595: * will be zero.
596: * @param spaceCharRatio the ratio between the extra word spacing and the extra character spacing
597: */
598: public void setSpaceCharRatio(float spaceCharRatio) {
599: column.setSpaceCharRatio(spaceCharRatio);
600: }
601:
602: /**
603: * Sets the run direction of the text content in the cell (PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL).
604: * @param runDirection
605: */
606: public void setRunDirection(int runDirection) {
607: column.setRunDirection(runDirection);
608: }
609:
610: /**
611: * Gets the run direction of the text content in the cell
612: * @return One of the following values: PdfWriter.RUN_DIRECTION_DEFAULT, PdfWriter.RUN_DIRECTION_NO_BIDI, PdfWriter.RUN_DIRECTION_LTR or PdfWriter.RUN_DIRECTION_RTL.
613: */
614: public int getRunDirection() {
615: return column.getRunDirection();
616: }
617:
618: /** Getter for property image.
619: * @return Value of property image.
620: *
621: */
622: public Image getImage() {
623: return this .image;
624: }
625:
626: /** Setter for property image.
627: * @param image New value of property image.
628: *
629: */
630: public void setImage(Image image) {
631: column.setText(null);
632: table = null;
633: this .image = image;
634: }
635:
636: /** Gets the cell event for this cell.
637: * @return the cell event
638: *
639: */
640: public PdfPCellEvent getCellEvent() {
641: return this .cellEvent;
642: }
643:
644: /** Sets the cell event for this cell.
645: * @param event the cell event
646: *
647: */
648: public void setCellEvent(PdfPCellEvent event) {
649: if (event == null)
650: this .cellEvent = null;
651: else if (this .cellEvent == null)
652: this .cellEvent = event;
653: else if (this .cellEvent instanceof PdfPCellEventForwarder)
654: ((PdfPCellEventForwarder) this .cellEvent)
655: .addCellEvent(event);
656: else {
657: PdfPCellEventForwarder forward = new PdfPCellEventForwarder();
658: forward.addCellEvent(this .cellEvent);
659: forward.addCellEvent(event);
660: this .cellEvent = forward;
661: }
662: }
663:
664: /** Gets the arabic shaping options.
665: * @return the arabic shaping options
666: */
667: public int getArabicOptions() {
668: return column.getArabicOptions();
669: }
670:
671: /** Sets the arabic shaping options. The option can be AR_NOVOWEL,
672: * AR_COMPOSEDTASHKEEL and AR_LIG.
673: * @param arabicOptions the arabic shaping options
674: */
675: public void setArabicOptions(int arabicOptions) {
676: column.setArabicOptions(arabicOptions);
677: }
678:
679: /** Gets state of first line height based on max ascender
680: * @return true if an ascender is to be used.
681: */
682: public boolean isUseAscender() {
683: return column.isUseAscender();
684: }
685:
686: /** Enables/ Disables adjustment of first line height based on max ascender.
687: *
688: * @param use adjust height if true
689: */
690: public void setUseAscender(boolean use) {
691: column.setUseAscender(use);
692: }
693:
694: /** Getter for property useDescender.
695: * @return Value of property useDescender.
696: *
697: */
698: public boolean isUseDescender() {
699: return this .useDescender;
700: }
701:
702: /** Setter for property useDescender.
703: * @param useDescender New value of property useDescender.
704: *
705: */
706: public void setUseDescender(boolean useDescender) {
707: this .useDescender = useDescender;
708: }
709:
710: /**
711: * Gets the ColumnText with the content of the cell.
712: * @return a columntext object
713: */
714: public ColumnText getColumn() {
715: return column;
716: }
717:
718: /**
719: * Sets the columntext in the cell.
720: * @param column
721: */
722: public void setColumn(ColumnText column) {
723: this .column = column;
724: }
725:
726: /**
727: * The rotation of the cell. Possible values are
728: * 0, 90, 180 and 270.
729: */
730: private int rotation;
731:
732: /**
733: * Gets the rotation of the cell.
734: * @return the rotation of the cell.
735: */
736: public int getRotation() {
737: return this .rotation;
738: }
739:
740: /**
741: * Sets the rotation of the cell. Possible values are
742: * 0, 90, 180 and 270.
743: * @param rotation the rotation of the cell
744: */
745: public void setRotation(int rotation) {
746: rotation %= 360;
747: if (rotation < 0)
748: rotation += 360;
749: if ((rotation % 90) != 0)
750: throw new IllegalArgumentException(
751: "Rotation must be a multiple of 90.");
752: this.rotation = rotation;
753: }
754: }
|