Display certain glyphs with paddings on the side

classic Classic list List threaded Threaded
17 messages Options
Reply | Threaded
Open this post in threaded view
|

Display certain glyphs with paddings on the side

Fu Yuan
Aligning ACSII characters with Chinese characters in Emacs has been a pain for me. The common solution (among Chinese community) is to scale Chinese characters with `face-font-rescale-alist’ and make one Chinese character precisely twice as wide as a ASCII character. However, that makes the Chinese characters taller than the ASCII characters. Is it possible to add some padding to the sides of a Chinese character and make it twice as wide in Emacs?

Yuan
Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Eli Zaretskii
> From: Yuan Fu <[hidden email]>
> Date: Sat, 30 Nov 2019 14:51:29 -0500
>
> Aligning ACSII characters with Chinese characters in Emacs has been a pain for me. The common solution (among Chinese community) is to scale Chinese characters with `face-font-rescale-alist’ and make one Chinese character precisely twice as wide as a ASCII character. However, that makes the Chinese characters taller than the ASCII characters. Is it possible to add some padding to the sides of a Chinese character and make it twice as wide in Emacs?

I don't think we have any such features.  You will have to choose your
fonts such that they provide this nice alignment.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Byung-Hee HWANG (황병희)
In reply to this post by Fu Yuan
Yuan Fu <[hidden email]> writes:

> Aligning ACSII characters with Chinese characters in Emacs has been a
> pain for me. The common solution (among Chinese community) is to scale
> Chinese characters with `face-font-rescale-alist’ and make one Chinese
> character precisely twice as wide as a ASCII character. However, that
> makes the Chinese characters taller than the ASCII characters. Is it
> possible to add some padding to the sides of a Chinese character and
> make it twice as wide in Emacs?

Or, you would be using Google Noto CJK font.

Below is example for Google Noto CJK font:
#+BEGIN_SRC text
HANGANG 漢江 한강
#+END_SRC

The screenshot here:
https://gitlab.com/soyeomul/Gnus/raw/master/ss/Screenshot_from_2019-12-03_03-28-35.png

Sincerely,

--
^고맙습니다 _地平天成_ 감사합니다_^))//

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Richard Stallman
In reply to this post by Eli Zaretskii
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  >> Is it possible to add some padding
  >> to the sides of a Chinese character and make it twice as wide in
  >> Emacs?

  > I don't think we have any such features.  You will have to choose your
  > fonts such that they provide this nice alignment.

I don't think it would be terribly hard to implement such a feature,
as features go.


--
Dr Richard Stallman
Founder, Free Software Foundation (https://gnu.org, https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)



Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Eli Zaretskii
> From: Richard Stallman <[hidden email]>
> Cc: [hidden email], [hidden email]
> Date: Mon, 02 Dec 2019 23:56:22 -0500
>
>   >> Is it possible to add some padding
>   >> to the sides of a Chinese character and make it twice as wide in
>   >> Emacs?
>
>   > I don't think we have any such features.  You will have to choose your
>   > fonts such that they provide this nice alignment.
>
> I don't think it would be terribly hard to implement such a feature,
> as features go.

I'll welcome patches to that effect, of course.

Personally, I don't think I have a clear idea what is the feature we
should implement.  First, how does users tell Emacs they want this to
happen?  Do we introduce some buffer-local variable or a minor mode?

Next, what exactly should the feature do? override the width of each
glyph with an integral multiple of the width of the default face's
font?  If so, what to do when the default face's font is not a
fixed-pitch font?  And should we do the same if the font to be widened
is also larger in height (i.e. we are talking about something like
chapter and section headings in Info)?

IOW, I think we need first to come up with a detailed specification,
so that we have a clear idea of what needs to be implemented.  I
encourage people to file a feature request bug report on debbugs.


Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Fu Yuan

> I'll welcome patches to that effect, of course.
>
> Personally, I don't think I have a clear idea what is the feature we
> should implement.  First, how does users tell Emacs they want this to
> happen?  Do we introduce some buffer-local variable or a minor mode?
>
> Next, what exactly should the feature do? override the width of each
> glyph with an integral multiple of the width of the default face's
> font?  If so, what to do when the default face's font is not a
> fixed-pitch font?  And should we do the same if the font to be widened
> is also larger in height (i.e. we are talking about something like
> chapter and section headings in Info)?
>
> IOW, I think we need first to come up with a detailed specification,
> so that we have a clear idea of what needs to be implemented.  I
> encourage people to file a feature request bug report on debbugs.
>

I don’t think I’m skilled enough to provide a patch, but I will think about this feature and report a more detailed feature request.

Thanks,
Yuan


Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Fu Yuan
In reply to this post by Byung-Hee HWANG (황병희)
>
> Or, you would be using Google Noto CJK font.
>
> Below is example for Google Noto CJK font:
> #+BEGIN_SRC text
> HANGANG 漢江 한강
> #+END_SRC

Thanks. But I don’t see the alignment between the CJK glyph and ASCII glyph in your screenshot. What effects are you achieving by using Google Noto CJK font?

Yuan
Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Clément Pit-Claudel
In reply to this post by Eli Zaretskii
On 2019-12-02 11:24, Eli Zaretskii wrote:
>> From: Yuan Fu <[hidden email]>
>> Date: Sat, 30 Nov 2019 14:51:29 -0500
>>
>> Aligning ACSII characters with Chinese characters in Emacs has been a pain for me. The common solution (among Chinese community) is to scale Chinese characters with `face-font-rescale-alist’ and make one Chinese character precisely twice as wide as a ASCII character. However, that makes the Chinese characters taller than the ASCII characters. Is it possible to add some padding to the sides of a Chinese character and make it twice as wide in Emacs?
>
> I don't think we have any such features.  You will have to choose your
> fonts such that they provide this nice alignment.

