From bac79657a5dac12776a5e0f2c2af8ae51dc618cd Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 9 Apr 2021 12:48:54 -0500 Subject: 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 --- early-init.el | 23 +++++------ lisp/acdw-fonts.el | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 lisp/acdw-fonts.el 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 @@ (add-function :after after-focus-change-function (defun hook--setup-fonts () - (dolist (face '(default fixed-pitch)) - ;; `default' and `fixed-pitch' should be the same. - (set-face-attribute face nil - :font (pcase acdw/system - (:home "DejaVu Sans Mono-10") - (:work "Consolas-10") - (:other "monospace-10")))) - ;; `variable-pitch' is, of course, different. - (set-face-attribute 'variable-pitch nil - :font (pcase acdw/system + (require 'acdw-fonts) + (setq acdw-fonts/monospace (pcase acdw/system + (:home "DejaVu Sans Mono") + (:work "Consolas") + (:other "monospace")) + acdw-fonts/monospace-size 10 + acdw-fonts/variable (pcase acdw/system (:home "DejaVu Sans") - (:work "Calibri-11") - (:other "sans-serif"))) + (:work "Calibri") + (:other "sans-serif")) + acdw-fonts/variable-size 11) + (acdw-fonts/set) (remove-function after-focus-change-function 'hook--setup-fonts))) 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 @@ +;;; acdw-fonts.el -- font setup -*- lexical-binding: t; coding: utf-8-unix -*- + +;; Author: Case Duckworth +;; Created: Sometime during Covid-19, 2020 +;; Keywords: configuration +;; URL: https://tildegit.org/acdw/emacs + +;; This file is NOT part of GNU Emacs. + +;; Everyone is permitted to do whatever with this software, without +;; limitation. This software comes without any warranty whatsoever, +;; but with two pieces of advice: +;; - Don't hurt yourself. +;; - Make good choices. + +;;; Commentary: +;; This code is based heavily on (and in fact, until I am able to tweak it, +;; will be a copy of) Oliver Taylor's code, available here: +;; https://github.com/olivertaylor/olivertaylor.github.io +;; /blob/master/notes/20210324_emacs-optical-font-adjustment.org + +;;; Code: + + +;; Variables + +(defvar acdw-fonts/monospace nil + "Monospace font to be used for `default' and `fixed-pitch' faces.") + +(defvar acdw-fonts/variable nil + "Variable font to be used for the `variable-pitch' face.") + +(defvar acdw-fonts/monospace-size 11 + "Font size, an integer, to be used for the `default' and `fixed-pitch' faces. + +This value is multiplied by 10, so 12 becomes 120, in order to +comply with Emacs's `set-face-attribute' requirements.") + +(defvar acdw-fonts/variable-size 12 + "Font size, an integer, to be used for the `variable-pitch' face. + +This value will be used to determine a relative (float) size +based on the default size. So if your default size is 12 and +your variable size is 14, the computed relative size will be +1.16.") + + +;; Functions + +(defun acdw-fonts/set () + "Set fonts according to `acdw-fonts' variables." + (interactive) + (set-face-attribute 'default nil + :family acdw-fonts/monospace + :height (* acdw-fonts/monospace-size 10)) + (set-face-attribute 'fixed-pitch nil + :family acdw-fonts/monospace + :height 1.0) + (set-face-attribute 'variable-pitch nil + :family acdw-fonts/variable + :height 1.0)) + + +;;; Larger Variable Pitch Mode + + +;; A minor mode to scale the variable-pitch face up to the height defined in +;; `acdw-fonts/variable-size' and the fixed-pitch face down to the height +;; defined in `acdw-fonts/monospace-size', buffer locally. This mode should +;; be enabled wherever you want to adjust face sizes, perhaps with a hook. + +(make-variable-buffer-local + (defvar larger-variable-pitch-mode-status nil + "Status of the larger-variable-pitch-mode")) + +(make-variable-buffer-local + (defvar variable-pitch-remapping nil + "variable-pitch remapping cookie for larger-variable-pitch-mode.")) + +(make-variable-buffer-local + (defvar fixed-pitch-remapping nil + "fixed-pitch remapping cookie for larger-variable-pitch-mode")) + +(defun larger-variable-pitch-mode-toggle () + (setq larger-variable-pitch-mode-status + (not larger-variable-pitch-mode-status)) + (if larger-variable-pitch-mode-status + (progn + (setq variable-pitch-remapping + (face-remap-add-relative + 'variable-pitch :height (/ (float acdw-fonts/variable-size) + (float acdw-fonts/monospace-size)))) + (setq fixed-pitch-remapping + (face-remap-add-relative + 'fixed-pitch :height (/ (float acdw-fonts/monospace-size) + (float acdw-fonts/variable-size)))) + (force-window-update (current-buffer))) + (progn + (face-remap-remove-relative variable-pitch-remapping) + (face-remap-remove-relative fixed-pitch-remapping)))) + +(define-minor-mode larger-variable-pitch-mode + "Minor mode to scale the variable- and fixed-pitch faces up and down." + :init-value nil + :lighter " V+" + (larger-variable-pitch-mode-toggle)) + +(defun acdw-fonts/buffer-face-hook () + "Activate and deactivate larger-variable-pitch-mode minor mode." + (if buffer-face-mode + (larger-variable-pitch-mode 1) + (larger-variable-pitch-mode -1))) + +(add-hook 'buffer-face-mode-hook #'acdw-fonts/buffer-face-hook) + +(provide 'acdw-fonts) +;;; acdw-fonts.el ends here -- cgit 1.4.1-21-gabe81