about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCase Duckworth2021-04-09 12:48:54 -0500
committerCase Duckworth2021-04-09 12:48:54 -0500
commitbac79657a5dac12776a5e0f2c2af8ae51dc618cd (patch)
tree707683fd93cfd163f2eecf8fd638fe7cefde835d
parentAdd `acdw/find-emacs-dotfiles' (diff)
downloademacs-bac79657a5dac12776a5e0f2c2af8ae51dc618cd.tar.gz
emacs-bac79657a5dac12776a5e0f2c2af8ae51dc618cd.zip
Change font loading logic to hopefully be more robust
Based heavily (basically copied) from
https://github.com/olivertaylor/olivertaylor.github.io/blob/master/notes/20210324_emacs-optical-font-adjustment.org
-rw-r--r--early-init.el23
-rw-r--r--lisp/acdw-fonts.el117
2 files changed, 128 insertions, 12 deletions
diff --git a/early-init.el b/early-init.el index 53059cd..8515fa9 100644 --- a/early-init.el +++ b/early-init.el
@@ -72,19 +72,18 @@
72 72
73(add-function :after after-focus-change-function 73(add-function :after after-focus-change-function
74 (defun hook--setup-fonts () 74 (defun hook--setup-fonts ()
75 (dolist (face '(default fixed-pitch)) 75 (require 'acdw-fonts)
76 ;; `default' and `fixed-pitch' should be the same. 76 (setq acdw-fonts/monospace (pcase acdw/system
77 (set-face-attribute face nil 77 (:home "DejaVu Sans Mono")
78 :font (pcase acdw/system 78 (:work "Consolas")
79 (:home "DejaVu Sans Mono-10") 79 (:other "monospace"))
80 (:work "Consolas-10") 80 acdw-fonts/monospace-size 10
81 (:other "monospace-10")))) 81 acdw-fonts/variable (pcase acdw/system
82 ;; `variable-pitch' is, of course, different.
83 (set-face-attribute 'variable-pitch nil
84 :font (pcase acdw/system
85 (:home "DejaVu Sans") 82 (:home "DejaVu Sans")
86 (:work "Calibri-11") 83 (:work "Calibri")
87 (:other "sans-serif"))) 84 (:other "sans-serif"))
85 acdw-fonts/variable-size 11)
86 (acdw-fonts/set)
88 (remove-function after-focus-change-function 87 (remove-function after-focus-change-function
89 'hook--setup-fonts))) 88 'hook--setup-fonts)))
90 89
diff --git a/lisp/acdw-fonts.el b/lisp/acdw-fonts.el new file mode 100644 index 0000000..c4f16e8 --- /dev/null +++ b/lisp/acdw-fonts.el
@@ -0,0 +1,117 @@
1;;; acdw-fonts.el -- font setup -*- lexical-binding: t; coding: utf-8-unix -*-
2
3;; Author: Case Duckworth <acdw@acdw.net>
4;; Created: Sometime during Covid-19, 2020
5;; Keywords: configuration
6;; URL: https://tildegit.org/acdw/emacs
7
8;; This file is NOT part of GNU Emacs.
9
10;; Everyone is permitted to do whatever with this software, without
11;; limitation. This software comes without any warranty whatsoever,
12;; but with two pieces of advice:
13;; - Don't hurt yourself.
14;; - Make good choices.
15
16;;; Commentary:
17;; This code is based heavily on (and in fact, until I am able to tweak it,
18;; will be a copy of) Oliver Taylor's code, available here:
19;; https://github.com/olivertaylor/olivertaylor.github.io
20;; /blob/master/notes/20210324_emacs-optical-font-adjustment.org
21
22;;; Code:
23
24
25;; Variables
26
27(defvar acdw-fonts/monospace nil
28 "Monospace font to be used for `default' and `fixed-pitch' faces.")
29
30(defvar acdw-fonts/variable nil
31 "Variable font to be used for the `variable-pitch' face.")
32
33(defvar acdw-fonts/monospace-size 11
34 "Font size, an integer, to be used for the `default' and `fixed-pitch' faces.
35
36This value is multiplied by 10, so 12 becomes 120, in order to
37comply with Emacs's `set-face-attribute' requirements.")
38
39(defvar acdw-fonts/variable-size 12
40 "Font size, an integer, to be used for the `variable-pitch' face.
41
42This value will be used to determine a relative (float) size
43based on the default size. So if your default size is 12 and
44your variable size is 14, the computed relative size will be
451.16.")
46
47
48;; Functions
49
50(defun acdw-fonts/set ()
51 "Set fonts according to `acdw-fonts' variables."
52 (interactive)
53 (set-face-attribute 'default nil
54 :family acdw-fonts/monospace
55 :height (* acdw-fonts/monospace-size 10))
56 (set-face-attribute 'fixed-pitch nil
57 :family acdw-fonts/monospace
58 :height 1.0)
59 (set-face-attribute 'variable-pitch nil
60 :family acdw-fonts/variable
61 :height 1.0))
62
63
64;;; Larger Variable Pitch Mode
65
66
67;; A minor mode to scale the variable-pitch face up to the height defined in
68;; `acdw-fonts/variable-size' and the fixed-pitch face down to the height
69;; defined in `acdw-fonts/monospace-size', buffer locally. This mode should
70;; be enabled wherever you want to adjust face sizes, perhaps with a hook.
71
72(make-variable-buffer-local
73 (defvar larger-variable-pitch-mode-status nil
74 "Status of the larger-variable-pitch-mode"))
75
76(make-variable-buffer-local
77 (defvar variable-pitch-remapping nil
78 "variable-pitch remapping cookie for larger-variable-pitch-mode."))
79
80(make-variable-buffer-local
81 (defvar fixed-pitch-remapping nil
82 "fixed-pitch remapping cookie for larger-variable-pitch-mode"))
83
84(defun larger-variable-pitch-mode-toggle ()
85 (setq larger-variable-pitch-mode-status
86 (not larger-variable-pitch-mode-status))
87 (if larger-variable-pitch-mode-status
88 (progn
89 (setq variable-pitch-remapping
90 (face-remap-add-relative
91 'variable-pitch :height (/ (float acdw-fonts/variable-size)
92 (float acdw-fonts/monospace-size))))
93 (setq fixed-pitch-remapping
94 (face-remap-add-relative
95 'fixed-pitch :height (/ (float acdw-fonts/monospace-size)
96 (float acdw-fonts/variable-size))))
97 (force-window-update (current-buffer)))
98 (progn
99 (face-remap-remove-relative variable-pitch-remapping)
100 (face-remap-remove-relative fixed-pitch-remapping))))
101
102(define-minor-mode larger-variable-pitch-mode
103 "Minor mode to scale the variable- and fixed-pitch faces up and down."
104 :init-value nil
105 :lighter " V+"
106 (larger-variable-pitch-mode-toggle))
107
108(defun acdw-fonts/buffer-face-hook ()
109 "Activate and deactivate larger-variable-pitch-mode minor mode."
110 (if buffer-face-mode
111 (larger-variable-pitch-mode 1)
112 (larger-variable-pitch-mode -1)))
113
114(add-hook 'buffer-face-mode-hook #'acdw-fonts/buffer-face-hook)
115
116(provide 'acdw-fonts)
117;;; acdw-fonts.el ends here