Python/Tk Postscript の日本語フォントの問題

2012/04/07
2012/04/15 修正

目標

Python/Tk の Canvas に日本語文字を含む場合も postscript 関数で EPS ファイルに落とせるようにする。ここでは Python には ver.3.2.2、Tcl/Tk には ver.8.6b2 を使った。日本語文字を扱えるためには Tcl/Tk にパッチを当てる必要がある。そのために、Python も、Tcl/Tk もソースを取り寄せ、コンパイルしなくてはならない。

インストール目標:

実現目標:

Tcl/Tk ver.8.6b2

src=$HOME/src		# my case
tcl=$srcdir/tcl8.6b2
tk=$srcdir/tk8.6b2
rm -rf $src/build	# if exists
$tcl/macosx および $tk/macosx の中には MakefileGNUmakefile が含まれている。Mac/OSX の make は実際には gnumake なので、ファイルを指定しない場合には GNUmakefile が使われる。そして、README には GNUmakefile の使用が推奨されている。しかし、以下では Makefile を使う。理由は、GNUmakefile を思うように操るのが困難だったからである。

Tcl の compile

cd $tcl.macosx
export CFLAGS="-arch i386 -arch x86_64"
./configure --enable-framework
make -f Makefile

確認

-bash$ ls -l Tcl
-rwxr-xr-x  1 arisawa  staff  2552984  4  7 14:22 Tcl
-bash$ ls -l tclsh
-rwxr-xr-x  1 arisawa  staff  37496  4  7 14:22 tclsh
-bash$ file Tcl
Tcl: Mach-O universal binary with 2 architectures
Tcl (for architecture i386):	Mach-O dynamically linked shared library i386
Tcl (for architecture x86_64):	Mach-O 64-bit dynamically linked shared library x86_64
-bash$ file tclsh
tclsh: Mach-O universal binary with 2 architectures
tclsh (for architecture i386):	Mach-O executable i386
tclsh (for architecture x86_64):	Mach-O 64-bit executable x86_64
-bash$ 

Tcl のインストール

sudo make -f Makefile install

確認

-bash$ ls -l /usr/local/bin/tcl*
-rwxr-xr-x  1 root  wheel  12728  3 27 07:48 /usr/local/bin/tclsh8.5
-rwxr-xr-x  1 root  wheel  37496  4  7 15:11 /usr/local/bin/tclsh8.6
-bash$ 

-bash$ ls -l /usr/local/libtcl* /usr/local/tcl*
ls: /usr/local/libtcl*: No such file or directory
ls: /usr/local/tcl*: No such file or directory
-bash$ 

-bash$ ls -l /Library/Frameworks/Tcl.framework
total 56
lrwxr-xr-x  1 root  admin   24  4  7 15:11 Headers -> Versions/Current/Headers
lrwxr-xr-x  1 root  admin   31  4  7 15:11 PrivateHeaders -> Versions/Current/PrivateHeaders
lrwxr-xr-x  1 root  admin   26  4  7 15:11 Resources -> Versions/Current/Resources
lrwxr-xr-x  1 root  admin   20  4  7 15:11 Tcl -> Versions/Current/Tcl
drwxr-xr-x  5 root  admin  170  4  7 15:11 Versions
lrwxr-xr-x  1 root  admin   28  3 27 07:48 libtclstub8.5.a -> Versions/8.5/libtclstub8.5.a
lrwxr-xr-x  1 root  admin   28  4  7 15:11 libtclstub8.6.a -> Versions/8.6/libtclstub8.6.a
lrwxr-xr-x  1 root  admin   29  4  7 15:11 tclConfig.sh -> Versions/Current/tclConfig.sh
-bash$ ls -l /Library/Frameworks/Tcl.framework/Versions
total 8
drwxr-xr-x   9 root  admin  306  3 27 07:48 8.5
drwxr-xr-x  10 root  admin  340  4  7 15:11 8.6
lrwxr-xr-x   1 root  admin    3  4  7 15:11 Current -> 8.6
-bash$ ls -l /Library/Frameworks/Tcl.framework/Versions/8.6
total 5040
drwxr-xr-x   9 root  admin      306  4  7 15:11 Headers
drwxr-xr-x  10 root  admin      340  4  7 15:11 PrivateHeaders
drwxr-xr-x   7 root  admin      238  4  7 15:11 Resources
-r-xr-xr-x   1 root  admin  2552984  4  7 15:11 Tcl
-rw-r--r--   1 root  admin     9608  4  7 15:11 libtclstub8.6.a
drwxr-xr-x   3 root  admin      102  4  7 15:11 pkgconfig
-rw-r--r--   1 root  admin     6280  4  7 15:11 tclConfig.sh
-rw-r--r--@  1 root  admin      788  4  7 15:11 tclooConfig.sh
-bash$ 

