···
super().__init__(x=x, y=-y-height, width=width, height=height,
xlink__href=uri, **kwargs)
353
-
class Text(DrawingBasicElement):
353
+
class Text(DrawingParentElement):
Additional keyword arguments are output as additional arguments to the
SVG node e.g. fill="red", font_size=20, text_anchor="middle". '''
360
-
def __init__(self, text, fontSize, x, y, center=False, **kwargs):
360
+
def __init__(self, text, fontSize, x, y, center=False, lineHeight=1,
362
+
singleLine = isinstance(text, str)
365
+
numLines = len(text)
if 'text_anchor' not in kwargs:
kwargs['text_anchor'] = 'middle'
372
+
centerOffset = fontSize*0.5*center
374
+
emOffset = 0.4 - lineHeight * (numLines - 1) / 2
fontSize = float(fontSize)
366
-
translate = 'translate(0,{})'.format(fontSize*0.5*center)
378
+
translate = 'translate(0,{})'.format(centerOffset)
if 'transform' in kwargs:
kwargs['transform'] = translate + ' ' + kwargs['transform']
···
super().__init__(x=x, y=-y, font_size=fontSize, **kwargs)
387
+
self.escapedText = xml.escape(text)
389
+
self.escapedText = ''
390
+
# Text is an iterable
391
+
for i, line in enumerate(text):
392
+
dy = '{}em'.format(emOffset if i == 0 else lineHeight)
393
+
self.appendLine(line, x=x, dy=dy)
394
+
def writeContent(self, idGen, isDuplicate, outputFile, dryRun):
397
+
outputFile.write(self.escapedText)
398
+
def writeChildrenContent(self, idGen, isDuplicate, outputFile, dryRun):
399
+
''' Override in a subclass to add data between the start and end
400
+
tags. This will not be called if hasContent is False. '''
402
+
for child in self.children:
403
+
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
405
+
for child in self.children:
406
+
child.writeSvgElement(idGen, isDuplicate, outputFile, dryRun)
407
+
def appendLine(self, line, **kwargs):
408
+
self.append(TSpan(line, **kwargs))
410
+
class TSpan(DrawingBasicElement):
411
+
''' A line of text within the Text element. '''
414
+
def __init__(self, text, **kwargs):
415
+
super().__init__(**kwargs)
self.escapedText = xml.escape(text)
def writeContent(self, idGen, isDuplicate, outputFile, dryRun):