treeviz: support multiline labels
authorSebastien Bourdeauducq <sebastien@milkymist.org>
Wed, 7 Aug 2013 19:46:03 +0000 (21:46 +0200)
committerSebastien Bourdeauducq <sebastien@milkymist.org>
Wed, 7 Aug 2013 19:46:03 +0000 (21:46 +0200)
migen/graph/treeviz.py

index 9e5a44fd7a342918061ece4fcb56fda96ecb61b7..196871d16acbf11f5a87a6c92e64b0943b813533 100644 (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.fill()
 
-       ctx.set_source_rgb(0, 0, 0)
-       x_bearing, y_bearing, textw, texth, x_advance, y_advance = ctx.text_extents(s)
-       ctx.translate(-textw/2, texth/2)
-       ctx.show_text(s)
+       lines = s.split("\n")
+       textws = []
+       texths = []
+       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()