Jade Dungeon

查询系统中的字体

查找已经安装的字体

查看已经安装的字体:

fc-list "family1,family2,...:key1=value1:key2=value2;..."

只显示中文字体:

fc-list :lang=zh

查找指定的字体:

fc-list :lang=zh | sort | grep 'Source Han' | less

查找指定的字体簇,字体簇名称必须匹配完整的大小写,可以忽略空格:

fc-list "Source Han Sans CN"

字体簇加上筛选条件:

fc-list "Source Han Sans CN:style=Bold"
fc-list ":color=true"                   # 支持Emoji的彩色字体
fc-list ":variable=true"                # 支持可变字体
fc-list ":spacing=mono"                 # 等宽字体
fc-list ":spacing=dual"                 # 全宽/半宽风格的中文等宽字体

查看字体属性

fc-query查看字体的信息,所查看的字体不一定要安装到系统中。

匹配已经安装的字体

fc-match按指定的字体族尝试匹配,如果没有精确匹配则尝试平替方案。

默认只返回一条结果:

$ fc-match "Inconsolata"
UbuntuMono-R.ttf: "Ubuntu Mono" "Regular"

-s-a匹配所有结果:

$ fc-match -s "Consolas" | head -n 20
consola.ttf: "Consolas" "Regular"
UbuntuMono-R.ttf: "Ubuntu Mono" "Regular"
NotoSansCJK-Regular.ttc: "Noto Sans Mono CJK SC" "Regular"
PowerlineSymbols.otf: "PowerlineSymbols" "Medium"
DejaVuSansMono.ttf: "DejaVu Sans Mono" "Book"
DejaVuSansMono-Bold.ttf: "DejaVu Sans Mono" "Bold"
Andale_Mono.ttf: "Andale Mono" "Regular"
cour.ttf: "Courier New" "Regular"
Courier_New.ttf: "Courier New" "Regular"
Courier_New_Italic.ttf: "Courier New" "Italic"
n022003l.pfb: "Nimbus Mono L" "Regular"
pcrr8a.pfb: "Courier" "Regular"
TlwgTypo.ttf: "Tlwg Typo" "Regular"
wqy-zenhei.ttc: "WenQuanYi Zen Hei Mono" "Regular"
KhmerOSsys.ttf: "Khmer OS System" "Regular"
simsun.ttc: "NSimSun" "Regular"
bsmi00lp.ttf: "AR PL Mingti2L Big5" "Regular"
UnDotum.ttf: "UnDotum" "Regular"
mitra.ttf: "Mitra Mono" "Regular"
Lohit-Bengali.ttf: "Lohit Bengali" "Regular"

解析字体Pattern

fc-pattern命令解析查询的内容到对应的pattern

$ fc-pattern "Consolas"
Pattern has 1 elts (size 16)
        family: "Consolas"(s)

fc-pattern -c会把系统中的字体匹配对应到查询内容上。下面的例子中可以看到系统配置 中对应的很长一串替代和备用字体:

$ fc-pattern -c "Consolas" 
Pattern has 6 elts (size 16)
        family: "PowerlineSymbols"(w) "PowerlineSymbols"(w) "PowerlineSymbols"
