2
* $Id: RtfCell.java,v 1.38 2006/09/12 13:12:53 blowagie Exp $
5
* Copyright 2001, 2002 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/
51
package com.lowagie.text.rtf;
53
import com.lowagie.text.*;
55
import java.util.Iterator;
57
import java.awt.Color;
60
* A Helper Class for the <CODE>RtfWriter</CODE>.
62
* Do not use it directly
64
* ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
66
* Parts of this Class were contributed by Steffen Stundzig. Many thanks for the
68
* Updates by Benoit Wiart
69
* @deprecated Please move to the RtfWriter2 and associated classes.
71
public class RtfCell {
72
/** Constants for merging Cells */
74
/** A possible value for merging */
75
private static final int MERGE_HORIZ_FIRST = 1;
76
/** A possible value for merging */
77
private static final int MERGE_VERT_FIRST = 2;
78
/** A possible value for merging */
79
private static final int MERGE_BOTH_FIRST = 3;
80
/** A possible value for merging */
81
private static final int MERGE_HORIZ_PREV = 4;
82
/** A possible value for merging */
83
private static final int MERGE_VERT_PREV = 5;
84
/** A possible value for merging */
85
private static final int MERGE_BOTH_PREV = 6;
91
/** First cell to merge with - Horizontal */
92
private static final byte[] cellMergeFirst = "clmgf".getBytes();
93
/** First cell to merge with - Vertical */
94
private static final byte[] cellVMergeFirst = "clvmgf".getBytes();
95
/** Merge cell with previous horizontal cell */
96
private static final byte[] cellMergePrev = "clmrg".getBytes();
97
/** Merge cell with previous vertical cell */
98
private static final byte[] cellVMergePrev = "clvmrg".getBytes();
99
/** Cell content vertical alignment bottom */
100
private static final byte[] cellVerticalAlignBottom = "clvertalb".getBytes();
101
/** Cell content vertical alignment center */
102
private static final byte[] cellVerticalAlignCenter = "clvertalc".getBytes();
103
/** Cell content vertical alignment top */
104
private static final byte[] cellVerticalAlignTop = "clvertalt".getBytes();
105
/** Cell border left */
106
private static final byte[] cellBorderLeft = "clbrdrl".getBytes();
107
/** Cell border right */
108
private static final byte[] cellBorderRight = "clbrdrr".getBytes();
109
/** Cell border top */
110
private static final byte[] cellBorderTop = "clbrdrt".getBytes();
111
/** Cell border bottom */
112
private static final byte[] cellBorderBottom = "clbrdrb".getBytes();
113
/** Cell background color */
114
private static final byte[] cellBackgroundColor = "clcbpat".getBytes();
115
/** Cell width format */
116
private static final byte[] cellWidthStyle = "clftsWidth3".getBytes();
118
private static final byte[] cellWidthTag = "clwWidth".getBytes();
119
/** Cell right border position */
120
private static final byte[] cellRightBorder = "cellx".getBytes();
121
/** Cell is part of table */
122
protected static final byte[] cellInTable = "intbl".getBytes();
124
private static final byte[] cellEnd = "cell".getBytes();
127
private static final byte[] cellPaddingTop = "clpadt".getBytes();
128
/** padding top unit */
129
private static final byte[] cellPaddingTopUnit = "clpadft3".getBytes();
130
/** padding bottom */
131
private static final byte[] cellPaddingBottom = "clpadb".getBytes();
132
/** padding bottom unit */
133
private static final byte[] cellPaddingBottomUnit = "clpadfb3".getBytes();
135
private static final byte[] cellPaddingLeft = "clpadl".getBytes();
136
/** padding left unit */
137
private static final byte[] cellPaddingLeftUnit = "clpadfl3".getBytes();
139
private static final byte[] cellPaddingRight = "clpadr".getBytes();
140
/** padding right unit */
141
private static final byte[] cellPaddingRightUnit = "clpadfr3".getBytes();
143
/** The <code>RtfWriter</code> to which this <code>RtfCell</code> belongs. */
144
private RtfWriter writer = null;
145
/** The <code>RtfTable</code> to which this <code>RtfCell</code> belongs. */
146
private RtfTable mainTable = null;
149
private int cellWidth = 0;
150
/** Cell right border position */
151
private int cellRight = 0;
152
/** <code>Cell</code> containing the actual data */
153
private Cell store = null;
154
/** Is this an empty cell */
155
private boolean emptyCell = true;
156
/** Type of merging to do */
157
private int mergeType = 0;
158
/** cell padding, because the table only renders the left and right cell padding
159
* and not the top and bottom one
161
private int cellpadding = 0;
164
* Create a new <code>RtfCell</code>.
166
* @param writer The <code>RtfWriter</code> that this <code>RtfCell</code> belongs to
167
* @param mainTable The <code>RtfTable</code> that created the
168
* <code>RtfRow</code> that created the <code>RtfCell</code> :-)
170
public RtfCell(RtfWriter writer, RtfTable mainTable) {
172
this.writer = writer;
173
this.mainTable = mainTable;
177
* Import a <code>Cell</code>.
179
* @param cell The <code>Cell</code> containing the data for this
180
* <code>RtfCell</code>
181
* @param cellLeft The position of the left border
182
* @param cellWidth The default width of a cell
183
* @param x The column index of this <code>RtfCell</code>
184
* @param y The row index of this <code>RtfCell</code>
185
* @param cellpadding the cellpadding
186
* @return the position of the right side of the cell
188
public int importCell(Cell cell, int cellLeft, int cellWidth, int x, int y, int cellpadding) {
189
this.cellpadding = cellpadding;
191
// set this value in any case
192
this.cellWidth = cellWidth;
194
cellRight = cellLeft + cellWidth;
197
if (cell.cellWidth() != null && !cell.cellWidth().equals("")) {
199
this.cellWidth = (int) (Integer.parseInt(cell.cellWidth()) * RtfWriter.TWIPSFACTOR);
201
cellRight = cellLeft + this.cellWidth;
204
if (cell.colspan() > 1) {
205
if (cell.rowspan() > 1) {
206
mergeType = MERGE_BOTH_FIRST;
207
for (int i = y; i < y + cell.rowspan(); i++) {
208
if (i > y) mainTable.setMerge(x, i, MERGE_VERT_PREV, this);
209
for (int j = x + 1; j < x + cell.colspan(); j++) {
210
mainTable.setMerge(j, i, MERGE_BOTH_PREV, this);
214
mergeType = MERGE_HORIZ_FIRST;
215
for (int i = x + 1; i < x + cell.colspan(); i++) {
216
mainTable.setMerge(i, y, MERGE_HORIZ_PREV, this);
219
} else if (cell.rowspan() > 1) {
220
mergeType = MERGE_VERT_FIRST;
221
for (int i = y + 1; i < y + cell.rowspan(); i++) {
222
mainTable.setMerge(x, i, MERGE_VERT_PREV, this);
229
* Write the properties of the <code>RtfCell</code>.
231
* @param os The <code>OutputStream</code> to which to write the properties
232
* of the <code>RtfCell</code> to.
233
* @return true if writing the cell settings succeeded
234
* @throws DocumentException
236
public boolean writeCellSettings(ByteArrayOutputStream os) throws DocumentException {
238
float lWidth, tWidth, rWidth, bWidth;
239
byte[] lStyle, tStyle, rStyle, bStyle;
241
if (store instanceof RtfTableCell) {
242
RtfTableCell c = (RtfTableCell) store;
243
lWidth = c.leftBorderWidth();
244
tWidth = c.topBorderWidth();
245
rWidth = c.rightBorderWidth();
246
bWidth = c.bottomBorderWidth();
247
lStyle = RtfTableCell.getStyleControlWord(c.leftBorderStyle());
248
tStyle = RtfTableCell.getStyleControlWord(c.topBorderStyle());
249
rStyle = RtfTableCell.getStyleControlWord(c.rightBorderStyle());
250
bStyle = RtfTableCell.getStyleControlWord(c.bottomBorderStyle());
252
lWidth = tWidth = rWidth = bWidth = store.borderWidth();
253
lStyle = tStyle = rStyle = bStyle = RtfRow.tableBorder;
256
if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV) {
260
case MERGE_VERT_FIRST:
261
os.write(RtfWriter.escape);
262
os.write(cellVMergeFirst);
264
case MERGE_BOTH_FIRST:
265
os.write(RtfWriter.escape);
266
os.write(cellVMergeFirst);
268
case MERGE_HORIZ_PREV:
269
os.write(RtfWriter.escape);
270
os.write(cellMergePrev);
272
case MERGE_VERT_PREV:
273
os.write(RtfWriter.escape);
274
os.write(cellVMergePrev);
276
case MERGE_BOTH_PREV:
277
os.write(RtfWriter.escape);
278
os.write(cellMergeFirst);
281
switch (store.verticalAlignment()) {
282
case Element.ALIGN_BOTTOM:
283
os.write(RtfWriter.escape);
284
os.write(cellVerticalAlignBottom);
286
case Element.ALIGN_CENTER:
287
case Element.ALIGN_MIDDLE:
288
os.write(RtfWriter.escape);
289
os.write(cellVerticalAlignCenter);
291
case Element.ALIGN_TOP:
292
os.write(RtfWriter.escape);
293
os.write(cellVerticalAlignTop);
297
if (((store.border() & Rectangle.LEFT) == Rectangle.LEFT) &&
299
os.write(RtfWriter.escape);
300
os.write(cellBorderLeft);
301
os.write(RtfWriter.escape);
303
os.write(RtfWriter.escape);
304
os.write(RtfRow.tableBorderWidth);
305
writeInt(os, (int) (lWidth * RtfWriter.TWIPSFACTOR));
306
os.write(RtfWriter.escape);
307
os.write(RtfRow.tableBorderColor);
308
if (store.borderColor() == null)
309
writeInt(os, writer.addColor(new
312
writeInt(os, writer.addColor(store.borderColor()));
313
os.write((byte) '\n');
315
if (((store.border() & Rectangle.TOP) == Rectangle.TOP) && (tWidth > 0)) {
316
os.write(RtfWriter.escape);
317
os.write(cellBorderTop);
318
os.write(RtfWriter.escape);
320
os.write(RtfWriter.escape);
321
os.write(RtfRow.tableBorderWidth);
322
writeInt(os, (int) (tWidth * RtfWriter.TWIPSFACTOR));
323
os.write(RtfWriter.escape);
324
os.write(RtfRow.tableBorderColor);
325
if (store.borderColor() == null)
326
writeInt(os, writer.addColor(new
329
writeInt(os, writer.addColor(store.borderColor()));
330
os.write((byte) '\n');
332
if (((store.border() & Rectangle.BOTTOM) == Rectangle.BOTTOM) &&
334
os.write(RtfWriter.escape);
335
os.write(cellBorderBottom);
336
os.write(RtfWriter.escape);
338
os.write(RtfWriter.escape);
339
os.write(RtfRow.tableBorderWidth);
340
writeInt(os, (int) (bWidth * RtfWriter.TWIPSFACTOR));
341
os.write(RtfWriter.escape);
342
os.write(RtfRow.tableBorderColor);
343
if (store.borderColor() == null)
344
writeInt(os, writer.addColor(new
347
writeInt(os, writer.addColor(store.borderColor()));
348
os.write((byte) '\n');
350
if (((store.border() & Rectangle.RIGHT) == Rectangle.RIGHT) &&
352
os.write(RtfWriter.escape);
353
os.write(cellBorderRight);
354
os.write(RtfWriter.escape);
356
os.write(RtfWriter.escape);
357
os.write(RtfRow.tableBorderWidth);
358
writeInt(os, (int) (rWidth * RtfWriter.TWIPSFACTOR));
359
os.write(RtfWriter.escape);
360
os.write(RtfRow.tableBorderColor);
361
if (store.borderColor() == null)
362
writeInt(os, writer.addColor(new
365
writeInt(os, writer.addColor(store.borderColor()));
366
os.write((byte) '\n');
368
os.write(RtfWriter.escape);
369
os.write(cellBackgroundColor);
370
if (store.backgroundColor() == null) {
371
writeInt(os, writer.addColor(new Color(255, 255, 255)));
373
writeInt(os, writer.addColor(store.backgroundColor()));
375
os.write((byte) '\n');
376
os.write(RtfWriter.escape);
377
os.write(cellWidthStyle);
378
os.write((byte) '\n');
379
os.write(RtfWriter.escape);
380
os.write(cellWidthTag);
381
writeInt(os, cellWidth);
382
os.write((byte) '\n');
383
if (cellpadding > 0) {
385
os.write(RtfWriter.escape);
386
os.write(cellPaddingLeft);
387
writeInt(os, cellpadding / 2);
388
os.write(RtfWriter.escape);
389
os.write(cellPaddingTop);
390
writeInt(os, cellpadding / 2);
391
os.write(RtfWriter.escape);
392
os.write(cellPaddingRight);
393
writeInt(os, cellpadding / 2);
394
os.write(RtfWriter.escape);
395
os.write(cellPaddingBottom);
396
writeInt(os, cellpadding / 2);
398
os.write(RtfWriter.escape);
399
os.write(cellPaddingLeftUnit);
400
os.write(RtfWriter.escape);
401
os.write(cellPaddingTopUnit);
402
os.write(RtfWriter.escape);
403
os.write(cellPaddingRightUnit);
404
os.write(RtfWriter.escape);
405
os.write(cellPaddingBottomUnit);
407
os.write(RtfWriter.escape);
408
os.write(cellRightBorder);
409
writeInt(os, cellRight);
410
} catch (IOException e) {
417
* Write the content of the <code>RtfCell</code>.
419
* @param os The <code>OutputStream</code> to which to write the content of
420
* the <code>RtfCell</code> to.
421
* @return true if writing the cell content succeeded
422
* @throws DocumentException
424
public boolean writeCellContent(ByteArrayOutputStream os) throws DocumentException {
426
if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV) {
431
Iterator cellIterator = store.getElements();
432
Paragraph container = null;
433
while (cellIterator.hasNext()) {
434
Element element = (Element) cellIterator.next();
435
// should we wrap it in a paragraph
436
if(!(element instanceof Paragraph)) {
437
if(container != null) {
438
container.add(element);
440
container = new Paragraph();
441
container.setAlignment(store.horizontalAlignment());
442
container.add(element);
445
if(container != null) {
446
writer.addElement(container, os);
452
// if horizontal alignment is undefined overwrite
453
// with that of enclosing cell
454
if (element instanceof Paragraph && ((Paragraph) element).alignment() == Element.ALIGN_UNDEFINED) {
455
((Paragraph) element).setAlignment(store.horizontalAlignment());
457
writer.addElement(element, os);
458
if (element.type() == Element.PARAGRAPH && cellIterator.hasNext()) {
459
os.write(RtfWriter.escape);
460
os.write(RtfWriter.paragraph);
464
if(container != null) {
465
writer.addElement(container, os);
469
os.write(RtfWriter.escape);
470
os.write(RtfWriter.paragraphDefaults);
471
os.write(RtfWriter.escape);
472
os.write(cellInTable);
474
os.write(RtfWriter.escape);
476
} catch (IOException e) {
483
* Sets the merge type and the <code>RtfCell</code> with which this
484
* <code>RtfCell</code> is to be merged.
486
* @param mergeType The merge type specifies the kind of merge to be applied
487
* (MERGE_HORIZ_PREV, MERGE_VERT_PREV, MERGE_BOTH_PREV)
488
* @param mergeCell The <code>RtfCell</code> that the cell at x and y is to
491
public void setMerge(int mergeType, RtfCell mergeCell) {
492
this.mergeType = mergeType;
493
store = mergeCell.getStore();
497
* Get the <code>Cell</code> with the actual content.
499
* @return <code>Cell</code> which is contained in the <code>RtfCell</code>
501
public Cell getStore() {
506
* Get the with of this <code>RtfCell</code>
508
* @return Width of the current <code>RtfCell</code>
510
public int getCellWidth() {
515
* sets the width of the cell
516
* @param value a width
518
public void setCellWidth(int value) {
523
* Get the position of the right border of this <code>RtfCell</code>.
524
* @return position of the right border
526
public int getCellRight() {
532
* Sets the right position of the cell
533
* @param value a cell position
535
public void setCellRight(int value) {
540
* Write an Integer to the Outputstream.
542
* @param out The <code>OutputStream</code> to be written to.
543
* @param i The int to be written.
544
* @throws IOException
546
private void writeInt(ByteArrayOutputStream out, int i) throws IOException {
547
out.write(Integer.toString(i).getBytes());