1 /*
2  * $Id$
3  *
4  * Copyright 2001, 2002, 2003, 2004 by Mark Hall
5  *
6  * The contents of this file are subject to the Mozilla Public License Version 1.1
7  * (the "License"); you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the License.
13  *
14  * The Original Code is 'iText, a free JAVA-PDF library'.
15  *
16  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
17  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
18  * All Rights Reserved.
19  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
20  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source code
23  * where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
27  * provisions of LGPL are applicable instead of those above.  If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
34  *
35  * This library is free software; you can redistribute it and/or modify it
36  * under the terms of the MPL as stated above or under the terms of the GNU
37  * Library General Public License as published by the Free Software Foundation;
38  * either version 2 of the License, or any later version.
39  *
40  * This library is distributed in the hope that it will be useful, but WITHOUT
41  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
42  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
43  * details.
44  *
45  * If you didn't download this code from the following link, you should check if
46  * you aren't using an obsolete version:
47  * http://www.lowagie.com/iText/
48  */
49 
50 package com.lowagie.text.rtf.table;
51 
52 import java.awt.Color;
53 import java.io.IOException;
54 import java.io.OutputStream;
55 
56 import com.lowagie.text.DocWriter;
57 import com.lowagie.text.rtf.RtfElement;
58 import com.lowagie.text.rtf.document.RtfDocument;
59 import com.lowagie.text.rtf.style.RtfColor;
60 
61 
62 /**
63  * The RtfBorder handle one row or cell border.
64  * INTERNAL USE ONLY
65  *
66  * @version $Id$
67  * @author Mark Hall (Mark.Hall@mail.room3b.eu)
68  * @author Thomas Bickel (tmb99@inode.at)
69  */
70 public class RtfBorder extends RtfElement {
71 
72     /**
73      * Constant for the left row border
74      */
75     protected static final byte[] ROW_BORDER_LEFT = DocWriter.getISOBytes("\\trbrdrl");
76     /**
77      * Constant for the top row border
78      */
79     protected static final byte[] ROW_BORDER_TOP = DocWriter.getISOBytes("\\trbrdrt");
80     /**
81      * Constant for the right row border
82      */
83     protected static final byte[] ROW_BORDER_RIGHT = DocWriter.getISOBytes("\\trbrdrr");
84     /**
85      * Constant for the bottom row border
86      */
87     protected static final byte[] ROW_BORDER_BOTTOM = DocWriter.getISOBytes("\\trbrdrb");
88     /**
89      * Constant for the horizontal line
90      */
91     protected static final byte[] ROW_BORDER_HORIZONTAL = DocWriter.getISOBytes("\\trbrdrh");
92     /**
93      * Constant for the vertical line
94      */
95     protected static final byte[] ROW_BORDER_VERTICAL = DocWriter.getISOBytes("\\trbrdrv");
96     /**
97      * Constant for the left cell border
98      */
99     protected static final byte[] CELL_BORDER_LEFT = DocWriter.getISOBytes("\\clbrdrl");
100     /**
101      * Constant for the top cell border
102      */
103     protected static final byte[] CELL_BORDER_TOP = DocWriter.getISOBytes("\\clbrdrt");
104     /**
105      * Constant for the right cell border
106      */
107     protected static final byte[] CELL_BORDER_RIGHT = DocWriter.getISOBytes("\\clbrdrr");
108     /**
109      * Constant for the bottom cell border
110      */
111     protected static final byte[] CELL_BORDER_BOTTOM = DocWriter.getISOBytes("\\clbrdrb");
112     /**
113      * Constant for the border width
114      */
115     protected static final byte[] BORDER_WIDTH = DocWriter.getISOBytes("\\brdrw");
116     /**
117      * Constant for the border color number
118      */
119     protected static final byte[] BORDER_COLOR_NUMBER = DocWriter.getISOBytes("\\brdrcf");
120     /**
121      * Constant for the single border style
122      */
123     protected static final byte[] BORDER_STYLE_SINGLE = DocWriter.getISOBytes("\\brdrs");
124     /**
125      * Constant for the double thick border style
126      */
127     protected static final byte[] BORDER_STYLE_DOUBLE_THICK	= DocWriter.getISOBytes("\\brdrth");
128     /**
129      * Constant for the shadowed border style
130      */
131     protected static final byte[] BORDER_STYLE_SHADOWED = DocWriter.getISOBytes("\\brdrsh");
132     /**
133      * Constant for the dotted border style
134      */
135     protected static final byte[] BORDER_STYLE_DOTTED = DocWriter.getISOBytes("\\brdrdot");
136     /**
137      * Constant for the dashed border style
138      */
139     protected static final byte[] BORDER_STYLE_DASHED = DocWriter.getISOBytes("\\brdrdash");
140     /**
141      * Constant for the hairline border style
142      */
143     protected static final byte[] BORDER_STYLE_HAIRLINE = DocWriter.getISOBytes("\\brdrhair");
144     /**
145      * Constant for the double border style
146      */
147     protected static final byte[] BORDER_STYLE_DOUBLE = DocWriter.getISOBytes("\\brdrdb");
148     /**
149      * Constant for the dot dash border style
150      */
151     protected static final byte[] BORDER_STYLE_DOT_DASH = DocWriter.getISOBytes("\\brdrdashd");
152     /**
153      * Constant for the dot dot dash border style
154      */
155     protected static final byte[] BORDER_STYLE_DOT_DOT_DASH	= DocWriter.getISOBytes("\\brdrdashdd");
156     /**
157      * Constant for the triple border style
158      */
159     protected static final byte[] BORDER_STYLE_TRIPLE = DocWriter.getISOBytes("\\brdrtriple");
160     /**
161      * Constant for the thick thin border style
162      */
163     protected static final byte[] BORDER_STYLE_THICK_THIN = DocWriter.getISOBytes("\\brdrtnthsg");
164     /**
165      * Constant for the thin thick border style
166      */
167     protected static final byte[] BORDER_STYLE_THIN_THICK = DocWriter.getISOBytes("\\brdrthtnsg");
168     /**
169      * Constant for the thin thick thin border style
170      */
171     protected static final byte[] BORDER_STYLE_THIN_THICK_THIN = DocWriter.getISOBytes("\\brdrtnthtnsg");
172     /**
173      * Constant for the thick thin medium border style
174      */
175     protected static final byte[] BORDER_STYLE_THICK_THIN_MED = DocWriter.getISOBytes("\\brdrtnthmg");
176     /**
177      * Constant for the thin thick medium border style
178      */
179     protected static final byte[] BORDER_STYLE_THIN_THICK_MED = DocWriter.getISOBytes("\\brdrthtnmg");
180     /**
181      * Constant for the thin thick thin medium border style
182      */
183     protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_MED = DocWriter.getISOBytes("\\brdrtnthtnmg");
184     /**
185      * Constant for the thick thin large border style
186      */
187     protected static final byte[] BORDER_STYLE_THICK_THIN_LARGE = DocWriter.getISOBytes("\\brdrtnthlg");
188     /**
189      * Constant for the thin thick large border style
190      */
191     protected static final byte[] BORDER_STYLE_THIN_THICK_LARGE	= DocWriter.getISOBytes("\\brdrthtnlg");
192     /**
193      * Constant for the thin thick thin large border style
194      */
195     protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_LARGE = DocWriter.getISOBytes("\\brdrtnthtnlg");
196     /**
197      * Constant for the wavy border style
198      */
199     protected static final byte[] BORDER_STYLE_WAVY = DocWriter.getISOBytes("\\brdrwavy");
200     /**
201      * Constant for the double wavy border style
202      */
203     protected static final byte[] BORDER_STYLE_DOUBLE_WAVY = DocWriter.getISOBytes("\\brdrwavydb");
204     /**
205      * Constant for the striped border style
206      */
207     protected static final byte[] BORDER_STYLE_STRIPED = DocWriter.getISOBytes("\\brdrdashdotstr");
208     /**
209      * Constant for the embossed border style
210      */
211     protected static final byte[] BORDER_STYLE_EMBOSS = DocWriter.getISOBytes("\\brdremboss");
212     /**
213      * Constant for the engraved border style
214      */
215     protected static final byte[] BORDER_STYLE_ENGRAVE = DocWriter.getISOBytes("\\brdrengrave");
216 
217     /**
218      * Constant for a row border
219      */
220     protected static final int ROW_BORDER = 1;
221     /**
222      * Constant for a cell border
223      */
224     protected static final int CELL_BORDER = 2;
225 
226     /**
227      * This border is no border :-)
228      */
229     protected static final int NO_BORDER = 0;
230     /**
231      * Constant for a left border
232      */
233     protected static final int LEFT_BORDER = 1;
234     /**
235      * Constant for a top border
236      */
237     protected static final int TOP_BORDER = 2;
238     /**
239      * Constant for a right border
240      */
241     protected static final int RIGHT_BORDER = 4;
242     /**
243      * Constant for a bottom border
244      */
245     protected static final int BOTTOM_BORDER = 8;
246     /**
247      * Constant for a box (left, top, right, bottom) border
248      */
249     protected static final int BOX_BORDER = 15;
250     /**
251      * Constant for a vertical line
252      */
253     protected static final int VERTICAL_BORDER = 16;
254     /**
255      * Constant for a horizontal line
256      */
257     protected static final int HORIZONTAL_BORDER = 32;
258 
259     /**
260      * Constant for a border with no border
261      */
262     public static final int BORDER_NONE = 0;
263     /**
264      * Constant for a single border
265      */
266     public static final int BORDER_SINGLE = 1;
267     /**
268      * Constant for a double thick border
269      */
270     public static final int BORDER_DOUBLE_THICK = 2;
271     /**
272      * Constant for a shadowed border
273      */
274     public static final int BORDER_SHADOWED = 3;
275     /**
276      * Constant for a dotted border
277      */
278     public static final int BORDER_DOTTED = 4;
279     /**
280      * Constant for a dashed border
281      */
282     public static final int BORDER_DASHED = 5;
283     /**
284      * Constant for a hairline border
285      */
286     public static final int BORDER_HAIRLINE = 6;
287     /**
288      * Constant for a double border
289      */
290     public static final int BORDER_DOUBLE = 7;
291     /**
292      * Constant for a dot dash border
293      */
294     public static final int BORDER_DOT_DASH = 8;
295     /**
296      * Constant for a dot dot dash border
297      */
298     public static final int BORDER_DOT_DOT_DASH = 9;
299     /**
300      * Constant for a triple border
301      */
302     public static final int BORDER_TRIPLE = 10;
303     /**
304      * Constant for a thick thin border
305      */
306     public static final int BORDER_THICK_THIN = 11;
307     /**
308      * Constant for a thin thick border
309      */
310     public static final int BORDER_THIN_THICK = 12;
311     /**
312      * Constant for a thin thick thin border
313      */
314     public static final int BORDER_THIN_THICK_THIN = 13;
315     /**
316      * Constant for a thick thin medium border
317      */
318     public static final int BORDER_THICK_THIN_MED = 14;
319     /**
320      * Constant for a thin thick medium border
321      */
322     public static final int BORDER_THIN_THICK_MED = 15;
323     /**
324      * Constant for a thin thick thin medium border
325      */
326     public static final int BORDER_THIN_THICK_THIN_MED = 16;
327     /**
328      * Constant for a thick thin large border
329      */
330     public static final int BORDER_THICK_THIN_LARGE = 17;
331     /**
332      * Constant for a thin thick large border
333      */
334     public static final int BORDER_THIN_THICK_LARGE = 18;
335     /**
336      * Constant for a thin thick thin large border
337      */
338     public static final int BORDER_THIN_THICK_THIN_LARGE = 19;
339     /**
340      * Constant for a wavy border
341      */
342     public static final int BORDER_WAVY = 20;
343     /**
344      * Constant for a double wavy border
345      */
346     public static final int BORDER_DOUBLE_WAVY = 21;
347     /**
348      * Constant for a striped border
349      */
350     public static final int BORDER_STRIPED = 22;
351     /**
352      * Constant for an embossed border
353      */
354     public static final int BORDER_EMBOSS = 23;
355     /**
356      * Constant for an engraved border
357      */
358     public static final int BORDER_ENGRAVE = 24;
359 
360     /**
361      * The type of this RtfBorder
362      */
363     private int borderType = ROW_BORDER;
364     /**
365      * The position of this RtfBorder
366      */
367     private int borderPosition = NO_BORDER;
368     /**
369      * The style of this RtfBorder
370      */
371     private int borderStyle = BORDER_NONE;
372     /**
373      * The width of this RtfBorder
374      */
375     private int borderWidth = 20;
376     /**
377      * The color of this RtfBorder
378      */
379     private RtfColor borderColor = null;
380 
381     /**
382      * Makes a copy of the given RtfBorder
383      *
384      * @param doc The RtfDocument this RtfBorder belongs to
385      * @param borderType The border type of this RtfBorder
386      * @param border The RtfBorder to copy
387      */
RtfBorder(RtfDocument doc, int borderType, RtfBorder border)388     protected RtfBorder(RtfDocument doc, int borderType, RtfBorder border) {
389         super(doc);
390         this.borderType = borderType;
391         this.borderPosition = border.getBorderPosition();
392         this.borderStyle = border.getBorderStyle();
393         this.borderWidth = border.getBorderWidth();
394         this.borderColor = new RtfColor(this.document, border.getBorderColor());
395     }
396 
397     /**
398      * Constructs a RtfBorder
399      *
400      * @param doc The RtfDocument this RtfBorder belongs to
401      * @param borderType The type of border this RtfBorder is
402      * @param borderPosition The position of this RtfBorder
403      * @param borderStyle The style of this RtfBorder
404      * @param borderWidth The width of this RtfBorder
405      * @param borderColor The color of this RtfBorder
406      */
RtfBorder(RtfDocument doc, int borderType, int borderPosition, int borderStyle, float borderWidth, Color borderColor)407     protected RtfBorder(RtfDocument doc, int borderType, int borderPosition, int borderStyle, float borderWidth, Color borderColor) {
408         super(doc);
409         this.borderType = borderType;
410         this.borderPosition = borderPosition;
411         this.borderStyle = borderStyle;
412         this.borderWidth = (int) Math.min((borderWidth * TWIPS_FACTOR), 75);
413         if(this.borderWidth == 0) {
414             this.borderStyle = BORDER_NONE;
415         }
416         if(borderColor == null) {
417             this.borderColor = new RtfColor(this.document, new Color(0, 0, 0));
418         } else {
419             this.borderColor = new RtfColor(this.document, borderColor);
420         }
421     }
422 
423     /**
424      * Writes the RtfBorder settings
425      */
writeContent(final OutputStream result)426     public void writeContent(final OutputStream result) throws IOException
427     {
428         if(this.borderStyle == BORDER_NONE || this.borderPosition == NO_BORDER || this.borderWidth == 0) {
429             return;
430         }
431 
432     	if(this.borderType == ROW_BORDER) {
433             switch(this.borderPosition) {
434                 case LEFT_BORDER:
435                     result.write(ROW_BORDER_LEFT);
436                 	break;
437                 case TOP_BORDER:
438                     result.write(ROW_BORDER_TOP);
439                     break;
440                 case RIGHT_BORDER:
441                     result.write(ROW_BORDER_RIGHT);
442                     break;
443                 case BOTTOM_BORDER:
444                     result.write(ROW_BORDER_BOTTOM);
445                     break;
446                 case HORIZONTAL_BORDER:
447                     result.write(ROW_BORDER_HORIZONTAL);
448                     break;
449                 case VERTICAL_BORDER:
450                     result.write(ROW_BORDER_VERTICAL);
451                     break;
452                 default:
453                     return;
454             }
455             result.write(writeBorderStyle());
456             result.write(BORDER_WIDTH);
457             result.write(intToByteArray(this.borderWidth));
458             result.write(BORDER_COLOR_NUMBER);
459             result.write(intToByteArray(this.borderColor.getColorNumber()));
460             this.document.outputDebugLinebreak(result);
461         } else if(this.borderType == CELL_BORDER) {
462             switch(this.borderPosition) {
463                 case LEFT_BORDER:
464                     result.write(CELL_BORDER_LEFT);
465                 	break;
466                 case TOP_BORDER:
467                     result.write(CELL_BORDER_TOP);
468                     break;
469                 case RIGHT_BORDER:
470                     result.write(CELL_BORDER_RIGHT);
471                     break;
472                 case BOTTOM_BORDER:
473                     result.write(CELL_BORDER_BOTTOM);
474                     break;
475                 default:
476                     return;
477             }
478             result.write(writeBorderStyle());
479             result.write(BORDER_WIDTH);
480             result.write(intToByteArray(this.borderWidth));
481             result.write(BORDER_COLOR_NUMBER);
482             result.write(intToByteArray(this.borderColor.getColorNumber()));
483             this.document.outputDebugLinebreak(result);
484         }
485     }
486 
487     /**
488      * Writes the style of this RtfBorder
489      *
490      * @return A byte array containing the style of this RtfBorder
491      */
writeBorderStyle()492     private byte[] writeBorderStyle() {
493         switch(this.borderStyle) {
494             case BORDER_NONE                    : return new byte[0];
495             case BORDER_SINGLE 					: return BORDER_STYLE_SINGLE;
496             case BORDER_DOUBLE_THICK	 		: return BORDER_STYLE_DOUBLE_THICK;
497             case BORDER_SHADOWED 				: return BORDER_STYLE_SHADOWED;
498             case BORDER_DOTTED   				: return BORDER_STYLE_DOTTED;
499             case BORDER_DASHED   				: return BORDER_STYLE_DASHED;
500             case BORDER_HAIRLINE   				: return BORDER_STYLE_HAIRLINE;
501             case BORDER_DOUBLE 		  			: return BORDER_STYLE_DOUBLE;
502             case BORDER_DOT_DASH   				: return BORDER_STYLE_DOT_DASH;
503             case BORDER_DOT_DOT_DASH			: return BORDER_STYLE_DOT_DOT_DASH;
504             case BORDER_TRIPLE					: return BORDER_STYLE_TRIPLE;
505             case BORDER_THICK_THIN				: return BORDER_STYLE_THICK_THIN;
506             case BORDER_THIN_THICK				: return BORDER_STYLE_THIN_THICK;
507             case BORDER_THIN_THICK_THIN			: return BORDER_STYLE_THIN_THICK_THIN;
508             case BORDER_THICK_THIN_MED			: return BORDER_STYLE_THICK_THIN_MED;
509             case BORDER_THIN_THICK_MED			: return BORDER_STYLE_THIN_THICK_MED;
510             case BORDER_THIN_THICK_THIN_MED		: return BORDER_STYLE_THIN_THICK_THIN_MED;
511             case BORDER_THICK_THIN_LARGE		: return BORDER_STYLE_THICK_THIN_LARGE;
512             case BORDER_THIN_THICK_LARGE		: return BORDER_STYLE_THIN_THICK_LARGE;
513             case BORDER_THIN_THICK_THIN_LARGE	: return BORDER_STYLE_THIN_THICK_THIN_LARGE;
514             case BORDER_WAVY					: return BORDER_STYLE_WAVY;
515             case BORDER_DOUBLE_WAVY				: return BORDER_STYLE_DOUBLE_WAVY;
516             case BORDER_STRIPED					: return BORDER_STYLE_STRIPED;
517             case BORDER_EMBOSS					: return BORDER_STYLE_EMBOSS;
518             case BORDER_ENGRAVE					: return BORDER_STYLE_ENGRAVE;
519             default                             : return BORDER_STYLE_SINGLE;
520         }
521     }
522 
523     /**
524      * Gets the color of this RtfBorder
525      *
526      * @return Returns RtfColor of this RtfBorder
527      */
getBorderColor()528     protected RtfColor getBorderColor() {
529         return borderColor;
530     }
531 
532     /**
533      * Gets the position of this RtfBorder
534      * @return Returns the position of this RtfBorder
535      */
getBorderPosition()536     protected int getBorderPosition() {
537         return borderPosition;
538     }
539 
540     /**
541      * Gets the style of this RtfBorder
542      *
543      * @return Returns the style of this RtfBorder
544      */
getBorderStyle()545     protected int getBorderStyle() {
546         return borderStyle;
547     }
548 
549     /**
550      * Gets the type of this RtfBorder
551      *
552      * @return Returns the type of this RtfBorder
553      */
getBorderType()554     protected int getBorderType() {
555         return borderType;
556     }
557 
558     /**
559      * Gets the width of this RtfBorder
560      *
561      * @return Returns the width of this RtfBorder
562      */
getBorderWidth()563     protected int getBorderWidth() {
564         return borderWidth;
565     }
566 }
567