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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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.io.IOException;
  23. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  24. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  25. import org.apache.xmlgraphics.ps.PSGenerator;
  26. import org.apache.fop.render.Graphics2DAdapter;
  27. import org.apache.fop.render.RendererContext;
  28. /**
  29. * Graphics2DAdapter implementation for PostScript.
  30. */
  31. public class PSGraphics2DAdapter implements Graphics2DAdapter {
  32. private PSGenerator gen;
  33. private boolean clip = true;
  34. /**
  35. * Main constructor
  36. * @param renderer the Renderer instance to which this instance belongs
  37. */
  38. public PSGraphics2DAdapter(PSRenderer renderer) {
  39. this(renderer.gen, true);
  40. }
  41. /**
  42. * Constructor for use without a PSRenderer instance.
  43. * @param gen the PostScript generator
  44. * @param clip true if the image should be clipped
  45. */
  46. public PSGraphics2DAdapter(PSGenerator gen, boolean clip) {
  47. this.gen = gen;
  48. this.clip = clip;
  49. }
  50. /** {@inheritDoc} */
  51. public void paintImage(Graphics2DImagePainter painter,
  52. RendererContext context,
  53. int x, int y, int width, int height) throws IOException {
  54. float fwidth = width / 1000f;
  55. float fheight = height / 1000f;
  56. float fx = x / 1000f;
  57. float fy = y / 1000f;
  58. // get the 'width' and 'height' attributes of the SVG document
  59. Dimension dim = painter.getImageSize();
  60. float imw = (float)dim.getWidth() / 1000f;
  61. float imh = (float)dim.getHeight() / 1000f;
  62. float sx = fwidth / (float)imw;
  63. float sy = fheight / (float)imh;
  64. gen.commentln("%FOPBeginGraphics2D");
  65. gen.saveGraphicsState();
  66. if (clip) {
  67. // Clip to the image area.
  68. gen.writeln("newpath");
  69. gen.defineRect(fx, fy, fwidth, fheight);
  70. gen.writeln("clip");
  71. }
  72. // transform so that the coordinates (0,0) is from the top left
  73. // and positive is down and to the right. (0,0) is where the
  74. // viewBox puts it.
  75. gen.concatMatrix(sx, 0, 0, sy, fx, fy);
  76. final boolean textAsShapes = false;
  77. PSGraphics2D graphics = new PSGraphics2D(textAsShapes, gen);
  78. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  79. AffineTransform transform = new AffineTransform();
  80. // scale to viewbox
  81. transform.translate(fx, fy);
  82. gen.getCurrentState().concatMatrix(transform);
  83. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  84. painter.paint(graphics, area);
  85. gen.restoreGraphicsState();
  86. gen.commentln("%FOPEndGraphics2D");
  87. }
  88. }