Providing dimensions for images in SVG output

I’m using Graphviz as a library, and I’d like to render a graph like this as SVG:

digraph {
  a [image="http://example.com/myimage.jpg"]
}

That is, the resulting SVG file should have an <image> element referring to "http://example.com/myimage.jpg". Of course, because Graphviz needs the image dimensions to render the graph, and only local images are supported, the result is this warning: "http://example.com/myimage.jpg" was not found as a file or as a shape library member.

However, I don’t want Graphviz (or a script like dot_url_resolve.py) to load the image – I already know its dimensions. And from looking at gvloadimage_core.c, it seems like Graphviz only really needs to know the dimensions of the image when rendering SVG – it doesn’t need to do anything else with the image data.

Is there currently any way to provide dimensions for an image ahead of time so that Graphviz won’t try to read them from an image file that doesn’t exist?

Specifically, this is to support images in Viz.js. I did figure out an implementation of this previously, but it involves writing a dummy SVG image to the Emscripten virtual filesystem at a path like /http:/example.com/myimage.jpg, which Graphviz will read to find its dimensions. I’m wondering if there’s a way to do it that’s less of a hack.

Not that I’m aware of, but this seems like a reasonable feature request. Happy to accept merge requests over on Gitlab.

1 Like

Yeah it seems reasonable to add width and height attributes or something similar. Html img tags have width and height specifyable for this reason, so you can reserve space before downloading the image.

OK, I’ll give it a shot!

I was thinking of this as an addition for library usage, but it seems like attributes would be easier to use.

What about a single point attribute that specifies the size of the image?

digraph {
  a [image="http://example.com/myimage.jpg", imagesize="300,200"]
}

Are user shapes measured in pixels or points? (That is, the w and h fields of usershape_s.)

imagesize sounds like a reasonable choice to me. Not sure offhand what units w and h are in. You would have to check the source.

For user-facing stuff like attributes there’s also been discussion of allowing explicit units, Feature request: allow specifying units in size attributes (#2100) · Issues · graphviz / graphviz · GitLab. This would be a nice thing to add uniformly.