Beispiel #1
0
def simple_background(canvas, document):
    header_text = document.title
    sub_header_text = document.subject

    canvas.saveState()
    margin = 0.2 * inch
    if header_text:
        canvas.setFont('Helvetica-Bold', 16)
        canvas.drawString(margin, A4[1] - 0.15 * inch - margin, header_text)

    if sub_header_text:
        canvas.setFont('Helvetica-Bold', 32)
        canvas.drawString(margin, A4[1] - 0.15 * inch - margin - 0.45 * inch,
                          sub_header_text)

    w = 1.5 * inch
    h = w / 257.0 * 86.0
    canvas.drawImage(os.path.join(resource_dir, "logo_pl.JPG"),
                     A4[0] - w - margin,
                     A4[1] - h - margin,
                     width=w,
                     height=h)

    p = canvas.beginPath()
    p.moveTo(0, A4[1] - h - 2 * margin)
    p.lineTo(A4[0], A4[1] - h - 2 * margin)
    canvas.drawPath(p)

    canvas.restoreState()
Beispiel #2
0
def spacer_ticker(canvas, feature, cLen, Y0, nudge, offset, side):
    """Draw separator indicators."""
    # take start and end points
    location = feature.location
    Zs = location.nofuzzy_start
    Ze = location.nofuzzy_end
    # calculate loop offset coordinates
    loop_offZs = offset_coord(Zs, cLen, offset)
    loop_offZe = offset_coord(Ze, cLen, offset)
    # calculate nudge offset coordinates
    offZs = nudge_coord(loop_offZs, nudge)
    offZe = nudge_coord(loop_offZe, nudge)
    xs, xe = offZs*u, offZe*u
    # set Y axis coordinates
    y0 = Y0-dop*3.5
    if side == 'low':
        yT1 = y0-h*4
    else:
        yT1 = y0+h*4
    # draw
    canvas.setLineWidth(2)
    ttl = canvas.beginPath()
    ttl.moveTo(xs,y0)
    ttl.lineTo(xs,yT1)
    ttl.lineTo(xe,yT1)
    ttl.lineTo(xe,y0)
    canvas.drawPath(ttl, stroke=1, fill=0)
    ttl.close()
Beispiel #3
0
 def draw(self, pmlOp, pageNr, canvas, pe):
     canvas.setLineWidth(pe.mm2points(pmlOp.width))
     path = canvas.beginPath()
     path.arc( pe.x(pmlOp.x - pmlOp.radius), pe.y(pmlOp.y - pmlOp.radius),
            pe.x(pmlOp.x + pmlOp.radius), pe.y(pmlOp.y + pmlOp.radius),
            startAng=pmlOp.startAngle, extent=90)
     canvas.drawPath(path)
Beispiel #4
0
def orf_eus(canvas, featL, coords, color_hex, shape):
    """Draw CDS and write count."""
    xs, xe, xn, y0, yt, yb, ynt, ynb = coords
    canvas.setLineWidth(1)
    # initialize path
    pORF = canvas.beginPath()
    if shape == 'square':
        pORF.moveTo(xs,ynt)
        pORF.lineTo(xe,ynt)
        pORF.lineTo(xe,ynb)
        pORF.lineTo(xs,ynb)
        pORF.lineTo(xs,ynt)
    # draw triangle-shaped ORFS
    elif featL <= minL:
        pORF.moveTo(xs,yt)
        pORF.lineTo(xe,y0)
        pORF.lineTo(xs,yb)
        pORF.lineTo(xs,yt)
    # draw arrow-shaped ORFS
    else:
        pORF.moveTo(xs,ynt)
        pORF.lineTo(xn,ynt)
        pORF.lineTo(xn,yt)
        pORF.lineTo(xe,y0)
        pORF.lineTo(xn,yb)
        pORF.lineTo(xn,ynb)
        pORF.lineTo(xs,ynb)
        pORF.lineTo(xs,ynt)
    # evaluate function category and set fill color
    canvas.setFillColor(color_hex)
    # finalize object path
    canvas.drawPath(pORF, stroke=1, fill=1)
    pORF.close()
    canvas.setFillColor(black)
Beispiel #5
0
 def draw_star(self,
               inner_length=1 * inch,
               outer_length=2 * inch,
               points=5,
               origin=None):
     canvas = self.canv
     canvas.setLineWidth(0)
     if not origin: canvas.translate(self.size * 0.5, self.size * 0.5)
     else: canvas.translate(*origin)
     canvas.setFillColor(self.fillcolor)
     canvas.setStrokeColor(self.strokecolor)
     p = canvas.beginPath()
     inner = False  # Start on top
     is_origin = True
     #print 'Drawing star with radius',outer_length,'(moving origin ',origin,')'
     for theta in range(0, 360, 360 / (points * 2)):
         if inner: r = inner_length
         else: r = outer_length
         x = (math.sin(math.radians(theta)) * r)
         y = (math.cos(math.radians(theta)) * r)
         #print 'POINT:',x,y
         if is_origin:
             p.moveTo(x, y)
             is_origin = False
         else:
             p.lineTo(x, y)
         inner = not inner
     p.close()
     canvas.drawPath(p, fill=1)
Beispiel #6
0
    def confidential(self, canvas):
        canvas.saveState()

        canvas.translate(18.5 * cm, 27.4 * cm)

        canvas.setLineWidth(3)
        canvas.setFillColorRGB(1, 0, 0)
        canvas.setStrokeGray(0.5)

        p = canvas.beginPath()
        p.moveTo(10, 0)
        p.lineTo(20, 10)
        p.lineTo(30, 0)
        p.lineTo(40, 10)
        p.lineTo(30, 20)
        p.lineTo(40, 30)
        p.lineTo(30, 40)
        p.lineTo(20, 30)
        p.lineTo(10, 40)
        p.lineTo(0, 30)
        p.lineTo(10, 20)
        p.lineTo(0, 10)

        canvas.drawPath(p, fill=1, stroke=0)

        canvas.restoreState()
Beispiel #7
0
    def process(self):
        kwargs = dict(self.getAttributeValues(ignore=('points', )))

        # Start the path and set the cursor to the start location.
        canvas = attr.getManager(self, interfaces.ICanvasManager).canvas
        self.path = canvas.beginPath()
        self.path.moveTo(kwargs.pop('x'), kwargs.pop('y'))

        # Process the text before the first sub-directive.
        if self.element.text is not None:
            self.processPoints(self.element.text)
        # Handle each sub-directive.
        for directive in self.element.getchildren():
            if directive.tag in self.factories:
                self.factories[directive.tag](directive, self).process()
            # If there is more text after sub-directive, process it.
            if directive.tail is not None:
                self.processPoints(directive.tail)

        if kwargs.pop('close', False):
            self.path.close()

        if kwargs.pop('clip', False):
            canvas.clipPath(self.path, **kwargs)
        else:
            canvas.drawPath(self.path, **kwargs)
    def __init__(self, canvas, segment, height, accumulated_height, textured, bottom_layer, fill_color=None):
        """
        :param canvas: the PDF canvas object
        :param segment: The layer segment to be drawn.  It may be ``"whole"``,
            ``"left"``, or ``"right"``.  The latter two are used for structured
            samples to draw the two columns they consist of.
        :param height: height of the layer
        :param accumulated_height: height of the layer measured from the very
            bottom of the stack
        :param textured: whether the layer is textured
        :param bottom_layer: the next non-structured layer below the current one
        :param fill_color: the color of the filling; if not given, just the outline
            is drawn

        :type canvas: canvas.Canvas
        :type segment: str
        :type height: float
        :type accumulated_height: float
        :type textured: bool
        :type bottom_layer: `Layer`
        :type fill_color: str
        """
        self.canvas, self.segment, self.height, self.accumulated_height, self.fill_color = \
            canvas, segment, height, accumulated_height, fill_color

        random.seed(1)
        self.textured_points = [(dimensions["stack_width"] / parameters["grid_points"] * i,
                                 random.uniform(-parameters["roughness"], parameters["roughness"]))
                                for i in range(parameters["grid_points"] + 1)]
        self.edge_indices = [int(round(x * parameters["grid_points"])) for x in [1 / 8, 3 / 8, 5 / 8, 7 / 8]]

        self.voffset = 0 if fill_color or not bottom_layer else bottom_layer.accumulated_height
        bottom_textured = bottom_layer and bottom_layer.textured
        self.p = canvas.beginPath()
        if segment == "whole":
            self.bottom_left = (0,
                                0 if fill_color else self.voffset + (self.textured_points[0][1] if bottom_textured else 0))
            self.top_left = (self.bottom_left[0], (self.textured_points[0][1] if textured else 0) + accumulated_height)
            self.bottom_right = (dimensions["stack_width"],
                                 0 if fill_color else self.voffset + (self.textured_points[-1][1] if bottom_textured else 0))
            self.top_right = (self.bottom_right[0], (self.textured_points[-1][1] if textured else 0) + accumulated_height)
        elif segment == "left":
            self.bottom_left = (self.textured_points[self.edge_indices[0]][0], 0 if fill_color else
                                self.voffset + (self.textured_points[self.edge_indices[0]][1] if bottom_textured else 0))
            self.top_left = (self.bottom_left[0],
                             (self.textured_points[self.edge_indices[0]][1] if textured else 0) + accumulated_height)
            self.bottom_right = (self.textured_points[self.edge_indices[1]][0], 0 if fill_color else
                                 self.voffset + (self.textured_points[self.edge_indices[1]][1] if bottom_textured else 0))
            self.top_right = (self.bottom_right[0],
                              (self.textured_points[self.edge_indices[1]][1] if textured else 0) + accumulated_height)
        else:
            self.bottom_left = (self.textured_points[self.edge_indices[2]][0], 0 if fill_color else
                                self.voffset + (self.textured_points[self.edge_indices[2]][1] if bottom_textured else 0))
            self.top_left = (self.bottom_left[0],
                             (self.textured_points[self.edge_indices[2]][1] if textured else 0) + accumulated_height)
            self.bottom_right = (self.textured_points[self.edge_indices[3]][0], 0 if fill_color else
                                 self.voffset + (self.textured_points[self.edge_indices[3]][1] if bottom_textured else 0))
            self.top_right = (self.bottom_right[0],
                              (self.textured_points[self.edge_indices[3]][1] if textured else 0) + accumulated_height)
        self.p.moveTo(*self.bottom_left)
Beispiel #9
0
    def _draw_single_background(self, canvas, x, margins, width, height):
        canvas.saveState()

        clipping_mask = canvas.beginPath()
        clipping_mask.roundRect(
            x + margins[Border.LEFT], margins[Border.BOTTOM],
            width - margins[Border.RIGHT] - margins[Border.LEFT],
            height - margins[Border.TOP] - margins[Border.BOTTOM],
            self.BACKGROUND_CORNER_DIAMETER)
        canvas.clipPath(clipping_mask, stroke=0, fill=0)

        # get optimum background orientation
        background_orientation = best_orientation(self.background_image_path,
                                                  width, height)
        if background_orientation == Orientation.TURN90:
            canvas.rotate(90)
            canvas.translate(0, -self.WIDTH * 2)
            canvas.drawImage(self.background_image_path,
                             0,
                             0,
                             width=height,
                             height=width,
                             mask=None)
        else:
            canvas.drawImage(self.background_image_path,
                             x,
                             0,
                             width=width,
                             height=height,
                             mask=None)

        canvas.restoreState()
Beispiel #10
0
def ref_ticker(canvas, feature, cLen, Y0, nudge, offset):
    """Draw contig separators."""
    # get contig name
    name = feature.qualifiers.get('id')[0]
    # take start and end points
    location = feature.location
    Zs = location.nofuzzy_start
    Ze = location.nofuzzy_end
    # calculate loop offset coordinates
    loop_offZs = offset_coord(Zs, cLen, offset)
    loop_offZe = offset_coord(Ze, cLen, offset)
    # calculate nudge offset coordinates
    offZs = nudge_coord(loop_offZs, nudge)
    offZe = nudge_coord(loop_offZe, nudge)
    xs, xe = offZs*u, offZe*u
    xmid = (xe+xs)/2
    # set Y axis coordinates
    y0 = Y0+dop*3
    # draw
    canvas.setLineWidth(2)
    ttl = canvas.beginPath()
    ttl.moveTo(xs,y0+w)
    ttl.lineTo(xs,y0+w+h*2)
    ttl.lineTo(xe,y0+w+h*2)
    ttl.lineTo(xe,y0+w)
    canvas.drawPath(ttl, stroke=1, fill=0)
    canvas.setFont(bFont, NfSize)
    canvas.drawCentredString(xmid, y0+h*5, name)
    canvas.setFont(rFont, NfSize)
    ttl.close()
