Subject: Re: bug#72771: 31.0.50; shr html renderer throwing "Specified window is not displaying the current buffer"

From: Jim Porter <[email-masked]>
Date: Sat, 24 Aug 2024 10:10:06 -0700

On 8/23/2024 11:08 PM, Eli Zaretskii wrote:
>> Date: Fri, 23 Aug 2024 15:39:00 -0700
>> Cc: Rob Stewart <[email-masked]>, [email-masked]
>> From: Jim Porter <[email-masked]>
>>
>> Here's a patch. I've tested this in a few configurations (in the current
>> window, in a buffer that's not being displayed, in a terminal Emacs) and
>> it all seems to work.
>>
>> One question, Eli: is there a better way than I'm using to get the font
>> that would be used for a character in the buffer? When the buffer is
>> being displayed in a window, '(font-at position window)' works, but that
>> doesn't address this bug, where the buffer isn't displayed. (The font
>> that we get back doesn't have to be 100% accurate; just a good guess
>> should be fine for this case.)
> 
> AFAIU, the code needs the width of the space character of a font used
> to show some text, is that correct?

Close, it needs the "average width", since the goal is to make a pixel 
specification like "(1.23 . width)". Based on my reading of 
'calc_pixel_width_or_height' in xdisp.c, the average width is the value 
the display engine uses for the 'width' unit.

> And the patch solves the problem of font-at by pretending that the
> relevant text is displayed in the current window, is that correct?

Yep. I figure there's a very good chance that the text in question 
(which is already in the current buffer) will soon be displayed in the 
current window, or failing that, a different window in the same frame. 
So it seemed like an ok guess to me. (Also, even if we guess wrong, 
things should usually look fine; it would only fail with certain 
pathological fonts, and even then it would just be a slight visual 
misalignment.)

> Alternatives to the solution in the patch are:

Thanks for the suggestions. (I reordered the list below when replying.)

>   . use the font obtained from (face-font 'default) (or the actual face
>     of the text, if you can get at it easily), like this:
> 
>        (aref (font-info (face-font 'default)) 10)

I think the problem is getting the actual face, which works for simple 
cases via 'get-text-property', but not for more complex ones, e.g. when 
the 'face' property is a list; 'face-font' raises an error in that case. 
Effectively what I want would be a Lisp version of 
'face_at_buffer_position', but that requires a window object anyway, so 
I'm back to the original problem...

>   . temporarily display the buffer in some window (if there is already
>     a window showing the buffer, use with-selected-window)

That could work, though I think it ends up being just as much code 
complexity as my current patch, and it might perform worse with all the 
temporary window-switching...

>   . use buffer-text-pixel-size or string-pixel-width to measure the
>     width of a string of a single SPC character

I think this wouldn't work since I want the average font width, not the 
width of SPC.

In light of the above, I think what I have now might be the best way to 
do it for the time being, unless my comments above gave you another idea 
for an alternative. If not, do you have any objections to me merging 
this patch?