aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/ps/PSImageHandlerSVG.java
blob: 6d277f2f0054c5612b18840622a6495d03388bd8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */

package org.apache.fop.render.ps;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.imageio.ImageIO;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.transcoder.SVGAbstractTranscoder;
import org.apache.batik.transcoder.TranscoderException;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;

import org.apache.xmlgraphics.image.loader.Image;
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
import org.apache.xmlgraphics.ps.ImageEncoder;
import org.apache.xmlgraphics.ps.ImageEncodingHelper;
import org.apache.xmlgraphics.ps.PSGenerator;

import org.apache.fop.image.loader.batik.BatikImageFlavors;
import org.apache.fop.image.loader.batik.BatikUtil;
import org.apache.fop.render.ImageHandler;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.ps.svg.PSSVGGraphics2D;
import org.apache.fop.svg.SVGEventProducer;
import org.apache.fop.svg.SVGUserAgent;
import org.apache.fop.svg.font.FOPFontFamilyResolverImpl;

/**
 * Image handler implementation which handles SVG images for PostScript output.
 */
public class PSImageHandlerSVG implements ImageHandler {

    private static final Color FALLBACK_COLOR = new Color(255, 33, 117);
    private HashMap<String, String> gradientsFound = new HashMap<String, String>();

    private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
        BatikImageFlavors.SVG_DOM
    };

    /** {@inheritDoc} */
    public void handleImage(RenderingContext context, Image image, Rectangle pos)
                throws IOException {
        PSRenderingContext psContext = (PSRenderingContext)context;
        PSGenerator gen = psContext.getGenerator();
        ImageXMLDOM imageSVG = (ImageXMLDOM)image;

        if (shouldRaster(imageSVG)) {
            InputStream is = renderSVGToInputStream(context, imageSVG);

            float x = (float) pos.getX() / 1000f;
            float y = (float) pos.getY() / 1000f;
            float w = (float) pos.getWidth() / 1000f;
            float h = (float) pos.getHeight() / 1000f;
            Rectangle2D targetRect = new Rectangle2D.Double(x, y, w, h);

            MaskedImage mi = convertToRGB(ImageIO.read(is));
            BufferedImage ri = mi.getImage();
            ImageEncoder encoder = ImageEncodingHelper.createRenderedImageEncoder(ri);
            Dimension imgDim = new Dimension(ri.getWidth(), ri.getHeight());
            String imgDescription = ri.getClass().getName();
            ImageEncodingHelper helper = new ImageEncodingHelper(ri);
            ColorModel cm = helper.getEncodedColorModel();
            PSImageUtils.writeImage(encoder, imgDim, imgDescription, targetRect, cm, gen, ri, mi.getMaskColor());
        } else {
            //Controls whether text painted by Batik is generated using text or path operations
            boolean strokeText = false;
            //TODO Configure text stroking

            SVGUserAgent ua = new SVGUserAgent(context.getUserAgent(),
                    new FOPFontFamilyResolverImpl(psContext.getFontInfo()), new AffineTransform());

            PSSVGGraphics2D graphics = new PSSVGGraphics2D(strokeText, gen);
            graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

            BridgeContext ctx = new PSBridgeContext(ua,
                    (strokeText ? null : psContext.getFontInfo()),
                    context.getUserAgent().getImageManager(),
                    context.getUserAgent().getImageSessionContext());

            //Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
            //to it.
            Document clonedDoc = BatikUtil.cloneSVGDocument(imageSVG.getDocument());

            GraphicsNode root;
            try {
                GVTBuilder builder = new GVTBuilder();
                root = builder.build(ctx, clonedDoc);
            } catch (Exception e) {
                SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
                        context.getUserAgent().getEventBroadcaster());
                eventProducer.svgNotBuilt(this, e, image.getInfo().getOriginalURI());
                return;
            }
            // get the 'width' and 'height' attributes of the SVG document
            float w = (float)ctx.getDocumentSize().getWidth() * 1000f;
            float h = (float)ctx.getDocumentSize().getHeight() * 1000f;

            float sx = pos.width / w;
            float sy = pos.height / h;

            gen.commentln("%FOPBeginSVG");
            gen.saveGraphicsState();
            final boolean clip = false;
            if (clip) {
                /*
                 * Clip to the svg area.
                 * Note: To have the svg overlay (under) a text area then use
                 * an fo:block-container
                 */
                gen.writeln("newpath");
                gen.defineRect(pos.getMinX() / 1000f, pos.getMinY() / 1000f,
                        pos.width / 1000f, pos.height / 1000f);
                gen.writeln("clip");
            }

            // transform so that the coordinates (0,0) is from the top left
            // and positive is down and to the right. (0,0) is where the
            // viewBox puts it.
            gen.concatMatrix(sx, 0, 0, sy, pos.getMinX() / 1000f, pos.getMinY() / 1000f);

            AffineTransform transform = new AffineTransform();
            // scale to viewbox
            transform.translate(pos.getMinX(), pos.getMinY());
            gen.getCurrentState().concatMatrix(transform);
            try {
                root.paint(graphics);
            } catch (Exception e) {
                SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
                        context.getUserAgent().getEventBroadcaster());
                eventProducer.svgRenderingError(this, e, image.getInfo().getOriginalURI());
            }

            gen.restoreGraphicsState();
            gen.commentln("%FOPEndSVG");
        }
    }

    private InputStream renderSVGToInputStream(RenderingContext context, ImageXMLDOM imageSVG) throws IOException {
        PNGTranscoder png = new PNGTranscoder();
        Float width = getDimension(imageSVG.getDocument(), "width") * 8;
        png.addTranscodingHint(SVGAbstractTranscoder.KEY_WIDTH, width);
        Float height = getDimension(imageSVG.getDocument(), "height") * 8;
        png.addTranscodingHint(SVGAbstractTranscoder.KEY_HEIGHT, height);
        TranscoderInput input = new TranscoderInput(imageSVG.getDocument());
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        TranscoderOutput output = new TranscoderOutput(os);
        try {
            png.transcode(input, output);
        } catch (TranscoderException ex) {
            SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
                    context.getUserAgent().getEventBroadcaster());
            eventProducer.svgRenderingError(this, ex, imageSVG.getInfo().getOriginalURI());
        } finally {
            os.flush();
            os.close();
        }
        return new ByteArrayInputStream(os.toByteArray());
    }

    private MaskedImage convertToRGB(BufferedImage alphaImage) {
        int[] red = new int[256];
        int[] green = new int[256];
        int[] blue = new int[256];
        BufferedImage rgbImage = new BufferedImage(alphaImage.getWidth(),
                alphaImage.getHeight(), BufferedImage.TYPE_INT_RGB);
        //Count occurances of each colour in image
        for (int cx = 0; cx < alphaImage.getWidth(); cx++) {
            for (int cy = 0; cy < alphaImage.getHeight(); cy++) {
                int pixelValue = alphaImage.getRGB(cx, cy);
                Color pixelColor = new Color(pixelValue);
                red[pixelColor.getRed()]++;
                green[pixelColor.getGreen()]++;
                blue[pixelColor.getBlue()]++;
            }
        }
        //Find colour not in image
        Color alphaSwap = null;
        for (int i = 0; i < 256; i++) {
            if (red[i] == 0) {
                alphaSwap = new Color(i, 0, 0);
                break;
            } else if (green[i] == 0) {
                alphaSwap = new Color(0, i, 0);
                break;
            } else if (blue[i] == 0) {
                alphaSwap = new Color(0, 0, i);
                break;
            }
        }
        //Check if all variations are used in all three colours
        if (alphaSwap == null) {
            //Fallback colour is no unique colour channel can be found
            alphaSwap = FALLBACK_COLOR;
        }
        //Replace alpha channel with the new mask colour
        for (int cx = 0; cx < alphaImage.getWidth(); cx++) {
            for (int cy = 0; cy < alphaImage.getHeight(); cy++) {
                int pixelValue = alphaImage.getRGB(cx, cy);
                if (pixelValue == 0) {
                    rgbImage.setRGB(cx, cy, alphaSwap.getRGB());
                } else {
                    rgbImage.setRGB(cx, cy, alphaImage.getRGB(cx, cy));
                }
            }
        }
        return new MaskedImage(rgbImage, alphaSwap);
    }

    private static class MaskedImage {
        private Color maskColor = new Color(0, 0, 0);
        private BufferedImage image;

        public MaskedImage(BufferedImage image, Color maskColor) {
            this.image = image;
            this.maskColor = maskColor;
        }

        public Color getMaskColor() {
            return maskColor;
        }

        public BufferedImage getImage() {
            return image;
        }
    }

    private Float getDimension(Document document, String dimension) {
        if (document.getFirstChild().getAttributes().getNamedItem(dimension) != null) {
            String width = document.getFirstChild().getAttributes().getNamedItem(dimension).getNodeValue();
            width = width.replaceAll("[^\\d.]", "");
            return Float.parseFloat(width);
        }
        return null;
    }

    private boolean shouldRaster(ImageXMLDOM image) {
        //A list of objects on which to check opacity
        try {
        List<String> gradMatches = new ArrayList<String>();
        gradMatches.add("radialGradient");
        gradMatches.add("linearGradient");
        return recurseSVGElements(image.getDocument().getChildNodes(), gradMatches, false);
        } finally {
            gradientsFound.clear();
        }
    }

    private boolean recurseSVGElements(NodeList childNodes, List<String> gradMatches, boolean isMatched) {
        boolean opacityFound = false;
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node curNode = childNodes.item(i);
            if (isMatched && curNode.getLocalName() != null && curNode.getLocalName().equals("stop")) {
                if (curNode.getAttributes().getNamedItem("style") != null) {
                    String[] stylePairs = curNode.getAttributes().getNamedItem("style").getNodeValue()
                            .split(";");
                    for (int styleAtt = 0; styleAtt < stylePairs.length; styleAtt++) {
                        String[] style = stylePairs[styleAtt].split(":");
                        if (style[0].equalsIgnoreCase("stop-opacity")) {
                            if (Double.parseDouble(style[1]) < 1) {
                                return true;
                            }
                        }
                    }
                }
                if (curNode.getAttributes().getNamedItem("stop-opacity") != null) {
                    String opacityValue = curNode.getAttributes().getNamedItem("stop-opacity").getNodeValue();
                    if (Double.parseDouble(opacityValue) < 1) {
                        return true;
                    }
                }
            }
            String nodeName = curNode.getLocalName();
            boolean inMatch = false;
            if (!isMatched) {
                inMatch = nodeName != null && gradMatches.contains(nodeName);
                if (inMatch) {
                    gradientsFound.put(curNode.getAttributes().getNamedItem("id").getNodeValue(), nodeName);
                }
            } else {
                inMatch = true;
            }
            opacityFound = recurseSVGElements(curNode.getChildNodes(), gradMatches, inMatch);
            if (opacityFound) {
                return true;
            }
        }
        return opacityFound;
    }

    /** {@inheritDoc} */
    public int getPriority() {
        return 400;
    }

    /** {@inheritDoc} */
    public Class getSupportedImageClass() {
        return ImageXMLDOM.class;
    }

    /** {@inheritDoc} */
    public ImageFlavor[] getSupportedImageFlavors() {
        return FLAVORS;
    }

    /** {@inheritDoc} */
    public boolean isCompatible(RenderingContext targetContext, Image image) {
        if (targetContext instanceof PSRenderingContext) {
            PSRenderingContext psContext = (PSRenderingContext)targetContext;
            return !psContext.isCreateForms()
                && (image == null || (image instanceof ImageXMLDOM
                        && image.getFlavor().isCompatible(BatikImageFlavors.SVG_DOM)));
        }
        return false;
    }

}