1 50 51 package com.lowagie.text.rtf; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.IOException ; 56 import java.util.Iterator ; 57 58 import com.lowagie.text.Cell; 59 import com.lowagie.text.DocumentException; 60 import com.lowagie.text.Element; 61 import com.lowagie.text.Paragraph; 62 import com.lowagie.text.Rectangle; 63 64 76 public class RtfCell { 77 78 79 80 private static final int MERGE_HORIZ_FIRST = 1; 81 82 private static final int MERGE_VERT_FIRST = 2; 83 84 private static final int MERGE_BOTH_FIRST = 3; 85 86 private static final int MERGE_HORIZ_PREV = 4; 87 88 private static final int MERGE_VERT_PREV = 5; 89 90 private static final int MERGE_BOTH_PREV = 6; 91 92 95 96 97 private static final byte[] cellMergeFirst = "clmgf".getBytes(); 98 99 private static final byte[] cellVMergeFirst = "clvmgf".getBytes(); 100 101 private static final byte[] cellMergePrev = "clmrg".getBytes(); 102 103 private static final byte[] cellVMergePrev = "clvmrg".getBytes(); 104 105 private static final byte[] cellVerticalAlignBottom = "clvertalb".getBytes(); 106 107 private static final byte[] cellVerticalAlignCenter = "clvertalc".getBytes(); 108 109 private static final byte[] cellVerticalAlignTop = "clvertalt".getBytes(); 110 111 private static final byte[] cellBorderLeft = "clbrdrl".getBytes(); 112 113 private static final byte[] cellBorderRight = "clbrdrr".getBytes(); 114 115 private static final byte[] cellBorderTop = "clbrdrt".getBytes(); 116 117 private static final byte[] cellBorderBottom = "clbrdrb".getBytes(); 118 119 private static final byte[] cellBackgroundColor = "clcbpat".getBytes(); 120 121 private static final byte[] cellWidthStyle = "clftsWidth3".getBytes(); 122 123 private static final byte[] cellWidthTag = "clwWidth".getBytes(); 124 125 private static final byte[] cellRightBorder = "cellx".getBytes(); 126 127 protected static final byte[] cellInTable = "intbl".getBytes(); 128 129 private static final byte[] cellEnd = "cell".getBytes(); 130 131 132 private static final byte[] cellPaddingTop = "clpadt".getBytes(); 133 134 private static final byte[] cellPaddingTopUnit = "clpadft3".getBytes(); 135 136 private static final byte[] cellPaddingBottom = "clpadb".getBytes(); 137 138 private static final byte[] cellPaddingBottomUnit = "clpadfb3".getBytes(); 139 140 private static final byte[] cellPaddingLeft = "clpadl".getBytes(); 141 142 private static final byte[] cellPaddingLeftUnit = "clpadfl3".getBytes(); 143 144 private static final byte[] cellPaddingRight = "clpadr".getBytes(); 145 146 private static final byte[] cellPaddingRightUnit = "clpadfr3".getBytes(); 147 148 149 private RtfWriter writer = null; 150 151 private RtfTable mainTable = null; 152 153 154 private int cellWidth = 0; 155 156 private int cellRight = 0; 157 158 private Cell store = null; 159 160 private boolean emptyCell = true; 161 162 private int mergeType = 0; 163 166 private int cellpadding = 0; 167 168 175 public RtfCell(RtfWriter writer, RtfTable mainTable) { 176 super(); 177 this.writer = writer; 178 this.mainTable = mainTable; 179 } 180 181 193 public int importCell(Cell cell, int cellLeft, int cellWidth, int x, int y, int cellpadding) { 194 this.cellpadding = cellpadding; 195 196 this.cellWidth = cellWidth; 198 if (cell == null) { 199 cellRight = cellLeft + cellWidth; 200 return cellRight; 201 } 202 if (cell.getWidth() != 0) { 203 this.cellWidth = (int) (cell.getWidth() * RtfWriter.TWIPSFACTOR); 204 } 205 cellRight = cellLeft + this.cellWidth; 206 store = cell; 207 emptyCell = false; 208 if (cell.getColspan() > 1) { 209 if (cell.getRowspan() > 1) { 210 mergeType = MERGE_BOTH_FIRST; 211 for (int i = y; i < y + cell.getRowspan(); i++) { 212 if (i > y) mainTable.setMerge(x, i, MERGE_VERT_PREV, this); 213 for (int j = x + 1; j < x + cell.getColspan(); j++) { 214 mainTable.setMerge(j, i, MERGE_BOTH_PREV, this); 215 } 216 } 217 } else { 218 mergeType = MERGE_HORIZ_FIRST; 219 for (int i = x + 1; i < x + cell.getColspan(); i++) { 220 mainTable.setMerge(i, y, MERGE_HORIZ_PREV, this); 221 } 222 } 223 } else if (cell.getRowspan() > 1) { 224 mergeType = MERGE_VERT_FIRST; 225 for (int i = y + 1; i < y + cell.getRowspan(); i++) { 226 mainTable.setMerge(x, i, MERGE_VERT_PREV, this); 227 } 228 } 229 return cellRight; 230 } 231 232 240 public boolean writeCellSettings(ByteArrayOutputStream os) { 241 try { 242 float lWidth, tWidth, rWidth, bWidth; 243 byte[] lStyle, tStyle, rStyle, bStyle; 244 245 if (store instanceof RtfTableCell) { 246 RtfTableCell c = (RtfTableCell) store; 247 lWidth = c.leftBorderWidth(); 248 tWidth = c.topBorderWidth(); 249 rWidth = c.rightBorderWidth(); 250 bWidth = c.bottomBorderWidth(); 251 lStyle = RtfTableCell.getStyleControlWord(c.leftBorderStyle()); 252 tStyle = RtfTableCell.getStyleControlWord(c.topBorderStyle()); 253 rStyle = RtfTableCell.getStyleControlWord(c.rightBorderStyle()); 254 bStyle = RtfTableCell.getStyleControlWord(c.bottomBorderStyle()); 255 } else { 256 lWidth = tWidth = rWidth = bWidth = store.getBorderWidth(); 257 lStyle = tStyle = rStyle = bStyle = RtfRow.tableBorder; 258 } 259 260 if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV) { 261 return true; 262 } 263 switch (mergeType) { 264 case MERGE_VERT_FIRST: 265 os.write(RtfWriter.escape); 266 os.write(cellVMergeFirst); 267 break; 268 case MERGE_BOTH_FIRST: 269 os.write(RtfWriter.escape); 270 os.write(cellVMergeFirst); 271 break; 272 case MERGE_HORIZ_PREV: 273 os.write(RtfWriter.escape); 274 os.write(cellMergePrev); 275 break; 276 case MERGE_VERT_PREV: 277 os.write(RtfWriter.escape); 278 os.write(cellVMergePrev); 279 break; 280 case MERGE_BOTH_PREV: 281 os.write(RtfWriter.escape); 282 os.write(cellMergeFirst); 283 break; 284 } 285 switch (store.getVerticalAlignment()) { 286 case Element.ALIGN_BOTTOM: 287 os.write(RtfWriter.escape); 288 os.write(cellVerticalAlignBottom); 289 break; 290 case Element.ALIGN_CENTER: 291 case Element.ALIGN_MIDDLE: 292 os.write(RtfWriter.escape); 293 os.write(cellVerticalAlignCenter); 294 break; 295 case Element.ALIGN_TOP: 296 os.write(RtfWriter.escape); 297 os.write(cellVerticalAlignTop); 298 break; 299 } 300 301 if (((store.getBorder() & Rectangle.LEFT) == Rectangle.LEFT) && 302 (lWidth > 0)) { 303 os.write(RtfWriter.escape); 304 os.write(cellBorderLeft); 305 os.write(RtfWriter.escape); 306 os.write(lStyle); 307 os.write(RtfWriter.escape); 308 os.write(RtfRow.tableBorderWidth); 309 writeInt(os, (int) (lWidth * RtfWriter.TWIPSFACTOR)); 310 os.write(RtfWriter.escape); 311 os.write(RtfRow.tableBorderColor); 312 if (store.getBorderColor() == null) 313 writeInt(os, writer.addColor(new 314 Color (0, 0, 0))); 315 else 316 writeInt(os, writer.addColor(store.getBorderColor())); 317 os.write((byte) '\n'); 318 } 319 if (((store.getBorder() & Rectangle.TOP) == Rectangle.TOP) && (tWidth > 0)) { 320 os.write(RtfWriter.escape); 321 os.write(cellBorderTop); 322 os.write(RtfWriter.escape); 323 os.write(tStyle); 324 os.write(RtfWriter.escape); 325 os.write(RtfRow.tableBorderWidth); 326 writeInt(os, (int) (tWidth * RtfWriter.TWIPSFACTOR)); 327 os.write(RtfWriter.escape); 328 os.write(RtfRow.tableBorderColor); 329 if (store.getBorderColor() == null) 330 writeInt(os, writer.addColor(new 331 Color (0, 0, 0))); 332 else 333 writeInt(os, writer.addColor(store.getBorderColor())); 334 os.write((byte) '\n'); 335 } 336 if (((store.getBorder() & Rectangle.BOTTOM) == Rectangle.BOTTOM) && 337 (bWidth > 0)) { 338 os.write(RtfWriter.escape); 339 os.write(cellBorderBottom); 340 os.write(RtfWriter.escape); 341 os.write(bStyle); 342 os.write(RtfWriter.escape); 343 os.write(RtfRow.tableBorderWidth); 344 writeInt(os, (int) (bWidth * RtfWriter.TWIPSFACTOR)); 345 os.write(RtfWriter.escape); 346 os.write(RtfRow.tableBorderColor); 347 if (store.getBorderColor() == null) 348 writeInt(os, writer.addColor(new 349 Color (0, 0, 0))); 350 else 351 writeInt(os, writer.addColor(store.getBorderColor())); 352 os.write((byte) '\n'); 353 } 354 if (((store.getBorder() & Rectangle.RIGHT) == Rectangle.RIGHT) && 355 (rWidth > 0)) { 356 os.write(RtfWriter.escape); 357 os.write(cellBorderRight); 358 os.write(RtfWriter.escape); 359 os.write(rStyle); 360 os.write(RtfWriter.escape); 361 os.write(RtfRow.tableBorderWidth); 362 writeInt(os, (int) (rWidth * RtfWriter.TWIPSFACTOR)); 363 os.write(RtfWriter.escape); 364 os.write(RtfRow.tableBorderColor); 365 if (store.getBorderColor() == null) 366 writeInt(os, writer.addColor(new 367 Color (0, 0, 0))); 368 else 369 writeInt(os, writer.addColor(store.getBorderColor())); 370 os.write((byte) '\n'); 371 } 372 os.write(RtfWriter.escape); 373 os.write(cellBackgroundColor); 374 if (store.getBackgroundColor() == null) { 375 writeInt(os, writer.addColor(new Color (255, 255, 255))); 376 } else { 377 writeInt(os, writer.addColor(store.getBackgroundColor())); 378 } 379 os.write((byte) '\n'); 380 os.write(RtfWriter.escape); 381 os.write(cellWidthStyle); 382 os.write((byte) '\n'); 383 os.write(RtfWriter.escape); 384 os.write(cellWidthTag); 385 writeInt(os, cellWidth); 386 os.write((byte) '\n'); 387 if (cellpadding > 0) { 388 os.write(RtfWriter.escape); 390 os.write(cellPaddingLeft); 391 writeInt(os, cellpadding / 2); 392 os.write(RtfWriter.escape); 393 os.write(cellPaddingTop); 394 writeInt(os, cellpadding / 2); 395 os.write(RtfWriter.escape); 396 os.write(cellPaddingRight); 397 writeInt(os, cellpadding / 2); 398 os.write(RtfWriter.escape); 399 os.write(cellPaddingBottom); 400 writeInt(os, cellpadding / 2); 401 os.write(RtfWriter.escape); 403 os.write(cellPaddingLeftUnit); 404 os.write(RtfWriter.escape); 405 os.write(cellPaddingTopUnit); 406 os.write(RtfWriter.escape); 407 os.write(cellPaddingRightUnit); 408 os.write(RtfWriter.escape); 409 os.write(cellPaddingBottomUnit); 410 } 411 os.write(RtfWriter.escape); 412 os.write(cellRightBorder); 413 writeInt(os, cellRight); 414 } catch (IOException e) { 415 return false; 416 } 417 return true; 418 } 419 420 428 public boolean writeCellContent(ByteArrayOutputStream os) throws DocumentException { 429 try { 430 if (mergeType == MERGE_HORIZ_PREV || mergeType == MERGE_BOTH_PREV) { 431 return true; 432 } 433 434 if (!emptyCell) { 435 Iterator cellIterator = store.getElements(); 436 Paragraph container = null; 437 while (cellIterator.hasNext()) { 438 Element element = (Element) cellIterator.next(); 439 if(!(element instanceof Paragraph)) { 441 if(container != null) { 442 container.add(element); 443 } else { 444 container = new Paragraph(); 445 container.setAlignment(store.getHorizontalAlignment()); 446 container.add(element); 447 } 448 } else { 449 if(container != null) { 450 writer.addElement(container, os); 451 container = null; 452 } 453 454 455 if (element instanceof Paragraph && ((Paragraph) element).getAlignment() == Element.ALIGN_UNDEFINED) { 458 ((Paragraph) element).setAlignment(store.getHorizontalAlignment()); 459 } 460 writer.addElement(element, os); 461 if (element.type() == Element.PARAGRAPH && cellIterator.hasNext()) { 462 os.write(RtfWriter.escape); 463 os.write(RtfWriter.paragraph); 464 } 465 } 466 } 467 if(container != null) { 468 writer.addElement(container, os); 469 container =null; 470 } 471 } else { 472 os.write(RtfWriter.escape); 473 os.write(RtfWriter.paragraphDefaults); 474 os.write(RtfWriter.escape); 475 os.write(cellInTable); 476 } 477 os.write(RtfWriter.escape); 478 os.write(cellEnd); 479 } catch (IOException e) { 480 return false; 481 } 482 return true; 483 } 484 485 494 public void setMerge(int mergeType, RtfCell mergeCell) { 495 this.mergeType = mergeType; 496 store = mergeCell.getStore(); 497 } 498 499 504 public Cell getStore() { 505 return store; 506 } 507 508 513 public int getCellWidth() { 514 return cellWidth; 515 } 516 517 521 public void setCellWidth(int value) { 522 cellWidth = value; 523 } 524 525 529 public int getCellRight() { 530 return cellRight; 531 } 532 533 534 538 public void setCellRight(int value) { 539 cellRight = value; 540 } 541 542 549 private void writeInt(ByteArrayOutputStream out, int i) throws IOException { 550 out.write(Integer.toString(i).getBytes()); 551 } 552 } 553 | Popular Tags |