87

GitHub - tumashu/cnfonts: emacs中文字体配置工具。可以快速方便的的实现中文字体和...

 6 years ago
source link: https://github.com/tumashu/cnfonts
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Note: this file is auto converted from cnfonts.el by el2org, please do not edit it by hand!!!

cnfonts README

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)添加如下代码:

    (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)
    

最简单的用法(懒人必备)

通过下面几个命令,用户可以 快速 了解 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, 如果想使用其它有意义的名称,可以设置:

(setq cnfonts-profiles
    '("program" "org-mode" "read-book"))

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

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

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

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

img

img

img

img

img

img

img

注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' 对应的文件中。

img

使用 cnfonts-use-system-type

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

让 cnfonts 随着 Emacs 自动启动

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

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

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

cnfonts 与 org-mode 配合使用

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

(setq cnfonts-use-face-font-rescale t)

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

cnfonts 高级功能

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

    (push '(#x3400 . #x4DFF) cnfonts-ornaments)
    

    注意事项:

    1. "(#x3400 . #x4DFF)" 代表了待设字符在 unicode-bmp 中的范围。
    2. 用户可以通过下面的方式来确定待字符的范围
      1. 运行 `describe-char' 来显示 待设字符 的信息
      2. 点击 “code point in charset” 处的链接,来显示整个 unicode-bmp 表
    3. 如果遇到 部分符号 无法正确对齐,可以参考:
      1. #64 (comment)
  2. 设置行距随着字号自动调整

    (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)
    
  1. 如果用户需要在自己的 Emacs 配置中管理一些个人字体,可以使用变量 `cnfonts-personal-fontnames' , 其结构与 `cnfonts–fontnames-fallback'一样。

  2. 使用命令: `describe-char' 可以了解光标处字符使用什么字体。

  3. 在 scratch 中写一行 elisp 代码:

    (cl-prettyprint (font-family-list))
    

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

  4. Windows 用户 (特别是 Windows XP 用户) 可以安装 MacType 软件来优化字体显示效果,推荐使用。

  5. Mac 用户配置 profile 文件的时候,偶尔会遇到 'C-c C-c' 刷新缓慢的问题,这可能是 ext-b 字体缺失引起的,建议安装 ext-b 字体试试。

  6. 字体设置和 coding 设置也有关系,如果 cnfonts 的行为很奇怪,又找不到确切原因,可以参考: #54 (comment)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK