def process(self): kw = dict(self.getAttributeValues()) canvas = attr.getManager(self, interfaces.ICanvasManager).canvas if 'width' in kw: canvas.setLineWidth(kw['width']) if 'join' in kw: canvas.setLineJoin(kw['join']) if 'cap' in kw: canvas.setLineCap(kw['cap']) if 'miterLimit' in kw: canvas.setMiterLimit(kw['miterLimit']) if 'dash' in kw: canvas.setDash(kw['dash'])
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 generate_list(list_id, names, pre_cross_dict={}, title="Coffee list", canvas=None): """ Generate a PDF for a coffee list Parameters: list_id: A (preferably unique) ID for this list. Will be embedded as a QR code into the URL names: A list of names for this list pre_cross_dict: A dictionary mapping names to a number of crosses to pre-draw onto the list title: A heading for the list. Could e.g. include a date. canvas: If set, draw to this canvas. Returns: A StringIO instance with the PDF file, or None if canvas is given. """ assert len(names) <= COFFEE_COUNT_PER_PAGE # Prepare QR code qr_code = tempfile.NamedTemporaryFile(suffix=".png") qr_data = "%s?id=%d" % (COFFEE_HOMEPAGE, list_id) qrcode.make(qr_data, border=0).save(qr_code.name) # Start page, prepare units had_canvas = canvas is not None if not had_canvas: outfile = StringIO.StringIO() canvas = reportlab.pdfgen.canvas.Canvas(outfile, pagesize=COFFEE_SHEET_PAGE_FORMAT) width, height = COFFEE_SHEET_PAGE_FORMAT cm_unit = reportlab.lib.units.cm qr_size = 2 * cm_unit canvas.translate(1.5 * cm_unit, 1.5 * cm_unit) width -= 3 * cm_unit height -= 3 * cm_unit # Draw orientation markers path = canvas.beginPath() path.moveTo(cm_unit, height) path.lineTo(0, height) path.lineTo(0, height - cm_unit) canvas.setLineWidth(5) canvas.setLineJoin(0) canvas.drawPath(path) path = canvas.beginPath() path.moveTo(width, height - cm_unit) path.lineTo(width, height) path.lineTo(width - cm_unit, height) canvas.drawPath(path) path = canvas.beginPath() path.moveTo(width, cm_unit) path.lineTo(width, 0) path.lineTo(width - cm_unit, 0) canvas.drawPath(path) path = canvas.beginPath() path.moveTo(0, cm_unit) path.lineTo(0, 0) path.lineTo(cm_unit, 0) canvas.drawPath(path) canvas.setLineWidth(1) # Draw title canvas.setFont("Helvetica", 16) canvas.drawString(.5 * cm_unit, height - 1 * cm_unit, title) # Draw the QR code and ID canvas.drawImage(qr_code.name, .5 * cm_unit, .5 * cm_unit, qr_size, qr_size) canvas.setFont("Helvetica", 8) canvas.drawString(.5 * cm_unit, .2 * cm_unit, "#%d" % list_id) # Draw bottom text canvas.setFont("Helvetica", 9) ypos = -.2 COFFEE_SHEET_BOTTOM_TEXT = getattr(config, "COFFEE_SHEET_BOTTOM_TEXT", "") for text in COFFEE_SHEET_BOTTOM_TEXT.split("\n"): text = text.strip() canvas.drawString(qr_size + 1. * cm_unit, qr_size - ypos * cm_unit, text) ypos += .5 # Draw grid grid_y = height - 2*cm_unit canvas.line(0, grid_y, width, grid_y) for name in names: new_y = grid_y - COFFEE_LINE_HEIGHT * cm_unit canvas.line(0, grid_y, 0, new_y) canvas.line(width, grid_y, width, new_y) box_start = COFFEE_NAME_FIELD_WIDTH * cm_unit box_width = (width - box_start) / COFFEE_BOXES_PER_LINE pre_draw_crosses = pre_cross_dict.get(name, 0) for i in range(int((width - box_start) / box_width)): canvas.line(box_start, grid_y, box_start, new_y) if pre_draw_crosses > 0: pre_draw_crosses -= 1 cross_margin = 2 canvas.line(box_start + cross_margin, grid_y - cross_margin, box_start + box_width - cross_margin, new_y + cross_margin) canvas.line(box_start + cross_margin, new_y + cross_margin, box_start + box_width - cross_margin, grid_y - cross_margin) box_start += box_width canvas.drawString(.2 * cm_unit, grid_y - (COFFEE_LINE_HEIGHT - .1) * cm_unit, name) grid_y = new_y canvas.line(0, grid_y, width, grid_y) canvas.showPage() if not had_canvas: canvas.save() return outfile