File: RtfBorder.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 (577 lines) | stat: -rw-r--r-- 20,709 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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
/*
 * $Id: RtfBorder.java,v 1.16 2005/05/04 14:33:54 blowagie 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.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.lowagie.text.rtf.RtfElement;
import com.lowagie.text.rtf.document.RtfDocument;
import com.lowagie.text.rtf.style.RtfColor;


/**
 * The RtfBorder handle one row or cell border.
 * INTERNAL USE ONLY
 * 
 * @version $Version:$
 * @author Mark Hall (mhall@edu.uni-klu.ac.at)
 * @author Unknown
 */
public class RtfBorder extends RtfElement {

    /**
     * Constant for the left row border
     */
    protected static final byte[] ROW_BORDER_LEFT = "\\trbrdrl".getBytes();
    /**
     * Constant for the top row border
     */
    protected static final byte[] ROW_BORDER_TOP = "\\trbrdrt".getBytes();
    /**
     * Constant for the right row border
     */
    protected static final byte[] ROW_BORDER_RIGHT = "\\trbrdrr".getBytes();
    /**
     * Constant for the bottom row border
     */
    protected static final byte[] ROW_BORDER_BOTTOM = "\\trbrdrb".getBytes();
    /**
     * Constant for the horizontal line
     */
    protected static final byte[] ROW_BORDER_HORIZONTAL = "\\trbrdrh".getBytes();
    /**
     * Constant for the vertical line
     */
    protected static final byte[] ROW_BORDER_VERTICAL = "\\trbrdrv".getBytes();
    /**
     * Constant for the left cell border
     */
    protected static final byte[] CELL_BORDER_LEFT = "\\clbrdrl".getBytes();
    /**
     * Constant for the top cell border
     */
    protected static final byte[] CELL_BORDER_TOP = "\\clbrdrt".getBytes();
    /**
     * Constant for the right cell border
     */
    protected static final byte[] CELL_BORDER_RIGHT = "\\clbrdrr".getBytes();
    /**
     * Constant for the bottom cell border
     */
    protected static final byte[] CELL_BORDER_BOTTOM = "\\clbrdrb".getBytes();
    /**
     * Constant for the border width
     */
    protected static final byte[] BORDER_WIDTH = "\\brdrw".getBytes();
    /**
     * Constant for the border colour number
     */
    protected static final byte[] BORDER_COLOR_NUMBER = "\\brdrcf".getBytes();
    /**
     * Constant for the single border style
     */
    protected static final byte[] BORDER_STYLE_SINGLE = "\\brdrs".getBytes();
    /**
     * Constant for the double thick border style
     */
    protected static final byte[] BORDER_STYLE_DOUBLE_THICK	= "\\brdrth".getBytes();
    /**
     * Constant for the shadowed border style
     */
    protected static final byte[] BORDER_STYLE_SHADOWED = "\\brdrsh".getBytes();
    /**
     * Constant for the dotted border style
     */
    protected static final byte[] BORDER_STYLE_DOTTED = "\\brdrdot".getBytes();
    /**
     * Constant for the dashed border style
     */
    protected static final byte[] BORDER_STYLE_DASHED = "\\brdrdash".getBytes();
    /**
     * Constant for the hairline border style
     */
    protected static final byte[] BORDER_STYLE_HAIRLINE = "\\brdrhair".getBytes();
    /**
     * Constant for the double border style
     */
    protected static final byte[] BORDER_STYLE_DOUBLE = "\\brdrdb".getBytes();
    /**
     * Constant for the dot dash border style
     */
    protected static final byte[] BORDER_STYLE_DOT_DASH = "\\brdrdashd".getBytes();
    /**
     * Constant for the dot dot dash border style
     */
    protected static final byte[] BORDER_STYLE_DOT_DOT_DASH	= "\\brdrdashdd".getBytes();
    /**
     * Constant for the triple border style
     */
    protected static final byte[] BORDER_STYLE_TRIPLE = "\\brdrtriple".getBytes();
    /**
     * Constant for the thick thin border style
     */
    protected static final byte[] BORDER_STYLE_THICK_THIN = "\\brdrtnthsg".getBytes();
    /**
     * Constant for the thin thick border style
     */
    protected static final byte[] BORDER_STYLE_THIN_THICK = "\\brdrthtnsg".getBytes();
    /**
     * Constant for the thin thick thin border style
     */
    protected static final byte[] BORDER_STYLE_THIN_THICK_THIN = "\\brdrtnthtnsg".getBytes();
    /**
     * Constant for the thick thin medium border style
     */
    protected static final byte[] BORDER_STYLE_THICK_THIN_MED = "\\brdrtnthmg".getBytes();
    /**
     * Constant for the thin thick medium border style
     */
    protected static final byte[] BORDER_STYLE_THIN_THICK_MED = "\\brdrthtnmg".getBytes();
    /**
     * Constant for the thin thick thin medium border style
     */
    protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_MED = "\\brdrtnthtnmg".getBytes();
    /**
     * Constant for the thick thin large border style
     */
    protected static final byte[] BORDER_STYLE_THICK_THIN_LARGE = "\\brdrtnthlg".getBytes();
    /**
     * Constant for the thin thick large border style
     */
    protected static final byte[] BORDER_STYLE_THIN_THICK_LARGE	= "\\brdrthtnlg".getBytes();
    /**
     * Constant for the thin thick thin large border style
     */
    protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_LARGE = "\\brdrtnthtnlg".getBytes();
    /**
     * Constant for the wavy border style
     */
    protected static final byte[] BORDER_STYLE_WAVY = "\\brdrwavy".getBytes();
    /**
     * Constant for the double wavy border style
     */
    protected static final byte[] BORDER_STYLE_DOUBLE_WAVY = "\\brdrwavydb".getBytes();
    /**
     * Constant for the striped border style
     */
    protected static final byte[] BORDER_STYLE_STRIPED = "\\brdrdashdotstr".getBytes();
    /**
     * Constant for the embossed border style
     */
    protected static final byte[] BORDER_STYLE_EMBOSS = "\\brdremboss".getBytes();
    /**
     * Constant for the engraved border style
     */
    protected static final byte[] BORDER_STYLE_ENGRAVE = "\\brdrengrave".getBytes();

