Example #1
0
    def __init__(self, elems, epsilon=None):
        """elems should contain metapost knots or links"""
        if epsilon is None:
            epsilon = _epsilon
        knots = []
        is_closed = True
        for i, elem in enumerate(elems):
            if isinstance(elem, _link):
                elem.set_knots(elems[i - 1], elems[(i + 1) % len(elems)])
            elif isinstance(elem, _knot):
                knots.append(elem)
                if elem.ltype == mp_endpoint or elem.rtype == mp_endpoint:
                    is_closed = False

        # link the knots among each other
        for i in range(len(knots)):
            knots[i - 1].next = knots[i]

        # determine the control points
        mp_make_choices(knots[0], epsilon)

        pathmodule.path.__init__(self)
        # build up the path
        do_moveto = True
        do_lineto = False
        do_curveto = False
        prev = None
        for i, elem in enumerate(elems):
            if isinstance(elem, _link):
                do_moveto = False
                if isinstance(elem, line):
                    do_lineto, do_curveto = True, False
                else:
                    do_lineto, do_curveto = False, True
            elif isinstance(elem, _knot):
                if do_moveto:
                    self.append(pathmodule.moveto_pt(elem.x_pt, elem.y_pt))
                if do_lineto:
                    self.append(pathmodule.lineto_pt(elem.x_pt, elem.y_pt))
                elif do_curveto:
                    self.append(
                        pathmodule.curveto_pt(prev.rx_pt, prev.ry_pt,
                                              elem.lx_pt, elem.ly_pt,
                                              elem.x_pt, elem.y_pt))
                do_moveto = True
                do_lineto = False
                do_curveto = False
                prev = elem

        # close the path if necessary
        if knots[0].ltype == mp_explicit:
            elem = knots[0]
            if do_lineto and is_closed:
                self.append(pathmodule.closepath())
            elif do_curveto:
                self.append(
                    pathmodule.curveto_pt(prev.rx_pt, prev.ry_pt, elem.lx_pt,
                                          elem.ly_pt, elem.x_pt, elem.y_pt))
                if is_closed:
                    self.append(pathmodule.closepath())
Example #2
0
 def updatepath(self, path, trafo, context):
     othersubrnumber = context.t1stack.pop()
     n = context.t1stack.pop()
     for i in range(n):
         context.psstack.append(context.t1stack.pop(0))
     if othersubrnumber == 0:
         flex_size, x, y = context.psstack[-3:]
         if context.flex:
             x1, y1, x2, y2, x3, y3 = context.psstack[2:8]
             x1, y1 = trafo.apply_pt(x1, y1)
             x2, y2 = trafo.apply_pt(x2, y2)
             x3, y3 = trafo.apply_pt(x3, y3)
             path.append(curveto_pt(x1, y1, x2, y2, x3, y3))
             x1, y1, x2, y2, x3, y3 = context.psstack[8:14]
             x1, y1 = trafo.apply_pt(x1, y1)
             x2, y2 = trafo.apply_pt(x2, y2)
             x3, y3 = trafo.apply_pt(x3, y3)
             path.append(curveto_pt(x1, y1, x2, y2, x3, y3))
         else:
             path.append(lineto_pt(*trafo.apply_pt(x, y)))
         context.psstack = [y, x]
     elif othersubrnumber == 1:
         pass
     elif othersubrnumber == 2:
         path.pathitems.pop()
         context.psstack.append(context.x)
         context.psstack.append(context.y)
