treeviz: support multiline labels

This commit is contained in:
Sebastien Bourdeauducq 2013-08-07 21:46:03 +02:00
parent ceddd8afa4
commit 0e369318bb
1 changed files with 14 additions and 4 deletions

View File

@ -14,10 +14,20 @@ def _cairo_draw_node(ctx, dx, radius, color, outer_color, s):
ctx.arc(0, 0, radius, 0, 2*math.pi) ctx.arc(0, 0, radius, 0, 2*math.pi)
ctx.fill() ctx.fill()
ctx.set_source_rgb(0, 0, 0) lines = s.split("\n")
x_bearing, y_bearing, textw, texth, x_advance, y_advance = ctx.text_extents(s) textws = []
ctx.translate(-textw/2, texth/2) texths = []
ctx.show_text(s) for line in lines:
x_bearing, y_bearing, w, h, x_advance, y_advance = ctx.text_extents(line)
textws.append(w)
texths.append(h + 2)
ctx.translate(0, -sum(texths[1:])/2)
for line, w, h in zip(lines, textws, texths):
ctx.translate(-w/2, h/2)
ctx.move_to(0, 0)
ctx.set_source_rgb(0, 0, 0)
ctx.show_text(line)
ctx.translate(w/2, h/2)
ctx.restore() ctx.restore()