Changeset 94
- Timestamp:
- 09/20/08 04:32:33 (3 years ago)
- Files:
-
- trunk/Lib/robofab/pens/marginPen.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/Lib/robofab/pens/marginPen.py
r93 r94 8 8 9 9 """ 10 Pen to calculate the horizontal margins at a given height. 10 Pen to calculate the margins at a given value. 11 When isHorizontal is True, the margins at <value> are horizontal. 12 When isHorizontal is False, the margins at <value> are vertical. 11 13 12 14 When a glyphset or font is given, MarginPen will also calculate for glyphs with components. … … 15 17 pen.getContourMargins() returns the minimum and maximum intersections for each contour. 16 18 19 20 Possible optimisation: 21 Initialise the pen object with a list of points we want to measure, 22 then draw the glyph once, but do the splitLine() math for all measure points. 23 17 24 """ 18 25 19 def __init__(self, glyphSet, height):26 def __init__(self, glyphSet, value, isHorizontal=True): 20 27 BasePen.__init__(self, glyphSet) 21 self. height = height28 self.value = value 22 29 self.hits = {} 23 30 self.filterDoubles = True 24 31 self.contourIndex = None 25 32 self.startPt = None 33 self.isHorizontal = isHorizontal 26 34 27 35 def _moveTo(self, pt): … … 37 45 if pt == self.currentPt: 38 46 return 39 hits = splitLine(self.currentPt, pt, self. height, True)47 hits = splitLine(self.currentPt, pt, self.value, self.isHorizontal) 40 48 if len(hits)>1: 41 49 # result will be 2 tuples of 2 coordinates … … 46 54 if not self.contourIndex in self.hits: 47 55 self.hits[self.contourIndex] = [] 48 self.hits[self.contourIndex].append(round(hits[0][-1][0], 4)) 49 if pt[1] == self.height: 56 if self.isHorizontal: 57 self.hits[self.contourIndex].append(round(hits[0][-1][0], 4)) 58 else: 59 self.hits[self.contourIndex].append(round(hits[0][-1][1], 4)) 60 if self.isHorizontal and pt[1] == self.value: 50 61 # it could happen 51 62 if not self.contourIndex in self.hits: 52 63 self.hits[self.contourIndex] = [] 53 64 self.hits[self.contourIndex].append(pt[0]) 65 elif (not self.isHorizontal) and (pt[0] == self.value): 66 # it could happen 67 if not self.contourIndex in self.hits: 68 self.hits[self.contourIndex] = [] 69 self.hits[self.contourIndex].append(pt[1]) 54 70 self.currentPt = pt 55 71 56 72 def _curveToOne(self, pt1, pt2, pt3): 57 hits = splitCubic(self.currentPt, pt1, pt2, pt3, self. height, True)73 hits = splitCubic(self.currentPt, pt1, pt2, pt3, self.value, self.isHorizontal) 58 74 for i in range(len(hits)-1): 59 75 # a number of intersections is possible. Just take the … … 61 77 if not self.contourIndex in self.hits: 62 78 self.hits[self.contourIndex] = [] 63 self.hits[self.contourIndex].append(round(hits[i][-1][0], 4)) 64 if pt3[1] == self.height: 79 if self.isHorizontal: 80 self.hits[self.contourIndex].append(round(hits[i][-1][0], 4)) 81 else: 82 self.hits[self.contourIndex].append(round(hits[i][-1][1], 4)) 83 if self.isHorizontal and pt3[1] == self.value: 65 84 # it could happen 66 85 if not self.contourIndex in self.hits: 67 86 self.hits[self.contourIndex] = [] 68 87 self.hits[self.contourIndex].append(pt3[0]) 88 if (not self.isHorizontal) and (pt3[0] == self.value): 89 # it could happen 90 if not self.contourIndex in self.hits: 91 self.hits[self.contourIndex] = [] 92 self.hits[self.contourIndex].append(pt3[1]) 69 93 self.currentPt = pt3 70 94 … … 78 102 79 103 def addComponent(self, baseGlyph, transformation): 80 from fontTools.pens.transformPen import TransformPen81 104 if self.glyphSet is None: 82 105 return … … 84 107 glyph = self.glyphSet[baseGlyph] 85 108 if glyph is not None: 86 tPen = TransformPen(self, transformation) 87 glyph.draw(tPen) 109 glyph.draw(self) 88 110 89 111 def getMargins(self): … … 123 145 124 146 pt = (74, 216) 125 if f is not None and g is not None: 126 pen = MarginPen(f, pt[1]) 127 g.draw(pen) 128 print 'glyph margins', pen.getMargins() 129 print pen.getContourMargins() 130 else: 131 print "no font or glyph" 147 148 pen = MarginPen(f, pt[1], isHorizontal=False) 149 g.draw(pen) 150 print 'glyph Y margins', pen.getMargins() 151 print pen.getContourMargins() 152
