def text(self, text, x, y, width=0, height=0, halign=HAlign.LEFT, valign=VAlign.TOP, word_wrap=False): """Draw multi-line text at (x, y) in millimeters and return new x.""" assert isinstance(halign, HAlign) assert isinstance(valign, VAlign) assert halign == HAlign.LEFT or width > 0, 'width must be specified' assert valign == VAlign.TOP or height > 0, 'height must be specified' assert not (width <= 0 and word_wrap), 'width must be specified' text_lines = self._wrap(text, width) if word_wrap else text.split('\n') text_height_pts = self.font.size_pts * len(text_lines) + \ self.font.interline_pts * (len(text_lines) - 1) x_pts = normalize(x) y_pts = normalize(self.height - y) - self.font.ascent_pts if height: y_pts -= valign.offset(text_height_pts, normalize(height)) obj = self._viewport.beginText(x_pts, y_pts) obj.setFont(self.font.name, self.font.size_pts, self.font.leading_pts) obj.setTextRenderMode(self.font.render_mode.value) obj.setCharSpace(self.font.char_space_pts) obj.setWordSpace(self.font.word_space_pts) obj.setRise(self.font.rise_pts) cursor_x = x_pts + self._get_text_width_pts(text_lines) for line in text_lines: if width: text_width_pts = self._get_text_width_pts(line) offset_pts = halign.offset(text_width_pts, normalize(width)) x_pts = normalize(x) + offset_pts cursor_x = max(cursor_x, x_pts + text_width_pts) obj.setTextOrigin(x_pts, obj.getY()) obj.textLine(line) self._viewport.drawText(obj) return denormalize(cursor_x)
def test_should_convert_points_to_millimeters(self): self.assertAlmostEqual(25.4, denormalize(72.0))
def height(self): """Return height of document page in millimeters.""" return denormalize(self._height)
def width(self): """Return width of document page in millimeters.""" return denormalize(self._width)
def get_text_width_mm(self, text): """Return the width of text in mm determined by the font.""" return denormalize(self._get_text_width_pts(text))
def miter_limit(self): """Return miter limit in millimeters.""" return denormalize(self._miter_limit)
def interline_mm(self): """Return interline height in millimeters.""" return denormalize(self.interline_pts)
def line_width(self): """Return line width in millimeters.""" return denormalize(self._line_width)
def leading_mm(self): """Return text line spacing in millimeters.""" return denormalize(self.leading_pts)
def word_space_mm(self): """Return word spacing in millimeters.""" return denormalize(self.word_space_pts)
def char_space_mm(self): """Return character spacing in millimeters.""" return denormalize(self.char_space_pts)
def rise_mm(self): """Return font rise in millimeters.""" return denormalize(self.rise_pts)
def size_mm(self): """Return font size in millimeters.""" return denormalize(self.size_pts)