Linux安装中文字体库

2,134 阅读3分钟

背景

在用node-canvas服务器增加图片文字水印,出现文字乱码情况。首先考虑的就是操作系统是否有中文字体。

技术方案

查看本机电脑字体

// mac 路径
/Library/Fonts/

// windows 路径
C:\Windows\Fonts

查看服务器字体库

Linux 机器上没有安装中文字体库,首先检查服务器支持的中文库

$fc-list :lang=zh

发现没有输出,再查看支持的英文库

$fc-list :lang=en
/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/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

问题明确了,现在安装中文字体库就能解决问题。

安装中文字体库

1、安装 fontconfig

如果/usr/share目录没有fonts和fontconfig目录,则需要先安装字体库,输入命令:

yum -y install fontconfig

如果已安装,则跳过此步。

安装完成后,在/usr/share目录就可以看到fonts和fontconfig目录了(之前是没有的):

在CentOS中,字体库的存放位置正是上图中看到的fonts目录,所以我们首先要做的就是找到中文字体文件放到该目录下

2、创建中文字体目录

首先在/usr/share/fonts目录下新建一个目录chinese:

mkdir /usr/share/fonts/chinese

3、下载上传字体

我们可以选择本机电脑上传字体,或者下载 阿里巴巴普惠体 2.0

要点击右侧的字体包下载 image.png

下载好以后是这样的 ttf 文件

image.png

将此文件上传至linux服务器 /usr/share/fonts/chinese 下,在目录下应该是这样的

image.png

4、修改chinese目录的权限

chmod -R 755 /usr/share/fonts/chinese

5、安装ttmkfdir来搜索目录中所有的字体信息,并汇总生成fonts.scale文件,输入命令

yum -y install ttmkfdir

// 然后执行ttmkfdir命令即可:

ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

6、修改字体配置文件了,首先通过编辑器打开配置文件

vim /etc/fonts/fonts.conf

可以看到一个Font list,即字体列表,在这里需要把我们添加的中文字体位置加进去:

image.png

<!-- Font directory list -->

    <dir>/usr/share/fonts</dir>
    <dir>/usr/share/X11/fonts/Type1</dir>
    <dir>/usr/share/X11/fonts/TTF</dir>
    <dir>/usr/local/share/fonts</dir>
    <dir>/usr/share/fonts/chinese</dir>
    <dir prefix="xdg">fonts</dir>
    <!-- the following element will be removed in the future -->
    <dir>~/.fonts</dir>

<!--
  Accept deprecated 'mono' alias, replacing it with 'monospace'
-->

7、不用重启,刷新内存中的字体缓存使配置对系统生效:

fc-cache

8、检查字体库是否安装

fc-list

fc-list :lang=zh

9、重启用到字体库的服务,将字体库应用到服务中去

把以上步骤写成 shell 脚本

#!/bin/bash

sudo yum -y install fontconfig

sudo mkdir /usr/share/fonts/chinese

sudo cp -r  /home/admin/live-engine/target/live-engine/media/fontFamily/阿里巴巴普惠体\ 2.0/. /usr/share/fonts/chinese/

cd /usr/share/fonts/chinese/

sudo yum -y install ttmkfdir

sudo ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir

sudo vim /etc/fonts/fonts.conf
// 这里用 shell 修改 vim 的不会写

sudo fc-cache

当然还有更简单的,不用去创建文件夹,我们发现 <dir>/usr/share/fonts</dir> 这个路径就在里面,因此不要改动 /etc/fonts/fonts.conf文件的写法如下:

#!/bin/bash

sudo yum -y install fontconfig
sudo cp -r  {你的字体路径}/阿里巴巴普惠体\ 2.0/. /usr/share/fonts/
cd /usr/share/fonts/
sudo yum -y install ttmkfdir
sudo ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
fc-cache