Programmatically generate SVG (vector) images, animations, and interactive Jupyter widgets
1 2from .elements import DrawingElement, DrawingParentElement 3 4 5class DrawingDef(DrawingParentElement): 6 ''' Parent class of SVG nodes that must be direct children of <defs> ''' 7 @property 8 def id(self): 9 return self.args.get('id', None) 10 @id.setter 11 def id(self, newId): 12 self.args['id'] = newId 13 def getSvgDefs(self): 14 return (self,) 15 def writeSvgDefs(self, idGen, isDuplicate, outputFile): 16 DrawingElement.writeSvgDefs(idGen, isDuplicate, outputFile) 17 18class DrawingDefSub(DrawingParentElement): 19 ''' Parent class of SVG nodes that are meant to be descendants of a Def ''' 20 pass 21 22class LinearGradient(DrawingDef): 23 ''' A linear gradient to use as a fill or other color 24 25 Has <stop> nodes as children. ''' 26 TAG_NAME = 'linearGradient' 27 def __init__(self, x1, y1, x2, y2, gradientUnits='userSpaceOnUse', **kwargs): 28 yShift = 0 29 if gradientUnits != 'userSpaceOnUse': 30 yShift = 1 31 try: y1 = yShift - y1 32 except TypeError: pass 33 try: y2 = yShift - y2 34 except TypeError: pass 35 super().__init__(x1=x1, y1=y1, x2=x2, y2=y2, gradientUnits=gradientUnits, 36 **kwargs) 37 def addStop(self, offset, color, opacity=None, **kwargs): 38 stop = GradientStop(offset=offset, stop_color=color, 39 stop_opacity=opacity, **kwargs) 40 self.append(stop) 41 42class RadialGradient(DrawingDef): 43 ''' A radial gradient to use as a fill or other color 44 45 Has <stop> nodes as children. ''' 46 TAG_NAME = 'radialGradient' 47 def __init__(self, cx, cy, r, gradientUnits='userSpaceOnUse', fy=None, **kwargs): 48 yShift = 0 49 if gradientUnits != 'userSpaceOnUse': 50 yShift = 1 51 try: cy = yShift - cy 52 except TypeError: pass 53 try: fy = yShift - fy 54 except TypeError: pass 55 super().__init__(cx=cx, cy=cy, r=r, gradientUnits=gradientUnits, 56 fy=fy, **kwargs) 57 def addStop(self, offset, color, opacity=None, **kwargs): 58 stop = GradientStop(offset=offset, stop_color=color, 59 stop_opacity=opacity, **kwargs) 60 self.append(stop) 61 62class GradientStop(DrawingDefSub): 63 ''' A control point for a radial or linear gradient ''' 64 TAG_NAME = 'stop' 65 hasContent = False 66