動作確認

-bash$ /usr/local/bin/tclsh8.6
% puts $tcl_patchLevel
8.6b2
% puts $tcl_library
/Library/Frameworks/Tcl.framework/Versions/8.6/Resources/Scripts
% 

Tk の compile

cd $tk/macosx
export CFLAGS="-arch i386 -arch x86_64"
./configure --enable-framework --enable-aqua
make -f Makefile

確認

-bash$ ls -l wish
-rwxr-xr-x  1 arisawa  staff  66504  4  7 15:32 wish
-bash$ ls -l Tk
-rwxr-xr-x  1 arisawa  staff  2779248  4  7 15:32 Tk
-bash$ file wish
wish: Mach-O universal binary with 2 architectures
wish (for architecture i386):	Mach-O executable i386
wish (for architecture x86_64):	Mach-O 64-bit executable x86_64
-bash$ file Tk
Tk: Mach-O universal binary with 2 architectures
Tk (for architecture i386):	Mach-O dynamically linked shared library i386
Tk (for architecture x86_64):	Mach-O 64-bit dynamically linked shared library x86_64
-bash$ 

Tk のインストール

sudo make -f Makefile install

確認

-bash$ ls -l /usr/local/bin/wish*
-rwxr-xr-x  1 root  wheel  123  3 27 07:55 /usr/local/bin/wish8.5
-rwxr-xr-x  1 root  wheel  123  4  7 15:44 /usr/local/bin/wish8.6
-bash$ ls -ld /usr/local/lib/libtk* /usr/local/lib/tk*
lrwxr-xr-x   1 root  wheel    48  4  6 18:14 /usr/local/lib/libtk8.5.dylib -> /Library/Frameworks/Tk.framework/Versions/8.5/Tk
lrwxr-xr-x   1 root  wheel    60  4  6 18:14 /usr/local/lib/libtkstub8.5.a -> /Library/Frameworks/Tk.framework/Versions/8.5/libtkstub8.5.a
drwxr-xr-x  37 root  wheel  1258  3 18 17:59 /usr/local/lib/tk8.5
-rw-r--r--   1 root  wheel  3551  4  4 06:18 /usr/local/lib/tkConfig.sh
-bash$ ls -l /Library/Frameworks/Tk.framework
total 56
lrwxr-xr-x  1 root  admin   24  4  7 15:44 Headers -> Versions/Current/Headers
lrwxr-xr-x  1 root  admin   31  4  7 15:44 PrivateHeaders -> Versions/Current/PrivateHeaders
lrwxr-xr-x  1 root  admin   26  4  7 15:44 Resources -> Versions/Current/Resources
lrwxr-xr-x  1 root  admin   19  4  7 15:44 Tk -> Versions/Current/Tk
drwxr-xr-x  5 root  admin  170  4  7 15:44 Versions
lrwxr-xr-x  1 root  admin   27  3 27 07:55 libtkstub8.5.a -> Versions/8.5/libtkstub8.5.a
lrwxr-xr-x  1 root  admin   27  4  7 15:44 libtkstub8.6.a -> Versions/8.6/libtkstub8.6.a
lrwxr-xr-x  1 root  admin   28  4  7 15:44 tkConfig.sh -> Versions/Current/tkConfig.sh
-bash$ ls -l /Library/Frameworks/Tk.framework/Versions
total 8
drwxr-xr-x  9 root  admin  306  3 27 07:55 8.5
drwxr-xr-x  9 root  admin  306  4  7 15:44 8.6
lrwxr-xr-x  1 root  admin    3  4  7 15:44 Current -> 8.6
-bash$ ls -l /Library/Frameworks/Tk.framework/Versions/8.6
total 5464
drwxr-xr-x   8 root  admin      272  4  7 15:44 Headers
drwxr-xr-x  11 root  admin      374  4  7 15:44 PrivateHeaders
drwxr-xr-x   9 root  admin      306  4  7 15:44 Resources
-r-xr-xr-x   1 root  admin  2779248  4  7 15:44 Tk
-rw-r--r--   1 root  admin    11544  4  7 15:44 libtkstub8.6.a
drwxr-xr-x   3 root  admin      102  4  7 15:44 pkgconfig
-rw-r--r--   1 root  admin     3512  4  7 15:44 tkConfig.sh
-bash$ 

