#Copyright ReportLab Europe Ltd. 2000-2017 #see license.txt for license details #history https://hg.reportlab.com/hg-public/reportlab/log/tip/src/reportlab/graphics/widgets/signsandsymbols.py # signsandsymbols.py # A collection of new widgets # author: John Precedo (johnp@reportlab.com) __version__='3.3.0' __doc__="""This file is a collection of widgets to produce some common signs and symbols. Widgets include: - ETriangle (an equilateral triangle), - RTriangle (a right angled triangle), - Octagon, - Crossbox, - Tickbox, - SmileyFace, - StopSign, - NoEntry, - NotAllowed (the red roundel from 'no smoking' signs), - NoSmoking, - DangerSign (a black exclamation point in a yellow triangle), - YesNo (returns a tickbox or a crossbox depending on a testvalue), - FloppyDisk, - ArrowOne, and - ArrowTwo - CrossHair """ from reportlab.lib import colors from reportlab.lib.validators import * from reportlab.lib.attrmap import * from reportlab.lib.utils import isStr, asUnicode from reportlab.graphics import shapes from reportlab.graphics.widgetbase import Widget from reportlab.graphics import renderPDF class _Symbol(Widget): """Abstract base widget possible attributes: 'x', 'y', 'size', 'fillColor', 'strokeColor' """ _nodoc = 1 _attrMap = AttrMap( x = AttrMapValue(isNumber,desc='symbol x coordinate'), y = AttrMapValue(isNumber,desc='symbol y coordinate'), dx = AttrMapValue(isNumber,desc='symbol x coordinate adjustment'), dy = AttrMapValue(isNumber,desc='symbol x coordinate adjustment'), size = AttrMapValue(isNumber), fillColor = AttrMapValue(isColorOrNone), strokeColor = AttrMapValue(isColorOrNone), strokeWidth = AttrMapValue(isNumber), ) def __init__(self): assert self.__class__.__name__!='_Symbol', 'Abstract class _Symbol instantiated' self.x = self.y = self.dx = self.dy = 0 self.size = 100 self.fillColor = colors.red self.strokeColor = None self.strokeWidth = 0.1 def demo(self): D = shapes.Drawing(200, 100) s = float(self.size) ob = self.__class__() ob.x=50 ob.y=0 ob.draw() D.add(ob) D.add(shapes.String(ob.x+(s/2),(ob.y-12), ob.__class__.__name__, fillColor=colors.black, textAnchor='middle', fontSize=10)) return D class ETriangle(_Symbol): """This draws an equilateral triangle.""" def __init__(self): _Symbol.__init__(self) def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # Triangle specific bits ae = s*0.125 #(ae = 'an eighth') triangle = shapes.Polygon(points = [ self.x, self.y, self.x+s, self.y, self.x+(s/2),self.y+s], fillColor = self.fillColor, strokeColor = self.strokeColor, strokeWidth=s/50.) g.add(triangle) return g class RTriangle(_Symbol): """This draws a right-angled triangle. possible attributes: 'x', 'y', 'size', 'fillColor', 'strokeColor' """ def __init__(self): self.x = 0 self.y = 0 self.size = 100 self.fillColor = colors.green self.strokeColor = None def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # Triangle specific bits ae = s*0.125 #(ae = 'an eighth') triangle = shapes.Polygon(points = [ self.x, self.y, self.x+s, self.y, self.x,self.y+s], fillColor = self.fillColor, strokeColor = self.strokeColor, strokeWidth=s/50.) g.add(triangle) return g class Octagon(_Symbol): """This widget draws an Octagon. possible attributes: 'x', 'y', 'size', 'fillColor', 'strokeColor' """ def __init__(self): self.x = 0 self.y = 0 self.size = 100 self.fillColor = colors.yellow self.strokeColor = None def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # Octagon specific bits athird=s/3 octagon = shapes.Polygon(points=[self.x+athird, self.y, self.x, self.y+athird, self.x, self.y+(athird*2), self.x+athird, self.y+s, self.x+(athird*2), self.y+s, self.x+s, self.y+(athird*2), self.x+s, self.y+athird, self.x+(athird*2), self.y], strokeColor = self.strokeColor, fillColor = self.fillColor, strokeWidth=10) g.add(octagon) return g class Crossbox(_Symbol): """This draws a black box with a red cross in it - a 'checkbox'. possible attributes: 'x', 'y', 'size', 'crossColor', 'strokeColor', 'crosswidth' """ _attrMap = AttrMap(BASE=_Symbol, crossColor = AttrMapValue(isColorOrNone), crosswidth = AttrMapValue(isNumber), ) def __init__(self): self.x = 0 self.y = 0 self.size = 100 self.fillColor = colors.white self.crossColor = colors.red self.strokeColor = colors.black self.crosswidth = 10 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 class Tickbox(_Symbol): """This draws a black box with a red tick in it - another 'checkbox'. possible attributes: 'x', 'y', 'size', 'tickColor', 'strokeColor', 'tickwidth' """ _attrMap = AttrMap(BASE=_Symbol, tickColor = AttrMapValue(isColorOrNone), tickwidth = AttrMapValue(isNumber), ) def __init__(self): self.x = 0 self.y = 0 self.size = 100 self.tickColor = colors.red self.strokeColor = colors.black self.fillColor = colors.white self.tickwidth = 10 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 class SmileyFace(_Symbol): """This draws a classic smiley face. possible attributes: 'x', 'y', 'size', 'fillColor' """ def __init__(self): _Symbol.__init__(self) self.x = 0 self.y = 0 self.size = 100 self.fillColor = colors.yellow self.strokeColor = colors.black def draw(self): # general widget bits s = float(self.size) # abbreviate as we will use this a lot g = shapes.Group() # SmileyFace specific bits g.add(shapes.Circle(cx=self.x+(s/2), cy=self.y+(s/2), r=s/2, fillColor=self.fillColor, strokeColor=self.strokeColor, strokeWidth=max(s/38.,self.strokeWidth))) for i in (1,2): g.add(shapes.Ellipse(self.x+(s/3)*i,self.y+(s/3)*2, s/30, s/10, fillColor=self.strokeColor, strokeColor = self.strokeColor, strokeWidth=max(s/38.,self.strokeWidth))) # calculate a pointslist for the mouth # THIS IS A HACK! - don't use if there is a 'shapes.Arc' centerx=self.x+(s/2) centery=self.y+(s/2) radius=s/3 yradius = radius xradius = radius startangledegrees=200 endangledegrees=340 degreedelta = 1 pointslist = [] a = pointslist.append from math import sin, cos, pi degreestoradians = pi/180.0 radiansdelta = degreedelta*degreestoradians startangle = startangledegrees*degreestoradians endangle = endangledegrees*degreestoradians while endangle