def _precalc_text(self, node): E = self._eval_func(node) node.fontname = E('fontName') node.lineheight = E('lineHeight') node.max_text_width = E('maxTextWidth') node.hyphenate = E('hyphenate') _ctx.font(node.fontname, node.fontsize) _ctx.lineheight(node.lineheight) lineheight = node.lineheight * node.fontsize textwidth_func = lambda (txt): textwidth(_ctx, txt, node.fontname, node .fontsize) if self._wraprect: (node._textlines, node._textlinewidths, node._textrects, node.textwidth, node.textheight) = textwrap.wrap_rect(node.label, lineheight, textwidth_func, node.max_text_width) else: (node._textlines, node._textlinewidths, node._textrects, node.textwidth, node.textheight) = textwrap.wrap_shape(node.label, lineheight, textwidth_func, self._shapefunc, hyphenate=node.hyphenate, **self._shapefunc_args)
def _precalc_text(self, node): E = self._eval_func(node) node.fontname = E('fontName') node.lineheight = E('lineHeight') node.max_text_width = E('maxTextWidth') node.hyphenate = E('hyphenate') _ctx.font(node.fontname, node.fontsize) _ctx.lineheight(node.lineheight) lineheight = node.lineheight * node.fontsize textwidth_func = lambda(txt): textwidth(_ctx, txt, node.fontname, node.fontsize) if self._wraprect: (node._textlines, node._textlinewidths, node._textrects, node.textwidth, node.textheight) = textwrap.wrap_rect(node.label, lineheight, textwidth_func, node.max_text_width) else: (node._textlines, node._textlinewidths, node._textrects, node.textwidth, node.textheight) = textwrap.wrap_shape(node.label, lineheight, textwidth_func, self._shapefunc, hyphenate=node.hyphenate, **self._shapefunc_args)
def _drawtext(self, node, xoffs, yoffs): E = self._eval_func(node) if not node._textrects: return # Text alignment alignment = LEFT text_align = E('textAlign') if text_align == 'right': alignment = RIGHT elif text_align == 'center': alignment = CENTER elif text_align == 'justify': alignment = JUSTIFY elif text_align == 'auto': if node.isroot(): alignment = CENTER else: alignment = (RIGHT if node.direction() == Direction.Left else LEFT) # Draw text baseline_corr = E('textBaselineCorrection') _ctx.font(node.fontname, node.fontsize) _ctx.lineheight(node.lineheight) justify_min_lines = E('justifyMinLines') ystep = node._textrects[0].h nonblank_lines = 0 for l in node._textlines: if l: nonblank_lines += 1 if nonblank_lines <= justify_min_lines: self._center_text_vertically(node) baseline_offs = node.fontsize * baseline_corr lineheight_offs = -(node.lineheight - 1) / 2 * node.fontsize if alignment == JUSTIFY and nonblank_lines <= justify_min_lines: alignment = CENTER if alignment == JUSTIFY: spacewidth = textwidth(_ctx, ' ', node.fontname, node.fontsize) for i, l in enumerate(node._textlines): x, y, w, h = node._textrects[i].params() x += xoffs y += yoffs + baseline_offs + lineheight_offs + ystep if DEBUG: _ctx.save() _ctx.nofill() _ctx.stroke(node.fontcolor) _ctx.rect(x, y - h, w, h) _ctx.stroke(1, 0, 0) _ctx.rect(tx, ty, node.textwidth, node.textheight) _ctx.restore() words = l.split() numspaces = float(l.count(' ')) x_spacing = spacewidth if numspaces: charswidth = (node._textlinewidths[i] - numspaces * spacewidth) x_spacing = (w - charswidth) / numspaces # TODO remove 7 magic constant if ((i == 0 or i == len(node._textlines) - 1) and (x_spacing > spacewidth * 7 or len(words) == 1)): x_spacing = spacewidth x += (w - node._textlinewidths[i]) / 2. for w in words: _ctx.text(w, x, y) x += x_spacing + textwidth(_ctx, w, node.fontname, node.fontsize) else: for i, l in enumerate(node._textlines): x, y, w, h = node._textrects[i].params() x += xoffs y += yoffs + ystep if DEBUG: _ctx.save() _ctx.nofill() _ctx.stroke(node.fontcolor) _ctx.rect(x, y - h, w, h) _ctx.stroke(1, 0, 0) _ctx.rect(tx, ty, node.textwidth, node.textheight) _ctx.restore() y += baseline_offs + lineheight_offs if alignment == RIGHT: x += w - node._textlinewidths[i] elif alignment == CENTER: x += (w - node._textlinewidths[i]) / 2. _ctx.text(l, x, y)