Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tumashu/cnfonts

emacs中文字体配置工具。可以快速方便的的实现中文字体和英文字体等宽(也就是常说的中英文对齐)。
https://github.com/tumashu/cnfonts

Last synced: about 2 months ago
JSON representation

emacs中文字体配置工具。可以快速方便的的实现中文字体和英文字体等宽(也就是常说的中英文对齐)。

Awesome Lists containing this project

README

        

#+title: A simple Chinese fonts config tool
#+author: Feng Shu

* 简介

cnfonts 原来叫: chinese-fonts-setup, 是一个 Emacs 中英文字体配置工
具。可以比较方便地实现中文字体和英文字体等宽(也就是大家常说的中英
文对齐)。

注:这个 package 特别适用于需要处理中英文混合表格的中文 org-mode 用
户。

* 基本原理

cnfonts 的核心很简单,就是让中文字体和英文字体使用不同的字号,从而
实现中英文对齐。

* 使用特点

cnfonts 添加了许多辅助工具,使配置和调节字体和字号的工作更加简便快
捷,它有几个优点:

1. 安装即用:cnfonts 内置字体 fallback 功能,只需安装,就能够配置中
文字体和英文字体,让中文可以 *正确* 显示(但未必完美),不会因为
Emacs 配置中指定的字体不存在而报错。

2. 设置方便:cnfonts 自带一个 profile 文件调整工具,这个工具有直观
的图形界面,可以让用户设置字体名称和字体大小,分分钟实现中文字体
和英文字体的等宽对齐。

* 下载安装

1. 配置melpa源,参考:http://melpa.org/#/getting-started
2. M-x package-install RET cnfonts RET
3. 在emacs配置文件中(比如: ~/.emacs)添加如下代码:

