前言
近期在項(xiàng)目開發(fā)中,遇到一個(gè)問題。程序在mac本地運(yùn)行正常,弄到服務(wù)器上,運(yùn)行在docker容器中異常。經(jīng)過檢查,問題的原因是程序使用了字體在docker中缺失。經(jīng)過一番搜索,解決了這個(gè)問題,特此記錄下。
原因
字體通常存在版權(quán)問題。linux上字體比較少。中文字體更是基本不會(huì)安裝。
這里我使用的容器是tomcat:9-jre8-alpine
該鏡像沒有我需要的宋體。因此下面我來介紹下如何安裝宋體。
解決
- 查看已經(jīng)安裝的中文字體。
bash-4.4# fc-list :lang=zh
/usr/share/fonts/SimSun.ttf: SimSun,宋體:style=Regular
bash-4.4#
上面的表示已經(jīng)安裝過宋體。
- 安裝字體軟件
如果上一步操作提示該命令不存在,則說明當(dāng)前系統(tǒng)沒有安裝字體軟件,通常執(zhí)行下面的命令進(jìn)行安裝
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
-
下載我們需要的字體文件放入到字體目錄中即可。
通常的字體我們可以去windows電腦上拷貝。
image.png
也可以選擇去網(wǎng)絡(luò)上下載。
下載后放入字體目錄即可。
字體通常存在以ttf和ttc為后綴的2個(gè)文件,只需要其中一個(gè)即可。
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
- 啟用字體
重啟容器即可啟用新字體。
也可以執(zhí)行下面的命令,啟用新字體。
fc-cache
再次執(zhí)行即可看到字體已經(jīng)安裝。
bash-4.4# fc-list :lang=zh
/usr/share/fonts/SimSun.ttf: SimSun,宋體:style=Regular
bash-4.4#
提示
- ttf和ttc的異同
下載的字體一般是ttc或ttf格式的,系統(tǒng)顯示這都是TrueType類型的字體。ttf格式的字體可以正常使用,但ttc的字體只有一些常用的漢字。
TTC是幾個(gè)TTF合成的字庫,安裝后字體列表中會(huì)看到兩個(gè)以上的字體。兩個(gè)字體中大部分字都一樣時(shí),可以將兩種字體做成一個(gè)TTC文件,現(xiàn)在常見的TTC中的不同字體,漢字一般沒有差別,只是英文符號(hào)的寬度不一樣,以便適應(yīng)不同的版面要求
