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.

PSGraphics2DAdapter.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.ps;
  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 java.util.Map;
  25. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  26. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  27. import org.apache.xmlgraphics.ps.PSGenerator;
  28. import org.apache.xmlgraphics.util.QName;
  29. import org.apache.fop.fo.extensions.ExtensionElementMapping;
  30. import org.apache.fop.render.AbstractGraphics2DAdapter;
  31. import org.apache.fop.render.Graphics2DAdapter;
  32. import org.apache.fop.render.RendererContext;
  33. import org.apache.fop.render.RendererContextConstants;
  34. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  35. import org.apache.fop.render.pdf.PDFRenderer;
  36. /**
  37. * Graphics2DAdapter implementation for PostScript.
  38. */
  39. public class PSGraphics2DAdapter extends AbstractGraphics2DAdapter {
  40. /** Qualified name for the "conversion-mode" extension attribute. */
  41. protected static final QName CONVERSION_MODE = new QName(
  42. ExtensionElementMapping.URI, null, "conversion-mode");
  43. private PSGenerator gen;
  44. private boolean clip = true;
  45. /**
  46. * Main constructor
  47. * @param renderer the Renderer instance to which this instance belongs
  48. */
  49. public PSGraphics2DAdapter(PSRenderer renderer) {
  50. this(renderer.gen, true);
  51. }
  52. /**
  53. * Constructor for use without a PSRenderer instance.
  54. * @param gen the PostScript generator
  55. * @param clip true if the image should be clipped
  56. */
  57. public PSGraphics2DAdapter(PSGenerator gen, boolean clip) {
  58. this.gen = gen;
  59. this.clip = clip;
  60. }
  61. /** {@inheritDoc} */
  62. public void paintImage(Graphics2DImagePainter painter,
  63. RendererContext context,
  64. int x, int y, int width, int height) throws IOException {
  65. float fwidth = width / 1000f;
  66. float fheight = height / 1000f;
  67. float fx = x / 1000f;
  68. float fy = y / 1000f;
  69. // get the 'width' and 'height' attributes of the SVG document
  70. Dimension dim = painter.getImageSize();
  71. float imw = (float)dim.getWidth() / 1000f;
  72. float imh = (float)dim.getHeight() / 1000f;
  73. boolean paintAsBitmap = false;
  74. if (context != null) {
  75. Map foreign = (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES);
  76. paintAsBitmap = (foreign != null
  77. && "bitmap".equalsIgnoreCase((String)foreign.get(CONVERSION_MODE)));
  78. }
  79. float sx = paintAsBitmap ? 1.0f : (fwidth / (float)imw);
  80. float sy = paintAsBitmap ? 1.0f : (fheight / (float)imh);
  81. gen.commentln("%FOPBeginGraphics2D");
  82. gen.saveGraphicsState();
  83. if (clip) {
  84. // Clip to the image area.
  85. gen.writeln("newpath");
  86. gen.defineRect(fx, fy, fwidth, fheight);
  87. gen.writeln("clip");
  88. }
  89. // transform so that the coordinates (0,0) is from the top left
  90. // and positive is down and to the right. (0,0) is where the
  91. // viewBox puts it.
  92. gen.concatMatrix(sx, 0, 0, sy, fx, fy);
  93. final boolean textAsShapes = false;
  94. PSGraphics2D graphics = new PSGraphics2D(textAsShapes, gen);
  95. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  96. AffineTransform transform = new AffineTransform();
  97. // scale to viewbox
  98. transform.translate(fx, fy);
  99. gen.getCurrentState().concatMatrix(transform);
  100. if (paintAsBitmap) {
  101. //Fallback solution: Paint to a BufferedImage
  102. int resolution = (int)Math.round(context.getUserAgent().getTargetResolution());
  103. RendererContextWrapper ctx = RendererContext.wrapRendererContext(context);
  104. BufferedImage bi = paintToBufferedImage(painter, ctx, resolution, false, false);
  105. float scale = PDFRenderer.NORMAL_PDF_RESOLUTION
  106. / context.getUserAgent().getTargetResolution();
  107. graphics.drawImage(bi, new AffineTransform(scale, 0, 0, scale, 0, 0), null);
  108. } else {
  109. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  110. painter.paint(graphics, area);
  111. }
  112. gen.restoreGraphicsState();
  113. gen.commentln("%FOPEndGraphics2D");
  114. }
  115. }