You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PCLGraphics2DAdapter.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.pcl;
  19. import java.awt.Dimension;
  20. import java.awt.geom.AffineTransform;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.io.IOException;
  24. import org.apache.commons.io.output.ByteArrayOutputStream;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.xmlgraphics.java2d.GraphicContext;
  28. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  29. import org.apache.fop.render.AbstractGraphics2DAdapter;
  30. import org.apache.fop.render.RendererContext;
  31. import org.apache.xmlgraphics.util.UnitConv;
  32. /**
  33. * Graphics2DAdapter implementation for PCL and HP GL/2.
  34. */
  35. public class PCLGraphics2DAdapter extends AbstractGraphics2DAdapter {
  36. /** logging instance */
  37. private static Log log = LogFactory.getLog(PCLGraphics2DAdapter.class);
  38. /**
  39. * Main constructor
  40. */
  41. public PCLGraphics2DAdapter() {
  42. }
  43. /** {@inheritDoc} */
  44. public void paintImage(Graphics2DImagePainter painter,
  45. RendererContext context,
  46. int x, int y, int width, int height) throws IOException {
  47. PCLRendererContext pclContext = PCLRendererContext.wrapRendererContext(context);
  48. PCLRenderer pcl = (PCLRenderer)context.getRenderer();
  49. PCLGenerator gen = pcl.gen;
  50. // get the 'width' and 'height' attributes of the image/document
  51. Dimension dim = painter.getImageSize();
  52. float imw = (float)dim.getWidth();
  53. float imh = (float)dim.getHeight();
  54. boolean painted = false;
  55. boolean paintAsBitmap = pclContext.paintAsBitmap();
  56. if (!paintAsBitmap) {
  57. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  58. PCLGenerator tempGen = new PCLGenerator(baout, gen.getMaximumBitmapResolution());
  59. try {
  60. GraphicContext ctx = (GraphicContext)pcl.getGraphicContext().clone();
  61. AffineTransform prepareHPGL2 = new AffineTransform();
  62. prepareHPGL2.scale(0.001, 0.001);
  63. ctx.setTransform(prepareHPGL2);
  64. PCLGraphics2D graphics = new PCLGraphics2D(tempGen);
  65. graphics.setGraphicContext(ctx);
  66. graphics.setClippingDisabled(pclContext.isClippingDisabled());
  67. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  68. painter.paint(graphics, area);
  69. //If we arrive here, the graphic is natively paintable, so write the graphic
  70. pcl.saveGraphicsState();
  71. pcl.setCursorPos(x, y);
  72. gen.writeCommand("*c" + gen.formatDouble4(width / 100f) + "x"
  73. + gen.formatDouble4(height / 100f) + "Y");
  74. gen.writeCommand("*c0T");
  75. gen.enterHPGL2Mode(false);
  76. gen.writeText("\nIN;");
  77. gen.writeText("SP1;");
  78. //One Plotter unit is 0.025mm!
  79. double scale = imw / UnitConv.mm2pt(imw * 0.025);
  80. gen.writeText("SC0," + gen.formatDouble4(scale)
  81. + ",0,-" + gen.formatDouble4(scale) + ",2;");
  82. gen.writeText("IR0,100,0,100;");
  83. gen.writeText("PU;PA0,0;\n");
  84. baout.writeTo(gen.getOutputStream()); //Buffer is written to output stream
  85. gen.writeText("\n");
  86. gen.enterPCLMode(false);
  87. pcl.restoreGraphicsState();
  88. painted = true;
  89. } catch (UnsupportedOperationException uoe) {
  90. log.debug(
  91. "Cannot paint graphic natively. Falling back to bitmap painting. Reason: "
  92. + uoe.getMessage());
  93. }
  94. }
  95. if (!painted) {
  96. //Fallback solution: Paint to a BufferedImage
  97. int resolution = Math.round(context.getUserAgent().getTargetResolution());
  98. BufferedImage bi = paintToBufferedImage(painter, pclContext,
  99. resolution, !pclContext.isColorCanvas(), false);
  100. pcl.setCursorPos(x, y);
  101. gen.paintBitmap(bi, new Dimension(width, height), pclContext.isSourceTransparency());
  102. }
  103. }
  104. }