···
+
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))
+
if not self.hasContent:
+
self.writeContent(outputFile)
+
outputFile.write(self.TAG_NAME)
+
def writeContent(self, outputFile):
+
''' Override in a subclass to add data between the start and end
+
tags. This will not be called if hasContent is False. '''
+
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,
+
xlink__href=uri, **kwargs)
+
class Text(DrawingBasicElement):
+
Additional keyword arguments are output as additional arguments to the
+
SVG node e.g. fill="red", font_size=20, text_anchor="middle". '''
+
def __init__(self, text, fontSize, x, y, center=False, **kwargs):
+
if 'text_anchor' not in kwargs:
+
kwargs['text_anchor'] = 'middle'
+
fontSize = float(fontSize)
+
translate = 'translate(0,{})'.format(fontSize*0.5*center)
+
if 'transform' in kwargs:
+
kwargs['transform'] = translate + ' ' + kwargs['transform']
+
kwargs['transform'] = translate
+
super().__init__(x=x, y=-y, font_size=fontSize, **kwargs)
+
self.escapedText = xml.escape(text)
+
def writeContent(self, outputFile):
+
outputFile.write(self.escapedText)
class Rectangle(DrawingBasicElement):