Set font size depending on DPI.
authorOleksandr Gavenko <gavenkoa@gmail.com>
Wed, 11 Oct 2017 00:28:06 +0300
changeset 1541 dca10cecc9e2
parent 1540 e3000459f683
child 1542 29783921b4f7
Set font size depending on DPI.
.emacs-my
--- a/.emacs-my	Tue Oct 10 22:41:43 2017 +0300
+++ b/.emacs-my	Wed Oct 11 00:28:06 2017 +0300
@@ -435,17 +435,53 @@
 ;; See what I am typing immediately (for keystroke in minibuffer).
 (setq echo-keystrokes 0.2)
 
+(defun my-dpi ()
+  (let* ((attrs (car (display-monitor-attributes-list)))
+         (size (assoc 'mm-size attrs))
+         (sizex (cadr size))
+         (res (cdr (assoc 'geometry attrs)))
+         (resx (- (caddr res) (car res)))
+         dpi)
+    (catch 'exit
+      ;; in terminal
+      (unless sizex
+        (throw 'exit 10))
+      ;; on big screen
+      (when (> sizex 1000)
+        (throw 'exit 10))
+      ;; DPI
+      (* (/ (float resx) sizex) 25.4))))
+
+(defun my-preferred-font-size ()
+  (let ( (dpi (my-dpi)) )
+  (cond
+    ((< dpi 110) 10)
+    ((< dpi 130) 11)
+    ((< dpi 160) 12)
+    (t 12))))
+
+(defvar my-preferred-font-size (my-preferred-font-size))
+
+(defvar my-regular-font
+  (cond
+   ((eq window-system 'x) (format "DejaVu Sans Mono-%d:weight=normal" my-preferred-font-size))
+   ((eq window-system 'w32) (format "Courier New-%d:antialias=none" my-preferred-font-size))))
+(defvar my-symbol-font
+  (cond
+   ((eq window-system 'x) (format "DejaVu Sans Mono-%d:weight=normal" my-preferred-font-size))
+   ((eq window-system 'w32) (format "DejaVu Sans Mono-%d:antialias=none" my-preferred-font-size))))
+
 (cond
  ((eq window-system 'x)
-  (if (and (fboundp 'find-font) (find-font (font-spec :name "DejaVu Sans Mono-11:weight=normal")))
-      (set-frame-font "DejaVu Sans Mono-11")
+  (if (and (fboundp 'find-font) (find-font (font-spec :name my-regular-font)))
+      (set-frame-font my-regular-font)
     (set-frame-font "7x14")))
  ((eq window-system 'w32)
-  (set-frame-font "Courier New-10:antialias=none")
-  (set-fontset-font nil 'cyrillic "Courier New-10:antialias=none")
-  (set-fontset-font nil 'greek "Courier New-10:antialias=none")
-  (set-fontset-font nil 'phonetic "Courier New-10:antialias=none")
-  (set-fontset-font nil 'symbol "DejaVu Sans Mono-10:antialias=none")))
+  (set-frame-font my-regular-font)
+  (set-fontset-font nil 'cyrillic my-regular-font)
+  (set-fontset-font nil 'greek my-regular-font)
+  (set-fontset-font nil 'phonetic my-regular-font)
+  (set-fontset-font nil 'symbol my-symbol-font)))
 
 (fset 'yes-or-no-p 'y-or-n-p)