Mercurial > hg > dmlib
annotate svg2qd.py @ 254:9c33e11c3d39
C nodes.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 09 Oct 2012 21:49:47 +0300 |
parents | 98483680ea0f |
children | cac0b6cfebb4 |
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 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 def printVertices(type, vertices, width) : |
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) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 str = "# " |
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" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
45 return str + "{} {} {}".format(len(vertices)-1, " ".join(list), width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 return "" |
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 out = "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
59 vertices = [] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 type = "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 for elem in path.attrib["d"].split(" ") : |
254 | 62 if elem == "m" or elem == "M" or elem == "c": |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 out += printVertices(type, vertices, width) |
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") |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 tmp = elem.split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 px = float(tmp[0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 py = float(tmp[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 vertices.append([px, py, 0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 out += printVertices(type, vertices, width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 print "{}{}".format(" "*level, out) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
77 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
78 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 def iterateDocument(elems, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 for elem in elems: |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
81 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
|
82 print "\n{}# GROUP".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 tmp = getTransform(elem) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 if tmp : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
85 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
|
86 iterateDocument(elem, level + 1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 iterateDocument(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
90 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
|
91 printPath(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
92 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
93 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 # Ns. paaohjelma |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 if len(sys.argv) != 2 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
96 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
|
97 sys.exit(1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 tree = ET.parse(sys.argv[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
100 iterateDocument(tree.getroot(), 0) |