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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.fop.render.RenderingContext;
  26. import org.apache.xmlgraphics.image.loader.Image;
  27. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  28. import org.apache.xmlgraphics.image.loader.ImageInfo;
  29. import org.apache.xmlgraphics.image.loader.impl.ImageGraphics2D;
  30. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  31. import org.apache.xmlgraphics.java2d.ps.PSGraphics2D;
  32. import org.apache.xmlgraphics.ps.FormGenerator;
  33. import org.apache.xmlgraphics.ps.PSGenerator;
  34. import org.apache.xmlgraphics.ps.PSProcSets;
  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, final 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. FormGenerator formGen = buildFormGenerator(gen.getPSLevel(), form, info, imageG2D);
  92. formGen.generate(gen);
  93. }
  94. /** {@inheritDoc} */
  95. public int getPriority() {
  96. return 200;
  97. }
  98. /** {@inheritDoc} */
  99. public Class getSupportedImageClass() {
  100. return ImageGraphics2D.class;
  101. }
  102. /** {@inheritDoc} */
  103. public ImageFlavor[] getSupportedImageFlavors() {
  104. return FLAVORS;
  105. }
  106. /** {@inheritDoc} */
  107. public boolean isCompatible(RenderingContext targetContext, Image image) {
  108. if (targetContext instanceof PSRenderingContext) {
  109. return (image == null || image instanceof ImageGraphics2D);
  110. }
  111. return false;
  112. }
  113. private FormGenerator buildFormGenerator(int psLanguageLevel, final PSImageFormResource form,
  114. final ImageInfo info, final ImageGraphics2D imageG2D) {
  115. String imageDescription = info.getMimeType() + " " + info.getOriginalURI();
  116. final Dimension2D dimensionsPt = info.getSize().getDimensionPt();
  117. final Dimension2D dimensionsMpt = info.getSize().getDimensionMpt();
  118. FormGenerator formGen;
  119. if (psLanguageLevel <= 2) {
  120. formGen = new EPSFormGenerator(form.getName(), imageDescription, dimensionsPt) {
  121. @Override
  122. void doGeneratePaintProc(PSGenerator gen) throws IOException {
  123. paintImageG2D(imageG2D, dimensionsMpt, gen);
  124. }
  125. };
  126. } else {
  127. formGen = new EPSFormGenerator(form.getName(), imageDescription, dimensionsPt) {
  128. @Override
  129. protected void generateAdditionalDataStream(PSGenerator gen) throws IOException {
  130. gen.writeln("/" + form.getName() + ":Data currentfile <<");
  131. gen.writeln(" /Filter /SubFileDecode");
  132. gen.writeln(" /DecodeParms << /EODCount 0 /EODString (%FOPEndOfData) >>");
  133. gen.writeln(">> /ReusableStreamDecode filter");
  134. paintImageG2D(imageG2D, dimensionsMpt, gen);
  135. gen.writeln("%FOPEndOfData");
  136. gen.writeln("def");
  137. }
  138. @Override
  139. void doGeneratePaintProc(PSGenerator gen) throws IOException {
  140. gen.writeln(form.getName() + ":Data 0 setfileposition");
  141. gen.writeln(form.getName() + ":Data cvx exec");
  142. }
  143. };
  144. }
  145. return formGen;
  146. }
  147. private static abstract class EPSFormGenerator extends FormGenerator {
  148. EPSFormGenerator(String formName, String title, Dimension2D dimensions) {
  149. super(formName, title, dimensions);
  150. }
  151. protected void paintImageG2D(final ImageGraphics2D imageG2D, Dimension2D dimensionsMpt,
  152. PSGenerator gen) throws IOException {
  153. PSGraphics2DAdapter adapter = new PSGraphics2DAdapter(gen, false);
  154. adapter.paintImage(imageG2D.getGraphics2DImagePainter(),
  155. null,
  156. 0, 0,
  157. (int) Math.round(dimensionsMpt.getWidth()),
  158. (int) Math.round(dimensionsMpt.getHeight()));
  159. }
  160. @Override
  161. protected final void generatePaintProc(PSGenerator gen) throws IOException {
  162. gen.getResourceTracker().notifyResourceUsageOnPage(
  163. PSProcSets.EPS_PROCSET);
  164. gen.writeln("BeginEPSF");
  165. doGeneratePaintProc(gen);
  166. gen.writeln("EndEPSF");
  167. }
  168. abstract void doGeneratePaintProc(PSGenerator gen) throws IOException;
  169. }
  170. }