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.

PDFImageHandlerGraphics2D.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.Rectangle;
  22. import java.awt.geom.AffineTransform;
  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.impl.ImageGraphics2D;
  28. import org.apache.fop.render.AbstractImageHandlerGraphics2D;
  29. import org.apache.fop.render.ImageHandlerUtil;
  30. import org.apache.fop.render.RenderingContext;
  31. import org.apache.fop.render.pdf.PDFLogicalStructureHandler.MarkedContentInfo;
  32. import org.apache.fop.svg.PDFGraphics2D;
  33. /**
  34. * PDFImageHandler implementation which handles Graphics2D images.
  35. */
  36. public class PDFImageHandlerGraphics2D extends AbstractImageHandlerGraphics2D {
  37. private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
  38. ImageFlavor.GRAPHICS2D,
  39. };
  40. /** {@inheritDoc} */
  41. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  42. throws IOException {
  43. PDFRenderingContext pdfContext = (PDFRenderingContext)context;
  44. PDFContentGenerator generator = pdfContext.getGenerator();
  45. ImageGraphics2D imageG2D = (ImageGraphics2D)image;
  46. float fwidth = pos.width / 1000f;
  47. float fheight = pos.height / 1000f;
  48. float fx = pos.x / 1000f;
  49. float fy = pos.y / 1000f;
  50. // get the 'width' and 'height' attributes of the SVG document
  51. Dimension dim = image.getInfo().getSize().getDimensionMpt();
  52. float imw = (float)dim.getWidth() / 1000f;
  53. float imh = (float)dim.getHeight() / 1000f;
  54. float sx = fwidth / imw;
  55. float sy = fheight / imh;
  56. generator.comment("G2D start");
  57. boolean accessibilityEnabled = context.getUserAgent().isAccessibilityEnabled();
  58. if (accessibilityEnabled) {
  59. MarkedContentInfo mci = pdfContext.getMarkedContentInfo();
  60. generator.saveGraphicsState(mci.tag, mci.mcid);
  61. } else {
  62. generator.saveGraphicsState();
  63. }
  64. generator.updateColor(Color.black, false, null);
  65. generator.updateColor(Color.black, true, null);
  66. //TODO Clip to the image area.
  67. // transform so that the coordinates (0,0) is from the top left
  68. // and positive is down and to the right. (0,0) is where the
  69. // viewBox puts it.
  70. generator.add(sx + " 0 0 " + sy + " " + fx + " " + fy + " cm\n");
  71. final boolean textAsShapes = false;
  72. PDFGraphics2D graphics = new PDFGraphics2D(textAsShapes,
  73. pdfContext.getFontInfo(), generator.getDocument(),
  74. generator.getResourceContext(), pdfContext.getPage().referencePDF(),
  75. "", 0.0f);
  76. graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  77. AffineTransform transform = new AffineTransform();
  78. transform.translate(fx, fy);
  79. generator.getState().concatenate(transform);
  80. graphics.setPaintingState(generator.getState());
  81. graphics.setOutputStream(generator.getOutputStream());
  82. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  83. imageG2D.getGraphics2DImagePainter().paint(graphics, area);
  84. generator.add(graphics.getString());
  85. if (accessibilityEnabled) {
  86. generator.restoreGraphicsStateAccess();
  87. } else {
  88. generator.restoreGraphicsState();
  89. }
  90. generator.comment("G2D end");
  91. }
  92. /** {@inheritDoc} */
  93. public int getPriority() {
  94. return 200;
  95. }
  96. /** {@inheritDoc} */
  97. public Class getSupportedImageClass() {
  98. return ImageGraphics2D.class;
  99. }
  100. /** {@inheritDoc} */
  101. public ImageFlavor[] getSupportedImageFlavors() {
  102. return FLAVORS;
  103. }
  104. /** {@inheritDoc} */
  105. public boolean isCompatible(RenderingContext targetContext, Image image) {
  106. boolean supported = (image == null || image instanceof ImageGraphics2D)
  107. && targetContext instanceof PDFRenderingContext;
  108. if (supported) {
  109. String mode = (String)targetContext.getHint(ImageHandlerUtil.CONVERSION_MODE);
  110. if (ImageHandlerUtil.isConversionModeBitmap(mode)) {
  111. //Disabling this image handler automatically causes a bitmap to be generated
  112. return false;
  113. }
  114. }
  115. return supported;
  116. }
  117. }