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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.pcl;
  18. import java.awt.Color;
  19. import java.awt.Dimension;
  20. import java.awt.Graphics2D;
  21. import java.awt.RenderingHints;
  22. import java.awt.geom.AffineTransform;
  23. import java.awt.geom.Rectangle2D;
  24. import java.awt.image.BufferedImage;
  25. import java.io.IOException;
  26. import org.apache.fop.render.Graphics2DAdapter;
  27. import org.apache.fop.render.Graphics2DImagePainter;
  28. import org.apache.fop.render.RendererContext;
  29. import org.apache.fop.util.UnitConv;
  30. import org.apache.xmlgraphics.java2d.GraphicContext;
  31. /**
  32. * Graphics2DAdapter implementation for PCL and HP GL/2.
  33. */
  34. public class PCLGraphics2DAdapter implements Graphics2DAdapter {
  35. /**
  36. * Main constructor
  37. */
  38. public PCLGraphics2DAdapter() {
  39. }
  40. /** @see org.apache.fop.render.Graphics2DAdapter */
  41. public void paintImage(Graphics2DImagePainter painter,
  42. RendererContext context,
  43. int x, int y, int width, int height) throws IOException {
  44. PCLRendererContext pclContext = PCLRendererContext.wrapRendererContext(context);
  45. PCLRenderer pcl = (PCLRenderer)context.getRenderer();
  46. PCLGenerator gen = pcl.gen;
  47. // get the 'width' and 'height' attributes of the SVG document
  48. Dimension dim = painter.getImageSize();
  49. float imw = (float)dim.getWidth();
  50. float imh = (float)dim.getHeight();
  51. boolean paintAsBitmap = pclContext.paintAsBitmap();
  52. if (paintAsBitmap) {
  53. int resolution = 300; //TODO not hard-coded, please!
  54. int bmw = UnitConv.mpt2px(pclContext.getWidth(), resolution);
  55. int bmh = UnitConv.mpt2px(pclContext.getHeight(), resolution);
  56. BufferedImage bi = new BufferedImage(
  57. bmw, bmh,
  58. BufferedImage.TYPE_INT_RGB);
  59. Graphics2D g2d = bi.createGraphics();
  60. try {
  61. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  62. RenderingHints.VALUE_ANTIALIAS_OFF);
  63. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  64. RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
  65. g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
  66. RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
  67. g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
  68. RenderingHints.VALUE_RENDER_QUALITY);
  69. g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
  70. RenderingHints.VALUE_COLOR_RENDER_QUALITY);
  71. g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
  72. RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
  73. g2d.setRenderingHint(RenderingHints.KEY_DITHERING,
  74. RenderingHints.VALUE_DITHER_ENABLE);
  75. g2d.setBackground(Color.white);
  76. g2d.setColor(Color.black);
  77. g2d.clearRect(0, 0, bmw, bmh);
  78. double sx = (double)bmw / pclContext.getWidth() * 1000;
  79. double sy = (double)bmh / pclContext.getHeight() * 1000;
  80. g2d.scale(sx, sy);
  81. //Paint the SVG on the BufferedImage
  82. Rectangle2D area = new Rectangle2D.Double(
  83. 0.0, 0.0, pclContext.getWidth(), pclContext.getHeight());
  84. painter.paint(g2d, area);
  85. } finally {
  86. g2d.dispose();
  87. }
  88. pcl.moveTo(x, y);
  89. gen.paintBitmap(bi, resolution);
  90. } else {
  91. pcl.saveGraphicsState();
  92. GraphicContext ctx = (GraphicContext)pcl.getGraphicContext().clone();
  93. // Clip to the image area.
  94. //gen.writeln("newpath");
  95. //gen.defineRect(fx, fy, fwidth, fheight);
  96. //gen.writeln("clip");
  97. AffineTransform prepareHPGL2 = new AffineTransform();
  98. //prepareHPGL2.scale(1, 1);
  99. ctx.setTransform(prepareHPGL2);
  100. pcl.moveTo(x, y);
  101. gen.writeCommand("*c" + gen.formatDouble4(width / 100f) + "x"
  102. + gen.formatDouble4(height / 100f) + "Y");
  103. gen.writeCommand("*c0T");
  104. gen.writeCommand("%0B");
  105. gen.writeText("IN;");
  106. gen.writeText("SP1;");
  107. //One Plotter unit is 0.025mm!
  108. double scale = imw / UnitConv.mm2pt(imw * 0.025);
  109. gen.writeText("SC0," + gen.formatDouble4(scale)
  110. + ",0,-" + gen.formatDouble4(scale) + ",2;");
  111. gen.writeText("IR0,100,0,100;");
  112. gen.writeText("PU;PA0,0;");
  113. PCLGraphics2D graphics = new PCLGraphics2D(gen);
  114. graphics.setGraphicContext(ctx);
  115. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  116. painter.paint(graphics, area);
  117. gen.writeCommand("%0A");
  118. pcl.restoreGraphicsState();
  119. }
  120. }
  121. }