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.0KB

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