Preserve order of SVG paths in extracted version.

pull/211/head
Brian White 2023-06-04 08:11:19 -04:00
parent eef26cfc23
commit 8f539a3822
1 changed files with 33 additions and 38 deletions

View File

@ -205,45 +205,40 @@ def svg2paths(svg_file_location,
# Short name for all the dom2dict calls below # Short name for all the dom2dict calls below
ip = include_parent_info ip = include_parent_info
# Use minidom to extract path strings from input SVG d_strings = []
paths = [dom2dict(el,ip) for el in doc.getElementsByTagName('path')] attribute_dictionary_list = []
d_strings = [el['d'] for el in paths]
attribute_dictionary_list = paths
# Use minidom to extract polyline strings from input SVG, convert to # Use minidom to extract path strings from input SVG.
# path strings, add to list # Each type is handled seperately but the overall order is preserved.
if convert_polylines_to_paths: for el in doc.getElementsByTagName('*'):
plins = [dom2dict(el,ip) for el in doc.getElementsByTagName('polyline')] if el.tagName == 'path':
d_strings += [polyline2pathd(pl) for pl in plins] path_data = dom2dict(el, ip)
attribute_dictionary_list += plins d_strings.append(path_data['d'])
attribute_dictionary_list.append(path_data)
# Use minidom to extract polygon strings from input SVG, convert to elif el.tagName == 'polyline' and convert_polylines_to_paths:
# path strings, add to list polyline_data = dom2dict(el, ip)
if convert_polygons_to_paths: d_strings.append(polyline2pathd(polyline_data))
pgons = [dom2dict(el,ip) for el in doc.getElementsByTagName('polygon')] attribute_dictionary_list.append(polyline_data)
d_strings += [polygon2pathd(pg) for pg in pgons] elif el.tagName == 'polygon' and convert_polygons_to_paths:
attribute_dictionary_list += pgons polygon_data = dom2dict(el, ip)
d_strings.append(polygon2pathd(polygon_data))
if convert_lines_to_paths: attribute_dictionary_list.append(polygon_data)
lines = [dom2dict(el,ip) for el in doc.getElementsByTagName('line')] elif el.tagName == 'line' and convert_lines_to_paths:
d_strings += [('M' + l['x1'] + ' ' + l['y1'] + line_data = dom2dict(el, ip)
'L' + l['x2'] + ' ' + l['y2']) for l in lines] d_strings.append(f"M{line_data['x1']} {line_data['y1']} L{line_data['x2']} {line_data['y2']}")
attribute_dictionary_list += lines attribute_dictionary_list.append(line_data)
elif el.tagName == 'ellipse' and convert_ellipses_to_paths:
if convert_ellipses_to_paths: ellipse_data = dom2dict(el, ip)
ellipses = [dom2dict(el,ip) for el in doc.getElementsByTagName('ellipse')] d_strings.append(ellipse2pathd(ellipse_data))
d_strings += [ellipse2pathd(e) for e in ellipses] attribute_dictionary_list.append(ellipse_data)
attribute_dictionary_list += ellipses elif el.tagName == 'circle' and convert_circles_to_paths:
circle_data = dom2dict(el, ip)
if convert_circles_to_paths: d_strings.append(ellipse2pathd(circle_data))
circles = [dom2dict(el,ip) for el in doc.getElementsByTagName('circle')] attribute_dictionary_list.append(circle_data)
d_strings += [ellipse2pathd(c) for c in circles] elif el.tagName == 'rect' and convert_rectangles_to_paths:
attribute_dictionary_list += circles rect_data = dom2dict(el, ip)
d_strings.append(rect2pathd(rect_data))
if convert_rectangles_to_paths: attribute_dictionary_list.append(rect_data)
rectangles = [dom2dict(el,ip) for el in doc.getElementsByTagName('rect')]
d_strings += [rect2pathd(r) for r in rectangles]
attribute_dictionary_list += rectangles
path_list = [parse_path(d) for d in d_strings] path_list = [parse_path(d) for d in d_strings]
retval = [path_list, attribute_dictionary_list] retval = [path_list, attribute_dictionary_list]