如何针对某个 face 改变一部分 charset 的字符size

解决方案总结

新版的设置字体方案出炉

https://github.com/nasyxx/emacs.d/tree/master/literate-config/ui#font

要点是:

  1. set-fontset-font 确定字符集使用什么字体
  2. face-font-rescale-alist 确定字体的缩放大小
(set-fontset-font nil charset (font-spec :family *font))
(setf (alist-get (format ".*%s.*" *font) 
                 face-font-rescale-alist 
                 nil 
                 nil 
                 'string=)
      *font-scale)

原问题

是这样的:

(set-fontset-font nil ?x (font-spec :family "xxx" :size 11))

之后似乎 ?x 的大小就被恒定住了。无论我改变 ?x 所在的 face 的 height 还是 width,?x 之外的其它字符大小都会变化,但是 ?x 不行。

问题就是,我该如何改变某个 face 下这个 ?x 的大小呢?

这个有个明显察觉到的地方。org-mode 下不同级别的 headline 大小不一样。中英文同时存在的时候,如果改变了 headline 的 height,英文可以明显发现变大变小,但是中文不行。


补一个简单可用的例子 (把 Kaiti SC 改为拥有的字体

在 org mode 下,改变 :height 为其它 小数 可以明显发现 ABC 变化,但是 不行

(set-fontset-font nil ?大 (font-spec :family "Kaiti SC" :size 9))

(set-face-attribute 'org-level-1 nil :height 1.4)

* 大ABC

然后把 :family 去掉,可以发现 可以跟着变化(

(set-fontset-font nil ?大 (font-spec  :size 9))
1 个赞

set-fontset-font 的优先级更高吧?

我用 describe-char 看了一下 set-fontset-font 有无 :faminly 的区别:

  • :family
             position: 6 of 28 (18%), column: 6
            character: 大 (displayed as 大) (codepoint 22823, #o54447, #x5927)
              charset: unicode (Unicode (ISO10646))
code point in charset: 0x5927
               script: han
               syntax: w 	which means: word
             category: .:Base, C:2-byte han, L:Left-to-right (strong), c:Chinese, h:Korean, j:Japanese, |:line breakable
             to input: type "C-x 8 RET 5927"
          buffer code: #xE5 #xA4 #xA7
            file code: #xE5 #xA4 #xA7 (encoded by coding system utf-8)
              display: by this font (glyph code)
    mac-ct:-*-Kaiti SC-bold-normal-normal-*-9-*-*-*-p-0-iso10646-1 (#x3FF)
                              字体和大小被锁定^^

Character code properties: customize what to show
  name: CJK IDEOGRAPH-5927
  general-category: Lo (Letter, Other)
  decomposition: (22823) ('大')

There are text properties here:
  face                 org-level-1
  fontified            t

[back]
  • :family
             position: 6 of 28 (18%), column: 6
            character: 大 (displayed as 大) (codepoint 22823, #o54447, #x5927)
              charset: unicode (Unicode (ISO10646))
code point in charset: 0x5927
               script: han
               syntax: w 	which means: word
             category: .:Base, C:2-byte han, L:Left-to-right (strong), c:Chinese, h:Korean, j:Japanese, |:line breakable
             to input: type "C-x 8 RET 5927"
          buffer code: #xE5 #xA4 #xA7
            file code: #xE5 #xA4 #xA7 (encoded by coding system utf-8)
              display: by this font (glyph code)
    mac-ct:-*-Arial Unicode MS-bold-normal-normal-*-16-*-*-*-p-0-iso10646-1 (#x2BB6)
                               字体大小随 headline 变化^^

Character code properties: customize what to show
  name: CJK IDEOGRAPH-5927
  general-category: Lo (Letter, Other)
  decomposition: (22823) ('大')

There are text properties here:
  face                 org-level-1
  fontified            t

[back]
1 个赞

不知是什么样的需求,是否可以增加一个额外的 face 来实现?

看起来没救了(也许可以尝试下 overlay ?

大概最主要的就是希望 org-mode headline 的中英文一起变大变小吧。

前面说了一下,就是最近发现 org-mode headline 的中英文大小不一样,英文放大了,中文没有,想来是 set-fontset-font 指定过中文字体。

1 个赞

我之前发现给中文设置字体大小后就不能缩放了,把 :size 15 删掉就正常了。

  (dolist (charset '(kana han symbol cjk-misc bopomofo))
    (set-fontset-font (frame-parameter nil 'font)
                      charset (font-spec :family "PingFang SC"
                                         :size 15)))

我是这样设置的,仅供参考。 :smile:

(set-face-attribute 'default nil :font "SF Mono 18")

(dolist (charset '(kana han symbol cjk-misc bopomofo))
  (set-fontset-font (frame-parameter nil 'font)
                    charset (font-spec :family "PingFang SC")))

1 个赞

是的,之前用doom-emacs的时候,我就发现了这个问题,如果单独设置了中文字体的大小,就没有办法用 doom 定义的函数来缩放中文字体了,但是把size去掉就好了。后面我使用了替代方案,就是使用 face-font-rescale-alist 来设置中文字体的缩放。

1 个赞

嗯,反正 size/family 删一个(但这俩删谁都会导致不等宽

所以,现在要做的有两个事,一个是 set-fontset-font 来确定 family,一个是 face-font-rescale-alist 来确定大小(真难…

2 个赞

可以参考下我的方法,特点是在variable-pitch里面将 sarasa 字体恢复原 scale 以与Iosevka字体等高等宽,而在其它的时候 rescale 以和 comic mono 字体等高(为了 org-mode 的表格还有 tag 对齐):smiley_cat:

1 个赞

謝謝,不過不用啦~

我旳字軆用旳比較雜,不同 charset 字軆不一像,已經調整好啦(

https://github.com/nasyxx/emacs.d/tree/master/literate-config/ui#font

1 个赞