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.

AFPGraphics2DAdapter.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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.afp;
  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 org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  25. import org.apache.fop.afp.AFPGraphics2D;
  26. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  27. import org.apache.fop.afp.AFPPaintingState;
  28. import org.apache.fop.afp.AFPResourceManager;
  29. import org.apache.fop.render.AbstractGraphics2DAdapter;
  30. import org.apache.fop.render.RendererContext;
  31. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  32. /**
  33. * Graphics2DAdapter implementation for AFP.
  34. */
  35. public class AFPGraphics2DAdapter extends AbstractGraphics2DAdapter {
  36. private final AFPPaintingState paintingState;
  37. /**
  38. * Main constructor
  39. *
  40. * @param paintingState the AFP painting state
  41. */
  42. public AFPGraphics2DAdapter(AFPPaintingState paintingState) {
  43. this.paintingState = paintingState;
  44. }
  45. /** {@inheritDoc} */
  46. public void paintImage(Graphics2DImagePainter painter,
  47. RendererContext rendererContext,
  48. int x, int y, int width, int height) throws IOException {
  49. AFPRendererContext afpRendererContext = (AFPRendererContext)rendererContext;
  50. AFPInfo afpInfo = afpRendererContext.getInfo();
  51. final boolean textAsShapes = false;
  52. AFPGraphics2D g2d = afpInfo.createGraphics2D(textAsShapes);
  53. paintingState.save();
  54. //Fallback solution: Paint to a BufferedImage
  55. if (afpInfo.paintAsBitmap()) {
  56. // paint image
  57. RendererContextWrapper rendererContextWrapper
  58. = RendererContext.wrapRendererContext(rendererContext);
  59. float targetResolution = rendererContext.getUserAgent().getTargetResolution();
  60. int resolution = Math.round(targetResolution);
  61. boolean colorImages = afpInfo.isColorSupported();
  62. BufferedImage bufferedImage = paintToBufferedImage(
  63. painter, rendererContextWrapper, resolution, !colorImages, false);
  64. // draw image
  65. AffineTransform at = paintingState.getData().getTransform();
  66. at.translate(x, y);
  67. g2d.drawImage(bufferedImage, at, null);
  68. } else {
  69. AFPGraphicsObjectInfo graphicsObjectInfo = new AFPGraphicsObjectInfo();
  70. graphicsObjectInfo.setPainter(painter);
  71. graphicsObjectInfo.setGraphics2D(g2d);
  72. // get the 'width' and 'height' attributes of the SVG document
  73. Dimension imageSize = painter.getImageSize();
  74. float imw = (float)imageSize.getWidth() / 1000f;
  75. float imh = (float)imageSize.getHeight() / 1000f;
  76. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  77. graphicsObjectInfo.setArea(area);
  78. AFPResourceManager resourceManager = afpInfo.getResourceManager();
  79. resourceManager.createObject(graphicsObjectInfo);
  80. }
  81. paintingState.restore();
  82. }
  83. /** {@inheritDoc} */
  84. protected int mpt2px(int unit, int resolution) {
  85. return Math.round(paintingState.getUnitConverter().mpt2units(unit));
  86. }
  87. }