>From 5a9e26a16b46797d10dee5c725877177d750e536 Mon Sep 17 00:00:00 2001 From: Alan Third Date: Wed, 9 Dec 2020 00:02:44 +0000 Subject: [PATCH] Use real DPI when rendering SVGs (bug#45124) * src/image.c (svg_css_length_to_pixels): Pass in a DPI value instead of using a hard coded value. (svg_load_image): Set the DPI on the rsvg_handle, and pass it to svg_css_length_to_pixels. --- src/image.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/image.c b/src/image.c index f16b9454a1..1523b4c5e3 100644 --- a/src/image.c +++ b/src/image.c @@ -9759,11 +9759,8 @@ svg_load (struct frame *f, struct image *img) #if LIBRSVG_CHECK_VERSION (2, 46, 0) static double -svg_css_length_to_pixels (RsvgLength length) +svg_css_length_to_pixels (RsvgLength length, double dpi) { - /* FIXME: 96 appears to be a pretty standard DPI but we should - probably use the real DPI if we can get it. */ - double dpi = 96; double value = length.length; switch (length.unit) @@ -9837,6 +9834,9 @@ svg_load_image (struct frame *f, struct image *img, char *contents, rsvg_handle = rsvg_handle_new_from_stream_sync (input_stream, base_file, RSVG_HANDLE_FLAGS_NONE, NULL, &err); + rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, + FRAME_DISPLAY_INFO (f)->resy); + if (base_file) g_object_unref (base_file); g_object_unref (input_stream); @@ -9848,6 +9848,9 @@ svg_load_image (struct frame *f, struct image *img, char *contents, rsvg_handle = rsvg_handle_new (); eassume (rsvg_handle); + rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, + FRAME_DISPLAY_INFO (f)->resy); + /* Set base_uri for properly handling referenced images (via 'href'). Can be explicitly specified using `:base_uri' image property. See rsvg bug 596114 - "image refs are relative to curdir, not .svg file" @@ -9872,6 +9875,7 @@ svg_load_image (struct frame *f, struct image *img, char *contents, /* Try the instrinsic dimensions first. */ gboolean has_width, has_height, has_viewbox; RsvgLength iwidth, iheight; + double dpi = FRAME_DISPLAY_INFO (f)->resx; rsvg_handle_get_intrinsic_dimensions (rsvg_handle, &has_width, &iwidth, @@ -9881,19 +9885,19 @@ svg_load_image (struct frame *f, struct image *img, char *contents, if (has_width && has_height) { /* Success! We can use these values directly. */ - viewbox_width = svg_css_length_to_pixels (iwidth); - viewbox_height = svg_css_length_to_pixels (iheight); + viewbox_width = svg_css_length_to_pixels (iwidth, dpi); + viewbox_height = svg_css_length_to_pixels (iheight, dpi); } else if (has_width && has_viewbox) { - viewbox_width = svg_css_length_to_pixels (iwidth); - viewbox_height = svg_css_length_to_pixels (iwidth) + viewbox_width = svg_css_length_to_pixels (iwidth, dpi); + viewbox_height = svg_css_length_to_pixels (iwidth, dpi) * viewbox.width / viewbox.height; } else if (has_height && has_viewbox) { - viewbox_height = svg_css_length_to_pixels (iheight); - viewbox_width = svg_css_length_to_pixels (iheight) + viewbox_height = svg_css_length_to_pixels (iheight, dpi); + viewbox_width = svg_css_length_to_pixels (iheight, dpi) * viewbox.height / viewbox.width; } else if (has_viewbox) @@ -10002,6 +10006,10 @@ svg_load_image (struct frame *f, struct image *img, char *contents, rsvg_handle = rsvg_handle_new_from_stream_sync (input_stream, base_file, RSVG_HANDLE_FLAGS_NONE, NULL, &err); + + rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, + FRAME_DISPLAY_INFO (f)->resy); + if (base_file) g_object_unref (base_file); g_object_unref (input_stream); @@ -10013,6 +10021,9 @@ svg_load_image (struct frame *f, struct image *img, char *contents, rsvg_handle = rsvg_handle_new (); eassume (rsvg_handle); + rsvg_handle_set_dpi_x_y (rsvg_handle, FRAME_DISPLAY_INFO (f)->resx, + FRAME_DISPLAY_INFO (f)->resy); + /* Set base_uri for properly handling referenced images (via 'href'). Can be explicitly specified using `:base_uri' image property. See rsvg bug 596114 - "image refs are relative to curdir, not .svg file" -- 2.29.2