summaryrefslogtreecommitdiffstats
path: root/share/extensions/cubicsuperpath.py
blob: fdd1afc59c7080f627651362fa8bb6b863a74855 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
"""
cubicsuperpath.py

Copyright (C) 2005 Aaron Spike, aaron@ekips.org

This program is free software; you can redistribute it and/or modify
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

"""
import simplepath 

def CubicSuperPath(simplepath):
	csp = []
	subpath = -1
	subpathstart = []
	last = []
	lastctrl = []
	for s in simplepath:
		cmd, params = s		
		if cmd == 'M':
			if last:
				csp[subpath].append([lastctrl[:],last[:],last[:]])
			subpath += 1
			csp.append([])
			subpathstart =  params[:]
			last = params[:]
			lastctrl = params[:]
		elif cmd == 'L':
			csp[subpath].append([lastctrl[:],last[:],last[:]])
			last = params[:]
			lastctrl = params[:]
		elif cmd == 'C':
			csp[subpath].append([lastctrl[:],last[:],params[:2]])
			last = params[-2:]
			lastctrl = params[2:4]
		elif cmd == 'Q':
			#TODO: convert to cubic
			csp[subpath].append([lastctrl[:],last[:],last[:]])
			last = params[-2:]
			lastctrl = params[-2:]
		elif cmd == 'A':
			#TODO: convert to cubics
			csp[subpath].append([lastctrl[:],last[:],last[:]])
			last = params[-2:]
			lastctrl = params[-2:]
		elif cmd == 'Z':
			csp[subpath].append([lastctrl[:],last[:],last[:]])
			last = subpathstart[:]
			lastctrl = subpathstart[:]
	#append final superpoint
	csp[subpath].append([lastctrl[:],last[:],last[:]])
	return csp	

def unCubicSuperPath(csp):
	a = []
	for subpath in csp:
		if subpath:
			a.append(['M',subpath[0][1][:]])
			for i in range(1,len(subpath)):
				a.append(['C',subpath[i-1][2][:] + subpath[i][0][:] + subpath[i][1][:]])
	return a

def parsePath(d):
	return CubicSuperPath(simplepath.parsePath(d))

def formatPath(p):
	return simplepath.formatPath(unCubicSuperPath(p))