動作確認

puts $tcl_patchLevel
puts $tk_patchLevel
puts $tcl_library
puts $tk_library

canvas .c -width 300 -height 200
pack .c -fill both -expand 1

.c create text 150 80 -text "ABC" -font {Times 30 italic}
.c create text 150 120 -text "いろは" -font {"Hiragino Kaku Gothic Pro" 30 bold}
.c create rectangle 100 50 200 150 -outline green
button .b -text 終了 -command {
  .c postscript -file a.eps
  exit
}
pack .b
a.tcl
/usr/local/bin/wish8.6 a.tcl
を実行すれば、次図の結果を得る。
-bash$ /usr/local/bin/wish8.6 a.tcl
8.6b2
8.6b2
/Library/Frameworks/Tcl.framework/Versions/8.6/Resources/Scripts
/Library/Frameworks/Tk.framework/Versions/8.6/Resources/Scripts
-bash$ 

Fixing the results

リンクを張って次のようにする。
-bash$ ls -l /usr/local/lib/libtcl* /usr/local/lib/libtk*
lrwxr-xr-x  1 root  wheel  50  4  7 17:03 /usr/local/lib/libtcl8.6.dylib -> /Library/Frameworks/Tcl.framework/Versions/8.6/Tcl
lrwxr-xr-x  1 root  wheel  62  4  7 17:01 /usr/local/lib/libtclstub8.6.a -> /Library/Frameworks/Tcl.framework/Versions/8.6/libtclstub8.6.a
lrwxr-xr-x  1 root  wheel  48  4  6 18:14 /usr/local/lib/libtk8.5.dylib -> /Library/Frameworks/Tk.framework/Versions/8.5/Tk
lrwxr-xr-x  1 root  wheel  48  4  7 17:02 /usr/local/lib/libtk8.6.dylib -> /Library/Frameworks/Tk.framework/Versions/8.6/Tk
lrwxr-xr-x  1 root  wheel  60  4  6 18:14 /usr/local/lib/libtkstub8.5.a -> /Library/Frameworks/Tk.framework/Versions/8.5/libtkstub8.5.a
lrwxr-xr-x  1 root  wheel  60  4  7 16:59 /usr/local/lib/libtkstub8.6.a -> /Library/Frameworks/Tk.framework/Versions/8.6/libtkstub8.6.a
-bash$ 
この作業は、Python のコンパイルのために必要

Python 3.2.2

Configure

src=$HOME/src
py=$src/Python-3.2.2
unset CFLAGS
./configure --enable-framework

Makefile の修正

Fix Makefile by inserting EXTLIBRARY as shown below.
Fix by hand usig text editor.
LIBRARY=	libpython$(VERSION)$(ABIFLAGS).a
EXTLIBRARY = /usr/local/lib/libtk8.6.dylib \
	/usr/local/lib/libtcl8.6.dylib \
	/usr/local/lib/libtkstub8.6.a \
	/usr/local/lib/libtclstub8.6.a \
	/usr/local/lib/libintl.dylib
	$(INSTALL) -d -m $(DIRMODE) $(PYTHONFRAMEWORKDIR)/Versions/$(VERSION)
	$(CC) -o $(LDLIBRARY) $(PY_LDFLAGS) -dynamiclib \
		$(EXTLIBRARY) \
		-all_load $(LIBRARY) -Wl,-single_module \

Make

make CFLAGS="-arch i386  -arch x86_64"
running build
running build_ext
ここで停止する。CTRL-C で抜け出せる。結果は、
Python build finished, but the necessary bits to build these modules were not found:
ossaudiodev        readline           spwd            
To find the necessary bits, look in setup.py in detect_modules() for the module's name.

running build_scripts
copying and adjusting /users/arisawa/src/Python-3.2.2/Tools/scripts/pydoc3 -> build/scripts-3.2
copying and adjusting /users/arisawa/src/Python-3.2.2/Tools/scripts/idle3 -> build/scripts-3.2
copying and adjusting /users/arisawa/src/Python-3.2.2/Tools/scripts/2to3 -> build/scripts-3.2
changing mode of build/scripts-3.2/pydoc3 from 644 to 755
changing mode of build/scripts-3.2/idle3 from 644 to 755
changing mode of build/scripts-3.2/2to3 from 644 to 755
renaming build/scripts-3.2/pydoc3 to build/scripts-3.2/pydoc3.2
renaming build/scripts-3.2/idle3 to build/scripts-3.2/idle3.2
renaming build/scripts-3.2/2to3 to build/scripts-3.2/2to3-3.2
-bash$

