[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: string-width for emojis
From: |
Eli Zaretskii |
Subject: |
Re: string-width for emojis |
Date: |
Fri, 28 Jan 2022 21:15:30 +0200 |
> From: Evgeny Zajcev <lg.zevlg@gmail.com>
> Date: Fri, 28 Jan 2022 19:32:47 +0300
>
> If I evaluate
> (insert "ABCDEF\n"
> "\x2734\xfe0f -> "
> (format "%d" (string-width "\x2734\xfe0f")))
> directly in the scratch buffer, I'll get
> ABCDEF
> ✴️ -> 2
> as output. However, if I evaluate
> (with-current-buffer (get-buffer-create "*help2*")
> (erase-buffer)
> (insert "ABCDEF\n"
> "\x2734\xfe0f -> "
> (format "%d" (string-width "\x2734\xfe0f")))
> (display-buffer (current-buffer)))
> I'll get
> ABCDEF
> ✴️ -> 1
> as output.
I get 1 in both cases.
> Documentation states, that "Width is measured by how many columns it occupies
> on the screen." I see
> visually that ✴️ occupies approximately 2 columns (see screenshot at
> http://lgarc.narod.ru/pics/emacs-string-width.png)
>
> What is the correct use of `string-width` to always get 2 as width for the
> "\x2734\xfe0f" ?
The correct answer is 1, not 2, since U+2434 is a single-column
character:
(char-width #x2734) => 1
That it takes more pixels than a single canonical character depends on
the font used to display it. Note that the doc string warns about
inaccuracies in the returned value, and advises to use
window-text-pixel-size instead.