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.

PCLImageHandlerGraphics2D.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Point2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.io.IOException;
  25. import org.apache.commons.io.output.ByteArrayOutputStream;
  26. import org.apache.commons.logging.Log;
  27. import org.apache.commons.logging.LogFactory;
  28. import org.apache.xmlgraphics.image.loader.Image;
  29. import org.apache.xmlgraphics.image.loader.ImageException;
  30. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  31. import org.apache.xmlgraphics.image.loader.ImageManager;
  32. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  33. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  34. import org.apache.xmlgraphics.java2d.GraphicContext;
  35. import org.apache.xmlgraphics.util.UnitConv;
  36. import org.apache.fop.apps.FOUserAgent;
  37. import org.apache.fop.render.ImageHandler;
  38. import org.apache.fop.render.ImageHandlerUtil;
  39. import org.apache.fop.render.RenderingContext;
  40. /**
  41. * Image handler implementation that paints Graphics2D images in PCL. Since PCL is limited in its
  42. * vector graphics capabilities, there's a fallback built in that switches to bitmap painting
  43. * if an advanced feature is encountered.
  44. */
  45. public class PCLImageHandlerGraphics2D implements ImageHandler {
  46. /** logging instance */
  47. private static Log log = LogFactory.getLog(PCLImageHandlerGraphics2D.class);
  48. /** {@inheritDoc} */
  49. public int getPriority() {
  50. return 400;
  51. }
  52. /** {@inheritDoc} */
  53. public Class getSupportedImageClass() {
  54. return ImageGraphics2D.class;
  55. }
  56. /** {@inheritDoc} */
  57. public ImageFlavor[] getSupportedImageFlavors() {
  58. return new ImageFlavor[] {
  59. ImageFlavor.GRAPHICS2D
  60. };
  61. }
  62. /** {@inheritDoc} */
  63. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  64. throws IOException {
  65. PCLRenderingContext pclContext = (PCLRenderingContext)context;
  66. ImageGraphics2D imageG2D = (ImageGraphics2D)image;
  67. Dimension imageDim = imageG2D.getSize().getDimensionMpt();
  68. PCLGenerator gen = pclContext.getPCLGenerator();
  69. Point2D transPoint = pclContext.transformedPoint(pos.x, pos.y);
  70. gen.setCursorPos(transPoint.getX(), transPoint.getY());
  71. boolean painted = false;
  72. ByteArrayOutputStream baout = new ByteArrayOutputStream();
  73. PCLGenerator tempGen = new PCLGenerator(baout, gen.getMaximumBitmapResolution());
  74. tempGen.setDitheringQuality(gen.getDitheringQuality());
  75. try {
  76. GraphicContext ctx = (GraphicContext)pclContext.getGraphicContext().clone();
  77. AffineTransform prepareHPGL2 = new AffineTransform();
  78. prepareHPGL2.scale(0.001, 0.001);
  79. ctx.setTransform(prepareHPGL2);
  80. PCLGraphics2D graphics = new PCLGraphics2D(tempGen);
  81. graphics.setGraphicContext(ctx);
  82. graphics.setClippingDisabled(false /*pclContext.isClippingDisabled()*/);
  83. Rectangle2D area = new Rectangle2D.Double(
  84. 0.0, 0.0, imageDim.getWidth(), imageDim.getHeight());
  85. imageG2D.getGraphics2DImagePainter().paint(graphics, area);
  86. //If we arrive here, the graphic is natively paintable, so write the graphic
  87. gen.writeCommand("*c" + gen.formatDouble4(pos.width / 100f) + "x"
  88. + gen.formatDouble4(pos.height / 100f) + "Y");
  89. gen.writeCommand("*c0T");
  90. gen.enterHPGL2Mode(false);
  91. gen.writeText("\nIN;");
  92. gen.writeText("SP1;");
  93. //One Plotter unit is 0.025mm!
  94. double scale = imageDim.getWidth() / UnitConv.mm2pt(imageDim.getWidth() * 0.025);
  95. gen.writeText("SC0," + gen.formatDouble4(scale)
  96. + ",0,-" + gen.formatDouble4(scale) + ",2;");
  97. gen.writeText("IR0,100,0,100;");
  98. gen.writeText("PU;PA0,0;\n");
  99. baout.writeTo(gen.getOutputStream()); //Buffer is written to output stream
  100. gen.writeText("\n");
  101. gen.enterPCLMode(false);
  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. if (!painted) {
  109. //Fallback solution: Paint to a BufferedImage
  110. FOUserAgent ua = context.getUserAgent();
  111. ImageManager imageManager = ua.getImageManager();
  112. ImageRendered imgRend;
  113. try {
  114. imgRend = (ImageRendered)imageManager.convertImage(
  115. imageG2D, new ImageFlavor[] {ImageFlavor.RENDERED_IMAGE}/*, hints*/);
  116. } catch (ImageException e) {
  117. throw new IOException(
  118. "Image conversion error while converting the image to a bitmap"
  119. + " as a fallback measure: " + e.getMessage());
  120. }
  121. gen.paintBitmap(imgRend.getRenderedImage(), new Dimension(pos.width, pos.height),
  122. pclContext.isSourceTransparencyEnabled(), pclContext.getPCLUtil());
  123. }
  124. }
  125. /** {@inheritDoc} */
  126. public boolean isCompatible(RenderingContext targetContext, Image image) {
  127. boolean supported = (image == null || image instanceof ImageGraphics2D)
  128. && targetContext instanceof PCLRenderingContext;
  129. if (supported) {
  130. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  131. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  132. //Disabling this image handler automatically causes a bitmap to be generated
  133. return false;
  134. }
  135. }
  136. return supported;
  137. }
  138. }