summaryrefslogtreecommitdiffstats
path: root/share/extensions/PathData.py
blob: 762f97173ee33dd017d254a01289d6702bb2d6c9 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'''
Copyright (C) 2011 Felipe Correa da Silva Sanches <juca@members.fsf.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
'''

class PathData():
	def __init__ (self, d):
		self.data = self.parse(d)

	def transform_coordinate_values(self, callback, units_per_em, baseline):

		for d in range(len(self.data)):
			cmd = self.data[d][0]
			relative = (cmd.lower()==cmd)

			if cmd.lower() in "mtlsqc":
				for params in range(len(self.data[d][1])):
					for values in range(len(self.data[d][1][params])):

						if d==0 and params==0 and values==0:
							self.data[d][1][params][values] = callback(self.data[d][1][params][values], units_per_em, baseline, False)
						else:
							self.data[d][1][params][values] = callback(self.data[d][1][params][values], units_per_em, baseline, relative)

		return self.encode()
		
	def get_coord_pairs(self, num_pairs, tokens, i, multiple=True):
		pairs = []
		for _ in range(num_pairs):
			x,y = tokens[i].split(",")
			pairs.append((float(x), float(y)))
			i+=1
		coords = [pairs]

		while multiple:
			try:
				pairs = []
				for _ in range(num_pairs):
					x,y = tokens[i].split(",")
					pairs.append((float(x), float(y)))
					i+=1
				coords.append(pairs)
			except:
				return coords, i
		return coords, i

	def get_coords(self, num_values, tokens, i, multiple=True):
		values = []
		for _ in range(num_values):
			values.append(float(tokens[i]))
			i+=1
		coords = [values]

		while multiple:
			try:
				values = []
				for _ in range(num_values):
					values.append(float(tokens[i]))
					i+=1
				coords.append(values)
			except:
				return coords, i
		return coords, i

	def get_arc_coords(tokens, i):
		return [] #TODO

	def get_tokens(self, d):
		tokens = []
		for t in d.split(" "):
			if len(t):
				if len(t)>1 and t[0].lower() in "hvmtlsqcaz":
					tokens.append(t[0])
					tokens.append(t[1:])
				else:
					tokens.append(t)
		return tokens

	def parse(self, d):
		data = []
		tokens = self.get_tokens(d)
		i=0
		while i < len(tokens):
			command = tokens[i]
			i+=1
			if command in ["z","Z"]:
				data.append((command, None))
				i+=1
			elif command in ["h","H", "v", "V"]:
				coords, i = self.get_coords(1, tokens, i)
				data.append((command, coords))
			elif command in ["a","A"]:
				coords, i = self.get_arc_coords(tokens, i)
				data.append((command, coords))
			elif command in ["m","M", "t", "T", "l", "L"]:
				coords, i = self.get_coord_pairs(1, tokens, i)
				data.append((command, coords))
			elif command in ["s","S", "q", "Q"]:
				coords, i = self.get_coord_pairs(2, tokens, i)
				data.append((command, coords))
			elif command in ["c","C"]:
				coords, i = self.get_coord_pairs(3, tokens, i)
				data.append((command, coords))
		return data

	def encode(self, data=None):
		if data is None:
			data = self.data

		d = ""
		for cmd, coords in data:
			d += cmd + " "
			if cmd.lower() in "mtlsqc":
				for c in coords:
					for x,y in c:
						d += str(x) + "," + str(y) + " "
			elif cmd.lower() == "z":
				d += cmd
			elif cmd.lower() in "hv":
				for values in coords:
					for v in values:
						d += str(v) + " "
			elif cmd.lower() == "a":
				pass #TODO
		return d