1 50 51 package com.lowagie.text; 52 53 import java.awt.Color ; 54 import java.net.URL ; 55 import java.util.ArrayList ; 56 import java.util.HashMap ; 57 import java.util.Hashtable ; 58 import java.util.Set ; 59 60 import com.lowagie.text.pdf.HyphenationEvent; 61 import com.lowagie.text.pdf.PdfAction; 62 import com.lowagie.text.pdf.PdfAnnotation; 63 import com.lowagie.text.pdf.PdfContentByte; 64 65 86 87 public class Chunk implements Element { 88 89 91 92 public static final String OBJECT_REPLACEMENT_CHARACTER = "\ufffc"; 93 94 95 public static final Chunk NEWLINE = new Chunk("\n"); 96 97 98 public static final Chunk NEXTPAGE = new Chunk(""); 99 static { 100 NEXTPAGE.setNewPage(); 101 } 102 103 105 106 protected StringBuffer content = null; 107 108 109 protected Font font = null; 110 111 112 protected HashMap attributes = null; 113 114 116 119 public Chunk() { 120 this.content = new StringBuffer (); 121 this.font = new Font(); 122 } 123 124 128 public Chunk(Chunk ck) { 129 if (ck.content != null) { 130 content = new StringBuffer (ck.content.toString()); 131 } 132 if (ck.font != null) { 133 font = new Font(ck.font); 134 } 135 if (ck.attributes != null) { 136 attributes = new HashMap (ck.attributes); 137 } 138 } 139 140 149 public Chunk(String content, Font font) { 150 this.content = new StringBuffer (content); 151 this.font = font; 152 } 153 154 161 public Chunk(String content) { 162 this(content, new Font()); 163 } 164 165 173 public Chunk(char c, Font font) { 174 this.content = new StringBuffer (); 175 this.content.append(c); 176 this.font = font; 177 } 178 179 186 public Chunk(char c) { 187 this(c, new Font()); 188 } 189 190 200 public Chunk(Image image, float offsetX, float offsetY) { 201 this(OBJECT_REPLACEMENT_CHARACTER, new Font()); 202 Image copyImage = Image.getInstance(image); 203 copyImage.setAbsolutePosition(Float.NaN, Float.NaN); 204 setAttribute(IMAGE, new Object [] { copyImage, new Float (offsetX), 205 new Float (offsetY), Boolean.FALSE }); 206 } 207 208 220 public Chunk(Image image, float offsetX, float offsetY, 221 boolean changeLeading) { 222 this(OBJECT_REPLACEMENT_CHARACTER, new Font()); 223 setAttribute(IMAGE, new Object [] { image, new Float (offsetX), 224 new Float (offsetY), new Boolean (changeLeading) }); 225 } 226 227 229 237 public boolean process(ElementListener listener) { 238 try { 239 return listener.add(this); 240 } catch (DocumentException de) { 241 return false; 242 } 243 } 244 245 250 public int type() { 251 return Element.CHUNK; 252 } 253 254 259 public ArrayList getChunks() { 260 ArrayList tmp = new ArrayList (); 261 tmp.add(this); 262 return tmp; 263 } 264 265 267 274 public StringBuffer append(String string) { 275 return content.append(string); 276 } 277 278 284 public void setFont(Font font) { 285 this.font = font; 286 } 287 288 290 295 public Font getFont() { 296 return font; 297 } 298 299 304 public String getContent() { 305 return content.toString(); 306 } 307 308 313 public String toString() { 314 return getContent(); 315 } 316 317 323 public boolean isEmpty() { 324 return (content.toString().trim().length() == 0) 325 && (content.toString().indexOf("\n") == -1) 326 && (attributes == null); 327 } 328 329 334 public float getWidthPoint() { 335 if (getImage() != null) { 336 return getImage().getScaledWidth(); 337 } 338 return font.getCalculatedBaseFont(true).getWidthPoint(getContent(), 339 font.getCalculatedSize()) 340 * getHorizontalScaling(); 341 } 342 343 345 350 351 public boolean hasAttributes() { 352 return attributes != null; 353 } 354 355 362 363 public HashMap getAttributes() { 364 return attributes; 365 } 366 367 376 377 private Chunk setAttribute(String name, Object obj) { 378 if (attributes == null) 379 attributes = new HashMap (); 380 attributes.put(name, obj); 381 return this; 382 } 383 384 386 387 public static final String HSCALE = "HSCALE"; 388 389 397 public Chunk setHorizontalScaling(float scale) { 398 return setAttribute(HSCALE, new Float (scale)); 399 } 400 401 406 public float getHorizontalScaling() { 407 if (attributes == null) 408 return 1f; 409 Float f = (Float ) attributes.get(HSCALE); 410 if (f == null) 411 return 1f; 412 return f.floatValue(); 413 } 414 415 416 public static final String UNDERLINE = "UNDERLINE"; 417 418 430 public Chunk setUnderline(float thickness, float yPosition) { 431 return setUnderline(null, thickness, 0f, yPosition, 0f, 432 PdfContentByte.LINE_CAP_BUTT); 433 } 434 435 458 public Chunk setUnderline(Color color, float thickness, float thicknessMul, 459 float yPosition, float yPositionMul, int cap) { 460 if (attributes == null) 461 attributes = new HashMap (); 462 Object obj[] = { 463 color, 464 new float[] { thickness, thicknessMul, yPosition, yPositionMul, 465 (float) cap } }; 466 Object unders[][] = Utilities.addToArray((Object [][]) attributes.get(UNDERLINE), 467 obj); 468 return setAttribute(UNDERLINE, unders); 469 } 470 471 472 public static final String SUBSUPSCRIPT = "SUBSUPSCRIPT"; 473 474 484 485 public Chunk setTextRise(float rise) { 486 return setAttribute(SUBSUPSCRIPT, new Float (rise)); 487 } 488 489 494 public float getTextRise() { 495 if (attributes != null && attributes.containsKey(SUBSUPSCRIPT)) { 496 Float f = (Float ) attributes.get(SUBSUPSCRIPT); 497 return f.floatValue(); 498 } 499 return 0.0f; 500 } 501 502 503 public static final String SKEW = "SKEW"; 504 505 515 public Chunk setSkew(float alpha, float beta) { 516 alpha = (float) Math.tan(alpha * Math.PI / 180); 517 beta = (float) Math.tan(beta * Math.PI / 180); 518 return setAttribute(SKEW, new float[] { alpha, beta }); 519 } 520 521 522 public static final String BACKGROUND = "BACKGROUND"; 523 524 531 public Chunk setBackground(Color color) { 532 return setBackground(color, 0, 0, 0, 0); 533 } 534 535 550 public Chunk setBackground(Color color, float extraLeft, float extraBottom, 551 float extraRight, float extraTop) { 552 return setAttribute(BACKGROUND, new Object [] { color, 553 new float[] { extraLeft, extraBottom, extraRight, extraTop } }); 554 } 555 556 557 public static final String TEXTRENDERMODE = "TEXTRENDERMODE"; 558 559 578 public Chunk setTextRenderMode(int mode, float strokeWidth, 579 Color strokeColor) { 580 return setAttribute(TEXTRENDERMODE, new Object [] { new Integer (mode), 581 new Float (strokeWidth), strokeColor }); 582 } 583 584 585 public static final String SPLITCHARACTER = "SPLITCHARACTER"; 586 587 594 595 public Chunk setSplitCharacter(SplitCharacter splitCharacter) { 596 return setAttribute(SPLITCHARACTER, splitCharacter); 597 } 598 599 600 public static final String HYPHENATION = "HYPHENATION"; 601 602 609 public Chunk setHyphenation(HyphenationEvent hyphenation) { 610 return setAttribute(HYPHENATION, hyphenation); 611 } 612 613 614 public static final String REMOTEGOTO = "REMOTEGOTO"; 615 616 625 626 public Chunk setRemoteGoto(String filename, String name) { 627 return setAttribute(REMOTEGOTO, new Object [] { filename, name }); 628 } 629 630 639 640 public Chunk setRemoteGoto(String filename, int page) { 641 return setAttribute(REMOTEGOTO, new Object [] { filename, 642 new Integer (page) }); 643 } 644 645 646 public static final String LOCALGOTO = "LOCALGOTO"; 647 648 657 658 public Chunk setLocalGoto(String name) { 659 return setAttribute(LOCALGOTO, name); 660 } 661 662 663 public static final String LOCALDESTINATION = "LOCALDESTINATION"; 664 665 672 public Chunk setLocalDestination(String name) { 673 return setAttribute(LOCALDESTINATION, name); 674 } 675 676 677 public static final String GENERICTAG = "GENERICTAG"; 678 679 688 689 public Chunk setGenericTag(String text) { 690 return setAttribute(GENERICTAG, text); 691 } 692 693 694 public static final String IMAGE = "IMAGE"; 695 696 701 702 public Image getImage() { 703 if (attributes == null) 704 return null; 705 Object obj[] = (Object []) attributes.get(Chunk.IMAGE); 706 if (obj == null) 707 return null; 708 else { 709 return (Image) obj[0]; 710 } 711 } 712 713 714 public static final String ACTION = "ACTION"; 715 716 723 724 public Chunk setAction(PdfAction action) { 725 return setAttribute(ACTION, action); 726 } 727 728 735 736 public Chunk setAnchor(URL url) { 737 return setAttribute(ACTION, new PdfAction(url.toExternalForm())); 738 } 739 740 747 748 public Chunk setAnchor(String url) { 749 return setAttribute(ACTION, new PdfAction(url)); 750 } 751 752 753 public static final String NEWPAGE = "NEWPAGE"; 754 755 760 761 public Chunk setNewPage() { 762 return setAttribute(NEWPAGE, null); 763 } 764 765 766 public static final String PDFANNOTATION = "PDFANNOTATION"; 767 768 775 public Chunk setAnnotation(PdfAnnotation annotation) { 776 return setAttribute(PDFANNOTATION, annotation); 777 } 778 779 781 782 public static final String COLOR = "COLOR"; 783 784 785 public static final String ENCODING = "ENCODING"; 786 787 789 797 798 public Chunk(java.util.Properties attributes) { 799 this(com.lowagie.text.factories.ElementFactory.getChunk(attributes)); 800 } 801 802 808 public String content() { 809 return getContent(); 810 } 811 812 818 819 public Font font() { 820 return getFont(); 821 } 822 823 831 public static Set getKeySet(Hashtable table) { 832 return Utilities.getKeySet(table); 833 } 834 835 845 public static Object [][] addToArray(Object original[][], Object item[]) { 846 return Utilities.addToArray(original, item); 847 } 848 } | Popular Tags |