1#!/usr/local/bin/python 2 3""" 4This script converts a subset of SVG into an HTML imagemap 5 6Note *subset*. It only handles <path> elements, for which it only pays 7attention to the M and L commands. Further, it only notices the "translate" 8transform. 9 10It was written to generate the examples in the documentation for maphilight, 11and thus is very squarely aimed at handling several SVG maps from wikipedia. 12It *assumes* that all the <path>s it will need are inside a <g>. Any <path> 13outside of a <g> will be ignored. 14 15It takes several possible arguments, in the form: 16$ svn2imagemap.py FILENAME [x y [group1 group2 ... groupN]] 17 18FILENAME must be the name of an SVG file. All other arguments are optional. 19 20x and y, if present, are the dimensions of the image you'll be creating from 21the SVG. If not present, it assumes the values of the width and height 22attributes in the SVG file. 23 24group1 through groupN are group ids. If only want particular groups used, 25enter their ids here and all others will be ignored. 26""" 27import os 28import re 29import sys 30import xml.dom.minidom 31 32import parse_path 33 34if len(sys.argv) == 1: 35 sys.exit("svn2imagemap.py FILENAME [x y [group1 group2 ... groupN]]") 36if not os.path.exists(sys.argv[1]): 37 sys.exit("Input file does not exist") 38x, y, groups = None, None, None 39if len(sys.argv) >= 3: 40 x = float(sys.argv[2]) 41 y = float(sys.argv[3]) 42 if len(sys.argv) > 3: 43 groups = sys.argv[4:] 44 45svg_file = xml.dom.minidom.parse(sys.argv[1]) 46svg = svg_file.getElementsByTagName('svg')[0] 47 48raw_width = float(svg.getAttribute('width')) 49raw_height = float(svg.getAttribute('height')) 50width_ratio = x and (x / raw_width) or 1 51height_ratio = y and (y / raw_height) or 1 52 53if groups: 54 elements = [g for g in svg.getElementsByTagName('g') if (g.hasAttribute('id') and g.getAttribute('id') in groups)] 55 elements.extend([p for p in svg.getElementsByTagName('path') if (p.hasAttribute('id') and p.getAttribute('id') in groups)]) 56else: 57 elements = svg.getElementsByTagName('g') 58 59parsed_groups = {} 60for e in elements: 61 paths = [] 62 if e.nodeName == 'g': 63 for path in e.getElementsByTagName('path'): 64 points = parse_path.get_points(path.getAttribute('d')) 65 for pointset in points: 66 paths.append([path.getAttribute('id'), pointset]) 67 else: 68 points = parse_path.get_points(e.getAttribute('d')) 69 for pointset in points: 70 paths.append([e.getAttribute('id'), pointset]) 71 if e.hasAttribute('transform'): 72 print(e.getAttribute('id'), e.getAttribute('transform')) 73 for transform in re.findall(r'(\w+)\((-?\d+.?\d*),(-?\d+.?\d*)\)', e.getAttribute('transform')): 74 if transform[0] == 'translate': 75 x_shift = float(transform[1]) 76 y_shift = float(transform[2]) 77 for path in paths: 78 path[1] = [(p[0] + x_shift, p[1] + y_shift) for p in path[1]] 79 80 parsed_groups[e.getAttribute('id')] = paths 81 82out = [] 83for g in parsed_groups: 84 for path in parsed_groups[g]: 85 out.append('<area href="#" title="%s" shape="poly" coords="%s"></area>' % 86 (path[0], ', '.join([("%d,%d" % (p[0]*width_ratio, p[1]*height_ratio)) for p in path[1]]))) 87 88outfile = open(sys.argv[1].replace('.svg', '.html'), 'w') 89outfile.write('\n'.join(out)) 90