emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 03366de 2/2: Add new function 'string-pixel-width'


From: Lars Ingebrigtsen
Subject: master 03366de 2/2: Add new function 'string-pixel-width'
Date: Wed, 27 Oct 2021 09:41:45 -0400 (EDT)

branch: master
commit 03366de3948225476545d891c584f7d30c497bd0
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    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 92932af..c9a9f7a 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 093c8ac..2106a62 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 8f65437..817dfa6 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 8d6bb19..6f01209 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 86460d9..3967624 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]