emacs.d

My emacs configuration, done in a literate programming fashion using org-mode

git clone git://git.shimmy1996.com/emacs.d.git
commit 4577cdd03dd51cec63d1a90110beb4da16ebb1a9
parent 48f4a5f1309c4277341bc6151f5b4dcbb53ad47f
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date:   Fri, 22 Jun 2018 01:24:18 -0500

CJK font fallback logic.

Diffstat:
MREADME.org | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------
1 file changed, 58 insertions(+), 21 deletions(-)
diff --git a/README.org b/README.org
@@ -100,6 +100,64 @@ It would also help to change the default command for =emacs= into:
   emacsclient -c -a emacs
 #+END_SRC
 
+** Font Settings
+*** Default Fonts
+Specify both Latin font and CJK font.
+#+BEGIN_SRC emacs-lisp
+  (defvar user/latin-font "Iosevka Term"
+    "Default font for Latin characters.")
+
+  (defvar user/cjk-font "Noto Sans CJK SC"
+    "Default font for CJK characters.")
+
+  (defvar user/font-size 16
+    "Default font size.")
+#+END_SRC
+
+*** CJK Font Fallback Logic
+Fallback logic for CJK fonts, ensures correct font gets used and CJK fonts
+scales correctly to twice the width of half-width characters.
+
+To ensure fontset gets correctly configured, I simply created my own. This is
+because =emacsclient= seems to have a different dynamic when creating
+=startup-fontset= and no other method guarantees consistent behavior between
+=emacs= and =emacsclient=.
+
+#+BEGIN_SRC emacs-lisp
+  (defvar user/cjk-font-scale
+    '((16 . 1.0)
+      (17 . 1.1)
+      (18 . 1.0))
+    "Scaling factor to use for cjk font of given size.")
+
+  (defvar user/standard-fontset
+    (create-fontset-from-fontset-spec standard-fontset-spec)
+    "User fontset with cjk font fallback.")
+
+  (defun user/set-font ()
+    "Set Latin and CJK font for user/standard-fontset."
+    ;; Latin font.
+    (set-fontset-font user/standard-fontset 'latin
+                      (font-spec :family user/latin-font :size user/font-size)
+                      nil 'prepend)
+    ;; CJK font.
+    (dolist (charset '(kana han chinese-gbk cjk-misc bopomofo))
+      (set-fontset-font user/standard-fontset charset
+                        (font-spec :family user/cjk-font)
+                        nil 'prepend))
+    ;; Specify scaling factor for CJK font.
+    (setq face-font-rescale-alist
+          (list (cons user/cjk-font
+                      (cdr (assoc user/font-size user/cjk-font-scale)))))
+    ;; Ensure user/standard-fontset gets used.
+    (add-to-list 'default-frame-alist (cons 'font user/standard-fontset)))
+
+  ;; Apply changes.
+  (user/set-font)
+  ;; For emacsclient.
+  (add-hook 'before-make-frame-hook #'user/set-font)
+#+END_SRC
+
 ** Line Highlighting
 Enable line highlighting.
 #+BEGIN_SRC emacs-lisp
@@ -187,27 +245,6 @@ Fills up gap in the border when tiling Emacs to half-screen.
   (setq frame-resize-pixelwise t)
 #+END_SRC
 
-*** Default Font
-Use Iosevka as the default font.
-#+BEGIN_SRC emacs-lisp
-  (setq default-frame-alist '((font . "Iosevka-13")))
-#+END_SRC
-
-*** DISABLED CJK Font fallback
-Fallback for CJK fonts. Disabled for now as the solution doesn't seem
-to work for =emacsclient=, besides this makes CJK characters much
-taller than Latin characters, which is really ugly.
-#+BEGIN_SRC emacs-lisp
-  (set-fontset-font "fontset-default" 'iso-8859-3
-                    (font-spec :family "Iosevka" :size 12))
-  (set-fontset-font "fontset-default"
-                    'han
-                    (font-spec :family "Noto Sans CJK SC") nil 'append)
-  (setq face-font-rescale-alist '(("Noto Sans CJK SC" . 1.1)))
-#+END_SRC
-
-#+RESULTS:
-
 ** Zoom In
 Use mouse wheel to adjust zoom level.
 #+BEGIN_SRC emacs-lisp