Install

sudo make install CFLAGS="-arch i386  -arch x86_64"

生成されたファイルの確認

/usr/local/bin

-bash$ ls -l /usr/local/bin/python*
lrwxr-xr-x  1 root  wheel  69 10 31 12:36 /usr/local/bin/python3 -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3
lrwxr-xr-x  1 root  wheel  72 10 31 12:36 /usr/local/bin/python3-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3-32
lrwxr-xr-x  1 root  wheel  76 10 31 12:36 /usr/local/bin/python3-config -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3-config
lrwxr-xr-x  1 root  wheel  71 10 31 12:36 /usr/local/bin/python3.2 -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2
lrwxr-xr-x  1 root  wheel  74 10 31 12:36 /usr/local/bin/python3.2-32 -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2-32
lrwxr-xr-x  1 root  wheel  78 10 31 12:36 /usr/local/bin/python3.2-config -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2-config
lrwxr-xr-x  1 root  wheel  72 10 31 12:36 /usr/local/bin/python3.2m -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2m
lrwxr-xr-x  1 root  wheel  79 10 31 12:36 /usr/local/bin/python3.2m-config -> ../../../Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2m-config
-bash$ 
注意: この中には、今回のインストール以前のものも混じっている。今回インストールされたのは
/usr/local/bin/python3.2
/usr/local/bin/python3.2m
のはずである。

"3.2" の後のサフィックスの意味は、サポートされている arch であろう。file コマンドで様子がわかる。

-bash$ file `ls /usr/local/bin/python3*`
/usr/local/bin/python3:           Mach-O universal binary with 2 architectures
/usr/local/bin/python3 (for architecture i386):	Mach-O executable i386
/usr/local/bin/python3 (for architecture x86_64):	Mach-O 64-bit executable x86_64
/usr/local/bin/python3-32:        Mach-O universal binary with 1 architecture
/usr/local/bin/python3-32 (for architecture i386):	Mach-O executable i386
/usr/local/bin/python3-config:    a /Library/Frameworks/Python.fram script text executable
/usr/local/bin/python3.2:         Mach-O 64-bit executable x86_64
/usr/local/bin/python3.2-32:      Mach-O universal binary with 1 architecture
/usr/local/bin/python3.2-32 (for architecture i386):	Mach-O executable i386
/usr/local/bin/python3.2-config:  a /Library/Frameworks/Python.fram script text executable
/usr/local/bin/python3.2m:        Mach-O 64-bit executable x86_64
/usr/local/bin/python3.2m-config: a /Library/Frameworks/Python.fram script text executable
-bash$ 

/Library/Frameworks/Python.framework

-bash$ ls -l /Library/Frameworks/Python.framework
total 24
lrwxr-xr-x  1 root  admin   24  4  7 17:34 Headers -> Versions/Current/Headers
lrwxr-xr-x  1 root  admin   23  4  7 17:34 Python -> Versions/Current/Python
lrwxr-xr-x  1 root  admin   26  4  7 17:34 Resources -> Versions/Current/Resources
drwxr-xr-x  7 root  admin  238  4  7 17:34 Versions
-bash$ ls -l /Library/Frameworks/Python.framework/Versions
total 8
drwxrwxr-x  10 root  admin  340  5 22  2009 2.5
drwxrwxr-x  10 root  admin  340  4 16  2009 2.6
drwxrwxr-x  10 root  admin  340  3 27 08:23 2.7
drwxrwxr-x   9 root  admin  306  4  7 17:34 3.2
lrwxr-xr-x   1 root  admin    3  4  7 17:34 Current -> 3.2
-bash$ ls -l /Library/Frameworks/Python.framework/Versions/3.2
total 4944
lrwxr-xr-x   1 root  admin       18  4  7 17:34 Headers -> include/python3.2m
-r-xr-xr-x   1 root  admin  2526992  4  7 17:34 Python
drwxrwxr-x   5 root  admin      170 10 31 12:36 Resources
drwxrwxr-x  20 root  admin      680  4  7 17:34 bin
drwxrwxr-x   3 root  admin      102  9  4  2011 include
drwxrwxr-x   5 root  admin      170  9  4  2011 lib
drwxrwxr-x   4 root  admin      136  9  4  2011 share
-bash$ ls -l /Library/Frameworks/Python.framework/Versions/3.2/bin/python3*
-rwxrwxr-x  1 root  admin  25624  9  4  2011 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3
lrwxr-xr-x  1 root  admin     12 10 31 12:36 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3-32 -> python3.2-32
lrwxr-xr-x  1 root  admin     16 10 31 12:36 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3-config -> python3.2-config
-rwxr-xr-x  2 root  admin   9296  4  7 17:34 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2
-rwxrwxr-x  1 root  admin  13928  9  4  2011 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2-32
lrwxr-xr-x  1 root  admin     17 10 31 12:36 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2-config -> python3.2m-config
-rwxr-xr-x  2 root  admin   9296  4  7 17:34 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2m
-rwxrwxr-x  1 root  admin   1865  9  4  2011 /Library/Frameworks/Python.framework/Versions/3.2/bin/python3.2m-config
-bash$ 

