Linux 下安装中文字体

本文以安装黑体simhei.ttf为例,简单演示如何在Linux下安装中文字体。

环境信息介绍

[root@thatsit ~]# cat /etc/redhat-release
CentOS Linux release 7.1.1503 (Core)
[root@thatsit ~]#

[root@thatsit ~]# uname -r
3.10.0-229.el7.x86_64
[root@thatsit ~]#

[root@thatsit ~]# echo $LANG
en_US.UTF-8
[root@thatsit ~]# cat /etc/locale.conf
LANG="zh_CN.UTF-8"
[root@thatsit ~]#

[root@thatsit ~]# locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
[root@thatsit ~]#

依赖的命令

操作依赖几个命令,如果系统中不存在需要手动安装下。

  • fc-list:列举可用的字体
  • fc-cache:生成字体信息的缓存

这两个命令包含在fontconfig包里,可以通过下面的命令进行安装。

yum install fontconfig
  • mkfontscale:生成字体索引

这个命令属于xorg-x11-font-utils工具包,可以通过下面的命令进行安装。

yum install mkfontscale

操作步骤

  • 创建一个用于存放中文字体的目录
mkdir /usr/share/fonts/zh_CN

PS: /usr/share/fonts/这个目录会在安装fontconfig包之后自动创建

  • 上传字体simhei.ttf到上述目录中

  • 构建字体索引和缓存

# cd /usr/share/fonts/zh_CN # 需要进到这个目录
mkfontscale
mkfontdir
fc-cache -fv

执行上述命令之后,在目录下可以看到一个fonts.dir和一个fonts.scale文件

[root@thatsit zh_CN]# pwd
/usr/share/fonts/zh_CN
[root@thatsit zh_CN]# ll
total 9532
-rw-r--r-- 1 root root      66 Sep  4 11:39 fonts.dir
-rw-r--r-- 1 root root      66 Sep  4 11:39 fonts.scale
-rwxrw-rw- 1 root root 9751960 Sep  4 11:38 simhei.ttf
[root@thatsit zh_CN]#

文件内容

[root@thatsit zh_CN]# cat fonts.dir
1
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-iso10646-1
[root@thatsit zh_CN]# cat fonts.scale
1
simhei.ttf -misc-simhei-medium-r-normal--0-0-0-0-p-0-iso10646-1
[root@thatsit zh_CN]#
  • 查看字体
# 查看所有可用字体
[root@thatsit zh_CN]# fc-list
/usr/share/fonts/dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/zh_CN/simhei.ttf: SimHei:style=Regular,Normal
/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
/usr/share/fonts/dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
/usr/share/fonts/dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique
[root@thatsit zh_CN]#

# 只查看中文字体,SimHei字体安装成功。
[root@thatsit zh_CN]# fc-list :lang=zh
/usr/share/fonts/zh_CN/simhei.ttf: SimHei:style=Regular,Normal
[root@thatsit zh_CN]#

dir和scale文件生成的详细过程

[root@thatsit zh_CN]# ls
simhei.ttf
[root@thatsit zh_CN]#
[root@thatsit zh_CN]#
[root@thatsit zh_CN]# mkfontscale
[root@thatsit zh_CN]# ls
fonts.scale  simhei.ttf
[root@thatsit zh_CN]# mkfontdir
[root@thatsit zh_CN]# ls
fonts.dir  fonts.scale  simhei.ttf
[root@thatsit zh_CN]# fc-cache -fv
/usr/share/fonts: caching, new cache contents: 0 fonts, 3 dirs
/usr/share/fonts/default: caching, new cache contents: 0 fonts, 2 dirs
/usr/share/fonts/default/Type1: caching, new cache contents: 35 fonts, 0 dirs
/usr/share/fonts/default/ghostscript: caching, new cache contents: 4 fonts, 0 dirs
/usr/share/fonts/dejavu: caching, new cache contents: 13 fonts, 0 dirs
/usr/share/fonts/zh_CN: caching, new cache contents: 1 fonts, 0 dirs
/usr/share/X11/fonts/Type1: caching, new cache contents: 13 fonts, 0 dirs
/usr/share/X11/fonts/TTF: skipping, no such directory
/usr/local/share/fonts: skipping, no such directory
/root/.local/share/fonts: skipping, no such directory
/root/.fonts: skipping, no such directory
/usr/share/fonts/default: skipping, looped directory detected
/usr/share/fonts/dejavu: skipping, looped directory detected
/usr/share/fonts/zh_CN: skipping, looped directory detected
/usr/share/fonts/default/Type1: skipping, looped directory detected
/usr/share/fonts/default/ghostscript: skipping, looped directory detected
/usr/lib/fontconfig/cache: cleaning cache directory
/root/.cache/fontconfig: not cleaning non-existent cache directory
/root/.fontconfig: not cleaning non-existent cache directory
/usr/bin/fc-cache-64: succeeded
[root@thatsit zh_CN]# ls
fonts.dir  fonts.scale  simhei.ttf
[root@thatsit zh_CN]#
posted @ 2019-09-04 15:05  That's_it  阅读(3530)  评论(0编辑  收藏  举报