comparison tools/svg2code.py @ 280:c8b6ca46950b mbtext

Add merged result
author wycc
date Sat, 31 Jan 2009 12:29:50 +0800
parents 86a5ae82ccf2 c96f38ad4bb6
children 248a40d51473
comparison
equal deleted inserted replaced
279:86a5ae82ccf2 280:c8b6ca46950b
211 translate_transform(shape_coord_id, transform, codefo, 'SHAPE_') 211 translate_transform(shape_coord_id, transform, codefo, 'SHAPE_')
212 coord_id = shape_coord_id 212 coord_id = shape_coord_id
213 pass 213 pass
214 return coord_id 214 return coord_id
215 215
216 ## \brief Calculate geometry of ellipse where the arc is on.
217 #
218 # This function calculate the ellipse with information from SVG path data.
219 #
220 # \see calc_center_and_x_aix()
221 def _calc_ellipse_of_arc(x0, y0, rx, ry, x_rotate, large, sweep, x, y):
222 import math
223
224 _sin = math.sin(x_rotate)
225 _cos = math.cos(x_rotate)
226
227 nrx = x * _cos + y * _sin
228 nry = x * -_sin + y * _cos
229 nrx0 = x0 * _cos + y0 * _sin
230 nry0 = x0 * -_sin + y0 * _cos
231
232 udx = (nrx - nrx0) / 2 / rx # ux - umx
233 udy = (nry - nry0) / 2 / ry # uy - umy
234 umx = (nrx + nrx0) / 2 / rx
235 umy = (nry + nry0) / 2 / ry
236
237 udx2 = udx * udx
238 udy2 = udy * udy
239 udl2 = udx2 + udy2
240
241 if udy != 0:
242 # center is at left-side of arc
243 udcx = -math.sqrt((1 - udl2) * udl2) / (udy + udx2 / udy)
244 udcy = -udcx * udx / udy
245 else:
246 # center is at down-side of arc
247 udcx = 0
248 udcy = math.sqrt((1 - udl2) * udl2) / udx
249 pass
250
251 reflect = 0
252 if large:
253 reflect ^= 1
254 pass
255 if sweep != 1:
256 reflect ^= 1
257 pass
258 if reflect:
259 udcx = -udcx
260 udcy = -udcy
261 pass
262
263 nrcx = rx * (udcx + umx)
264 nrcy = ry * (udcy + umy)
265
266 cx = nrcx * _cos - nrcy * _sin
267 cy = nrcx * _sin + nrcy * _cos
268
269 xx = rx * _cos + cx
270 xy = rx * _sin + cy
271 return cx, cy, xx, xy
272
216 # M x y : Move to (x,y) 273 # M x y : Move to (x,y)
217 # Z : close path 274 # Z : close path
218 # L x y : lineto (x,y) 275 # L x y : lineto (x,y)
219 # H x : horizontal line to x 276 # H x : horizontal line to x
220 # V y : Vertical line to y 277 # V y : Vertical line to y
234 'H': 1, 'h':1, 291 'H': 1, 'h':1,
235 'V': 1, 'v':1, 292 'V': 1, 'v':1,
236 'C': 6, 'c':6, 293 'C': 6, 'c':6,
237 'S': 4, 's':4, 294 'S': 4, 's':4,
238 'Q': 4, 'q':4, 295 'Q': 4, 'q':4,
239 'T': 2, 't':2} 296 'T': 2, 't':2,
240 297 'A': 7, 'a':7}
241 298
242 def translate_path_data(data,codefo): 299 def translate_path_data(data,codefo):
243 temp = data.split() 300 temp = data.split()
244 fields=[] 301 fields=[]
245 for f in temp: 302 for f in temp:
246 for s in f.split(','): 303 for s in f.split(','):
247 if s != '': 304 if s != '':
248 fields.append(s) 305 fields.append(s)
249 cmd = '' 306 cmd = ''
307 cmd_args=0
250 commands='' 308 commands=''
251 args=[] 309 args=[]
252 fix_args=[] 310 fix_args=[]
253 for f in fields: 311 for f in fields:
254 if cmd == 'A' or cmd == 'a': 312 if f in command_length:
255 try: 313 if cmd_args != 0 and (narg % cmd_args) != 0:
256 d = int(f) 314 raise ValueError, 'invalid path data %s' % (repr(fields))
257 fix_args.append(d) 315 cmd = f
258 if (narg % 7) == 0: 316 cmd_args = command_length[f]
259 commands = commands + cmd 317 narg = 0
260 narg = narg + 1 318 continue
261 except: 319
262 pass 320 if (narg % cmd_args) == 0:
263 else: 321 commands = commands + cmd
264 try: 322 pass
265 d = float(f) 323 arg = float(f)
266 args.append(d) 324 args.append(arg)
267 if (narg % command_length[cmd]) == 0: 325 narg = narg + 1
268 commands = commands + cmd 326
269 narg = narg + 1 327 if (narg % cmd_args) == 0 and (cmd in 'Aa'):
270 continue 328 x0, y0, rx, ry, x_rotate, large, sweep, x, y = \
271 except: 329 tuple(args[-9:])
272 pass 330 x_rotate = int(x_rotate)
273 cmd = f 331 large = int(large)
274 narg=0 332 sweep = int(sweep)
275 pass 333 cx, cy, xx, xy = _calc_ellipse_of_arc(x0, y0, rx, ry,
276 return [commands,args,fix_args] 334 x_rotate, large,
335 sweep, x, y)
336 args[-7:] = [cx, cy, xx, xy, x, y]
337 fix_args.append(sweep)
338 pass
339 pass
340 return commands, args, fix_args
277 341
278 _id_sn = 0 342 _id_sn = 0
279 343
280 def _get_id(obj): 344 def _get_id(obj):
281 global _id_sn 345 global _id_sn