Function: string-pixel-width
string-pixel-width is an autoloaded and byte-compiled function defined
in subr-x.el.gz.
Signature
(string-pixel-width STRING)
Documentation
Return the width of STRING in pixels.
Other relevant functions are documented in the string group.
Probably introduced at or before Emacs version 29.1.
Shortdoc
;; string
(string-pixel-width "foo")
=> 3
(string-pixel-width "avocado: 🥑")
=> 11
Aliases
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/subr-x.el.gz
;;;###autoload
(defun string-pixel-width (string)
"Return the width of STRING in pixels."
(if (zerop (length string))
0
;; Keeping a work buffer around is more efficient than creating a
;; new temporary buffer.
(with-current-buffer (get-buffer-create " *string-pixel-width*")
;; If `display-line-numbers' is enabled in internal buffers
;; (e.g. globally), it breaks width calculation (bug#59311)
(setq-local display-line-numbers nil)
(delete-region (point-min) (point-max))
;; Disable line-prefix and wrap-prefix, for the same reason.
(setq line-prefix nil
wrap-prefix nil)
(insert (propertize string 'line-prefix nil 'wrap-prefix nil))
(car (buffer-text-pixel-size nil nil t)))))