Beispiel #11
0
def funcy(canvas, document):
    global header_text
    global sub_header_text

    canvas.saveState()
    margin = 0.2 * inch
    canvas.setFont('Helvetica-Bold', 16)
    canvas.drawString(margin, A4[1] - 0.2 * inch - margin, header_text)

    canvas.setFont('Helvetica-Bold', 32)
    canvas.drawString(margin, A4[1] - 0.2 * inch - margin - 0.40 * inch,
                      sub_header_text)

    w = 1.5 * inch
    h = w / 257.0 * 86.0
    canvas.drawImage(os.path.join(resource_dir, "logo_pl.JPG"),
                     A4[0] - w - margin,
                     A4[1] - h - margin,
                     width=w,
                     height=h)

    p = canvas.beginPath()
    p.moveTo(0, A4[1] - h - 2 * margin)
    p.lineTo(A4[0], A4[1] - h - 2 * margin)
    canvas.drawPath(p)

    canvas.restoreState()
Beispiel #12
0
 def drawShape(self, canvas, controls, color):
     """Utlity to draw a closed shape through a list of control points;
     extends the previous proc"""
     canvas.setFillColor(color)
     p = canvas.beginPath()
     self.curveThrough(p, controls)
     p.close()
     canvas.drawPath(p, stroke=1, fill=1)
Beispiel #13
0
def canvasBackground(canvas,colour):
    canvas.setFillColor(colour)
    path = canvas.beginPath()
    path.moveTo(0*cm,0*cm)
    path.lineTo(0*cm,30*cm)
    path.lineTo(25*cm,30*cm)
    path.lineTo(25*cm,0*cm)
    canvas.drawPath(path,True,True)  
Beispiel #14
0
def detailsBackground(canvas,colour):
    canvas.setFillColor(colour)
    path = canvas.beginPath()
    path.moveTo(0.7*cm,16.5*cm)
    path.lineTo(0.7*cm,24*cm)
    path.lineTo(20*cm,24*cm)
    path.lineTo(20*cm,16.5*cm)
    path.lineTo(0.7*cm,16.5*cm)
    canvas.drawPath(path,True,True)     
Beispiel #15
0
 def draw_circle(self, x, y, r):
     # Test...
     canvas = self.canv
     canvas.setLineWidth(0)
     canvas.setStrokeColor(colors.grey)
     canvas.setFillColor(colors.grey)
     p = canvas.beginPath()
     p.circle(x, y, r)
     p.close()
     canvas.drawPath(p, fill=1)
Beispiel #16
0
def baseliner(cLen, canvas, Y_map, nudge):  # loop offset has no effect
    """Draw sequence baseline."""
    canvas.setLineWidth(3)
    y0 = Y_map
    Zs = 0              # all sequences are initially aligned on the left
    Ze = cLen
    # calculate nudge offsets
    offZs = nudge_coord(Zs, nudge)
    offZe = nudge_coord(Ze, nudge)
    # set zeroes
    x0 = offZs*u
    x1 = offZe*u
    pBL = canvas.beginPath()
    pBL.moveTo(x0,y0)
    pBL.lineTo(x1,y0)
    canvas.drawPath(pBL, stroke=1, fill=0)
    pBL.close()
Beispiel #17
0
    def draw(self, pmlOp, pageNr, canvas, pe):
        if pmlOp.lw != -1:
            canvas.setLineWidth(pe.mm2points(pmlOp.lw))

        path = canvas.beginPath()

        path.rect(pe.x(pmlOp.x),
                  pe.y(pmlOp.y) - pe.mm2points(pmlOp.height),
                  pe.mm2points(pmlOp.width), pe.mm2points(pmlOp.height))

        if pmlOp.fillType == pml.STROKE:
            canvas.drawPath(path)
        elif pmlOp.fillType == pml.FILL:
            canvas.drawPath(path, stroke=0, fill=1)
        elif pmlOp.fillType == pml.STROKE_FILL:
            canvas.drawPath(path, stroke=1, fill=1)
        else:
            print "Invalid fill type for RectOp"
Beispiel #18
0
def seq_scale(canvas, scX, scY, incrT, incrN, dip, dop) :
    """Draws the sequence scale bar."""
    canvas.setLineWidth(1.2)
    canvas.setFillColor(black)
    incrCNT = 0							    # initialize count of increments
    psc = canvas.beginPath()
    psc.moveTo(scX, scY+dip-dop)		        # start at beginning (duh!)
    psc.lineTo(scX+incrT*incrN, scY+dip-dop)	# draw the scale baseline
    # draw ticks until the max number of increments is reached
    while incrCNT <= incrN :
        psc.moveTo(scX+incrT*incrCNT, scY+dip-dop)
        psc.lineTo(scX+incrT*incrCNT, scY+dip)
        incrCNT += 1
    canvas.drawPath(psc, stroke=1, fill=0)
    psc.close()
    # write out scale extremities values (needs hand-fix if not using kbs)
    canvas.setFont(rFont,NfSize)
    canvas.drawRightString(scX, scY+dip+dop, '0')
    canvas.drawString(scX+incrT*incrN, scY+dip+dop, str(incrN)+' kb')
Beispiel #19
0
def contig_ticker(canvas, feature, cLen, Y0, nudge, offset, side):
    """Draw contig separators."""
    # get contig name
    try:
    	name = feature.qualifiers.get('locus_tag')[0]
    except TypeError:
    	name = 'unnamed feature'
    # take start and end points
    location = feature.location
    Zs = location.nofuzzy_start
    Ze = location.nofuzzy_end
    # calculate loop offset coordinates
    loop_offZs = offset_coord(Zs, cLen, offset)
    loop_offZe = offset_coord(Ze, cLen, offset)
    # calculate nudge offset coordinates
    offZs = nudge_coord(loop_offZs, nudge)
    offZe = nudge_coord(loop_offZe, nudge)
    xs, xe = offZs*u, offZe*u
    # set Y axis coordinates
    if side == 'low':
        y0 = Y0-dop*3.5
        yT1 = y0-h*4
        yT2 = y0-h*4.5
        yT3 = y0-h*8
    else:
        y0 = Y0+dop*3.5
        yT1 = y0+h*4
        yT2 = y0+h*4.5
        yT3 = y0+h*8
    # draw
    canvas.setLineWidth(2)
    ttl = canvas.beginPath()
    ttl.moveTo(xs,y0)
    ttl.lineTo(xs,yT1)
    ttl.lineTo(xs+dop,yT1)
    canvas.drawPath(ttl, stroke=1, fill=0)
    canvas.setFont(bFont, NfSize)
    canvas.drawString(xs+dop*2, yT2, name)
    canvas.setFont(rFont, SfSize)
    canvas.drawString(xs+dop*2,yT3,"".join(["[",str(Zs),"-",str(Ze),"]"]))
    canvas.setFont(rFont, NfSize)
    ttl.close()
