-
Notifications
You must be signed in to change notification settings - Fork 16
/
SvgConvertUtils.java
275 lines (216 loc) · 7.6 KB
/
SvgConvertUtils.java
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
package com.gemserk.commons.svg.inkscape;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.w3c.dom.Element;
import com.badlogic.gdx.graphics.Color;
import com.gemserk.vecmath.Matrix3f;
import com.gemserk.vecmath.Vector2f;
public class SvgConvertUtils {
private static enum Command {
None, AbsoluteMoveTo, RelativeMoveTo, AbsoluteLineTo, RelativeLineTo, AbsoluteElipticalArc, RelativeElipticalArc, ClosePath, //
AbsoluteCurveTo, RelativeCurveTo, AbsoluteSmoothCurveTo, RelativeSmoothCurveTo
}
@SuppressWarnings("serial")
private static Map<String, Command> commandMap = new HashMap<String, Command>() {
{
put("m", Command.RelativeMoveTo);
put("M", Command.AbsoluteMoveTo);
put("l", Command.RelativeLineTo);
put("L", Command.AbsoluteLineTo);
put("z", Command.ClosePath);
put("Z", Command.ClosePath);
put("A", Command.AbsoluteElipticalArc);
put("a", Command.RelativeElipticalArc);
put("C", Command.AbsoluteCurveTo);
put("c", Command.RelativeCurveTo);
put("S", Command.AbsoluteSmoothCurveTo);
put("s", Command.RelativeSmoothCurveTo);
}
};
public static SvgGroup getSvgGroup(Element element) {
String id = element.getAttribute("id");
Matrix3f transform = new Matrix3f();
transform.setIdentity();
SvgInkscapeUtils.getTransform(element, transform);
SvgGroupImpl svgGroup = new SvgGroupImpl();
svgGroup.setId(id);
svgGroup.setTransform(transform);
return svgGroup;
}
public static SvgImage getSvgImage(Element element) {
String id = element.getAttribute("id");
float x = Float.parseFloat(element.getAttribute("x"));
float y = Float.parseFloat(element.getAttribute("y"));
float width = Float.parseFloat(element.getAttribute("width"));
float height = Float.parseFloat(element.getAttribute("height"));
Matrix3f transform = new Matrix3f();
transform.setIdentity();
// should be processed before by the TransformProcessor?
SvgInkscapeUtils.getTransform(element, transform);
SvgImageImpl svgImage = new SvgImageImpl();
svgImage.setId(id);
svgImage.setX(x);
svgImage.setY(y);
svgImage.setWidth(width);
svgImage.setHeight(height);
svgImage.setTransform(transform);
return svgImage;
}
public static SvgPath getSvgPath(Element element) {
String id = element.getAttribute("id");
// String label = SvgInkscapeUtils.getLabel(element);
String d = element.getAttribute("d");
Vector2f[] points = pathDataToVector2fList(d);
SvgPathImpl svgPath = new SvgPathImpl();
svgPath.setId(id);
svgPath.setPoints(points);
return svgPath;
}
/**
* Returns a list of Vector2f[] built using an SVG path data, it only supports basic stuff.
*/
public static Vector2f[] pathDataToVector2fList(String d) {
StringTokenizer tokens = new StringTokenizer(d, ", ");
ArrayList<Vector2f> pointList = new ArrayList<Vector2f>();
Vector2f currentLocation = new Vector2f(0f, 0f);
Command currentCommand = Command.None;
while (tokens.hasMoreTokens()) {
String token = tokens.nextToken();
Command command = commandMap.get(token);
if (command != null) {
currentCommand = command;
continue;
}
if (currentCommand == Command.None)
continue;
// http://www.w3.org/TR/SVG/paths.html#PathData
if (currentCommand == Command.RelativeMoveTo || currentCommand == Command.RelativeLineTo) {
float xr = Float.parseFloat(token);
float yr = Float.parseFloat(tokens.nextToken());
float x = xr + currentLocation.x;
float y = yr + currentLocation.y;
pointList.add(new Vector2f(x, y));
currentLocation.set(x, y);
} else if (currentCommand == Command.AbsoluteMoveTo || currentCommand == Command.AbsoluteLineTo) {
float x = Float.parseFloat(token);
float y = Float.parseFloat(tokens.nextToken());
pointList.add(new Vector2f(x, y));
currentLocation.set(x, y);
} else {
throw new UnsupportedOperationException(currentCommand.toString() + " not supported yet");
}
// other commands?
}
Vector2f[] points = new Vector2f[pointList.size()];
pointList.toArray(points);
return points;
}
public static Color fillColorFromStyle(String style) {
return fillColorFromStyle(new Color(), style);
}
public static Color fillColorFromStyle(Color color, String style) {
String fillColorStr = getFillFromStyle(style);
if (fillColorStr != null)
colorValue(color, fillColorStr);
String fillOpacityStr = getFillOpacityFromStyle(style);
if (fillOpacityStr != null)
color.a = Float.valueOf(fillOpacityStr);
return color;
}
private static Pattern fillStylePattern = Pattern.compile("fill:(.[^;]*)");
private static Pattern fillOpacityStylePattern = Pattern.compile("fill\\-opacity:(.[^;]*)");
public static String getFillFromStyle(String style) {
Matcher matcher = fillStylePattern.matcher(style.trim());
while (matcher.find()) {
if (matcher.groupCount() == 0)
continue;
String fill = matcher.group(1);
return fill;
}
return null;
}
public static String getFillOpacityFromStyle(String style) {
Matcher matcher = fillOpacityStylePattern.matcher(style.trim());
while (matcher.find()) {
if (matcher.groupCount() == 0)
continue;
String fill = matcher.group(1);
return fill;
}
return null;
}
public static String replicateHexNotation(CharSequence hexColor) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(hexColor.charAt(0));
stringBuilder.append(hexColor.charAt(0));
stringBuilder.append(hexColor.charAt(1));
stringBuilder.append(hexColor.charAt(1));
stringBuilder.append(hexColor.charAt(2));
stringBuilder.append(hexColor.charAt(2));
return stringBuilder.toString();
}
public static Color colorValue(String colorValue) {
return colorValue(new Color(), colorValue);
}
public static Color colorValue(Color color, String colorValue) {
String hexNotationPrefix = "#";
String rgbNotationPrefix = "rgb";
int hexRadix = 16;
if (colorValue.startsWith(hexNotationPrefix)) {
String colorHexValue = colorValue.trim().substring(1);
if (colorHexValue.length() == 3)
colorHexValue = replicateHexNotation(colorHexValue);
Color.rgb888ToColor(color, Integer.valueOf(colorHexValue, hexRadix));
return color;
} else if (colorValue.startsWith(rgbNotationPrefix)) {
throw new UnsupportedOperationException("color from rgb() format not supported yet");
} else {
throw new UnsupportedOperationException("color style format not supported yet");
}
}
public static float opacityValue(String opacityValue) {
float opacity = Float.parseFloat(opacityValue);
if (opacity < 0f)
opacity = 0f;
else if (opacity > 1f)
opacity = 1f;
return opacity;
}
/**
* Returns a new implementation of SvgElementConverter which converts the XML Element to SvgGroup.
*/
public static SvgElementConverter<SvgGroup> groupConverter() {
return new SvgElementConverter<SvgGroup>() {
@Override
public SvgGroup convert(Element element) {
return getSvgGroup(element);
}
};
}
/**
* Returns a new implementation of SvgElementConverter which converts the XML Element to SvgImage.
*/
public static SvgElementConverter<SvgImage> imageConverter() {
return new SvgElementConverter<SvgImage>() {
@Override
public SvgImage convert(Element element) {
return getSvgImage(element);
}
};
}
/**
* Returns a new implementation of SvgElementConverter which converts the XML Element to SvgPath.
*/
public static SvgElementConverter<SvgPath> pathConverter() {
return new SvgElementConverter<SvgPath>() {
@Override
public SvgPath convert(Element element) {
return getSvgPath(element);
}
};
}
}