    /**
     * Constant for a row border
     */
    protected static final int ROW_BORDER = 1;
    /**
     * Constant for a cell border
     */
    protected static final int CELL_BORDER = 2;
    
    /**
     * This border is no border :-)
     */
    protected static final int NO_BORDER = 0;
    /**
     * Constant for a left border
     */
    protected static final int LEFT_BORDER = 1;
    /**
     * Constant for a top border
     */
    protected static final int TOP_BORDER = 2;
    /**
     * Constant for a right border
     */
    protected static final int RIGHT_BORDER = 4;
    /**
     * Constant for a bottom border
     */
    protected static final int BOTTOM_BORDER = 8;
    /**
     * Constant for a box (left, top, right, bottom) border 
     */
    protected static final int BOX_BORDER = 15;
    /**
     * Constant for a vertical line
     */
    protected static final int VERTICAL_BORDER = 16;
    /**
     * Constant for a horizontal line
     */
    protected static final int HORIZONTAL_BORDER = 32;
    
    /**
     * Constant for a border with no border
     */
    public static final int BORDER_NONE = 0;
    /**
     * Constant for a single border
     */
    public static final int BORDER_SINGLE = 1;
    /**
     * Constant for a double thick border
     */
    public static final int BORDER_DOUBLE_THICK = 2;
    /**
     * Constant for a shadowed border
     */
    public static final int BORDER_SHADOWED = 3;
    /**
     * Constant for a dotted border
     */
    public static final int BORDER_DOTTED = 4;
    /**
     * Constant for a dashed border
     */
    public static final int BORDER_DASHED = 5;
    /**
     * Constant for a hairline border
     */
    public static final int BORDER_HAIRLINE = 6;
    /**
     * Constant for a double border
     */
    public static final int BORDER_DOUBLE = 7;
    /**
     * Constant for a dot dash border
     */
    public static final int BORDER_DOT_DASH = 8;
    /**
     * Constant for a dot dot dash border
     */
    public static final int BORDER_DOT_DOT_DASH = 9;
    /**
     * Constant for a triple border
     */
    public static final int BORDER_TRIPLE = 10;
    /**
     * Constant for a thick thin border
     */
    public static final int BORDER_THICK_THIN = 11;
    /**
     * Constant for a thin thick border
     */
    public static final int BORDER_THIN_THICK = 12;
    /**
     * Constant for a thin thick thin border
     */
    public static final int BORDER_THIN_THICK_THIN = 13;
    /**
     * Constant for a thick thin medium border
     */
    public static final int BORDER_THICK_THIN_MED = 14;
    /**
     * Constant for a thin thick medium border
     */
    public static final int BORDER_THIN_THICK_MED = 15;
    /**
     * Constant for a thin thick thin medium border
     */
    public static final int BORDER_THIN_THICK_THIN_MED = 16;
    /**
     * Constant for a thick thin large border
     */
    public static final int BORDER_THICK_THIN_LARGE = 17;
    /**
     * Constant for a thin thick large border
     */
    public static final int BORDER_THIN_THICK_LARGE = 18;
    /**
     * Constant for a thin thick thin large border
     */
    public static final int BORDER_THIN_THICK_THIN_LARGE = 19;
    /**
     * Constant for a wavy border
     */
    public static final int BORDER_WAVY = 20;
    /**
     * Constant for a double wavy border
     */
    public static final int BORDER_DOUBLE_WAVY = 21;
    /**
     * Constant for a striped border
     */
    public static final int BORDER_STRIPED = 22;
    /**
     * Constant for an embossed border
     */
    public static final int BORDER_EMBOSS = 23;
    /**
     * Constant for an engraved border
     */
    public static final int BORDER_ENGRAVE = 24;
    
