def get_png(self, e=None): with self.lock: d = self._create_drawing() self._render(d) rect = shapes.Rect(0, 0, d.width, d.height) rect.fillColor = colors.HexColor(0xeeeeee) rect.strokeWidth = 0 d.background = rect im = renderPM.drawToPIL(d, dpi=self.DPI) bg = PIL.Image.new("RGB", im.size, 0xffffff) diff = PIL.ImageChops.difference(im, bg) bbox = diff.getbbox() if bbox: # center bbox width = im.size[0] minx, miny, maxx, maxy = bbox width2 = width / 2 if width2 - minx < maxx - width2: minx = width - maxx # width2 - (maxx - width2) else: maxx = width - minx # width2 + (width2 - minx) im = im.crop((minx, miny, maxx, maxy)) imgdata = StringIO.StringIO() im.save(imgdata, 'PNG') return imgdata.getvalue()
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # crossbox specific bits box = shapes.Rect(self.x + 1, self.y + 1, s - 2, s - 2, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=2) g.add(box) crossLine1 = shapes.Line(self.x + (s * 0.15), self.y + (s * 0.15), self.x + (s * 0.85), self.y + (s * 0.85), fillColor=self.crossColor, strokeColor=self.crossColor, strokeWidth=self.crosswidth) g.add(crossLine1) crossLine2 = shapes.Line(self.x + (s * 0.15), self.y + (s * 0.85), self.x + (s * 0.85), self.y + (s * 0.15), fillColor=self.crossColor, strokeColor=self.crossColor, strokeWidth=self.crosswidth) g.add(crossLine2) return g
def draw(self): s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() box = shapes.Rect(self.x + 1, self.y + 1, s - 2, s - 2, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=0.6) g.add(box) s += self._oversize crossLine1 = shapes.Line(self.x + (s * 0.15) - self._oversize, self.y + (s * 0.15) - self._oversize, self.x + (s * 0.85), self.y + (s * 0.85), fillColor=self.crossColor, strokeColor=self.crossColor, strokeWidth=self.crosswidth) g.add(crossLine1) crossLine2 = shapes.Line(self.x + (s * 0.15) - self._oversize, self.y + (s * 0.85), self.x + (s * 0.85), self.y + (s * 0.15) - self._oversize, fillColor=self.crossColor, strokeColor=self.crossColor, strokeWidth=self.crosswidth) g.add(crossLine2) return g
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # tickbox specific bits box = shapes.Rect(self.x + 1, self.y + 1, s - 2, s - 2, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=2) g.add(box) tickLine = shapes.PolyLine(points=[ self.x + (s * 0.15), self.y + (s * 0.35), self.x + (s * 0.35), self.y + (s * 0.15), self.x + (s * 0.35), self.y + (s * 0.15), self.x + (s * 0.85), self.y + (s * 0.85) ], fillColor=self.tickColor, strokeColor=self.tickColor, strokeWidth=self.tickwidth) g.add(tickLine) return g
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # arrow specific bits body = shapes.Rect(x=self.x, y=(self.y + (s / 2)) - (s / 6), width=2 * (s / 3), height=(s / 3), fillColor=self.fillColor, strokeColor=None, strokeWidth=0) g.add(body) head = shapes.Polygon(points=[ self.x + (3 * (s / 6)), (self.y + (s / 2)), self.x + (3 * (s / 6)), self.y + 8 * (s / 10), self.x + s, self.y + (s / 2), self.x + (3 * (s / 6)), self.y + 2 * (s / 10) ], fillColor=self.fillColor, strokeColor=None, strokeWidth=0) g.add(head) return g
def draw_label(label, width, height, obj): # + ' <img src="images/air.png" valign="middle"/>', i = 90 for t in obj[0]: for r in t: label.add(shapes.String(10, i, str(r), fontName="Helvetica", fontSize=9)) p = Paragraph("lol", normalStyle) i = i - 10 i = i - 20 # i = 50 # for t in obj[1]: # label.add(shapes.String(10, i, str(t), fontName="Helvetica", fontSize=9)) # i = i - 10 i = 10 # for t in obj[2]: for t in obj[1]: label.add(shapes.String(10, i, '<b>' + str(t) + '</b>', fontName="Helvetica", fontSize=9)) # label.add(drawing) i = i - 10 s = shapes.Rect(5, 8, 150, 11, fill = True) s.fillColor = transparent label.add(s)
def draw(self): # general widget bits w = float(self.length) h = float(self.height) g = shapes.Group() body = shapes.Rect(x=self.x, y=self.y-h/2, width=w, height=h, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(body) if self.label: b = g.getBounds() s = Label() s.setText(self.label) s.setOrigin(self.x+0.5*w, self.y-h/2+b[3]-b[1]+4) s.boxAnchor = self.boxAnchor s.textAnchor = self.textAnchor s.fontName = 'Helvetica' s.fontSize = self.fontSize s.angle = self.labelAngle g.add(s) return g
def draw(self): """ returns a group of shapes """ g = shapes.Group() #overall border and fill if self.borderStrokeColor or self.fillColor: # adds border and filling color rect = shapes.Rect(self.x, self.y, self.width, self.height) rect.fillColor = self.fillColor rect.strokeColor = self.borderStrokeColor rect.strokeWidth = self.borderStrokeWidth g.add(rect) #special case - for an empty table we want to avoid divide-by-zero data = self.preProcessData(self.data) rows = len(self.data) cols = len(self.data[0]) #print "(rows,cols)=(%s, %s)"%(rows,cols) row_step = self.height / float(rows) col_step = self.width / float(cols) #print "(row_step,col_step)=(%s, %s)"%(row_step,col_step) # draw the grid if self.horizontalDividerStrokeColor: for i in range(rows): # make horizontal lines x1 = self.x x2 = self.x + self.width y = self.y + row_step*i #print 'line (%s, %s), (%s, %s)'%(x1, y, x2, y) line = shapes.Line(x1, y, x2, y) line.strokeDashArray = self.dividerDashArray line.strokeWidth = self.horizontalDividerStrokeWidth line.strokeColor = self.horizontalDividerStrokeColor g.add(line) if self.verticalDividerStrokeColor: for i in range(cols): # make vertical lines x = self.x+col_step*i y1 = self.y y2 = self.y + self.height #print 'line (%s, %s), (%s, %s)'%(x, y1, x, y2) line = shapes.Line(x, y1, x, y2) line.strokeDashArray = self.dividerDashArray line.strokeWidth = self.verticalDividerStrokeWidth line.strokeColor = self.verticalDividerStrokeColor g.add(line) # since we plot data from down up, we reverse the list self.data.reverse() for (j, row) in enumerate(self.data): y = self.y + j*row_step + 0.5*row_step - 0.5 * self.fontSize for (i, datum) in enumerate(row): if datum: x = self.x + i*col_step + 0.5*col_step s = shapes.String(x, y, str(datum), textAnchor=self.textAnchor) s.fontName = self.fontName s.fontSize = self.fontSize s.fillColor = self.fontColor g.add(s) return g
def draw(self): # general widget bits w = float(self.length) h = float(self.height) # print self.label,w,h # Set minimum size if abs(w)<self.wmin: xmid = self.x+0.5*w w = w/abs(w) * self.wmin self.x = xmid-0.5*w g = shapes.Group() if abs(w)>self.wNoTail: # arrow specific bits body = shapes.Rect(x=self.x, y=self.y-self.aspectRatio*h/2, width=2*(w/3), height=self.aspectRatio*h, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(body) head = shapes.Polygon( points=[self.x+w, self.y, self.x+2*(w/3), self.y+h/2, self.x+2*(w/3), self.y-h/2, self.x+w, self.y], fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(head) else: head = shapes.Polygon( points=[self.x+w, self.y, self.x, self.y+h/2, self.x, self.y-h/2, self.x+w, self.y], fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(head) if self.label: b = g.getBounds() s = Label() s.setText(self.label) s.setOrigin(self.x+0.5*w+self.labeldx, self.y-h/2+b[3]-b[1]+self.labeldy) s.boxAnchor = self.boxAnchor s.textAnchor = self.textAnchor s.fontName = 'Helvetica' s.fontSize = self.fontSize s.angle = self.labelAngle g.add(s) return g
def _draw(self, canvas, x, y): drawing = shapes.Drawing(self.width, self.height) drawing.rotate(self.rotation) rect = shapes.Rect(0, 0, self.width, self.height) rect.setProperties({'strokeWidth':self.strokeWidth, 'fillColor':self.fillColor, 'strokeColor':self.strokeColor, 'strokeDashArray':self.strokeDashArray, }) drawing.add(rect) drawing.drawOn(canvas, x, y)
def draw(self): g = shapes.Group() (x1, y1, x2, y2) = self.getBounds() r = shapes.Rect(x=x1, y=y1, width=x2 - x1, height=y2 - y1, fillColor=self.fillColor, strokeColor=self.strokeColor) g.add(r) for elem in self.contents: g.add(elem) return g
def draw_label(label, width, height, part): # The coordinate begins on lower left, use width and height together # with fontsize (although in points and not mm) to align the shapes. # In partkeepr I'm using a hierarchy exactly similar to # http://octopart.com/search, the lines here represent a part with # the longest (end node) in that hierarchy. You can use these as a # static test to see if the label is correct # label.add(shapes.String(5, height-5, "Clock Generators, PLLs, Frequency Synthesizers", fontName="Consolas Bold", fontSize=10)) # label.add(shapes.String(10, height-15, "MOSFETs", fontName="Consolas Bold", fontSize=10)) # label.add(shapes.String(10, height-45, "Lattice Semiconductor", fontName="Consolas Bold", fontSize=13)) # label.add(shapes.String(10, height-60, "LC4064V-75TN100C", fontName="Consolas Bold", fontSize=13)) # label.add(shapes.Rect(width-50, height-55, 40, 20, strokeWidth=1, fillColor=None)) # label.add(shapes.String(width-45, height-48, "#4444", fontName="Consolas Bold", fontSize=11)) # label.add(shapes.Line(10, height-75, width-10,height-75, strokeWidth=1)) label.add( shapes.String(10, height - 15, part["category"], fontName="Consolas Bold", fontSize=10)) label.add( shapes.String(10, height - 45, part["name"], fontName="Consolas Bold", fontSize=13)) label.add( shapes.String(10, height - 60, part["description"], fontName="Consolas Bold", fontSize=10)) label.add( shapes.Line(10, height - 75, width - 10, height - 75, strokeWidth=1)) label.add( shapes.Rect(width - 50, height - 43, 40, 20, strokeWidth=1, fillColor=None)) label.add( shapes.String(width - 46, height - 36, part["id36"], fontName="Consolas Bold", fontSize=10))
def _missing_label(self, label, width, height, obj=None): """Helper drawing callable to shade a missing label. Not intended for external use. """ # Sanity check. Should never be False if we get here but who knows. if not self.shade_missing: return # Shade a rectangle over the entire bounding box. The clipping path will # take care of any rounded corners. r = shapes.Rect(0, 0, width, height) r.fillColor = self.shade_missing r.strokeColor = None label.add(r)
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # no-entry-sign specific bits if self.strokeColor: g.add(shapes.Circle(cx = (self.x+(s/2)), cy = (self.y+(s/2)), r = s/2, fillColor = None, strokeColor = self.strokeColor, strokeWidth=1)) if self.fillColor: g.add(shapes.Circle(cx = (self.x+(s/2)), cy =(self.y+(s/2)), r = ((s/2)-(s/50)), fillColor = self.fillColor, strokeColor = None, strokeWidth=0)) innerBarColor = self.innerBarColor if innerBarColor: g.add(shapes.Rect(self.x+(s*0.1), self.y+(s*0.4), width=s*0.8, height=s*0.2, fillColor = innerBarColor, strokeColor = innerBarColor, strokeLineCap = 1, strokeWidth = 0)) return g
def _shade_missing_label(self): """Helper method to shade a missing label. Not intended for external use. """ # Start a drawing for the whole label. label = Drawing(float(self._lw), float(self._lh)) label.add(self._clip_label) # Fill with a rectangle; the clipping path will take care of the borders. r = shapes.Rect(0, 0, float(self._lw), float(self._lh)) r.fillColor = self.shade_missing r.strokeColor = None label.add(r) # Add the label to the page. label.shift(*self._calculate_edges()) self._current_page.add(label)
def draw_label(label, width, height, obj): # Pick a background colour. colour = random.choice(colours) # And a style. style = random.choice(('solid', 'stripes', 'hatch')) # Draw a solid background. if style == 'solid': r = shapes.Rect(0, 0, width, height) r.fillColor = colour r.strokeColor = None label.add(r) # Both stripes and hatches need vertical stripes. if style in ('stripes', 'hatch'): g = Grid() g.width = width g.height = height g.delta = width / 14.0 # The width of the stripes. g.delta0 = random.random() * (width / 14.0 ) # Offset of the start of the stripe. g.fillColor = None g.strokeColor = None g.orientation = 'vertical' g.stripeColors = (colors.HexColor(0xFFFFFF), colour) label.add(g) # Draw the horizontal stripes of any hatching. # Note the empty parts need to be 'coloured' transparent to avoid # hiding the vertical stripes. if style == 'hatch': g2 = Grid() g2.width = width g2.height = height g2.delta = height / 4.0 g2.delta0 = random.random() * (height / 4.0) g2.fillColor = None g2.strokeColor = None g2.orientation = 'horizontal' g2.stripeColors = (colors.transparent, colour) label.add(g2) # Print the label value as a string. label.add(shapes.String(2, 2, str(obj), fontName="Helvetica", fontSize=40))
def simple_label(): drawing = shapes.Drawing(width=400, height=200) drawing.add(shapes.Rect(200, 100, 10, 10, fillColor=colors.red)) x = 50 angle = 0 for item in range(3): label = Label() label.setOrigin(200, 100) label.boxAnchor = 'se' label.angle = angle #label.boxStrokeColor = colors.black label.setText('ReportLab label') drawing.add(label) x += 25 angle += 45 renderPDF.drawToFile(drawing, 'simple_label.pdf')
def draw(self): # general widget bits w = float(self.length) h = float(self.height) g = shapes.Group() block = shapes.Rect(x=self.x, y=self.y-h/2, width=self.length, height=h, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(block) point = shapes.Polygon( points=[self.x+w, self.y-h/2, self.x+w+signum(w)*h/4, self.y, self.x+w, self.y+h/2, self.x+w, self.y-h/2], fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=self.strokeWidth) g.add(point) return g
#tutorial 37 #create png #reportlab graphics sub package save to png #save to png #rectangle from reportlab.graphics import shapes, renderPM from reportlab.lib import colors drawing_obj = shapes.Drawing(700, 800) drawing_obj.add(shapes.Rect(20, 20, 300, 300, fillColor=colors.green)) renderPM.drawToFile(drawing_obj, "tutorial37.png", "PNG") #circle from reportlab.graphics import shapes, renderPM from reportlab.lib import colors drawing_obj = shapes.Drawing(700, 800) drawing_obj.add(shapes.Circle(150, 150, 75, fillColor=colors.green)) renderPM.drawToFile(drawing_obj, "tutorial37.png", "PNG")
def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # floppy disk specific bits diskBody = shapes.Rect(x=self.x, y=self.y + (s / 100), width=s, height=s - (s / 100), fillColor=self.diskColor, strokeColor=None, strokeWidth=0) g.add(diskBody) label = shapes.Rect(x=self.x + (s * 0.1), y=(self.y + s) - (s * 0.5), width=s * 0.8, height=s * 0.48, fillColor=colors.whitesmoke, strokeColor=None, strokeWidth=0) g.add(label) labelsplash = shapes.Rect(x=self.x + (s * 0.1), y=(self.y + s) - (s * 0.1), width=s * 0.8, height=s * 0.08, fillColor=colors.royalblue, strokeColor=None, strokeWidth=0) g.add(labelsplash) line1 = shapes.Line(x1=self.x + (s * 0.15), y1=self.y + (0.6 * s), x2=self.x + (s * 0.85), y2=self.y + (0.6 * s), fillColor=colors.black, strokeColor=colors.black, strokeWidth=0) g.add(line1) line2 = shapes.Line(x1=self.x + (s * 0.15), y1=self.y + (0.7 * s), x2=self.x + (s * 0.85), y2=self.y + (0.7 * s), fillColor=colors.black, strokeColor=colors.black, strokeWidth=0) g.add(line2) line3 = shapes.Line(x1=self.x + (s * 0.15), y1=self.y + (0.8 * s), x2=self.x + (s * 0.85), y2=self.y + (0.8 * s), fillColor=colors.black, strokeColor=colors.black, strokeWidth=0) g.add(line3) metalcover = shapes.Rect(x=self.x + (s * 0.2), y=(self.y), width=s * 0.5, height=s * 0.35, fillColor=colors.silver, strokeColor=None, strokeWidth=0) g.add(metalcover) coverslot = shapes.Rect(x=self.x + (s * 0.28), y=(self.y) + (s * 0.035), width=s * 0.12, height=s * 0.28, fillColor=self.diskColor, strokeColor=None, strokeWidth=0) g.add(coverslot) return g
def _draw_rectangle(self, X, Y, w, h, color=blue): r = shapes.Rect(X * mm, Y * mm, w * mm, h * mm) r.fillColor = None r.strokeColor = color r.strokeWidth = self.strokeWidth return r
def write_name(label, width, height, data): """ :param label: :param width: :param height: :param data: A dictionary containing fields like id, Entry Number brewStyle, Style brewCategory, Category brewSubCategory, Sub Category brewPaid, Paid brewReceived, Received brewBoxNum, Box Num brewTable Table :return: """ table_number = data['Table'].split(':')[0].lstrip('0') qrw = QrCodeWidget( data['prefixed_id'], barLevel='H', barWidth=46*mm, barHeight=46*mm) label.add(qrw) offset = 40 label.add(shapes.String(140, height-offset, "Entry: %s" % data['Entry Number'], fontSize=20)) offset += 18 label.add(shapes.String(140, height-offset, "Table: %s" % table_number, fontSize=16)) offset += 16 label.add(shapes.String(140, height-offset, "Category: %s%s" % (data['Category'], data['Sub Category']), fontSize=16)) offset += 14 label.add(shapes.String(140, height-offset, data['Style'], fontSize=10)) offset += 12 label.add(shapes.String(140, height-offset, " Final", fontSize=8)) label.add(shapes.Rect(180, height-(offset + 22), 60, 30, fillColor=None, strokeWidth=1)) offset += 10 label.add(shapes.String(140, height-offset, "Composite", fontSize=8)) offset += 10 label.add(shapes.String(140, height-offset, " Score", fontSize=8))
#tutorial 36 #create index #reportlab graphics sub package #RECTANGLE DRAWING from reportlab.graphics import shapes, renderPDF from reportlab.lib import colors drawing_obj = shapes.Drawing(500, 200) drawing_obj.add(shapes.Rect(10, 10, 250, 100, fillColor=colors.blue)) renderPDF.drawToFile(drawing_obj, "tutorial36.pdf", msg="tutorial36") #CIRCLE DRAWING from reportlab.graphics import shapes, renderPDF from reportlab.lib import colors drawing_obj = shapes.Drawing(500, 200) drawing_obj.add(shapes.Circle(50, 50, 50, fillColor=colors.blue)) renderPDF.drawToFile(drawing_obj, "tutorial36.pdf", msg="tutorial36")
def draw_oneBlock_label(self, label, width, height, obj): """ Split the label into 4 quads, calculate the maximum size that the main center label can be. """ topLeft = str(obj[0]) bottomLeft = str(obj[1]) topRight = str(obj[2]) bottomRight = str(obj[3]) middleCenter = str(obj[4]) bottomCenter = str(obj[5]) isBundle = obj[6] largeOrSmall = obj[7] font = self.font fillColor = colors.HexColor("#00000") if isBundle == True: if self.tagEnds == True: fillColor = colors.HexColor("#FFFFFF") r = shapes.Rect(0, 0, width, height) r.fillColor = colors.HexColor("#00000") r.strokeColor = None label.add(r) horizontalCenterLine = width / 2.0 verticalCenterLine = height / 2.0 if largeOrSmall == 'Large': largeFontSize = 28 smallFontSize = 8 if largeOrSmall == 'OneBlock': largeFontSize = 20 smallFontSize = 7 elif largeOrSmall == 'Small': largeFontSize = 22 smallFontSize = 7 topLeftText = shapes.String(5, height - smallFontSize, topLeft, fontName=font, fontSize=smallFontSize, fillColor=fillColor) label.add(topLeftText) bottomLeftText = shapes.String(2, height - height / 3.0 + smallFontSize, bottomLeft, fontName=font, fontSize=smallFontSize, fillColor=fillColor) label.add(bottomLeftText) topLeftTextWidth = stringWidth(topLeft, font, smallFontSize) topRightText = shapes.String(width - (topLeftTextWidth + 2), height - smallFontSize, topRight, fontName=font, fontSize=smallFontSize, fillColor=fillColor) label.add(topRightText) bottomLeftTextWidth = stringWidth(bottomLeft, font, smallFontSize) bottomRightText = shapes.String(width - (bottomLeftTextWidth + 2), height - height / 3.0, bottomRight, fontName=font, fontSize=smallFontSize, fillColor=fillColor) label.add(bottomRightText) middleTextWidth = stringWidth(middleCenter, font, largeFontSize) if len(middleCenter) > 4: topFour = middleCenter[0:4] bottomFour = middleCenter[4:8] if len(middleCenter) > 8: lowFour = middleCenter[8:] else: topFour = middleCenter bottomFour = '' lowFour = '' middleText = shapes.String(2, (height - height / 4.0) + 2.0, middleCenter, fontSize=largeFontSize, fillColor=fillColor) label.add(middleText)
def __call__(self, _x, _y, _size, _color): '''the symbol interface''' chart = self.chart() xA = getattr(chart, self.axes[0]) _xScale = getattr(xA, 'midScale', None) if not _xScale: _xScale = getattr(xA, 'scale') xScale = lambda x: _xScale(x) if x is not None else None yA = getattr(chart, self.axes[1]) _yScale = getattr(yA, 'midScale', None) if not _yScale: _yScale = getattr(yA, 'scale') yScale = lambda x: _yScale(x) if x is not None else None G = shapes.Group().add strokeWidth = self.strokeWidth strokeColor = self.strokeColor strokeDashArray = self.strokeDashArray crossWidth = self.crossWidth crossLo = yScale(self.crossLo) crossHi = yScale(self.crossHi) boxWidth = self.boxWidth boxFillColor = self.boxFillColor boxStrokeColor = NotSetOr.conditionalValue(self.boxStrokeColor, strokeColor) boxStrokeWidth = NotSetOr.conditionalValue(self.boxStrokeWidth, strokeWidth) boxStrokeDashArray = NotSetOr.conditionalValue(self.boxStrokeDashArray, strokeDashArray) boxLo = yScale(self.boxLo) boxMid = yScale(self.boxMid) boxHi = yScale(self.boxHi) position = xScale(self.position) candleKind = self.candleKind haveBox = None not in (boxWidth, boxLo, boxHi) haveLine = None not in (crossLo, crossHi) def aLine(x0, y0, x1, y1): if candleKind != 'vertical': x0, y0 = y0, x0 x1, y1 = y1, x1 G( shapes.Line(x0, y0, x1, y1, strokeWidth=strokeWidth, strokeColor=strokeColor, strokeDashArray=strokeDashArray)) if haveBox: boxLo, boxHi = min(boxLo, boxHi), max(boxLo, boxHi) if haveLine: crossLo, crossHi = min(crossLo, crossHi), max(crossLo, crossHi) if not haveBox or crossLo >= boxHi or crossHi <= boxLo: aLine(position, crossLo, position, crossHi) if crossWidth is not None: aLine(position - crossWidth * 0.5, crossLo, position + crossWidth * 0.5, crossLo) aLine(position - crossWidth * 0.5, crossHi, position + crossWidth * 0.5, crossHi) elif haveBox: if crossLo < boxLo: aLine(position, crossLo, position, boxLo) aLine(position - crossWidth * 0.5, crossLo, position + crossWidth * 0.5, crossLo) if crossHi > boxHi: aLine(position, boxHi, position, crossHi) aLine(position - crossWidth * 0.5, crossHi, position + crossWidth * 0.5, crossHi) if haveBox: x = position - boxWidth * 0.5 y = boxLo h = boxHi - boxLo w = boxWidth if candleKind != 'vertical': x, y, w, h = y, x, h, w G( shapes.Rect( x, y, w, h, strokeColor=boxStrokeColor if self.boxSides else None, strokeWidth=boxStrokeWidth, strokeDashArray=boxStrokeDashArray, fillColor=boxFillColor)) if not self.boxSides: aLine(position - 0.5 * boxWidth, boxHi, position + 0.5 * boxWidth, boxHi) aLine(position - 0.5 * boxWidth, boxLo, position + 0.5 * boxWidth, boxLo) if boxMid is not None: aLine(position - 0.5 * boxWidth, boxMid, position + 0.5 * boxWidth, boxMid) return G.__self__
def write_data(label, width, height, data): (num1, str1, str2, str3, str4) = get_labels_from_data(data) pad = 10 # section 1 : barcode D = Drawing(width, height) d = createBarcodeDrawing('Code128', value=num1, barHeight=0.4 * inch, humanReadable=True, quiet=False) #d = createBarcodeDrawing('I2of5', value=the_num, barHeight=10*mm, humanReadable=True) barcode_width = d.width barcode_height = d.height #d.rotate(-90) #d.translate( - barcode_height ,pad) # translate d.translate(width - barcode_width - pad / 2.0, 0) # translate #pprint(d.dumpProperties()) #D.add(d) #label.add(D) label.add(d) rect = shapes.Rect(0, pad, barcode_width + pad, barcode_height + pad) rect.fillColor = None rect.strokeColor = random.choice((colors.blue, colors.red, colors.green)) #rect.strokeWidth = d.borderStrokeWidth #label.add(rect) # section 2 : room number #the_text = "gr" + str(data['youngest_child_grade']) + " rm" + str(data['youngest_child_room']) #label.add(shapes.String(15, height-15, the_text, fontName="Judson Bold", fontSize=8, textAnchor="start")) # section2: family name # Measure the width of the name and shrink the font size until it fits. font_name = "Judson Bold" font_name = "PermanentMarker" # Measure the width of the name and shrink the font size until it fits. # try out 2 options and select the one that gives a taller font text_width_limit = width - barcode_width - pad text_height_limit = height / 2.0 s1 = fit_text_in_area(str1, font_name, text_width_limit, text_height_limit) text_width_limit = width - pad text_height_limit = height - barcode_height s2 = fit_text_in_area(str1, font_name, text_width_limit, text_height_limit) if (s1.fontSize >= s2.fontSize): s = s1 else: s = s2 s.x = pad / 2.0 s.y = height - s.fontSize + pad / 2.0 s.textAnchor = "start" label.add(s) family_name_height = get_font_height(s.fontSize, str1) family_name_width = stringWidth(str1, font_name, s.fontSize) # section3: parent names text_width_limit = width - barcode_width - 2 * pad text_height_limit = (height - family_name_height) / 2.0 font_name = "Judson Bold" s = fit_text_in_area(str2, font_name, text_width_limit, text_height_limit) s.x = pad / 2.0 s.y = height - family_name_height - s.fontSize + pad / 2.0 s.textAnchor = "start" label.add(s) parent_name_height = get_font_height(s.fontSize, str2) # section4: child's names text_width_limit = width - barcode_width - 2 * pad text_height_limit = height - family_name_height - parent_name_height font_name = "Judson Bold" s = fit_text_in_area(str3, font_name, text_width_limit, text_height_limit) s.x = pad / 2.0 s.y = height - family_name_height - parent_name_height - s.fontSize + pad / 2.0 s.textAnchor = "start" label.add(s) child_name_height = s.fontSize # section 4 : label number font_name = "Judson Bold" font_size = 5 s = shapes.String(width, height - font_size, str4, fontName=font_name, fontSize=font_size, textAnchor="end") #s.x = width #s.y = 0 #s.textAnchor = "start" #label.add(s) # section 5 : logo s = shapes.Image(0, 0, 25, 25, "logo.jpg") s.x = width - barcode_width - (barcode_width - 25) / 2.0 + 1 s.y = height - pad - 15 # enough space? if ((width - family_name_width - pad) > barcode_width): label.add(s)
def testRect(self): s = shapes.Rect(10, 20, 30, 40) #width, height assert s.getBounds() == (10, 20, 40, 60)
def draw_cable_label(self, label, width, height, obj): """ Split the label into 4 quads, calculate the maximum size that the main center label can be. """ topLeft = str(obj[0]) bottomLeft = str(obj[1]) topRight = str(obj[2]) bottomRight = str(obj[3]) middleCenter = str(obj[4]) bottomCenter = str(obj[5]) isBundle = obj[6] largeOrSmall = obj[7] font = self.font fillColor = colors.HexColor("#00000") if isBundle == True: if self.tagEnds == True: fillColor = colors.HexColor("#FFFFFF") r = shapes.Rect(0, 0, width, height) r.fillColor = colors.HexColor("#00000") r.strokeColor = None label.add(r) horizontalCenterLine = width / 2.0 verticalCenterLine = height / 2.0 if largeOrSmall == 'Large': largeFontSize = 32 smallFontSize = 10 elif largeOrSmall == 'Small': largeFontSize = 22 smallFontSize = 7 middleCenterTextWidth = stringWidth( middleCenter, font, largeFontSize) # start with large font size of 22 middleCenterTextHeight = largeFontSize * self.pointsToMM leftVerticalLimit = verticalCenterLine - middleCenterTextWidth / 2.0 rightVerticalLimit = verticalCenterLine + middleCenterTextWidth / 2.0 label.add( shapes.String(5, height - smallFontSize, topLeft, fontName=font, fontSize=smallFontSize, fillColor=fillColor)) label.add( shapes.String(2, 2, bottomLeft, fontName=font, fontSize=smallFontSize, fillColor=fillColor)) name_width = stringWidth(topRight, font, smallFontSize) label.add( shapes.String(width - (name_width + 2), height - smallFontSize, topRight, fontName=font, fontSize=smallFontSize, fillColor=fillColor)) name2_width = stringWidth(bottomRight, font, smallFontSize) label.add( shapes.String(width - (name2_width + 2), 2, bottomRight, fontName=font, fontSize=smallFontSize, fillColor=fillColor)) if largeOrSmall == 'Large': label.add( shapes.String(width / 2.0, height - (largeFontSize + largeFontSize / 2.0), middleCenter, textAnchor="middle", fontSize=largeFontSize, fillColor=fillColor)) elif largeOrSmall == 'Small': label.add( shapes.String(width / 2.0, height - (largeFontSize + largeFontSize / 5.0), middleCenter, textAnchor="middle", fontSize=largeFontSize, fillColor=fillColor))