Mercurial > hg > dmlib
annotate svg2qd.py @ 251:98483680ea0f
Improve converter and adjust parser accordingly.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 09 Oct 2012 20:59:30 +0300 |
parents | 3520489320bc |
children | 9c33e11c3d39 |
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 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
17 def getTransform(elem) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
18 if "transform" in elem.attrib : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
19 ntrans = elem.attrib["transform"] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
20 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
|
21 for trns in tmatch.finditer(ntrans) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
22 coord = trns.group(1).split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
23 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
|
24 return None |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
25 |
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 def getStyle(elem) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
28 style = {} |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
29 if "style" in elem.attrib : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
30 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
|
31 kv = elem.split(":") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 style[kv[0]] = kv[1] |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
33 return style |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
34 |
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 def printVertices(type, vertices, width) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
37 if len(vertices) > 0 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
38 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
|
39 str = "# " |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
40 if type == "m" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
41 str = "R" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
42 elif type == "M" : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
43 str = "L" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
44 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
|
45 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
46 return "" |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
47 |
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 def printPath(path, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
50 style = getStyle(path) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
51 width = bf(style["stroke-width"]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
53 trans = getTransform(path) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
54 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 print "{}G{}".format(" "*level, printVertex(trans)) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
57 out = "" |
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(" ") : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 if elem == "m" or elem == "M": |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 out += printVertices(type, vertices, width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 type = elem |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 elif elem == "z" : |
251
98483680ea0f
Improve converter and adjust parser accordingly.
Matti Hamalainen <ccr@tnsp.org>
parents:
250
diff
changeset
|
65 vertices.append("Z") |
250
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
66 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
67 tmp = elem.split(",") |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
68 px = float(tmp[0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
69 py = float(tmp[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
70 vertices.append([px, py, 0]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
71 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
72 out += printVertices(type, vertices, width) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
73 print "{}{}".format(" "*level, out) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
74 if trans : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
75 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
76 |
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 def iterateDocument(elems, level) : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
79 for elem in elems: |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
80 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
|
81 print "\n{}# GROUP".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
82 tmp = getTransform(elem) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
83 if tmp : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
84 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
|
85 iterateDocument(elem, level + 1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
86 print "{}E\n".format(" "*level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
87 else : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
88 iterateDocument(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
89 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
|
90 printPath(elem, level) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
91 |
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 # Ns. paaohjelma |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
94 if len(sys.argv) != 2 : |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
95 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
|
96 sys.exit(1) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
97 |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
98 tree = ET.parse(sys.argv[1]) |
3520489320bc
Add simplistic SVG to Q3D converter written in Python.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
99 iterateDocument(tree.getroot(), 0) |