This is the approach that I've been using in a different context.  I use programming languages with heavy reliane on mathematical symbols, so I have patched versions of mathematical fonts to ensure that symbols line up nicely with monospace characters.  The python code that does these transformations is here: https://github.com/cpitclaudel/monospacifier

I think Emacs might have most of the features needed already as part of the composition engine, but applying the right composition properties to each character would be costly and complicated.

Clément.




Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Eli Zaretskii
> From: Clément Pit-Claudel <[hidden email]>
> Date: Tue, 3 Dec 2019 11:42:03 -0500
>
> I think Emacs might have most of the features needed already as part of the composition engine

No, character composition does nothing to the width of glyphs on
display.  We take the width from the glyph metrics reported by the
font.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Yuri Khan-2
On Wed, 4 Dec 2019 at 01:13, Eli Zaretskii <[hidden email]> wrote:

> > I think Emacs might have most of the features needed already as part of the composition engine
>
> No, character composition does nothing to the width of glyphs on
> display.  We take the width from the glyph metrics reported by the
> font.

I use composition in prettify-symbols-mode to render a symbol in a
two-space field, e.g. to visualize a ‘!=’ sequence as a '(?\s (Br .
Bl) ?\s (Bc . Bc) ?≠) composition. Pretty sure that any CJK glyph, if
its width is less than two spaces, can also be padded that way. No
idea how to arrange for the compositions to be used for all CJK
characters though.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Eli Zaretskii
> From: Yuri Khan <[hidden email]>
> Date: Wed, 4 Dec 2019 01:36:05 +0700
> Cc: Clément Pit-Claudel <[hidden email]>,
> Emacs developers <[hidden email]>
>
> On Wed, 4 Dec 2019 at 01:13, Eli Zaretskii <[hidden email]> wrote:
>
> I use composition in prettify-symbols-mode to render a symbol in a
> two-space field, e.g. to visualize a ‘!=’ sequence as a '(?\s (Br .
> Bl) ?\s (Bc . Bc) ?≠) composition. Pretty sure that any CJK glyph, if
> its width is less than two spaces, can also be padded that way. No
> idea how to arrange for the compositions to be used for all CJK
> characters though.

That's easy, but how do you know which glyphs need to be padded and by
how much?

And anyway, doing layout from Lisp is generally not a good idea: at
best you will have slow and fragile layout.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Clément Pit-Claudel
In reply to this post by Eli Zaretskii
On 2019-12-03 12:38, Eli Zaretskii wrote:
>> From: Clément Pit-Claudel <[hidden email]>
>> Date: Tue, 3 Dec 2019 11:42:03 -0500
>>
>> I think Emacs might have most of the features needed already as part of the composition engine
>
> No, character composition does nothing to the width of glyphs on
> display.  We take the width from the glyph metrics reported by the
> font.

For the OP's purpose, I think character composition is "enough", in the sense that it can be used to display a glyphs with padding on the sides (as far as I know, composition can't be used to shrink a glyph, however).

