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
|
/*
* 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.svg;
import java.awt.Rectangle;
import java.io.IOException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlgraphics.image.loader.Image;
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.impl.ImageRawStream;
import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
import org.apache.fop.image.loader.batik.BatikImageFlavors;
import org.apache.fop.render.ImageHandler;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.DelegatingFragmentContentHandler;
/**
* Image handler implementation that embeds SVG images in the target SVG file.
*/
public class EmbeddedSVGImageHandler implements ImageHandler, SVGConstants {
/** logging instance */
private static Log log = LogFactory.getLog(EmbeddedSVGImageHandler.class);
/** Constant for the "CDATA" attribute type. */
private static final String CDATA = "CDATA";
/** {@inheritDoc} */
public int getPriority() {
return 500;
}
/** {@inheritDoc} */
public Class getSupportedImageClass() {
return ImageRawStream.class;
}
/** {@inheritDoc} */
public ImageFlavor[] getSupportedImageFlavors() {
return new ImageFlavor[] {
BatikImageFlavors.SVG_DOM
};
}
/*
private void addAttribute(AttributesImpl atts, QName attribute, String value) {
atts.addAttribute(attribute.getNamespaceURI(),
attribute.getLocalName(), attribute.getQName(), CDATA, value);
}
*/
/** {@inheritDoc} */
public void handleImage(RenderingContext context, Image image, final Rectangle pos)
throws IOException {
SVGRenderingContext svgContext = (SVGRenderingContext)context;
if (!(image instanceof ImageXMLDOM)) {
throw new IllegalStateException();
}
ImageXMLDOM svg = (ImageXMLDOM) image;
ContentHandler handler = svgContext.getContentHandler();
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "x", "x", CDATA, SVGUtil.formatMptToPt(pos.x));
atts.addAttribute("", "y", "y", CDATA, SVGUtil.formatMptToPt(pos.y));
atts.addAttribute("", "width", "width", CDATA, SVGUtil.formatMptToPt(pos.width));
atts.addAttribute("", "height", "height", CDATA, SVGUtil.formatMptToPt(pos.height));
try {
Document doc = (Document)svg.getDocument();
Element svgEl = (Element)doc.getDocumentElement();
if (svgEl.getAttribute("viewBox").length() == 0) {
log.warn("SVG doesn't have a viewBox. The result might not be scaled correctly!");
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource src = new DOMSource(svg.getDocument());
SAXResult res = new SAXResult(new DelegatingFragmentContentHandler(handler) {
private boolean topLevelSVGFound;
private void setAttribute(AttributesImpl atts, String localName, String value) {
int index;
index = atts.getIndex("", localName);
if (index < 0) {
atts.addAttribute("", localName, localName, CDATA, value);
} else {
atts.setAttribute(index, "", localName, localName, CDATA, value);
}
}
public void startElement(String uri, String localName, String name, Attributes atts)
throws SAXException {
if (!topLevelSVGFound
&& SVG_ELEMENT.getNamespaceURI().equals(uri)
&& SVG_ELEMENT.getLocalName().equals(localName)) {
topLevelSVGFound = true;
AttributesImpl modAtts = new AttributesImpl(atts);
setAttribute(modAtts, "x", SVGUtil.formatMptToPt(pos.x));
setAttribute(modAtts, "y", SVGUtil.formatMptToPt(pos.y));
setAttribute(modAtts, "width", SVGUtil.formatMptToPt(pos.width));
setAttribute(modAtts, "height", SVGUtil.formatMptToPt(pos.height));
super.startElement(uri, localName, name, modAtts);
} else {
super.startElement(uri, localName, name, atts);
}
}
});
transformer.transform(src, res);
} catch (TransformerException te) {
throw new IOException(te.getMessage());
}
}
/** {@inheritDoc} */
public boolean isCompatible(RenderingContext targetContext, Image image) {
if (targetContext instanceof SVGRenderingContext) {
if (image == null) {
return true;
}
if (image instanceof ImageXMLDOM) {
ImageXMLDOM svg = (ImageXMLDOM)image;
return NAMESPACE.equals(svg.getRootNamespace());
}
}
return false;
}
}
|