Show
Ignore:
Timestamp:
02/11/07 20:54:49 (2 years ago)
Author:
fdb
Message:

1.8.3 updates, bezier changes, not complete yet.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • nodebox/trunk/src/DrawingPrimitives.py

    r50 r76  
    101101        self.strokecolor = None 
    102102        self.strokewidth = 1.0 
     103        self._segment_cache = None 
    103104 
    104105    def inheritFromContext(self): 
     
    139140 
    140141    def moveto(self, x, y): 
     142        self._segment_cache = None 
    141143        self.path.moveToPoint_( (x, y) ) 
    142144 
    143145    def lineto(self, x, y): 
     146        self._segment_cache = None 
    144147        self.path.lineToPoint_( (x, y) ) 
    145148 
    146149    def curveto(self, x1, y1, x2, y2, x3, y3): 
     150        self._segment_cache = None 
    147151        self.path.curveToPoint_controlPoint1_controlPoint2_( (x3, y3), (x1, y1), (x2, y2) ) 
    148152 
    149153    def closepath(self): 
     154        self._segment_cache = None 
    150155        self.path.closePath() 
    151156 
     
    179184 
    180185    def extend(self, pathElements): 
     186        self._segment_cache = None 
    181187        for el in pathElements: 
    182188            self.append(el) 
    183189 
    184190    def append(self, el): 
     191        self._segment_cache = None 
    185192        if el.cmd == MOVETO: 
    186193            self.moveto(el.x, el.y) 
     
    191198        elif el.cmd == CLOSE: 
    192199            self.closepath() 
     200             
     201    def _get_contours(self): 
     202        import bezier 
     203        return bezier.contours(self) 
     204    contours = property(_get_contours) 
    193205 
    194206    ### Drawing methods ### 
     
    223235            self.path.stroke() 
    224236        _restore() 
     237         
     238    ### Mathematics ### 
     239     
     240    def segment_lengths(self, percentual=False, n=10): 
     241        import bezier 
     242        if percentual: # Use the opportunity to store the segment cache. 
     243            if self._segment_cache is None: 
     244                self._segment_cache = bezier.segment_lengths(self, percentual=True, n=n) 
     245            return self._segment_cache 
     246        else: 
     247            return bezier.segment_lengths(self, percentual=True, n=n) 
     248 
     249    def _get_length(self, segmented=False, n=10): 
     250        import bezier 
     251        return bezier.length(self, segmented=segmented, n=n) 
     252    length = property(_get_length) 
     253         
     254    def point(self, t): 
     255        import bezier 
     256        return bezier.point(self, t) 
     257         
     258    def points(self, amount=100): 
     259        import bezier 
     260        if len(self) == 0: 
     261            raise NodeBoxError, "The given path is empty" 
     262 
     263        delta = 1.0/amount 
     264        for i in xrange(amount): 
     265            yield self.point(delta*i) 
    225266 
    226267class PathElement(object): 
     
    246287        elif cmd == CLOSE: 
    247288            assert pts is None or len(pts) == 0 
     289 
     290    def __repr__(self): 
     291        if self.cmd == MOVETO: 
     292            return "PathElement(MOVETO, ((%s, %s),))" % (self.x, self.y) 
     293        elif self.cmd == LINETO: 
     294            return "PathElement(LINETO, ((%s, %s),))" % (self.x, self.y) 
     295        elif self.cmd == CURVETO: 
     296            return "PathElement(LINETO, ((%s, %s), (%s, %s), (%s, %s))" % \ 
     297                (self.ctrl1.x, self.ctrl1.y, self.ctrl2.x, self.ctrl2.y, self.x, self.y) 
     298        elif self.cmd == CLOSE: 
     299            return "PathElement(CLOSE)" 
    248300 
    249301class Rect(BezierPath): 
     
    897949        self.height = height 
    898950        self.speed = None 
    899         print "canvas init" 
    900951        self.mousedown = False 
    901952 
     
    12121263        self._autoclosepath = close 
    12131264 
     1265    def findpath(self, points, curvature=1.0): 
     1266        import bezier 
     1267        path = bezier.findpath(points, curvature=curvature) 
     1268        path._ctx = self 
     1269        path.inheritFromContext() 
     1270        return path 
     1271     
    12141272    ### Clipping Commands ### 
    12151273    def beginclip(self, path): 
     
    14231481    k = KantGenerator(sourceFile) 
    14241482    return k.output() 
     1483 
     1484def _test(): 
     1485    import doctest, DrawingPrimitives 
     1486    return doctest.testmod(DrawingPrimitives) 
     1487 
     1488if __name__=='__main__': 
     1489    #import profile 
     1490    #profile.run(" 
     1491     
     1492    _test()