Subject: Re: bug#75712: HAVE_RSVG: svg_css_length_to_pixels doesn't handle RSVG_UNIT_CH

From: Stefan Kangas <[email-masked]>
Date: Mon, 20 Jan 2025 20:25:18 -0600

Stefan Kangas <[email-masked]> writes:

> In Bug#75451, Pip Cet <[email-masked]> writes:
>
>> svg_css_length_to_pixels doesn't handle the RSVG_UNIT_CH case.  I don't
>> know at which version rsvg started to offer this unit; it's not
>> indicated in the header file.
>
> It seems to have been added in 2.58:
>
> https://gnome.pages.gitlab.gnome.org/librsvg/Rsvg-2.0/enum.Unit.html

I came up with the following patch, but it's not ideal.

With CSS 3, "the ch unit falls back to 0.5em in the general case, and to
1em when it would be typeset upright".
https://www.w3.org/TR/css-values-3/#ch

Unfortunately, this patch doesn't take into account the "direction" or
"writing-mode" CSS attributes, which is what seems to specify the above.
I couldn't find a way to easily get these CSS attributes using librsvg.

Instead, I just naively assume that they are set to their default
non-vertical value, and thus use 0.5em.  That's not always going to
work, obviously.

While the "ch" attribute seems to be rarely used, it would be nice to
get this detail right.  I'm just not sure how to do it.  Maybe look
through the CSS by hand, searching for these strings and then parsing it
out?  But that's likely to be brittle, ugly, and hard to maintain.

Anyone have any good ideas here?  It's my first time fiddling with
librsvg, so it's quite possible that I'm overlooking something.

That said, I guess this could be installed as a stopgap, to have a
better result than we have now for the common case.
From 63f0dc0e2abbd77c1c6a6b3e445591d000e91cf1 Mon Sep 17 00:00:00 2001
From: Stefan Kangas <[email-masked]>
Date: Tue, 21 Jan 2025 03:04:49 +0100
Subject: [PATCH] Support SVG "ch" length values

* src/image.c (svg_css_length_to_pixels): Support SVG "ch" length
values.  (Bug#75712)
---
 src/image.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/src/image.c b/src/image.c
index b8405d81111..b62857c710a 100644
--- a/src/image.c
+++ b/src/image.c
@@ -12008,9 +12008,15 @@ svg_css_length_to_pixels (RsvgLength length, double dpi, int font_size)
 	 what size we make the image.  */
       value = 0;
       break;
-    default:
-      /* We should never reach this.  */
-      value = 0;
+#if LIBRSVG_CHECK_VERSION (2, 58, 0)
+    case RSVG_UNIT_CH:
+      /* FIXME: With CSS 3, "the ch unit falls back to 0.5em in the
+	 general case, and to 1em when it would be typeset upright".
+	 However, I could not find a way to easily get the relevant CSS
+	 attributes using librsvg.  Thus, we simply wrongly assume the
+	 general case is always true here.  See Bug#75712.  */
+      value = value * font_size / 2.0;
+#endif
     }
 
   return value;
-- 
2.48.1