Skip to content

Instantly share code, notes, and snippets.

@cpbotha
Last active February 6, 2023 10:45
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cpbotha/e1663767f8497104a2b6f5723af73503 to your computer and use it in GitHub Desktop.
Save cpbotha/e1663767f8497104a2b6f5723af73503 to your computer and use it in GitHub Desktop.
;; minimal version of my Emacs setup which approximates the effective points per inch on your screen
;; and then selects your default font pts based on that
;; Also works on wayland / WSLg because it parses out physical dims from weston.log (necessary in 2023-01)
;; LIMITATION: Will probably not work on multi-monitor setups. Exercise for the reader!
;; BSD 3-clause copyright Charl P. Botha <info@charlbotha.com>
(defun get-mon-attr (which)
(cdr (assoc which (car (display-monitor-attributes-list)))))
(defun get-monitor-width-mm ()
(if (getenv "WSL_DISTRO_NAME")
;; under WSLg 2021-01-21, physical dimensions are not correctly reported anywhere but weston.log,
;; (I hear this is a wayland issue, not just wslg)
;; so we use good old awk to get the last occurrence in the log file
(let* ((awk-dims (shell-command-to-string "awk 'match($0, /physicalWidth:([0-9]+), physicalHeight:([0-9]+)/,a) {width=a[1];height=a[2]} END {print width \",\" height}' /mnt/wslg/weston.log"))
(splitd (split-string awk-dims ","))
(width-mm (string-to-number (car splitd))))
width-mm)
;; on other systems, we use whatever Emacs thinks it is
(car (get-mon-attr 'mm-size))))
(setq monitor-width (get-monitor-width-mm))
(if monitor-width
(progn
(setq monitor-hpix (nth 2 (get-mon-attr 'geometry)))
;; estimate *effective* PPI (i.e. taking into account scaling / super-sampling)
(setq ppi-est (/ monitor-hpix (/ monitor-width 25.4)))
;; some examples. Adjust the cond below to your taste!
;; 4K at 100% because mac: 3840x => 139
;; thinkpad 14" at 100%: (/ 1920 (/ 344 24.4)) => 136.186
;; m1 mba built-in display: (/ 1440.0 (/ 286 25.4)) => 127.88
;; 4K at 125%: (/ 3072.0 (/ 697 25.4)) => 111.949
;; 4K with WACKY 127% macOS scaling 3008x1692: => 109.147
;; 27" at 2560x1440 is 108.92 -- font size 130 looks fine there
(setq my-font-pts (cond
((>= ppi-est 130) 160)
((>= ppi-est 109) 150)
(t 130))))
;; fallback for when we DON'T have monitor-width, e.g. in text mode
(setq my-font-pts 140))
;; FONTS =================================================================
;; MONOSPACE FONT
;; mac: brew install font-jetbrains-mono-nerd-font
;; ubu2204: sudo apt install fonts-jetbrains-mono
(set-face-attribute 'default nil :family "JetBrainsMono Nerd Font" :height my-font-pts)
(set-face-attribute 'fixed-pitch nil :family "JetBrainsMono Nerd Font" :height 1.0) ;; relative to default
;; VARIABLE PITCH FONT
(set-face-attribute 'variable-pitch nil :family "Noto Serif" :height 1.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment