1 51 package org.apache.fop.render.txt; 52 53 import org.apache.fop.render.PrintRenderer; 55 import org.apache.fop.fo.properties.*; 56 import org.apache.fop.image.FopImage; 57 import org.apache.fop.image.FopImageException; 58 import org.apache.fop.layout.*; 59 import org.apache.fop.layout.inline.*; 60 import org.apache.fop.datatypes.*; 61 import org.apache.fop.pdf.PDFPathPaint; 62 import org.apache.fop.pdf.PDFColor; 63 import org.apache.fop.image.*; 64 65 import org.apache.fop.svg.SVGArea; 66 67 import org.w3c.dom.svg.SVGSVGElement; 68 import org.w3c.dom.svg.SVGDocument; 69 70 import java.io.IOException ; 72 import java.io.OutputStream ; 73 import java.util.List ; 74 75 82 public class TXTRenderer extends PrintRenderer { 83 84 87 TXTStream currentStream; 88 public static final String encodingOptionName = "txt.encoding"; 89 private static final String DEFAULT_ENCODING = "UTF-8"; 90 91 private int pageHeight = 7920; 92 93 public int curdiv = 0; 95 private int divisions = -1; 96 private int paperheight = -1; public int orientation = 98 -1; public int topmargin = -1; public int leftmargin = -1; private int fullmargin = 0; 102 final boolean debug = false; 103 104 StringBuffer charData[]; 106 StringBuffer decoData[]; 107 public float textCPI = 16.67f; 108 public float textLPI = 8; 109 int maxX = (int)(8.5f * textCPI + 1); 110 int maxY = (int)(11f * textLPI + 1); 111 float xFactor; 112 float yFactor; 113 public String lineEnding = 114 "\r\n"; public String pageEnding = 116 "\f"; public boolean suppressGraphics = 118 false; boolean firstPage = false; 120 123 protected java.util.Map options; 124 125 public TXTRenderer() {} 126 127 130 public void setOptions(java.util.Map options) { 131 this.options = options; 132 } 133 134 139 public void setProducer(String producer) {} 140 141 142 void addStr(int row, int col, String str, boolean ischar) { 143 if (debug) 144 System.out.println("TXTRenderer.addStr(" + row + ", " + col 145 + ", \"" + str + "\", " + ischar + ")"); 146 if (suppressGraphics &&!ischar) 147 return; 148 StringBuffer sb; 149 if (row < 0) 150 row = 0; 151 if (ischar) 152 sb = charData[row]; 153 else 154 sb = decoData[row]; 155 if (sb == null) 156 sb = new StringBuffer (); 157 if ((col + str.length()) > maxX) 158 col = maxX - str.length(); 159 if (col < 0) { 160 col = 0; 161 if (str.length() > maxX) 162 str = str.substring(0, maxX); 163 } 164 for (int countr = sb.length(); countr < col; countr++) 166 sb.append(' '); 167 if (debug) 168 System.out.println("TXTRenderer.addStr() sb.length()=" 169 + sb.length()); 170 for (int countr = col; countr < (col + str.length()); countr++) { 171 if (countr >= sb.length()) 172 sb.append(str.charAt(countr - col)); 173 else { 174 if (debug) 175 System.out.println("TXTRenderer.addStr() sb.length()=" 176 + sb.length() + " countr=" + countr); 177 sb.setCharAt(countr, str.charAt(countr - col)); 178 } 179 } 180 181 if (ischar) 182 charData[row] = sb; 183 else 184 decoData[row] = sb; 185 } 186 187 197 protected void addLine(int x1, int y1, int x2, int y2, int th, 198 PDFPathPaint stroke) { 199 if (x1 == x2) { 200 addRect(x1, y1, th, y2 - y1 + 1, stroke, stroke); 201 } else if (y1 == y2) { 202 addRect(x1, y1, x2 - x1 + 1, th, stroke, stroke); 203 } 204 } 205 206 217 protected void addLine(int x1, int y1, int x2, int y2, int th, int rs, 218 PDFPathPaint stroke) { 219 PDFColor lstroke = null; 220 if (rs == org.apache.fop.fo.properties.RuleStyle.DOTTED) 221 lstroke = new PDFColor(0.7f, 0.7f, 0.7f); 222 else 223 lstroke = (PDFColor)stroke; 224 if (x1 == x2) { 225 addRect(x1, y1, th, y2 - y1 + 1, lstroke, lstroke); 226 } else if (y1 == y2) { 227 addRect(x1, y1, x2 - x1 + 1, th, lstroke, lstroke); 228 } 229 } 230 231 protected void addLine(float x1, float y1, float x2, float y2, 232 PDFColor sc, float sw) { 233 337 } 338 339 private void xferLineBytes(int startpos, int bitcount, List save, 340 int start2) { 341 449 } 450 451 460 protected void addRect(int x, int y, int w, int h, PDFPathPaint stroke) { 461 if (h < 0) 462 h *= -1; 463 464 if (h < 720 || w < 720) { 465 if (w < 720) 466 w = 720; 467 if (h < 720) 468 h = 720; 469 addRect(x, y, w, h, stroke, stroke); 470 } else { 471 addRect(x, y, w, 720, stroke, stroke); 472 addRect(x, y, 720, h, stroke, stroke); 473 addRect(x + w - 720, y, 720, h, stroke, stroke); 474 addRect(x, y - h + 720, w, 720, stroke, stroke); 475 } 476 } 477 478 488 protected void addRect(int x, int y, int w, int h, PDFPathPaint stroke, 489 PDFPathPaint fill) { 490 if ((w == 0) || (h == 0)) 492 return; 493 if (h < 0) 494 h *= -1; 495 496 int row = (int)((pageHeight - (y / 100)) * 100 * yFactor); 497 int col = (int)(x * xFactor); 498 499 PDFColor sc = (PDFColor)stroke; 500 PDFColor fc = (PDFColor)fill; 501 502 sc.setColorSpace(ColorSpace.DEVICE_RGB); 503 fc.setColorSpace(ColorSpace.DEVICE_RGB); 504 505 int lineshade = 506 (int)(100 507 - ((0.3f * sc.red() + 0.59f * sc.green() + 0.11f * sc.blue()) 508 * 100f)); 509 int fillshade = 510 (int)(100 511 - ((0.3f * fc.red() + 0.59f * fc.green() + 0.11f * fc.blue()) 512 * 100f)); 513 if (debug) 514 System.out.println("TXTRenderer.addRect(" + x + ", " + y + ", " 515 + w + ", " + h + ", " + stroke + ", " + fill 516 + ") fillshade=" + fillshade); 517 char fillchar = ' '; 518 if (fillshade >= 75) 519 fillchar = '#'; 520 else if (fillshade >= 50) 521 fillchar = '*'; 522 else if (fillshade >= 25) 523 fillchar = ':'; 524 525 526 if (fillchar != ' ') { 527 StringBuffer linefill = new StringBuffer (); 528 int sw = (int)(w * xFactor); 529 int sh = (int)(h * yFactor); 530 if (sw == 0 || sh == 0) { 531 if (fillshade >= 50) { 532 if (h > w) 533 fillchar = '|'; 534 else 535 fillchar = '-'; 536 } else { 537 if (h > w) 538 fillchar = ':'; 539 else 540 fillchar = '.'; 541 } 542 } 543 if (sw == 0) 544 linefill.append(fillchar); 545 else 546 for (int countr = 0; countr < sw; countr++) 547 linefill.append(fillchar); 548 if (sh == 0) 549 addStr(row, col, linefill.toString(), false); 550 else 551 for (int countr = 0; countr < sh; countr++) 552 addStr(row + countr, col, linefill.toString(), false); 553 } 554 555 if (lineshade >= 25) { 556 char vlinechar = '|'; 557 char hlinechar = '-'; 558 if (lineshade < 50) { 559 vlinechar = ':'; 560 hlinechar = '.'; 561 } 562 StringBuffer linefill = new StringBuffer (); 563 int sw = (int)(w * xFactor); 564 for (int countr = 0; countr < sw; countr++) 565 linefill.append(hlinechar); 566 int sh = (int)(h * yFactor); 567 568 if (w > h) { 569 for (int countr = 1; countr < (sh - 1); countr++) { 570 addStr(row + countr, col, String.valueOf(vlinechar), 571 false); 572 addStr(row + countr, col + sw, String.valueOf(vlinechar), 573 false); 574 } 575 addStr(row, col, linefill.toString(), false); 576 addStr(row + sh, col, linefill.toString(), false); 577 578 } else { 579 addStr(row, col, linefill.toString(), false); 580 addStr(row + sh, col, linefill.toString(), false); 581 for (int countr = 1; countr < (sh - 1); countr++) { 582 addStr(row + countr, col, String.valueOf(vlinechar), 583 false); 584 addStr(row + countr, col + sw, String.valueOf(vlinechar), 585 false); 586 } 587 588 } 589 } 590 } 591 592 593 607 protected void addRect(float x, float y, float w, float h, float rx, 608 float ry, PDFColor fc, PDFColor sc, float sw) { 609 773 } 774 775 protected void addPolyline(List points, int posx, int posy, 777 PDFColor fc, PDFColor sc, float sw, 778 boolean close) {} 779 780 793 protected void drawImageScaled(int x, int y, int w, int h, 794 FopImage image, 795 FontState fs) { 796 } 798 799 812 protected void drawImageClipped(int x, int y, 813 int clipX, int clipY, 814 int clipW, int clipH, 815 FopImage image, 816 FontState fs) { 817 } 819 820 boolean printBMP(FopImage img, int x, int y, int w, 821 int h) throws FopImageException { 822 if (debug) 823 System.out.println("TXTRenderer.printBMP(" + img + ", " + x 824 + ", " + y + ", " + w + ", " + h + ")"); 825 addRect(x, y, w, h, new PDFColor(1f, 1f, 1f), 826 new PDFColor(0f, 0f, 0f)); 827 int nameh = (int)(h * yFactor / 2); 828 if (nameh > 0) { 829 int namew = (int)(w * xFactor); 830 831 if (namew > 4) { 832 String iname = img.getURL(); 833 if (iname.length() >= namew) 834 addStr((int)((pageHeight - (y / 100)) * 100 * yFactor) 835 + nameh, (int)(x * xFactor), 836 iname.substring(iname.length() - namew), 837 true); 838 else 839 addStr((int)((pageHeight - (y / 100)) * 100 * yFactor) 840 + nameh, (int)(x * xFactor 841 + (namew - iname.length()) 842 / 2), iname, true); 843 844 } 845 } 846 return (true); 847 } 848 849 854 public void renderImageArea(ImageArea area) { 855 int x = this.currentAreaContainerXPosition + area.getXOffset(); 856 int y = this.currentYPosition; 857 int w = area.getContentWidth(); 858 int h = area.getHeight(); 859 860 this.currentYPosition -= h; 861 862 FopImage img = area.getImage(); 863 864 try { 865 printBMP(img, x, y, w, h); 866 } catch (FopImageException e) { 867 log.error("TXTRenderer.renderImageArea() printing BMP (" 869 + e.toString() + ").", e); 870 } 871 } 872 873 public void renderImage(FontState fontState, String href, float x, 874 float y, float width, float height) { 875 try { 876 if (href.indexOf(":") == -1) 877 href = "file:" + href; 878 FopImage img = FopImageFactory.Make(href); 879 if (img != null) { 880 if (img instanceof SVGImage) { 881 SVGSVGElement svg = 882 ((SVGImage)img).getSVGDocument().getRootElement(); 883 renderSVG(fontState, svg, (int)x * 1000, (int)y * 1000); 884 } else { 885 printBMP(img, (int)x, (int)y, (int)width, (int)height); 886 } 887 } 888 } catch (Exception e) { 889 log.error("could not add image to SVG: " + href, e); 890 } 891 } 892 893 896 public void renderForeignObjectArea(ForeignObjectArea area) { 897 this.currentXPosition = this.currentXPosition + area.getXOffset(); 899 this.currentYPosition = this.currentYPosition; 900 switch (area.getAlign()) { 901 case TextAlign.START: 902 break; 903 case TextAlign.END: 904 break; 905 case TextAlign.CENTER: 906 case TextAlign.JUSTIFY: 907 break; 908 } 909 switch (area.getVerticalAlign()) { 910 case VerticalAlign.BASELINE: 911 break; 912 case VerticalAlign.MIDDLE: 913 break; 914 case VerticalAlign.SUB: 915 break; 916 case VerticalAlign.SUPER: 917 break; 918 case VerticalAlign.TEXT_TOP: 919 break; 920 case VerticalAlign.TEXT_BOTTOM: 921 break; 922 case VerticalAlign.TOP: 923 break; 924 case VerticalAlign.BOTTOM: 925 break; 926 } 927 929 931 switch (area.scalingMethod()) { 932 case Scaling.UNIFORM: 933 break; 934 case Scaling.NON_UNIFORM: 935 break; 936 } 937 switch (area.getOverflow()) { 941 case Overflow.VISIBLE: 942 case Overflow.SCROLL: 943 case Overflow.AUTO: 944 break; 945 case Overflow.HIDDEN: 946 break; 947 } 948 area.getObject().render(this); 949 950 this.currentXPosition += area.getEffectiveWidth(); 951 } 953 954 955 void renderSVG(FontState fontState, SVGSVGElement svg, int x, int y) { 956 966 } 967 968 973 public void renderSVGArea(SVGArea area) { 974 if (debug) 975 System.out.println("TXTRenderer.renderSVGArea(" + area + ")"); 976 int x = this.currentAreaContainerXPosition; 977 int y = this.currentYPosition; 978 SVGSVGElement svg = 979 ((SVGDocument)area.getSVGDocument()).getRootElement(); 980 int w = (int)(svg.getWidth().getBaseVal().getValue() * 1000); 981 int h = (int)(svg.getHeight().getBaseVal().getValue() * 1000); 982 983 989 1001 1003 renderSVG(area.getFontState(), svg, x, y); 1004 1005 1013 } 1017 1018 1231 1232 private void setFont(String name, float size) { 1233 return; 1234 } 1235 1236 1504 1505 1518 1519 1600 1601 1606 public void renderWordArea(WordArea area) { 1607 int rx = this.currentXPosition; 1609 int bl = this.currentYPosition; 1610 1611 String s = area.getText(); 1612 1613 if (debug) 1614 System.out.println("TXTRenderer.renderInlineArea: rx=" + rx 1615 + " bl=" + bl + " pageHeight=" + pageHeight); 1616 addStr((int)((pageHeight - (bl / 100)) * 100 * yFactor) - 1, 1617 (int)(rx * xFactor), s, true); 1618 1619 this.currentXPosition += area.getContentWidth(); 1620 } 1621 1622 1627 public void renderInlineSpace(InlineSpace space) { 1628 this.currentXPosition += space.getSize(); 1629 } 1630 1631 1636 public void renderPage(Page page) { 1637 if (debug) 1638 System.out.println("TXTRenderer.renderPage() page.getHeight() = " 1639 + page.getHeight()); 1640 1641 maxX = (int)(textCPI * page.getWidth() / 72000 + 1); 1642 maxY = (int)(textLPI * page.getHeight() / 72000 + 1); 1643 xFactor = (float)(maxX - 1) / (float)page.getWidth(); 1644 yFactor = (float)(maxY - 1) / (float)page.getHeight(); 1645 charData = new StringBuffer [maxY + 1]; 1646 decoData = new StringBuffer [maxY + 1]; 1647 1648 if (paperheight > 0) 1649 pageHeight = paperheight; 1650 else 1651 pageHeight = page.getHeight() / 100; 1652 1653 if (debug) 1654 System.out.println("TXTRenderer.renderPage() maxX=" + maxX 1655 + " maxY=" + maxY + " xFactor=" + xFactor 1656 + " yFactor=" + yFactor + " paperHeight=" 1657 + pageHeight); 1658 1659 this.currentFontName = ""; 1660 this.currentFontSize = 0; 1661 1662 renderRegions(page); 1664 1665 for (int row = 0; row <= maxY; row++) { 1667 StringBuffer cr = charData[row]; 1668 StringBuffer dr = decoData[row]; 1669 StringBuffer outr = null; 1670 1671 if (cr != null && dr == null) 1672 outr = cr; 1673 else if (dr != null && cr == null) 1674 outr = dr; 1675 else if (cr != null && dr != null) { 1676 int len = dr.length(); 1677 if (cr.length() > len) 1678 len = cr.length(); 1679 outr = new StringBuffer (); 1680 for (int countr = 0; countr < len; countr++) { 1681 if (countr < cr.length() && cr.charAt(countr) != ' ') 1682 outr.append(cr.charAt(countr)); 1683 else if (countr < dr.length()) 1684 outr.append(dr.charAt(countr)); 1685 else 1686 outr.append(' '); 1687 } 1688 } 1689 1690 if (outr != null) 1691 currentStream.add(outr.toString()); 1692 if (row < maxY) 1693 currentStream.add(lineEnding); 1694 } 1695 1696 1703 1729 } 1730 public void startRenderer(OutputStream outputStream) 1731 throws IOException { 1732 log.info("rendering areas to TEXT"); 1733 currentStream = new TXTStream(outputStream); 1734 String encoding; 1735 if (options != null && (encoding=(String )options.get(encodingOptionName))!=null) { 1736 try { 1737 byte buff[] = " ".getBytes(encoding); 1738 } catch (java.io.UnsupportedEncodingException uee) { 1739 log.warn("Encoding '"+encoding+"' is not supported, so defaulted to " + DEFAULT_ENCODING); 1740 encoding = DEFAULT_ENCODING; 1741 } 1742 } 1743 else 1744 encoding = DEFAULT_ENCODING; 1745 currentStream.setEncoding(encoding); 1746 firstPage=true; 1747 } 1748 1749 1754 1755 public void stopRenderer(OutputStream outputStream) 1756 throws IOException { 1757 log.info("writing out TEXT"); 1758 outputStream.flush(); 1759 } 1760 1761 public void render(Page page, OutputStream outputStream) { 1762 idReferences = page.getIDReferences(); 1763 1764 if ( firstPage ) 1765 firstPage = false; 1766 else 1767 currentStream.add(pageEnding); 1768 this.renderPage(page); 1769 currentStream.add(lineEnding); 1770 } 1771} 1772 | Popular Tags |