Rewrite how Doom scales font-size

The previous approach only resized `doom-font`. Now it resizes
`doom-variable-pitch-font` and `doom-serif-font` too, so variable-pitch,
mixed-pitch, and fixed-serif users can enjoy dynamic font scaling.
This commit is contained in:
Henrik Lissner 2021-06-04 02:16:31 -04:00
parent ffeca3c7fd
commit f408016c6f

View file

@ -7,9 +7,9 @@ or `doom/decrease-font-size' are invoked.")
;;;###autoload ;;;###autoload
(defvar doom-big-font nil (defvar doom-big-font nil
"The font to use for `doom-big-font-mode'. If nil, `doom-font' will be used, "The font to use for `doom-big-font-mode'.
scaled up by `doom-big-font-increment'. See `doom-font' for details on If nil, `doom-font' will be used, scaled up by `doom-big-font-increment'. See
acceptable values for this variable.") `doom-font' for details on acceptable values for this variable.")
;;;###autoload ;;;###autoload
(defvar doom-big-font-increment 4 (defvar doom-big-font-increment 4
@ -20,47 +20,81 @@ acceptable values for this variable.")
;; ;;
;;; Library ;;; Library
(defun doom--font-name (fontname) (defun doom--font2xlfd (font)
(when (query-fontset fontname) (cond ((stringp font) (aref (font-info font) 0))
(when-let (ascii (assq 'ascii (aref (fontset-info fontname) 2))) ((fontp font) (font-xlfd-name font))
(setq fontname (nth 2 ascii)))) ((vectorp font) font)))
(or (x-decompose-font-name fontname)
(error "Cannot decompose font name")))
(defvar doom--font-scale nil)
;;;###autoload ;;;###autoload
(defun doom-adjust-font-size (increment) (defun doom-adjust-font-size (increment &optional fixed-size-p font-alist)
"Increase size of font in FRAME by INCREMENT. "Increase size of font in FRAME by INCREMENT.
FRAME parameter defaults to current frame."
(if (null increment) If FIXED-SIZE-P is non-nil, treat INCREMENT as a font size, rather than a
(progn scaling factor.
(set-frame-font doom-font 'keep-size t)
(setf (alist-get 'font default-frame-alist) FONT-ALIST is an alist give temporary values to certain Doom font variables,
(cond ((stringp doom-font) doom-font) like `doom-font' or `doom-variable-pitch-font'. e.g.
((fontp doom-font) (font-xlfd-name doom-font))
((signal 'wrong-type-argument (list '(fontp stringp) `((doom-font . ,(font-spec :family \"Sans Serif\" :size 12)))
doom-font)))))
t) Doesn't work in terminal Emacs."
(let* ((font (frame-parameter nil 'font)) (unless (display-multi-font-p)
(font (doom--font-name font)) (user-error "Cannot resize fonts in terminal Emacs"))
(increment (* increment doom-font-increment)) (condition-case-unless-debug e
(zoom-factor (or doom--font-scale 0))) (let (changed)
(let ((new-size (+ (string-to-number (aref font xlfd-regexp-pixelsize-subnum)) (dolist (sym '((doom-font . default)
increment))) (doom-serif-font . fixed-pitch-serif)
(unless (> new-size 0) (doom-variable-pitch-font . variable-pitch))
(error "Font is too small at %d" new-size)) (when changed
(aset font xlfd-regexp-pixelsize-subnum (number-to-string new-size))) (doom-init-fonts-h 'reload)
;; Set point size & width to "*", so frame width will adjust to new font size t))
(aset font xlfd-regexp-pointsize-subnum "*") (cl-destructuring-bind (var . face) sym
(aset font xlfd-regexp-avgwidth-subnum "*") (if (null increment)
(setq font (x-compose-font-name font)) (when (get var 'initial-value)
(unless (x-list-fonts font) (set var (get var 'initial-value))
(error "Cannot change font size")) (put var 'new-size nil)
(set-frame-font font 'keep-size t) (put var 'initial-value nil)
(setf (alist-get 'font default-frame-alist) font) (setq changed t))
(setq doom--font-scale (+ zoom-factor increment)) (let* ((orig-font (or (symbol-value var)
;; Unlike `set-frame-font', `set-frame-parameter' won't trigger this (face-font face t)
(run-hooks 'after-setting-font-hook)))) (with-temp-buffer (face-font face))))
(font (doom--font2xlfd orig-font))
(font (or (and (query-fontset font)
(if-let (ascii (assq 'ascii (aref (fontset-info font) 2)))
(nth 2 ascii)
font))
font))
(dfont (and (stringp font) (x-decompose-font-name font)))
(dfont (if-let* ((remap-font (alist-get var font-alist))
(remap-xlfd (if (stringp remap-font)
(aref (font-info remap-font) 0)
(font-xlfd-name remap-font))))
(x-decompose-font-name remap-xlfd)
dfont)))
(unless (get var 'initial-value)
(put var 'initial-value orig-font))
(unless (vectorp dfont)
(error "Could not decompose %S font: %S" var font))
(let* ((step (if fixed-size-p 0 (* increment doom-font-increment)))
(orig-size (string-to-number (aref dfont xlfd-regexp-pixelsize-subnum)))
(new-size (if fixed-size-p increment (+ orig-size step))))
(unless (> new-size 0)
(error "`%s' font is too small to be reszied (%d)" var new-size))
(if (= orig-size new-size)
(message "Could not resize `%s' for some reason" var)
(put var 'new-size new-size)
(aset dfont xlfd-regexp-pixelsize-subnum (number-to-string new-size))
;; Set point size & width to "*", so frame width will adjust to new font size
(aset dfont xlfd-regexp-pointsize-subnum "*")
(aset dfont xlfd-regexp-avgwidth-subnum "*")
(setq font (x-compose-font-name dfont))
(unless (x-list-fonts font)
(error "Cannot change font size"))
(set var font)
(setq changed t))))))))
(error
(ignore-errors (doom-adjust-font-size nil))
(signal (car e) (cdr e)))))
;; ;;
@ -74,16 +108,16 @@ See `doom-init-fonts-h'."
(doom-init-fonts-h 'reload)) (doom-init-fonts-h 'reload))
;;;###autoload ;;;###autoload
(defun doom/increase-font-size (count) (defun doom/increase-font-size (count &optional increment)
"Enlargens the font size across the current and child frames." "Enlargens the font size across the current and child frames."
(interactive "p") (interactive "p")
(doom-adjust-font-size count)) (doom-adjust-font-size (* count (or increment doom-font-increment))))
;;;###autoload ;;;###autoload
(defun doom/decrease-font-size (count) (defun doom/decrease-font-size (count &optional increment)
"Shrinks the font size across the current and child frames." "Shrinks the font size across the current and child frames."
(interactive "p") (interactive "p")
(doom-adjust-font-size (- count))) (doom-adjust-font-size (* (- count) (or increment doom-font-increment))))
;;;###autoload ;;;###autoload
(defun doom/reset-font-size () (defun doom/reset-font-size ()
@ -97,34 +131,32 @@ Assuming it has been adjusted via `doom/increase-font-size' and
(/= text-scale-mode-amount 0)) (/= text-scale-mode-amount 0))
(text-scale-set 0) (text-scale-set 0)
(setq success t)) (setq success t))
(when (doom-adjust-font-size nil) (cond (doom-big-font-mode
(setq success t)) (message "Disabling `doom-big-font-mode'")
(doom-big-font-mode -1)
(setq success t))
((doom-adjust-font-size nil)
(setq success t)))
(unless success (unless success
(user-error "The font hasn't been resized")))) (user-error "The font hasn't been resized"))))
;;;###autoload ;;;###autoload
(define-minor-mode doom-big-font-mode (define-minor-mode doom-big-font-mode
"A global mode that resizes the font, for streams, screen-sharing and "Globally resizes your fonts for streams, screen-sharing or presentations.
presentations.
This uses `doom/increase-font-size' under the hood, and enlargens the font by Uses `doom-big-font' if its set, otherwise uses `doom-font' (falling back to
`doom-big-font-increment'." your system font).
Also resizees `doom-variable-pitch-font' and `doom-serif-font'."
:init-value nil :init-value nil
:lighter " BIG" :lighter " BIG"
:global t :global t
(unless doom-font
(or (setq doom-font (face-attribute 'default :font))
(user-error "`doom-font' must be set to a valid font")))
(if doom-big-font (if doom-big-font
(let ((font (if doom-big-font-mode doom-big-font doom-font))) ;; Use `doom-big-font' in lieu of `doom-font'
(set-frame-font font 'keep-size t) (doom-adjust-font-size
(setf (alist-get 'font default-frame-alist) (and doom-big-font-mode
(cond ((stringp doom-font) font) (aref (x-decompose-font-name (doom--font2xlfd font))
((fontp font) (font-xlfd-name font)) xlfd-regexp-pixelsize-subnum))
((signal 'wrong-type-argument (list '(fontp stringp) t `((doom-font . ,doom-big-font)))
font)))))) ;; Resize the current font
(doom-adjust-font-size (doom-adjust-font-size (if doom-big-font-mode doom-big-font-increment))))
(and doom-big-font-mode
(integerp doom-big-font-increment)
(/= doom-big-font-increment 0)
doom-big-font-increment))))