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