1 51 package org.apache.fop.render.ps; 52 53 import org.apache.fop.pdf.*; 54 import org.apache.fop.layout.*; 55 import org.apache.fop.image.*; 56 import org.apache.fop.datatypes.ColorSpace; 57 58 import org.apache.batik.ext.awt.g2d.*; 59 60 import java.text.AttributedCharacterIterator ; 61 import java.text.CharacterIterator ; 62 import java.awt.*; 63 import java.awt.Font ; 64 import java.awt.Image ; 65 import java.awt.image.*; 66 import java.awt.geom.*; 67 import java.awt.image.renderable.*; 68 69 import java.util.Map ; 70 import java.util.ArrayList ; 71 72 84 public class PSGraphics2D extends AbstractGraphics2D { 85 boolean standalone = false; 86 87 90 protected PSRenderer psRenderer; 91 92 protected FontState fontState; 93 94 97 protected String currentFontName; 98 99 102 protected int currentFontSize; 103 104 107 protected int currentYPosition = 0; 108 109 112 protected int currentXPosition = 0; 113 114 117 PDFColor currentColour = new PDFColor(0, 0, 0); 118 119 FontInfo fontInfo; 120 121 126 public PSGraphics2D(boolean textAsShapes, FontState fs, PSRenderer ren, 127 String font, int size, int xpos, int ypos) { 128 super(textAsShapes); 129 psRenderer = ren; 130 currentFontName = font; 131 currentFontSize = size; 132 currentYPosition = ypos; 133 currentXPosition = xpos; 134 fontState = fs; 135 } 136 137 public PSGraphics2D(boolean textAsShapes) { 138 super(textAsShapes); 139 } 140 141 public void setGraphicContext(GraphicContext c) { 142 gc = c; 143 } 144 145 148 public PSGraphics2D(PSGraphics2D g) { 149 super(g); 150 } 151 152 158 public Graphics create() { 159 return new PSGraphics2D(this); 160 } 161 162 186 public boolean drawImage(Image img, int x, int y, 187 ImageObserver observer) { 188 190 final int width = img.getWidth(observer); 191 final int height = img.getHeight(observer); 192 if (width == -1 || height == -1) { 193 return false; 194 } 195 196 Dimension size = new Dimension(width, height); 197 BufferedImage buf = buildBufferedImage(size); 198 199 java.awt.Graphics2D g = buf.createGraphics(); 200 g.setComposite(AlphaComposite.SrcOver); 201 g.setBackground(new Color(1, 1, 1, 0)); 202 g.setPaint(new Color(1, 1, 1, 0)); 203 g.fillRect(0, 0, width, height); 204 g.clip(new Rectangle(0, 0, buf.getWidth(), buf.getHeight())); 205 206 if (!g.drawImage(img, 0, 0, observer)) { 207 return false; 208 } 209 g.dispose(); 210 211 final byte[] result = new byte[buf.getWidth() * buf.getHeight() * 3]; 212 final byte[] mask = new byte[buf.getWidth() * buf.getHeight()]; 213 214 Raster raster = buf.getData(); 215 DataBuffer bd = raster.getDataBuffer(); 216 217 int count = 0; 218 int maskpos = 0; 219 switch (bd.getDataType()) { 220 case DataBuffer.TYPE_INT: 221 int[][] idata = ((DataBufferInt)bd).getBankData(); 222 for (int i = 0; i < idata.length; i++) { 223 for (int j = 0; j < idata[i].length; j++) { 224 if (((idata[i][j] >> 24) & 0xFF) != 255) { 226 result[count++] = (byte)0xFF; 227 result[count++] = (byte)0xFF; 228 result[count++] = (byte)0xFF; 229 } else { 230 result[count++] = (byte)((idata[i][j] >> 16) & 0xFF); 231 result[count++] = (byte)((idata[i][j] >> 8) & 0xFF); 232 result[count++] = (byte)((idata[i][j]) & 0xFF); 233 } 234 } 235 } 236 break; 237 default: 238 break; 240 } 241 242 try { 243 FopImage fopimg = new TempImage(width, height, result, mask); 244 AffineTransform at = getTransform(); 245 double[] matrix = new double[6]; 246 at.getMatrix(matrix); 247 psRenderer.write("gsave"); 248 Shape imclip = getClip(); 249 writeClip(imclip); 250 psRenderer.write("1000 -1000 scale"); 251 psRenderer.renderBitmap(fopimg, x, -y, width, height); 255 psRenderer.write("grestore"); 256 } catch (Exception e) { 257 e.printStackTrace(); 258 } 259 return true; 260 } 261 262 public BufferedImage buildBufferedImage(Dimension size) { 263 return new BufferedImage(size.width, size.height, 264 BufferedImage.TYPE_INT_ARGB); 265 } 266 267 class TempImage implements FopImage { 268 int m_height; 269 int m_width; 270 int m_bitsPerPixel; 271 ColorSpace m_colorSpace; 272 int m_bitmapSiye; 273 byte[] m_bitmaps; 274 byte[] m_mask; 275 PDFColor transparent = new PDFColor(255, 255, 255); 276 277 TempImage(int width, int height, byte[] result, 278 byte[] mask) throws FopImageException { 279 this.m_height = height; 280 this.m_width = width; 281 this.m_bitsPerPixel = 8; 282 this.m_colorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 283 this.m_bitmaps = result; 286 this.m_mask = mask; 287 } 288 public boolean invertImage() { 289 return false; 290 } 291 292 public String getURL() { 293 return "" + m_bitmaps; 294 } 295 296 public int getWidth() throws FopImageException { 298 return m_width; 299 } 300 301 public int getHeight() throws FopImageException { 302 return m_height; 303 } 304 305 public ColorSpace getColorSpace() throws FopImageException { 307 return m_colorSpace; 308 } 309 310 public int getBitsPerPixel() throws FopImageException { 312 return m_bitsPerPixel; 313 } 314 315 public boolean isTransparent() throws FopImageException { 317 return transparent != null; 318 } 319 320 public PDFColor getTransparentColor() throws FopImageException { 321 return transparent; 322 } 323 324 public byte[] getMask() throws FopImageException { 325 return m_mask; 326 } 327 328 330 public byte[] getBitmaps() throws FopImageException { 332 return m_bitmaps; 333 } 334 335 public int getBitmapsSize() throws FopImageException { 337 return m_width * m_height * 3; 338 } 339 340 public byte[] getRessourceBytes() throws FopImageException { 344 return null; 345 } 346 347 public int getRessourceBytesSize() throws FopImageException { 348 return 0; 349 } 350 351 public PDFFilter getPDFFilter() throws FopImageException { 353 return null; 354 } 355 356 public void close() {} 358 359 } 360 361 362 395 public boolean drawImage(Image img, int x, int y, int width, int height, 396 ImageObserver observer) { 397 System.out.println("drawImage"); 398 return true; 399 } 400 401 428 public void dispose() { 429 psRenderer = null; 431 fontState = null; 432 currentFontName = null; 433 currentColour = null; 434 fontInfo = null; 435 } 436 437 453 public void draw(Shape s) { 454 psRenderer.write("gsave"); 456 Shape imclip = getClip(); 457 writeClip(imclip); 458 Color c = getColor(); 459 psRenderer.write(c.getRed() + " " + c.getGreen() + " " + c.getBlue() 460 + " setrgbcolor"); 461 462 applyPaint(getPaint(), false); 463 applyStroke(getStroke()); 464 465 psRenderer.write("newpath"); 466 PathIterator iter = s.getPathIterator(getTransform()); 467 while (!iter.isDone()) { 468 double vals[] = new double[6]; 469 int type = iter.currentSegment(vals); 470 switch (type) { 471 case PathIterator.SEG_CUBICTO: 472 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 473 + PDFNumber.doubleOut(1000 * vals[1]) + " " 474 + PDFNumber.doubleOut(1000 * vals[2]) + " " 475 + PDFNumber.doubleOut(1000 * vals[3]) + " " 476 + PDFNumber.doubleOut(1000 * vals[4]) + " " 477 + PDFNumber.doubleOut(1000 * vals[5]) 478 + " curveto"); 479 break; 480 case PathIterator.SEG_LINETO: 481 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 482 + PDFNumber.doubleOut(1000 * vals[1]) 483 + " lineto"); 484 break; 485 case PathIterator.SEG_MOVETO: 486 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 487 + PDFNumber.doubleOut(1000 * vals[1]) 488 + " M"); 489 break; 490 case PathIterator.SEG_QUADTO: 491 break; 496 case PathIterator.SEG_CLOSE: 497 psRenderer.write("closepath"); 498 break; 499 default: 500 break; 501 } 502 iter.next(); 503 } 504 doDrawing(false, true, false); 505 psRenderer.write("grestore"); 506 } 507 508 protected void writeClip(Shape s) { 509 PathIterator iter = s.getPathIterator(getTransform()); 510 psRenderer.write("newpath"); 511 while (!iter.isDone()) { 512 double vals[] = new double[6]; 513 int type = iter.currentSegment(vals); 514 switch (type) { 515 case PathIterator.SEG_CUBICTO: 516 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 517 + PDFNumber.doubleOut(1000 * vals[1]) + " " 518 + PDFNumber.doubleOut(1000 * vals[2]) + " " 519 + PDFNumber.doubleOut(1000 * vals[3]) + " " 520 + PDFNumber.doubleOut(1000 * vals[4]) + " " 521 + PDFNumber.doubleOut(1000 * vals[5]) 522 + " curveto"); 523 break; 524 case PathIterator.SEG_LINETO: 525 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 526 + PDFNumber.doubleOut(1000 * vals[1]) 527 + " lineto"); 528 break; 529 case PathIterator.SEG_MOVETO: 530 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 531 + PDFNumber.doubleOut(1000 * vals[1]) 532 + " M"); 533 break; 534 case PathIterator.SEG_QUADTO: 535 break; 540 case PathIterator.SEG_CLOSE: 541 psRenderer.write("closepath"); 542 break; 543 default: 544 break; 545 } 546 iter.next(); 547 } 548 psRenderer.write("clippath"); 550 } 551 552 protected void applyPaint(Paint paint, boolean fill) { 553 if (paint instanceof GradientPaint) { 554 GradientPaint gp = (GradientPaint)paint; 555 Color c1 = gp.getColor1(); 556 Color c2 = gp.getColor2(); 557 Point2D p1 = gp.getPoint1(); 558 Point2D p2 = gp.getPoint2(); 559 boolean cyclic = gp.isCyclic(); 560 561 ArrayList theCoords = new ArrayList (); 562 theCoords.add(new Double (p1.getX())); 563 theCoords.add(new Double (p1.getY())); 564 theCoords.add(new Double (p2.getX())); 565 theCoords.add(new Double (p2.getY())); 566 567 ArrayList theExtend = new ArrayList (); 568 theExtend.add(new Boolean (true)); 569 theExtend.add(new Boolean (true)); 570 571 ArrayList theDomain = new ArrayList (); 572 theDomain.add(new Double (0)); 573 theDomain.add(new Double (1)); 574 575 ArrayList theEncode = new ArrayList (); 576 theEncode.add(new Double (0)); 577 theEncode.add(new Double (1)); 578 theEncode.add(new Double (0)); 579 theEncode.add(new Double (1)); 580 581 ArrayList theBounds = new ArrayList (); 582 theBounds.add(new Double (0)); 583 theBounds.add(new Double (1)); 584 585 ArrayList theFunctions = new ArrayList (); 586 587 ArrayList someColors = new ArrayList (); 588 589 PDFColor color1 = new PDFColor(c1.getRed(), c1.getGreen(), 590 c1.getBlue()); 591 someColors.add(color1); 592 PDFColor color2 = new PDFColor(c2.getRed(), c2.getGreen(), 593 c2.getBlue()); 594 someColors.add(color2); 595 596 ColorSpace aColorSpace = new ColorSpace(ColorSpace.DEVICE_RGB); 597 } else if (paint instanceof TexturePaint) {} 598 } 599 600 protected void applyStroke(Stroke stroke) { 601 if (stroke instanceof BasicStroke) { 602 BasicStroke bs = (BasicStroke)stroke; 603 604 float[] da = bs.getDashArray(); 605 if (da != null) { 606 psRenderer.write("["); 607 for (int count = 0; count < da.length; count++) { 608 psRenderer.write("" + (1000 * (int)da[count])); 609 if (count < da.length - 1) { 610 psRenderer.write(" "); 611 } 612 } 613 psRenderer.write("] "); 614 float offset = bs.getDashPhase(); 615 psRenderer.write((1000 * (int)offset) + " setdash"); 616 } 617 int ec = bs.getEndCap(); 618 switch (ec) { 619 case BasicStroke.CAP_BUTT: 620 psRenderer.write(0 + " setlinecap"); 621 break; 622 case BasicStroke.CAP_ROUND: 623 psRenderer.write(1 + " setlinecap"); 624 break; 625 case BasicStroke.CAP_SQUARE: 626 psRenderer.write(2 + " setlinecap"); 627 break; 628 } 629 630 int lj = bs.getLineJoin(); 631 switch (lj) { 632 case BasicStroke.JOIN_MITER: 633 psRenderer.write(0 + " setlinejoin"); 634 break; 635 case BasicStroke.JOIN_ROUND: 636 psRenderer.write(1 + " setlinejoin"); 637 break; 638 case BasicStroke.JOIN_BEVEL: 639 psRenderer.write(2 + " setlinejoin"); 640 break; 641 } 642 float lw = bs.getLineWidth(); 643 psRenderer.write(PDFNumber.doubleOut(1000 * lw) 644 + " setlinewidth"); 645 646 float ml = bs.getMiterLimit(); 647 psRenderer.write(PDFNumber.doubleOut(1000 * ml) 648 + " setmiterlimit"); 649 } 650 } 651 652 672 public void drawRenderedImage(RenderedImage img, AffineTransform xform) { 673 System.out.println("drawRenderedImage"); 674 } 675 676 677 707 public void drawRenderableImage(RenderableImage img, 708 AffineTransform xform) { 709 System.out.println("drawRenderableImage"); 710 } 711 712 734 public void drawString(String s, float x, float y) { 735 System.out.println("drawString(String)"); 736 psRenderer.write("BT"); 737 Shape imclip = getClip(); 738 writeClip(imclip); 739 Color c = getColor(); 740 psRenderer.write(c.getRed() + " " + c.getGreen() + " " + c.getBlue() 741 + " setrgbcolor"); 742 743 AffineTransform trans = getTransform(); 744 trans.translate(x, y); 745 double[] vals = new double[6]; 746 trans.getMatrix(vals); 747 748 psRenderer.write(PDFNumber.doubleOut(vals[0]) + " " 749 + PDFNumber.doubleOut(vals[1]) + " " 750 + PDFNumber.doubleOut(vals[2]) + " " 751 + PDFNumber.doubleOut(vals[3]) + " " 752 + PDFNumber.doubleOut(vals[4]) + " " 753 + PDFNumber.doubleOut(vals[5]) + " " 754 + PDFNumber.doubleOut(vals[6]) + " Tm [" + s + "]"); 755 756 psRenderer.write("ET"); 757 } 758 759 782 public void drawString(AttributedCharacterIterator iterator, float x, 783 float y) { 784 System.err.println("drawString(AttributedCharacterIterator)"); 785 786 psRenderer.write("BT"); 787 Shape imclip = getClip(); 788 writeClip(imclip); 789 Color c = getColor(); 790 currentColour = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); 791 psRenderer.write(currentColour.getColorSpaceOut(true)); 792 c = getBackground(); 793 PDFColor col = new PDFColor(c.getRed(), c.getGreen(), c.getBlue()); 794 psRenderer.write(col.getColorSpaceOut(false)); 795 796 AffineTransform trans = getTransform(); 797 trans.translate(x, y); 798 double[] vals = new double[6]; 799 trans.getMatrix(vals); 800 801 for (char ch = iterator.first(); ch != CharacterIterator.DONE; 802 ch = iterator.next()) { 803 Map attr = iterator.getAttributes(); 804 805 psRenderer.write(PDFNumber.doubleOut(vals[0]) + " " 806 + PDFNumber.doubleOut(vals[1]) + " " 807 + PDFNumber.doubleOut(vals[2]) + " " 808 + PDFNumber.doubleOut(vals[3]) + " " 809 + PDFNumber.doubleOut(vals[4]) + " " 810 + PDFNumber.doubleOut(vals[5]) + " " 811 + PDFNumber.doubleOut(vals[6]) + " Tm [" + ch 812 + "]"); 813 } 814 815 psRenderer.write("ET"); 816 } 817 818 832 public void fill(Shape s) { 833 psRenderer.write("gsave"); 835 Shape imclip = getClip(); 836 writeClip(imclip); 837 Color c = getColor(); 838 psRenderer.write(c.getRed() + " " + c.getGreen() + " " + c.getBlue() 839 + " setrgbcolor"); 840 841 applyPaint(getPaint(), true); 842 843 psRenderer.write("newpath"); 844 PathIterator iter = s.getPathIterator(getTransform()); 845 while (!iter.isDone()) { 846 double vals[] = new double[6]; 847 int type = iter.currentSegment(vals); 848 switch (type) { 849 case PathIterator.SEG_CUBICTO: 850 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 851 + PDFNumber.doubleOut(1000 * vals[1]) + " " 852 + PDFNumber.doubleOut(1000 * vals[2]) + " " 853 + PDFNumber.doubleOut(1000 * vals[3]) + " " 854 + PDFNumber.doubleOut(1000 * vals[4]) + " " 855 + PDFNumber.doubleOut(1000 * vals[5]) 856 + " curveto"); 857 break; 858 case PathIterator.SEG_LINETO: 859 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 860 + PDFNumber.doubleOut(1000 * vals[1]) 861 + " lineto"); 862 break; 863 case PathIterator.SEG_MOVETO: 864 psRenderer.write(PDFNumber.doubleOut(1000 * vals[0]) + " " 865 + PDFNumber.doubleOut(1000 * vals[1]) 866 + " M"); 867 break; 868 case PathIterator.SEG_QUADTO: 869 break; 874 case PathIterator.SEG_CLOSE: 875 psRenderer.write("closepath"); 876 break; 877 default: 878 break; 879 } 880 iter.next(); 881 } 882 doDrawing(true, false, 883 iter.getWindingRule() == PathIterator.WIND_EVEN_ODD); 884 psRenderer.write("grestore"); 885 } 886 887 protected void doDrawing(boolean fill, boolean stroke, boolean nonzero) { 888 if (fill) { 889 if (stroke) { 890 if (!nonzero) 891 psRenderer.write("stroke"); 892 else 893 psRenderer.write("stroke"); 894 } else { 895 if (!nonzero) 896 psRenderer.write("fill"); 897 else 898 psRenderer.write("fill"); 899 } 900 } else { 901 psRenderer.write("stroke"); 903 } 904 } 905 906 910 public GraphicsConfiguration getDeviceConfiguration() { 911 return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(); 913 } 914 915 918 private Graphics2D fmg; 919 920 { 921 BufferedImage bi = new BufferedImage(1, 1, 922 BufferedImage.TYPE_INT_ARGB); 923 924 fmg = bi.createGraphics(); 925 } 926 927 935 public FontMetrics getFontMetrics(Font f) { 936 return fmg.getFontMetrics(f); 937 } 938 939 954 public void setXORMode(Color c1) { 955 System.out.println("setXORMode"); 956 } 957 958 959 978 public void copyArea(int x, int y, int width, int height, int dx, 979 int dy) { 980 System.out.println("copyArea"); 981 } 982 983 } 984 | Popular Tags |