動作確認

環境変数

次の環境変数の設定が必要かも。(tk.tclが見つからないと言われたら設定する)
export TK_LIBRARY=/Library/Frameworks/Tk.framework/Versions/8.6/Resources/Scripts

確認1

-bash$ python3.2
Python 3.2.2 (default, Apr  7 2012, 17:12:01) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> tkinter.TclVersion
8.6
>>> tkinter.TkVersion
8.6
>>> tk=tkinter.Tk()
>>> tk.getvar("tk_version")
'8.6'
>>> tk.getvar("tk_patchLevel")
'8.6b2'
>>> print(tk.getvar('tcl_library'))
/Library/Frameworks/Tcl.framework/Versions/8.6/Resources/Scripts
>>> print(tk.getvar("tk_library"))
/Library/Frameworks/Tk.framework/Versions/8.6/Resources/Scripts
>>> 

確認2

#encoding: utf-8
from tkinter import *
tk=Tk()
c=Canvas(tk,width=300,height=200)
c.pack(fill="both",expand=1)

c.create_text(150,80,text="ABC",font=("Times",30,"italic"))
c.create_text(150,120,text="いろは",font=("Hiragino Kaku Gothic Pro",30,"bold"))
c.create_rectangle(100,50,200,150,outline="green")
def cmd():
  c.postscript(file="a.eps")
  tk.destroy()
dir(tk)
b=Button(tk,text="終了", command=cmd)
b.pack()
tk.mainloop()
a.py による Canvas

フォント(ファイミリー)名の確認

import tkinter
tk=tkinter.Tk()
a=tk.call("font","families")
b=list(a)
b.sort()
for f in b: print(f)
f.py
見やすく表示するために、フォント名の一覧を変数に入れている。変数 a はタプルであるためソートできない。そこでリスト b に変換し、それをソートしている。
-bash$ python3.2 f.py
AGENDAJinmeiGyoshotaiL1
AGENDAJinmeiSeikaishotaiL1
[中略]
Helvetica
Helvetica CY
Helvetica Neue
Herculanum
Hiragino Kaku Gothic Pro
Hiragino Kaku Gothic ProN
Hiragino Kaku Gothic Std
Hiragino Kaku Gothic StdN
Hiragino Maru Gothic Pro
Hiragino Maru Gothic ProN
Hiragino Mincho Pro
Hiragino Mincho ProN
Hiragino Sans GB
Hoefler Text
[中略]
jsMath-cmsy10
jsMath-cmti10
-bash$ 

Tk postscript の修正

2012/04/15 修正

Tcl/Tk の postscript コマンドで日本語文字が扱えるようにするためには、次の修正を行えば良い。もっと良い方法もあろうが、取りあえず、この方法で何とかなる。

mkpsenc.tcl の修正

    ps function ISOEncode {dup /CMap known {} {ISOEncode1} ifelse }

    ps function ISOEncode1 {
	dup length dict begin
	{1 index /FID ne {def} {pop pop} ifelse} forall
	/Encoding CurrentEncoding def
	currentdict
	end
	% I'm not sure why it's necessary to use "definefont" on this new
	% font, but it seems to be important; just use the name "Temporary"
	% for the font.
	/Temporary exch definefont
    }
