Subject: Re: bug#67533: SVG images confound position pixel measurements

From: Eli Zaretskii <[email-masked]>
Date: Sat, 02 Dec 2023 21:39:22 +0200

> Cc: [email-masked]
> Date: Sat, 02 Dec 2023 16:18:28 +0200
> From: Eli Zaretskii <[email-masked]>
> 
> > From: JD Smith <[email-masked]>
> > Date: Sat, 2 Dec 2023 08:36:07 -0500
> > Cc: [email-masked]
> > 
> > Strange.  Both of my ports here (NS and Mac) show the same issue.  Did you check the
> > svg-pixel-demo-report buffer the header mentions, where the mismatch measurement information
> > gets reported?  The specific problems encountered are listed in the report buffer, updated with
> > my/check-buffer-pixel-values.  For example, at a standard width of 80 characters, I get this report:
> > 
> >  SVG Position analysis for svg-pixel-demo (width 80, text-properties)
> > 
> >  Incorrect at point= 34: line  2 at   (224 . 14) (image): expected 14 got 28
> >  Incorrect at point= 99: line  3 at   (244 . 43) (image): expected 29 got 46
> >  Incorrect at point=126: line  4 at    (42 . 75) (image): expected 32 got 46
> >  Incorrect at point=162: line  4 at   (345 . 75) (image): expected 32 got 52
> >  Incorrect at point=210: line  5 at  (133 . 110) (image): expected 35 got 49
> 
> I never understood I need to look in another buffer.  I will look into
> these.

Please try the patch below and see if any problems remain.

diff --git a/src/xdisp.c b/src/xdisp.c
index 0b2508c..cc95ab3 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -11436,7 +11436,7 @@ window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
       /* Start at the beginning of the line containing FROM.  Otherwise
 	 IT.current_x will be incorrectly set to zero at some arbitrary
 	 non-zero X coordinate.  */
-      reseat_at_previous_visible_line_start (&it);
+      move_it_by_lines (&it, 0);
       it.current_x = it.hpos = 0;
       if (IT_CHARPOS (it) != start)
 	{
@@ -11513,6 +11513,8 @@ window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
      the width of the last buffer position manually.  */
   if (IT_CHARPOS (it) > end)
     {
+      int end_y = it.current_y;
+
       end--;
       RESTORE_IT (&it, &it2, it2data);
       x = move_it_to (&it, end, to_x, max_y, -1, move_op);
@@ -11525,8 +11527,13 @@ window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 
 	  /* DTRT if ignore_line_at_end is t.  */
 	  if (!NILP (ignore_line_at_end))
-	    doff = (max (it.max_ascent, it.ascent)
-		    + max (it.max_descent, it.descent));
+	    {
+	      /* If END-1 is on the previous screen line, we need to
+                 account for the vertical dimensions of previous line.  */
+	      if (it.current_y < end_y)
+		doff = (max (it.max_ascent, it.ascent)
+			+ max (it.max_descent, it.descent));
+	    }
 	  else
 	    {
 	      it.max_ascent = max (it.max_ascent, it.ascent);