Beispiel #20
0
def baseliner(cLen, canvas, Y_map, offset, offset_mode):
    """Draw sequence baseline."""
    canvas.setLineWidth(3)
    y0 = Y_map
    Zs = 0  # all sequences are initially aligned on the left
    Ze = cLen
    # calculate offsets
    if offset_mode == 'nudge':
        offZs = nudge_coord(Zs, offset)
        offZe = nudge_coord(Ze, offset)
    else:
        offZs = Ze
        offZe = Zs
    x0 = offZs * u
    x1 = offZe * u
    pBL = canvas.beginPath()
    pBL.moveTo(x0, y0)
    pBL.lineTo(x1, y0)
    canvas.drawPath(pBL, stroke=1, fill=0)
    pBL.close()
Beispiel #21
0
    def draw(self):
        canvas = self.canv
        canvas.setLineWidth(0.1 * cm)
        canvas.setFillColor(self.fillcolor)
        canvas.setStrokeColor(self.strokecolor)
        canvas.translate((1 - self.scale) * self.size / 2,
                         (1 - self.scale) * self.size / 2)
        canvas.scale(self.scale, self.scale)
        canvas.setFillColor(self.fillcolor)
        canvas.setLineJoin(2)

        ident = self.ident
        shapes = [[(0, 0), (1, 0), (0.5, 1)], [(0, 0), (1, 0), (0, 1), (1, 1)],
                  [(0, 0), (1, 0), (1, 1), (0, 1)],
                  [(0, 0.5), (0.5, 1), (1, 0.5), (0.5, 0)],
                  [(0, 1), (1, 1), (0.5, 0)]]

        if self.ident % 2 == 1:
            canvas.setDash(3, 2)

        p = canvas.beginPath()

        ident = ident // 2
        if self.ident % 2 == 0:
            p.moveTo(0.5 * self.size, 0)
            p.lineTo(0.5 * self.size, 1 * self.size)

        ident = ident // 2
        sndx = (ident) % (len(shapes) + 1)
        if sndx < len(shapes):
            d = shapes[sndx]
            p.moveTo(d[0][0] * self.size, d[0][1] * self.size)
            for ndx in range(len(d) - 1):
                p.lineTo(d[ndx + 1][0] * self.size, d[ndx + 1][1] * self.size)
        else:
            p.ellipse(0, 0, 1 * self.size, 1 * self.size)

        p.close()

        canvas.drawPath(p)
    def _draw_single_background(self,
                                canvas,
                                x,
                                margins,
                                width,
                                height,
                                orientation=Orientation.NORMAL):
        canvas.saveState()

        canvas.setFillColor("white")
        clipping_mask = canvas.beginPath()

        if orientation == Orientation.TURN90:
            clipping_mask.roundRect(
                x + margins[Border.BOTTOM],
                margins[Border.LEFT],
                width - margins[Border.TOP] - margins[Border.BOTTOM],
                height - margins[Border.RIGHT] - margins[Border.LEFT],
                self.BACKGROUND_CORNER_DIAMETER,
            )
        else:
            clipping_mask.roundRect(
                x + margins[Border.LEFT],
                margins[Border.BOTTOM],
                width - margins[Border.RIGHT] - margins[Border.LEFT],
                height - margins[Border.TOP] - margins[Border.BOTTOM],
                self.BACKGROUND_CORNER_DIAMETER,
            )
        canvas.clipPath(clipping_mask, stroke=0, fill=1)

        if self.background_image_path is not None:
            canvas.drawImage(self.background_image_path,
                             x,
                             0,
                             width=width,
                             height=height,
                             mask=None)

        canvas.restoreState()
Beispiel #23
0
    def draw(self, pmlOp, pageNr, canvas, pe):
        p = pmlOp.points

        pc = len(p)

        if pc < 2:
            print "LineOp contains only %d points" % pc

            return

        canvas.setLineWidth(pe.mm2points(pmlOp.width))

        path = canvas.beginPath()

        path.moveTo(*pe.xy(p[0]))

        for point in p[1:]:
            path.lineTo(*pe.xy(point))

        if pmlOp.isClosed:
            path.close()

        canvas.drawPath(path)
Beispiel #24
0
    def beforeDrawPage(self, canvas, doc):
        self.senderFrame._reset()
        self.senderFrame.addFromList(self.sendertext[:], canvas)
        self.senderFooterFrame._reset()
        self.senderFooterFrame.addFromList(self.sendertext[:], canvas)
        self.detailsFooterFrame._reset()
        self.detailsFooterFrame.addFromList(self.detailstext, canvas)
        self.bankFooterFrame._reset()
        self.bankFooterFrame.addFromList(self.banktext, canvas)
        with save_state(canvas):
            p = canvas.beginPath()
            p.moveTo(15 * mm, 35 * mm)
            p.lineTo(self.width - 15 * mm, 35 * mm)
            canvas.drawPath(p)

            to = canvas.beginText()
            to.setTextOrigin(118 * mm, self.height - 15 * mm)
            to.setFont('Helvetica-Bold', 24)
            to.textOut(self.kind_text)
            to.moveCursor(50 * mm, 0)
            to.setFont('Helvetica', 10)
            to.textOut(_(u'page %d') % doc.page)
            to.setTextOrigin(118 * mm, self.height - 21 * mm)
            to.textOut(self.date_text)
            to.moveCursor(50 * mm, 0)
            to.textLine(formatters.date(self.date))
            to.moveCursor(-50 * mm, 0)
            to.textOut(self.ocr_text)
            to.moveCursor(50 * mm, 0)
            to.textLine(self.ocr)
            to.moveCursor(-50 * mm, 0)
            if self.expiryDate:
                to.textOut(_(u'Due date'))
                to.moveCursor(50 * mm, 0)
                to.textLine(formatters.date(self.expiryDate))
                to.moveCursor(-50 * mm, 0)
            canvas.drawText(to)
