001: /*
002: * $Id: RtfBorder.java 2776 2007-05-23 20:01:40Z hallm $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
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.rtf.table;
052:
053: import java.awt.Color;
054: import java.io.ByteArrayOutputStream;
055: import java.io.IOException;
056: import java.io.OutputStream;
057:
058: import com.lowagie.text.rtf.RtfElement;
059: import com.lowagie.text.rtf.document.RtfDocument;
060: import com.lowagie.text.rtf.style.RtfColor;
061:
062: /**
063: * The RtfBorder handle one row or cell border.
064: * INTERNAL USE ONLY
065: *
066: * @version $Id: RtfBorder.java 2776 2007-05-23 20:01:40Z hallm $
067: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
068: * @author Thomas Bickel (tmb99@inode.at)
069: */
070: public class RtfBorder extends RtfElement {
071:
072: /**
073: * Constant for the left row border
074: */
075: protected static final byte[] ROW_BORDER_LEFT = "\\trbrdrl"
076: .getBytes();
077: /**
078: * Constant for the top row border
079: */
080: protected static final byte[] ROW_BORDER_TOP = "\\trbrdrt"
081: .getBytes();
082: /**
083: * Constant for the right row border
084: */
085: protected static final byte[] ROW_BORDER_RIGHT = "\\trbrdrr"
086: .getBytes();
087: /**
088: * Constant for the bottom row border
089: */
090: protected static final byte[] ROW_BORDER_BOTTOM = "\\trbrdrb"
091: .getBytes();
092: /**
093: * Constant for the horizontal line
094: */
095: protected static final byte[] ROW_BORDER_HORIZONTAL = "\\trbrdrh"
096: .getBytes();
097: /**
098: * Constant for the vertical line
099: */
100: protected static final byte[] ROW_BORDER_VERTICAL = "\\trbrdrv"
101: .getBytes();
102: /**
103: * Constant for the left cell border
104: */
105: protected static final byte[] CELL_BORDER_LEFT = "\\clbrdrl"
106: .getBytes();
107: /**
108: * Constant for the top cell border
109: */
110: protected static final byte[] CELL_BORDER_TOP = "\\clbrdrt"
111: .getBytes();
112: /**
113: * Constant for the right cell border
114: */
115: protected static final byte[] CELL_BORDER_RIGHT = "\\clbrdrr"
116: .getBytes();
117: /**
118: * Constant for the bottom cell border
119: */
120: protected static final byte[] CELL_BORDER_BOTTOM = "\\clbrdrb"
121: .getBytes();
122: /**
123: * Constant for the border width
124: */
125: protected static final byte[] BORDER_WIDTH = "\\brdrw".getBytes();
126: /**
127: * Constant for the border colour number
128: */
129: protected static final byte[] BORDER_COLOR_NUMBER = "\\brdrcf"
130: .getBytes();
131: /**
132: * Constant for the single border style
133: */
134: protected static final byte[] BORDER_STYLE_SINGLE = "\\brdrs"
135: .getBytes();
136: /**
137: * Constant for the double thick border style
138: */
139: protected static final byte[] BORDER_STYLE_DOUBLE_THICK = "\\brdrth"
140: .getBytes();
141: /**
142: * Constant for the shadowed border style
143: */
144: protected static final byte[] BORDER_STYLE_SHADOWED = "\\brdrsh"
145: .getBytes();
146: /**
147: * Constant for the dotted border style
148: */
149: protected static final byte[] BORDER_STYLE_DOTTED = "\\brdrdot"
150: .getBytes();
151: /**
152: * Constant for the dashed border style
153: */
154: protected static final byte[] BORDER_STYLE_DASHED = "\\brdrdash"
155: .getBytes();
156: /**
157: * Constant for the hairline border style
158: */
159: protected static final byte[] BORDER_STYLE_HAIRLINE = "\\brdrhair"
160: .getBytes();
161: /**
162: * Constant for the double border style
163: */
164: protected static final byte[] BORDER_STYLE_DOUBLE = "\\brdrdb"
165: .getBytes();
166: /**
167: * Constant for the dot dash border style
168: */
169: protected static final byte[] BORDER_STYLE_DOT_DASH = "\\brdrdashd"
170: .getBytes();
171: /**
172: * Constant for the dot dot dash border style
173: */
174: protected static final byte[] BORDER_STYLE_DOT_DOT_DASH = "\\brdrdashdd"
175: .getBytes();
176: /**
177: * Constant for the triple border style
178: */
179: protected static final byte[] BORDER_STYLE_TRIPLE = "\\brdrtriple"
180: .getBytes();
181: /**
182: * Constant for the thick thin border style
183: */
184: protected static final byte[] BORDER_STYLE_THICK_THIN = "\\brdrtnthsg"
185: .getBytes();
186: /**
187: * Constant for the thin thick border style
188: */
189: protected static final byte[] BORDER_STYLE_THIN_THICK = "\\brdrthtnsg"
190: .getBytes();
191: /**
192: * Constant for the thin thick thin border style
193: */
194: protected static final byte[] BORDER_STYLE_THIN_THICK_THIN = "\\brdrtnthtnsg"
195: .getBytes();
196: /**
197: * Constant for the thick thin medium border style
198: */
199: protected static final byte[] BORDER_STYLE_THICK_THIN_MED = "\\brdrtnthmg"
200: .getBytes();
201: /**
202: * Constant for the thin thick medium border style
203: */
204: protected static final byte[] BORDER_STYLE_THIN_THICK_MED = "\\brdrthtnmg"
205: .getBytes();
206: /**
207: * Constant for the thin thick thin medium border style
208: */
209: protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_MED = "\\brdrtnthtnmg"
210: .getBytes();
211: /**
212: * Constant for the thick thin large border style
213: */
214: protected static final byte[] BORDER_STYLE_THICK_THIN_LARGE = "\\brdrtnthlg"
215: .getBytes();
216: /**
217: * Constant for the thin thick large border style
218: */
219: protected static final byte[] BORDER_STYLE_THIN_THICK_LARGE = "\\brdrthtnlg"
220: .getBytes();
221: /**
222: * Constant for the thin thick thin large border style
223: */
224: protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_LARGE = "\\brdrtnthtnlg"
225: .getBytes();
226: /**
227: * Constant for the wavy border style
228: */
229: protected static final byte[] BORDER_STYLE_WAVY = "\\brdrwavy"
230: .getBytes();
231: /**
232: * Constant for the double wavy border style
233: */
234: protected static final byte[] BORDER_STYLE_DOUBLE_WAVY = "\\brdrwavydb"
235: .getBytes();
236: /**
237: * Constant for the striped border style
238: */
239: protected static final byte[] BORDER_STYLE_STRIPED = "\\brdrdashdotstr"
240: .getBytes();
241: /**
242: * Constant for the embossed border style
243: */
244: protected static final byte[] BORDER_STYLE_EMBOSS = "\\brdremboss"
245: .getBytes();
246: /**
247: * Constant for the engraved border style
248: */
249: protected static final byte[] BORDER_STYLE_ENGRAVE = "\\brdrengrave"
250: .getBytes();
251:
252: /**
253: * Constant for a row border
254: */
255: protected static final int ROW_BORDER = 1;
256: /**
257: * Constant for a cell border
258: */
259: protected static final int CELL_BORDER = 2;
260:
261: /**
262: * This border is no border :-)
263: */
264: protected static final int NO_BORDER = 0;
265: /**
266: * Constant for a left border
267: */
268: protected static final int LEFT_BORDER = 1;
269: /**
270: * Constant for a top border
271: */
272: protected static final int TOP_BORDER = 2;
273: /**
274: * Constant for a right border
275: */
276: protected static final int RIGHT_BORDER = 4;
277: /**
278: * Constant for a bottom border
279: */
280: protected static final int BOTTOM_BORDER = 8;
281: /**
282: * Constant for a box (left, top, right, bottom) border
283: */
284: protected static final int BOX_BORDER = 15;
285: /**
286: * Constant for a vertical line
287: */
288: protected static final int VERTICAL_BORDER = 16;
289: /**
290: * Constant for a horizontal line
291: */
292: protected static final int HORIZONTAL_BORDER = 32;
293:
294: /**
295: * Constant for a border with no border
296: */
297: public static final int BORDER_NONE = 0;
298: /**
299: * Constant for a single border
300: */
301: public static final int BORDER_SINGLE = 1;
302: /**
303: * Constant for a double thick border
304: */
305: public static final int BORDER_DOUBLE_THICK = 2;
306: /**
307: * Constant for a shadowed border
308: */
309: public static final int BORDER_SHADOWED = 3;
310: /**
311: * Constant for a dotted border
312: */
313: public static final int BORDER_DOTTED = 4;
314: /**
315: * Constant for a dashed border
316: */
317: public static final int BORDER_DASHED = 5;
318: /**
319: * Constant for a hairline border
320: */
321: public static final int BORDER_HAIRLINE = 6;
322: /**
323: * Constant for a double border
324: */
325: public static final int BORDER_DOUBLE = 7;
326: /**
327: * Constant for a dot dash border
328: */
329: public static final int BORDER_DOT_DASH = 8;
330: /**
331: * Constant for a dot dot dash border
332: */
333: public static final int BORDER_DOT_DOT_DASH = 9;
334: /**
335: * Constant for a triple border
336: */
337: public static final int BORDER_TRIPLE = 10;
338: /**
339: * Constant for a thick thin border
340: */
341: public static final int BORDER_THICK_THIN = 11;
342: /**
343: * Constant for a thin thick border
344: */
345: public static final int BORDER_THIN_THICK = 12;
346: /**
347: * Constant for a thin thick thin border
348: */
349: public static final int BORDER_THIN_THICK_THIN = 13;
350: /**
351: * Constant for a thick thin medium border
352: */
353: public static final int BORDER_THICK_THIN_MED = 14;
354: /**
355: * Constant for a thin thick medium border
356: */
357: public static final int BORDER_THIN_THICK_MED = 15;
358: /**
359: * Constant for a thin thick thin medium border
360: */
361: public static final int BORDER_THIN_THICK_THIN_MED = 16;
362: /**
363: * Constant for a thick thin large border
364: */
365: public static final int BORDER_THICK_THIN_LARGE = 17;
366: /**
367: * Constant for a thin thick large border
368: */
369: public static final int BORDER_THIN_THICK_LARGE = 18;
370: /**
371: * Constant for a thin thick thin large border
372: */
373: public static final int BORDER_THIN_THICK_THIN_LARGE = 19;
374: /**
375: * Constant for a wavy border
376: */
377: public static final int BORDER_WAVY = 20;
378: /**
379: * Constant for a double wavy border
380: */
381: public static final int BORDER_DOUBLE_WAVY = 21;
382: /**
383: * Constant for a striped border
384: */
385: public static final int BORDER_STRIPED = 22;
386: /**
387: * Constant for an embossed border
388: */
389: public static final int BORDER_EMBOSS = 23;
390: /**
391: * Constant for an engraved border
392: */
393: public static final int BORDER_ENGRAVE = 24;
394:
395: /**
396: * The type of this RtfBorder
397: */
398: private int borderType = ROW_BORDER;
399: /**
400: * The position of this RtfBorder
401: */
402: private int borderPosition = NO_BORDER;
403: /**
404: * The style of this RtfBorder
405: */
406: private int borderStyle = BORDER_NONE;
407: /**
408: * The width of this RtfBorder
409: */
410: private int borderWidth = 20;
411: /**
412: * The colour of this RtfBorder
413: */
414: private RtfColor borderColor = null;
415:
416: /**
417: * Makes a copy of the given RtfBorder
418: *
419: * @param doc The RtfDocument this RtfBorder belongs to
420: * @param borderType The border type of this RtfBorder
421: * @param border The RtfBorder to copy
422: */
423: protected RtfBorder(RtfDocument doc, int borderType,
424: RtfBorder border) {
425: super (doc);
426: this .borderType = borderType;
427: this .borderPosition = border.getBorderPosition();
428: this .borderStyle = border.getBorderStyle();
429: this .borderWidth = border.getBorderWidth();
430: this .borderColor = new RtfColor(this .document, border
431: .getBorderColor());
432: }
433:
434: /**
435: * Constructs a RtfBorder
436: *
437: * @param doc The RtfDocument this RtfBorder belongs to
438: * @param borderType The type of border this RtfBorder is
439: * @param borderPosition The position of this RtfBorder
440: * @param borderStyle The style of this RtfBorder
441: * @param borderWidth The width of this RtfBorder
442: * @param borderColor The colour of this RtfBorder
443: */
444: protected RtfBorder(RtfDocument doc, int borderType,
445: int borderPosition, int borderStyle, float borderWidth,
446: Color borderColor) {
447: super (doc);
448: this .borderType = borderType;
449: this .borderPosition = borderPosition;
450: this .borderStyle = borderStyle;
451: if (borderWidth > 2) {
452: borderWidth = 2;
453: }
454: this .borderWidth = (int) (borderWidth * TWIPS_FACTOR);
455: if (this .borderWidth == 0) {
456: this .borderStyle = BORDER_NONE;
457: }
458: if (borderColor == null) {
459: this .borderColor = new RtfColor(this .document, new Color(0,
460: 0, 0));
461: } else {
462: this .borderColor = new RtfColor(this .document, borderColor);
463: }
464: }
465:
466: /**
467: * Writes the RtfBorder settings
468: *
469: * @return A byte array with the RtfBorder settings
470: * @deprecated replaced by {@link #writeContent(OutputStream)}
471: */
472: public byte[] write() {
473: ByteArrayOutputStream result = new ByteArrayOutputStream();
474: try {
475: writeContent(result);
476: } catch (IOException ioe) {
477: ioe.printStackTrace();
478: }
479:
480: return result.toByteArray();
481: }
482:
483: /**
484: * Writes the RtfBorder settings
485: */
486: public void writeContent(final OutputStream result)
487: throws IOException {
488: if (this .borderStyle == BORDER_NONE
489: || this .borderPosition == NO_BORDER
490: || this .borderWidth == 0) {
491: return;
492: }
493:
494: if (this .borderType == ROW_BORDER) {
495: switch (this .borderPosition) {
496: case LEFT_BORDER:
497: result.write(ROW_BORDER_LEFT);
498: break;
499: case TOP_BORDER:
500: result.write(ROW_BORDER_TOP);
501: break;
502: case RIGHT_BORDER:
503: result.write(ROW_BORDER_RIGHT);
504: break;
505: case BOTTOM_BORDER:
506: result.write(ROW_BORDER_BOTTOM);
507: break;
508: case HORIZONTAL_BORDER:
509: result.write(ROW_BORDER_HORIZONTAL);
510: break;
511: case VERTICAL_BORDER:
512: result.write(ROW_BORDER_VERTICAL);
513: break;
514: default:
515: return;
516: }
517: result.write(writeBorderStyle());
518: result.write(BORDER_WIDTH);
519: result.write(intToByteArray(this .borderWidth));
520: result.write(BORDER_COLOR_NUMBER);
521: result.write(intToByteArray(this .borderColor
522: .getColorNumber()));
523: result.write('\n');
524: } else if (this .borderType == CELL_BORDER) {
525: switch (this .borderPosition) {
526: case LEFT_BORDER:
527: result.write(CELL_BORDER_LEFT);
528: break;
529: case TOP_BORDER:
530: result.write(CELL_BORDER_TOP);
531: break;
532: case RIGHT_BORDER:
533: result.write(CELL_BORDER_RIGHT);
534: break;
535: case BOTTOM_BORDER:
536: result.write(CELL_BORDER_BOTTOM);
537: break;
538: default:
539: return;
540: }
541: result.write(writeBorderStyle());
542: result.write(BORDER_WIDTH);
543: result.write(intToByteArray(this .borderWidth));
544: result.write(BORDER_COLOR_NUMBER);
545: result.write(intToByteArray(this .borderColor
546: .getColorNumber()));
547: result.write('\n');
548: }
549: }
550:
551: /**
552: * Writes the style of this RtfBorder
553: *
554: * @return A byte array containing the style of this RtfBorder
555: */
556: private byte[] writeBorderStyle() {
557: switch (this .borderStyle) {
558: case BORDER_NONE:
559: return new byte[0];
560: case BORDER_SINGLE:
561: return BORDER_STYLE_SINGLE;
562: case BORDER_DOUBLE_THICK:
563: return BORDER_STYLE_DOUBLE_THICK;
564: case BORDER_SHADOWED:
565: return BORDER_STYLE_SHADOWED;
566: case BORDER_DOTTED:
567: return BORDER_STYLE_DOTTED;
568: case BORDER_DASHED:
569: return BORDER_STYLE_DASHED;
570: case BORDER_HAIRLINE:
571: return BORDER_STYLE_HAIRLINE;
572: case BORDER_DOUBLE:
573: return BORDER_STYLE_DOUBLE;
574: case BORDER_DOT_DASH:
575: return BORDER_STYLE_DOT_DASH;
576: case BORDER_DOT_DOT_DASH:
577: return BORDER_STYLE_DOT_DOT_DASH;
578: case BORDER_TRIPLE:
579: return BORDER_STYLE_TRIPLE;
580: case BORDER_THICK_THIN:
581: return BORDER_STYLE_THICK_THIN;
582: case BORDER_THIN_THICK:
583: return BORDER_STYLE_THIN_THICK;
584: case BORDER_THIN_THICK_THIN:
585: return BORDER_STYLE_THIN_THICK_THIN;
586: case BORDER_THICK_THIN_MED:
587: return BORDER_STYLE_THICK_THIN_MED;
588: case BORDER_THIN_THICK_MED:
589: return BORDER_STYLE_THIN_THICK_MED;
590: case BORDER_THIN_THICK_THIN_MED:
591: return BORDER_STYLE_THIN_THICK_THIN_MED;
592: case BORDER_THICK_THIN_LARGE:
593: return BORDER_STYLE_THICK_THIN_LARGE;
594: case BORDER_THIN_THICK_LARGE:
595: return BORDER_STYLE_THIN_THICK_LARGE;
596: case BORDER_THIN_THICK_THIN_LARGE:
597: return BORDER_STYLE_THIN_THICK_THIN_LARGE;
598: case BORDER_WAVY:
599: return BORDER_STYLE_WAVY;
600: case BORDER_DOUBLE_WAVY:
601: return BORDER_STYLE_DOUBLE_WAVY;
602: case BORDER_STRIPED:
603: return BORDER_STYLE_STRIPED;
604: case BORDER_EMBOSS:
605: return BORDER_STYLE_EMBOSS;
606: case BORDER_ENGRAVE:
607: return BORDER_STYLE_ENGRAVE;
608: default:
609: return BORDER_STYLE_SINGLE;
610: }
611: }
612:
613: /**
614: * Gets the colour of this RtfBorder
615: *
616: * @return Returns RtfColor of this RtfBorder
617: */
618: protected RtfColor getBorderColor() {
619: return borderColor;
620: }
621:
622: /**
623: * Gets the position of this RtfBorder
624: * @return Returns the position of this RtfBorder
625: */
626: protected int getBorderPosition() {
627: return borderPosition;
628: }
629:
630: /**
631: * Gets the style of this RtfBorder
632: *
633: * @return Returns the style of this RtfBorder
634: */
635: protected int getBorderStyle() {
636: return borderStyle;
637: }
638:
639: /**
640: * Gets the type of this RtfBorder
641: *
642: * @return Returns the type of this RtfBorder
643: */
644: protected int getBorderType() {
645: return borderType;
646: }
647:
648: /**
649: * Gets the width of this RtfBorder
650: *
651: * @return Returns the width of this RtfBorder
652: */
653: protected int getBorderWidth() {
654: return borderWidth;
655: }
656: }
|