source: trunk/robofab/Lib/robofab/pens/digestPen.py @ 26

Revision 26, 1.6 KB checked in by erik, 5 years ago (diff)

Change to the way the transformation tuple of a component is added to the digest. Values which can be ints (int(a) == a) are added as int, rather than float. This solves a problem comparing digests in UFO and VFB.

RevLine 
[1]1"""A couple of point pens which return the glyph as a list of basic values."""
2
3
4from robofab.pens.pointPen import AbstractPointPen
5
6
7class DigestPointPen(AbstractPointPen):
8
9        """Calculate a digest of all points
10                AND coordinates
11                AND components
12        in a glyph.
13        """
14
15        def __init__(self, ignoreSmoothAndName=False):
16                self._data = []
17                self.ignoreSmoothAndName = ignoreSmoothAndName
18
19        def beginPath(self):
20                self._data.append('beginPath')
21
22        def endPath(self):
23                self._data.append('endPath')
24
25        def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
26                if self.ignoreSmoothAndName:
27                        self._data.append((pt, segmentType))
28                else:
29                        self._data.append((pt, segmentType, smooth, name))
30
31        def addComponent(self, baseGlyphName, transformation):
[26]32                t = []
33                for v in transformation:
34                        if int(v) == v:
35                                t.append(int(v))
36                        else:
37                                t.append(v)
38                self._data.append((baseGlyphName, tuple(t)))
[1]39
40        def getDigest(self):
41                return tuple(self._data)
42       
43        def getDigestPointsOnly(self):
44                """Return a tuple with all coordinates of all points,
45                        but without smooth info or drawing instructions.
46                        For instance if you want to compare 2 glyphs in shape,
47                        but not interpolatability.
48                        """
49                points = []
50                for item in self._data:
51                        points.append(item[0])
52                points.sort()
53                return tuple(points)
54
55
56class DigestPointStructurePen(DigestPointPen):
57
58        """Calculate a digest of the structure of the glyph
59                NOT coordinates
60                NOT values.
61        """
62
63        def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
64                self._data.append(segmentType)
65
66        def addComponent(self, baseGlyphName, transformation):
67                self._data.append(baseGlyphName)
68
Note: See TracBrowser for help on using the repository browser.