SOLVED append contour to a glyph with Python



  • Hi, I have been trying to append segments to a new contour. That gives me an error. When I do the same but instead of creating a contour with python I use already existing one it works. What is the correct way to create a contour with segments and append it to the glyph? When I change contour = RContour() to contour = c1 # existing contour and leave it's native segment type, then it works. Thanks for help!

    def interpolateContours(c1, c2, f):
        contour = RContour()
        for seg1, seg2 in zip(c1, c2):
            points = map(lambda p: interPoint(*p, f), zip(seg1, seg2))
            segType = seg1.type if seg1.type != 'move' else 'line'
            contour.appendSegment(segType, list(points))
    
    IndexError: list index out of range
    

  • admin

    true, good luck!



  • @frederik sweet, thanks! It needs more when it needs to handle curves too I think, for now it is good enough.

    def interpolateContours(c1, c2, f):
        contour = RContour()    
        for seg1, seg2 in zip(c1, c2):
            assert seg1.type == seg2.type
            coos = map(lambda p: interPoint(*p, f), zip(seg1, seg2))
            for coo, point in zip(coos, seg1.points):
                contour.appendPoint(coo, point.type)
        return contour
    

  • admin

    oh see pull request here: https://github.com/robotools/fontParts/pull/480

    this will be fixed in the release!!

    but something else: why using appendSegment? and not using a pen like contour.appendPoint to draw into that contour, or even do the calculation with pens or use fontMath

    def interPoint(p1, p2, factor):
        return p1.x + (p2.x - p1.x) * factor, p1.y + (p2.y - p1.y) * factor
    
    def interpolateContours(c1, c2, f):
        contour = RContour()    
        for seg1, seg2 in zip(c1, c2):
            assert seg1.type == seg2.type
            points = map(lambda p: interPoint(*p, f), zip(seg1, seg2))
            for point in points:
                contour.appendPoint(point, seg1.type)
        return contour
    

Log in to reply