File: RtfCell.java

package info (click to toggle)
libitext1-java 1.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 7,636 kB
  • sloc: java: 86,865; xml: 396; makefile: 15
file content (534 lines) | stat: -rw-r--r-- 18,662 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
 * $Id: RtfCell.java,v 1.15 2006/02/09 17:13:11 hallm Exp $
 * $Name:  $
 *
 * Copyright 2001, 2002, 2003, 2004 by Mark Hall
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */

package com.lowagie.text.rtf.table;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;
import java.util.ArrayList;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.List;
import com.lowagie.text.Paragraph;
import com.lowagie.text.rtf.RtfBasicElement;
import com.lowagie.text.rtf.RtfExtendedElement;
import com.lowagie.text.rtf.document.RtfDocument;
import com.lowagie.text.rtf.style.RtfColor;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import com.lowagie.text.rtf.text.RtfParagraph;


/**
 * The RtfCell wraps a Cell, but can also be added directly to a Table.
 * The RtfCell is an extension of Cell, that supports a multitude of different
 * borderstyles.
 * 
 * @version $Id: RtfCell.java,v 1.15 2006/02/09 17:13:11 hallm Exp $
 * @author Mark Hall (mhall@edu.uni-klu.ac.at)
 * @author Steffen Stundzig
 * @author Benoit WIART <b.wiart@proxiad.com>
 * @see com.lowagie.text.rtf.table.RtfBorder
 */
public class RtfCell extends Cell implements RtfExtendedElement {

    /**
     * This cell is not merged
     */
    private static final int MERGE_NONE = 0;
    /**
     * This cell is the parent cell of a vertical merge operation
     */
    private static final int MERGE_VERT_PARENT = 1;
    /**
     * This cell is a child cell of a vertical merge operation
     */
    private static final int MERGE_VERT_CHILD = 2;

    /**
     * The parent RtfRow of this RtfCell
     */
    private RtfRow parentRow = null;
    /**
     * The content of this RtfCell
     */
    private ArrayList content = null;
    /**
     * The right margin of this RtfCell
     */
    private int cellRight = 0;
    /**
     * The width of this RtfCell
     */
    private int cellWidth = 0;
    /**
     * The borders of this RtfCell
     */
    private RtfBorderGroup borders = null;
    /**
     * The vertical alignment of this RtfCell
     */
    private int verticalAlignment = Cell.ALIGN_MIDDLE;
    /**
     * The background color of this RtfCell
     */
    private RtfColor backgroundColor = null;
    /**
     * The padding of this RtfCell
     */
    private int cellPadding = 0;
    /**
     * The merge type of this RtfCell
     */
    private int mergeType = MERGE_NONE;
    /**
     * The number of columns spanned by this RtfCell
     */
    private int colspan = 1;
    /**
     * The number of rows spanned by this RtfCell
     */
    private int rowspan = 1;
    /**
     * The RtfDocument this RtfCell belongs to
     */
    private RtfDocument document = null;
    /**
     * Whether this RtfCell is in a header
     */
    private boolean inHeader = false;

    /**
     * Constructs an empty RtfCell
     */
    public RtfCell() {
        super();
        this.borders = new RtfBorderGroup();
    }
    
    /**
     * Constructs a RtfCell based upon a String
     * 
     * @param content The String to base the RtfCell on
     */
    public RtfCell(String content) {
        super(content);
        this.borders = new RtfBorderGroup();
    }
    
    /**
     * Constructs a RtfCell based upon an Element
     * 
     * @param element The Element to base the RtfCell on
     * @throws BadElementException If the Element is not valid
     */
    public RtfCell(Element element) throws BadElementException {
        super(element);
        this.borders = new RtfBorderGroup();
    }
    
    /**
     * Constructs a RtfCell based upon certain Properties
     * 
     * @param properties The Properties for this RtfCell
     */
    public RtfCell(Properties properties) {
        super(properties);
        this.borders = new RtfBorderGroup();
    }
    
    /**
     * Constructs a RtfCell based on a Cell.
     * 
     * @param doc The RtfDocument this RtfCell belongs to
     * @param row The RtfRow this RtfCell lies in
     * @param cell The Cell to base this RtfCell on
     */
    protected RtfCell(RtfDocument doc, RtfRow row, Cell cell) {
        this.document = doc;
        this.parentRow = row;
        importCell(cell);
    }

    /**
     * Imports the Cell properties into the RtfCell
     * 
     * @param cell The Cell to import
     */
    private void importCell(Cell cell) {
        this.content = new ArrayList();
        
        if(cell == null) {
            this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, this.parentRow.getParentTable().getBorders());
            return;
        }
        