#+begin_example
(require 'cnfonts)
;; 让 cnfonts 在 Emacs 启动时自动生效。
(cnfonts-mode 1)
;; 添加两个字号增大缩小的快捷键
;; (define-key cnfonts-mode-map (kbd "C--") #'cnfonts-decrease-fontsize)
;; (define-key cnfonts-mode-map (kbd "C-=") #'cnfonts-increase-fontsize)
#+end_example

* 配置使用
** 最简单的用法(懒人必备)

通过下面几个命令,用户可以 *快速* 了解 cnfonts 的大部分功能,而不需
要阅读整篇文档,如果用户想深入了解 cnfonts 或者自定义一些特殊的功能,
阅读整篇文档是逃不开的。

| 命令 | 功能 |
|---------------------------+--------------|
| cnfonts-edit-profile | 调整字体设置 |
| cnfonts-increase-fontsize | 增大字号 |
| cnfonts-decrease-fontsize | 减小字号 |

** profile 的概念

profile 代表了一套字体配置,cnfonts 使用 profile 的概念,来维护多套
字体配置,从而实现特定的环境使用特定的字体配置,比如:在编程时使用
“Consolas + 微米黑”,在阅读文章时使用 “PragmataPro + 黑体”,等等。

每一个 profile 都对应一个 emacs-lisp 文件, 保存在
`cnfonts-directory' 对应的目录中, 这些文件包含了英文字体设置,中文
字体设置以及中文字体大小等。

** profile 命名与切换

cnfonts 默认使用三个 profile: profile1, profile2 和 profile3, 如果
想使用其它有意义的名称,可以设置:

#+begin_example
(setq cnfonts-profiles
'("program" "org-mode" "read-book"))
#+end_example

cnfonts 使用下面两个命令来切换 profile :

| Command | Help |
|------------------------+-------------------------|
| cnfonts-switch-profile | 选择并切换 profile |
| cnfonts-next-profile | 直接切换到下一个profile |

** 使用 cnfonts-edit-profile 命令调整 profile

如果 *当前使用* 的字体不符合使用习惯,用户可以运行
`cnfonts-edit-profile'命令来调整 *当前* profile,这个命令会弹出一个
图形化界面,类似:

[[file:./snapshots/cnfonts-ui-1.png]]
[[file:./snapshots/cnfonts-ui-2.png]]
[[file:./snapshots/cnfonts-ui-3.png]]
[[file:./snapshots/cnfonts-ui-4.png]]
[[file:./snapshots/cnfonts-ui-5.png]]
[[file:./snapshots/cnfonts-ui-6.png]]
[[file:./snapshots/cnfonts-ui-7.png]]

注1: 配置完成后,有可能需要重启 Emacs, 参考:
http://debbugs.gnu.org/db/17/1785.html

** 使用 cnfonts-regenerate-profile 重置 profile

`cnfonts-regenerate-profile' 命令会使用 cnfonts 自带的 fallback 信
息,覆盖需要 *重置* 的 profile, 这个 profile 原来的内容将丢失,请紧
慎使用!

** 调整字体大小
`cnfonts' 使用下述两个命令调整字体大小:

| Command | Help |
|---------------------------+--------------|
| cnfonts-increase-fontsize | 增大字体大小 |
| cnfonts-decrease-fontsize | 减小字体大小 |

注意:在调整字体大小的同时,字号信息也会保存到 `cnfonts-directory'
目录下`cnfonts-config-filename' 对应的文件中。

[[file:./snapshots/cnfonts-increase-and-decrease-fontsize.gif]]

** 使用 cnfonts-use-system-type

有些用户希望将 profile 配置文件做为自己的 Emacs 配置,在不同的计算
机上同步和管理,我建议这些用户将 `cnfonts-use-system-type'设置为 t,
这样,相同名称的 profile 在不同的操作系统下,保存的位置也不同,可以
避免 profile 冲突。

** 让 cnfonts 随着 Emacs 自动启动

`cnfonts-mode' 命令可以让 cnfonts 随着 Emacs 自动启动,这个命令将
`cnfonts-set-font' 添加到下面两个 hook:

1. `after-make-frame-functions'
2. `window-setup-hook'

用户也可以手动运行 `cnfonts-set-font' 来让 cnfonts 生效。

** cnfonts 与 org-mode 配合使用

许多用户使用 org-mode 时,习惯让不同的标题,使用的字体大小也不同,
这个特性需要用户设置:

#+begin_example
(setq cnfonts-use-face-font-rescale t)
#+end_example

注:这个功能不能在 window 系统下使用,它会让对齐功能失效,Linux 下
这个功能 *一般* 可以使用,Mac 系统未测试,同学可以亲自试一试。

** cnfonts 高级功能

*** 设置一些不常用汉字字符的字体

#+begin_example
(push '(#x3400 . #x4DFF) cnfonts-ornaments)
#+end_example

注意事项:

1. "(#x3400 . #x4DFF)" 代表了待设字符在 unicode-bmp 中的范围。
2. 用户可以通过下面的方式来确定待字符的范围
1. 运行 `describe-char' 来显示 *待设字符* 的信息
2. 点击 “code point in charset” 处的链接,来显示整个 unicode-bmp 表
3. 获取范围
3. 如果遇到 *部分符号* 无法正确对齐,可以参考:
1. https://github.com/tumashu/cnfonts/issues/64#issuecomment-296414028

*** 设置行距随着字号自动调整

#+begin_example
(defvar my-line-spacing-alist
'((9 . 0.1) (10 . 0.9) (11.5 . 0.2)
(12.5 . 0.2) (14 . 0.2) (16 . 0.2)
(18 . 0.2) (20 . 1.0) (22 . 0.2)
(24 . 0.2) (26 . 0.2) (28 . 0.2)
(30 . 0.2) (32 . 0.2)))

(defun my-line-spacing-setup (fontsizes-list)
(let ((fontsize (car fontsizes-list))
(line-spacing-alist (copy-list my-line-spacing-alist)))
(dolist (list line-spacing-alist)
(when (= fontsize (car list))
(setq line-spacing-alist nil)
(setq-default line-spacing (cdr list))))))

(add-hook 'cnfonts-set-font-finish-hook #'my-line-spacing-setup)
#+end_example

* Tips

1. 如果用户需要在自己的 Emacs 配置中管理一些个人字体,可以使用变量
`cnfonts-personal-fontnames' , 其结构与
`cnfonts--fontnames-fallback'一样。
2. 使用命令: `describe-char' 可以了解光标处字符使用什么字体。
3. 在 scratch 中写一行 elisp 代码:

#+begin_example
(cl-prettyprint (font-family-list))
#+end_example

执行后,就会在 scratch 中插入当前可用字体的名称列表,这是一个很
有用的技巧。

4. Windows 用户 (特别是 Windows XP 用户) 可以安装 MacType 软件来优
化字体显示效果,推荐使用。
5. Mac 用户配置 profile 文件的时候,偶尔会遇到 'C-c C-c' 刷新缓慢的
问题,这可能是 ext-b 字体缺失引起的,建议安装 ext-b 字体试试。
1. Ext-B字符列表: https://cdo.wikipedia.org/wiki/Wikipedia:Unicode%E6%93%B4%E5%B1%95%E6%BC%A2%E5%AD%97
2. HanaMinB 下载地址: https://osdn.jp/projects/hanazono-font/downloads/62072/hanazono-20141012.zip/
6. 字体设置和 coding 设置也有关系,如果 cnfonts 的行为很奇怪,又找
不到确切原因,可以参考:
https://github.com/tumashu/cnfonts/issues/54#issuecomment-246228904

* 参考文章
1. http://baohaojun.github.io/perfect-emacs-chinese-font.html
2. http://zhuoqiang.me/torture-emacs.html