    /**
     * The type of this RtfBorder
     */
    private int borderType = ROW_BORDER;
    /**
     * The position of this RtfBorder
     */
    private int borderPosition = NO_BORDER;
    /**
     * The style of this RtfBorder
     */
    private int borderStyle = BORDER_NONE;
    /**
     * The width of this RtfBorder
     */
    private int borderWidth = 20;
    /**
     * The colour of this RtfBorder
     */
    private RtfColor borderColor = null;
    
    /**
     * Makes a copy of the given RtfBorder
     * 
     * @param doc The RtfDocument this RtfBorder belongs to
     * @param borderType The border type of this RtfBorder
     * @param border The RtfBorder to copy
     */
    protected RtfBorder(RtfDocument doc, int borderType, RtfBorder border) {
        super(doc);
        this.borderType = borderType;
        this.borderPosition = border.getBorderPosition();
        this.borderStyle = border.getBorderStyle();
        this.borderWidth = border.getBorderWidth();
        this.borderColor = new RtfColor(this.document, border.getBorderColor());
    }
    
    /**
     * Constructs a RtfBorder
     * 
     * @param doc The RtfDocument this RtfBorder belongs to
     * @param borderType The type of border this RtfBorder is
     * @param borderPosition The position of this RtfBorder
     * @param borderStyle The style of this RtfBorder
     * @param borderWidth The width of this RtfBorder
     * @param borderColor The colour of this RtfBorder
     */
    protected RtfBorder(RtfDocument doc, int borderType, int borderPosition, int borderStyle, float borderWidth, Color borderColor) {
        super(doc);
        this.borderType = borderType;
        this.borderPosition = borderPosition;
        this.borderStyle = borderStyle;
        if(borderWidth > 2) {
            borderWidth = 2;
        }
        this.borderWidth = (int) (borderWidth * TWIPS_FACTOR);
        if(this.borderWidth == 0) {
            this.borderStyle = BORDER_NONE;
        }
        if(borderColor == null) {
            this.borderColor = new RtfColor(this.document, new Color(0, 0, 0));
        } else {
            this.borderColor = new RtfColor(this.document, borderColor);
        }
    }
    
    /**
     * Writes the RtfBorder settings
     * 
     * @return A byte array with the RtfBorder settings
     */
    public byte[] write() {
        if(this.borderStyle == BORDER_NONE || this.borderPosition == NO_BORDER || this.borderWidth == 0) {
            return new byte[0];
        }
        
        ByteArrayOutputStream result = new ByteArrayOutputStream();
        try {
            if(this.borderType == ROW_BORDER) {
                switch(this.borderPosition) {
                    case LEFT_BORDER:
                        result.write(ROW_BORDER_LEFT);
                    	break;
                    case TOP_BORDER:
                        result.write(ROW_BORDER_TOP);
                        break;
                    case RIGHT_BORDER:
                        result.write(ROW_BORDER_RIGHT);
                        break;
                    case BOTTOM_BORDER:
                        result.write(ROW_BORDER_BOTTOM);
                        break;
                    case HORIZONTAL_BORDER:
                        result.write(ROW_BORDER_HORIZONTAL);
                        break;
                    case VERTICAL_BORDER:
                        result.write(ROW_BORDER_VERTICAL);
                        break;
                    default:
                        return new byte[0];
                }
                result.write(writeBorderStyle());
                result.write(BORDER_WIDTH);
                result.write(intToByteArray(this.borderWidth));
                result.write(BORDER_COLOR_NUMBER);
                result.write(intToByteArray(this.borderColor.getColorNumber()));
                result.write('\n');
            } else if(this.borderType == CELL_BORDER) {
                switch(this.borderPosition) {
                    case LEFT_BORDER:
                        result.write(CELL_BORDER_LEFT);
                    	break;
                    case TOP_BORDER:
                        result.write(CELL_BORDER_TOP);
                        break;
                    case RIGHT_BORDER:
                        result.write(CELL_BORDER_RIGHT);
                        break;
                    case BOTTOM_BORDER:
                        result.write(CELL_BORDER_BOTTOM);
                        break;
                    default:
                        return new byte[0];
                }
                result.write(writeBorderStyle());
                result.write(BORDER_WIDTH);
                result.write(intToByteArray(this.borderWidth));
                result.write(BORDER_COLOR_NUMBER);
                result.write(intToByteArray(this.borderColor.getColorNumber()));
                result.write('\n');
            }
        } catch(IOException ioe) {
            ioe.printStackTrace();
        }
        
        return result.toByteArray();
    }
    
