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

From: Eli Zaretskii <[email-masked]>
Date: Thu, 30 Nov 2023 19:32:52 +0200

> From: JD Smith <[email-masked]>
> Date: Wed, 29 Nov 2023 15:31:43 -0500
> 
> ;;; test-svg-pixel-position --- test pixel position for SVG images
> 
> ;;; This small code creates a buffer with two lines, the first of
> ;;; which is long, and the second of which has an SVG image at start.
> ;;; Line truncation is turned on.  `window-text-pixel-size` returns
> ;;; differing results depending on whether truncation is actually in
> ;;; effect (alter the frame width to see this).
> 
> ;;; Code:
> (require 'svg)
> (let ((buf "svg-pixel-demo")
>      (svg (svg-create 50 25)))
>  (svg-circle svg 25 25 25 :stroke-color "green")
>  (with-current-buffer (get-buffer-create buf)
>    (erase-buffer)
>    (insert "Pellentesque condimentum, magna ut suscipit hendrerit, ipsum augue ornare nulla, non luctus diam neque sit amet urna.\n")
>    (insert (propertize "THISISACIRCLE" 'display (svg-image svg)))
>    (insert " Aliquam posuere.\n")
>    (pop-to-buffer buf)
>    (goto-char (point-max))
>    (forward-line -1)
>    (toggle-truncate-lines 1)
>    (message "PIXEL SIZE OF LINE ABOVE IMAGE: %S"
> 	     (window-text-pixel-size nil (cons (point) -1) (point) nil nil nil t))))

Does the patch below fix the issue?  (It should fix the recipe you
posted, but you hinted that this is just the simplest way of seeing a
more general problem, so I wonder whether that more general problem is
also fixed.)

diff --git a/src/xdisp.c b/src/xdisp.c
index eb38ebb..7cf3902 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -11487,6 +11487,16 @@ window_text_pixel_size (Lisp_Object window, Lisp_Object from, Lisp_Object to,
 	      it.max_descent = max (it.max_descent, it.descent);
 	    }
 	}
+      else if (IT_CHARPOS (it) > end
+	       && it.line_wrap == TRUNCATE
+	       && it.current_x - it.first_visible_x >= it.last_visible_x)
+	{
+	  /* If the display property at END is at the beginning of the
+	     line, and the previous line was truncated, we are at END,
+	     but it.current_y is not yet updated to reflect that.  */
+	  it.current_y += max (it.max_ascent, it.ascent)
+			  + max (it.max_descent, it.descent);
+	}
     }
   else
     bidi_unshelve_cache (it2data, true);