$tk/library/mkpsenc.tcl
これで、ISOEncodeが、CMAP に対しては無効になる。

tkFont.c の修正


			/*
			 * No known mapping for the character into the space
			 * of PostScript glyphs. Ignore it. :-(
			 */
			 // begin Kenar
			char utfbuf[5]; // the size must be >=4 for Japanese utf8 char.
			int i,n;
			n = Tcl_UniCharToUtf(ch, utfbuf); //Kenar
			utfbuf[n] = 0;
			printf("DEBUG: tkFont.c: utfbuf %s\n",utfbuf);
			for(i=0; i < n; i++)
				buf[used++] = utfbuf[i];
			// end Kenar
#ifdef TK_DEBUG_POSTSCRIPT_OUTPUT
			fprintf(stderr, "Warning: no mapping to PostScript "
				"glyphs for \\u%04x\n", ch);
#endif
$tk/generic/tkFont.c
// begin Kenar
の行から
// end Kenar
までの行を挿入する。デバッグコードも含まれているが、邪魔なら外す。

Tk の再コンパイルと再インストール

cd $tk/macosx
make -f Makefile
sudo make -f Makefile install
「終了」ボタンをクリックすると、a.eps が生成される。
%!PS-Adobe-3.0 EPSF-3.0
%%Creator: Tk Canvas Widget
%%For: 有澤 健治
%%Title: Window .c
%%CreationDate: Sat Apr  7 21:37:13 2012
%%BoundingBox: 152 292 460 500
%%Pages: 1
%%DocumentData: Clean7Bit
%%Orientation: Portrait
%%DocumentNeededResources: font Times-Italic
%%+ font HiraginoKakuGothicPro-Bold
%%EndComments
a.eps のヘッダ
-bash$ ps2pdf -dEPSCrop a.eps
-bash$ gs a.pdf
-bash$ open -a "/Applications/Adobe Reader 9/Adobe Reader.app" a.pdf
gs(左)とadobe reader(右) で見た、a.pdf
Mac/OSX の Preview.app で開くと、Preview.app が潰れる! (潰れるのは Preview.app のバグ)

注意

gs の設定が必要。詳しくは「Gostscript の日本語フォントの問題」を見よ。

問題点

2012/04/15 修正

もっと、良い方法があるだろう。OSAKA フォントに関して問題があるかも知れない。

付録

Python/Tk postscript の Font Map

ついでに a.py の フォントマップ版を作ろう。この考え方は役に立つ場面があるかも知れない。
#encoding: utf-8
from tkinter import *
FONT1="Times 30 italic"
FONT2="HiraginoKakuGothicPro 30 bold"
tk=Tk()
c=Canvas(tk,width=300,height=200)
c.pack(fill="both",expand=1)

c.create_text(150,80,text="ABC",font=FONT1)
c.create_text(150,120,text="いろは",font=FONT2)
c.create_rectangle(100,50,200,150,outline="green")

tk.call("set","fmap(%s)"%FONT1,"Helvetica 30")
tk.call("set","fmap(%s)"%FONT2,"HiraKakuPro-W6 30")

def cmd():
  c.postscript(file="b.eps",fontmap="fmap")
  tk.destroy()
dir(tk)
b=Button(tk,text="終了", command=cmd)
b.pack()
tk.mainloop()
Python/Tk の Canvas postscript fontmap の使い方
どうやらフォント名は文字列でないとダメらしい。

fmap のところは、普通は fontmap と書くところであるが、postscript オプションの fontmap と紛らわしいので、このプログラムでは敢えて fmap とした。他の名前で構わない。

Python は、どの Tk と結び付くか?

Tk は Python が利用する Tk のダイナミックライブラリ(dylib)である。システム上に多数ある Tk のどれとリンクするのか?

$py/setup.py
がコンパイル時に実行される。
    def detect_tkinter_darwin(self, inc_dirs, lib_dirs):
        # The _tkinter module, using frameworks. Since frameworks are quite
        # different the UNIX search logic is not sharable.
        from os.path import join, exists
        framework_dirs = [
            '/Library/Frameworks',
            '/System/Library/Frameworks/',
            join(os.getenv('HOME'), '/Library/Frameworks')
        ]
$py/setup.pyの一部
これを見ると、
  1. /Library/Frameworks
  2. /System/Library/Frameworks
  3. $HOME/Library/Frameworks
の順で、Tk.framework/Tk を探すことが分かる。