Open jeroen opened 2 years ago
I've just filed https://gitlab.gnome.org/GNOME/librsvg/-/issues/852 to remind myself to document how to deal exactly with this case :)
Short summary, which I'll expand in the docs:
rsvg_handle_get_intrinsic_size_in_pixels()
. If that returns true, you have a size you can use. If it doesn't, it means the SVG has something like <svg width="50%" height="100%">
, which must be resolved against a viewport..rsvg_handle_get_geometry_for_element(handle, NULL, &out_ink_rect, NULL, &error)
to ask librsvg to actually measure the geometries of all elements. This is the implicit behavior from the old rsvg_handle_get_dimensions()
.
Currently we manually calculate the size and scale parameters, based on the w and h given by the user and fitting that into the size given by
rsvg_handle_get_intrinsic_dimensions
.However as of librsvg 2.52 we have to use the new
rsvg_handle_render_document
API which allows us to pass a viewport size to do auto-scaling. Perhaps this also makes the output work better with css.However the thing I can't figure out is how to make this work if we don't know the viewport size beforehand, i.e. if we just want to render to the size defined in the svg itself, rather than provides by the user. The API seems to assume that the application uses a fixed size viewport, but that is not always the case.
In our application, we first create a cairo canvas using
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height)
, and then load the svg handle. So we need to give a width/height before loading the svg, but if the user has not specified a width/height, we want to use the width/height as specified in the svg file (which we haven't loaded yet at that point).So for now we don't use auto-scaling yet, and keep manual scaling (with the new apis): https://github.com/jeroen/rsvg/pull/28