Example #3
0
    def __init__(self, elems, epsilon=None):
        """elems should contain metapost knots or links"""
        if epsilon is None:
            epsilon = _epsilon
        knots = []
        is_closed = True
        for i, elem in enumerate(elems):
            if isinstance(elem, _link):
                elem.set_knots(elems[i-1], elems[(i+1)%len(elems)])
            elif isinstance(elem, _knot):
                knots.append(elem)
                if elem.ltype == mp_endpoint or elem.rtype == mp_endpoint:
                    is_closed = False

        # link the knots among each other
        for i in range(len(knots)):
            knots[i-1].next = knots[i]

        # determine the control points
        mp_make_choices(knots[0], epsilon)

        pathmodule.path.__init__(self)
        # build up the path
        do_moveto = True
        do_lineto = False
        do_curveto = False
        prev = None
        for i, elem in enumerate(elems):
            if isinstance(elem, _link):
                do_moveto = False
                if isinstance(elem, line):
                    do_lineto, do_curveto = True, False
                else:
                    do_lineto, do_curveto = False, True
            elif isinstance(elem, _knot):
                if do_moveto:
                    self.append(pathmodule.moveto_pt(elem.x_pt, elem.y_pt))
                if do_lineto:
                    self.append(pathmodule.lineto_pt(elem.x_pt, elem.y_pt))
                elif do_curveto:
                    self.append(pathmodule.curveto_pt(prev.rx_pt, prev.ry_pt, elem.lx_pt, elem.ly_pt, elem.x_pt, elem.y_pt))
                do_moveto = True
                do_lineto = False
                do_curveto = False
                prev = elem

        # close the path if necessary
        if knots[0].ltype == mp_explicit:
            elem = knots[0]
            if do_lineto and is_closed:
                self.append(pathmodule.closepath())
            elif do_curveto:
                self.append(pathmodule.curveto_pt(prev.rx_pt, prev.ry_pt, elem.lx_pt, elem.ly_pt, elem.x_pt, elem.y_pt))
                if is_closed:
                    self.append(pathmodule.closepath())
Example #4
0
 def updatepath(self, path, trafo, context):
     dy1 = context.t1stack.pop(0)
     dx2 = context.t1stack.pop(0)
     dy2 = context.t1stack.pop(0)
     dx3 = context.t1stack.pop(0)
     path.append(curveto_pt(*(trafo.apply_pt(context.x,             context.y + dy1) +
                              trafo.apply_pt(context.x + dx2,       context.y + dy1 + dy2) +
                              trafo.apply_pt(context.x + dx2 + dx3, context.y + dy1 + dy2))))
     context.x += dx2+dx3
     context.y += dy1+dy2
Example #5
0
 def updatepath(self, path, trafo, context):
     dx1 = context.t1stack.pop(0)
     dx2 = context.t1stack.pop(0)
     dy2 = context.t1stack.pop(0)
     dy3 = context.t1stack.pop(0)
     path.append(curveto_pt(*(trafo.apply_pt(context.x + dx1,       context.y) +
                              trafo.apply_pt(context.x + dx1 + dx2, context.y + dy2) +
                              trafo.apply_pt(context.x + dx1 + dx2, context.y + dy2 + dy3))))
     context.x += dx1+dx2
     context.y += dy2+dy3
Example #6
0
 def updatepath(self, path, trafo, context):
     dy1 = context.t1stack.pop(0)
     dx2 = context.t1stack.pop(0)
     dy2 = context.t1stack.pop(0)
     dx3 = context.t1stack.pop(0)
     path.append(curveto_pt(*(trafo.apply_pt(context.x,             context.y + dy1) +
                              trafo.apply_pt(context.x + dx2,       context.y + dy1 + dy2) +
                              trafo.apply_pt(context.x + dx2 + dx3, context.y + dy1 + dy2))))
     context.x += dx2+dx3
     context.y += dy1+dy2
Example #7
0
 def updatepath(self, path, trafo, context):
     dx1 = context.t1stack.pop(0)
     dx2 = context.t1stack.pop(0)
     dy2 = context.t1stack.pop(0)
     dy3 = context.t1stack.pop(0)
     path.append(curveto_pt(*(trafo.apply_pt(context.x + dx1,       context.y) +
                              trafo.apply_pt(context.x + dx1 + dx2, context.y + dy2) +
                              trafo.apply_pt(context.x + dx1 + dx2, context.y + dy2 + dy3))))
     context.x += dx1+dx2
     context.y += dy2+dy3