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: Yuan Fu
Subject: Re: Add a function that returns pixel distance between points?
Date: Mon, 1 Feb 2021 18:00:18 -0500


> On Feb 1, 2021, at 1:22 PM, Eli Zaretskii <eliz@gnu.org> wrote:
> 
>> From: Yuan Fu <casouri@gmail.com>
>> Date: Mon, 1 Feb 2021 09:16:52 -0500
>> Cc: "emacs-devel\\@gnu.org" <emacs-devel@gnu.org>
>> 
>>> IOW, can you show a test case where using window-text-pixel-size
>>> returns incorrect results for :align-to due to line-prefix?
>> 
>> Here is an example:
>> 
>> (let ((width))
>>  (insert "woome")
>>  (setq width (car (window-text-pixel-size
>>                    nil (line-beginning-position) (point))))
>>  (put-text-property (line-beginning-position) (point)
>>                     'line-prefix "   ")
>>  (message "true width: %d returned width: %d"
>>           width (car (window-text-pixel-size
>>                       nil (line-beginning-position) (point)))))
>> 
>> Run it in an empty buffer and it will print the expected width and returned 
>> width. In the snippet I’m trying to measure the width of the text.
> 
> This says:
> 
>  true width: 40 returned width: 64
> 
> However, if I use :align-to with the value of 64 pixels, the stretch
> of whitespace ends where I'd expect it to end: aligned to right after
> "woome".
> 
> So I still don't understand the problem you are facing.  Which is why
> I asked for an example code which uses :align-to in a way similar to
> what you intend to do in your table rendering code, because I wanted
> to see a situation where :align-to produces incorrect results when
> using the return value of window-text-pixel-size in the presence of
> line-prefix.  But the example you show doesn't use :align-to.

Ah, I see. Here is a demo that should show that.

(let (width-1 width-2 (start (point)))
  (insert (propertize "| loooooong |\n| world |"
                      'line-prefix "     "))
  (goto-char start)
  (search-forward "|")
  (let ((start (point)))
    (search-forward "|")
    (setq width-1 (car (window-text-pixel-size
                        nil (1+ start) (- (point) 2)))))
  (search-forward "|")
  (let ((start (point)))
    (search-forward "|")
    (setq width-2 (car (window-text-pixel-size
                        nil (1+ start) (- (point) 2)))))
  (let ((col-width (+ 2 (max width-1 width-2)))
        cell-start)
    (goto-char start)
    (search-forward "|")
    (setq cell-start (car (window-text-pixel-size
                           nil (line-beginning-position) (point))))
    (search-forward "|")
    (put-text-property (- (point) 2) (1- (point))
                       'display
                       `(space :align-to (,(+ cell-start col-width))))
    (search-forward "|")
    (setq cell-start (car (window-text-pixel-size
                           nil (line-beginning-position) (point))))
    (search-forward "|")
    (put-text-property (- (point) 2) (1- (point))
                       'display
                       `(space :align-to (,(+ cell-start col-width))))))

This code inserts a mini table

| looooong |
| world |

And tries to aligns it to

| looooong |
| world    |

You will notice there are extra white space on the right of the cells, and the 
table actually looks like this:

| loooooong    |
| world        |

That’s because window-text-pixel-size returned a width that’s larger than the 
true with of the cells. If you enlarge the line-prefix, the extra space grows.

Yuan


reply via email to

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