From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Alan Third Newsgroups: gmane.emacs.bugs Subject: bug#44206: 28.0.50; SVG image fail to show Date: Sun, 25 Oct 2020 16:01:05 +0000 Message-ID: <20201025160105.GE59267@breton.holly.idiocy.org> References: <87o8krkmwg.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="MGYHOYXEY6WxJCY8" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="29469"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 44206@debbugs.gnu.org To: styang@fastmail.com Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Oct 25 17:02:28 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kWiTT-0007Zj-NY for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 25 Oct 2020 17:02:27 +0100 Original-Received: from localhost ([::1]:34716 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kWiTS-0005Ca-CJ for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 25 Oct 2020 12:02:26 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51280) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kWiT6-0004td-5X for bug-gnu-emacs@gnu.org; Sun, 25 Oct 2020 12:02:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:54096) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kWiT4-0007Cb-UU for bug-gnu-emacs@gnu.org; Sun, 25 Oct 2020 12:02:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kWiT4-0003cm-Qh for bug-gnu-emacs@gnu.org; Sun, 25 Oct 2020 12:02:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Alan Third Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 25 Oct 2020 16:02:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44206 X-GNU-PR-Package: emacs Original-Received: via spool by 44206-submit@debbugs.gnu.org id=B44206.160364168013853 (code B ref 44206); Sun, 25 Oct 2020 16:02:02 +0000 Original-Received: (at 44206) by debbugs.gnu.org; 25 Oct 2020 16:01:20 +0000 Original-Received: from localhost ([127.0.0.1]:37405 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kWiSN-0003bM-O0 for submit@debbugs.gnu.org; Sun, 25 Oct 2020 12:01:20 -0400 Original-Received: from wilbur.contactoffice.com ([212.3.242.68]:58704) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kWiSL-0003b7-CP for 44206@debbugs.gnu.org; Sun, 25 Oct 2020 12:01:18 -0400 Original-Received: from smtpauth1.co-bxl (smtpauth1.co-bxl [10.2.0.15]) by wilbur.contactoffice.com (Postfix) with ESMTP id BB56096B; Sun, 25 Oct 2020 17:01:09 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; t=1603641669; s=20200222-6h9o; d=idiocy.org; i=alan@idiocy.org; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version:Content-Type:In-Reply-To; l=6032; bh=X9n5dJEQf8BZTMQnl4qx/90z+YprTD5Zh3UQYBqOOec=; b=uooe+XC2Crcn4a32Ukoe0nY3BpEPVwnuDyulefM8j7GTrmMVjCu6rl0ztQtcq7T7 eJsx/g3h/u+v5YJrC/nUsarse/azt4GdFsBXoojEUGI7nxYmP4TgJplh4IWkrx2kzLf g6WnGAhuRqDlRW4XH6cfwS/M2K08aZmlhodz9bO3QqeTuSo2Hbmpdc/TCjp8+7ZNF34 B9GLV3AgEs5SNwY7ufW6krZO973SdfvPX7WVtoi2/LXxVExGCD09912pIBgAd/bhak8 CIFvZIFrermQbAuRpUp53qdR3CJII2IsjZErgwu+u3GW5Uc8GLoDY4MsRYPQH3Kl/d0 vsWTPuFTVw== Original-Received: by smtp.mailfence.com with ESMTPA ; Sun, 25 Oct 2020 17:01:06 +0100 (CET) Original-Received: by breton.holly.idiocy.org (Postfix, from userid 501) id 57D8220263E150; Sun, 25 Oct 2020 16:01:05 +0000 (GMT) Mail-Followup-To: Alan Third , styang@fastmail.com, 44206@debbugs.gnu.org Content-Disposition: inline In-Reply-To: <87o8krkmwg.fsf@gmail.com> X-ContactOffice-Account: com:241649512 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:191507 Archived-At: --MGYHOYXEY6WxJCY8 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sat, Oct 24, 2020 at 10:25:03PM -0500, styang@fastmail.com wrote: > Some SVG images fail to show due to regression caused by: > b42481e22e * | Fix SVG image dimension calculations (bug#44065) > > Please find the offending SVG file in attachment (in fact, all > custom avatars in telega.el are not showing). Thanks. It appears there are more ways to define an SVG size than I've had hot dinners. librsvg is really not helping here. The documentation makes it pretty clear that they don't want us to be querying the SVG dimensions and would prefer us to just give them the dimensions we want and/or to use Cairo. That doesn't really work for us because we don't know up-front what dimensions are usable for the image. It would be a different matter if we were using the SVGs as GUI components like buttons or something. Anyway, I've thrown in another attempt at calculating the image size, and it works for this and also for the previous images. This one is more complex because it's trying to convert CSS sizes to pixel sizes and I don't think we can be entirely sure of some of them (like ex height? Maybe we can query that, but then we have to know font and font size). I have no doubt that there are many more SVG files out there that won't display properly even with this patch. > Affected librsvg version: 2:2.50.1-1 in Archlinux, and 2.44.10-2.1 in Debian. I'm concerned that librsvg 2.44 is affected as it should be using the same code path as before the commit in question. -- Alan Third --MGYHOYXEY6WxJCY8 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-Calculate-SVG-image-sizes-more-accurately-bug-44206.patch" >From a178c8b58e37550a897062f1edfe1f674bf7d210 Mon Sep 17 00:00:00 2001 From: Alan Third Date: Sun, 25 Oct 2020 15:45:07 +0000 Subject: [PATCH] Calculate SVG image sizes more accurately (bug#44206) * src/image.c (svg_css_length_to_pixels): New function. (svg_load_image): Try more methods to work out the image size. --- src/image.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 93 insertions(+), 5 deletions(-) diff --git a/src/image.c b/src/image.c index 5f6d9f4c44..2f9c21cbcc 100644 --- a/src/image.c +++ b/src/image.c @@ -9724,6 +9724,44 @@ svg_load (struct frame *f, struct image *img) return success_p; } +static double +svg_css_length_to_pixels (RsvgLength length) +{ + /* 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) + { + case RSVG_UNIT_PX: + /* Already a pixel value. */ + break; + case RSVG_UNIT_CM: + /* 2.54 cm in an inch. */ + value = dpi * value / 2.54; + case RSVG_UNIT_MM: + /* 25.4 mm in an inch. */ + value = dpi * value / 25.4; + break; + case RSVG_UNIT_PT: + /* 72 points in an inch. */ + value = dpi * value / 72; + case RSVG_UNIT_PC: + /* 6 picas in an inch. */ + value = dpi * value / 6; + case RSVG_UNIT_IN: + value *= dpi; + break; + default: + /* Probably one of em, ex, or %. We can't know what the pixel + value is without more information. */ + value = 0; + } + + return value; +} + /* Load frame F and image IMG. CONTENTS contains the SVG XML data to be parsed, SIZE is its size, and FILENAME is the name of the SVG file being loaded. @@ -9792,11 +9830,48 @@ svg_load_image (struct frame *f, struct image *img, char *contents, #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; + /* Try the instrinsic dimensions first. */ + gboolean has_width, has_height, has_viewbox; + RsvgLength iwidth, iheight; + + rsvg_handle_get_intrinsic_dimensions (rsvg_handle, + &has_width, &iwidth, + &has_height, &iheight, + &has_viewbox, &viewbox); + + 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); + } + else if (has_width && has_viewbox) + { + viewbox_width = svg_css_length_to_pixels (iwidth); + viewbox_height = svg_css_length_to_pixels (iwidth) + * 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 / viewbox.width; + } + else if (has_viewbox) + { + viewbox_width = viewbox.width; + viewbox_height = viewbox.height; + } + else + { + /* We haven't found a useable set of sizes, so try working out + the visible area. */ + 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 /* The function used above to get the geometry of the visible area of the SVG are only available in librsvg 2.46 and above, so in @@ -9809,6 +9884,19 @@ svg_load_image (struct frame *f, struct image *img, char *contents, viewbox_width = dimension_data.width; viewbox_height = dimension_data.height; #endif + + if (viewbox_width == 0 || viewbox_height == 0) + { + /* We do not have any usable dimensions, so make some up. The + values below are supposedly the default values most web + browsers use for SVGs with no set size. */ + /* FIXME: At this stage we should perhaps consider rendering the + image out to a bitmap and getting the dimensions from + that. */ + viewbox_width = 300; + viewbox_height = 150; + } + compute_image_size (viewbox_width, viewbox_height, img->spec, &width, &height); -- 2.26.1 --MGYHOYXEY6WxJCY8--