想一想 發表於 2019-8-27 09:52:00

Linux CentOS 7 安装字体库 & 中文字体

<h2 id="前言">前言</h2>
<p>报表中发现有中文乱码和中文字体不整齐(重叠)的情况,首先考虑的就是操作系统是否有中文字体,在CentOS&nbsp;7中发现输入命令查看字体列表是提示命令无效:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144331.png" alt="这里写图片描述">&nbsp;<br>如上图可以看出,不仅没有中文字体,连字体库都没有,那么接下来就记录一下在Linux CentOS 7中如何安装字体库以及中文字体。</p>
<h2 id="安装字体库">安装字体库</h2>
<p>在CentOS 4.x开始用fontconfig来安装字体库,所以输入以下命令即可:</p>
<pre class="prettyprint"><code class=" hljs cmake">yum -y <span class="hljs-keyword">install fontconfig</span></code></pre>
<p>当看到下图的提示信息时说明已安装成功:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144332.png" alt="这里写图片描述"></p>
<p>这时在/usr/shared目录就可以看到fonts和fontconfig目录了(之前是没有的):&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144333.png" alt="这里写图片描述"></p>
<p>接下来就可以给我们的字体库中添加中文字体了。</p>
<h2 id="添加中文字体">添加中文字体</h2>
<p>在CentOS中,字体库的存放位置正是上图中看到的fonts目录,所以我们首先要做的就是找到中文字体文件放到该目录下,而中文字体文件在我们的windows系统中就可以找到,打开c盘下的Windows/Fonts目录:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144334.jpg" alt="这里写图片描述"></p>
<p>如上图,我们只需要将我们需要的字体拷贝出来并上传至linux服务器即可,在这里我选择宋体和黑体(报表中用到了这两种字体),可以看到是两个后缀名为ttf和ttc的文件:<img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144335.png" alt="这里写图片描述">&nbsp;<br>在这之前我们还需要新建目录,首先在/usr/shared/fonts目录下新建一个目录chinese:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144336.png" alt="这里写图片描述"></p>
<p>然后就是将上面的两个字体上传至/usr/shared/fonts/chinese目录下即可:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144337.png" alt="这里写图片描述"></p>
<p>紧接着需要修改chinese目录的权限:</p>
<pre class="prettyprint"><code class=" hljs perl"><span class="hljs-keyword">chmod -R <span class="hljs-number">755 /usr/share/fonts/chinese</span></span></code></pre>
<p>接下来需要安装ttmkfdir来搜索目录中所有的字体信息,并汇总生成fonts.scale文件,输入命令:</p>
<pre class="prettyprint"><code class=" hljs cmake">yum -y <span class="hljs-keyword">install ttmkfdir</span></code></pre>
<p>当看到下图的提示信息时说明已安装成功:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144338.png" alt="这里写图片描述"></p>
<p>然后执行ttmkfdir命令即可:</p>
<pre class="prettyprint"><code class=" hljs bash">ttmkfdir <span class="hljs-operator">-e /usr/share/X11/fonts/encodings/encodings.dir</span></code></pre>
<p>最后一步就是修改字体配置文件了,首先通过编辑器打开配置文件:</p>
<pre class="prettyprint"><code class=" hljs avrasm">vi /etc/fonts/fonts<span class="hljs-preprocessor">.conf</span></code></pre>
<p>可以看到一个Font list,即字体列表,在这里需要把我们添加的中文字体位置加进去:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/160927092144339.png" alt="这里写图片描述">&nbsp;<br>然后输入:wq保存退出,最后别忘了刷新内存中的字体缓存,这样就不用reboot重启了:</p>
<pre class="prettyprint"><code class=" hljs lasso">fc<span class="hljs-attribute">-cache</span></code></pre>
<p>这样所有的步骤就算完成了,最后再次通过fc-list看一下字体列表:&nbsp;<br><img title="" src="https://www.linuxidc.com/upload/2016_09/1609270921443310.png" alt="这里写图片描述"></p>
<p>可以看到已经成功安装上了中文字体,至此安装过程就全部结束,再次查看报表可以发现中文样式和内容均已可以正常显示了。</p>
<h2 id="总结">总结</h2>
<p>简单记录一下在CentOS 7中安装字体库以及中文字体的问题,希望对遇到同样问题的同学有所帮助,The End。</p><br><br>
来源:https://www.cnblogs.com/dfsxh/p/11416474.html
頁: [1]
查看完整版本: Linux CentOS 7 安装字体库 & 中文字体