Re: [PATCH] emacs: fix author field width with double-width chars

Subject: Re: [PATCH] emacs: fix author field width with double-width chars

Date: Wed, 2 Jul 2025 22:10:10 +0800

To: David Bremner

Cc: notmuch@notmuchmail.org

From: 赵一开


On Wed, Jul 2, 2025 at 7:19 PM David Bremner <david@tethera.net> wrote:
>
> 赵一开 <yikai@z1k.dev> writes:
>
> > Hi,
> >
> > This can be easily reproduced if some emails have a long non-ascii
> > sender name. See attached for such one. (You may need to install a
> > Chinese font. e.g. Noto CJK font).
> >
>
> Thank you for the test data. For me your fix works in "emacs -nw", but
> not in the emacs gtk GUI frame. In that case it seems to truncate more
> than needed (see figure at the end). I am using emacs-30.1 on Debian.

You've come across the most common issue that every new CJK user
struggles with :)

The issue in your screenshot is that the CJK font together with the
ASCII font is not monospaced.
You should try using Noto CJK Mono as the default font in emacs (apply
to both CJK characters and ASCII characters), with e.g.
(set-face-attribute 'default nil :family "Noto Sans Mono CJK KR").
Sorry I wasn't specific enough for this earlier.

Since CJK characters are wider than ASCII characters and defined as
"double-width" in unicode, although CJK chars themselves are always
monospaced, they should be exactly 2x the width of the ASCII chars to
make the whole buffer properly aligned. However, sometimes CJK chars
and ASCII chars are provided by different fonts, so the user is
responsible to make sure of that. The easy way is to use one single
monospaced font for all chars (e.g. "Noto Sans Mono CJK" mentioned
above), or alternatively there are certain known compatible font
pairs, for example, IIRC "Fira Code" and "Source Code Pro" works well
with "Noto Sans CJK".

The terminal emulator however, being monospace natively, usually
ensures that CJK characters take exactly two columns. That's why it
works in "emacs -nw".

I believe the same alignment issue would appear without CJK chars if
the user chose a non-monospaced ASCII font in GUI emacs. That's why I
think this patch works as intended. Technically I think there's a way
to support such cases: we can use `string-pixel-width` to calculate
the width in pixels and use `(propertize " " 'display `(space :width
w))` to insert spaces in pixels. Another time maybe.

> I don't know very much about CJK fonts, but according to emacs, the font
> for the Chinese characters is
>
>   ftcrhb:-GOOG-Noto Sans CJK KR-regular-normal-normal-*-15-*-*-*-*-0-iso10646-1 (#x2509)
>
> It seems odd to me that emacs would choose a "Korean" font, maybe that
> is the problem?

That's not a problem. It's complex.. apparently C J K characters are
slightly different but shares most unicode codepoints, it's the OS's
responsibility to choose proper font based on locale settings etc. But
the difference is minimal and it does not matter in this case.

> I checked and I have the Debian packages "fonts-noto-cjk" and "fonts-noto-cjk-extra" installed.
>
> I tested in ./devel/try-notmuch-mua -Q
>
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: