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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.pdf;
  18. import java.awt.Color;
  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.fop.render.Graphics2DAdapter;
  24. import org.apache.fop.render.Graphics2DImagePainter;
  25. import org.apache.fop.render.RendererContext;
  26. import org.apache.fop.svg.PDFGraphics2D;
  27. /**
  28. * Graphics2DAdapter implementation for PDF.
  29. */
  30. public class PDFGraphics2DAdapter implements Graphics2DAdapter {
  31. private PDFRenderer renderer;
  32. /**
  33. * Main constructor
  34. * @param renderer the Renderer instance to which this instance belongs
  35. */
  36. public PDFGraphics2DAdapter(PDFRenderer renderer) {
  37. this.renderer = renderer;
  38. }
  39. /** @see org.apache.fop.render.Graphics2DAdapter */
  40. public void paintImage(Graphics2DImagePainter painter,
  41. RendererContext context,
  42. int x, int y, int width, int height) throws IOException {
  43. PDFSVGHandler.PDFInfo pdfInfo = PDFSVGHandler.getPDFInfo(context);
  44. float fwidth = width / 1000f;
  45. float fheight = height / 1000f;
  46. float fx = x / 1000f;
  47. float fy = y / 1000f;
  48. // get the 'width' and 'height' attributes of the SVG document
  49. Dimension dim = painter.getImageSize();
  50. float imw = (float)dim.getWidth() / 1000f;
  51. float imh = (float)dim.getHeight() / 1000f;
  52. float sx = fwidth / (float)imw;
  53. float sy = fheight / (float)imh;
  54. renderer.saveGraphicsState();
  55. renderer.setColor(Color.black, false, null);
  56. renderer.setColor(Color.black, true, null);
  57. //TODO Clip to the image area.
  58. // transform so that the coordinates (0,0) is from the top left
  59. // and positive is down and to the right. (0,0) is where the
  60. // viewBox puts it.
  61. renderer.currentStream.add(sx + " 0 0 " + sy + " " + fx + " "
  62. + fy + " cm\n");
  63. final boolean textAsShapes = false;
  64. PDFGraphics2D graphics = new PDFGraphics2D(textAsShapes,
  65. pdfInfo.fi, pdfInfo.pdfDoc,
  66. pdfInfo.pdfContext, pdfInfo.pdfPage.referencePDF(),
  67. renderer.currentFontName,
  68. renderer.currentFontSize);
  69. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  70. AffineTransform transform = new AffineTransform();
  71. transform.translate(fx, fy);
  72. pdfInfo.pdfState.setTransform(transform);
  73. graphics.setPDFState(pdfInfo.pdfState);
  74. graphics.setOutputStream(pdfInfo.outputStream);
  75. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  76. painter.paint(graphics, area);
  77. pdfInfo.currentStream.add(graphics.getString());
  78. renderer.restoreGraphicsState();
  79. pdfInfo.pdfState.pop();
  80. }
  81. }