;;; acdw-fonts.el -- font setup -*- lexical-binding: t; coding: utf-8-unix -*- ;; Author: Case Duckworth <(rot13-string "npqj@npqj.arg")> ;; 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) ;;; Emoji fonts ;; from https://old.reddit.com/r/emacs/comments/mvlid5/ (defun acdw-fonts/setup-emoji-fonts (&rest emoji-fonts) "For all EMOJI-FONTS that exist, add them to the symbol fontset. This is for emoji fonts." (let ((ffl (font-family-list))) (dolist (font emoji-fonts) (when (member font ffl) (set-fontset-font t 'symbol (font-spec :family font) nil 'append))))) ;;; Variable-pitch ;; from https://github.com/turbana/emacs-config#variable-pitch (defcustom acdw-fonts/fixed-pitch-faces '(linum org-block org-block-begin-line org-block-end-line org-checkbox org-code org-date org-document-info-keyword org-hide org-indent org-link org-meta-line org-special-keyword org-table whitespace-space) "Faces to keep fixed-pitch in `acdw/variable-pitch-mode'." :type 'sexp :group 'faces) (defun acdw-fonts//variable-pitch-add-inherit (attrs parent) "Add `:inherit PARENT' to ATTRS unless already present. Handles cases where `:inherit' is already specified." (let ((current-parent (plist-get attrs :inherit))) (unless (or (eq parent current-parent) (and (listp current-parent) (member parent current-parent))) (plist-put attrs :inherit (if current-parent (list current-parent parent) parent))))) (defun acdw-fonts/adapt-variable-pitch () "Adapt `variable-pitch-mode' to keep some fonts fixed-pitch." (when variable-pitch-mode (mapc (lambda (face) (when (facep face) (apply #'set-face-attribute face nil (acdw-fonts//variable-pitch-add-inherit (face-attr-construct face) 'fixed-pitch)))) acdw-fonts/fixed-pitch-faces))) (provide 'acdw-fonts) ;;; acdw-fonts.el ends here