summaryrefslogtreecommitdiff
path: root/misc/pylib/robofab/pens/digestPen.py
blob: 930daf468774a864e278a48566ca9982520a0beb (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
"""A couple of point pens which return the glyph as a list of basic values."""


from robofab.pens.pointPen import AbstractPointPen


class DigestPointPen(AbstractPointPen):

	"""Calculate a digest of all points
		AND coordinates
		AND components
	in a glyph.
	"""

	def __init__(self, ignoreSmoothAndName=False):
		self._data = []
		self.ignoreSmoothAndName = ignoreSmoothAndName

	def beginPath(self):
		self._data.append('beginPath')

	def endPath(self):
		self._data.append('endPath')

	def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
		if self.ignoreSmoothAndName:
			self._data.append((pt, segmentType))
		else:
			self._data.append((pt, segmentType, smooth, name))

	def addComponent(self, baseGlyphName, transformation):
		t = []
		for v in transformation:
			if int(v) == v:
				t.append(int(v))
			else:
				t.append(v)
		self._data.append((baseGlyphName, tuple(t)))

	def getDigest(self):
		return tuple(self._data)
	
	def getDigestPointsOnly(self, needSort=True):
		""" Return a tuple with all coordinates of all points, 
			but without smooth info or drawing instructions.
			For instance if you want to compare 2 glyphs in shape,
			but not interpolatability.
			"""
		points = []
		from types import TupleType
		for item in self._data:
			if type(item) == TupleType:
				points.append(item[0])
		if needSort:
			points.sort()
		return tuple(points)


class DigestPointStructurePen(DigestPointPen):

	"""Calculate a digest of the structure of the glyph
		NOT coordinates
		NOT values.
	"""

	def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
		self._data.append(segmentType)

	def addComponent(self, baseGlyphName, transformation):
		self._data.append(baseGlyphName)

if __name__ == "__main__":
	"""
	
	beginPath
	((112, 651), 'line', False, None)
	((112, 55), 'line', False, None)
	((218, 55), 'line', False, None)
	((218, 651), 'line', False, None)
	endPath
	
	"""
	# a test
	
	from robofab.objects.objectsRF import RGlyph
	
	g = RGlyph()
	p = g.getPen()
	p.moveTo((112, 651))
	p.lineTo((112, 55))
	p.lineTo((218, 55))
	p.lineTo((218, 651))
	p.closePath()
	
	print g, len(g)
	
	digestPen = DigestPointPen()
	g.drawPoints(digestPen)

	print
	print "getDigest", digestPen.getDigest()
	
	print
	print "getDigestPointsOnly", digestPen.getDigestPointsOnly()