(w) "Consolas"(s) "PowerlineSymbols"(w) "PowerlineSymbols"(w) "Ubuntu Mono"(s) 
"Noto Sans Mono CJK SC"(s) "Noto Sans Mono CJK TC"(s) "Noto Sans Mono CJK JP"
(s) "Noto Sans Mono CJK KR"(s) "DejaVu Sans Mono"(w) "DejaVu LGC Sans Mono"(w) 
"DejaVu LGC Sans Mono"(w) "DejaVu Sans Mono"(w) "Bitstream Vera Sans Mono"(w) 
"Inconsolata"(w) "Andale Mono"(w) "Courier New"(w) "Cumberland AMT"(w) "Luxi Mo
no"(w) "Nimbus Mono L"(w) "Nimbus Mono"(w) "Nimbus Mono PS"(w) "Courier"(w) "Tl
wgTypo"(w) "TlwgTypist"(w) "TlwgMono"(w) "Noto Sans Mono CJK JP"(w) "Noto Sans 
Mono CJK KR"(w) "Noto Sans Mono CJK SC"(w) "Noto Sans Mono CJK TC"(w) "Noto San
s Mono CJK HK"(w) "WenQuanYi Zen Hei Mono"(w) "Khmer OS System"(w) "Miriam Mono
"(w) "VL Gothic"(w) "IPAMonaGothic"(w) "IPAGothic"(w) "Sazanami Gothic"(w) "Koc
hi Gothic"(w) "AR PL KaitiM GB"(w) "MS Gothic"(w) "UmePlus Gothic"(w) "NSimSun"
(w) "MingLiu"(w) "AR PL ShanHeiSun Uni"(w) "AR PL New Sung Mono"(w) "HanyiSong"
(w) "AR PL SungtiL GB"(w) "AR PL Mingti2L Big5"(w) "ZYSong18030"(w) "NanumGothi
cCoding"(w) "NanumGothic"(w) "UnDotum"(w) "Baekmuk Dotum"(w) "Baekmuk Gulim"(w) 
"TlwgTypo"(w) "TlwgTypist"(w) "TlwgTypewriter"(w) "TlwgMono"(w) "Hasida"(w) "Mi
tra Mono"(w) "GF Zemen Unicode"(w) "Hapax Berbère"(w) "Lohit Bengali"(w) "Lohit 
Gujarati"(w) "Lohit Hindi"(w) "Lohit Marathi"(w) "Lohit Maithili"(w) "Lohit Kas
hmiri"(w) "Lohit Konkani"(w) "Lohit Nepali"(w) "Lohit Sindhi"(w) "Lohit Punjabi
"(w) "Lohit Tamil"(w) "Meera"(w) "Lohit Malayalam"(w) "Lohit Kannada"(w) "Lohit 
Telugu"(w) "Lohit Oriya"(w) "LKLUG"(w) "WenQuanYi Micro Hei Mono"(w) "FreeMono"
(w) "monospace"(w) "Terafik"(w) "monospace"(w) "FreeSans"(w) "Arial Unicode MS"
(w) "Arial Unicode"(w) "Code2000"(w) "Code2001"(w) "sans-serif"(w) "sans-serif"
(w) "sans-serif"(w) "sans-serif"(w) "sans-serif"(w)
        antialias: True(w)
        hintstyle: 1(i)(w)
        lang: "en"(w)
        lcdfilter: 1(i)(w)
        prgname: "fc-pattern"(s)

  • 字体后带(s)表示strong强匹配,表示指定要使用的字体。
  • 字体后带(w)表示weak弱匹配,表示通过匹配文件扩展出来的替换字体。
  • 字体配置中的binging规则可以修改strongweak的值。

再加上-d参数会补充一些查询字段的缺少值,这才是font-config中真正用到的查询

$ fc-pattern -c -d "Consolas" 
Pattern has 6 elts (size 16)
        family: "PowerlineSymbols"(w) "PowerlineSymbols"(w) "PowerlineSymbols"
