-
Notifications
You must be signed in to change notification settings - Fork 4
/
SVGParser.gd
333 lines (292 loc) · 10.5 KB
/
SVGParser.gd
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
tool
extends EditorScript
"""
SVG PARSER
"""
var file_path = "res://files/map01.svg"
var use_path2d = false #true to deploy Path2D for vector paths
var xml_data = XMLParser.new()
var root_node : Node
var current_node : Node
const MAX_WIDTH = 7.0
func _run() -> void:
if xml_data.open(file_path) != OK:
print("Error opening file: ", file_path)
return
root_node = self.get_scene()
current_node = root_node
#clear tree
for c in root_node.get_children():
c.queue_free()
parse()
"""
Loop through all nodes and create the respective element.
"""
func parse() -> void:
print("start parsing ...")
while xml_data.read() == OK:
if not xml_data.get_node_type() in [XMLParser.NODE_ELEMENT, XMLParser.NODE_ELEMENT_END]:
continue
elif xml_data.get_node_name() == "g":
if xml_data.get_node_type() == XMLParser.NODE_ELEMENT:
process_group(xml_data)
elif xml_data.get_node_type() == XMLParser.NODE_ELEMENT_END:
current_node = current_node.get_parent()
elif xml_data.get_node_name() == "rect":
process_svg_rectangle(xml_data)
elif xml_data.get_node_name() == "polygon":
process_svg_polygon(xml_data)
elif xml_data.get_node_name() == "path":
process_svg_path(xml_data)
print("... end parsing")
func process_group(element:XMLParser) -> void:
var new_group = Node2D.new()
new_group.name = element.get_named_attribute_value("id")
new_group.transform = get_svg_transform(element)
current_node.add_child(new_group)
new_group.set_owner(root_node)
new_group.set_meta("_edit_group_", true)
current_node = new_group
print("group " + new_group.name + " created")
func process_svg_rectangle(element:XMLParser) -> void:
var new_rect = ColorRect.new()
new_rect.name = element.get_named_attribute_value("id")
current_node.add_child(new_rect)
new_rect.set_owner(root_node)
#transform
var x = float(element.get_named_attribute_value("x"))
var y = float(element.get_named_attribute_value("y"))
var width = float(element.get_named_attribute_value("width"))
var height = float(element.get_named_attribute_value("height"))
var transform = get_svg_transform(element)
new_rect.rect_position = Vector2((x), (y))
new_rect.rect_size = Vector2(width, height)
new_rect.rect_position = transform.xform(new_rect.rect_position)
new_rect.rect_size.x *= transform[0][0]
new_rect.rect_size.y *= transform[1][1]
#style
var style = get_svg_style(element)
if style.has("fill"):
new_rect.color = Color(style["fill"])
if style.has("fill-opacity"):
new_rect.color.a = float(style["fill-opacity"])
print("-rect ", new_rect.name, " created")
func process_svg_polygon(element:XMLParser) -> void:
var points : PoolVector2Array
var points_split = element.get_named_attribute_value("points").split(" ", false)
for i in points_split:
var values = i.split_floats(",", false)
points.append(Vector2(values[0], values[1]))
points.append(points[0])
#create closed line
var new_line = Line2D.new()
new_line.name = element.get_named_attribute_value("id")
new_line.transform = get_svg_transform(element)
current_node.add_child(new_line)
new_line.set_owner(root_node)
new_line.points = points
#style
var style = get_svg_style(element)
if style.has("fill"):
new_line.default_color = Color(style["fill"])
if style.has("stroke-width"):
new_line.width = float(style["stroke-width"])
print("-line ", new_line.name, " created")
func process_svg_path(element:XMLParser) -> void:
#prepare element string
var element_string = element.get_named_attribute_value("d")
for symbol in ["m", "M", "v", "V", "h", "H", "l", "L", "c", "C", "s", "S", "z", "Z"]:
element_string = element_string.replacen(symbol, " " + symbol + " ")
element_string = element_string.replacen(",", " ")
#split element string into multiple arrays
var element_string_array = element_string.split(" ", false)
var string_arrays = []
var string_array : PoolStringArray
for a in element_string_array:
if a == "m" or a == "M":
if string_array.size() > 0:
string_arrays.append(string_array)
string_array.resize(0)
string_array.append(a)
string_arrays.append(string_array)
#convert into Line2Ds
var string_array_count = -1
for string_array in string_arrays:
var cursor = Vector2.ZERO
var points : PoolVector2Array
var curve = Curve2D.new()
string_array_count += 1
for i in string_array.size()-1:
match string_array[i]:
"m":
while string_array.size() > i + 2 and string_array[i+1].is_valid_float():
cursor += Vector2(float(string_array[i+1]), float(string_array[i+2]))
points.append(cursor)
i += 2
"M":
while string_array.size() > i + 2 and string_array[i+1].is_valid_float():
cursor = Vector2(float(string_array[i+1]), float(string_array[i+2]))
points.append(cursor)
curve.add_point(Vector2(float(string_array[i+1]), float(string_array[i+2])))
i += 2
"v":
while string_array[i+1].is_valid_float():
cursor.y += float(string_array[i+1])
points.append(cursor)
i += 1
"V":
while string_array[i+1].is_valid_float():
cursor.y = float(string_array[i+1])
points.append(cursor)
i += 1
"h":
while string_array[i+1].is_valid_float():
cursor.x += float(string_array[i+1])
points.append(cursor)
i += 1
"H":
while string_array[i+1].is_valid_float():
cursor.x = float(string_array[i+1])
points.append(cursor)
i += 1
"l":
while string_array.size() > i + 2 and string_array[i+1].is_valid_float():
cursor += Vector2(float(string_array[i+1]), float(string_array[i+2]))
points.append(cursor)
i += 2
"L":
while string_array.size() > i + 2 and string_array[i+1].is_valid_float():
cursor = Vector2(float(string_array[i+1]), float(string_array[i+2]))
points.append(cursor)
i += 2
#simpify Bezier curves with straight line
"c":
while string_array.size() > i + 6 and string_array[i+1].is_valid_float():
cursor += Vector2(float(string_array[i+5]), float(string_array[i+6]))
points.append(cursor)
i += 6
"C":
while string_array.size() > i + 6 and string_array[i+1].is_valid_float():
var controll_point_in = Vector2(float(string_array[i+5]), float(string_array[i+6])) - cursor
cursor = Vector2(float(string_array[i+5]), float(string_array[i+6]))
points.append(cursor)
curve.add_point( cursor,
-cursor + Vector2(float(string_array[i+3]), float(string_array[i+4])),
cursor - Vector2(float(string_array[i+3]), float(string_array[i+4]))
)
i += 6
"s":
while string_array.size() > i + 4 and string_array[i+1].is_valid_float():
cursor += Vector2(float(string_array[i+3]), float(string_array[i+4]))
points.append(cursor)
i += 4
"S":
while string_array.size() > i + 4 and string_array[i+1].is_valid_float():
cursor = Vector2(float(string_array[i+3]), float(string_array[i+4]))
points.append(cursor)
i += 4
if use_path2d and curve.get_point_count() > 1:
create_path2d( element.get_named_attribute_value("id") + "_" + str(string_array_count),
current_node,
curve,
get_svg_transform(element),
get_svg_style(element))
elif string_array[string_array.size()-1].to_upper() == "Z": #closed polygon
create_polygon2d( element.get_named_attribute_value("id") + "_" + str(string_array_count),
current_node,
points,
get_svg_transform(element),
get_svg_style(element))
else:
create_line2d( element.get_named_attribute_value("id") + "_" + str(string_array_count),
current_node,
points,
get_svg_transform(element),
get_svg_style(element))
func create_path2d( name:String,
parent:Node,
curve:Curve2D,
transform:Transform2D,
style:Dictionary) -> void:
var new_path = Path2D.new()
new_path.name = name
new_path.transform = transform
parent.add_child(new_path)
new_path.set_owner(root_node)
new_path.curve = curve
#style
if style.has("stroke"):
new_path.modulate = Color(style["stroke"])
# if style.has("stroke-width"):
# new_path.width = float(style["stroke-width"])
func create_line2d( name:String,
parent:Node,
points:PoolVector2Array,
transform:Transform2D,
style:Dictionary) -> void:
var new_line = Line2D.new()
new_line.name = name
new_line.transform = transform
parent.add_child(new_line)
new_line.set_owner(root_node)
new_line.points = points
#style
if style.has("stroke"):
new_line.default_color = Color(style["stroke"])
if style.has("stroke-width"):
new_line.width = float(style["stroke-width"])
func create_polygon2d( name:String,
parent:Node,
points:PoolVector2Array,
transform:Transform2D,
style:Dictionary) -> void:
var new_poly
#style
if style.has("fill") and style["fill"] != "none":
#create base
new_poly = Polygon2D.new()
new_poly.name = name
parent.add_child(new_poly)
new_poly.set_owner(root_node)
new_poly.transform = transform
new_poly.polygon = points
new_poly.color = Color(style["fill"])
if style.has("stroke") and style["stroke"] != "none":
#create outline
var new_outline = Line2D.new()
new_outline.name = name + "_stroke"
if new_poly:
new_poly.add_child(new_outline)
else:
parent.add_child(new_outline)
new_outline.transform = transform
new_outline.set_owner(root_node)
points.append(points[0])
new_outline.points = points
new_outline.default_color = Color(style["stroke"])
if style.has("stroke-width"):
new_outline.width = float(style["stroke-width"])
static func get_svg_transform(element:XMLParser) -> Transform2D:
var transform = Transform2D.IDENTITY
if element.has_attribute("transform"):
var svg_transform = element.get_named_attribute_value("transform")
#check transform method
if svg_transform.begins_with("translate"):
svg_transform = svg_transform.replace("translate", "").replacen("(", "").replacen(")", "")
var transform_split = svg_transform.split_floats(",")
transform[2] = Vector2(transform_split[0], transform_split[1])
elif svg_transform.begins_with("matrix"):
svg_transform = svg_transform.replace("matrix", "").replacen("(", "").replacen(")", "")
var matrix = svg_transform.split_floats(",")
for i in 3:
transform[i] = Vector2(matrix[i*2], matrix[i*2+1])
return transform
static func get_svg_style(element:XMLParser) -> Dictionary:
var style = {}
if element.has_attribute("style"):
var svg_style = element.get_named_attribute_value("style")
svg_style = svg_style.replacen(":", "\":\"")
svg_style = svg_style.replacen(";", "\",\"")
svg_style = "{\"" + svg_style + "\"}"
style = parse_json(svg_style)
return style