Changeset 94

Show
Ignore:
Timestamp:
09/20/08 04:32:33 (3 years ago)
Author:
erik
Message:

MarginPen? now has a isHorizontal flag when initialising. Default set to True, the calculations will be horizontal. When False, the measurements will be vertical.

Scripts using MarginPen? should continue to work without changes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Lib/robofab/pens/marginPen.py

    r93 r94  
    88 
    99        """ 
    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. 
    1113                 
    1214                When a glyphset or font is given, MarginPen will also calculate for glyphs with components. 
     
    1517                pen.getContourMargins() returns the minimum and maximum intersections for each contour. 
    1618                 
     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                 
    1724        """ 
    1825 
    19         def __init__(self, glyphSet, height): 
     26        def __init__(self, glyphSet, value, isHorizontal=True): 
    2027                BasePen.__init__(self, glyphSet) 
    21                 self.height = height 
     28                self.value = value 
    2229                self.hits = {} 
    2330                self.filterDoubles = True 
    2431                self.contourIndex = None 
    2532                self.startPt = None 
     33                self.isHorizontal = isHorizontal 
    2634                 
    2735        def _moveTo(self, pt): 
     
    3745                        if pt == self.currentPt: 
    3846                                return 
    39                 hits = splitLine(self.currentPt, pt, self.height, True
     47                hits = splitLine(self.currentPt, pt, self.value, self.isHorizontal
    4048                if len(hits)>1: 
    4149                        # result will be 2 tuples of 2 coordinates 
     
    4654                        if not self.contourIndex in self.hits: 
    4755                                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: 
    5061                        # it could happen 
    5162                        if not self.contourIndex in self.hits: 
    5263                                self.hits[self.contourIndex] =  [] 
    5364                        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]) 
    5470                self.currentPt = pt 
    5571 
    5672        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
    5874                for i in range(len(hits)-1): 
    5975                        # a number of intersections is possible. Just take the  
     
    6177                        if not self.contourIndex in self.hits: 
    6278                                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: 
    6584                        # it could happen 
    6685                        if not self.contourIndex in self.hits: 
    6786                                self.hits[self.contourIndex] = [] 
    6887                        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]) 
    6993                self.currentPt = pt3 
    7094                 
     
    78102 
    79103        def addComponent(self, baseGlyph, transformation): 
    80                 from fontTools.pens.transformPen import TransformPen 
    81104                if self.glyphSet is None: 
    82105                        return 
     
    84107                        glyph = self.glyphSet[baseGlyph] 
    85108                if glyph is not None: 
    86                         tPen = TransformPen(self, transformation) 
    87                         glyph.draw(tPen) 
     109                        glyph.draw(self) 
    88110                 
    89111        def getMargins(self): 
     
    123145 
    124146        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