Changeset 88

Show
Ignore:
Timestamp:
05/30/08 04:47:54 (4 years ago)
Author:
erik
Message:

Handle components. Handle self overlapping bezier segments with any number of intersections.

Files:

Legend:

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

    r87 r88  
    1919        def __init__(self, glyphSet, height): 
    2020                BasePen.__init__(self, glyphSet) 
    21                  
    2221                self.height = height 
    2322                self.hits = {} 
    2423                self.filterDoubles = True 
    2524                self.contourIndex = None 
     25                self.startPt = None 
    2626                 
    2727        def _moveTo(self, pt): 
    2828                self.currentPt = pt 
     29                self.startPt = pt 
    2930                if self.contourIndex is None: 
    3031                        self.contourIndex = 0 
     
    5051                        if not self.contourIndex in self.hits: 
    5152                                self.hits[self.contourIndex] =  [] 
    52                                  
    5353                        self.hits[self.contourIndex].append(pt[0]) 
    5454                self.currentPt = pt 
     
    5656        def _curveToOne(self, pt1, pt2, pt3): 
    5757                hits = splitCubic(self.currentPt, pt1, pt2, pt3, self.height, True) 
    58                 if len(hits)==2: 
     58                for i in range(len(hits)-1): 
     59                        # a number of intersections is possible. Just take the  
     60                        # last point of each segment. 
    5961                        if not self.contourIndex in self.hits: 
    6062                                self.hits[self.contourIndex] = [] 
    61                         self.hits[self.contourIndex].append(round(hits[0][-1][0], 4)) 
    62                 elif pt3[1] == self.height: 
     63                        self.hits[self.contourIndex].append(round(hits[i][-1][0], 4)) 
     64                if pt3[1] == self.height: 
    6365                        # it could happen 
    6466                        if not self.contourIndex in self.hits: 
     
    6870                 
    6971        def _closePath(self): 
    70                 self.currentPt = None 
     72                if self.currentPt != self.startPt: 
     73                        self._lineTo(self.startPt) 
     74                self.currentPt = self.startPt = None 
    7175         
    7276        def _endPath(self): 
    7377                self.currentPt = None 
     78 
     79        def addComponent(self, baseGlyph, transformation): 
     80                if self.glyphSet is None: 
     81                        return 
     82                if baseGlyph in self.glyphSet: 
     83                        glyph = self.glyphSet[baseGlyph] 
     84                if glyph is not None: 
     85                        glyph.draw(self) 
    7486                 
    7587        def getMargins(self): 
     
    7890                for index, pts in self.hits.items(): 
    7991                        allHits.extend(pts) 
    80                 unique = list(Set(allHits)) 
    81                 unique.sort() 
    82                 if unique: 
    83                         return min(unique), max(unique) 
     92                if allHits: 
     93                        return min(allHits), max(allHits) 
    8494                return None 
    8595                 
     
    93103                return allHits 
    94104         
    95         def addComponent(self, baseGlyph, transformation): 
    96                 if self.glyphSet is None: 
    97                         return 
    98                 if baseGlyph in self.glyphSet: 
    99                         glyph = self.glyphSet[baseGlyph] 
    100                 if glyph is not None: 
    101                         glyph.draw(self) 
    102105                 
    103106                 
     
    108111        g = CurrentGlyph() 
    109112 
    110         pt = (100, 249
     113        pt = (74, 216
    111114        pen = MarginPen(f, pt[1]) 
    112115        g.draw(pen)      
    113116        print 'glyph margins', pen.getMargins() 
    114  
    115117        print pen.getContourMargins()