printrun-src/printrun/svg2gcode/simplepath.py

Tue, 19 Jan 2021 20:45:09 +0100

author
mdd
date
Tue, 19 Jan 2021 20:45:09 +0100
changeset 45
c82943fb205f
parent 16
36d478bde840
permissions
-rw-r--r--

updated main files to new github master version

16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
2 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
3 simplepath.py
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
4 functions for digesting paths into a simple list structure
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
5
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
6 Copyright (C) 2005 Aaron Spike, aaron@ekips.org
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
7
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
8 This program is free software; you can redistribute it and/or modify
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
10 the Free Software Foundation; either version 2 of the License, or
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
11 (at your option) any later version.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
12
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
16 GNU General Public License for more details.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
17
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
19 along with this program; if not, write to the Free Software
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
21
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
22 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
23 import re, math
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
24
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
25 def lexPath(d):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
26 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
27 returns and iterator that breaks path data
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
28 identifies command and parameter tokens
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
29 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
30 offset = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
31 length = len(d)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
32 delim = re.compile(r'[ \t\r\n,]+')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
33 command = re.compile(r'[MLHVCSQTAZmlhvcsqtaz]')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
34 parameter = re.compile(r'(([-+]?[0-9]+(\.[0-9]*)?|[-+]?\.[0-9]+)([eE][-+]?[0-9]+)?)')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
35 while 1:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
36 m = delim.match(d, offset)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
37 if m:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
38 offset = m.end()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
39 if offset >= length:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
40 break
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
41 m = command.match(d, offset)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
42 if m:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
43 yield [d[offset:m.end()], True]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
44 offset = m.end()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
45 continue
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
46 m = parameter.match(d, offset)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
47 if m:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
48 yield [d[offset:m.end()], False]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
49 offset = m.end()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
50 continue
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
51 #TODO: create new exception
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
52 raise Exception, 'Invalid path data!'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
53 '''
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
54 pathdefs = {commandfamily:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
55 [
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
56 implicitnext,
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
57 #params,
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
58 [casts,cast,cast],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
59 [coord type,x,y,0]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
60 ]}
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
61 '''
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
62 pathdefs = {
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
63 'M':['L', 2, [float, float], ['x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
64 'L':['L', 2, [float, float], ['x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
65 'H':['H', 1, [float], ['x']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
66 'V':['V', 1, [float], ['y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
67 'C':['C', 6, [float, float, float, float, float, float], ['x','y','x','y','x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
68 'S':['S', 4, [float, float, float, float], ['x','y','x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
69 'Q':['Q', 4, [float, float, float, float], ['x','y','x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
70 'T':['T', 2, [float, float], ['x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
71 'A':['A', 7, [float, float, float, int, int, float, float], [0,0,0,0,0,'x','y']],
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
72 'Z':['L', 0, [], []]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
73 }
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
74 def parsePath(d):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
75 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
76 Parse SVG path and return an array of segments.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
77 Removes all shorthand notation.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
78 Converts coordinates to absolute.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
79 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
80 retval = []
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
81 lexer = lexPath(d)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
82
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
83 pen = (0.0,0.0)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
84 subPathStart = pen
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
85 lastControl = pen
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
86 lastCommand = ''
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
87
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
88 while 1:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
89 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
90 token, isCommand = lexer.next()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
91 except StopIteration:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
92 break
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
93 params = []
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
94 needParam = True
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
95 if isCommand:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
96 if not lastCommand and token.upper() != 'M':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
97 raise Exception, 'Invalid path, must begin with moveto.'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
98 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
99 command = token
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
100 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
101 #command was omited
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
102 #use last command's implicit next command
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
103 needParam = False
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
104 if lastCommand:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
105 if lastCommand.isupper():
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
106 command = pathdefs[lastCommand][0]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
107 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
108 command = pathdefs[lastCommand.upper()][0].lower()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
109 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
110 raise Exception, 'Invalid path, no initial command.'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
111 numParams = pathdefs[command.upper()][1]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
112 while numParams > 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
113 if needParam:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
114 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
115 token, isCommand = lexer.next()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
116 if isCommand:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
117 raise Exception, 'Invalid number of parameters'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
118 except StopIteration:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
119 raise Exception, 'Unexpected end of path'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
120 cast = pathdefs[command.upper()][2][-numParams]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
121 param = cast(token)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
122 if command.islower():
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
123 if pathdefs[command.upper()][3][-numParams]=='x':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
124 param += pen[0]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
125 elif pathdefs[command.upper()][3][-numParams]=='y':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
126 param += pen[1]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
127 params.append(param)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
128 needParam = True
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
129 numParams -= 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
130 #segment is now absolute so
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
131 outputCommand = command.upper()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
132
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
133 #Flesh out shortcut notation
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
134 if outputCommand in ('H','V'):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
135 if outputCommand == 'H':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
136 params.append(pen[1])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
137 if outputCommand == 'V':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
138 params.insert(0,pen[0])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
139 outputCommand = 'L'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
140 if outputCommand in ('S','T'):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
141 params.insert(0,pen[1]+(pen[1]-lastControl[1]))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
142 params.insert(0,pen[0]+(pen[0]-lastControl[0]))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
143 if outputCommand == 'S':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
144 outputCommand = 'C'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
145 if outputCommand == 'T':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
146 outputCommand = 'Q'
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
147
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
148 #current values become "last" values
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
149 if outputCommand == 'M':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
150 subPathStart = tuple(params[0:2])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
151 pen = subPathStart
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
152 if outputCommand == 'Z':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
153 pen = subPathStart
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
154 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
155 pen = tuple(params[-2:])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
156
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
157 if outputCommand in ('Q','C'):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
158 lastControl = tuple(params[-4:-2])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
159 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
160 lastControl = pen
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
161 lastCommand = command
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
162
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
163 retval.append([outputCommand,params])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
164 return retval
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
165
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
166 def formatPath(a):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
167 """Format SVG path data from an array"""
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
168 return "".join([cmd + " ".join([str(p) for p in params]) for cmd, params in a])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
169
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
170 def translatePath(p, x, y):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
171 for cmd,params in p:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
172 defs = pathdefs[cmd]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
173 for i in range(defs[1]):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
174 if defs[3][i] == 'x':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
175 params[i] += x
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
176 elif defs[3][i] == 'y':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
177 params[i] += y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
178
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
179 def scalePath(p, x, y):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
180 for cmd,params in p:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
181 defs = pathdefs[cmd]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
182 for i in range(defs[1]):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
183 if defs[3][i] == 'x':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
184 params[i] *= x
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
185 elif defs[3][i] == 'y':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
186 params[i] *= y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
187
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
188 def rotatePath(p, a, cx = 0, cy = 0):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
189 if a == 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
190 return p
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
191 for cmd,params in p:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
192 defs = pathdefs[cmd]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
193 for i in range(defs[1]):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
194 if defs[3][i] == 'x':
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
195 x = params[i] - cx
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
196 y = params[i + 1] - cy
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
197 r = math.sqrt((x**2) + (y**2))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
198 if r != 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
199 theta = math.atan2(y, x) + a
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
200 params[i] = (r * math.cos(theta)) + cx
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
201 params[i + 1] = (r * math.sin(theta)) + cy
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
202

mercurial