Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions trml2pdf/canv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand Down