diff options
Diffstat (limited to 'src/sandbox/org/apache/fop/render/pcl/PCLGraphics2DAdapter.java')
-rw-r--r-- | src/sandbox/org/apache/fop/render/pcl/PCLGraphics2DAdapter.java | 128 |
1 files changed, 69 insertions, 59 deletions
diff --git a/src/sandbox/org/apache/fop/render/pcl/PCLGraphics2DAdapter.java b/src/sandbox/org/apache/fop/render/pcl/PCLGraphics2DAdapter.java index 268cc24ec..7f2c9d6cf 100644 --- a/src/sandbox/org/apache/fop/render/pcl/PCLGraphics2DAdapter.java +++ b/src/sandbox/org/apache/fop/render/pcl/PCLGraphics2DAdapter.java @@ -27,6 +27,9 @@ import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import java.io.IOException; +import org.apache.commons.io.output.ByteArrayOutputStream; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.apache.fop.render.Graphics2DAdapter; import org.apache.fop.render.Graphics2DImagePainter; import org.apache.fop.render.RendererContext; @@ -38,6 +41,9 @@ import org.apache.xmlgraphics.java2d.GraphicContext; */ public class PCLGraphics2DAdapter implements Graphics2DAdapter { + /** logging instance */ + private static Log log = LogFactory.getLog(PCLGraphics2DAdapter.class); + /** * Main constructor */ @@ -57,80 +63,84 @@ public class PCLGraphics2DAdapter implements Graphics2DAdapter { float imw = (float)dim.getWidth(); float imh = (float)dim.getHeight(); + boolean painted = false; boolean paintAsBitmap = pclContext.paintAsBitmap(); - if (paintAsBitmap) { + if (!paintAsBitmap) { + ByteArrayOutputStream baout = new ByteArrayOutputStream(); + PCLGenerator tempGen = new PCLGenerator(baout); + try { + GraphicContext ctx = (GraphicContext)pcl.getGraphicContext().clone(); + + AffineTransform prepareHPGL2 = new AffineTransform(); + prepareHPGL2.scale(0.001, 0.001); + ctx.setTransform(prepareHPGL2); + + PCLGraphics2D graphics = new PCLGraphics2D(tempGen); + graphics.setGraphicContext(ctx); + Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh); + painter.paint(graphics, area); + + //If we arrive here, the graphic is natively paintable, so write the graphic + pcl.saveGraphicsState(); + pcl.setCursorPos(x, y); + gen.writeCommand("*c" + gen.formatDouble4(width / 100f) + "x" + + gen.formatDouble4(height / 100f) + "Y"); + gen.writeCommand("*c0T"); + gen.enterHPGL2Mode(false); + gen.writeText("\nIN;"); + gen.writeText("SP1;"); + //One Plotter unit is 0.025mm! + double scale = imw / UnitConv.mm2pt(imw * 0.025); + gen.writeText("SC0," + gen.formatDouble4(scale) + + ",0,-" + gen.formatDouble4(scale) + ",2;"); + gen.writeText("IR0,100,0,100;"); + gen.writeText("PU;PA0,0;\n"); + baout.writeTo(gen.getOutputStream()); //Buffer is written to output stream + gen.writeText("\n"); + + gen.enterPCLMode(false); + pcl.restoreGraphicsState(); + painted = true; + } catch (UnsupportedOperationException uoe) { + log.debug( + "Cannot paint graphic natively. Falling back to bitmap painting. Reason: " + + uoe.getMessage()); + } + } + + if (!painted) { int resolution = 300; //TODO not hard-coded, please! int bmw = UnitConv.mpt2px(pclContext.getWidth(), resolution); int bmh = UnitConv.mpt2px(pclContext.getHeight(), resolution); BufferedImage bi = new BufferedImage( bmw, bmh, - BufferedImage.TYPE_INT_RGB); + BufferedImage.TYPE_BYTE_GRAY); Graphics2D g2d = bi.createGraphics(); try { g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, - RenderingHints.VALUE_ANTIALIAS_OFF); - g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, - RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); - g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, - RenderingHints.VALUE_FRACTIONALMETRICS_OFF); - g2d.setRenderingHint(RenderingHints.KEY_RENDERING, - RenderingHints.VALUE_RENDER_QUALITY); - g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, - RenderingHints.VALUE_COLOR_RENDER_QUALITY); - g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); - g2d.setRenderingHint(RenderingHints.KEY_DITHERING, - RenderingHints.VALUE_DITHER_ENABLE); - g2d.setBackground(Color.white); - g2d.setColor(Color.black); - g2d.clearRect(0, 0, bmw, bmh); - double sx = (double)bmw / pclContext.getWidth() * 1000; - double sy = (double)bmh / pclContext.getHeight() * 1000; - g2d.scale(sx, sy); + RenderingHints.VALUE_ANTIALIAS_OFF); + g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, + RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); + g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, + RenderingHints.VALUE_FRACTIONALMETRICS_ON); + + g2d.setBackground(Color.white); + g2d.setColor(Color.black); + g2d.clearRect(0, 0, bmw, bmh); + double sx = (double)bmw / pclContext.getWidth(); + double sy = (double)bmh / pclContext.getHeight(); + g2d.scale(sx, sy); - //Paint the SVG on the BufferedImage - Rectangle2D area = new Rectangle2D.Double( - 0.0, 0.0, pclContext.getWidth(), pclContext.getHeight()); - painter.paint(g2d, area); + //Paint the SVG on the BufferedImage + Rectangle2D area = new Rectangle2D.Double( + 0.0, 0.0, pclContext.getWidth(), pclContext.getHeight()); + painter.paint(g2d, area); } finally { g2d.dispose(); } - pcl.moveTo(x, y); + pcl.setCursorPos(x, y); gen.paintBitmap(bi, resolution); - } else { - pcl.saveGraphicsState(); - GraphicContext ctx = (GraphicContext)pcl.getGraphicContext().clone(); - - // Clip to the image area. - //gen.writeln("newpath"); - //gen.defineRect(fx, fy, fwidth, fheight); - //gen.writeln("clip"); - - AffineTransform prepareHPGL2 = new AffineTransform(); - //prepareHPGL2.scale(1, 1); - ctx.setTransform(prepareHPGL2); - - pcl.moveTo(x, y); - gen.writeCommand("*c" + gen.formatDouble4(width / 100f) + "x" - + gen.formatDouble4(height / 100f) + "Y"); - gen.writeCommand("*c0T"); - gen.writeCommand("%0B"); - gen.writeText("IN;"); - gen.writeText("SP1;"); - //One Plotter unit is 0.025mm! - double scale = imw / UnitConv.mm2pt(imw * 0.025); - gen.writeText("SC0," + gen.formatDouble4(scale) - + ",0,-" + gen.formatDouble4(scale) + ",2;"); - gen.writeText("IR0,100,0,100;"); - gen.writeText("PU;PA0,0;"); - PCLGraphics2D graphics = new PCLGraphics2D(gen); - graphics.setGraphicContext(ctx); - Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh); - painter.paint(graphics, area); - - gen.writeCommand("%0A"); - pcl.restoreGraphicsState(); } } |