emacs-devel
[Top][All Lists]
Advanced

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

Re: Add a function that returns pixel distance between points?


From: Ihor Radchenko
Subject: Re: Add a function that returns pixel distance between points?
Date: Sun, 31 Jan 2021 13:52:05 +0800

Yuan Fu <casouri@gmail.com> writes:

> When the text is displayed in variable-pitch font, the bars aren’t aligned. 
> I’m using display property (space :align-to) to align those bars. To know 
> with pixel position to align to, I need to calculate each column’s pixel 
> width, and met the difficulties mentioned earlier.

You can do it in temp buffer with line numbers and wrap prefix disabled.
Something like

(defun string-display-pixel-width (string &optional mode)
  "Calculate pixel width of STRING.
Optional MODE specifies major mode used for display."
  (with-temp-buffer
    (with-silent-modifications
      (setf (buffer-string) string))
    (when (fboundp mode)
      (funcall mode)
      (font-lock-fontify-buffer))
    (let (wrap-prefix display-line-numbers)
      (if (get-buffer-window (current-buffer))
          (car (window-text-pixel-size nil (line-beginning-position) (point)))
        (set-window-buffer nil (current-buffer))
        (car (window-text-pixel-size nil (line-beginning-position) (point)))))))

Best,
Ihor



reply via email to

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