Programmatically generate SVG (vector) images, animations, and interactive Jupyter widgets
at 1.0.1 2.4 kB view raw
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 def getSvgDefs(self): 8 return (self,) 9 def writeSvgDefs(self, idGen, isDuplicate, outputFile): 10 DrawingElement.writeSvgDefs(idGen, isDuplicate, outputFile) 11 12class DrawingDefSub(DrawingParentElement): 13 ''' Parent class of SVG nodes that are meant to be descendants of a Def ''' 14 pass 15 16class LinearGradient(DrawingDef): 17 ''' A linear gradient to use as a fill or other color 18 19 Has <stop> nodes as children. ''' 20 TAG_NAME = 'linearGradient' 21 def __init__(self, x1, y1, x2, y2, gradientUnits='userSpaceOnUse', **kwargs): 22 yShift = 0 23 if gradientUnits != 'userSpaceOnUse': 24 yShift = 1 25 try: y1 = yShift - y1 26 except TypeError: pass 27 try: y2 = yShift - y2 28 except TypeError: pass 29 super().__init__(x1=x1, y1=y1, x2=x2, y2=y2, gradientUnits=gradientUnits, 30 **kwargs) 31 def addStop(self, offset, color, opacity=None, **kwargs): 32 stop = GradientStop(offset=offset, stop_color=color, 33 stop_opacity=opacity, **kwargs) 34 self.append(stop) 35 36class RadialGradient(DrawingDef): 37 ''' A radial gradient to use as a fill or other color 38 39 Has <stop> nodes as children. ''' 40 TAG_NAME = 'radialGradient' 41 def __init__(self, cx, cy, r, gradientUnits='userSpaceOnUse', fy=None, **kwargs): 42 yShift = 0 43 if gradientUnits != 'userSpaceOnUse': 44 yShift = 1 45 try: cy = yShift - cy 46 except TypeError: pass 47 try: fy = yShift - fy 48 except TypeError: pass 49 super().__init__(cx=cx, cy=cy, r=r, gradientUnits=gradientUnits, 50 fy=fy, **kwargs) 51 def addStop(self, offset, color, opacity=None, **kwargs): 52 stop = GradientStop(offset=offset, stop_color=color, 53 stop_opacity=opacity, **kwargs) 54 self.append(stop) 55 56class GradientStop(DrawingDefSub): 57 ''' A control point for a radial or linear gradient ''' 58 TAG_NAME = 'stop' 59 hasContent = False 60 61class ClipPath(DrawingDef): 62 ''' A shape used to crop another element by not drawing outside of this 63 shape 64 65 Has regular drawing elements as children. ''' 66 TAG_NAME = 'clipPath' 67