1 50 51 package com.lowagie.text; 52 53 import java.awt.Color ; 54 import java.util.ArrayList ; 55 56 import com.lowagie.text.pdf.GrayColor; 57 58 72 73 public class Rectangle implements Element { 74 75 77 78 public static final int UNDEFINED = -1; 79 80 81 public static final int TOP = 1; 82 83 84 public static final int BOTTOM = 2; 85 86 87 public static final int LEFT = 4; 88 89 90 public static final int RIGHT = 8; 91 92 93 public static final int NO_BORDER = 0; 94 95 96 public static final int BOX = TOP + BOTTOM + LEFT + RIGHT; 97 98 100 101 protected float llx; 102 103 104 protected float lly; 105 106 107 protected float urx; 108 109 110 protected float ury; 111 112 113 protected int rotation = 0; 114 115 116 protected int border = UNDEFINED; 117 118 119 protected float borderWidth = UNDEFINED; 120 121 122 protected Color borderColor = null; 123 124 125 protected Color backgroundColor = null; 126 127 128 protected boolean useVariableBorders = false; 129 130 131 protected float borderWidthLeft = UNDEFINED; 132 133 134 protected float borderWidthRight = UNDEFINED; 135 136 137 protected float borderWidthTop = UNDEFINED; 138 139 140 protected float borderWidthBottom = UNDEFINED; 141 142 143 protected Color borderColorLeft = null; 144 145 146 protected Color borderColorRight = null; 147 148 149 protected Color borderColorTop = null; 150 151 152 protected Color borderColorBottom = null; 153 154 156 168 public Rectangle(float llx, float lly, float urx, float ury) { 169 this.llx = llx; 170 this.lly = lly; 171 this.urx = urx; 172 this.ury = ury; 173 } 174 175 184 public Rectangle(float urx, float ury) { 185 this(0, 0, urx, ury); 186 } 187 188 194 public Rectangle(Rectangle rect) { 195 this(rect.llx, rect.lly, rect.urx, rect.ury); 196 cloneNonPositionParameters(rect); 197 } 198 199 201 209 public boolean process(ElementListener listener) { 210 try { 211 return listener.add(this); 212 } catch (DocumentException de) { 213 return false; 214 } 215 } 216 217 222 public int type() { 223 return Element.RECTANGLE; 224 } 225 226 231 public ArrayList getChunks() { 232 return new ArrayList (); 233 } 234 235 237 243 public void setLeft(float value) { 244 llx = value; 245 } 246 247 252 public float getLeft() { 253 return llx; 254 } 255 256 263 public float getLeft(float margin) { 264 return llx + margin; 265 } 266 267 273 274 public void setRight(float value) { 275 urx = value; 276 } 277 278 283 public float getRight() { 284 return urx; 285 } 286 287 294 public float getRight(float margin) { 295 return urx - margin; 296 } 297 298 303 public float getWidth() { 304 return urx - llx; 305 } 306 307 313 public void setTop(float value) { 314 ury = value; 315 } 316 317 322 public float getTop() { 323 return ury; 324 } 325 326 333 public float getTop(float margin) { 334 return ury - margin; 335 } 336 337 343 public void setBottom(float value) { 344 lly = value; 345 } 346 347 352 public float getBottom() { 353 return lly; 354 } 355 356 363 public float getBottom(float margin) { 364 return lly + margin; 365 } 366 367 372 public float getHeight() { 373 return ury - lly; 374 } 375 376 379 public void normalize() { 380 if (llx > urx) { 381 float a = llx; 382 llx = urx; 383 urx = a; 384 } 385 if (lly > ury) { 386 float a = lly; 387 lly = ury; 388 ury = a; 389 } 390 } 391 392 394 399 public int getRotation() { 400 return rotation; 401 } 402 403 409 public Rectangle rotate() { 410 Rectangle rect = new Rectangle(lly, llx, ury, urx); 411 rect.rotation = rotation + 90; 412 rect.rotation %= 360; 413 return rect; 414 } 415 416 418 423 public int getBorder() { 424 return border; 425 } 426 427 432 public boolean hasBorders() { 433 return (border > 0) 434 && ((borderWidth > 0) || (borderWidthLeft > 0) 435 || (borderWidthRight > 0) || (borderWidthTop > 0) || (borderWidthBottom > 0)); 436 } 437 438 445 public boolean hasBorder(int type) { 446 return border != UNDEFINED && (border & type) == type; 447 } 448 449 459 public void setBorder(int value) { 460 border = value; 461 } 462 463 470 public void enableBorderSide(int side) { 471 if (border == UNDEFINED) { 472 border = 0; 473 } 474 border |= side; 475 } 476 477 484 public void disableBorderSide(int side) { 485 if (border == UNDEFINED) { 486 border = 0; 487 } 488 border &= ~side; 489 } 490 491 493 498 public float getBorderWidth() { 499 return borderWidth; 500 } 501 502 508 509 public void setBorderWidth(float value) { 510 borderWidth = value; 511 } 512 513 515 520 521 public Color getBorderColor() { 522 return borderColor; 523 } 524 525 531 532 public void setBorderColor(Color value) { 533 borderColor = value; 534 } 535 536 538 543 public Color getBackgroundColor() { 544 return backgroundColor; 545 } 546 547 553 554 public void setBackgroundColor(Color value) { 555 backgroundColor = value; 556 } 557 558 563 564 public float getGrayFill() { 565 if (backgroundColor instanceof GrayColor) 566 return ((GrayColor)backgroundColor).getGray(); 567 else 568 return 0; 569 } 570 571 577 public void setGrayFill(float value) { 578 backgroundColor = new GrayColor(value); 579 } 580 581 583 591 public boolean isUseVariableBorders() { 592 return useVariableBorders; 593 } 594 595 601 public void setUseVariableBorders(boolean useVariableBorders) { 602 this.useVariableBorders = useVariableBorders; 603 } 604 605 607 608 private float getVariableBorderWidth(float variableWidthValue, int side) { 609 if ((border & side) != 0) { 610 return variableWidthValue != UNDEFINED ? variableWidthValue 611 : borderWidth; 612 } else { 613 return 0; 614 } 615 } 616 617 626 private void updateBorderBasedOnWidth(float width, int side) { 627 useVariableBorders = true; 628 if (width > 0) { 629 enableBorderSide(side); 630 } else { 631 disableBorderSide(side); 632 } 633 } 634 635 640 public float getBorderWidthLeft() { 641 return getVariableBorderWidth(borderWidthLeft, LEFT); 642 } 643 644 650 public void setBorderWidthLeft(float borderWidthLeft) { 651 this.borderWidthLeft = borderWidthLeft; 652 updateBorderBasedOnWidth(borderWidthLeft, LEFT); 653 } 654 655 660 public float getBorderWidthRight() { 661 return getVariableBorderWidth(borderWidthRight, RIGHT); 662 } 663 664 670 public void setBorderWidthRight(float borderWidthRight) { 671 this.borderWidthRight = borderWidthRight; 672 updateBorderBasedOnWidth(borderWidthRight, RIGHT); 673 } 674 675 680 public float getBorderWidthTop() { 681 return getVariableBorderWidth(borderWidthTop, TOP); 682 } 683 684 690 public void setBorderWidthTop(float borderWidthTop) { 691 this.borderWidthTop = borderWidthTop; 692 updateBorderBasedOnWidth(borderWidthTop, TOP); 693 } 694 695 700 public float getBorderWidthBottom() { 701 return getVariableBorderWidth(borderWidthBottom, BOTTOM); 702 } 703 704 710 public void setBorderWidthBottom(float borderWidthBottom) { 711 this.borderWidthBottom = borderWidthBottom; 712 updateBorderBasedOnWidth(borderWidthBottom, BOTTOM); 713 } 714 715 717 722 public Color getBorderColorLeft() { 723 if (borderColorLeft == null) return borderColor; 724 return borderColorLeft; 725 } 726 727 733 public void setBorderColorLeft(Color value) { 734 borderColorLeft = value; 735 } 736 737 742 public Color getBorderColorRight() { 743 if (borderColorRight == null) return borderColor; 744 return borderColorRight; 745 } 746 747 753 public void setBorderColorRight(Color value) { 754 borderColorRight = value; 755 } 756 757 762 public Color getBorderColorTop() { 763 if (borderColorTop == null) return borderColor; 764 return borderColorTop; 765 } 766 767 773 public void setBorderColorTop(Color value) { 774 borderColorTop = value; 775 } 776 777 782 public Color getBorderColorBottom() { 783 if (borderColorBottom == null) return borderColor; 784 return borderColorBottom; 785 } 786 787 793 public void setBorderColorBottom(Color value) { 794 borderColorBottom = value; 795 } 796 797 799 808 public Rectangle rectangle(float top, float bottom) { 809 Rectangle tmp = new Rectangle(this); 810 if (getTop() > top) { 811 tmp.setTop(top); 812 tmp.disableBorderSide(TOP); 813 } 814 if (getBottom() < bottom) { 815 tmp.setBottom(bottom); 816 tmp.disableBorderSide(BOTTOM); 817 } 818 return tmp; 819 } 820 821 828 public void cloneNonPositionParameters(Rectangle rect) { 829 this.rotation = rect.rotation; 830 this.border = rect.border; 831 this.borderWidth = rect.borderWidth; 832 this.borderColor = rect.borderColor; 833 this.backgroundColor = rect.backgroundColor; 834 this.useVariableBorders = rect.useVariableBorders; 835 this.borderWidthLeft = rect.borderWidthLeft; 836 this.borderWidthRight = rect.borderWidthRight; 837 this.borderWidthTop = rect.borderWidthTop; 838 this.borderWidthBottom = rect.borderWidthBottom; 839 this.borderColorLeft = rect.borderColorLeft; 840 this.borderColorRight = rect.borderColorRight; 841 this.borderColorTop = rect.borderColorTop; 842 this.borderColorBottom = rect.borderColorBottom; 843 } 844 845 852 public void softCloneNonPositionParameters(Rectangle rect) { 853 if (rect.rotation != 0) 854 this.rotation = rect.rotation; 855 if (rect.border != UNDEFINED) 856 this.border = rect.border; 857 if (rect.borderWidth != UNDEFINED) 858 this.borderWidth = rect.borderWidth; 859 if (rect.borderColor != null) 860 this.borderColor = rect.borderColor; 861 if (rect.backgroundColor != null) 862 this.backgroundColor = rect.backgroundColor; 863 if (useVariableBorders) 864 this.useVariableBorders = rect.useVariableBorders; 865 if (rect.borderWidthLeft != UNDEFINED) 866 this.borderWidthLeft = rect.borderWidthLeft; 867 if (rect.borderWidthRight != UNDEFINED) 868 this.borderWidthRight = rect.borderWidthRight; 869 if (rect.borderWidthTop != UNDEFINED) 870 this.borderWidthTop = rect.borderWidthTop; 871 if (rect.borderWidthBottom != UNDEFINED) 872 this.borderWidthBottom = rect.borderWidthBottom; 873 if (rect.borderColorLeft != null) 874 this.borderColorLeft = rect.borderColorLeft; 875 if (rect.borderColorRight != null) 876 this.borderColorRight = rect.borderColorRight; 877 if (rect.borderColorTop != null) 878 this.borderColorTop = rect.borderColorTop; 879 if (rect.borderColorBottom != null) 880 this.borderColorBottom = rect.borderColorBottom; 881 } 882 883 886 public String toString() { 887 StringBuffer buf = new StringBuffer ("Rectangle: "); 888 buf.append(getWidth()); 889 buf.append('x'); 890 buf.append(getHeight()); 891 buf.append(" (rot: "); 892 buf.append(rotation); 893 buf.append(" degrees)"); 894 return buf.toString(); 895 } 896 897 899 905 public float left() { 906 return getLeft(); 907 } 908 909 915 public float right() { 916 return getRight(); 917 } 918 919 925 public float top() { 926 return getTop(); 927 } 928 929 935 public float bottom() { 936 return getBottom(); 937 } 938 939 947 public float left(float margin) { 948 return getLeft(margin); 949 } 950 951 959 public float right(float margin) { 960 return getRight(margin); 961 } 962 963 969 public float width() { 970 return getWidth(); 971 } 972 973 981 public float top(float margin) { 982 return getTop(margin); 983 } 984 985 993 public float bottom(float margin) { 994 return getBottom(margin); 995 } 996 997 1003 public float height() { 1004 return getHeight(); 1005 } 1006 1007 1013 public int border() { 1014 return getBorder(); 1015 } 1016 1017 1023 public float borderWidth() { 1024 return getBorderWidth(); 1025 } 1026 1027 1033 1034 public Color borderColor() { 1035 return getBorderColor(); 1036 } 1037 1038 1044 public Color backgroundColor() { 1045 return getBackgroundColor(); 1046 } 1047 1048 1054 1055 public float grayFill() { 1056 return getGrayFill(); 1057 } 1058 1059} | Popular Tags |