Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/svg/InkscapeLoader.java
1463 views
1
package org.newdawn.slick.svg;
2
3
import java.io.ByteArrayInputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.ArrayList;
7
8
import javax.xml.parsers.DocumentBuilder;
9
import javax.xml.parsers.DocumentBuilderFactory;
10
11
import org.newdawn.slick.SlickException;
12
import org.newdawn.slick.geom.Transform;
13
import org.newdawn.slick.svg.inkscape.DefsProcessor;
14
import org.newdawn.slick.svg.inkscape.ElementProcessor;
15
import org.newdawn.slick.svg.inkscape.EllipseProcessor;
16
import org.newdawn.slick.svg.inkscape.GroupProcessor;
17
import org.newdawn.slick.svg.inkscape.LineProcessor;
18
import org.newdawn.slick.svg.inkscape.PathProcessor;
19
import org.newdawn.slick.svg.inkscape.PolygonProcessor;
20
import org.newdawn.slick.svg.inkscape.RectProcessor;
21
import org.newdawn.slick.svg.inkscape.UseProcessor;
22
import org.newdawn.slick.util.ResourceLoader;
23
import org.w3c.dom.Document;
24
import org.w3c.dom.Element;
25
import org.w3c.dom.NodeList;
26
import org.xml.sax.EntityResolver;
27
import org.xml.sax.InputSource;
28
import org.xml.sax.SAXException;
29
30
/**
31
* A loader specifically for the SVG that is produced from Inkscape
32
*
33
* @author kevin
34
*/
35
public class InkscapeLoader implements Loader {
36
/**
37
* The number of times to over trigulate to get enough tesselation for
38
* smooth shading
39
*/
40
public static int RADIAL_TRIANGULATION_LEVEL = 1;
41
42
/** The list of XML element processors */
43
private static ArrayList processors = new ArrayList();
44
45
/** The diagram loaded */
46
private Diagram diagram;
47
48
static {
49
addElementProcessor(new RectProcessor());
50
addElementProcessor(new EllipseProcessor());
51
addElementProcessor(new PolygonProcessor());
52
addElementProcessor(new PathProcessor());
53
addElementProcessor(new LineProcessor());
54
addElementProcessor(new GroupProcessor());
55
addElementProcessor(new DefsProcessor());
56
addElementProcessor(new UseProcessor());
57
}
58
59
/**
60
* Add an <code>ElementProcessor</code> which will be passed
61
* each element read as the Inkscape SVG document is processed.
62
*
63
* @param proc The processor to be added
64
*/
65
public static void addElementProcessor(ElementProcessor proc) {
66
processors.add(proc);
67
}
68
69
/**
70
* Load a SVG document into a diagram
71
*
72
* @param ref
73
* The reference in the classpath to load the diagram from
74
* @param offset
75
* Offset the diagram for the height of the document
76
* @return The diagram loaded
77
* @throws SlickException
78
* Indicates a failure to process the document
79
*/
80
public static Diagram load(String ref, boolean offset)
81
throws SlickException {
82
return load(ResourceLoader.getResourceAsStream(ref), offset);
83
}
84
85
/**
86
* Load a SVG document into a diagram
87
*
88
* @param ref
89
* The reference in the classpath to load the diagram from
90
* @return The diagram loaded
91
* @throws SlickException
92
* Indicates a failure to process the document
93
*/
94
public static Diagram load(String ref) throws SlickException {
95
return load(ResourceLoader.getResourceAsStream(ref), false);
96
}
97
98
/**
99
* Load a SVG document into a diagram
100
*
101
* @param offset
102
* Offset the diagram for the height of the document
103
* @param in
104
* The input stream from which to read the SVG
105
* @return The diagram loaded
106
* @throws SlickException
107
* Indicates a failure to process the document
108
*/
109
public static Diagram load(InputStream in, boolean offset)
110
throws SlickException {
111
return new InkscapeLoader().loadDiagram(in, offset);
112
}
113
114
/**
115
* Private, you should be using the static method
116
*/
117
private InkscapeLoader() {
118
}
119
120
/**
121
* Load a SVG document into a diagram
122
*
123
* @param in
124
* The input stream from which to read the SVG
125
* @return The diagram loaded
126
* @throws SlickException
127
* Indicates a failure to process the document
128
*/
129
private Diagram loadDiagram(InputStream in) throws SlickException {
130
return loadDiagram(in, false);
131
}
132
133
/**
134
* Load a SVG document into a diagram
135
*
136
* @param in
137
* The input stream from which to read the SVG
138
* @param offset
139
* Offset the diagram for the height of the document
140
* @return The diagram loaded
141
* @throws SlickException
142
* Indicates a failure to process the document
143
*/
144
private Diagram loadDiagram(InputStream in, boolean offset)
145
throws SlickException {
146
try {
147
DocumentBuilderFactory factory = DocumentBuilderFactory
148
.newInstance();
149
factory.setValidating(false);
150
factory.setNamespaceAware(true);
151
152
DocumentBuilder builder = factory.newDocumentBuilder();
153
builder.setEntityResolver(new EntityResolver() {
154
public InputSource resolveEntity(String publicId,
155
String systemId) throws SAXException, IOException {
156
return new InputSource(
157
new ByteArrayInputStream(new byte[0]));
158
}
159
});
160
161
Document doc = builder.parse(in);
162
Element root = doc.getDocumentElement();
163
164
String widthString = root.getAttribute("width");
165
while (Character.isLetter(widthString
166
.charAt(widthString.length() - 1))) {
167
widthString = widthString.substring(0, widthString.length() - 1);
168
}
169
170
String heightString = root.getAttribute("height");
171
while (Character.isLetter(heightString
172
.charAt(heightString.length() - 1))) {
173
heightString = heightString.substring(0,heightString.length() - 1);
174
}
175
176
float docWidth = Float.parseFloat(widthString);
177
float docHeight = Float.parseFloat(heightString);
178
179
diagram = new Diagram(docWidth, docHeight);
180
if (!offset) {
181
docHeight = 0;
182
}
183
loadChildren(root, Transform
184
.createTranslateTransform(0, -docHeight));
185
186
return diagram;
187
} catch (Exception e) {
188
throw new SlickException("Failed to load inkscape document", e);
189
}
190
}
191
192
/**
193
* @see org.newdawn.slick.svg.Loader#loadChildren(org.w3c.dom.Element,
194
* org.newdawn.slick.geom.Transform)
195
*/
196
public void loadChildren(Element element, Transform t)
197
throws ParsingException {
198
NodeList list = element.getChildNodes();
199
for (int i = 0; i < list.getLength(); i++) {
200
if (list.item(i) instanceof Element) {
201
loadElement((Element) list.item(i), t);
202
}
203
}
204
}
205
206
/**
207
* Load a single element into the diagram
208
*
209
* @param element
210
* The element ot be loaded
211
* @param t
212
* The transform to apply to the loaded element from the parent
213
* @throws ParsingException
214
* Indicates a failure to parse the element
215
*/
216
private void loadElement(Element element, Transform t)
217
throws ParsingException {
218
for (int i = 0; i < processors.size(); i++) {
219
ElementProcessor processor = (ElementProcessor) processors.get(i);
220
221
if (processor.handles(element)) {
222
processor.process(this, element, diagram, t);
223
}
224
}
225
}
226
}
227
228