Beispiel #25
0
def contig_ticker(canvas, feature, cLen, Y0, offset, offset_mode):
    """Draw contig separators."""
    # get contig name
    name = feature.qualifiers.get('id')[0]
    # take start and end points
    location = feature.location
    Zs = location.nofuzzy_start
    Ze = location.nofuzzy_end
    # calculate offset coordinates
    if offset_mode == 'loop':
        offZs = offset_coord(Zs, cLen, offset)
        offZe = offset_coord(Ze, cLen, offset)
    elif offset_mode == 'nudge':
        offZs = nudge_coord(Zs, offset)
        offZe = nudge_coord(Ze, offset)
    else:
        offZs = Zs
        offZe = Ze
    xs, xe = offZs * u, offZe * u
    # set Y axis coordinates
    y0 = Y0 - dop * 3.5
    # draw
    canvas.setLineWidth(2)
    ttl = canvas.beginPath()
    ttl.moveTo(xs, y0)
    ttl.lineTo(xs, y0 - h * 4)
    ttl.lineTo(xs + dop, y0 - h * 4)
    canvas.drawPath(ttl, stroke=1, fill=0)
    canvas.setFont(bFont, NfSize)
    canvas.drawString(xs + dop * 2, y0 - h * 4.5, name)
    canvas.setFont(rFont, SfSize)
    canvas.drawString(xs + dop * 2, y0 - h * 8,
                      "".join(["[", str(Zs), "-",
                               str(Ze), "]"]))
    canvas.setFont(rFont, NfSize)
    ttl.close()
Beispiel #26
0
    def render_element(root, element, canvas, styles, text_state=None):
        canvas.saveState()

        current_style = {}
        if len(styles):
            current_style.update(styles[-1])
        for declaration in element.get("style", "").split(";"):
            if declaration == "":
                continue
            key, value = declaration.split(":")
            if key == "dominant-baseline" and value == "inherit":
                continue
            current_style[key] = value
        styles.append(current_style)

        if "stroke-width" in current_style:
            canvas.setLineWidth(float(current_style["stroke-width"]))

        if "stroke-dasharray" in current_style:
            canvas.setDash([
                float(length)
                for length in current_style["stroke-dasharray"].split(",")
            ])

        if current_style.get("visibility") != "hidden":

            if "transform" in element.attrib:
                for transformation in element.get("transform").split(")")[::1]:
                    if transformation:
                        type, arguments = transformation.split("(")
                        arguments = arguments.split(",")
                        if type.strip() == "translate":
                            if len(arguments) == 2:
                                canvas.translate(float(arguments[0]),
                                                 float(arguments[1]))
                        elif type.strip() == "rotate":
                            if len(arguments) == 1:
                                canvas.rotate(float(arguments[0]))
                            if len(arguments) == 3:
                                canvas.translate(float(arguments[1]),
                                                 float(arguments[2]))
                                canvas.rotate(float(arguments[0]))
                                canvas.translate(-float(arguments[1]),
                                                 -float(arguments[2]))

            if element.tag == "svg":
                if "background-color" in current_style:
                    set_fill_color(
                        canvas,
                        toyplot.color.css(current_style["background-color"]))
                    canvas.rect(
                        0,
                        0,
                        float(element.get("width")[:-2]),
                        float(element.get("height")[:-2]),
                        stroke=0,
                        fill=1,
                    )
                for child in element:
                    render_element(root, child, canvas, styles)

            elif element.tag == "g":
                if element.get("clip-path", None) is not None:
                    clip_id = element.get("clip-path")[5:-1]
                    clip_path = root.find(".//*[@id='%s']" % clip_id)
                    for child in clip_path:
                        if child.tag == "rect":
                            x = float(child.get("x"))
                            y = float(child.get("y"))
                            width = float(child.get("width"))
                            height = float(child.get("height"))
                            path = canvas.beginPath()
                            path.moveTo(x, y)
                            path.lineTo(x + width, y)
                            path.lineTo(x + width, y + height)
                            path.lineTo(x, y + height)
                            path.close()
                            canvas.clipPath(path, stroke=0, fill=1)
                        else:
                            toyplot.log.error("Unhandled clip tag: %s" %
                                              child.tag)  # pragma: no cover

                for child in element:
                    render_element(root, child, canvas, styles)

            elif element.tag == "clipPath":
                pass

            elif element.tag == "line":
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)
                    canvas.line(
                        float(element.get("x1")),
                        float(element.get("y1")),
                        float(element.get("x2")),
                        float(element.get("y2")),
                    )
            elif element.tag == "path":
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)
                    path = canvas.beginPath()
                    commands = element.get("d").split()
                    while len(commands):
                        command = commands.pop(0)
                        if command == "L":
                            path.lineTo(float(commands.pop(0)),
                                        float(commands.pop(0)))
                        elif command == "M":
                            path.moveTo(float(commands.pop(0)),
                                        float(commands.pop(0)))
                    canvas.drawPath(path)
            elif element.tag == "polygon":
                fill, fill_gradient = get_fill(root, current_style)
                if fill_gradient is not None:
                    raise NotImplementedError(
                        "Gradient <polygon> not implemented."
                    )  # pragma: no cover
                if fill is not None:
                    set_fill_color(canvas, fill)
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)

                points = [
                    point.split(",")
                    for point in element.get("points").split()
                ]
                path = canvas.beginPath()
                for point in points[:1]:
                    path.moveTo(float(point[0]), float(point[1]))
                for point in points[1:]:
                    path.lineTo(float(point[0]), float(point[1]))
                path.close()
                canvas.drawPath(path,
                                stroke=stroke is not None,
                                fill=fill is not None)
            elif element.tag == "rect":
                fill, fill_gradient = get_fill(root, current_style)
                if fill is not None:
                    set_fill_color(canvas, fill)
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)

                x = float(element.get("x"))
                y = float(element.get("y"))
                width = float(element.get("width"))
                height = float(element.get("height"))

                path = canvas.beginPath()
                path.moveTo(x, y)
                path.lineTo(x + width, y)
                path.lineTo(x + width, y + height)
                path.lineTo(x, y + height)
                path.close()

                if fill_gradient is not None:
                    pdf_colors = []
                    pdf_offsets = []
                    for stop in fill_gradient:
                        offset = float(stop.get("offset"))
                        color = toyplot.color.css(stop.get("stop-color"))
                        opacity = float(stop.get("stop-opacity"))
                        pdf_colors.append(
                            reportlab.lib.colors.Color(color["r"], color["g"],
                                                       color["b"], color["a"]))
                        pdf_offsets.append(offset)
                    canvas.saveState()
                    canvas.clipPath(path, stroke=0, fill=1)
                    canvas.setFillAlpha(1)
                    canvas.linearGradient(
                        float(fill_gradient.get("x1")),
                        float(fill_gradient.get("y1")),
                        float(fill_gradient.get("x2")),
                        float(fill_gradient.get("y2")),
                        pdf_colors,
                        pdf_offsets,
                    )
                    canvas.restoreState()

                canvas.drawPath(path,
                                stroke=stroke is not None,
                                fill=fill is not None)
            elif element.tag == "circle":
                fill, fill_gradient = get_fill(root, current_style)
                if fill_gradient is not None:
                    raise NotImplementedError(
                        "Gradient <circle> not implemented."
                    )  # pragma: no cover
                if fill is not None:
                    set_fill_color(canvas, fill)
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)

                cx = float(element.get("cx"))
                cy = float(element.get("cy"))
                r = float(element.get("r"))
                canvas.circle(cx,
                              cy,
                              r,
                              stroke=stroke is not None,
                              fill=fill is not None)
            elif element.tag == "text":
                text_state = {"x": 0, "y": 0, "chunks": [[]]}
                for child in element:
                    render_element(root, child, canvas, styles, text_state)
                for chunk in text_state["chunks"]:
                    width = sum([span[7] for span in chunk])

                    dx = 0
                    text_anchor = current_style.get("text-anchor", "start")
                    if text_anchor == "middle":
                        dx = -width * 0.5
                    elif text_anchor == "end":
                        dx = -width

                    for x, y, fill, stroke, font_family, font_size, text, width in chunk:
                        canvas.saveState()
                        canvas.setFont(font_family, font_size)
                        if fill is not None:
                            set_fill_color(canvas, fill)
                        if stroke is not None:
                            set_stroke_color(canvas, stroke)
                        canvas.translate(x + dx, y)
                        canvas.scale(1, -1)
                        canvas.drawString(0, 0, text)
                        canvas.restoreState()

            elif element.tag == "tspan":
                #                    if "font-weight" in current_style:
                #                        font_description.set_weight(
                #                            pango.WEIGHT_BOLD if current_style["font-weight"] == "bold" else pango.WEIGHT_NORMAL)
                font_family = get_font_family(current_style)
                font_size = toyplot.units.convert(
                    current_style["font-size"].strip(), "px")

                string_width = reportlab.pdfbase.pdfmetrics.stringWidth(
                    element.text, font_family, font_size)
                ascent, descent = reportlab.pdfbase.pdfmetrics.getAscentDescent(
                    font_family, font_size)

                if "x" in element.attrib:
                    text_state["x"] = float(element.get("x"))
                    text_state["chunks"].append([])

                if "dy" in element.attrib:
                    text_state["y"] += float(element.get("dy"))

                x = text_state["x"]
                y = text_state["y"]

                alignment_baseline = current_style.get("alignment-baseline",
                                                       "middle")
                if alignment_baseline == "hanging":
                    y += ascent
                elif alignment_baseline == "central":
                    y += ascent * 0.5
                elif alignment_baseline == "middle":
                    y += (ascent + descent) * 0.5
                elif alignment_baseline == "alphabetic":
                    pass
                else:
                    raise ValueError("Unsupported alignment-baseline: %s" %
                                     alignment_baseline)  # pragma: no cover

                baseline_shift = current_style.get("baseline-shift",
                                                   "0").strip()
                baseline_shift = toyplot.units.convert(baseline_shift, "px",
                                                       "px", ascent - descent)
                y -= baseline_shift

                fill, fill_gradient = get_fill(root, current_style)
                stroke = get_stroke(current_style)

                text_state["chunks"][-1].append(
                    (x, y, fill, stroke, font_family, font_size, element.text,
                     string_width))
                text_state["x"] += string_width

            elif element.tag in ["defs", "title"]:
                pass

            else:
                raise Exception("unhandled tag: %s" %
                                element.tag)  # pragma: no cover

        styles.pop()
        canvas.restoreState()
