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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.fop.afp.AFPGraphics2D;
  25. import org.apache.fop.afp.AFPGraphicsObjectInfo;
  26. import org.apache.fop.afp.AFPPaintingState;
  27. import org.apache.fop.afp.AFPResourceInfo;
  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. import org.apache.xmlgraphics.java2d.Graphics2DImagePainter;
  33. /**
  34. * Graphics2DAdapter implementation for AFP.
  35. */
  36. public class AFPGraphics2DAdapter extends AbstractGraphics2DAdapter {
  37. private final AFPRenderer renderer;
  38. private final AFPGraphics2D g2d;
  39. /**
  40. * Main constructor
  41. *
  42. * @param renderer the afp renderer
  43. */
  44. public AFPGraphics2DAdapter(AFPRenderer renderer) {
  45. this.renderer = renderer;
  46. final boolean textAsShapes = false;
  47. this.g2d = new AFPGraphics2D(textAsShapes);
  48. }
  49. /**
  50. * Returns the AFP graphics 2D implementation
  51. *
  52. * @return the AFP graphics 2D implementation
  53. */
  54. public AFPGraphics2D getGraphics2D() {
  55. return g2d;
  56. }
  57. /** {@inheritDoc} */
  58. public void paintImage(Graphics2DImagePainter painter,
  59. RendererContext context,
  60. int x, int y, int width, int height) throws IOException {
  61. AFPInfo afpInfo = AFPSVGHandler.getAFPInfo(context);
  62. // set resource manager
  63. AFPResourceManager resourceManager = afpInfo.getResourceManager();
  64. g2d.setResourceManager(resourceManager);
  65. // set resource information
  66. AFPResourceInfo resourceInfo = afpInfo.getResourceInfo();
  67. g2d.setResourceInfo(resourceInfo);
  68. // set painting state
  69. AFPPaintingState paintingState = afpInfo.getPaintingState();
  70. g2d.setPaintingState(paintingState);
  71. // set graphic context
  72. g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
  73. float fwidth = width / 1000f;
  74. float fheight = height / 1000f;
  75. // get the 'width' and 'height' attributes of the SVG document
  76. Dimension imageSize = painter.getImageSize();
  77. float imw = (float)imageSize.getWidth() / 1000f;
  78. float imh = (float)imageSize.getHeight() / 1000f;
  79. float sx = fwidth / imw;
  80. float sy = fheight / imh;
  81. AffineTransform at = new AffineTransform(sx, 0, 0, sy, x, y);
  82. renderer.saveGraphicsState();
  83. if (afpInfo.paintAsBitmap()) {
  84. //Fallback solution: Paint to a BufferedImage
  85. int resolution = Math.round(context.getUserAgent().getTargetResolution());
  86. RendererContextWrapper ctx = RendererContext.wrapRendererContext(context);
  87. BufferedImage bufferedImage = paintToBufferedImage(painter, ctx, resolution, false, false);
  88. AFPPaintingState state = afpInfo.getPaintingState();
  89. AffineTransform trans = state.getData().getTransform();
  90. float scale = AFPRenderer.NORMAL_AFP_RESOLUTION
  91. / context.getUserAgent().getTargetResolution();
  92. if (scale != 1) {
  93. at.scale(scale, scale);
  94. }
  95. if (!at.isIdentity()) {
  96. trans.concatenate(at);
  97. }
  98. g2d.drawImage(bufferedImage, trans, null);
  99. } else {
  100. AFPGraphicsObjectInfo graphicsObjectInfo = new AFPGraphicsObjectInfo();
  101. graphicsObjectInfo.setPainter(painter);
  102. graphicsObjectInfo.setGraphics2D(g2d);
  103. Rectangle2D area = new Rectangle2D.Double(0.0, 0.0, imw, imh);
  104. graphicsObjectInfo.setArea(area);
  105. resourceManager.createObject(graphicsObjectInfo);
  106. }
  107. renderer.restoreGraphicsState();
  108. }
  109. }