[Top][All Lists]

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

Re: width of propertized string

From: David Reitter
Subject: Re: width of propertized string
Date: Fri, 6 Jun 2008 09:40:05 +0100

On 5 Jun 2008, at 20:47, Stefan Monnier wrote:

Given a propertized string, how would one find out what its (pixel)
dimensions are going to be?

Display it, measure its size.

Tried that (see code below). This is too slow because we need to switch to the temporary buffer.

All of my use cases concern displaying things in the echo area or the header line. Thus, I would want to ignore any font locking and other automatically applied properties.

My use cases are:

- adding appropriate spacing and justifying buttons in the header line
- word-wrapping text, primarily in the echo area before displaying it.

(defun visual-pixel-col-at-point ()
  (car-safe (pos-visible-in-window-p (point) nil 'partial))

(defun string-pixel-width (text)
  (let ((width))
    (let ((old-buffer (current-buffer)))
          (with-temp-buffer ;; should be in fundamental-mode
            ;; this is way too slow.
            (switch-to-buffer (current-buffer) t)
            (let ((buffer-undo-list nil) (start (point))
                  (truncate-lines t) (after-change-functions nil))
              (let ((left (visual-pixel-col-at-point)))
                (insert text)
                (setq width (- (visual-pixel-col-at-point) left)))
              (delete-region start (point)))
            (switch-to-buffer old-buffer t)))

reply via email to

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