GNU bug report logs - #64420
string-width of … is 2 in CJK environments

Previous Next

Package: emacs;

Reported by: Dmitry Gutov <dmitry <at> gutov.dev>

Date: Sun, 2 Jul 2023 12:58:02 UTC

Severity: normal

Full log


Message #14 received at 64420 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Dmitry Gutov <dmitry <at> gutov.dev>
Cc: 64420 <at> debbugs.gnu.org
Subject: Re: bug#64420: string-width of … is 2 in CJK
 environments
Date: Sun, 02 Jul 2023 16:43:04 +0300
> Date: Sun, 2 Jul 2023 16:20:25 +0300
> Cc: 64420 <at> debbugs.gnu.org
> From: Dmitry Gutov <dmitry <at> gutov.dev>
> 
> > On GUI frames Lisp programs that need to know the actual width of some
> > string should use string-pixel-width, not string-width.  The latter is
> > basically only for TTY frames.
> > 
> >     (progn
> >       (set-language-environment "Chinese-BIG5")
> >       (ceiling (/ (string-pixel-width "…")
> >                   (float (default-font-width))))) ;; => 1
> 
> Thank you.
> 
> Is there some inherent reason why string-width differs from the result 
> of the above expression

Because string-width doesn't consult the actual metrics of the font.
It uses a char-table that we set "by hand".

> and especially only does that on CJK?

In CJK locales, most characters are double-width because those locales
use fonts where the glyphs are wider.  Or at least this is the theory.
string-pixel-width is free from these assumptions because it actually
measures the font glyphs.

> Since the overlay-based popup is used on both GUI and Terminal frames, 
> are you suggesting I define my own string-width like this?
> 
> (defun company--string-width (str)
>    (if (display-graphic-p)
>        (ceiling (/ (string-pixel-width str)
>                    (float (default-font-width))))
>      (string-width str)))

Yes, definitely.  (Actually, display-multi-font-p is better than
display-graphic-p, but in practice they will return the same value.)




This bug report was last modified 2 years and 1 day ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.