def computeTransformMatrix(transformList):
    """ compute the transform matrix that is the concatenation of all transforms in the 
        specifed transformList
    """
    transformMatrix = shapes.nullTransform()
    for transform in transformList:
        if not 'transform' in transform:
            raise InvalidTransform()
        if transform['transform'] is 'translate':
            transformMatrix = shapes.mmult(
                transformMatrix,
                shapes.translate(transform["tx"], transform["ty"]))
        elif transform['transform'] is 'rotate':
            transformMatrix = shapes.mmult(
                transformMatrix,
                shapes.translate(transform['cx'], transform['cy']))
            transformMatrix = shapes.mmult(transformMatrix,
                                           shapes.rotate(transform["theta"]))
            transformMatrix = shapes.mmult(
                transformMatrix,
                shapes.translate(-1.0 * transform['cx'],
                                 -1.0 * transform['cy']))
        elif transform['transform'] is 'scale':
            transformMatrix = shapes.mmult(
                transformMatrix, shapes.scale(transform['sx'],
                                              transform['sy']))
        else:
            raise InvalidTransform(transform['transform'])
    return transformMatrix
def bezier_arc_from_end_points(x1, y1, rx, ry, phi, fA, fS, x2, y2):
    if (x1 == x2 and y1 == y2):
        # From https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes:
        # If the endpoints (x1, y1) and (x2, y2) are identical, then this is
        # equivalent to omitting the elliptical arc segment entirely.
        return []
    if phi:
        # Our box bezier arcs can't handle rotations directly
        # move to a well known point, eliminate phi and transform the other point
        mx = mmult(rotate(-phi), translate(-x1, -y1))
        tx2, ty2 = transformPoint(mx, (x2, y2))
        # Convert to box form in unrotated coords
        cx, cy, rx, ry, start_ang, extent = end_point_to_center_parameters(
            0, 0, tx2, ty2, fA, fS, rx, ry)
        bp = bezier_arc_from_centre(cx, cy, rx, ry, start_ang, extent)
        # Re-rotate by the desired angle and add back the translation
        mx = mmult(translate(x1, y1), rotate(phi))
        res = []
        for x1, y1, x2, y2, x3, y3, x4, y4 in bp:
            res.append(
                transformPoint(mx, (x1, y1)) + transformPoint(mx, (x2, y2)) +
                transformPoint(mx, (x3, y3)) + transformPoint(mx, (x4, y4)))
        return res
    else:
        cx, cy, rx, ry, start_ang, extent = end_point_to_center_parameters(
            x1, y1, x2, y2, fA, fS, rx, ry)
        return bezier_arc_from_centre(cx, cy, rx, ry, start_ang, extent)
Beispiel #3
0
def bezier_arc_from_end_points(x1, y1, rx, ry, phi, fA, fS, x2, y2):
    if phi:
        # Our box bezier arcs can't handle rotations directly
        # move to a well known point, eliminate phi and transform the other point
        mx = mmult(rotate(-phi), translate(-x1, -y1))
        tx2, ty2 = transformPoint(mx, (x2, y2))
        # Convert to box form in unrotated coords
        cx, cy, rx, ry, start_ang, extent = end_point_to_center_parameters(
            0, 0, tx2, ty2, fA, fS, rx, ry
        )
        bp = bezier_arc_from_centre(cx, cy, rx, ry, start_ang, extent)
        # Re-rotate by the desired angle and add back the translation
        mx = mmult(translate(x1, y1), rotate(phi))
        res = []
        for x1, y1, x2, y2, x3, y3, x4, y4 in bp:
            res.append(
                transformPoint(mx, (x1, y1)) + transformPoint(mx, (x2, y2)) +
                transformPoint(mx, (x3, y3)) + transformPoint(mx, (x4, y4))
            )
        return res
    else:
        cx, cy, rx, ry, start_ang, extent = end_point_to_center_parameters(
            x1, y1, x2, y2, fA, fS, rx, ry
        )
        return bezier_arc_from_centre(cx, cy, rx, ry, start_ang, extent)