Beispiel #27
0
 def __init__(self, glyphSet, canvas):
     BasePen.__init__(self, glyphSet)
     self.path = canvas.beginPath()
Beispiel #28
0
    def beforeDrawPage(self, canvas, doc):
        BaseInvoiceTemplate.beforeDrawPage(self, canvas, doc)
        self.rcptFrame.addFromList(self.rcpttext, canvas)
        with save_state(canvas):
            p = canvas.beginPath()
            to = canvas.beginText()
            to.setTextOrigin(20 * mm, self.height - 44 * mm)
            to.setFont('Helvetica', 8)
            to.textOut(_(u'Your reference'))
            to.setFont('Helvetica', 10)
            to.textOut(' ')
            to.textLine(self.buyerref)
            to.setFont('Helvetica', 8)
            to.textLine(_(u'Your notes'))
            paragraphs = [
                Paragraph(line, self.styles['Normal'])
                for line in self.annotation
            ]
            self.annotationframe.addFromList(paragraphs, canvas)
            canvas.drawText(to)

            if self.pgnum and self.total != 0:
                ypos = self.height - 75 * mm - 5
                to = canvas.beginText()
                to.setTextOrigin(20 * mm, ypos)
                to.setFont('Helvetica', 10)
                to.textLine('Betalas till')
                to.setFont('Helvetica-Bold', 12)
                to.textOut('Pg ' + formatters.pgnum(self.pgnum))

                to.setTextOrigin(55 * mm, ypos)
                to.setFont('Helvetica', 10)
                to.textLine('OCR / referens')
                to.setFont('Helvetica-Bold', 12)
                to.textOut(self.ocr)

                to.setTextOrigin(85 * mm, ypos)
                to.setFont('Helvetica', 10)
                to.textLine('Att betala')
                to.setFont('Helvetica-Bold', 12)
                to.textLine(formatters.money(self.total, True))

                ypos = to.getY() - 5
                if self.expiryDate:
                    to.setTextOrigin(20 * mm, ypos)
                    to.setFont('Helvetica', 10)
                    to.textLine(u'Förfallodatum')
                    to.setFont('Helvetica-Bold', 12)
                    to.textLine(formatters.date(self.expiryDate))

                    latestyle = ParagraphStyle('Late',
                                               fontName='Helvetica',
                                               fontSize=8,
                                               leading=9)
                    lateframe = Frame(45 * mm,
                                      ypos - 15,
                                      65 * mm,
                                      22,
                                      id='latefee',
                                      leftPadding=0,
                                      bottomPadding=0,
                                      rightPadding=0,
                                      topPadding=0,
                                      showBoundary=self.showBoundary)
                    lateframe.addFromList([Paragraph(self.latefee, latestyle)],
                                          canvas)

                canvas.drawText(to)
                canvas.roundRect(17 * mm, ypos - 21, 100 * mm + 68, 68, 6)

                # Need to set ECI to UTF-8
                # seee https://github.com/zxing/zxing/blob/3de3374dd25739bde952c788c1064cb17bc11ff8/core/src/main/java/com/google/zxing/qrcode/decoder/Mode.java
                # etc, update qrcode encoder...
                qd = Drawing()
                # No border, deal with quiet zone manually
                data = [QRECI(26), QR8bitByte(self.qrcode)]
                q = qr.QrCodeWidget(data,
                                    barLevel='L',
                                    barBorder=4.0,
                                    barWidth=68.0,
                                    barHeight=68.0)
                qd.add(q)
                renderPDF.draw(qd, canvas, 117 * mm, ypos - 21)

                qrstyle = ParagraphStyle('QR',
                                         fontName='Helvetica',
                                         fontSize=10,
                                         leading=12)
                qrinfoframe = Frame(145 * mm,
                                    ypos - 21,
                                    self.width - 160 * mm,
                                    68,
                                    id='qrinfo',
                                    topPadding=0,
                                    leftPadding=0,
                                    bottomPadding=0,
                                    rightPadding=0,
                                    showBoundary=self.showBoundary)
                qrinfoframe.addFromList([
                    Paragraph(
                        u'Om du betalar med bankapp kan du skanna QR-koden '
                        u'bredvid eller OCR-raden nedan. '
                        u'Läs mer om QR-koden på http://usingqr.se', qrstyle)
                ], canvas)

                # http://www.nordea.se/sitemod/upload/root/content/nordea_se/foretag/programleverantorer/filer/ocr_totalin.pdf
                to = canvas.beginText()
                to.setTextOrigin(10 * mm, self.height - 105 * mm)
                to.setFont('OCRB', 10)

                #  7777777777666666666655555555554444444444333333333322222222221111111111000000000
                #  9876543210987654321098765432109876543210987654321098765432109876543210987654321
                # '#              123456789012 #12345678 50   9 >                12345678 #14#    '
                #  # ooooooooooooooooooooooooo #kkkkkkkk öö   c >                PpppppppP#tt#

                ocrformat = '# {ocr:>25} #{totalkr:>8} {totaldec:<02}   {totalcheck:1} > {pgnum:>23} #14#    '
                totalkr = str(int(self.total))  # integer part of Decimal
                totaldec = str(int(self.total % 1 *
                                   100))  # fraction part of Decimal
                totalcheck = luhn.luhn_checksum(
                    str(self.total).replace('.', '') + '0')
                ocrkeys = {
                    'ocr': self.ocr,
                    'totalkr': totalkr,
                    'totaldec': totaldec,
                    'totalcheck': totalcheck,
                    'pgnum': self.pgnum
                }

                ocrline = ocrformat.format(**ocrkeys)

                assert len(ocrline) == 79
                to.textLine(ocrline)

                canvas.drawText(to)
