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.

PDFGraphics2DAdapter.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.pdf;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics2D;
  22. import java.awt.RenderingHints;
  23. import java.awt.geom.AffineTransform;
  24. import java.awt.geom.Rectangle2D;
  25. import java.awt.image.BufferedImage;
  26. import java.io.IOException;
  27. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  28. import org.apache.fop.render.AbstractGraphics2DAdapter;
  29. import org.apache.fop.render.RendererContext;
  30. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  31. import org.apache.fop.svg.PDFGraphics2D;
  32. /**
  33. * Graphics2DAdapter implementation for PDF.
  34. */
  35. public class PDFGraphics2DAdapter extends AbstractGraphics2DAdapter {
  36. private PDFRenderer renderer;
  37. /**
  38. * Main constructor
  39. * @param renderer the Renderer instance to which this instance belongs
  40. */
  41. public PDFGraphics2DAdapter(PDFRenderer renderer) {
  42. this.renderer = renderer;
  43. }
  44. /** {@inheritDoc} */
  45. public void paintImage(Graphics2DImagePainter painter,
  46. RendererContext context,
  47. int x, int y, int width, int height) throws IOException {
  48. PDFSVGHandler.PDFInfo pdfInfo = PDFSVGHandler.getPDFInfo(context);
  49. float fwidth = width / 1000f;
  50. float fheight = height / 1000f;
  51. float fx = x / 1000f;
  52. float fy = y / 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. renderer.saveGraphicsState();
  60. renderer.setColor(Color.black, false, null);
  61. renderer.setColor(Color.black, true, null);
  62. //TODO Clip to the image area.
  63. // transform so that the coordinates (0,0) is from the top left
  64. // and positive is down and to the right. (0,0) is where the
  65. // viewBox puts it.
  66. renderer.currentStream.add(sx + " 0 0 " + sy + " " + fx + " "
  67. + fy + " cm\n");
  68. final boolean textAsShapes = false;
  69. if (pdfInfo.pdfContext == null) {
  70. pdfInfo.pdfContext = pdfInfo.pdfPage;
  71. }
  72. PDFGraphics2D graphics = new PDFGraphics2D(textAsShapes,
  73. pdfInfo.fi, pdfInfo.pdfDoc,
  74. pdfInfo.pdfContext, pdfInfo.pdfPage.referencePDF(),
  75. pdfInfo.currentFontName,
  76. pdfInfo.currentFontSize);
  77. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  78. AffineTransform transform = new AffineTransform();
  79. transform.translate(fx, fy);
  80. pdfInfo.pdfState.concatenate(transform);
  81. graphics.setPDFState(pdfInfo.pdfState);
  82. graphics.setOutputStream(pdfInfo.outputStream);
  83. if (pdfInfo.paintAsBitmap) {
  84. //Fallback solution: Paint to a BufferedImage
  85. int resolution = (int)Math.round(context.getUserAgent().getTargetResolution());
  86. RendererContextWrapper ctx = RendererContext.wrapRendererContext(context);
  87. BufferedImage bi = paintToBufferedImage(painter, ctx, resolution, false, false);
  88. float scale = PDFRenderer.NORMAL_PDF_RESOLUTION
  89. / context.getUserAgent().getTargetResolution();
  90. graphics.drawImage(bi, new AffineTransform(scale, 0, 0, scale, 0, 0), null);
  91. } else {
  92. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  93. painter.paint(graphics, area);
  94. }
  95. pdfInfo.currentStream.add(graphics.getString());
  96. renderer.restoreGraphicsState();
  97. }
  98. /** {@inheritDoc} */
  99. protected void setRenderingHintsForBufferedImage(Graphics2D g2d) {
  100. super.setRenderingHintsForBufferedImage(g2d);
  101. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  102. RenderingHints.VALUE_ANTIALIAS_ON);
  103. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  104. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  105. }
  106. }