Add new function 'string-pixel-width'

* doc/lispref/display.texi (Size of Displayed Text): Mention it.
* lisp/emacs-lisp/shortdoc.el (string): Mention it.

* lisp/emacs-lisp/subr-x.el (string-pixel-width): New function.
This commit is contained in:
Lars Ingebrigtsen 2021-10-27 15:41:18 +02:00
parent 3fac3120f8
commit 03366de394
5 changed files with 40 additions and 3 deletions

View file

@ -2008,7 +2008,8 @@ The return value is an approximation: it only considers the values
returned by @code{char-width} for the constituent characters, always
takes a tab character as taking @code{tab-width} columns, ignores
display properties and fonts, etc. For these reasons, we recommend
using @code{window-text-pixel-size}, described below, instead.
using @code{window-text-pixel-size} or @code{string-pixel-width},
described below, instead.
@end defun
@defun truncate-string-to-width string width &optional start-column padding ellipsis ellipsis-text-property
@ -2190,6 +2191,11 @@ though when this function is run from an idle timer with a delay of zero
seconds.
@end defun
@defun string-pixel-width string
This is a convenience function that uses @code{window-text-pixel-size}
to compute the width of @var{string} (in pixels).
@end defun
@defun line-pixel-height
This function returns the height in pixels of the line at point in the
selected window. The value includes the line spacing of the line

View file

@ -397,6 +397,12 @@ syntax.
This function returns t if point is on a valid image, and nil
otherwise.
+++
** New function 'string-pixel-width'.
This returns the width of a string in pixels. This can be useful when
dealing with variable pitch fonts and glyphs that have widths that
aren't integer multiples of the default font.
* Changes in Emacs 29.1 on Non-Free Operating Systems

View file

@ -242,7 +242,14 @@ There can be any number of :example/:result elements."
:eval (number-to-string 42))
"Data About Strings"
(length
:eval (length "foo"))
:eval (length "foo")
:eval (length "avocado: 🥑"))
(string-width
:eval (string-width "foo")
:eval (string-width "avocado: 🥑"))
(string-pixel-width
:eval (string-pixel-width "foo")
:eval (string-pixel-width "avocado: 🥑"))
(string-search
:eval (string-search "bar" "foobarzot"))
(assoc-string

View file

@ -441,6 +441,25 @@ is inserted before adjusting the number of empty lines."
((< (- (point) start) lines)
(insert (make-string (- lines (- (point) start)) ?\n))))))
;;;###autoload
(defun string-pixel-width (string)
"Return the width of STRING in pixels."
(with-temp-buffer
(insert string)
(save-window-excursion
(let ((dedicated (window-dedicated-p)))
;; Avoid errors if the selected window is a dedicated one,
;; and they just want to insert a document into it.
(unwind-protect
(progn
(when dedicated
(set-window-dedicated-p nil nil))
(set-window-buffer nil (current-buffer))
(car (window-text-pixel-size
nil (line-beginning-position) (point))))
(when dedicated
(set-window-dedicated-p nil dedicated)))))))
(provide 'subr-x)
;;; subr-x.el ends here

View file

@ -6734,5 +6734,4 @@ string will be displayed only if BODY takes longer than TIMEOUT seconds.
(lambda ()
,@body)))
;;; subr.el ends here