1 17 18 19 20 package org.apache.fop.render.pcl; 21 22 import java.awt.Dimension ; 23 import java.awt.geom.AffineTransform ; 24 import java.awt.geom.Rectangle2D ; 25 import java.awt.image.BufferedImage ; 26 import java.io.IOException ; 27 28 import org.apache.commons.io.output.ByteArrayOutputStream; 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.fop.render.AbstractGraphics2DAdapter; 32 import org.apache.fop.render.Graphics2DImagePainter; 33 import org.apache.fop.render.RendererContext; 34 import org.apache.fop.util.UnitConv; 35 import org.apache.xmlgraphics.java2d.GraphicContext; 36 37 40 public class PCLGraphics2DAdapter extends AbstractGraphics2DAdapter { 41 42 43 private static Log log = LogFactory.getLog(PCLGraphics2DAdapter.class); 44 45 48 public PCLGraphics2DAdapter() { 49 } 50 51 52 public void paintImage(Graphics2DImagePainter painter, 53 RendererContext context, 54 int x, int y, int width, int height) throws IOException { 55 PCLRendererContext pclContext = PCLRendererContext.wrapRendererContext(context); 56 PCLRenderer pcl = (PCLRenderer)context.getRenderer(); 57 PCLGenerator gen = pcl.gen; 58 59 Dimension dim = painter.getImageSize(); 61 float imw = (float)dim.getWidth(); 62 float imh = (float)dim.getHeight(); 63 64 boolean painted = false; 65 boolean paintAsBitmap = pclContext.paintAsBitmap(); 66 if (!paintAsBitmap) { 67 ByteArrayOutputStream baout = new ByteArrayOutputStream(); 68 PCLGenerator tempGen = new PCLGenerator(baout, gen.getMaximumBitmapResolution()); 69 try { 70 GraphicContext ctx = (GraphicContext)pcl.getGraphicContext().clone(); 71 72 AffineTransform prepareHPGL2 = new AffineTransform (); 73 prepareHPGL2.scale(0.001, 0.001); 74 ctx.setTransform(prepareHPGL2); 75 76 PCLGraphics2D graphics = new PCLGraphics2D(tempGen); 77 graphics.setGraphicContext(ctx); 78 graphics.setClippingDisabled(pclContext.isClippingDisabled()); 79 Rectangle2D area = new Rectangle2D.Double (0.0, 0.0, imw, imh); 80 painter.paint(graphics, area); 81 82 pcl.saveGraphicsState(); 84 pcl.setCursorPos(x, y); 85 gen.writeCommand("*c" + gen.formatDouble4(width / 100f) + "x" 86 + gen.formatDouble4(height / 100f) + "Y"); 87 gen.writeCommand("*c0T"); 88 gen.enterHPGL2Mode(false); 89 gen.writeText("\nIN;"); 90 gen.writeText("SP1;"); 91 double scale = imw / UnitConv.mm2pt(imw * 0.025); 93 gen.writeText("SC0," + gen.formatDouble4(scale) 94 + ",0,-" + gen.formatDouble4(scale) + ",2;"); 95 gen.writeText("IR0,100,0,100;"); 96 gen.writeText("PU;PA0,0;\n"); 97 baout.writeTo(gen.getOutputStream()); gen.writeText("\n"); 99 100 gen.enterPCLMode(false); 101 pcl.restoreGraphicsState(); 102 painted = true; 103 } catch (UnsupportedOperationException uoe) { 104 log.debug( 105 "Cannot paint graphic natively. Falling back to bitmap painting. Reason: " 106 + uoe.getMessage()); 107 } 108 } 109 110 if (!painted) { 111 int resolution = (int)Math.round(context.getUserAgent().getTargetResolution()); 113 BufferedImage bi = paintToBufferedImage(painter, pclContext, resolution, true, false); 114 115 pcl.setCursorPos(x, y); 116 gen.paintBitmap(bi, new Dimension (width, height), pclContext.isSourceTransparency()); 117 } 118 } 119 120 } 121 | Popular Tags |