Concretely, I'm think of this:

  (insert (compose-string "a" 0 1 '(?\s (br . bl) ?\s (bc . bc) ?我)))

If I'm reading the spec correctly, this should draw the character 我 with padding on both sides to reach the width of two spaces (assuming the choice of fonts is such as 我 is narrower than two spaces to being with).

Clément.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Eli Zaretskii
> Cc: [hidden email]
> From: Clément Pit-Claudel <[hidden email]>
> Date: Tue, 3 Dec 2019 13:43:33 -0500
>
> > No, character composition does nothing to the width of glyphs on
> > display.  We take the width from the glyph metrics reported by the
> > font.
>
> For the OP's purpose, I think character composition is "enough", in the sense that it can be used to display a glyphs with padding on the sides (as far as I know, composition can't be used to shrink a glyph, however).
>
> Concretely, I'm think of this:
>
>   (insert (compose-string "a" 0 1 '(?\s (br . bl) ?\s (bc . bc) ?我)))
>
> If I'm reading the spec correctly, this should draw the character 我 with padding on both sides to reach the width of two spaces (assuming the choice of fonts is such as 我 is narrower than two spaces to being with).

This isn't really workable in practice.  First, you'd need to do this
for every CJK character, which means redisplay will be much slower for
them (since character composition calls Lisp from the display engine,
and will have to references a huge char-table).  Second, not all CJK
characters need to be padded, and you have no easy way of knowing that
from Lisp, certainly no fast way.  And third, you assume that the
width of the space is identical to the width of other characters,
which may be true for many fonts, but not for all of them.

IOW, as I said in another message, doing layout in Lisp will get you
rotten layout.  It is only "good enough" for simplistic subsets of the
real-life problems, IME.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Clément Pit-Claudel
On 2019-12-03 14:13, Eli Zaretskii wrote:

>> Cc: [hidden email]
>> From: Clément Pit-Claudel <[hidden email]>
>> Date: Tue, 3 Dec 2019 13:43:33 -0500
>>
>>> No, character composition does nothing to the width of glyphs on
>>> display.  We take the width from the glyph metrics reported by the
>>> font.
>>
>> For the OP's purpose, I think character composition is "enough", in the sense that it can be used to display a glyphs with padding on the sides (as far as I know, composition can't be used to shrink a glyph, however).
>>
>> Concretely, I'm think of this:
>>
>>   (insert (compose-string "a" 0 1 '(?\s (br . bl) ?\s (bc . bc) ?我)))
>>
>> If I'm reading the spec correctly, this should draw the character 我 with padding on both sides to reach the width of two spaces (assuming the choice of fonts is such as 我 is narrower than two spaces to being with).
>
> This isn't really workable in practice.

Sorry, my point wasn't that the OP should do that; rather, I was pointing out that the display engine was powerful enough to achieve the desired result.

Ideally, what I'd love to have is a mode that implements a grid-like layout, where each character occupies an exact multiple of the base character width, regardless of which font it comes from.

Deciding what this multiple should be for each character could be done in various ways; for my purposes (and for OP's case as well, I think), it would be enough to use one cell for halfwidth characters, two for fullwidth characters, and add a few exceptions for things like TAB and zero-width spaces (I'm using the terms fullwidth and halfwidth in the sense of https://www.unicode.org/reports/tr11/).

Should I open a bug report about this?

Cheers,
Clément.

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Byung-Hee HWANG (황병희)
In reply to this post by Fu Yuan
Hello, Yuan^^^

> [...]  What effects are you achieving by using
> Google Noto CJK font?

I would say that your case will be resolving by font change.
And Google Noto CJK font is better than other font.

Sincerely,


--
^고맙습니다 _地平天成_ 감사합니다_^))//

Screenshot from 2019-12-04 07-32-08.png (527K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Fu Yuan
In reply to this post by Clément Pit-Claudel

Ideally, what I'd love to have is a mode that implements a grid-like layout, where each character occupies an exact multiple of the base character width, regardless of which font it comes from.

Deciding what this multiple should be for each character could be done in various ways; for my purposes (and for OP's case as well, I think), it would be enough to use one cell for halfwidth characters, two for fullwidth characters, and add a few exceptions for things like TAB and zero-width spaces (I'm using the terms fullwidth and halfwidth in the sense of https://www.unicode.org/reports/tr11/).


Clément expressed the idea better than I could have. However, I’d like to see it as a face attribute instead of a mode. (Because I want to align my org table). Maybe there could be a face attribute (:grid WIDTH) that instructs the display engine to pad each glyph to have width that is a multiple of WIDTH in pixel, and if WIDTH is t, default to “base character width”?

I remembered that ‘window-width’ gives width in char widths and had a look at its source. It knows the character width from FRAME_COLUMN_WIDTH; the comment says the value currently equals to the average width of the default font of the frame. I think this value can be used as the “base character width”.

Yuan

Reply | Threaded
Open this post in threaded view
|

Re: Display certain glyphs with paddings on the side

Eli Zaretskii
In reply to this post by Clément Pit-Claudel
> Cc: [hidden email]
> From: Clément Pit-Claudel <[hidden email]>
> Date: Tue, 3 Dec 2019 15:00:19 -0500
>
> Should I open a bug report about this?

Yes, please.  As I said, we need a more detailed specification for how
the feature will be defined and used.