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 &optional BUFFER)
Documentation
Return the width of STRING in pixels.
If BUFFER is non-nil, use the face remappings, alternative and default properties from that buffer when determining the width. If you call this function to measure pixel width of a string with embedded newlines, it returns the width of the widest substring that does not include newlines.
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
Source Code
;; Defined in /usr/src/emacs/lisp/emacs-lisp/subr-x.el.gz
;;;###autoload
(defun string-pixel-width (string &optional buffer)
"Return the width of STRING in pixels.
If BUFFER is non-nil, use the face remappings, alternative and default
properties from that buffer when determining the width.
If you call this function to measure pixel width of a string
with embedded newlines, it returns the width of the widest
substring that does not include newlines."
(declare (important-return-value t))
(if (zerop (length string))
0
;; Keeping a work buffer around is more efficient than creating a
;; new temporary buffer.
(with-work-buffer
(work-buffer--prepare-pixelwise string buffer)
(car (buffer-text-pixel-size nil nil t)))))