    /**
     * Writes the style of this RtfBorder
     * 
     * @return A byte array containing the style of this RtfBorder
     */
    private byte[] writeBorderStyle() {
        switch(this.borderStyle) {
            case BORDER_NONE                    : return new byte[0];
            case BORDER_SINGLE 					: return BORDER_STYLE_SINGLE;
            case BORDER_DOUBLE_THICK	 		: return BORDER_STYLE_DOUBLE_THICK;
            case BORDER_SHADOWED 				: return BORDER_STYLE_SHADOWED;
            case BORDER_DOTTED   				: return BORDER_STYLE_DOTTED;
            case BORDER_DASHED   				: return BORDER_STYLE_DASHED;
            case BORDER_HAIRLINE   				: return BORDER_STYLE_HAIRLINE;
            case BORDER_DOUBLE 		  			: return BORDER_STYLE_DOUBLE;
            case BORDER_DOT_DASH   				: return BORDER_STYLE_DOT_DASH;
            case BORDER_DOT_DOT_DASH			: return BORDER_STYLE_DOT_DOT_DASH;
            case BORDER_TRIPLE					: return BORDER_STYLE_TRIPLE;
            case BORDER_THICK_THIN				: return BORDER_STYLE_THICK_THIN;
            case BORDER_THIN_THICK				: return BORDER_STYLE_THIN_THICK;
            case BORDER_THIN_THICK_THIN			: return BORDER_STYLE_THIN_THICK_THIN;
            case BORDER_THICK_THIN_MED			: return BORDER_STYLE_THICK_THIN_MED;
            case BORDER_THIN_THICK_MED			: return BORDER_STYLE_THIN_THICK_MED;
            case BORDER_THIN_THICK_THIN_MED		: return BORDER_STYLE_THIN_THICK_THIN_MED;
            case BORDER_THICK_THIN_LARGE		: return BORDER_STYLE_THICK_THIN_LARGE;
            case BORDER_THIN_THICK_LARGE		: return BORDER_STYLE_THIN_THICK_LARGE;
            case BORDER_THIN_THICK_THIN_LARGE	: return BORDER_STYLE_THIN_THICK_THIN_LARGE;
            case BORDER_WAVY					: return BORDER_STYLE_WAVY;
            case BORDER_DOUBLE_WAVY				: return BORDER_STYLE_DOUBLE_WAVY;
            case BORDER_STRIPED					: return BORDER_STYLE_STRIPED;
            case BORDER_EMBOSS					: return BORDER_STYLE_EMBOSS;
            case BORDER_ENGRAVE					: return BORDER_STYLE_ENGRAVE;
            default                             : return BORDER_STYLE_SINGLE;
        }
    }
    
    /**
     * Gets the colour of this RtfBorder
     * 
     * @return Returns RtfColor of this RtfBorder
     */
    protected RtfColor getBorderColor() {
        return borderColor;
    }

    /**
     * Gets the position of this RtfBorder
     * @return Returns the position of this RtfBorder
     */
    protected int getBorderPosition() {
        return borderPosition;
    }

    /**
     * Gets the style of this RtfBorder
     * 
     * @return Returns the style of this RtfBorder
     */
    protected int getBorderStyle() {
        return borderStyle;
    }

    /**
     * Gets the type of this RtfBorder
     * 
     * @return Returns the type of this RtfBorder
     */
    protected int getBorderType() {
        return borderType;
    }

    /**
     * Gets the width of this RtfBorder
     * 
     * @return Returns the width of this RtfBorder
     */
    protected int getBorderWidth() {
        return borderWidth;
    }
}