Skip to content

Instantly share code, notes, and snippets.

@chirimenmonster
Last active September 5, 2021 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chirimenmonster/a00d9bcf743dee9169627103c0bff03d to your computer and use it in GitHub Desktop.
Save chirimenmonster/a00d9bcf743dee9169627103c0bff03d to your computer and use it in GitHub Desktop.
Ghostscript で日本語フォントの表示確認 (横書き・縦書きとも)
%!PS-Adobe-3.0
/FontEncoding (-UniJIS-UTF8) def
/FontSizeLabel 12 def
/FontSizeSample 16 def
/LineSep 20 def
/LabelSep 12 def
/ErrorFont /Courier def
/MarkerSize 2 def
/BorderColor { 0.8 0.8 0.8 } def
/BackgroundColor { 1 1 1 } def
% ページサイズをA4に設定 (-sPAPERSIZE が指定されていない場合)
% gs であれば userdict にページサイズ設定用の命令 a4 が定義されていると
% 期待できるが、定義されていない場合はサイズを直接指定する
systemdict /PAPERSIZE known not {
userdict /a4 known { a4 } { << /PageSize [595 842] >> setpagedevice } ifelse
} if
% ページサイズの取得
/GetPageSize {
currentpagedevice /PageSize get
aload pop
} def
% ページの初期化
/ClearPage {
currentrgbcolor
0 0 GetPageSize 4 copy
BackgroundColor setrgbcolor rectfill % ページ全体を BackgroundColor で塗りつぶす
BorderColor setrgbcolor rectstroke % 枠線の描画
setrgbcolor
} def
% 文字列の結合
/Cat {
2 copy length exch length add string % 2文字列の合計長さ分の文字列を確保
dup 4 3 roll
dup length 3 1 roll
0 exch putinterval
exch dup 4 2 roll exch putinterval
} def
% フォント名の生成
/GetFontName {
256 string cvs % 文字列への変換
FontEncoding Cat
IsVertical { (-V) } { (-H) } ifelse Cat
} def
% currentpoint にマーカーを描画
% - DrawCurrentPoint
/DrawCurrentPoint {
currentrgbcolor
1 0 0 setrgbcolor
currentpoint currentpoint MarkerSize 2 div 0 360 arc fill moveto
setrgbcolor
} def
% 現在のフォントが横書きがどうかを調べる
/IsCurrentFontHorizontal {
currentfont /WMode known {
currentfont /WMode get 0 eq { true } { false } ifelse
} {
true
} ifelse
} def
% 横書きの場合描画位置をフォントサイズの0.3倍だけ下げる
% ベースラインからのフォント高さの取得方法がわからないので、0.3とした
/DriftCurrentpoint {
IsCurrentFontHorizontal {
currentfont /ScaleMatrix get 0 get % フォントサイズを取得
-0.3 mul
0 exch rmoveto
} if
} def
% 現在の位置に対し、右詰めで文字列を描画する
/ShowAlignRight {
dup stringwidth pop neg 0 rmoveto
show
} def
% ラベルの出力
% string LabelShow -
% string: ラベル文字列
/ShowLabel {
(IPAPMincho) FontEncoding Cat (-H) Cat findfont
FontSizeLabel scalefont setfont
DriftCurrentpoint
ShowAlignRight
} def
% フォントが読み込めなかった場合のエラーメッセージ描画
/ShowError {
/Courier findfont FontSizeLabel scalefont setfont
(font is not found)
IsVertical {
gsave
-90 rotate
DriftCurrentpoint
show
grestore
} {
DriftCurrentpoint
show
} ifelse
} def
% フォントサンプルの出力
% name SampleShow -
% name: フォント名
/ShowFontSample {
save exch
GetFontName
{
findfont
} stopped % 指定のフォント名に対し findfont が失敗した場合に備える
{
$error /newerror false put
pop % 失敗: スタックからフォント名を pop して出力処理をスキップ
ShowError
} {
FontSizeSample scalefont setfont
(書体(フォント)のサンプルを、比較する。ABC)
DriftCurrentpoint
gsave
DrawCurrentPoint
dup stringwidth rmoveto
DrawCurrentPoint
grestore
show
} ifelse
restore
} def
% フォントのサンプル出力
/TestStroke { % フォント説明 フォント名
gsave
gsave
IsVertical { -90 rotate } if
ShowLabel
grestore
IsVertical {
0 LabelSep neg rmoveto
} {
LabelSep 0 rmoveto
} ifelse
ShowFontSample
grestore
IsVertical {
LineSep neg 0 rmoveto
} {
0 LineSep neg rmoveto
} ifelse
} def
/FontList [
[ /IPAMincho (IPA明朝 ver.003.03) ]
[ /IPAPMincho (IPAP明朝 ver.003.03) ]
[ /IPAexMincho (IPAex明朝 ver.004.01) ]
[ /HiraginoMin-W3-90ms-RKSJ-H (ヒラギノ明朝体3等幅) ]
[ /HiraginoMin-W3-90msp-RKSJ-H (ヒラギノ明朝体3) ]
[ /HiraginoKaku-W5-90ms-RKSJ-H (ヒラギノ角ゴ5等幅) ]
[ /HiraginoKaku-W5-90msp-RKSJ-H (ヒラギノ角ゴ5) ]
[ /HiraginoGyoDS-W4-90ms-RKSJ-H (ヒラギノ行書等幅) ]
[ /HiraginoGyoDS-W4-90msp-RKSJ-H (ヒラギノ行書) ]
[ /BIZ-UDMincho-Medium (BIZ UD明朝 Medium) ]
[ /BIZ-UDPMincho-Medium (BIZ UDP明朝 Medium) ]
[ /BIZ-UDGothic-Regular (BIZ UDゴシック Regular) ]
[ /BIZ-UDPGothic-Regular (BIZ UDPゴシック Regular) ]
[ /BIZ-UDGothic-Bold (BIZ UDゴシック Bold) ]
[ /BIZ-UDPGothic-Bold (BIZ UDPゴシック Bold) ]
[ /UDDigiKyokashoN-R (UDデジタル教科書体 N-R) ]
[ /UDDigiKyokashoNP-R (UDデジタル教科書体 NP-R) ]
[ /UDDigiKyokashoNK-R (UDデジタル教科書体 NK-R) ]
[ /MS-Mincho (MS明朝) ]
[ /MS-Gothic (MSゴシック) ]
[ /HaranoAjiMincho-Regular (原ノ味明朝 Regular 20210410) ]
[ /HaranoAjiGothic-Medium (原ノ味ゴシック Medium 20210410) ]
] def
ErrorFont findfont pop
%
/IsVertical false def
ClearPage
0 GetPageSize exch pop moveto
200 -40 rmoveto
FontList { aload pop TestStroke } forall
showpage
/IsVertical true def
ClearPage
GetPageSize moveto
-40 -200 rmoveto
FontList { aload pop TestStroke } forall
showpage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment