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
10class DrawingDefSub(DrawingParentElement):
11 ''' Parent class of SVG nodes that are meant to be descendants of a Def '''
12 pass
13
14class LinearGradient(DrawingDef):
15 ''' A linear gradient to use as a fill or other color
16
17 Has <stop> nodes as children. '''
18 TAG_NAME = 'linearGradient'
19 def __init__(self, x1, y1, x2, y2, gradientUnits='userSpaceOnUse', **kwargs):
20 yShift = 0
21 if gradientUnits != 'userSpaceOnUse':
22 yShift = 1
23 try: y1 = yShift - y1
24 except TypeError: pass
25 try: y2 = yShift - y2
26 except TypeError: pass
27 super().__init__(x1=x1, y1=y1, x2=x2, y2=y2, gradientUnits=gradientUnits,
28 **kwargs)
29 def addStop(self, offset, color, opacity=None, **kwargs):
30 stop = GradientStop(offset=offset, stop_color=color,
31 stop_opacity=opacity, **kwargs)
32 self.append(stop)
33
34class RadialGradient(DrawingDef):
35 ''' A radial gradient to use as a fill or other color
36
37 Has <stop> nodes as children. '''
38 TAG_NAME = 'radialGradient'
39 def __init__(self, cx, cy, r, gradientUnits='userSpaceOnUse', fy=None, **kwargs):
40 yShift = 0
41 if gradientUnits != 'userSpaceOnUse':
42 yShift = 1
43 try: cy = yShift - cy
44 except TypeError: pass
45 try: fy = yShift - fy
46 except TypeError: pass
47 super().__init__(cx=cx, cy=cy, r=r, gradientUnits=gradientUnits,
48 fy=fy, **kwargs)
49 def addStop(self, offset, color, opacity=None, **kwargs):
50 stop = GradientStop(offset=offset, stop_color=color,
51 stop_opacity=opacity, **kwargs)
52 self.append(stop)
53
54class GradientStop(DrawingDefSub):
55 ''' A control point for a radial or linear gradient '''
56 TAG_NAME = 'stop'
57 hasContent = False
58
59class ClipPath(DrawingDef):
60 ''' A shape used to crop another element by not drawing outside of this
61 shape
62
63 Has regular drawing elements as children. '''
64 TAG_NAME = 'clipPath'
65
66class Filter(DrawingDef):
67 ''' A filter to apply to geometry
68
69 For example a blur filter. '''
70 TAG_NAME = 'filter'
71
72class FilterItem(DrawingDefSub):
73 ''' A child of Filter with any tag name'''
74 def __init__(self, tag_name, **args):
75 super().__init__(**args)
76 self.TAG_NAME = tag_name
77
78class Marker(DrawingDef):
79 ''' A small drawing that can be placed at the ends of (or along) a path.
80
81 This can be used for arrow heads or points on a graph for example.
82
83 By default, units are multiples of stroke width.'''
84 TAG_NAME = 'marker'
85 def __init__(self, minx, miny, maxx, maxy, scale=1, orient='auto',
86 **kwargs):
87 width = maxx - minx
88 height = maxy - miny
89 kwargs = {
90 'markerWidth': width if scale == 1 else float(width) * scale,
91 'markerHeight': height if scale == 1 else float(height) * scale,
92 'viewBox': '{} {} {} {}'.format(minx, -maxy, width, height),
93 'orient': orient,
94 **kwargs,
95 }
96 super().__init__(**kwargs)