(w) "Consolas"(s) "PowerlineSymbols"(w) "PowerlineSymbols"(w) "Ubuntu Mono"(s) 
"Noto Sans Mono CJK SC"(s) "Noto Sans Mono CJK TC"(s) "Noto Sans Mono CJK JP"
(s) "Noto Sans Mono CJK KR"(s) "DejaVu Sans Mono"(w) "DejaVu LGC Sans Mono"(w) 
"DejaVu LGC Sans Mono"(w) "DejaVu Sans Mono"(w) "Bitstream Vera Sans Mono"(w) 
"Inconsolata"(w) "Andale Mono"(w) "Courier New"(w) "Cumberland AMT"(w) "Luxi Mo
no"(w) "Nimbus Mono L"(w) "Nimbus Mono"(w) "Nimbus Mono PS"(w) "Courier"(w) "Tl
wgTypo"(w) "TlwgTypist"(w) "TlwgMono"(w) "Noto Sans Mono CJK JP"(w) "Noto Sans 
Mono CJK KR"(w) "Noto Sans Mono CJK SC"(w) "Noto Sans Mono CJK TC"(w) "Noto San
s Mono CJK HK"(w) "WenQuanYi Zen Hei Mono"(w) "Khmer OS System"(w) "Miriam Mono
"(w) "VL Gothic"(w) "IPAMonaGothic"(w) "IPAGothic"(w) "Sazanami Gothic"(w) "Koc
hi Gothic"(w) "AR PL KaitiM GB"(w) "MS Gothic"(w) "UmePlus Gothic"(w) "NSimSun"
(w) "MingLiu"(w) "AR PL ShanHeiSun Uni"(w) "AR PL New Sung Mono"(w) "HanyiSong"
(w) "AR PL SungtiL GB"(w) "AR PL Mingti2L Big5"(w) "ZYSong18030"(w) "NanumGothi
cCoding"(w) "NanumGothic"(w) "UnDotum"(w) "Baekmuk Dotum"(w) "Baekmuk Gulim"(w) 
"TlwgTypo"(w) "TlwgTypist"(w) "TlwgTypewriter"(w) "TlwgMono"(w) "Hasida"(w) "Mi
tra Mono"(w) "GF Zemen Unicode"(w) "Hapax Berbère"(w) "Lohit Bengali"(w) "Lohit 
Gujarati"(w) "Lohit Hindi"(w) "Lohit Marathi"(w) "Lohit Maithili"(w) "Lohit Kas
hmiri"(w) "Lohit Konkani"(w) "Lohit Nepali"(w) "Lohit Sindhi"(w) "Lohit Punjabi
"(w) "Lohit Tamil"(w) "Meera"(w) "Lohit Malayalam"(w) "Lohit Kannada"(w) "Lohit 
Telugu"(w) "Lohit Oriya"(w) "LKLUG"(w) "WenQuanYi Micro Hei Mono"(w) "FreeMono"
(w) "monospace"(w) "Terafik"(w) "monospace"(w) "FreeSans"(w) "Arial Unicode MS"
(w) "Arial Unicode"(w) "Code2000"(w) "Code2001"(w) "sans-serif"(w) "sans-serif"
(w) "sans-serif"(w) "sans-serif"(w) "sans-serif"(w)
        familylang: "en"(s) "en-us"(w)                                                                           [0/226]
        stylelang: "en"(s) "en-us"(w)
        fullnamelang: "en"(s) "en-us"(w)
        slant: 0(i)(s)
        weight: 80(i)(s)
        width: 100(i)(s)
        size: 12(f)(s)
        pixelsize: 12.5(f)(s)
        antialias: True(w)
        hintstyle: 1(i)(w)
        hinting: True(s)
        verticallayout: False(s)
        autohint: False(s)
        globaladvance: True(s)
        dpi: 75(f)(s)
        scale: 1(f)(s)
        lang: "en"(w)
        fontversion: 2147483647(i)(s)
        embeddedbitmap: True(s)
        decorative: False(s)
        lcdfilter: 1(i)(w)
        namelang: "en"(s)
        prgname: "fc-pattern"(s)
        symbol: False(s)

字体配置

配置文件位置