        this.colspan = cell.colspan();
        this.rowspan = cell.rowspan();
        if(cell.rowspan() > 1) {
            this.mergeType = MERGE_VERT_PARENT;
        }
        if(cell instanceof RtfCell) {
            this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, ((RtfCell) cell).getBorders());
        } else {
            this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, cell.border(), cell.borderWidth(), cell.borderColor());
        }
        this.verticalAlignment = cell.verticalAlignment();
        if(cell.backgroundColor() == null) {
            this.backgroundColor = new RtfColor(this.document, 255, 255, 255);
        } else {
            this.backgroundColor = new RtfColor(this.document, cell.backgroundColor());
        }
        
        this.cellPadding = (int) this.parentRow.getParentTable().getCellPadding();
        
        if(cell != null) {
            Iterator cellIterator = cell.getElements();
            Paragraph container = null;
            while(cellIterator.hasNext()) {
                try {
                    Element element = (Element) cellIterator.next();
                    // should we wrap it in a paragraph
                    if(!(element instanceof Paragraph) && !(element instanceof List)) {
                        if(container != null) {
                            container.add(element);
                        } else {
                            container = new Paragraph();
                            container.setAlignment(cell.horizontalAlignment());
                            container.add(element);
                        }
                    } else {
                        if(container != null) {
                            RtfBasicElement rtfElement = this.document.getMapper().mapElement(container);
                            rtfElement.setInTable(true);
                            this.content.add(rtfElement);
                            container = null;
                        }
                        // if horizontal alignment is undefined overwrite
                        // with that of enclosing cell
                        if (element instanceof Paragraph && ((Paragraph) element).alignment() == Element.ALIGN_UNDEFINED) {
                            ((Paragraph) element).setAlignment(cell.horizontalAlignment());
                        }

                        RtfBasicElement rtfElement = this.document.getMapper().mapElement(element);
                        rtfElement.setInTable(true);
                        this.content.add(rtfElement);
                    }
                } catch(DocumentException de) {
                    de.printStackTrace();
                }
            }
            if(container != null) {
                try {
                    RtfBasicElement rtfElement = this.document.getMapper().mapElement(container);
                    rtfElement.setInTable(true);
                    this.content.add(rtfElement);
                } catch(DocumentException de) {
                    de.printStackTrace();
                }
            }
        }
    }
    
    /**
     * Write the cell definition part of this RtfCell
     * 
     * @return A byte array with the cell definition
     */
    public byte[] writeDefinition() {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            if(this.mergeType == MERGE_VERT_PARENT) {
                result.write("\\clvmgf".getBytes());
            } else if(this.mergeType == MERGE_VERT_CHILD) {
                result.write("\\clvmrg".getBytes());
            }
            switch (verticalAlignment) {
                case Element.ALIGN_BOTTOM:
                    result.write("\\clvertalb".getBytes());
                    break;
                case Element.ALIGN_CENTER:
                case Element.ALIGN_MIDDLE:
                    result.write("\\clvertalc".getBytes());
                    break;
                case Element.ALIGN_TOP:
                    result.write("\\clvertalt".getBytes());
                    break;
            }
            result.write(this.borders.write());

            if(this.backgroundColor != null) {
                result.write("\\clcbpat".getBytes());
                result.write(intToByteArray(this.backgroundColor.getColorNumber()));
            }
            result.write('\n');
            
            result.write("\\clftsWidth3".getBytes());
            result.write('\n');
            
            result.write("\\clwWidth".getBytes());
            result.write(intToByteArray(this.cellWidth));
            result.write('\n');
            
            if(this.cellPadding > 0) {
                result.write("\\clpadl".getBytes());
                result.write(intToByteArray(this.cellPadding / 2));
                result.write("\\clpadt".getBytes());
                result.write(intToByteArray(this.cellPadding / 2));
                result.write("\\clpadr".getBytes());
                result.write(intToByteArray(this.cellPadding / 2));
                result.write("\\clpadb".getBytes());
                result.write(intToByteArray(this.cellPadding / 2));
                result.write("\\clpadfl3".getBytes());
                result.write("\\clpadft3".getBytes());
                result.write("\\clpadfr3".getBytes());
                result.write("\\clpadfb3".getBytes());
            }
            result.write("\\cellx".getBytes());
            result.write(intToByteArray(this.cellRight));
            
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        
        return result.toByteArray();
    }
    
    /**
     * Write the content of this RtfCell
     * 
     * @return A byte array with the content of this RtfCell
     */
    public byte[] write() {
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            if(this.content.size() == 0) {
                result.write(RtfParagraph.PARAGRAPH_DEFAULTS);
                if(this.parentRow.getParentTable().getTableFitToPage()) {
                    result.write(RtfParagraphStyle.KEEP_TOGETHER_WITH_NEXT);
                }
                result.write(RtfParagraph.IN_TABLE);
            } else {
                for(int i = 0; i < this.content.size(); i++) {
                    RtfBasicElement rtfElement = (RtfBasicElement) this.content.get(i);
                    if(rtfElement instanceof RtfParagraph) {
                        ((RtfParagraph) rtfElement).setKeepTogetherWithNext(this.parentRow.getParentTable().getTableFitToPage());
                    }
                    result.write(rtfElement.write());
                    if(rtfElement instanceof RtfParagraph && i < (this.content.size() - 1)) {
                        result.write(RtfParagraph.PARAGRAPH);
                    }
                }
            }
            result.write("\\cell".getBytes());
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        
        return result.toByteArray();
    }

    /**
     * Sets the right margin of this cell. Used in merge operations
     * 
     * @param cellRight The right margin to use
     */
    protected void setCellRight(int cellRight) {
        this.cellRight = cellRight;
    }
    
    /**
     * Gets the right margin of this RtfCell
     * 
     * @return The right margin of this RtfCell.
     */
    protected int getCellRight() {
        return this.cellRight;
    }
    
    /**
     * Sets the cell width of this RtfCell. Used in merge operations.
     * 
     * @param cellWidth The cell width to use
     */
    protected void setCellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
    }
    
    /**
     * Gets the cell width of this RtfCell
     * 
     * @return The cell width of this RtfCell
     */
    protected int getCellWidth() {
        return this.cellWidth;
    }
    
    /**
     * Gets the number of columns this RtfCell spans
     * 
     * @return The number of columns this RtfCell spans
     */
    protected int getColspan() {
        return this.colspan;
    }
    
    /**
     * Gets the number of rows this RtfCell spans
     * 
     * @return The number of rows this RtfCell spans
     */
    protected int getRowspan() {
        return this.rowspan;
    }
    
    /**
     * Gets the cell padding of this RtfCell
     * 
     * @return The cell padding of this RtfCell
     */
    protected int getCellpadding() {
        return this.cellPadding;
    }

    /**
     * Gets the borders of this RtfCell
     * 
     * @return The borders of this RtfCell
     */
    protected RtfBorderGroup getBorders() {
        return this.borders;
    }
    
    /**
     * Set the borders of this RtfCell
     * 
     * @param borderGroup The RtfBorderGroup to use as borders
     */
    public void setBorders(RtfBorderGroup borderGroup) {
        this.borders = new RtfBorderGroup(this.document, RtfBorder.CELL_BORDER, borderGroup);
    }
    
    /**
     * Get the vertical alignment of this RtfCell
     * 
     * @return The vertical alignment of this RtfCell
     */
    protected int getVerticalAlignment() {
        return this.verticalAlignment;
    }
    
    /**
     * Get the background color of this RtfCell
     * 
     * @return The background color of this RtfCell
     */
    protected RtfColor getBackgroundColor() {
        return this.backgroundColor;
    }

    /**
     * Merge this cell into the parent cell.
     * 
     * @param mergeParent The RtfCell to merge with
     */
    protected void setCellMergeChild(RtfCell mergeParent) {
        this.mergeType = MERGE_VERT_CHILD;
        this.cellWidth = mergeParent.getCellWidth();
        this.cellRight = mergeParent.getCellRight();
        this.cellPadding = mergeParent.getCellpadding();
        this.borders = mergeParent.getBorders();
        this.verticalAlignment = mergeParent.getVerticalAlignment();
        this.backgroundColor = mergeParent.getBackgroundColor();
    }

    /**
     * Sets the RtfDocument this RtfCell belongs to
     * 
     * @param doc The RtfDocument to use
     */
    public void setRtfDocument(RtfDocument doc) {
        this.document = doc;
    }
    
    /**
     * Unused
     * @param inTable
     */
    public void setInTable(boolean inTable) {
    }
    
    /**
     * Sets whether this RtfCell is in a header
     * 
     * @param inHeader <code>True</code> if this RtfCell is in a header, <code>false</code> otherwise
     */
    public void setInHeader(boolean inHeader) {
        this.inHeader = inHeader;
        for(int i = 0; i < this.content.size(); i++) {
            ((RtfBasicElement) this.content.get(i)).setInHeader(inHeader);
        }
    }

    /**
     * Transforms an integer into its String representation and then returns the bytes
     * of that string.
     *
     * @param i The integer to convert
     * @return A byte array representing the integer
     */
    private byte[] intToByteArray(int i) {
        return Integer.toString(i).getBytes();
    }
}