From 03366de3948225476545d891c584f7d30c497bd0 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Wed, 27 Oct 2021 15:41:18 +0200 Subject: [PATCH] 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. --- doc/lispref/display.texi | 8 +++++++- etc/NEWS | 6 ++++++ lisp/emacs-lisp/shortdoc.el | 9 ++++++++- lisp/emacs-lisp/subr-x.el | 19 +++++++++++++++++++ lisp/subr.el | 1 - 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/doc/lispref/display.texi b/doc/lispref/display.texi index 92932af9ba1..c9a9f7a2d58 100644 --- a/doc/lispref/display.texi +++ b/doc/lispref/display.texi @@ -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 diff --git a/etc/NEWS b/etc/NEWS index 093c8ac9632..2106a62c8f5 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -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 diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 8f654372079..817dfa6b71e 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -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 diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 8d6bb19fd49..6f01209574d 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -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 diff --git a/lisp/subr.el b/lisp/subr.el index 86460d9da6c..39676249cdd 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -6734,5 +6734,4 @@ string will be displayed only if BODY takes longer than TIMEOUT seconds. (lambda () ,@body))) - ;;; subr.el ends here -- 2.39.2