Text wrapping in Graphviz?

I’m wondering if anything has changed with regard to supporting automatically wrapping text in Graphviz? I didn’t see anything on this forum and DuckDuckGo continues to point to the StackOverflow posts from a decade ago: Text wrapping with dot, Automatic multiline labels in Graphviz?

Using dot2tex may be just fine for my purposes; I’m just wondering if there have been other developments or solutions that I might have missed?

You can insert “\n” for a line break - not sure if that counts as automatic?

Not if the newlines have to be manually inserted. :wink: I’m really just wondering if there have been any additional solutions, scripts, etc beyond the ones linked to above.

Such a script would be very welcome and a valuable addition to Graphviz. It would need to take into account node size, font width in points, node margin, etc. Maybe something in Python, or an ancillary tool in Graphviz. I know nothing about gvpr but maybe it would be up to the task.

1 Like

For reasons that escape me, I’ve written a script that does word-wrap.
Node labels only, no HTML and no record nodes, just strings with \\, \n, \r, \l, \G, and \N (sorry).
It adds one new node attribute “maxTextWidth” - units are inches - e.g
mynode [maxTextWidth=3.3 label="bla bla bla ..."]

It is one of the nastiest things I’ve written in 50 years of programming, so before I consider releasing it to the world:

  1. Given the constraints listed above, does anyone want it?
  2. While it has been tested a bit, (see below) it needs much more testing - non-ASCII chars, and who knows what.

If you are interested, please send me a test file via forum message (click on steveroush, then click message) and attach your best. Remember to add the new attribute as desired (otherwise, no word wrap).

[why a script and not changes to the codebase - good question]

Hi @steveroush , I want to know the script since I want to use it for a project. I am automatically generating flowcharts so I will need text wrapping.

I have posted it here: word wrap for Graphviz · GitHub
(I think I grabbed the correct script, let me know if things don’t work)

1 Like

Thanks for doing this! Really appreciate it!

Hi! So I found a way to do this; I’m working on a hierarchy model in Python (I’m not sure what you are using so sorry if this is irrelevant + I know its been a few years). My data is exported from excel, and I’m using two of the columns are part of node text.

This code automatically wraps them for me

# Wrap the text in nodes using textwrap
        wrapped_role = textwrap.fill(root.role, width=30)
        wrapped_responsibility = textwrap.fill(root.responsibilities, width=30)

node_label = f'{wrapped_role}\n{wrapped_responsibility}'

If you want to see how this fits in my overall code I’d be glad to share but for some reason this took me a while to figure out!

1 Like

Yep, an external solution is the best, as long as it considers character spacing of the desired font, the actual width/height available, …

Of course! After your comment, I just realised that the character spacing in my output was a bit off (and that it could definitely look better), so I’m going to try out the script you added earlier in the forum. Thanks!

pango has the ability to word wrap.
The call is pango_layout_set_width (Pango.Layout.set_width)

I hacked the following into plugin/pango/gvtextlayout_pango.c and got word wrapping after 3 inches of text.

   pango_layout_set_width (layout, 1024*72*3);  // 3 inches

There is much more to it that my simple hack, but its nice to think of pango doing the heavy lifting.
The other part of the challenge is setting lengths for node labels, edge labels, xlabels, graph labels, and fields within records and html nodes. (probably forgot a few).
How to do that without really hacking up the Graphviz language will take some real thought.

Example:

Yes, it’s a little complicated because we don’t necessarily have pangocairo.