[cairo] Computing a group bounding box for an SVG image

Victor Danilchenko vdanilchenko at cimpress.com
Tue Jun 2 13:17:11 PDT 2015


Hi all,

I need to compute the size and coordinates for a bounding box for a <g> subtree in an SVG image (well, actually I just need the box center), identified by a specific attribute. It's easy to do in a web browser (a little DOM traversal, then a getBoundingClientRect() call), but I need to do it server-side. It looks to me like Cairo library might be able to do that, but it also looks fairly complicated, and I just need to perform that one specific task.

I am using the Ruby bindings for Cairo, together with SVG. Any suggestions on how to locate a given node by attribute (without the convenience of jQuery), and then to compute its bounding rectangle?

I am currently loading SVG into a Cairo surface as follows:

@rsvgHandle = RSVG::Handle.new_from_file(@baseFile)
dim = @rsvgHandle.dimensions
@width = dim.width
@height = dim.height

surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, @width, @height)
cr = Cairo::Context.new(surface)
cr.render_rsvg_handle(@rsvgHandle)

Thanks in advance!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20150602/35d54378/attachment.html>


More information about the cairo mailing list