Subject: bug#40097: 28.0.50; Preferred font ignored for specific charset

From: Sergey Organov <[email-masked]>
Date: Tue, 17 Mar 2020 07:31:02 +0300

Hello,

[Note: this has been originally observed in GNU Emacs 26.1 running on
Debian Buster GNU/Linux, and then reproduced on the latest Emacs
snapshot, 28.0.50.]

When there is some particular charset property on text, Emacs chooses
to render it using font that has matching encoding, such as:

x:-xos4-terminus-medium-r-normal--16-160-72-72-c-80-microsoft-cp1251 (#xEF)

for windows-1251 charset rather than the default font:

ftcrhb:-PfEd-DejaVu Sans Mono-normal-normal-normal-*-15-*-*-*-m-0-iso10646-1

even though the default (unicode) font does support corresponding
characters.

This behavior results in rather unpleasant mixture of fonts.

To reproduce this starting with emacs -Q, evaluate this form:

(let ((buf (get-buffer-create "test encodings")))
  (with-current-buffer buf
    (erase-buffer)
    (insert "Encoding windows-1251: "
            (propertize "привет\n" 'charset 'windows-1251))
    (insert "Encoding      unicode: "
            (propertize "привет\n" 'charset 'unicode)))
  (switch-to-buffer-other-window buf))

Be warned that it's known not to be reproducible on at least some
systems. What I see in "emacs -Q" is attached as Emacs window snapshot.
Please notice how two strings look very different where encodings
differ.

Here are outputs of C-u C-x = being pressed on 2 differing texts:

--- >8 ---
             position: 24 of 60 (38%), column: 23
            character: п (displayed as п) (codepoint 1087, #o2077, #x43f)
              charset: windows-1251 (WINDOWS-1251 (Cyrillic))
code point in charset: 0xEF
               script: cyrillic
               syntax: w 	which means: word
             category: .:Base, L:Left-to-right (strong), Y:2-byte Cyrillic, c:Chinese, h:Korean, j:Japanese, y:Cyrillic
             to input: type "C-x 8 RET 43f" or "C-x 8 RET CYRILLIC SMALL LETTER PE"
          buffer code: #xD0 #xBF
            file code: #xD0 #xBF (encoded by coding system utf-8-unix)
              display: by this font (glyph code)
    x:-xos4-terminus-medium-r-normal--16-160-72-72-c-80-microsoft-cp1251 (#xEF)

Character code properties: customize what to show
  name: CYRILLIC SMALL LETTER PE
  general-category: Ll (Letter, Lowercase)
  decomposition: (1087) ('п')

There are text properties here:
  charset              windows-1251
--- >8 ---
             position: 54 of 60 (88%), column: 23
            character: п (displayed as п) (codepoint 1087, #o2077, #x43f)
              charset: unicode (Unicode (ISO10646))
code point in charset: 0x043F
               script: cyrillic
               syntax: w 	which means: word
             category: .:Base, L:Left-to-right (strong), Y:2-byte Cyrillic, c:Chinese, h:Korean, j:Japanese, y:Cyrillic
             to input: type "C-x 8 RET 43f" or "C-x 8 RET CYRILLIC SMALL LETTER PE"
          buffer code: #xD0 #xBF
            file code: #xD0 #xBF (encoded by coding system utf-8-unix)
              display: by this font (glyph code)
    ftcrhb:-PfEd-DejaVu Sans Mono-normal-normal-normal-*-15-*-*-*-m-0-iso10646-1 (#x37E)

Character code properties: customize what to show
  name: CYRILLIC SMALL LETTER PE
  general-category: Ll (Letter, Lowercase)
  decomposition: (1087) ('п')

There are text properties here:
  charset              unicode
--- >8 ---

For reference, here is a link to original report/discussion:

https://lists.gnu.org/archive/html/help-gnu-emacs/2020-03/msg00049.html

In GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.5, cairo version 1.16.0)
 of 2020-03-08, unofficial emacs-snapshot build: http://emacs.ganneff.de/, git commit 0a3f8da6e1a56ada409cf1677ac40fcc75a8a33c built on runner-19980c3f-project-26-concurrent-0
Repository revision: d01cf197911a365e4422a5561a0cd77fed4d8fc3
Repository branch: HEAD
Windowing system distributor 'The X.Org Foundation', version 11.0.12004000
System Description: Debian GNU/Linux 10 (buster)

Recent messages:
For information about GNU Emacs and the GNU system, type C-h C-a.
Defining kbd macro...
Keyboard macro defined
completion--do-completion: Keyboard macro terminated by a command ringing the bell
Quit
#<buffer test encodings>
Configured using:
 'configure --build x86_64-linux-gnu --prefix=/usr
 --sharedstatedir=/var/lib --libexecdir=/usr/lib
 --localstatedir=/var/lib --infodir=/usr/share/info
 --mandir=/usr/share/man --with-pop=yes
 --enable-locallisppath=/etc/emacs-snapshot:/etc/emacs:/usr/local/share/emacs/28.0.50/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/28.0.50/site-lisp:/usr/share/emacs/site-lisp
 --build x86_64-linux-gnu --prefix=/usr --sharedstatedir=/var/lib
 --libexecdir=/usr/lib --localstatedir=/var/lib
 --infodir=/usr/share/info --mandir=/usr/share/man --with-pop=yes
 --enable-locallisppath=/etc/emacs-snapshot:/etc/emacs:/usr/local/share/emacs/28.0.50/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/28.0.50/site-lisp:/usr/share/emacs/site-lisp
 --with-x=yes --with-x-toolkit=gtk3 --with-toolkit-scroll-bars
 'CFLAGS=-g -O2
 -fdebug-prefix-map=/builds/joerg/emacs/buster_amd64/emacs-snapshot-20200308+emacs-27.0.90-434-g0a3f8da6e1=. -fstack-protector-strong
 -Wformat -Werror=format-security -Wall -fno-omit-frame-pointer'
 'CPPFLAGS=-Wdate-time -D_FORTIFY_SOURCE=2' LDFLAGS=-Wl,-z,relro'

Configured features:
XPM JPEG TIFF GIF PNG RSVG CAIRO SOUND GPM DBUS GSETTINGS GLIB NOTIFY
INOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF
ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM MODULES THREADS PDUMPER LCMS2
GMP

Important settings:
  value of $LC_MONETARY: en_US.UTF-8
  value of $LC_NUMERIC: en_US.UTF-8
  value of $LC_TIME: en_US.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
format-spec rfc822 mml easymenu mml-sec password-cache epa derived epg
epg-config gnus-util rmail rmail-loaddefs text-property-search time-date
subr-x seq byte-opt gv bytecomp byte-compile cconv mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils kmacro
cl-loaddefs cl-lib tooltip eldoc electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel term/x-win x-win term/common-win x-dnd tool-bar
dnd fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
cairo move-toolbar gtk x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 45267 8851)
 (symbols 48 6071 1)
 (strings 32 15735 1935)
 (string-bytes 1 511300)
 (vectors 16 9543)
 (vector-slots 8 130234 10600)
 (floats 8 25 44)
 (intervals 56 261 6)
 (buffers 1000 13))