[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Add a function that returns pixel distance between points?
From: |
Yuan Fu |
Subject: |
Re: Add a function that returns pixel distance between points? |
Date: |
Sun, 31 Jan 2021 14:42:27 -0500 |
> On Jan 31, 2021, at 12:52 AM, Ihor Radchenko <yantar92@gmail.com> wrote:
>
> 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
Thanks, I’ll try that and see if it is fast enough for my use :-)
Yuan