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
|
/* ====================================================================
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.
==================================================================== */
package org.apache.poi.xslf.draw;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.bridge.UserAgent;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.ext.awt.RenderingHintsKeyExt;
import org.apache.batik.ext.awt.image.renderable.ClipRable8Bit;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.XMLResourceDescriptor;
import org.apache.poi.sl.draw.ImageRenderer;
import org.w3c.dom.Document;
public class SVGImageRenderer implements ImageRenderer {
private final GVTBuilder builder = new GVTBuilder();
private final BridgeContext context;
private final SAXSVGDocumentFactory svgFact;
private GraphicsNode svgRoot;
private double alpha = 1.0;
public SVGImageRenderer() {
String parser = XMLResourceDescriptor.getXMLParserClassName();
// TOOO: tell the batik guys to use secure parsing feature
svgFact = new SAXSVGDocumentFactory(parser);
UserAgent agent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(agent);
context = new BridgeContext(agent, loader);
context.setDynamic(true);
}
@Override
public void loadImage(InputStream data, String contentType) throws IOException {
Document document = svgFact.createDocument("", data);
svgRoot = builder.build(context, document);
}
@Override
public void loadImage(byte[] data, String contentType) throws IOException {
loadImage(new ByteArrayInputStream(data), contentType);
}
@Override
public Dimension getDimension() {
Rectangle2D r = svgRoot.getPrimitiveBounds();
return new Dimension((int)Math.ceil(r.getWidth()), (int)Math.ceil(r.getHeight()));
}
@Override
public void setAlpha(double alpha) {
this.alpha = alpha;
}
@Override
public BufferedImage getImage() {
return getImage(getDimension());
}
@Override
public BufferedImage getImage(Dimension dim) {
BufferedImage bi = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = (Graphics2D) bi.getGraphics();
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.setRenderingHint(RenderingHintsKeyExt.KEY_BUFFERED_IMAGE, new WeakReference(bi));
Dimension dimSVG = getDimension();
double scaleX = dim.getWidth() / dimSVG.getWidth();
double scaleY = dim.getHeight() / dimSVG.getHeight();
g2d.scale(scaleX, scaleY);
svgRoot.paint(g2d);
g2d.dispose();
return bi;
}
@Override
public boolean drawImage(Graphics2D graphics, Rectangle2D anchor) {
return drawImage(graphics, anchor, null);
}
@Override
public boolean drawImage(Graphics2D graphics, Rectangle2D anchor, Insets clip) {
if (clip == null) {
svgRoot.setClip(null);
} else {
Rectangle2D clippedRect = new Rectangle2D.Double(
anchor.getX()+clip.left,
anchor.getY()+clip.top,
anchor.getWidth()-(clip.left+clip.right),
anchor.getHeight()-(clip.top+clip.bottom)
);
svgRoot.setClip(new ClipRable8Bit(null, clippedRect));
}
svgRoot.paint(graphics);
return true;
}
}
|