def parse(filename): svg_file = xml.dom.minidom.parse(filename) svg = svg_file.getElementsByTagName('svg')[0] x, y = 800, 600 #x , y = None, None groups = 'States' groups = None width = svg.getAttribute('width') height = svg.getAttribute('height') #width, height = 958.691, 592.79 raw_width = float(width) raw_height = float(height) width_ratio = x and (x / raw_width) or 1 height_ratio = y and (y / raw_height) or 1 if groups: elements = [g for g in svg.getElementsByTagName('g') if (g.hasAttribute('id') and g.getAttribute('id') in groups)] elements.extend([p for p in svg.getElementsByTagName('path') if (p.hasAttribute('id') and p.getAttribute('id') in groups)]) else: elements = svg.getElementsByTagName('glyph') parsed_groups = {} for e in elements: paths = [] if e.nodeName == 'glyph': for path in e.getElementsByTagName('path'): points = parse_path.get_points(path.getAttribute('d')) print points #print points #points = path.getAttribute('d') for pointset in points: paths.append([path.getAttribute('id'), pointset]) else: points = parse_path.get_points(e.getAttribute('d')) for pointset in points: paths.append([e.getAttribute('id'), pointset]) if e.hasAttribute('transform'): #print e.getAttribute('id'), e.getAttribute('transform') for transform in re.findall(r'(\w+)\((-?\d+.?\d*),(-?\d+.?\d*)\)', e.getAttribute('transform')): if transform[0] == 'translate': x_shift = float(transform[1]) y_shift = float(transform[2]) for path in paths: path[1] = [(p[0] + x_shift, p[1] + y_shift) for p in path[1]] parsed_groups[e.getAttribute('id')] = paths return parsed_groups
def points_from_path(path): paths = [] points = parse_path.get_points(path.getAttribute('d')) for pointset in points: paths.append([path.getAttribute('id'), pointset]) apply_element_transform(paths, path) return paths
def parse(filename): svg_file = xml.dom.minidom.parse(filename) svg = svg_file.getElementsByTagName("svg")[0] x, y = 800, 600 width = svg.getAttribute("width") height = svg.getAttribute("height") raw_width = float(width) raw_height = float(height) width_ratio = x and (x / raw_width) or 1 height_ratio = y and (y / raw_height) or 1 elements = [g for g in svg.getElementsByTagName("g") if (g.hasAttribute("id"))] elements.extend([p for p in svg.getElementsByTagName("path") if (p.hasAttribute("id"))]) parsed_groups = {} colors = {} for e in elements: color = e.getAttribute("style")[6:12] paths = [] points = parse_path.get_points(e.getAttribute("d")) for pointset in points: paths.append([e.getAttribute("id"), pointset]) parsed_groups[e.getAttribute("id")] = paths colors[e.getAttribute("id")] = color return parsed_groups
raw_height = float(svg.getAttribute('height')) width_ratio = x and (x / raw_width) or 1 height_ratio = y and (y / raw_height) or 1 if groups: elements = [g for g in svg.getElementsByTagName('g') if (g.hasAttribute('id') and g.getAttribute('id') in groups)] elements.extend([p for p in svg.getElementsByTagName('path') if (p.hasAttribute('id') and p.getAttribute('id') in groups)]) else: elements = svg.getElementsByTagName('g') parsed_groups = {} for e in elements: paths = [] if e.nodeName == 'g': for path in e.getElementsByTagName('path'): points = parse_path.get_points(path.getAttribute('d')) for pointset in points: paths.append([path.getAttribute('id'), pointset]) else: points = parse_path.get_points(e.getAttribute('d')) for pointset in points: paths.append([e.getAttribute('id'), pointset]) if e.hasAttribute('transform'): print e.getAttribute('id'), e.getAttribute('transform') for transform in re.findall(r'(\w+)\((-?\d+.?\d*),(-?\d+.?\d*)\)', e.getAttribute('transform')): if transform[0] == 'translate': x_shift = float(transform[1]) y_shift = float(transform[2]) for path in paths: path[1] = [(p[0] + x_shift, p[1] + y_shift) for p in path[1]]
g for g in svg.getElementsByTagName('g') if (g.hasAttribute('id') and g.getAttribute('id') in groups) ] elements.extend([ p for p in svg.getElementsByTagName('path') if (p.hasAttribute('id') and p.getAttribute('id') in groups) ]) else: elements = svg.getElementsByTagName('g') parsed_groups = {} for e in elements: paths = [] if e.nodeName == 'g': for path in e.getElementsByTagName('path'): points = parse_path.get_points(path.getAttribute('d')) for pointset in points: paths.append([path.getAttribute('id'), pointset]) else: points = parse_path.get_points(e.getAttribute('d')) for pointset in points: paths.append([e.getAttribute('id'), pointset]) if e.hasAttribute('transform'): print e.getAttribute('id'), e.getAttribute('transform') for transform in re.findall(r'(\w+)\((-?\d+.?\d*),(-?\d+.?\d*)\)', e.getAttribute('transform')): if transform[0] == 'translate': x_shift = float(transform[1]) y_shift = float(transform[2]) for path in paths: path[1] = [(p[0] + x_shift, p[1] + y_shift)