emacs-config/init.org.d/40-fonts.org

3.4 KiB

Font Configuration

set default fonts

  (let ((default-family "Roboto Mono")
        (variable-family "Inter"))
    (custom-set-faces
     ;; set default font
     `(default ((t (:inherit nil :extend nil :stipple nil
                             :inverse-video nil :box nil
                             :strike-through nil :overline nil :underline nil
                             :slant normal :weight normal
                             :height 100 :width normal
                             :family ,default-family))))
     `(fixed-pitch ((t (:family ,default-family))))
     `(fixed-pitch-serif ((t (:family ,default-family))))
     `(variable-pitch ((t (:inherit default :weight regular :family ,variable-family))))))

Font setter function

  (defun set-face-attr-family (face font-families &optional attributes)
    "Set FACE's font family to the first available font family from FONT-FAMILIES list, 
  and apply other ATTRIBUTES specified in the plist."

    (when attributes
      (while attributes
      (let ((attr (pop attributes))
            (value (pop attributes)))
        (set-face-attribute face nil attr value))))

    (let ((available-font (seq-find (lambda (font)
                                      (find-font (font-spec :family font)))
                                    font-families)))
      (when available-font
        ;; Set the font family
        (set-face-attribute face nil :family available-font)
        ;; Apply other attributes from the plist

        (message "Set %s face to use font family: %s with attributes: %s" 
                 face available-font attributes))))

Set face family and attributes

  (set-face-attr-family 'default
                        '("Roboto Mono" "Source Code Pro" "Fira Code" "Noto Sans Mono" "Cascadia Code" "SF Pro Mono" "fixed")
                        '(:inherit nil :extend nil :stipple nil
                                   :inverse-video nil :box nil
                                   :strike-through nil :overline nil :underline nil
                                   :slant normal :weight normal
                                   :height 100 :width normal))
  (set-face-attr-family 'variable-pitch
                        '("Inter" "Cantarell" "Fira Sans" "Noto Sans" "Segoe UI" "SF Pro Display")
                        '(:inherit default :weight regular))

  (set-face-attribute 'fixed-pitch nil :family (face-attribute 'default :family))
  (set-face-attribute 'fixed-pitch-serif nil :family (face-attribute 'default :family))
  
  (set-face-attribute 'fringe nil :background (face-background 'default))
  (set-face-attribute 'line-number nil :background (face-background 'default))
  (set-face-attribute 'line-number nil :foreground (face-foreground 'shadow))

Chinese Font

  ;; (dolist (charset '(kana han symbol cjk-misc bopomofo))
  ;;   (set-fontset-font (frame-parameter nil 'font)
  ;;                     charset (font-spec :family "Noto Sans Mono CJK TC Regular"
  ;;                                        :size 10)))

Font finder function

  ;; (defun find-available-font-family (font-families)
  ;;   "Return the first available font family from FONT-FAMILIES list, or nil if none is found."
  ;;   (seq-find (lambda (font)
  ;;               (find-font (font-spec :family font)))
  ;;             font-families))