GNU bug report logs - #50440
Emacs does not rescale fonts when display changes DPI

Previous Next

Package: emacs;

Reported by: Radon Rosborough <radon.neon <at> gmail.com>

Date: Mon, 6 Sep 2021 20:06:01 UTC

Severity: normal

To reply to this bug, email your comments to 50440 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#50440; Package emacs. (Mon, 06 Sep 2021 20:06:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Radon Rosborough <radon.neon <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 06 Sep 2021 20:06:02 GMT) Full text and rfc822 format available.

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

From: Radon Rosborough <radon.neon <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: Emacs does not rescale fonts when display changes DPI
Date: Mon, 6 Sep 2021 13:04:27 -0700
[Message part 1 (text/plain, inline)]
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))
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50440; Package emacs. (Tue, 07 Sep 2021 05:25:01 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: Radon Rosborough <radon.neon <at> gmail.com>
Cc: 50440 <at> debbugs.gnu.org
Subject: Re: bug#50440: Emacs does not rescale fonts when display changes DPI
Date: Tue, 07 Sep 2021 08:24:36 +0300
> From: Radon Rosborough <radon.neon <at> gmail.com>
> 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.

We call x_term_init each time a frame is created for the first time on
a new display.  So potentially make-frame-on-display could call it.

One other thing to try is to start Emacs as a daemon, and when you
switch displays, close all the client frames and make new ones via
emacsclient.

Does any of the above allow you to solve the issue?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#50440; Package emacs. (Tue, 07 Sep 2021 20:48:02 GMT) Full text and rfc822 format available.

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

From: Radon Rosborough <radon.neon <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 50440 <at> debbugs.gnu.org
Subject: Re: bug#50440: Emacs does not rescale fonts when display changes DPI
Date: Tue, 7 Sep 2021 13:47:08 -0700
[Message part 1 (text/plain, inline)]
> potentially make-frame-on-display could call it

I haven't tried it yet, but I suspect this may not work: the reason is that
DISPLAY is actually the same before and after; it's ":1" in both cases,
despite the DPI being different between the two displays. (Is this the root
cause of the bug?)

> Does any of the above allow you to solve the issue?

After my previous email, I found a workaround which is clearly a hack but
works well enough to satisfy me: changing the size of the default font
appears to be able to cancel out the wrong DPI more or less visually
identically. The code looks like this:

(defun radian-local-make-it-bigger ()
  (interactive)
  (set-face-attribute 'default nil :height (* (face-attribute 'default
:height) 2)))

(defun radian-local-make-it-smaller ()
  (interactive)
  (set-face-attribute 'default nil :height (/ (face-attribute 'default
:height) 2)))

(bind-key "s--" #'radian-local-make-it-smaller)
(bind-key "s-=" #'radian-local-make-it-bigger)

There are some issues with rounding errors, but it's a lot better than
nothing. I just use those keybindings whenever plugging in or unplugging my
external monitor.
[Message part 2 (text/html, inline)]

This bug report was last modified 2 years and 231 days ago.

Previous Next


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