Beispiel #4
0
def getCaptcha(n=5,fontName='Courier',fontSize=14,text=None,fillColor=None):
    '''return n random chars in a string and in a byte string structured
    as a GIF image'''
    from reportlab.graphics.shapes import Drawing, Group, String, \
        rotate, skewX, skewY, mmult, translate, Rect
    if not text:
        from random import randint, uniform
        text=''.join([_allowed[randint(0,_mx)] for i in range(n)])
    else:
        uniform = lambda l,h: 0.5*(l+h)
        n = len(text)
    baseline = 0
    x = 0
    G0 = Group()
    for c in text:
        x += 1
        A = translate(x,uniform(baseline-5,baseline+5))
        A = mmult(A,rotate(uniform(-15,15)))
        A = mmult(A,skewX(uniform(-8,8)))
        A = mmult(A,skewY(uniform(-8,8)))
        G = Group(transform=A)
        G.add(String(0,0,c,fontname=fontName,fontSize=fontSize))
        G0.add(G)
        x0,y0,x1,y1 = G0.getBounds()
        x = 1+x1
    G0.transform=translate(2-x0,2-y0)
    W = 4+x1-x0
    H = 4+y1-y0
    D = Drawing(W,H)
    if fillColor:
        from reportlab.lib.colors import toColor
        D.add(Rect(0,0,W,H, fillColor = toColor(fillColor),strokeColor=None))
    D.add(G0)
    return text, D.asString('gif')
Beispiel #5
0
def bezier_arc_from_end_points(x1, y1, rx, ry, phi, fA, fS, x2, y2):
    if phi:
        # Our box bezier arcs can't handle rotations directly
        # move to a well known point, eliminate phi and transform the other point
        mx = mmult(rotate(-phi), translate(-x1, -y1))
        tx2, ty2 = transformPoint(mx, (x2, y2))
        # Convert to box form in unrotated coords
        cx, cy, rx, ry, start_ang, extent = end_point_to_center_parameters(
            0, 0, tx2, ty2, fA, fS, rx, ry
        )
        bp = bezier_arc_from_centre(cx, cy, rx, ry, start_ang, extent)
        # Re-rotate by the desired angle and add back the translation
        mx = mmult(translate(x1, y1), rotate(phi))
        res = []
        for x1, y1, x2, y2, x3, y3, x4, y4 in bp:
            res.append(
                transformPoint(mx, (x1, y1)) + transformPoint(mx, (x2, y2)) +
                transformPoint(mx, (x3, y3)) + transformPoint(mx, (x4, y4))
            )
        return res
    else:
        cx, cy, rx, ry, start_ang, extent = end_point_to_center_parameters(
            x1, y1, x2, y2, fA, fS, rx, ry
        )
        return bezier_arc_from_centre(cx, cy, rx, ry, start_ang, extent)
Beispiel #6
0
    def applyTransformOnGroup(self, group, transform):
        op, args = transform

        if op == 'scale':
            group.scale(*args)

        elif op == 'translate':
            group.translate(*args)

        elif op == 'rotate':
            if len(args) == 2:
                angle, center = args
                if center:
                    cx, cy = center
                    group.translate(cx, cy)
                    group.rotate(angle)
                    group.translate(-cx, -cy)
                else:
                    group.rotate(angle)

        elif op == "skewX":
            angle = args[0]
            group.skew(angle, 0)

        elif op == "skewY":
            angle = args[0]
            group.skew(0, angle)

        elif op == "matrix":
            group.transform = mmult(group.transform, args)
Beispiel #7
0
    def applyTransformOnGroup(self, group, transform):
        op, args = transform

        if op == 'scale':
            group.scale(*args)

        elif op == 'translate':
            group.translate(*args)

        elif op == 'rotate':
            if len(args) == 2:
                angle, center = args
                if center:
                    cx, cy = center
                    group.translate(cx, cy)
                    group.rotate(angle)
                    group.translate(-cx, -cy)
                else:
                    group.rotate(angle)

        elif op == "skewX":
            angle = args[0]
            group.skew(angle, 0)

        elif op == "skewY":
            angle = args[0]
            group.skew(0, angle)

        elif op == "matrix":
            group.transform = mmult(group.transform, args)
Beispiel #8
0
 def doTest0(c, vp, x, y, c0=0x0,c1=0xff0000, angle=0, c2=0xff00ff):
     c.ctm=(1,0,0,1,x,y)
     c.fillColor = c0
     doVPath(c,vp,128,128)
     c.ctm = shapes.mmult(c.ctm, shapes.rotate(180))
     c.fillColor = c1
     doVPath(c,vp,128,128)
     c.ctm=(1,0,0,1,x,y)
     c.pathBegin()
     c.ctm=(1,0,0,1,x+20,y)
     c.ctm = shapes.mmult(c.ctm, shapes.rotate(angle))
     c.moveTo(0,0)
     c.lineTo(20,0)
     c.strokeColor = c2
     c.pathStroke()
     c.ctm=(1,0,0,1,x+20,y-20)
     c.drawString(0,0,"Robin")
