1 50 51 package com.lowagie.text.rtf.table; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.IOException ; 56 import java.io.OutputStream ; 57 58 import com.lowagie.text.rtf.RtfElement; 59 import com.lowagie.text.rtf.document.RtfDocument; 60 import com.lowagie.text.rtf.style.RtfColor; 61 62 63 71 public class RtfBorder extends RtfElement { 72 73 76 protected static final byte[] ROW_BORDER_LEFT = "\\trbrdrl".getBytes(); 77 80 protected static final byte[] ROW_BORDER_TOP = "\\trbrdrt".getBytes(); 81 84 protected static final byte[] ROW_BORDER_RIGHT = "\\trbrdrr".getBytes(); 85 88 protected static final byte[] ROW_BORDER_BOTTOM = "\\trbrdrb".getBytes(); 89 92 protected static final byte[] ROW_BORDER_HORIZONTAL = "\\trbrdrh".getBytes(); 93 96 protected static final byte[] ROW_BORDER_VERTICAL = "\\trbrdrv".getBytes(); 97 100 protected static final byte[] CELL_BORDER_LEFT = "\\clbrdrl".getBytes(); 101 104 protected static final byte[] CELL_BORDER_TOP = "\\clbrdrt".getBytes(); 105 108 protected static final byte[] CELL_BORDER_RIGHT = "\\clbrdrr".getBytes(); 109 112 protected static final byte[] CELL_BORDER_BOTTOM = "\\clbrdrb".getBytes(); 113 116 protected static final byte[] BORDER_WIDTH = "\\brdrw".getBytes(); 117 120 protected static final byte[] BORDER_COLOR_NUMBER = "\\brdrcf".getBytes(); 121 124 protected static final byte[] BORDER_STYLE_SINGLE = "\\brdrs".getBytes(); 125 128 protected static final byte[] BORDER_STYLE_DOUBLE_THICK = "\\brdrth".getBytes(); 129 132 protected static final byte[] BORDER_STYLE_SHADOWED = "\\brdrsh".getBytes(); 133 136 protected static final byte[] BORDER_STYLE_DOTTED = "\\brdrdot".getBytes(); 137 140 protected static final byte[] BORDER_STYLE_DASHED = "\\brdrdash".getBytes(); 141 144 protected static final byte[] BORDER_STYLE_HAIRLINE = "\\brdrhair".getBytes(); 145 148 protected static final byte[] BORDER_STYLE_DOUBLE = "\\brdrdb".getBytes(); 149 152 protected static final byte[] BORDER_STYLE_DOT_DASH = "\\brdrdashd".getBytes(); 153 156 protected static final byte[] BORDER_STYLE_DOT_DOT_DASH = "\\brdrdashdd".getBytes(); 157 160 protected static final byte[] BORDER_STYLE_TRIPLE = "\\brdrtriple".getBytes(); 161 164 protected static final byte[] BORDER_STYLE_THICK_THIN = "\\brdrtnthsg".getBytes(); 165 168 protected static final byte[] BORDER_STYLE_THIN_THICK = "\\brdrthtnsg".getBytes(); 169 172 protected static final byte[] BORDER_STYLE_THIN_THICK_THIN = "\\brdrtnthtnsg".getBytes(); 173 176 protected static final byte[] BORDER_STYLE_THICK_THIN_MED = "\\brdrtnthmg".getBytes(); 177 180 protected static final byte[] BORDER_STYLE_THIN_THICK_MED = "\\brdrthtnmg".getBytes(); 181 184 protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_MED = "\\brdrtnthtnmg".getBytes(); 185 188 protected static final byte[] BORDER_STYLE_THICK_THIN_LARGE = "\\brdrtnthlg".getBytes(); 189 192 protected static final byte[] BORDER_STYLE_THIN_THICK_LARGE = "\\brdrthtnlg".getBytes(); 193 196 protected static final byte[] BORDER_STYLE_THIN_THICK_THIN_LARGE = "\\brdrtnthtnlg".getBytes(); 197 200 protected static final byte[] BORDER_STYLE_WAVY = "\\brdrwavy".getBytes(); 201 204 protected static final byte[] BORDER_STYLE_DOUBLE_WAVY = "\\brdrwavydb".getBytes(); 205 208 protected static final byte[] BORDER_STYLE_STRIPED = "\\brdrdashdotstr".getBytes(); 209 212 protected static final byte[] BORDER_STYLE_EMBOSS = "\\brdremboss".getBytes(); 213 216 protected static final byte[] BORDER_STYLE_ENGRAVE = "\\brdrengrave".getBytes(); 217 218 221 protected static final int ROW_BORDER = 1; 222 225 protected static final int CELL_BORDER = 2; 226 227 230 protected static final int NO_BORDER = 0; 231 234 protected static final int LEFT_BORDER = 1; 235 238 protected static final int TOP_BORDER = 2; 239 242 protected static final int RIGHT_BORDER = 4; 243 246 protected static final int BOTTOM_BORDER = 8; 247 250 protected static final int BOX_BORDER = 15; 251 254 protected static final int VERTICAL_BORDER = 16; 255 258 protected static final int HORIZONTAL_BORDER = 32; 259 260 263 public static final int BORDER_NONE = 0; 264 267 public static final int BORDER_SINGLE = 1; 268 271 public static final int BORDER_DOUBLE_THICK = 2; 272 275 public static final int BORDER_SHADOWED = 3; 276 279 public static final int BORDER_DOTTED = 4; 280 283 public static final int BORDER_DASHED = 5; 284 287 public static final int BORDER_HAIRLINE = 6; 288 291 public static final int BORDER_DOUBLE = 7; 292 295 public static final int BORDER_DOT_DASH = 8; 296 299 public static final int BORDER_DOT_DOT_DASH = 9; 300 303 public static final int BORDER_TRIPLE = 10; 304 307 public static final int BORDER_THICK_THIN = 11; 308 311 public static final int BORDER_THIN_THICK = 12; 312 315 public static final int BORDER_THIN_THICK_THIN = 13; 316 319 public static final int BORDER_THICK_THIN_MED = 14; 320 323 public static final int BORDER_THIN_THICK_MED = 15; 324 327 public static final int BORDER_THIN_THICK_THIN_MED = 16; 328 331 public static final int BORDER_THICK_THIN_LARGE = 17; 332 335 public static final int BORDER_THIN_THICK_LARGE = 18; 336 339 public static final int BORDER_THIN_THICK_THIN_LARGE = 19; 340 343 public static final int BORDER_WAVY = 20; 344 347 public static final int BORDER_DOUBLE_WAVY = 21; 348 351 public static final int BORDER_STRIPED = 22; 352 355 public static final int BORDER_EMBOSS = 23; 356 359 public static final int BORDER_ENGRAVE = 24; 360 361 364 private int borderType = ROW_BORDER; 365 368 private int borderPosition = NO_BORDER; 369 372 private int borderStyle = BORDER_NONE; 373 376 private int borderWidth = 20; 377 380 private RtfColor borderColor = null; 381 382 389 protected RtfBorder(RtfDocument doc, int borderType, RtfBorder border) { 390 super(doc); 391 this.borderType = borderType; 392 this.borderPosition = border.getBorderPosition(); 393 this.borderStyle = border.getBorderStyle(); 394 this.borderWidth = border.getBorderWidth(); 395 this.borderColor = new RtfColor(this.document, border.getBorderColor()); 396 } 397 398 408 protected RtfBorder(RtfDocument doc, int borderType, int borderPosition, int borderStyle, float borderWidth, Color borderColor) { 409 super(doc); 410 this.borderType = borderType; 411 this.borderPosition = borderPosition; 412 this.borderStyle = borderStyle; 413 if(borderWidth > 2) { 414 borderWidth = 2; 415 } 416 this.borderWidth = (int) (borderWidth * TWIPS_FACTOR); 417 if(this.borderWidth == 0) { 418 this.borderStyle = BORDER_NONE; 419 } 420 if(borderColor == null) { 421 this.borderColor = new RtfColor(this.document, new Color (0, 0, 0)); 422 } else { 423 this.borderColor = new RtfColor(this.document, borderColor); 424 } 425 } 426 427 433 public byte[] write() 434 { 435 ByteArrayOutputStream result = new ByteArrayOutputStream (); 436 try { 437 writeContent(result); 438 } catch(IOException ioe) { 439 ioe.printStackTrace(); 440 } 441 442 return result.toByteArray(); 443 } 444 447 public void writeContent(final OutputStream result) throws IOException 448 { 449 if(this.borderStyle == BORDER_NONE || this.borderPosition == NO_BORDER || this.borderWidth == 0) { 450 return; 451 } 452 453 if(this.borderType == ROW_BORDER) { 454 switch(this.borderPosition) { 455 case LEFT_BORDER: 456 result.write(ROW_BORDER_LEFT); 457 break; 458 case TOP_BORDER: 459 result.write(ROW_BORDER_TOP); 460 break; 461 case RIGHT_BORDER: 462 result.write(ROW_BORDER_RIGHT); 463 break; 464 case BOTTOM_BORDER: 465 result.write(ROW_BORDER_BOTTOM); 466 break; 467 case HORIZONTAL_BORDER: 468 result.write(ROW_BORDER_HORIZONTAL); 469 break; 470 case VERTICAL_BORDER: 471 result.write(ROW_BORDER_VERTICAL); 472 break; 473 default: 474 return; 475 } 476 result.write(writeBorderStyle()); 477 result.write(BORDER_WIDTH); 478 result.write(intToByteArray(this.borderWidth)); 479 result.write(BORDER_COLOR_NUMBER); 480 result.write(intToByteArray(this.borderColor.getColorNumber())); 481 result.write('\n'); 482 } else if(this.borderType == CELL_BORDER) { 483 switch(this.borderPosition) { 484 case LEFT_BORDER: 485 result.write(CELL_BORDER_LEFT); 486 break; 487 case TOP_BORDER: 488 result.write(CELL_BORDER_TOP); 489 break; 490 case RIGHT_BORDER: 491 result.write(CELL_BORDER_RIGHT); 492 break; 493 case BOTTOM_BORDER: 494 result.write(CELL_BORDER_BOTTOM); 495 break; 496 default: 497 return; 498 } 499 result.write(writeBorderStyle()); 500 result.write(BORDER_WIDTH); 501 result.write(intToByteArray(this.borderWidth)); 502 result.write(BORDER_COLOR_NUMBER); 503 result.write(intToByteArray(this.borderColor.getColorNumber())); 504 result.write('\n'); 505 } 506 } 507 508 513 private byte[] writeBorderStyle() { 514 switch(this.borderStyle) { 515 case BORDER_NONE : return new byte[0]; 516 case BORDER_SINGLE : return BORDER_STYLE_SINGLE; 517 case BORDER_DOUBLE_THICK : return BORDER_STYLE_DOUBLE_THICK; 518 case BORDER_SHADOWED : return BORDER_STYLE_SHADOWED; 519 case BORDER_DOTTED : return BORDER_STYLE_DOTTED; 520 case BORDER_DASHED : return BORDER_STYLE_DASHED; 521 case BORDER_HAIRLINE : return BORDER_STYLE_HAIRLINE; 522 case BORDER_DOUBLE : return BORDER_STYLE_DOUBLE; 523 case BORDER_DOT_DASH : return BORDER_STYLE_DOT_DASH; 524 case BORDER_DOT_DOT_DASH : return BORDER_STYLE_DOT_DOT_DASH; 525 case BORDER_TRIPLE : return BORDER_STYLE_TRIPLE; 526 case BORDER_THICK_THIN : return BORDER_STYLE_THICK_THIN; 527 case BORDER_THIN_THICK : return BORDER_STYLE_THIN_THICK; 528 case BORDER_THIN_THICK_THIN : return BORDER_STYLE_THIN_THICK_THIN; 529 case BORDER_THICK_THIN_MED : return BORDER_STYLE_THICK_THIN_MED; 530 case BORDER_THIN_THICK_MED : return BORDER_STYLE_THIN_THICK_MED; 531 case BORDER_THIN_THICK_THIN_MED : return BORDER_STYLE_THIN_THICK_THIN_MED; 532 case BORDER_THICK_THIN_LARGE : return BORDER_STYLE_THICK_THIN_LARGE; 533 case BORDER_THIN_THICK_LARGE : return BORDER_STYLE_THIN_THICK_LARGE; 534 case BORDER_THIN_THICK_THIN_LARGE : return BORDER_STYLE_THIN_THICK_THIN_LARGE; 535 case BORDER_WAVY : return BORDER_STYLE_WAVY; 536 case BORDER_DOUBLE_WAVY : return BORDER_STYLE_DOUBLE_WAVY; 537 case BORDER_STRIPED : return BORDER_STYLE_STRIPED; 538 case BORDER_EMBOSS : return BORDER_STYLE_EMBOSS; 539 case BORDER_ENGRAVE : return BORDER_STYLE_ENGRAVE; 540 default : return BORDER_STYLE_SINGLE; 541 } 542 } 543 544 549 protected RtfColor getBorderColor() { 550 return borderColor; 551 } 552 553 557 protected int getBorderPosition() { 558 return borderPosition; 559 } 560 561 566 protected int getBorderStyle() { 567 return borderStyle; 568 } 569 570 575 protected int getBorderType() { 576 return borderType; 577 } 578 579 584 protected int getBorderWidth() { 585 return borderWidth; 586 } 587 } 588 | Popular Tags |