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.

PSImageHandlerGraphics2D.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Dimension2D;
  23. import java.awt.geom.Rectangle2D;
  24. import java.io.IOException;
  25. import org.apache.xmlgraphics.image.loader.Image;
  26. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  27. import org.apache.xmlgraphics.image.loader.ImageInfo;
  28. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  29. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  30. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  31. import org.apache.xmlgraphics.ps.FormGenerator;
  32. import org.apache.xmlgraphics.ps.PSGenerator;
  33. import org.apache.xmlgraphics.ps.PSProcSets;
  34. import org.apache.fop.render.RenderingContext;
  35. /**
  36. * Image handler implementation which handles vector graphics (Java2D) for PostScript output.
  37. */
  38. public class PSImageHandlerGraphics2D implements PSImageHandler {
  39. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  40. ImageFlavor.GRAPHICS2D
  41. };
  42. /** {@inheritDoc} */
  43. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  44. throws IOException {
  45. PSRenderingContext psContext = (PSRenderingContext)context;
  46. PSGenerator gen = psContext.getGenerator();
  47. ImageGraphics2D imageG2D = (ImageGraphics2D)image;
  48. Graphics2DImagePainter painter = imageG2D.getGraphics2DImagePainter();
  49. float fx = (float)pos.getX() / 1000f;
  50. float fy = (float)pos.getY() / 1000f;
  51. float fwidth = (float)pos.getWidth() / 1000f;
  52. float fheight = (float)pos.getHeight() / 1000f;
  53. // get the 'width' and 'height' attributes of the SVG document
  54. Dimension dim = painter.getImageSize();
  55. float imw = (float)dim.getWidth() / 1000f;
  56. float imh = (float)dim.getHeight() / 1000f;
  57. float sx = fwidth / (float)imw;
  58. float sy = fheight / (float)imh;
  59. gen.commentln("%FOPBeginGraphics2D");
  60. gen.saveGraphicsState();
  61. final boolean clip = false;
  62. if (clip) {
  63. // Clip to the image area.
  64. gen.writeln("newpath");
  65. gen.defineRect(fx, fy, fwidth, fheight);
  66. gen.writeln("clip");
  67. }
  68. // transform so that the coordinates (0,0) is from the top left
  69. // and positive is down and to the right. (0,0) is where the
  70. // viewBox puts it.
  71. gen.concatMatrix(sx, 0, 0, sy, fx, fy);
  72. final boolean textAsShapes = false;
  73. PSGraphics2D graphics = new PSGraphics2D(textAsShapes, gen);
  74. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  75. AffineTransform transform = new AffineTransform();
  76. // scale to viewbox
  77. transform.translate(fx, fy);
  78. gen.getCurrentState().concatMatrix(transform);
  79. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  80. painter.paint(graphics, area);
  81. gen.restoreGraphicsState();
  82. gen.commentln("%FOPEndGraphics2D");
  83. }
  84. /** {@inheritDoc} */
  85. public void generateForm(RenderingContext context, Image image, PSImageFormResource form)
  86. throws IOException {
  87. PSRenderingContext psContext = (PSRenderingContext)context;
  88. PSGenerator gen = psContext.getGenerator();
  89. final ImageGraphics2D imageG2D = (ImageGraphics2D)image;
  90. ImageInfo info = image.getInfo();
  91. String imageDescription = info.getMimeType() + " " + info.getOriginalURI();
  92. final Dimension2D dimensionsPt = info.getSize().getDimensionPt();
  93. final Dimension2D dimensionsMpt = info.getSize().getDimensionMpt();
  94. FormGenerator formGen = new FormGenerator(
  95. form.getName(), imageDescription, dimensionsPt) {
  96. protected void generatePaintProc(PSGenerator gen)
  97. throws IOException {
  98. gen.getResourceTracker().notifyResourceUsageOnPage(
  99. PSProcSets.EPS_PROCSET);
  100. gen.writeln("BeginEPSF");
  101. PSGraphics2DAdapter adapter = new PSGraphics2DAdapter(gen, false);
  102. adapter.paintImage(imageG2D.getGraphics2DImagePainter(),
  103. null,
  104. 0, 0,
  105. (int)Math.round(dimensionsMpt.getWidth()),
  106. (int)Math.round(dimensionsMpt.getHeight()));
  107. gen.writeln("EndEPSF");
  108. }
  109. };
  110. formGen.generate(gen);
  111. }
  112. /** {@inheritDoc} */
  113. public int getPriority() {
  114. return 200;
  115. }
  116. /** {@inheritDoc} */
  117. public Class getSupportedImageClass() {
  118. return ImageGraphics2D.class;
  119. }
  120. /** {@inheritDoc} */
  121. public ImageFlavor[] getSupportedImageFlavors() {
  122. return FLAVORS;
  123. }
  124. /** {@inheritDoc} */
  125. public boolean isCompatible(RenderingContext targetContext, Image image) {
  126. if (targetContext instanceof PSRenderingContext) {
  127. return (image == null || image instanceof ImageGraphics2D);
  128. }
  129. return false;
  130. }
  131. }