1

Given a face using a proportional font, can an accurate pixel width of a single row of text be measured in Emacs?

Note: not monospaced text.

  • Looks like (- (car (pos-visible-in-window-p (line-end-position) nil t)) (car (pos-visible-in-window-p (line-beginning-position) nil t))) should do that. The given example measures the width of the current line. The beginning and the end of line must be visible. The above version is not foolproof yet. See the documentation for pos-visible-in-window-p. Maybe, there is a better way. – Tobias May 3 '15 at 7:13
  • My emacs-version is 24.3.1. The last section of the help for pos-visible-in-window-p states that the function returns the position in pixels if PARTIALLY is non-nil. – Tobias May 3 '15 at 7:19
  • 1
    Unfortunately for my needs, I need to do this on text that isn't visible. But this answers the question I posed, so please repost the original comment as an answer so I can mark it as correct. – ocodo May 3 '15 at 7:22
  • I'd prefer to use your one. – ocodo May 4 '15 at 0:17
1

In the Emacs 24.4+ Manual Section 37.10 Size of Displayed Text

The following function returns the size in pixels of text as if it were displayed in a given window.

Function: window-text-pixel-size &optional window from to x-limit y-limit mode-and-header-line

This function returns the size of the text of window’s buffer in pixels. window must be a live window and defaults to the selected one. The return value is a cons of the maximum pixel-width of any text line and the maximum pixel-height of all text lines.

The optional argument from, if non-nil, specifies the first text position to consider and defaults to the minimum accessible position of the buffer. If from is t, it uses the minimum accessible position that is not a newline character. The optional argument to, if non-nil, specifies the last text position to consider and defaults to the maximum accessible position of the buffer. If to is t, it uses the maximum accessible position that is not a newline character.

See the documentation for full details on the other parameters.

Please Note: window-text-pixel-size is a function introduced in emacs 24.4

  • Just a note: window-text-pixel-size is a function introduced in emacs 24.4. It does not yet exist in emacs 24.3.1 shipped with Ubuntu 14.04 LTS. – Tobias May 3 '15 at 16:08
  • That's ok, I'm only interested in the current release of Emacs – ocodo May 3 '15 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.