Subject: bug#76390: [PATCH] Center lines vertically with line-spacing-vertical-center

From: Daniel Mendler <[email-masked]>
Date: Tue, 18 Feb 2025 16:40:15 +0100

Eli Zaretskii <[email-masked]> writes:

>> Cc: Stefan Kangas <[email-masked]>
>> Date: Tue, 18 Feb 2025 11:41:26 +0100
>> From:  Daniel Mendler via "Bug reports for GNU Emacs,
>>  the Swiss army knife of text editors" <[email-masked]>
>> 
>> A few years ago there was a discussion on emacs-devel started by Jesse
>> Nazario about vertically centering text for line-spacing > 0. A patch
>> was provided back then by Jesse. I hope to revive the discussion and I
>> like to provide an updated patch.
>> 
>> Centering text would be particularly useful if the text has a background
>> color (e.g., hl-line or hi-lock) as demonstrated by the following steps:
>> 
>> 1. (setq-local line-spacing 0.5)
>> 2. (hl-line-mode 1)
>> 3. M-x hi-lock-face-symbol-at-point on a symbol at point
>> 
>> The proposal was to add a new variable `line-spacing-vertical-center' to
>> enable text centering. Maybe it makes sense to use a more general
>> approach which allows finer control over the alignment, e.g., a variable
>> `line-spacing-align' which can be set to a floating point number in the
>> interval [0.0, 1.0]. The value 0.5 means to center the text, 0.0 means
>> to align the text at the top of the line and 1.0 means to align the text
>> at the bottom of the line. Fine control over the alignment allows to to
>> adjust to fine differences of the font and its baseline alignment.
>
> Rather than introducing yet another buffer-local knob, I think I'd
> prefer extending line-spacing to have a cons cell value (with car
> applied to ascent and cdr to descent).  This will probably need to add
> a new field to the iterator struct.

Sounds good. A cons cell for line spacing (ascent . descent) would work.
However there is a small concern regarding backward compatibility since
some code may expect line-spacing to be only a number or nil. However
packages can also be updated and I doubt that there are many which are
affected.

> Also, we should support the same extension for the line-spacing
> property on the newline ending a line (see the node "Line Height" in
> the ELisp manual).

Agree.

> And finally, I have a question: what does this patch do with a line
> that mixes different fonts, especially fonts with very different
> values of ascent and descent?  Does the result look well?  ISTR that
> we need to handle the underline property specially due to these
> considerations, so as to avoid producing broken underline.

I cannot tell yet. I can answer that when I worked a little bit on the
patch. All I can say confidently is that the status quo with the
non-centered line-spacing does not look good, not even with a single
font (see my hi-lock example from above). I am sure that we can do
better in all cases; with a single font or with multiple fonts of
different height.

Daniel