comparison svg2qd.py @ 255:cac0b6cfebb4

Improve python converter.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 09 Oct 2012 22:08:12 +0300
parents 9c33e11c3d39
children 31ce6d32408f
comparison
equal deleted inserted replaced
254:9c33e11c3d39 255:cac0b6cfebb4
32 kv = elem.split(":") 32 kv = elem.split(":")
33 style[kv[0]] = kv[1] 33 style[kv[0]] = kv[1]
34 return style 34 return style
35 35
36 36
37 def printVertices(type, vertices, width) : 37 def printVertices(type, vertices, width, level) :
38 if len(vertices) > 0 : 38 if len(vertices) > 0 :
39 list = map(lambda v:printVertex(v), vertices) 39 list = map(lambda v:printVertex(v), vertices)
40 str = "# " 40 str = "# "+ type
41 if type == "m" : 41 if type == "m" :
42 str = "R" 42 str = "R"
43 elif type == "M" : 43 elif type == "M" :
44 str = "L" 44 str = "L"
45 return str + "{} {} {}".format(len(vertices)-1, " ".join(list), width) 45 elif type == "c" :
46 else : 46 str = "R"
47 return "" 47 print "{}{}{} {} {}".format(" "*level, str, len(vertices)-1, " ".join(list), width)
48 48
49 49
50 def printPath(path, level) : 50 def printPath(path, level) :
51 style = getStyle(path) 51 style = getStyle(path)
52 width = bf(style["stroke-width"]) 52 width = bf(style["stroke-width"])
53 53
54 trans = getTransform(path) 54 trans = getTransform(path)
55 if trans : 55 if trans :
56 print "{}G{}".format(" "*level, printVertex(trans)) 56 print "{}G{}".format(" "*level, printVertex(trans))
57 57
58 out = ""
59 vertices = [] 58 vertices = []
60 type = "" 59 type = ""
61 for elem in path.attrib["d"].split(" ") : 60 for elem in path.attrib["d"].split(" ") :
62 if elem == "m" or elem == "M" or elem == "c": 61 if elem == "m" or elem == "M" or elem == "c" or elem == "C":
63 out += printVertices(type, vertices, width) 62 printVertices(type, vertices, width, level)
63 vertices = []
64 type = elem 64 type = elem
65 elif elem == "z" : 65 elif elem == "z" :
66 vertices.append("Z") 66 vertices.append("Z")
67 else : 67 else :
68 tmp = elem.split(",") 68 tmp = elem.split(",")
69 px = float(tmp[0]) 69 px = float(tmp[0])
70 py = float(tmp[1]) 70 py = float(tmp[1])
71 vertices.append([px, py, 0]) 71 vertices.append([px, py, 0])
72 72
73 out += printVertices(type, vertices, width) 73 printVertices(type, vertices, width, level)
74 print "{}{}".format(" "*level, out)
75 if trans : 74 if trans :
76 print "{}E\n".format(" "*level) 75 print "{}E\n".format(" "*level)
77 76
78 77
79 def iterateDocument(elems, level) : 78 def iterateDocument(elems, level) :