1 28 package net.sf.jasperreports.engine.fill; 29 30 import java.awt.Color ; 31 import java.awt.font.TextAttribute ; 32 import java.text.AttributedString ; 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 import net.sf.jasperreports.engine.JRBox; 37 import net.sf.jasperreports.engine.JRElement; 38 import net.sf.jasperreports.engine.JRFont; 39 import net.sf.jasperreports.engine.JRPrintText; 40 import net.sf.jasperreports.engine.JRReportFont; 41 import net.sf.jasperreports.engine.JRStyle; 42 import net.sf.jasperreports.engine.JRTextElement; 43 import net.sf.jasperreports.engine.util.JRFontUtil; 44 import net.sf.jasperreports.engine.util.JRStyleResolver; 45 import net.sf.jasperreports.engine.util.JRStyledText; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 import org.xml.sax.SAXException ; 50 51 52 56 public abstract class JRFillTextElement extends JRFillElement implements JRTextElement 57 { 58 59 60 63 private static final Log log = LogFactory.getLog(JRFillTextElement.class); 64 65 68 private boolean isLeftToRight = true; 69 private TextMeasurer textMeasurer = null; 70 private float lineSpacingFactor = 0; 71 private float leadingOffset = 0; 72 private float textHeight = 0; 73 private int textStart = 0; 74 private int textEnd = 0; 75 private String rawText = null; 76 private JRStyledText styledText = null; 77 private Map styledTextAttributesMap = new HashMap (); 78 79 protected TextChopper textChopper = null; 80 81 protected final JRReportFont reportFont; 82 83 86 protected JRFillTextElement( 87 JRBaseFiller filler, 88 JRTextElement textElement, 89 JRFillObjectFactory factory 90 ) 91 { 92 super(filler, textElement, factory); 93 94 reportFont = factory.getReportFont(textElement.getReportFont()); 95 96 97 createTextMeasurer(); 98 createTextChopper(); 99 } 100 101 102 protected JRFillTextElement(JRFillTextElement textElement, JRFillCloneFactory factory) 103 { 104 super(textElement, factory); 105 106 reportFont = textElement.reportFont; 107 108 createTextMeasurer(); 109 createTextChopper(); 110 } 111 112 113 private void createTextMeasurer() 114 { 115 textMeasurer = new TextMeasurer(this); 116 } 117 118 119 private void createTextChopper() 120 { 121 textChopper = isStyledText() ? styledTextChopper : simpleTextChopper; 122 } 123 124 125 128 public byte getMode() 129 { 130 return JRStyleResolver.getMode(this, JRElement.MODE_TRANSPARENT); 131 } 132 133 136 public byte getTextAlignment() 137 { 138 return JRStyleResolver.getHorizontalAlignment(this); 139 } 140 141 144 public void setTextAlignment(byte horizontalAlignment) 145 { 146 } 147 148 151 public byte getHorizontalAlignment() 152 { 153 return JRStyleResolver.getHorizontalAlignment(this); 154 } 155 156 public Byte getOwnHorizontalAlignment() 157 { 158 return ((JRTextElement)parent).getOwnHorizontalAlignment(); 159 } 160 161 164 public void setHorizontalAlignment(byte horizontalAlignment) 165 { 166 } 167 168 171 public void setHorizontalAlignment(Byte horizontalAlignment) 172 { 173 } 174 175 178 public byte getVerticalAlignment() 179 { 180 return JRStyleResolver.getVerticalAlignment(this); 181 } 182 183 public Byte getOwnVerticalAlignment() 184 { 185 return ((JRTextElement)parent).getOwnVerticalAlignment(); 186 } 187 188 191 public void setVerticalAlignment(byte verticalAlignment) 192 { 193 } 194 195 198 public void setVerticalAlignment(Byte verticalAlignment) 199 { 200 } 201 202 205 public byte getRotation() 206 { 207 return JRStyleResolver.getRotation(this); 208 } 209 210 public Byte getOwnRotation() 211 { 212 return ((JRTextElement)parent).getOwnRotation(); 213 } 214 215 218 public void setRotation(byte rotation) 219 { 220 } 221 222 225 public void setRotation(Byte rotation) 226 { 227 } 228 229 232 public byte getLineSpacing() 233 { 234 return JRStyleResolver.getLineSpacing(this); 235 } 236 237 public Byte getOwnLineSpacing() 238 { 239 return ((JRTextElement)parent).getOwnLineSpacing(); 240 } 241 242 245 public void setLineSpacing(byte lineSpacing) 246 { 247 } 248 249 252 public void setLineSpacing(Byte lineSpacing) 253 { 254 } 255 256 259 public boolean isStyledText() 260 { 261 return JRStyleResolver.isStyledText(this); 262 } 263 264 public Boolean isOwnStyledText() 265 { 266 return ((JRTextElement)parent).isOwnStyledText(); 267 } 268 269 272 public void setStyledText(boolean isStyledText) 273 { 274 } 275 276 279 public void setStyledText(Boolean isStyledText) 280 { 281 } 282 283 286 public JRBox getBox() 287 { 288 return this; 289 } 290 291 294 public JRFont getFont() 295 { 296 return this; 297 } 298 299 300 303 protected Map getStyledTextAttributes() 304 { 305 JRStyle style = getStyle(); 306 Map styledTextAttributes = (Map )styledTextAttributesMap.get(style); 307 if (styledTextAttributes == null) 308 { 309 styledTextAttributes = new HashMap (); 310 JRFontUtil.setAttributes(styledTextAttributes, this); 311 styledTextAttributes.put(TextAttribute.FOREGROUND, getForecolor()); 312 if (getMode() == JRElement.MODE_OPAQUE) 313 { 314 styledTextAttributes.put(TextAttribute.BACKGROUND, getBackcolor()); 315 } 316 styledTextAttributesMap.put(style, styledTextAttributes); 317 } 318 319 return styledTextAttributes; 320 } 321 322 325 protected float getLineSpacingFactor() 326 { 327 return lineSpacingFactor; 328 } 329 330 333 protected void setLineSpacingFactor(float lineSpacingFactor) 334 { 335 this.lineSpacingFactor = lineSpacingFactor; 336 } 337 338 341 protected float getLeadingOffset() 342 { 343 return leadingOffset; 344 } 345 346 349 protected void setLeadingOffset(float leadingOffset) 350 { 351 this.leadingOffset = leadingOffset; 352 } 353 354 357 protected byte getRunDirection() 358 { 359 return isLeftToRight ? JRPrintText.RUN_DIRECTION_LTR : JRPrintText.RUN_DIRECTION_RTL; 360 } 361 362 365 protected float getTextHeight() 366 { 367 return textHeight; 368 } 369 370 373 protected void setTextHeight(float textHeight) 374 { 375 this.textHeight = textHeight; 376 } 377 378 381 protected int getTextStart() 382 { 383 return textStart; 384 } 385 386 389 protected void setTextStart(int textStart) 390 { 391 this.textStart = textStart; 392 } 393 394 397 protected int getTextEnd() 398 { 399 return textEnd; 400 } 401 402 405 protected void setTextEnd(int textEnd) 406 { 407 this.textEnd = textEnd; 408 } 409 410 413 protected String getRawText() 414 { 415 return rawText; 416 } 417 418 421 protected void setRawText(String rawText) 422 { 423 this.rawText = rawText; 424 styledText = null; 425 } 426 427 428 431 protected void reset() 432 { 433 super.reset(); 434 435 isLeftToRight = true; 436 lineSpacingFactor = 0; 437 leadingOffset = 0; 438 textHeight = 0; 439 } 440 441 442 445 protected void rewind() 446 { 447 textStart = 0; 448 textEnd = 0; 449 } 450 451 452 455 protected JRStyledText getStyledText() 456 { 457 if (styledText == null) 458 { 459 String text = getRawText(); 460 if (text != null) 461 { 462 if (isStyledText()) 463 { 464 try 465 { 466 styledText = filler.getStyledTextParser().parse(getStyledTextAttributes(), text); 467 } 468 catch (SAXException e) 469 { 470 if (log.isWarnEnabled()) 471 log.warn("Invalid styled text.", e); 472 } 473 } 474 475 if (styledText == null) 476 { 477 styledText = new JRStyledText(); 478 styledText.append(text); 479 styledText.addRun(new JRStyledText.Run(getStyledTextAttributes(), 0, text.length())); 480 } 481 } 482 } 483 484 return styledText; 485 } 486 487 490 public String getText() 491 { 492 JRStyledText tmpStyledText = getStyledText(); 493 494 if (tmpStyledText == null) 495 { 496 return null; 497 } 498 499 return tmpStyledText.getText(); 500 } 501 502 503 506 protected void chopTextElement( 507 int availableStretchHeight 508 ) 509 { 510 JRStyledText tmpStyledText = getStyledText(); 511 512 if (tmpStyledText == null) 513 { 514 return; 515 } 516 517 String remainingText = 518 getText().substring( 519 getTextEnd() 520 ); 521 522 if (remainingText.length() == 0) 523 { 524 return; 525 } 526 527 528 textMeasurer.measure( 529 tmpStyledText, 530 remainingText, 531 getTextEnd(), 532 availableStretchHeight 533 ); 534 535 isLeftToRight = textMeasurer.isLeftToRight(); 536 setTextHeight(textMeasurer.getTextHeight()); 537 if (getRotation() == ROTATION_NONE) 538 { 539 setStretchHeight((int)getTextHeight() + getTopPadding() + getBottomPadding()); 540 } 541 else 542 { 543 setStretchHeight(getHeight()); 544 } 545 setTextStart(getTextEnd()); 546 setTextEnd(textMeasurer.getTextOffset()); 547 setLineSpacingFactor(textMeasurer.getLineSpacingFactor()); 548 setLeadingOffset(textMeasurer.getLeadingOffset()); 549 } 550 551 552 555 protected static interface TextChopper 556 { 557 560 public String chop(JRFillTextElement textElement, int startIndex, int endIndex); 561 } 562 563 564 567 private static TextChopper simpleTextChopper = 568 new TextChopper() 569 { 570 public String chop(JRFillTextElement textElement, int startIndex, int endIndex) 571 { 572 return textElement.getStyledText().getText().substring(startIndex, endIndex); 573 } 574 }; 575 576 579 private static TextChopper styledTextChopper = 580 new TextChopper() 581 { 582 public String chop(JRFillTextElement textElement, int startIndex, int endIndex) 583 { 584 return 585 textElement.filler.getStyledTextParser().write( 586 textElement.getStyledTextAttributes(), 587 new AttributedString ( 588 textElement.getStyledText().getAttributedString().getIterator(), 589 startIndex, 590 endIndex 591 ).getIterator(), 592 textElement.getText().substring(startIndex, endIndex) 593 ); 594 } 595 }; 596 597 600 public byte getBorder() 601 { 602 return JRStyleResolver.getBorder(this); 603 } 604 605 public Byte getOwnBorder() 606 { 607 return ((JRBox)parent).getOwnBorder(); 608 } 609 610 613 public void setBorder(byte border) 614 { 615 } 616 617 620 public Color getBorderColor() 621 { 622 return JRStyleResolver.getBorderColor(this, getForecolor()); 623 } 624 625 public Color getOwnBorderColor() 626 { 627 return ((JRBox)parent).getOwnBorderColor(); 628 } 629 630 633 public void setBorderColor(Color borderColor) 634 { 635 } 636 637 640 public int getPadding() 641 { 642 return JRStyleResolver.getPadding(this); 643 } 644 645 public Integer getOwnPadding() 646 { 647 return ((JRBox)parent).getOwnPadding(); 648 } 649 650 653 public void setPadding(int padding) 654 { 655 } 656 657 660 public byte getTopBorder() 661 { 662 return JRStyleResolver.getTopBorder(this); 663 } 664 665 668 public Byte getOwnTopBorder() 669 { 670 return ((JRBox)parent).getOwnTopBorder(); 671 } 672 673 676 public void setTopBorder(byte topBorder) 677 { 678 } 679 680 683 public Color getTopBorderColor() 684 { 685 return JRStyleResolver.getTopBorderColor(this, getForecolor()); 686 } 687 688 691 public Color getOwnTopBorderColor() 692 { 693 return ((JRBox)parent).getOwnTopBorderColor(); 694 } 695 696 699 public void setTopBorderColor(Color topBorderColor) 700 { 701 } 702 703 706 public int getTopPadding() 707 { 708 return JRStyleResolver.getTopPadding(this); 709 } 710 711 714 public Integer getOwnTopPadding() 715 { 716 return ((JRBox)parent).getOwnTopPadding(); 717 } 718 719 722 public void setTopPadding(int topPadding) 723 { 724 } 725 726 729 public byte getLeftBorder() 730 { 731 return JRStyleResolver.getLeftBorder(this); 732 } 733 734 737 public Byte getOwnLeftBorder() 738 { 739 return ((JRBox)parent).getOwnLeftBorder(); 740 } 741 742 745 public void setLeftBorder(byte leftBorder) 746 { 747 } 748 749 752 public Color getLeftBorderColor() 753 { 754 return JRStyleResolver.getLeftBorderColor(this, getForecolor()); 755 } 756 757 760 public Color getOwnLeftBorderColor() 761 { 762 return ((JRBox)parent).getOwnLeftBorderColor(); 763 } 764 765 768 public void setLeftBorderColor(Color leftBorderColor) 769 { 770 } 771 772 775 public int getLeftPadding() 776 { 777 return JRStyleResolver.getLeftPadding(this); 778 } 779 780 783 public Integer getOwnLeftPadding() 784 { 785 return ((JRBox)parent).getOwnLeftPadding(); 786 } 787 788 791 public void setLeftPadding(int leftPadding) 792 { 793 } 794 795 798 public byte getBottomBorder() 799 { 800 return JRStyleResolver.getBottomBorder(this); 801 } 802 803 806 public Byte getOwnBottomBorder() 807 { 808 return ((JRBox)parent).getOwnBottomBorder(); 809 } 810 811 814 public void setBottomBorder(byte bottomBorder) 815 { 816 } 817 818 821 public Color getBottomBorderColor() 822 { 823 return JRStyleResolver.getBottomBorderColor(this, getForecolor()); 824 } 825 826 829 public Color getOwnBottomBorderColor() 830 { 831 return ((JRBox)parent).getOwnBottomBorderColor(); 832 } 833 834 837 public void setBottomBorderColor(Color bottomBorderColor) 838 { 839 } 840 841 844 public int getBottomPadding() 845 { 846 return JRStyleResolver.getBottomPadding(this); 847 } 848 849 852 public Integer getOwnBottomPadding() 853 { 854 return ((JRBox)parent).getOwnBottomPadding(); 855 } 856 857 860 public void setBottomPadding(int bottomPadding) 861 { 862 } 863 864 867 public byte getRightBorder() 868 { 869 return JRStyleResolver.getRightBorder(this); 870 } 871 872 875 public Byte getOwnRightBorder() 876 { 877 return ((JRBox)parent).getOwnRightBorder(); 878 } 879 880 883 public void setRightBorder(byte rightBorder) 884 { 885 } 886 887 890 public Color getRightBorderColor() 891 { 892 return JRStyleResolver.getRightBorderColor(this, getForecolor()); 893 } 894 895 898 public Color getOwnRightBorderColor() 899 { 900 return ((JRBox)parent).getOwnRightBorderColor(); 901 } 902 903 906 public void setRightBorderColor(Color rightBorderColor) 907 { 908 } 909 910 913 public int getRightPadding() 914 { 915 return JRStyleResolver.getRightPadding(this); 916 } 917 918 921 public Integer getOwnRightPadding() 922 { 923 return ((JRBox)parent).getOwnRightPadding(); 924 } 925 926 929 public void setRightPadding(int rightPadding) 930 { 931 } 932 933 934 937 public JRReportFont getReportFont() 938 { 939 return reportFont; 940 } 941 942 public void setReportFont(JRReportFont reportFont) 943 { 944 } 945 946 949 public String getFontName() 950 { 951 return JRStyleResolver.getFontName(this); 952 } 953 954 957 public String getOwnFontName() 958 { 959 return ((JRFont)parent).getOwnFontName(); 960 } 961 962 965 public void setFontName(String fontName) 966 { 967 } 968 969 970 973 public boolean isBold() 974 { 975 return JRStyleResolver.isBold(this); 976 } 977 978 981 public Boolean isOwnBold() 982 { 983 return ((JRFont)parent).isOwnBold(); 984 } 985 986 989 public void setBold(boolean isBold) 990 { 991 } 992 993 997 public void setBold(Boolean isBold) 998 { 999 } 1000 1001 1002 1005 public boolean isItalic() 1006 { 1007 return JRStyleResolver.isItalic(this); 1008 } 1009 1010 1013 public Boolean isOwnItalic() 1014 { 1015 return ((JRFont)parent).isOwnItalic(); 1016 } 1017 1018 1021 public void setItalic(boolean isItalic) 1022 { 1023 } 1024 1025 1029 public void setItalic(Boolean isItalic) 1030 { 1031 } 1032 1033 1036 public boolean isUnderline() 1037 { 1038 return JRStyleResolver.isUnderline(this); 1039 } 1040 1041 1044 public Boolean isOwnUnderline() 1045 { 1046 return ((JRFont)parent).isOwnUnderline(); 1047 } 1048 1049 1052 public void setUnderline(boolean isUnderline) 1053 { 1054 } 1055 1056 1060 public void setUnderline(Boolean isUnderline) 1061 { 1062 } 1063 1064 1067 public boolean isStrikeThrough() 1068 { 1069 return JRStyleResolver.isStrikeThrough(this); 1070 } 1071 1072 1075 public Boolean isOwnStrikeThrough() 1076 { 1077 return ((JRFont)parent).isOwnStrikeThrough(); 1078 } 1079 1080 1083 public void setStrikeThrough(boolean isStrikeThrough) 1084 { 1085 } 1086 1087 1091 public void setStrikeThrough(Boolean isStrikeThrough) 1092 { 1093 } 1094 1095 1098 public int getFontSize() 1099 { 1100 return JRStyleResolver.getFontSize(this); 1101 } 1102 1103 1106 public Integer getOwnFontSize() 1107 { 1108 return ((JRFont)parent).getOwnFontSize(); 1109 } 1110 1111 1114 public void setFontSize(int size) 1115 { 1116 } 1117 1118 1122 public void setFontSize(Integer size) 1123 { 1124 } 1125 1126 1129 public int getSize() 1130 { 1131 return getFontSize(); 1132 } 1133 1134 1137 public Integer getOwnSize() 1138 { 1139 return getOwnFontSize(); 1140 } 1141 1142 1145 public void setSize(int size) 1146 { 1147 } 1148 1149 1152 public void setSize(Integer size) 1153 { 1154 } 1155 1156 1159 public String getPdfFontName() 1160 { 1161 return JRStyleResolver.getPdfFontName(this); 1162 } 1163 1164 1167 public String getOwnPdfFontName() 1168 { 1169 return ((JRFont)parent).getOwnPdfFontName(); 1170 } 1171 1172 1175 public void setPdfFontName(String pdfFontName) 1176 { 1177 } 1178 1179 1180 1183 public String getPdfEncoding() 1184 { 1185 return JRStyleResolver.getPdfEncoding(this); 1186 } 1187 1188 1191 public String getOwnPdfEncoding() 1192 { 1193 return ((JRFont)parent).getOwnPdfEncoding(); 1194 } 1195 1196 1199 public void setPdfEncoding(String pdfEncoding) 1200 { 1201 } 1202 1203 1204 1207 public boolean isPdfEmbedded() 1208 { 1209 return JRStyleResolver.isPdfEmbedded(this); 1210 } 1211 1212 1215 public Boolean isOwnPdfEmbedded() 1216 { 1217 return ((JRFont)parent).isOwnPdfEmbedded(); 1218 } 1219 1220 1223 public void setPdfEmbedded(boolean isPdfEmbedded) 1224 { 1225 setPdfEmbedded(isPdfEmbedded ? Boolean.TRUE : Boolean.FALSE); 1226 } 1227 1228 1232 public void setPdfEmbedded(Boolean isPdfEmbedded) 1233 { 1234 } 1235 1236 1239 public void setBorder(Byte border) 1240 { 1241 } 1242 1243 1246 public void setPadding(Integer padding) 1247 { 1248 } 1249 1250 1253 public void setTopBorder(Byte topBorder) 1254 { 1255 } 1256 1257 1260 public void setTopPadding(Integer topPadding) 1261 { 1262 } 1263 1264 1267 public void setLeftBorder(Byte leftBorder) 1268 { 1269 } 1270 1271 1274 public void setLeftPadding(Integer leftPadding) 1275 { 1276 } 1277 1278 1281 public void setBottomBorder(Byte bottomBorder) 1282 { 1283 } 1284 1285 1288 public void setBottomPadding(Integer bottomPadding) 1289 { 1290 } 1291 1292 1295 public void setRightBorder(Byte rightBorder) 1296 { 1297 } 1298 1299 1302 public void setRightPadding(Integer rightPadding) 1303 { 1304 } 1305 1306 1309 public void setHeight(int height) 1310 { 1311 super.setHeight(height); 1312 1313 createTextMeasurer(); 1314 } 1315 1316 1317 public void setWidth(int width) 1318 { 1319 super.setWidth(width); 1320 1321 createTextMeasurer(); 1322 } 1323 1324} 1325 | Popular Tags |