GNU bug report logs - #73129
31.0.50; buffer-text-pixel-size vs. newlines

Previous Next

Package: emacs;

Reported by: David Ponce <da_vid <at> orange.fr>

Date: Sun, 8 Sep 2024 22:23:02 UTC

Severity: normal

Found in version 31.0.50

Done: Eli Zaretskii <eliz <at> gnu.org>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: help-debbugs <at> gnu.org (GNU bug Tracking System)
To: David Ponce <da_vid <at> orange.fr>
Subject: bug#73129: closed (Re: bug#73129: 31.0.50; buffer-text-pixel-size
 vs. newlines)
Date: Mon, 09 Sep 2024 13:26:02 +0000
[Message part 1 (text/plain, inline)]
Your bug report

#73129: 31.0.50; buffer-text-pixel-size vs. newlines

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to 73129 <at> debbugs.gnu.org.

-- 
73129: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=73129
GNU Bug Tracking System
Contact help-debbugs <at> gnu.org with problems
[Message part 2 (message/rfc822, inline)]
From: Eli Zaretskii <eliz <at> gnu.org>
To: David Ponce <da_vid <at> orange.fr>
Cc: 73129-done <at> debbugs.gnu.org
Subject: Re: bug#73129: 31.0.50; buffer-text-pixel-size vs. newlines
Date: Mon, 09 Sep 2024 16:25:07 +0300
> Date: Mon, 9 Sep 2024 14:56:22 +0200
> Cc: 73129 <at> debbugs.gnu.org
> From: David Ponce <da_vid <at> orange.fr>
> 
> > May I ask where and for what purpose did you need to measure pixel
> > width of a string that included newlines?
> 
> Actually, the current behavior doesn't really impact my current work. I
> just noticed it while experimenting, having passed to `string-pixel-width'
> a buffer substring that spanned multiple lines.  And I thought it would be
> good to, at least, clarify the behavior I observed ;-)
> 
> You can close this bug.

Thanks, closing.

[Message part 3 (message/rfc822, inline)]
From: David Ponce <da_vid <at> orange.fr>
To: bug-gnu-emacs <at> gnu.org
Subject: 31.0.50; buffer-text-pixel-size vs. newlines
Date: Mon, 9 Sep 2024 00:22:11 +0200
Hello,

Its seems that the function `buffer-text-pixel-size' is not working
as described when there are newlines in the buffer.  Here is a quick
illustration (on my laptop (frame-char-width) returns 12 pixels):

;; As expected.
(with-temp-buffer
  (insert "abcdef")
  (car (buffer-text-pixel-size nil nil t)))
72

;; I suppose it is as expected because newline is not displayed.
(with-temp-buffer
  (insert "\n")
  (car (buffer-text-pixel-size nil nil t)))
0

;; ?
(with-temp-buffer
  (insert "abcd\nef")
  (car (buffer-text-pixel-size nil nil t)))
48

The doc string of `buffer-text-pixel-size' mentions:

"Return size of whole text of BUFFER-OR-NAME in WINDOW."

So ,I expect that the last example will return 72px (6 x 12px) + 0px
for the newline.  But the result is 48px, which means that all the
text after the first newline is not counted.

This also impacts "string-pixel-width" which is supposed to return
the pixel size of the passed string, not part of it:

(string-pixel-width "abcdef") => 72
(string-pixel-width "\n") => 0
(string-pixel-width "abcd\nef") => 48
(string-pixel-width "abcd\nef\ngh") => 48

At least this limitation should be mentioned in the doc string?

A possible fix for `string-pixel-width' could be to remove all
the newlines from the passed string before to call
`buffer-text-pixel-size'?  Something like this:

diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 4cdb065feeb..c68b3b37f9b 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -406,6 +406,11 @@ string-pixel-width
                         face-remapping-alist))
         (kill-local-variable 'face-remapping-alist))
       (insert string)
+      ;; Remove newlines.
+      (forward-line 0)
+      (while (not (bobp))
+        (delete-char -1)
+        (forward-line 0))
       ;; Prefer `remove-text-properties' to `propertize' to avoid
       ;; creating a new string on each call.
       (remove-text-properties


Thanks!

In GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.43, cairo version 1.18.0)
Repository revision: 2ce0d397b12cafcb3e1ac5630bc3fbca61bd6b87
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12014000
System Description: Fedora Linux 40 (KDE Plasma)

Configured using:
 'configure --with-x-toolkit=gtk3 --with-cairo-xcb
 --with-native-compilation=no
 PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/lib/pkgconfig'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS TREE_SITTER WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

Important settings:
  value of $LC_TIME: fr_FR.utf8
  value of $LANG: fr_FR.UTF-8
  locale-coding-system: utf-8-unix



This bug report was last modified 305 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.