Changeset 617
- Timestamp:
- 07/13/03 01:18:47 (5 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/RBRapier/RBRapier/Formats/Attic/SVG.old/SVGSkin/SVGPathBuilder.py
r616 r617 20 20 ##~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 21 21 22 """ 23 Path commands (info from http://www.protocol7.com/svg-wiki/ow.asp?ThePath) 24 25 moveto 26 M absolute moveto (x y)+ 27 m relative moveto (x y)+ 28 closepath 29 Z absolute closepath (none) 30 z relative closepath (none) 31 lineto 32 L absolute lineto (x y)+ 33 l relative lineto (x y)+ 34 horizontal lineto 35 H absolute horizontal lineto x+ 36 h relative horizontal lineto x+ 37 vertical lineto 38 V absolute vertical lineto y+ 39 v relative vertical lineto y+ 40 curveto (cubic bezier) 41 C absolute curveto (x1 y1 x2 y2 x y)+ 42 c relative curveto (x1 y1 x2 y2 x y)+ 43 smooth curveto 44 S absolute smooth curveto (x2 y2 x y)+ 45 s relative smooth curveto (x2 y2 x y)+ 46 quadratic bezier curveto 47 Q absolute quadratic bezier curveto (x1 y1 x y)+ 48 q relative quadratic bezier curveto (x1 y1 x y)+ 49 smooth quadratic bezier curveto 50 T absolute smooth quadratic bezier curveto (x y)+ 51 t relative smooth quadratic bezier curveto (x y)+ 52 eliptical arc 53 A absolute elliptical arc (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ 54 """ 55 22 56 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23 57 #~ Imports … … 28 62 29 63 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 64 #~ Definitions 65 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66 67 def _pathcommand(name, relative, nextcommand, argtypes): 68 return {'name':name, 'relative':relative, 'nextcommand':nextcommand, 'argtypes':argtypes} 69 70 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30 71 #~ Path Factories 31 72 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 32 73 33 74 class PathFactoryAbstract(object): 75 def BeginPath(self, pathstr): 76 raise NotImplementedError 77 def EndPath(self, pathstr): 78 raise NotImplementedError 34 79 def MoveTo(self, relative, x, y): 35 80 raise NotImplementedError … … 52 97 def EllipticArcTo(self, relative, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y): 53 98 raise NotImplementedError 54 55 class PathFactoryPrinter(PathFactoryAbstract):56 def MoveTo(self, relative, x, y):57 print "MoveTo", (relative, x, y)58 def ClosePath(self, relative):59 print "ClosePath", (relative,)60 def LineTo(self, relative, x, y):61 print "LineTo", (relative, x, y)62 def HLineTo(self, relative, x):63 print "HLineTo", (relative, x)64 def VLineTo(self, relative, y):65 print "VLineTo", (relative, y)66 def CubicBezierTo(self, relative, x1, y1, x2, y2, x, y):67 print "CubicBezierTo", (relative, x1, y1, x2, y2, x, y)68 def SmoothCurveTo(self, relative, x2, y2, x, y):69 print "SmoothCurveTo", (relative, x2, y2, x, y)70 def QuadraticBezierTo(self, relative, x1, y1, x, y):71 print "QuadraticBezierTo", (relative, x1, y1, x, y)72 def SmoothQuadraticBezierTo(self, relative, x, y):73 print "SmoothQuadraticBezierTo", (relative, x, y)74 def EllipticArcTo(self, relative, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y):75 print "EllipticArcTo", (relative, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y)76 99 77 100 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 87 110 _re_command = re.compile(r'\s*([MmZzLlHhVvCcSsQqTtAa])?\s*') 88 111 _re_coord = re.compile(r'([-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:[eE][-+]?\d+)?)'+_delim) 112 _coord = (_re_coord, float) 89 113 _re_rotation = re.compile(r'([-+]?(?:\d+(?:\.\d*)?|\d*\.\d+)(?:[eE][-+]?\d+)?)'+_delim) 114 _rotation = (_re_rotation, float) 90 115 _re_flag = re.compile(r'(0|1)'+_delim) 91 92 def pathcommand(name, relative, nextcommand, argtypes): 93 return {'name':name, 'relative':relative, 'nextcommand':nextcommand, 'argtypes':argtypes} 94 pathcommand = staticmethod(pathcommand) 116 _flag = (_re_flag, int) 95 117 96 118 pathCommandTable = { 97 'M': pathcommand('MoveTo', False, 'L', (_re_coord, _re_coord)),98 'm': pathcommand('MoveTo', True, 'l', (_re_coord, _re_coord)),99 100 'Z': pathcommand('ClosePath', False, 'M', ()),101 'z': pathcommand('ClosePath', True, 'm', ()),102 103 'L': pathcommand('LineTo', False, None, (_re_coord, _re_coord)),104 'l': pathcommand('LineTo', True, None, (_re_coord, _re_coord)),105 106 'H': pathcommand('HLineTo', False, None, (_re_coord, )),107 'h': pathcommand('HLineTo', True, None, (_re_coord, )),108 109 'V': pathcommand('VLineTo', False, None, (_re_coord, )),110 'v': pathcommand('VLineTo', True, None, (_re_coord, )),111 112 'C': pathcommand('CubicBezierTo', False, None, (_re_coord, _re_coord, _re_coord, _re_coord, _re_coord, _re_coord)),113 'c': pathcommand('CubicBezierTo', True, None, (_re_coord, _re_coord, _re_coord, _re_coord, _re_coord, _re_coord)),114 115 'S': pathcommand('SmoothCurveTo', False, None, (_re_coord, _re_coord, _re_coord, _re_coord)),116 's': pathcommand('SmoothCurveTo', True, None, (_re_coord, _re_coord, _re_coord, _re_coord)),117 118 'Q': pathcommand('QuadraticBezierTo', False, None, (_re_coord, _re_coord, _re_coord, _re_coord)),119 'q': pathcommand('QuadraticBezierTo', True, None, (_re_coord, _re_coord, _re_coord, _re_coord)),120 121 'T': pathcommand('SmoothQuadraticBezierTo', False, None, (_re_coord, _re_coord)),122 't': pathcommand('SmoothQuadraticBezierTo', True, None, (_re_coord, _re_coord)),123 124 'A': pathcommand('EllipticArcTo', False, None, (_re_coord, _re_coord, _re_rotation, _re_flag, _re_flag, _re_coord, _re_coord)),125 'a': pathcommand('EllipticArcTo', True, None, (_re_coord, _re_coord, _re_rotation, _re_flag, _re_flag, _re_coord, _re_coord)),119 'M': _pathcommand('MoveTo', False, 'L', (_coord, _coord)), 120 'm': _pathcommand('MoveTo', True, 'l', (_coord, _coord)), 121 122 'Z': _pathcommand('ClosePath', False, 'M', ()), 123 'z': _pathcommand('ClosePath', True, 'm', ()), 124 125 'L': _pathcommand('LineTo', False, None, (_coord, _coord)), 126 'l': _pathcommand('LineTo', True, None, (_coord, _coord)), 127 128 'H': _pathcommand('HLineTo', False, None, (_coord, )), 129 'h': _pathcommand('HLineTo', True, None, (_coord, )), 130 131 'V': _pathcommand('VLineTo', False, None, (_coord, )), 132 'v': _pathcommand('VLineTo', True, None, (_coord, )), 133 134 'C': _pathcommand('CubicBezierTo', False, None, (_coord, _coord, _coord, _coord, _coord, _coord)), 135 'c': _pathcommand('CubicBezierTo', True, None, (_coord, _coord, _coord, _coord, _coord, _coord)), 136 137 'S': _pathcommand('SmoothCurveTo', False, None, (_coord, _coord, _coord, _coord)), 138 's': _pathcommand('SmoothCurveTo', True, None, (_coord, _coord, _coord, _coord)), 139 140 'Q': _pathcommand('QuadraticBezierTo', False, None, (_coord, _coord, _coord, _coord)), 141 'q': _pathcommand('QuadraticBezierTo', True, None, (_coord, _coord, _coord, _coord)), 142 143 'T': _pathcommand('SmoothQuadraticBezierTo', False, None, (_coord, _coord)), 144 't': _pathcommand('SmoothQuadraticBezierTo', True, None, (_coord, _coord)), 145 146 'A': _pathcommand('EllipticArcTo', False, None, (_coord, _coord, _rotation, _flag, _flag, _coord, _coord)), 147 'a': _pathcommand('EllipticArcTo', True, None, (_coord, _coord, _rotation, _flag, _flag, _coord, _coord)), 126 148 } 127 149 128 def Build(self, pathstr, pathfactory): 129 print "Building path:", repr(pathstr) 150 def __init__(self, pathfactory=None): 151 self.pathfactory = pathfactory 152 153 def Build(self, *args, **kw): 154 return list(self.iterBuild(*args, **kw)) 155 156 def iterBuild(self, pathstr, pathfactory=None): 157 pathfactory = pathfactory or self.pathfactory 158 159 result = pathfactory.BeginPath(pathstr) 160 if result is not None: yield result 161 130 162 matchidx = 0 131 163 # MoveTo is the default command … … 133 165 while matchidx < len(pathstr): 134 166 # Find out what command we're dealing with 135 commandmatch = self. commandre.match(pathstr, matchidx)167 commandmatch = self._re_command.match(pathstr, matchidx) 136 168 if commandmatch is not None: 137 commandidx = commandmatch.group ()169 commandidx = commandmatch.groups()[0] 138 170 matchidx = commandmatch.end() 139 171 140 # Lookup our new command 141 command = self.pathCommandTable[commandidx] 172 if commandidx: 173 # Lookup our new command 174 command = self.pathCommandTable[commandidx] 142 175 143 176 # parse out the arguments for the path element 144 177 arguments = [] 145 for arg in command['argtypes']:146 argmatch = arg .match(pathstr, matchidx)178 for argre, argvalue in command['argtypes']: 179 argmatch = argre.match(pathstr, matchidx) 147 180 if argmatch is None: 148 181 raise ValueError, "Error parsing argument of path at index %d (path=%r)" % (matchidx, pathstr) 149 182 else: 150 arguments.append(arg match.group())183 arguments.append(argvalue(argmatch.groups()[0])) 151 184 matchidx = argmatch.end() 152 185 … … 154 187 pathFactoryMethod = getattr(pathfactory, command['name']) 155 188 result = pathFactoryMethod(command['relative'], *arguments) 156 yield result189 if result is not None: yield result 157 190 158 191 # see if we have a command transfer … … 162 195 command = self.pathCommandTable[commandidx] 163 196 197 result = pathfactory.EndPath(pathstr) 198 if result is not None: yield result 199 200 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 201 #~ Testing 202 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 203 204 class TestPathFactory(PathFactoryAbstract): 205 def BeginPath(self, pathstr): 206 return "BeginPath" 207 def EndPath(self, pathstr): 208 return "EndPath" 209 def MoveTo(self, relative, x, y): 210 return "MoveTo", (relative, x, y) 211 def ClosePath(self, relative): 212 return "ClosePath", (relative,) 213 def LineTo(self, relative, x, y): 214 return "LineTo", (relative, x, y) 215 def HLineTo(self, relative, x): 216 return "HLineTo", (relative, x) 217 def VLineTo(self, relative, y): 218 return "VLineTo", (relative, y) 219 def CubicBezierTo(self, relative, x1, y1, x2, y2, x, y): 220 return "CubicBezierTo", (relative, x1, y1, x2, y2, x, y) 221 def SmoothCurveTo(self, relative, x2, y2, x, y): 222 return "SmoothCurveTo", (relative, x2, y2, x, y) 223 def QuadraticBezierTo(self, relative, x1, y1, x, y): 224 return "QuadraticBezierTo", (relative, x1, y1, x, y) 225 def SmoothQuadraticBezierTo(self, relative, x, y): 226 return "SmoothQuadraticBezierTo", (relative, x, y) 227 def EllipticArcTo(self, relative, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y): 228 return "EllipticArcTo", (relative, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y) 229 230 if __name__=='__main__': 231 from pprint import pprint 232 builder = PathCommandBuilder(TestPathFactory()) 233 234 print 235 print "Example 1:" 236 pprint(builder.Build('M28.495428 -80.762271 L76.495428 -80.762271 ')) 237 238 print 239 print "Example 2:" 240 pprint(builder.Build('M28.495428 -80.762271 76.495428 -80.762271 ')) 241 242 print 243 print "Example 3:" 244 pprint(builder.Build('M-32.351307 -1329.4836 A0.8746 0.8746 0 1 0 -30.602107 -1329.4836')) 245 trunk/RBRapier/RBRapier/Formats/Attic/SVG.old/SVGSkin/path.py
r615 r617 26 26 import warnings 27 27 from SVGShapeSkinObject import SVGShapeSkinObject, SVGShapeRenderItem 28 from SVGPathBuilder import * 28 29 29 30 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 31 32 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 32 33 34 class PathFactory(PathFactoryAbstract): 35 def BeginPath(self, pathstr): 36 warnings.warn("TODO: implement path command 'BeginPath'") 37 def EndPath(self, pathstr): 38 warnings.warn("TODO: implement path command 'EndPath'") 39 def MoveTo(self, relative, x, y): 40 warnings.warn("TODO: implement path command 'MoveTo'") 41 def ClosePath(self, relative): 42 warnings.warn("TODO: implement path command 'ClosePath'") 43 def LineTo(self, relative, x, y): 44 warnings.warn("TODO: implement path command 'LineTo'") 45 def HLineTo(self, relative, x): 46 warnings.warn("TODO: implement path command 'HLineTo'") 47 def VLineTo(self, relative, y): 48 warnings.warn("TODO: implement path command 'VLineTo'") 49 def CubicBezierTo(self, relative, x1, y1, x2, y2, x, y): 50 warnings.warn("TODO: implement path command 'CubicBezierTo'") 51 def SmoothCurveTo(self, relative, x2, y2, x, y): 52 warnings.warn("TODO: implement path command 'SmoothCurveTo'") 53 def QuadraticBezierTo(self, relative, x1, y1, x, y): 54 warnings.warn("TODO: implement path command 'QuadraticBezierTo'") 55 def SmoothQuadraticBezierTo(self, relative, x, y): 56 warnings.warn("TODO: implement path command 'SmoothQuadraticBezierTo'") 57 def EllipticArcTo(self, relative, rx, ry, xrotation, largeArcFlag, sweepFlag, x, y): 58 warnings.warn("TODO: implement path command 'EllipticArcTo'") 59 60 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 61 33 62 class PathRenderItem(SVGShapeRenderItem): 34 """ 35 Path commands (from http://www.protocol7.com/svg-wiki/ow.asp?ThePath) 36 37 moveto 38 M absolute moveto (x y)+ 39 m relative moveto (x y)+ 40 closepath 41 Z absolute closepath (none) 42 z relative closepath (none) 43 lineto 44 L absolute lineto (x y)+ 45 l relative lineto (x y)+ 46 horizontal lineto 47 H absolute horizontal lineto x+ 48 h relative horizontal lineto x+ 49 vertical lineto 50 V absolute vertical lineto y+ 51 v relative vertical lineto y+ 52 curveto (cubic bezier) 53 C absolute curveto (x1 y1 x2 y2 x y)+ 54 c relative curveto (x1 y1 x2 y2 x y)+ 55 smooth curveto 56 S absolute smooth curveto (x2 y2 x y)+ 57 s relative smooth curveto (x2 y2 x y)+ 58 quadratic bezier curveto 59 Q absolute quadratic bezier curveto (x1 y1 x y)+ 60 q relative quadratic bezier curveto (x1 y1 x y)+ 61 smooth quadratic bezier curveto 62 T absolute smooth quadratic bezier curveto (x y)+ 63 t relative smooth quadratic bezier curveto (x y)+ 64 eliptical arc 65 A absolute elliptical arc (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ 66 67 """ 63 pathbuilder = PathCommandBuilder() 68 64 69 65 def DisplayOn(self, renderer, rendercontext): … … 77 73 78 74 def _asPath(self, path): 79 warnings.warn("TODO: implement path interpretation") 80 return path 75 return self.pathbuilder.Build(path, PathFactory()) 81 76 82 77 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
