ubuntu16.04下gmt5.4.1 中文支持

參考具神http://seisman.info/gmt-chinese-under-linux.htmlhtml

  • 操做系統:ubuntu16.04
  • ghostscript:9.18
  • GMT:5.4.1

(1)字體拷貝

打開teamviewer,遠程文件傳輸,windows系統 c:/windows/fonts 中拷貝最經常使用的simsun.ttc simfang.ttf simhei.ttf simkai.ttf四種中文字體。linux

/usr/share/fonts/目錄下。ubuntu

(2)對 gs 的中文配置文件作以下修改:

  1. gs 中文配置文件能夠用以下命令安裝(默認已安裝):windows

    sudo apt-get install poppler-data
  2. gs 中文配置文件路徑爲:/etc/ghostscript/cidfmap.d/90gs-cjk-resource-gb1.conf
  3. gs 中文配置文件中默認使用的 Linux 字體爲 uming 和 ukai,須要經過以下命令安裝:bash

    sudo apt-get install fonts-arphic-uming fonts-arphic-ukai
  4. gs 中文配置文件的默認內容爲:編輯器

    /BousungEG-Light-GB <</FileType /TrueType /Path (/usr/share/fonts/truetype/arphic/uming.ttc) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /GBZenKai-Medium    <</FileType /TrueType /Path (/usr/share/fonts/truetype/arphic/ukai.ttc) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /Song-Medium /GBZenKai-Medium ;
    /STSong-Light /BousungEG-Light-GB ;
    /STFangsong-Light /BousungEG-Light-GB ;
    /STHeiti-Regular /BousungEG-Light-GB ;
    /STKaiti-Regular /BousungEG-Light-GB ;
    /Adobe-GB1      /BousungEG-Light-GB ;
    /Adobe-GB1-Bold /GBZenKai-Medium ;

    須要將該文件改爲:post

    % 原配置文件的內容,與 STSong-Light 等相關的四行必須刪除
    /BousungEG-Light-GB <</FileType /TrueType /Path (/usr/share/fonts/truetype/arphic/uming.ttc) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /GBZenKai-Medium    <</FileType /TrueType /Path (/usr/share/fonts/truetype/arphic/ukai.ttc) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /Song-Medium /GBZenKai-Medium ;
    /Adobe-GB1      /BousungEG-Light-GB ;
    /Adobe-GB1-Bold /GBZenKai-Medium ;
    
    % 新增 Windows 字體的支持
    /STSong-Light <</FileType /TrueType /Path (/usr/share/fonts/simsun.ttc) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /STFangsong-Light <</FileType /TrueType /Path (/usr/share/fonts/simfang.ttf) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /STHeiti-Regular <</FileType /TrueType /Path (/usr/share/fonts/simhei.ttf) /SubfontId 0 /CSI [(GB1) 4] >> ;
    /STKaiti-Regular <</FileType /TrueType /Path (/usr/share/fonts/simkai.ttf) /SubfontId 0 /CSI [(GB1) 4] >> ;

    修改完 gs 中文配置文件後,必需要執行以下命令:測試

    $ sudo update-gsfontmap

    該命令會將 /etc/ghostscript/cidfmap.d/*.conf 合併成單獨的文件 /var/lib/ghostscript/fonts/cidfmap 。gs 在須要中文字體時會讀取 /var/lib/ghostscript/fonts/cidfmap 而不是 /etc/ghostscript/cidfmap.d/*.conf 。這是 Ubuntu/Debian 和 CentOS 的一個很大不一樣。字體

(3)測試 gs 對 Linux 默認字體的支持

ghostscript 中文配置文件90gs-cjk-resource-gb1.conf中,默認有四行,分別定義了四個字體名,儘管本質上這四個 字體名都指向同一個字體。下面先測試一下如何讓 gs 顯示 Linux 的默認字體。編碼

編輯器新建一個 PS 文件(是的,PS 文件其中就是純文本,能夠直接用編輯器編輯!),名爲 linux_fonts.ps ,其內容爲:

%! PS-Adobe-3. 0
/BousungEG-Light-GB--UniGB-UTF8-H findfont 20 scalefont setfont
150 400 moveto
(BousungEG 字體) show

/GBZenKai-Medium--UniGB-UTF8-H findfont 20 scalefont setfont
150 375 moveto
(GBZenKai 字體) show

/MSungGBK-Light--UniGB-UTF8-H findfont 20 scalefont setfont
150 350 moveto
(MSungGBK 字體) show

/Adobe-GB1--UniGB-UTF8-H findfont 20 scalefont setfont
150 325 moveto
(Adobe 字體) show

showpage
%%Trailer
%%EOF

簡單解釋一下,PS 文件中要使用某個中文字體,須要用 FontName--CMap 的格式來調用。其中 FontName 即 gs 中文配置文件中給定的字體名。CMap 能夠取 UniGB-UTF8-HGB-EUC-H, Linux 下通常用前者,Windows 下通常用後者,應該是用於指定漢字或中文字體的編碼,具體原理不知。

用 gs 查看該 PS 文件,正常狀況下顯示以下圖,代表 gs 能夠正常顯示 Linux 下的默認中文字體。

這個字體亂碼,不能正常顯示。個人系統中MSungGBK

(4)測試 gs 對 Windows 中文字體的支持

編輯器新建一個 PS 文件,名爲 windows_fonts.ps ,其內容爲:

%! PS-Adobe-3. 0
/STSong-Light--UniGB-UTF8-H findfont 20 scalefont setfont
150 400 moveto
(Song Typeface 宋體) show

/STFangsong-Light--UniGB-UTF8-H findfont 20 scalefont setfont
150 375 moveto
(Fangsong Typeface 仿宋體) show

/STHeiti-Regular--UniGB-UTF8-H findfont 20 scalefont setfont
150 350 moveto
(Hei Typeface 黑體) show

/STKaiti-Regular--UniGB-UTF8-H findfont 20 scalefont setfont
150 325 moveto
(Kai Typeface 楷體) show

showpage
%%Trailer
%%EOF

用 gs 查看該 PS 文件,若正確顯示中文以下圖,則代表 gs 已支持 Windows 字體。

到這一步, ghostscript的windows中文字體正常顯示

(5)gmt中文配置

修改 GMT 字體配置文件

打開 GMT 字體配置文件 /opt/GMT-5.4.1/share/postscriptlight/PSL_custom_fonts.txt,在文件顯示的註釋最後一行#Fontname            Fontheight    Encoded

後加入 以下語句(以 Windows 下的四大經常使用字體爲例):

STSong-Light--UniGB-UTF8-H  0.700    1
STFangsong-Light--UniGB-UTF8-H  0.700    1
STHeiti-Regular--UniGB-UTF8-H   0.700   1
STKaiti-Regular--UniGB-UTF8-H   0.700   1

第一列爲字體名,第二列爲字母 A 的高度,第三列與編碼有關。

查看 GMT 當前支持的字體

pstext -L 命令查看 GMT 當前的字體配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
$ pstext -L
Font # Font Name
------------------------------------
0 Helvetica
1 Helvetica-Bold
... ......
32 Palatino-BoldItalic
33 ZapfChancery-MediumItalic
34 ZapfDingbats
35 STSong-Light--UniGB-UTF8-H
36 STFangsong-Light--UniGB-UTF8-H
37 STHeiti-Regular--UniGB-UTF8-H
38 STKaiti-Regular--UniGB-UTF8-H

其中 0-34 爲 GMT/gs 默認支持的西文字體,35 至 38 爲新添加的中文字體。

使 GMT 支持中文

GMT5 測試腳本:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
gmt gmtset FONT_TITLE 40p,35,black
gmt pstext -R0/10/0/3 -JX15c/3c -Bafg -B+t "GMT中文支持" -F+a+c+f -P > gmt5_cn.ps << EOF
3 2.1 0 LM 35p,35,red GMT宋體
3 0.9 0 LM 35p,36,blue GMT仿宋
7 2.1 0 LM 35p,37,black GMT黑體
7 0.9 0 LM 35p,38,green GMT楷體
EOF
rm gmt.*

中文顯示成功。

相關文章
相關標籤/搜索