simplepath.py revision 8caa7597ccfbc50526a6ad201b599bcb3f827773
"""
functions for digesting paths into a simple list structure
Copyright (C) 2005 Aaron Spike, aaron@ekips.org
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
"""
def lexPath(d):
"""
returns and iterator that breaks path data
identifies command and parameter tokens
"""
offset = 0
while 1:
if m:
break
if m:
continue
if m:
continue
#TODO: create new exception
raise Exception, 'Invalid path data!'
'''
pathdefs = {commandfamily:
[
implicitnext,
#params,
[casts,cast,cast],
[coord type,x,y,0]
]}
'''
pathdefs = {
'Z':['L', 0, [], []]
}
def parsePath(d):
"""
Parse SVG path and return an array of segments.
Removes all shorthand notation.
Converts coordinates to absolute.
"""
retval = []
lastCommand = ''
while 1:
try:
except StopIteration:
break
params = []
if isCommand:
raise Exception, 'Invalid path, must begin with moveto.'
else:
else:
#command was omited
#use last command's implicit next command
if lastCommand:
if lastCommand.isupper():
else:
else:
raise Exception, 'Invalid path, no initial command.'
while numParams > 0:
if needParam:
try:
if isCommand:
raise Exception, 'Invalid number of parameters'
except StopIteration:
raise Exception, 'Unexpected end of path'
numParams -= 1
#segment is now absolute so
#Flesh out shortcut notation
if outputCommand == 'H':
if outputCommand == 'V':
outputCommand = 'L'
if outputCommand == 'S':
outputCommand = 'C'
if outputCommand == 'T':
outputCommand = 'Q'
#current values become "last" values
if outputCommand == 'M':
if outputCommand == 'Z':
else:
else:
return retval
def formatPath(a):
"""Format SVG path data from an array"""
def translatePath(p, x, y):
params[i] += x
params[i] += y
def scalePath(p, x, y):
params[i] *= x
params[i] *= y
if a == 0:
return p
if r != 0: