Emacs 字体设置的种种: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
| (24 intermediate revisions by the same user not shown) | |||
| Line 26: | Line 26: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== 调试 == | |||
需要调试,可以 M-x eval-expression 执行下列命令: | 需要调试,可以 M-x eval-expression 执行下列命令: | ||
<syntaxhighlight lang="elisp" line> | <syntaxhighlight lang="elisp" line> | ||
| Line 34: | Line 35: | ||
(find-font | (find-font | ||
(font-spec :family "DejaVu Sans Mono")) | (font-spec :family "DejaVu Sans Mono")) | ||
;; 字体查找,能找到就有,找不着就 nil | |||
(member "Noto Serif CJK SC" (font-family-list)) | |||
;; 查找实际使用的英文字体 | ;; 查找实际使用的英文字体 | ||
| Line 45: | Line 49: | ||
执行 <code>M-x describe-face</code>,输入 <code>default</code>、<code>fixed-pitch</code> 或 <code>variable-pitch</code>,查看该 face 当前的家族、字号、粗细、继承关系等。 | 执行 <code>M-x describe-face</code>,输入 <code>default</code>、<code>fixed-pitch</code> 或 <code>variable-pitch</code>,查看该 face 当前的家族、字号、粗细、继承关系等。 | ||
执行 <code>M-x describe-char</code>,可以查看具体的字体。 | |||
== 核心 == | == 核心 == | ||
| Line 51: | Line 57: | ||
!face | !face | ||
!职责 | !职责 | ||
!中文 Debian-GUI | |||
!中文 Windows | |||
!英文 Debian-GUI | |||
!英文 Windows | |||
|- | |- | ||
|<code>default</code> | |<code>default</code> | ||
|普通文本的基础,也是编程环境的默认字体 | |普通文本的基础,也是编程环境的默认字体 | ||
|WenQuanYi Micro Hei | |||
|MicroSoft YaHei | |||
|DejaVu Sans Mono | |||
|Consolas | |||
|- | |- | ||
|<code>fixed-pitch</code> | |<code>fixed-pitch</code> | ||
|明确要求等宽的内容,如代码、表格、代码块 | |明确要求等宽的内容,如代码、表格、代码块 | ||
|WenQuanYi Micro Hei | |||
|MicroSoft YaHei | |||
|DejaVu Sans Mono | |||
|Consolas | |||
|- | |- | ||
|<code>variable-pitch</code> | |<code>variable-pitch</code> | ||
|适合文章阅读的比例字体 | |适合文章阅读的比例字体 | ||
|Noto Serif CJK SC | |||
|宋体 | |||
|DejaVu Serif | |||
|Segoe UI | |||
|- | |- | ||
|<code>variable-pitch-text</code> | |<code>variable-pitch-text</code> | ||
|与 <code>variable-pitch</code> 类似,但默认稍大,更适合长文 | |与 <code>variable-pitch</code> 类似,但默认稍大,更适合长文 | ||
| | |||
| | |||
|} | |} | ||
pitch = 一个字符在横向排版时占据的格子宽度,也就是写完这个字符后,光标向右前进的距离。 | |||
{| class="wikitable" | {| class="wikitable" | ||
|+fontset 层:为不同字符指定字体 | |+fontset 层:为不同字符指定字体 | ||
| Line 95: | Line 122: | ||
== 平台 == | == 平台 == | ||
* 字体配置一般放在主题加载之后,避免主题再次修改 default、fixed-pitch 或 variable-pitch。 | |||
* set-fontset-font 应尽量在汉字、Emoji 等字符首次显示前执行,因为 Emacs 会缓存某些 script 所采用的字体。 | |||
<pre> | |||
Windows 使用 Windows 自带字体。 | |||
Debian GUI 使用自由字体。 | |||
default 使用绝对字号。 | |||
fixed-pitch、variable-pitch 使用相对字号。 | |||
字体家族和字号分开设置。 | |||
中文字体由 fontset 单独处理。 | |||
</pre> | |||
=== Windows === | === Windows === | ||
<syntaxhighlight lang="elisp" line> | <syntaxhighlight lang="elisp" line> | ||
;;; init-windows.el --- | ;;; init-windows.el -*- lexical-binding: t; -*- | ||
;; -- | ;; 基础字体:编程、普通文本 | ||
(set-face-attribute 'default nil | |||
:family "Consolas" | |||
:height 170) | |||
;; | ;; 明确要求等宽字体的内容 | ||
(set-face-attribute | (set-face-attribute 'fixed-pitch nil | ||
:family "Consolas") | |||
;; | ;; 文章阅读等比例字体内容 | ||
(set-face-attribute 'variable-pitch nil | |||
(set-face-attribute | :family "Segoe UI") | ||
;; | ;; 中文 | ||
(set-fontset-font | (set-fontset-font | ||
"fontset-default" | "fontset-default" | ||
'han | 'han | ||
(font-spec :family " | (font-spec :family "SimSun")) | ||
(provide 'init-windows) | (provide 'init-windows) | ||
| Line 138: | Line 160: | ||
=== Debian-GUI === | === Debian-GUI === | ||
<syntaxhighlight lang="elisp" line> | <syntaxhighlight lang="elisp" line> | ||
(setq x-gtk-use-native-input t) | |||
;; -- | ;; 基础字体:编程、普通文本 | ||
(set-face-attribute 'default nil | |||
:family "DejaVu Sans Mono" | |||
:height 170) | |||
;; | ;; 明确要求等宽字体的内容 | ||
(set-face-attribute | (set-face-attribute 'fixed-pitch nil | ||
:family "DejaVu Sans Mono") | |||
;; | ;; 文章阅读等比例字体内容 | ||
(set-face-attribute | (set-face-attribute 'variable-pitch nil | ||
:family "DejaVu Serif") | |||
;; | ;; 中文 | ||
(set-fontset-font | (set-fontset-font | ||
"fontset-default" | "fontset-default" | ||
'han | 'han | ||
(font-spec :family " | (font-spec :family "AR PL UKai TW MBE")) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 21:10, 20 July 2026
在 GUI Frame 中,Emacs 通过 face 和 fontset,从系统已安装的字体中决定字族、字号、粗体斜体、中文字体、Emoji、符号及字体回退链。
在 TUI Frame 中,字族和字号由终端模拟器或 Linux 虚拟控制台决定;Emacs 仍然负责 face、颜色、语法高亮,并向终端请求粗体、斜体等显示效果。终端不支持的属性会被忽略。
总览
字体设置极简总览:
;; 统一控制基础字号
(set-face-attribute
'default nil
:height 160)
;; 汉字优先使用 Noto Sans CJK SC
(set-fontset-font
"fontset-default"
'han
(font-spec :family "Noto Sans CJK SC"))
;; 主字体缺少某些汉字时,再尝试文泉驿正黑
(set-fontset-font
"fontset-default"
'han
(font-spec :family "WenQuanYi Zen Hei")
nil
'append)
调试
需要调试,可以 M-x eval-expression 执行下列命令:
;; 列出当前 Frame 可使用的字体家族
(font-family-list)
;; 字体查找,能找到就有,找不着就 nil
(find-font
(font-spec :family "DejaVu Sans Mono"))
;; 字体查找,能找到就有,找不着就 nil
(member "Noto Serif CJK SC" (font-family-list))
;; 查找实际使用的英文字体
(face-font 'default nil ?A)
;; 查找实际使用的中文字体
(face-font 'default nil ?中)
将光标放在字符上,执行 C-u C-x =(what-cursor-position),可以查看字符码点、script、charset 和实际显示字体。
执行 M-x describe-face,输入 default、fixed-pitch 或 variable-pitch,查看该 face 当前的家族、字号、粗细、继承关系等。
执行 M-x describe-char,可以查看具体的字体。
核心
| face | 职责 | 中文 Debian-GUI | 中文 Windows | 英文 Debian-GUI | 英文 Windows |
|---|---|---|---|---|---|
default
|
普通文本的基础,也是编程环境的默认字体 | WenQuanYi Micro Hei | MicroSoft YaHei | DejaVu Sans Mono | Consolas |
fixed-pitch
|
明确要求等宽的内容,如代码、表格、代码块 | WenQuanYi Micro Hei | MicroSoft YaHei | DejaVu Sans Mono | Consolas |
variable-pitch
|
适合文章阅读的比例字体 | Noto Serif CJK SC | 宋体 | DejaVu Serif | Segoe UI |
variable-pitch-text
|
与 variable-pitch 类似,但默认稍大,更适合长文
|
pitch = 一个字符在横向排版时占据的格子宽度,也就是写完这个字符后,光标向右前进的距离。
| 字符类别 | 一般由谁决定 |
|---|---|
| 英文、数字、普通标点 | default、fixed-pitch、variable-pitch 的字体家族
|
| 汉字 | fontset 中的 han
|
| Emoji | fontset 中的 emoji
|
| 特殊符号 | fontset 中的 symbol 或具体字符范围
|
细节
- default face 常用于设置西文基础字体,也可用于符号
- fontset 常用于设置中文等非拉丁字符,也可用于西文
- append 表示备用,prepend 表示优先
- 建议编程用等宽字体
- 建议阅读用比例字体
- Org 写作用可选比例字体
- pixelsize 依赖 DPI 和屏幕缩放比例,用 :height 可以表达更稳定的排版字号概念
- default 用
:height 160这种绝对整数,表示 16 point;其他用:height 1.1这种浮点数,表示底层字体高度的 1.1 倍 - 字族与字号不要混在一起,分开写
- 不要指望英文等宽字体同时负责显示汉字
平台
- 字体配置一般放在主题加载之后,避免主题再次修改 default、fixed-pitch 或 variable-pitch。
- set-fontset-font 应尽量在汉字、Emoji 等字符首次显示前执行,因为 Emacs 会缓存某些 script 所采用的字体。
Windows 使用 Windows 自带字体。 Debian GUI 使用自由字体。 default 使用绝对字号。 fixed-pitch、variable-pitch 使用相对字号。 字体家族和字号分开设置。 中文字体由 fontset 单独处理。
Windows
;;; init-windows.el -*- lexical-binding: t; -*-
;; 基础字体:编程、普通文本
(set-face-attribute 'default nil
:family "Consolas"
:height 170)
;; 明确要求等宽字体的内容
(set-face-attribute 'fixed-pitch nil
:family "Consolas")
;; 文章阅读等比例字体内容
(set-face-attribute 'variable-pitch nil
:family "Segoe UI")
;; 中文
(set-fontset-font
"fontset-default"
'han
(font-spec :family "SimSun"))
(provide 'init-windows)
Debian-GUI
(setq x-gtk-use-native-input t)
;; 基础字体:编程、普通文本
(set-face-attribute 'default nil
:family "DejaVu Sans Mono"
:height 170)
;; 明确要求等宽字体的内容
(set-face-attribute 'fixed-pitch nil
:family "DejaVu Sans Mono")
;; 文章阅读等比例字体内容
(set-face-attribute 'variable-pitch nil
:family "DejaVu Serif")
;; 中文
(set-fontset-font
"fontset-default"
'han
(font-spec :family "AR PL UKai TW MBE"))
Debian-TUI
;;; init-debian-tui.el --- Debian 终端环境配置
;; TUI Emacs 不负责选择字体家族和字号。
;;
;; 字体应在实际使用的终端中配置,例如:
;; GNOME Terminal、Konsole、kitty、WezTerm 或 Linux 控制台。
;;
;; 此文件故意不设置:
;; default 的 :family / :height
;; fixed-pitch 的 :family / :height
;; variable-pitch 的 :family / :height
;; fontset-default 的 han 字体
(provide 'init-debian-tui)