···
6
+
import xml.sax.saxutils as xml
# TODO: Support drawing ellipses without manually using Path
···
''' Base class for SVG drawing elements that are a single node with no
def __init__(self, **args):
def writeSvgElement(self, outputFile):
···
outputFile.write('{}="{}" '.format(k,v))
33
-
outputFile.write('/>')
35
+
if not self.hasContent:
36
+
outputFile.write('/>')
38
+
outputFile.write('>')
39
+
self.writeContent(outputFile)
40
+
outputFile.write('</')
41
+
outputFile.write(self.TAG_NAME)
42
+
outputFile.write('>')
43
+
def writeContent(self, outputFile):
44
+
''' Override in a subclass to add data between the start and end
45
+
tags. This will not be called if hasContent is False. '''
46
+
raise RuntimeError('This element has no content')
if isinstance(other, type(self)):
return (self.tagName == other.tagName and
···
encData = base64.b64encode(data).decode()
uri = 'data:{};base64,{}'.format(mimeType, encData)
super().__init__(x=x, y=-y-height, width=width, height=height,
106
+
xlink__href=uri, **kwargs)
108
+
class Text(DrawingBasicElement):
111
+
Additional keyword arguments are output as additional arguments to the
112
+
SVG node e.g. fill="red", font_size=20, text_anchor="middle". '''
115
+
def __init__(self, text, fontSize, x, y, center=False, **kwargs):
117
+
if 'text_anchor' not in kwargs:
118
+
kwargs['text_anchor'] = 'middle'
120
+
fontSize = float(fontSize)
121
+
translate = 'translate(0,{})'.format(fontSize*0.5*center)
122
+
if 'transform' in kwargs:
123
+
kwargs['transform'] = translate + ' ' + kwargs['transform']
125
+
kwargs['transform'] = translate
128
+
super().__init__(x=x, y=-y, font_size=fontSize, **kwargs)
129
+
self.escapedText = xml.escape(text)
130
+
def writeContent(self, outputFile):
131
+
outputFile.write(self.escapedText)
class Rectangle(DrawingBasicElement):