From dcad0b836c912e8716e73cd077a0bca30df0035e Mon Sep 17 00:00:00 2001 From: Darryl Ross Date: Mon, 22 Aug 2022 21:08:56 +1000 Subject: [PATCH] Create drawWrappedString tag --- trml2pdf/canv.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/trml2pdf/canv.py b/trml2pdf/canv.py index b9b3664..9d4839c 100644 --- a/trml2pdf/canv.py +++ b/trml2pdf/canv.py @@ -67,6 +67,31 @@ def _drawRightString(self, node): self.canvas.drawRightString( text=self._textual(node), **utils.attr_get(node, ['x', 'y'])) + def _drawWrappedString(self, node): + text = self._textual(node) + if isinstance(text, bytes): + text = text.decode() + x = utils.unit_get(node.getAttribute('x')) + y = utils.unit_get(node.getAttribute('y')) + max_width = utils.unit_get(node.getAttribute('maxWidth')) + max_lines = int(node.getAttribute('maxLines')) + current_line = 1 + while current_line <= max_lines: + tmp = text + width = self.canvas.stringWidth(tmp, self.canvas._fontname, self.canvas._fontsize) + while width > max_width: + # Chop off a word and try again + tmp = ' '.join(tmp.split()[:-1]) + width = self.canvas.stringWidth(tmp, self.canvas._fontname, self.canvas._fontsize) + self.canvas.drawString(text=tmp, x=x, y=y) + # Remove the text we've just drawn from our string + text = ' '.join(text.split()[len(tmp.split()):]) + # If we've run out of text, no more lines to draw + if not text: + break + y -= (self.canvas._fontsize * 1.2) # Line space at 1.2 times font size + current_line += 1 + def _rect(self, node): if node.hasAttribute('round'): self.canvas.roundRect(radius=utils.unit_get(node.getAttribute( @@ -319,6 +344,7 @@ def render(self, node): 'drawCenteredString': self._drawCenteredString, 'drawRightString': self._drawRightString, 'drawString': self._drawString, + 'drawWrappedString': self._drawWrappedString, 'rect': self._rect, 'ellipse': self._ellipse, 'lines': self._lines,