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 #2
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)
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 #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)
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