Subject: bug#50440: Emacs does not rescale fonts when display changes DPI

From: Radon Rosborough <[email-masked]>
Date: Mon, 6 Sep 2021 13:04:27 -0700

I run Emacs either on my laptop (1920x1080, 96 DPI) or on my external
monitor (3840x2160, 192 DPI, configured with 200% display scaling so that
applications display identically on both displays, and with my laptop
display disabled).

What I observe is that if I start Emacs on my external monitor and unplug
it (so the display DPI decreases from 192 to 96), the fonts in Emacs appear
to be twice as large, although the window title bar is sized normally and
all other applications scale correctly. Similarly, when I start Emacs on my
laptop screen and then plug in my monitor, the fonts in Emacs appear to be
twice as small, although all other applications scale correctly.

I observed that when I start Emacs on my external monitor, M-x
list-fontsets returns "-CTDB-Fira
Mono-normal-normal-normal-*-29-*-*-*-m-0-fontset-startup", whereas when I
start it on my laptop screen, it returns "-CTDB-Fira
Mono-normal-normal-normal-*-15-*-*-*-m-0-fontset-startup". The fontset does
not change when the underlying display DPI changes, which is presumably
part of the issue.

By inserting some debugging code into Emacs, I saw that x_term_init is only
called once, when Emacs initially starts up. This appears to be where the
DPI is set in the x_display_info struct, and then it is never updated
again. I tried manually invoking x-open-connection from Lisp after
switching displays, which did call x_display_info again and set a new DPI
value. However, this value didn't seem to propagate to FRAME_RES_Y which is
called from font_pixel_size when initializing the fonts, for some reason.
Moreover, it seems like additional changes would be required to get the
default font to be reinitialized even if the underlying x_display_info is
updated.

The only workaround that I know of is to restart Emacs whenever my display
changes DPI. I would be really happy if anyone could suggest a way to tell
Emacs to recompute the default font face without restarting.

In GNU Emacs 28.0.50 (build 17, x86_64-pc-linux-gnu, GTK+ Version 3.24.25,
cairo version 1.16.0)
 of 2021-09-06 built on wit
Repository revision: dd4be8ad3b0cbcd701a5d5f7221a7e4c2a69d6be
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Pop!_OS 21.04

Configured using:
 'configure --with-json --with-native-compilation --with-xwidgets
 --with-x'

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

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
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  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
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
rfc822 mml mml-sec epa derived epg rfc6068 epg-config gnus-util rmail
rmail-loaddefs auth-source cl-seq eieio eieio-core cl-macs
eieio-loaddefs password-cache json map 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 cl-loaddefs
cl-lib sendmail rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils
iso-transl 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 easymenu timer select scroll-bar mouse jit-lock
font-lock syntax 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 button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads
xwidget-internal dbusbind inotify lcms2 dynamic-setting
system-font-setting font-render-setting cairo move-toolbar gtk x-toolkit
x multi-tty make-network-process native-compile emacs)

Memory information:
((conses 16 66907 8302)
 (symbols 48 6630 0)
 (strings 32 19284 1796)
 (string-bytes 1 657106)
 (vectors 16 13452)
 (vector-slots 8 287737 11209)
 (floats 8 22 31)
 (intervals 56 207 0)
 (buffers 992 10))