From: Eli Zaretskii <eliz@gnu.org>
To: David Ponce <da_vid@orange.fr>
Cc: 73129@debbugs.gnu.org
Subject: bug#73129: 31.0.50; buffer-text-pixel-size vs. newlines
Date: Mon, 09 Sep 2024 14:34:38 +0300 [thread overview]
Message-ID: <868qw1vz5d.fsf@gnu.org> (raw)
In-Reply-To: <76135c62-bc5c-4bcd-9caa-ccd9f939d78f@orange.fr> (bug-gnu-emacs@gnu.org)
> Date: Mon, 9 Sep 2024 00:22:11 +0200
> From: David Ponce via "Bug reports for GNU Emacs,
> the Swiss army knife of text editors" <bug-gnu-emacs@gnu.org>
>
> 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 above is the intended behavior, assuming that the default width of
the frame's characters is 12 in your case. The maximum width of the
text "abcd\nef" is 4 characters ("abcd"), which are 12*4 = 48 pixels.
The part after the newline is only 2 characters, so it is narrower,
and does not affect the result.
IOW, buffer-text-pixel-size measures the 2D _dimensions_ of the text,
not is total width.
> The doc string of `buffer-text-pixel-size' mentions:
>
> "Return size of whole text of BUFFER-OR-NAME in WINDOW."
It also says:
The return value is a cons of the maximum pixel-width of any text
line and the pixel-height of all the text lines of the buffer
specified by BUFFER-OR-NAME.
Does the above explain the behavior you see?
This is not a bug.
> 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.
Your expectations are incorrect, see above.
> 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
Yes. If you want the _total_ width of a string, remove the newlines
from it.
I think I understand the source of your confusion, so I have now
clarified these subtle points in the doc strings of
window-text-pixel-size, buffer-text-pixel-size, and
string-pixel-width.
> At least this limitation should be mentioned in the doc string?
I've now done so (and they are not limitations, but conscious design
decisions which have good reasons).
> 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:
Thanks, but that would be the wrong thing. If the caller wants a
total width, the caller should remover the newlines or measure each
substring separately and add the results.
May I ask where and for what purpose did you need to measure pixel
width of a string that included newlines?
next prev parent reply other threads:[~2024-09-09 11:34 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-08 22:22 bug#73129: 31.0.50; buffer-text-pixel-size vs. newlines David Ponce via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-09 11:34 ` Eli Zaretskii [this message]
2024-09-09 12:56 ` David Ponce via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-09-09 13:25 ` Eli Zaretskii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=868qw1vz5d.fsf@gnu.org \
--to=eliz@gnu.org \
--cc=73129@debbugs.gnu.org \
--cc=da_vid@orange.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).