Subject: Re: bug#64908: 29.1; svg parse failure

From: David Ponce <[email-masked]>
Date: Sat, 5 Aug 2023 10:30:38 +0200

On 04/08/2023 23:08, David Ponce wrote:
> On 04/08/2023 21:08, Eli Zaretskii wrote:
>>> Date: Fri, 4 Aug 2023 19:32:13 +0100
>>> From: Alan Third <[email-masked]>
>>> Cc: David Ponce <[email-masked]>, [email-masked]
>>>
>>> On Fri, Aug 04, 2023 at 01:26:45PM +0300, Eli Zaretskii wrote:
>>>>
>>>> Thanks.  I hope this will help an SVG expert to figure out what's
>>>> going on with these images.
>>>>
>>>> Alan, do you have time to look into this, per chance?
>>>
>>> Yeah. I know roughly what needs to be done here thanks to David's most
>>> recent email, but I'll have a wee think about how best to do it since
>>> we already have, I think, three different methods of finding the image
>>> size depending on the version of librsvg. If I can merge a couple of
>>> those approaches it will probably be best.
>>
>> Thanks in advance.
> 
> Eli, Alan,
> 
> Until Alan finds the best solution, the attached simple patch fixed
> the issue for me with librsvg 2.56.3.
> 
> Thanks & Regards

Hello,

I attached a better patch, because it is possible to have svg image
definition that combines viewBox, and percentage of width/height, like
in this example:

(insert-image
   '(image :type svg :data
      "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 22 22\" width=\"110%\" height=\"80%\">
   <defs id=\"defs3051\">
     <style type=\"text/css\" id=\"current-color-scheme\">
       .ColorScheme-Text {
         color:#232629;
       }
       </style>
   </defs>
  <path
     style=\"fill:currentColor;fill-opacity:1;stroke:none\"
     d=\"m7.707031 3l-.707031.707031 6.125 6.125 1.167969 1.167969-1.167969 1.167969-6.125 6.125.707031.707031 6.125-6.125 1.875-1.875-1.875-1.875-6.125-6.125\"
     class=\"ColorScheme-Text\"
     />
</svg>"
     :scale 1))

I hope Alan will find a simpler implementation for all these cases!

Thanks & Regards

diff --git a/src/image.c b/src/image.c
index cb9725892d1..ae24f5b3893 100644
--- a/src/image.c
+++ b/src/image.c
@@ -11358,12 +11358,16 @@ 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, dpi,
-						    img->face_font_size);
-	  viewbox_height = svg_css_length_to_pixels (iheight, dpi,
-						     img->face_font_size);
-
+	  viewbox_width = ((iwidth.unit == RSVG_UNIT_PERCENT) && has_viewbox)
+	    ? viewbox.width * iwidth.length
+	    : svg_css_length_to_pixels (iwidth, dpi, img->face_font_size);
+	  
+	  viewbox_height = ((iheight.unit == RSVG_UNIT_PERCENT) && has_viewbox)
+	    ? viewbox.height * iheight.length
+	    : svg_css_length_to_pixels (iheight, dpi, img->face_font_size);
+	  
 	  /* Here one dimension could be zero because in percent unit.
 	     So calculate this dimension with the other.  */
 	  if (! (0 < viewbox_width) && (iwidth.unit == RSVG_UNIT_PERCENT))