Mercurial > hg > dmlib
annotate svg2qd.py @ 300:4972ca91d062
Cleanups.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 11 Oct 2012 17:55:15 +0300 |
parents | 31ce6d32408f |
children |
rev | line source |
---|---|
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 import sys |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
3 import re |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
4 import xml.etree.ElementTree as ET |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 def bf(x) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
8 return int(round(float(x))) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
11 def printVertex(v) : |
251
98483680ea0f
Improve converter and adjust parser accordingly.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
12 if type(v) is list : |
98483680ea0f
Improve converter and adjust parser accordingly.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
13 return "{:.2f},{:.2f},{:.2f}".format(v[0], v[1], v[2]) |
98483680ea0f
Improve converter and adjust parser accordingly.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
14 else : |
98483680ea0f
Improve converter and adjust parser accordingly.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
15 return v |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
16 |
254 | 17 |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 def getTransform(elem) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 if "transform" in elem.attrib : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 ntrans = elem.attrib["transform"] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
21 tmatch = re.compile(r"translate\((.*?)\)", re.IGNORECASE) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 for trns in tmatch.finditer(ntrans) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 coord = trns.group(1).split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
24 return [float(coord[0]), float(coord[1]), 0] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 return None |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
26 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
27 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 def getStyle(elem) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 style = {} |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 if "style" in elem.attrib : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 for elem in elem.attrib["style"].split(";") : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 kv = elem.split(":") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 style[kv[0]] = kv[1] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 return style |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
35 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
36 |
255 | 37 def printVertices(type, vertices, width, level) : |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 if len(vertices) > 0 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
39 list = map(lambda v:printVertex(v), vertices) |
255 | 40 str = "# "+ type |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 if type == "m" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 str = "R" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 elif type == "M" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 str = "L" |
255 | 45 elif type == "c" : |
46 str = "R" | |
47 print "{}{}{} {} {}".format(" "*level, str, len(vertices)-1, " ".join(list), width) | |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
48 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
49 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 def printPath(path, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 style = getStyle(path) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 width = bf(style["stroke-width"]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 trans = getTransform(path) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 print "{}G{}".format(" "*level, printVertex(trans)) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 vertices = [] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 type = "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 for elem in path.attrib["d"].split(" ") : |
256
31ce6d32408f
Error out when bezier segments are found.
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
61 if elem == "m" or elem == "M" : |
255 | 62 printVertices(type, vertices, width, level) |
63 vertices = [] | |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 type = elem |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
65 elif elem == "z" : |
251
98483680ea0f
Improve converter and adjust parser accordingly.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
66 vertices.append("Z") |
256
31ce6d32408f
Error out when bezier segments are found.
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
67 elif elem == "c" or elem == "C" : |
31ce6d32408f
Error out when bezier segments are found.
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
68 print "Curves not supported! Path ID '{}':\n{}".format(path.attrib["id"], path.attrib["d"]) |
31ce6d32408f
Error out when bezier segments are found.
Matti Hamalainen <ccr@tnsp.org>
parents:
255
diff
changeset
|
69 sys.exit(0) |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 tmp = elem.split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 px = float(tmp[0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 py = float(tmp[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 vertices.append([px, py, 0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 |
255 | 76 printVertices(type, vertices, width, level) |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 def iterateDocument(elems, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 for elem in elems: |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 if elem.tag == "{http://www.w3.org/2000/svg}g" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 print "\n{}# GROUP".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 tmp = getTransform(elem) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 if tmp : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 print "{}G{}".format(" "*level, printVertex(getTransform(elem))) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 iterateDocument(elem, level + 1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 iterateDocument(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 elif elem.tag == "{http://www.w3.org/2000/svg}path" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 printPath(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 # Ns. paaohjelma |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 if len(sys.argv) != 2 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 print "Usage: "+sys.argv[0]+" <input.svg>" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 sys.exit(1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
101 tree = ET.parse(sys.argv[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
102 iterateDocument(tree.getroot(), 0) |