From 760fe87e1dcb7451a5fa9ff19b80f940b8e03304 Mon Sep 17 00:00:00 2001 From: Alan Third Date: Mon, 19 Oct 2020 21:19:57 +0100 Subject: [PATCH v2] Fix SVG image dimension calculations (bug#44065) * src/image.c (svg_load_image): Calculate the image size by using the viewBox size and applying it to the image. --- src/image.c | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/image.c b/src/image.c index 25d5af8a8d..c734623739 100644 --- a/src/image.c +++ b/src/image.c @@ -9736,7 +9736,7 @@ svg_load_image (struct frame *f, struct image *img, char *contents, ptrdiff_t size, char *filename) { RsvgHandle *rsvg_handle; - RsvgDimensionData dimension_data; + double viewbox_width, viewbox_height; GError *err = NULL; GdkPixbuf *pixbuf; int width; @@ -9789,15 +9789,34 @@ svg_load_image (struct frame *f, struct image *img, char *contents, #endif /* Get the image dimensions. */ +#if LIBRSVG_CHECK_VERSION (2, 46, 0) + RsvgRectangle zero_rect, viewbox; + + rsvg_handle_get_geometry_for_layer (rsvg_handle, NULL, + &zero_rect, &viewbox, + NULL, NULL); + viewbox_width = viewbox.x + viewbox.width; + viewbox_height = viewbox.y + viewbox.height; +#else + RsvgDimensionData dimension_data; + rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); + viewbox_width = dimension_data.width; + viewbox_height = dimension_data.height; +#endif + compute_image_size (viewbox_width, viewbox_height, img->spec, + &width, &height); + + if (! check_image_size (f, width, height)) + { + image_size_error (); + goto rsvg_error; + } + /* We are now done with the unmodified data. */ g_object_unref (rsvg_handle); - /* Calculate the final image size. */ - compute_image_size (dimension_data.width, dimension_data.height, - img->spec, &width, &height); - /* Wrap the SVG data in another SVG. This allows us to set the width and height, as well as modify the foreground and background colors. */ @@ -9820,7 +9839,7 @@ svg_load_image (struct frame *f, struct image *img, char *contents, "xmlns:xi=\"http://www.w3.org/2001/XInclude\" " "style=\"color: #%06X; fill: currentColor;\" " "width=\"%d\" height=\"%d\" preserveAspectRatio=\"none\" " - "viewBox=\"0 0 %d %d\">" + "viewBox=\"0 0 %f %f\">" "" "" ""; @@ -9845,8 +9864,9 @@ svg_load_image (struct frame *f, struct image *img, char *contents, if (!wrapped_contents || buffer_size <= snprintf (wrapped_contents, buffer_size, wrapper, foreground & 0xFFFFFF, width, height, - dimension_data.width, dimension_data.height, - background & 0xFFFFFF, SSDATA (encoded_contents))) + viewbox_width, viewbox_height, + background & 0xFFFFFF, + SSDATA (encoded_contents))) goto rsvg_error; wrapped_size = strlen (wrapped_contents); @@ -9887,12 +9907,6 @@ svg_load_image (struct frame *f, struct image *img, char *contents, if (err) goto rsvg_error; #endif - rsvg_handle_get_dimensions (rsvg_handle, &dimension_data); - if (! check_image_size (f, dimension_data.width, dimension_data.height)) - { - image_size_error (); - goto rsvg_error; - } /* We can now get a valid pixel buffer from the svg file, if all went ok. */ -- 2.26.1