Beispiel #29
0
                 background=[0xff, 0xff, 0xff])
#address_code.show()

qr_code_file = 'code.png'

qr_path = os.path.expanduser(qr_code_file)
logo_path = os.path.expanduser('logo.png')

im = Image.open(qr_code_file)
qr_width, qr_height = im.size

canvas = canvas.Canvas("candy-wallet.pdf", pagesize=landscape(letter))

canvas.setFillColor((HexColor("#ab20fd")))

path = canvas.beginPath()
path.moveTo(-5 * cm, -5 * cm)
path.lineTo(-5 * cm, 35 * cm)
path.lineTo(35 * cm, 35 * cm)
path.lineTo(35 * cm, -5 * cm)
canvas.drawPath(path, True, True)

canvas.setLineWidth(.9)
canvas.setFont('Helvetica', 10)
canvas.setFillColorRGB(32, 5, 137)

canvas.drawString(35, 550, 'Address:')
canvas.drawString(35, 540, address)

canvas.drawString(35, 530, 'Private Key:')
canvas.drawString(35, 520, private_key)
Beispiel #30
0
    def render_element(root, element, canvas, styles):
        canvas.saveState()

        current_style = {}
        if len(styles):
            current_style.update(styles[-1])
        for declaration in element.get("style", "").split(";"):
            if declaration == "":
                continue
            key, value = declaration.split(":")
            current_style[key] = value
        styles.append(current_style)

        if "stroke-width" in current_style:
            canvas.setLineWidth(float(current_style["stroke-width"]))

        if "stroke-dasharray" in current_style:
            canvas.setDash([
                float(length)
                for length in current_style["stroke-dasharray"].split(",")
            ])

        if current_style.get("visibility") != "hidden":

            if "transform" in element.attrib:
                for transformation in element.get("transform").split(")")[::1]:
                    if transformation:
                        transform, arguments = transformation.split("(")
                        arguments = arguments.split(",")
                        if transform.strip() == "translate":
                            if len(arguments) == 2:
                                canvas.translate(float(arguments[0]),
                                                 float(arguments[1]))
                        elif transform.strip() == "rotate":
                            if len(arguments) == 1:
                                canvas.rotate(float(arguments[0]))
                            if len(arguments) == 3:
                                canvas.translate(float(arguments[1]),
                                                 float(arguments[2]))
                                canvas.rotate(float(arguments[0]))
                                canvas.translate(-float(arguments[1]),
                                                 -float(arguments[2]))

            if element.tag == "svg":
                if "background-color" in current_style:
                    set_fill_color(
                        canvas,
                        toyplot.color.css(current_style["background-color"]))
                    canvas.rect(
                        0,
                        0,
                        float(element.get("width")[:-2]),
                        float(element.get("height")[:-2]),
                        stroke=0,
                        fill=1,
                    )
                for child in element:
                    render_element(root, child, canvas, styles)

            elif element.tag == "g":
                if element.get("clip-path", None) is not None:
                    clip_id = element.get("clip-path")[5:-1]
                    clip_path = root.find(".//*[@id='%s']" % clip_id)
                    for child in clip_path:
                        if child.tag == "rect":
                            x = float(child.get("x"))
                            y = float(child.get("y"))
                            width = float(child.get("width"))
                            height = float(child.get("height"))
                            path = canvas.beginPath()
                            path.moveTo(x, y)
                            path.lineTo(x + width, y)
                            path.lineTo(x + width, y + height)
                            path.lineTo(x, y + height)
                            path.close()
                            canvas.clipPath(path, stroke=0, fill=1)
                        else:
                            toyplot.log.error("Unhandled clip tag: %s",
                                              child.tag)  # pragma: no cover

                for child in element:
                    render_element(root, child, canvas, styles)

            elif element.tag == "clipPath":
                pass

            elif element.tag == "line":
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)
                    canvas.setLineCap(get_line_cap(current_style))
                    canvas.line(
                        float(element.get("x1", 0)),
                        float(element.get("y1", 0)),
                        float(element.get("x2", 0)),
                        float(element.get("y2", 0)),
                    )
            elif element.tag == "path":
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)
                    canvas.setLineCap(get_line_cap(current_style))
                    path = canvas.beginPath()
                    commands = element.get("d").split()
                    while len(commands):
                        command = commands.pop(0)
                        if command == "L":
                            path.lineTo(float(commands.pop(0)),
                                        float(commands.pop(0)))
                        elif command == "M":
                            path.moveTo(float(commands.pop(0)),
                                        float(commands.pop(0)))
                    canvas.drawPath(path)
            elif element.tag == "polygon":
                fill, fill_gradient = get_fill(root, current_style)
                if fill_gradient is not None:
                    raise NotImplementedError(
                        "Gradient <polygon> not implemented."
                    )  # pragma: no cover
                if fill is not None:
                    set_fill_color(canvas, fill)
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)

                points = [
                    point.split(",")
                    for point in element.get("points").split()
                ]
                path = canvas.beginPath()
                for point in points[:1]:
                    path.moveTo(float(point[0]), float(point[1]))
                for point in points[1:]:
                    path.lineTo(float(point[0]), float(point[1]))
                path.close()
                canvas.drawPath(path,
                                stroke=stroke is not None,
                                fill=fill is not None)
            elif element.tag == "rect":
                fill, fill_gradient = get_fill(root, current_style)
                if fill is not None:
                    set_fill_color(canvas, fill)
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)

                x = float(element.get("x", 0))
                y = float(element.get("y", 0))
                width = float(element.get("width"))
                height = float(element.get("height"))

                path = canvas.beginPath()
                path.moveTo(x, y)
                path.lineTo(x + width, y)
                path.lineTo(x + width, y + height)
                path.lineTo(x, y + height)
                path.close()

                if fill_gradient is not None:
                    pdf_colors = []
                    pdf_offsets = []
                    for stop in fill_gradient:
                        offset = float(stop.get("offset"))
                        color = toyplot.color.css(stop.get("stop-color"))
                        opacity = float(stop.get("stop-opacity"))
                        pdf_colors.append(
                            reportlab.lib.colors.Color(color["r"], color["g"],
                                                       color["b"],
                                                       color["a"] * opacity))
                        pdf_offsets.append(offset)
                    canvas.saveState()
                    canvas.clipPath(path, stroke=0, fill=1)
                    canvas.setFillAlpha(1)
                    canvas.linearGradient(
                        float(fill_gradient.get("x1")),
                        float(fill_gradient.get("y1")),
                        float(fill_gradient.get("x2")),
                        float(fill_gradient.get("y2")),
                        pdf_colors,
                        pdf_offsets,
                    )
                    canvas.restoreState()

                canvas.drawPath(path,
                                stroke=stroke is not None,
                                fill=fill is not None)
            elif element.tag == "circle":
                fill, fill_gradient = get_fill(root, current_style)
                if fill_gradient is not None:
                    raise NotImplementedError(
                        "Gradient <circle> not implemented."
                    )  # pragma: no cover
                if fill is not None:
                    set_fill_color(canvas, fill)
                stroke = get_stroke(current_style)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)

                cx = float(element.get("cx", 0))
                cy = float(element.get("cy", 0))
                r = float(element.get("r"))
                canvas.circle(cx,
                              cy,
                              r,
                              stroke=stroke is not None,
                              fill=fill is not None)
            elif element.tag == "text":
                x = float(element.get("x", 0))
                y = float(element.get("y", 0))
                fill, fill_gradient = get_fill(element, current_style)
                stroke = get_stroke(current_style)
                font_family = get_font_family(current_style)
                font_size = toyplot.units.convert(current_style["font-size"],
                                                  target="px")
                text = element.text

                canvas.saveState()
                canvas.setFont(font_family, font_size)
                if fill is not None:
                    set_fill_color(canvas, fill)
                if stroke is not None:
                    set_stroke_color(canvas, stroke)
                canvas.translate(x, y)
                canvas.scale(1, -1)
                canvas.drawString(0, 0, text)
                canvas.restoreState()

            elif element.tag == "image":
                # pylint: disable=redefined-variable-type

                import PIL.Image
                image = element.get("xlink:href")
                if not image.startswith("data:image/png;base64,"):
                    raise ValueError(
                        "Unsupported image type.")  # pragma: no cover
                image = base64.standard_b64decode(image[22:])
                image = io.BytesIO(image)
                image = PIL.Image.open(image)
                image = reportlab.lib.utils.ImageReader(image)

                x = float(element.get("x", 0))
                y = float(element.get("y", 0))
                width = float(element.get("width"))
                height = float(element.get("height"))

                canvas.saveState()
                path = canvas.beginPath()
                set_fill_color(canvas, toyplot.color.rgb(1, 1, 1))
                canvas.rect(x, y, width, height, stroke=0, fill=1)
                canvas.translate(x, y + height)
                canvas.scale(1, -1)
                canvas.drawImage(image=image,
                                 x=0,
                                 y=0,
                                 width=width,
                                 height=height,
                                 mask=None)
                canvas.restoreState()

            elif element.tag in ["defs", "title"]:
                pass

            else:
                raise Exception("unhandled tag: %s" %
                                element.tag)  # pragma: no cover

        styles.pop()
        canvas.restoreState()