Beispiel #9
0
 def doTest0(c, vp, x, y, c0=0x0,c1=0xff0000, angle=0, c2=0xff00ff):
     c.ctm=(1,0,0,1,x,y)
     c.fillColor = c0
     doVPath(c,vp,128,128)
     c.ctm = shapes.mmult(c.ctm, shapes.rotate(180))
     c.fillColor = c1
     doVPath(c,vp,128,128)
     c.ctm=(1,0,0,1,x,y)
     c.pathBegin()
     c.ctm=(1,0,0,1,x+20,y)
     c.ctm = shapes.mmult(c.ctm, shapes.rotate(angle))
     c.moveTo(0,0)
     c.lineTo(20,0)
     c.strokeColor = c2
     c.pathStroke()
     c.ctm=(1,0,0,1,x+20,y-20)
     c.drawString(0,0,"Robin")
def computeTransformMatrix(transformList):
    """ compute the transform matrix that is the concatenation of all transforms in the 
        specifed transformList
    """
    transformMatrix = shapes.nullTransform()
    for transform in transformList:
        if not 'transform' in transform:
            raise InvalidTransform()
        if transform['transform'] is 'translate':
            transformMatrix = shapes.mmult(transformMatrix, shapes.translate(transform["tx"], transform["ty"]))
        elif transform['transform'] is 'rotate':
            transformMatrix = shapes.mmult(transformMatrix, shapes.translate(transform['cx'], transform['cy']))
            transformMatrix = shapes.mmult(transformMatrix, shapes.rotate(transform["theta"]))
            transformMatrix = shapes.mmult(transformMatrix, shapes.translate(-1.0 * transform['cx'], -1.0 * transform['cy']))
        elif transform['transform'] is 'scale':
            transformMatrix = shapes.mmult(transformMatrix, shapes.scale(transform['sx'], transform['sy']))
        else:
            raise InvalidTransform(transform['transform'])
    return transformMatrix
Beispiel #11
0
            doCTest(doCPath1, c, 0, 0)
            do_save(c, 1)

        if flagged(2):
            c = renderPM.PMCanvas(25, 25, bg=0xffffff)
            doCTest(doCPath2, c, 0, 0)
            do_save(c, 2, txt=1, pil=1)

        if flagged(3):
            c = renderPM.PMCanvas(256, 256, bg=0xffffff)
            c.fillColor = 0x000000
            c.setFont('Times-Roman', 18)
            text = "ABC"
            c.ctm = (1, 0, 0, ("invert" in sys.argv) and -1 or 1, 127.5, 127.5)
            c.drawString(0, 0, text)
            c.ctm = shapes.mmult(c.ctm, shapes.rotate(180))
            c.fillColor = 0xff0000
            c.drawString(0, 0, text)
            do_save(c, 3)

        if flagged(4):
            c = renderPM.PMCanvas(25, 25, bg=0xffffff)
            doCTest(doCPath4, c, 0, 0, c0=0x8000, c1=0xff0000)
            do_save(c, 4)

        if flagged(5):
            c = renderPM.PMCanvas(25, 25, bg=0xffffff)
            doCTest(doCPath5, c, 0, 0)
            do_save(c, 5)

        if flagged(6):
Beispiel #12
0
            doCTest(doCPath1, c, 0, 0 )
            do_save(c,1)

        if flagged(2):
            c = renderPM.PMCanvas(25, 25, bg=0xffffff)
            doCTest(doCPath2, c, 0, 0 )
            do_save(c,2,txt=1,pil=1)

        if flagged(3):
            c = renderPM.PMCanvas(256, 256, bg=0xffffff)
            c.fillColor = 0x000000
            c.setFont('Times-Roman',18)
            text = "ABC"
            c.ctm=(1,0,0,("invert" in sys.argv) and -1 or 1, 127.5,127.5)
            c.drawString(0, 0, text)
            c.ctm = shapes.mmult(c.ctm, shapes.rotate(180))
            c.fillColor = 0xff0000
            c.drawString(0, 0, text)
            do_save(c,3)

        if flagged(4):
            c = renderPM.PMCanvas(25, 25, bg=0xffffff)
            doCTest(doCPath4, c, 0, 0, c0=0x8000, c1=0xff0000)
            do_save(c,4)

        if flagged(5):
            c = renderPM.PMCanvas(25, 25, bg=0xffffff)
            doCTest(doCPath5, c, 0, 0 )
            do_save(c,5)

        if flagged(6):