技术

docker容器安装宋体

2018-12-06  本文已影响0人  有时右逝

前言

近期在项目开发中,遇到一个问题。程序在mac本地运行正常,弄到服务器上,运行在docker容器中异常。经过检查,问题的原因是程序使用了字体在docker中缺失。经过一番搜索,解决了这个问题,特此记录下。

原因

字体通常存在版权问题。linux上字体比较少。中文字体更是基本不会安装。

这里我使用的容器是tomcat:9-jre8-alpine

该镜像没有我需要的宋体。因此下面我来介绍下如何安装宋体。

解决

bash-4.4# fc-list :lang=zh
/usr/share/fonts/SimSun.ttf: SimSun,宋体:style=Regular
bash-4.4#

上面的表示已经安装过宋体。

yum -y install fontconfig

由于这里我的容器是alpine,它的字体软件使用下面的命令。

apk add ttf-dejavu

查看所有字体。

bash-4.4# fc-list
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed-Oblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSerif-BoldItalic.ttf: DejaVu Serif:style=Bold Italic
/usr/share/fonts/SimSun.ttf: SimSun,宋体:style=Regular
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed-Italic.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed Italic,Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSans-BoldOblique.ttf: DejaVu Sans:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed-BoldOblique.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSans-ExtraLight.ttf: DejaVu Sans,DejaVu Sans Light:style=ExtraLight
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed-Oblique.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed Oblique,Oblique
/usr/share/fonts/ttf-dejavu/DejaVuSans.ttf: DejaVu Sans:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed-Bold.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuSans-Oblique.ttf: DejaVu Sans:style=Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono-BoldOblique.ttf: DejaVu LGC Sans Mono:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono-Oblique.ttf: DejaVu Sans Mono:style=Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans.ttf: DejaVu LGC Sans:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono-Bold.ttf: DejaVu Sans Mono:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono.ttf: DejaVu LGC Sans Mono:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed-BoldItalic.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed Bold Italic,Bold Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-ExtraLight.ttf: DejaVu LGC Sans,DejaVu LGC Sans Light:style=ExtraLight
/usr/share/fonts/ttf-dejavu/DejaVuSerif-Bold.ttf: DejaVu Serif:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed-Italic.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed Italic,Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif.ttf: DejaVu LGC Serif:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-BoldOblique.ttf: DejaVu LGC Sans:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif-BoldItalic.ttf: DejaVu LGC Serif:style=Bold Italic
/usr/share/fonts/ttf-dejavu/DejaVuSerif-Italic.ttf: DejaVu Serif:style=Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed-BoldOblique.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed Bold Oblique,Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuMathTeXGyre.ttf: DejaVu Math TeX Gyre:style=Regular
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif-Italic.ttf: DejaVu LGC Serif:style=Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-Oblique.ttf: DejaVu LGC Sans:style=Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSans-Bold.ttf: DejaVu LGC Sans:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono-Bold.ttf: DejaVu LGC Sans Mono:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSans-Bold.ttf: DejaVu Sans:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSansCondensed-Bold.ttf: DejaVu Sans,DejaVu Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuSerif.ttf: DejaVu Serif:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed-Bold.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerif-Bold.ttf: DejaVu LGC Serif:style=Bold
/usr/share/fonts/ttf-dejavu/DejaVuSerifCondensed.ttf: DejaVu Serif,DejaVu Serif Condensed:style=Condensed,Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono.ttf: DejaVu Sans Mono:style=Book
/usr/share/fonts/ttf-dejavu/DejaVuSansMono-BoldOblique.ttf: DejaVu Sans Mono:style=Bold Oblique
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansCondensed-Bold.ttf: DejaVu LGC Sans,DejaVu LGC Sans Condensed:style=Condensed Bold,Bold
/usr/share/fonts/ttf-dejavu/DejaVuLGCSerifCondensed-BoldItalic.ttf: DejaVu LGC Serif,DejaVu LGC Serif Condensed:style=Condensed Bold Italic,Bold Italic
/usr/share/fonts/ttf-dejavu/DejaVuLGCSansMono-Oblique.ttf: DejaVu LGC Sans Mono:style=Oblique
/usr/share/fonts

也可以选择去网络上下载。

宋体下载

下载后放入字体目录即可。
字体通常存在以ttf和ttc为后缀的2个文件,只需要其中一个即可。

ls /usr/share/fonts -lsh
total 10264
 10256 -rw-r--r--    1 root     root       10.0M Oct 28  1998 SimSun.ttf
     4 drwxr-xr-x    3 root     root        4.0K Dec  5 02:51 encodings
     4 drwxr-xr-x    2 root     root        4.0K Dec  5 02:51 ttf-dejavu

重启容器即可启用新字体。
也可以执行下面的命令,启用新字体。

fc-cache

再次执行即可看到字体已经安装。

bash-4.4# fc-list :lang=zh
/usr/share/fonts/SimSun.ttf: SimSun,宋体:style=Regular
bash-4.4#

提示

上一篇 下一篇

猜你喜欢

热点阅读