系统的根配置文件是/etc/fonts/fonts.conf,它会载入更多具体的配置文件. 这些文件按文件名形状的序号依次生效。如:

  • /etc/fonts/config.d/10-*.conf
  • /etc/fonts/config.d/20-*.conf
  • ...
  • /etc/fonts/config.d/50-user.conf会载入用户自行编写的配置文件
    • ~/.config/fontconfig/fonts.conf
    • ~/.config/fontconfig/conf.d/*.conf
  • ...
  • /etc/fonts/config.d/90-*.conf

可以通过命令fc-conflist查询具体的配置文件的位置。

一般一为说常用的全局文件有:

sudo gedit /etc/fonts/conf.avail/69-language-selector-zh-cn.conf

常用的个人配置有:

~/.fonts
# or
/usr/share/fonts

字体配置文件:

~/.fonts.conf

# 或
/usr/share/fonts

字体配置文件格式

字体替换写法有两种:

  • match规则
  • alias规则

match规则

对于以下规则:

<match target="pattern">
	<test name="family">
		<string>Consolas</string>
	</test>
	<edit name="family" mode="append" binding="same">
		<string>Inconsolata</string>
		<string>DejaVu Serif</string>
	</edit>
</match>

如果网页上指定要显示的字体是:

Monaco,Menlo,Consolas,monospace

要显示的Consolas字体,匹配到了<test>条件, <edit>里的mode="append"就是要在Consolas后面追加指定的 的内容作为替换备选。 之前指定的字体要求就改成了:

Monaco,Menlo,Consolas,Inconsolata,DejaVu Serif,monospace
修改模式mode

mode所支持的修改方式有:

# 向前添加: mode="prepend"
Monaco,Menlo,Inconsolata,DejaVu Serif,Consolas,monospace
             -------------------------^^^^^^^^

# 向后添加: mode="append"
Monaco,Menlo,Consolas,Inconsolata,DejaVu Serif,monospace
             ^^^^^^^^ ------------------------

# 头部添加: mode="prepend_first"
Inconsolata,DejaVu Serif,,Monaco,Menlo,Consolasmonospace
-------------------------              ^^^^^^^^

# 尾部添加: mode="append_last"
Monaco,Menlo,Consolas,monospace,Inconsolata,DejaVu Serif
             ^^^^^^^^          -------------------------

# 替换: mode="assign"
Monaco,Menlo,Inconsolata,DejaVu Serif,monospace
            ^-------------------------

# 删除: mode="delete"
Monaco,Menlo,monospace
            ^             
优先级bingding

通过binding指定匹配的优先级,fc-pattern查询中:

  • 字体后带(s)表示strong强匹配,表示指定要使用的字体。
  • 字体后带(w)表示weak弱匹配,表示通过匹配文件扩展出来的替换字体。
  • 字体配置中的binging规则可以修改strongweak的值。

例如:

<match target="pattern">
	<test name="family">
		<string>Consolas</string>
	</test>
	<edit name="family" mode="append" binding="same">
		<string>Inconsolata</string>
	</edit>
</match>

指定bindingsamestrong可以让规则有更高的优先级, 如果还是不生效,可以再把mode="append"改为mode="assign"

alias规则

简化的规则写法,支持向前(prefer)向后(accept)和末尾(default)追加:

<alias binding="same">
	<family>Consolas</family>
	<prefer><family>Jetbrains Mono</family></prefer>
	<accept><family>Inconsolata</family></accept>
	<default><family>monospace</family></default>
</alias>

通过文泉驿视觉习惯工具可以很方便地生成字体配置文件:

http://wenq.org/cloud/fcdesigner.html

重建字体配置,生成索引信息:

sudo mkfontscale
sudo mkfontdir
sudo fc-cache -f -v

字体配置不是万能的

通过系统的字体配置不能解决一切问题。因为如果网站瞎写CSS,搞成这副样子:

font-family: 'Arial', 'Helvetica', 'SimSun', sans-serif;

那靠系统的字体配置是抢救不过来的。

思源字体

  • https://github.com/adobe-fonts/source-han-serif/
  • https://github.com/adobe-fonts/source-han-sans/

chrome-flash字体方块

我找到了中文显示方块的原因了 还计得导致flash 乱码的那个文件吗? /etc/fonts/conf.d/49-sansserif.conf正是这个文件 只要把 倒数第四行 改为simsun 或其它中文字体就ok了

        <edit name="family" mode="append_last">
            <string>sans-serif</string>
        </edit

修改为

        <edit name="family" mode="append_last">
            <string>sans</string>
        </edit>

使用微软字体

如果你在 Linux 上用 LibreOffice 打开一些微软文档,你会发现字体看起来有一点不同。你也将注意到有些常用字体找不到,如 Times New Roman、Arial 等等。

基于 Ubuntu 的 Linux 发行版上安装这些字体。但是在此之前,让我告诉你为什么这些字体没有被默认安装。

这些字体 可在 multiverse 存储库中获得,首先要确保启用它:

sudo add-apt-repository multiverse

在此之后,你可以更新你的 Ubuntu 存储库缓存,并像这样安装微软字体:

sudo apt update && sudo apt install ttf-mscorefonts-installer

当微软的最终用户协议出现时,按 tab 键来选择 “OK” ,并按回车键。 当安装完成后,你应该使用下面的命令来更新字体缓存:

sudo fc-cache -f -v

CJK字体显示优先级问题

如果系统的默认语言不是中文,而是英语,那么会遇到显示汉字时优先使用是文字体的问题。

比如思源字体会打包成很多版本进行安装。ubuntu里默认用的是简中、港、台、日、韩 五种字型打包在一起的版本,就可能会显示的的字形不正确,默认用日文汉字的字型。 这时要修改配置文件: /etc/fonts/conf.avail/64-language-selector-prefer.conf

把中文字形的顺序放在前面:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
	<alias>
		<family>sans-serif</family>
		<prefer>
			<family>Noto Sans CJK SC</family>
			<family>Noto Sans CJK TC</family>
			<family>Noto Sans CJK HK</family>
			<family>Noto Sans CJK JP</family>
			<family>Noto Sans CJK KR</family>
			<family>Lohit Devanagari</family>
			<family>Noto Sans Sinhala</family>
		</prefer>
	</alias>
	<alias>
		<family>serif</family>
		<prefer>
			<family>Noto Serif CJK SC</family>
			<family>Noto Serif CJK TC</family>
			<family>Noto Serif CJK JP</family>
			<family>Noto Serif CJK KR</family>
			<family>Lohit Devanagari</family>
			<family>Noto Serif Sinhala</family>
		</prefer>
	</alias>
	<alias>
		<family>monospace</family>
		<prefer>
			<family>Noto Sans Mono CJK SC</family>
			<family>Noto Sans Mono CJK TC</family>
			<family>Noto Sans Mono CJK HK</family>
			<family>Noto Sans Mono CJK JP</family>
			<family>Noto Sans Mono CJK KR</family>
		</prefer>
	</alias>
</fontconfig>