ルギア君の戯言

雑多な記事。

Mac 上の TeXLive の luaotfload で「クレー」などのフォントを使えるようにする

例えば、「ヒラギノ」とか、「源ノ角ゴシック」のように自分でインストールしたフォントは、Font Book の「フォント情報」のところに場所としてファイルのパスが書かれているんだけど、「クレー」とか「筑紫丸ゴシック」とか「凸版文久明朝」とか、最近追加されたシステムのフォントファイルは、Font Book にその場所が書かれていない。

というわけで、まず、実体がどこにあるのかという点と、luaotfload の検索パスにそれを追加する点の2つを見てみる。

実体の場所

さすがに、何かしらの方法で呼び出されたときにインターネットからダウンロードされるとか、カーネルに組み込まれているとか、そういうものではなかった。

/System/Library/Assets/com_apple_MobileAsset_Font3/<a folder with a slew of numbers and letters .asset>/AssetData

にあった。MobileAsset とあるので、iOS 用のものでしょうか。

luaotfload への追加

fontspec のフォント検索、といえば、fontconfig の機能を使っているのは知っている。ならば、fonts.conf にそのディレクトリを追加しよう。

~/.fonts.conf を作成して、

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/fonts.conf file to configure system font access -->
<fontconfig>
<!-- 
        Find fonts in these directories
-->
<dir>/System/Library/Assets/com_apple_MobileAsset_Font3/0412b97ba362b3017d1f5d445681f7912d13823b.asset/AssetData</dir>
<dir>/System/Library/Assets/com_apple_MobileAsset_Font3/06722e9d680cf43caeeaf24d40f37ebdddca37e2.asset/AssetData</dir>
<dir>/System/Library/Assets/com_apple_MobileAsset_Font3/0d72c02a426a7f3d6eaf3b41585acc6a9c9f3c83.asset/AssetData</dir>
<!-- 以下略 -->
</fontconfig>

キャッシュをアップデート。

$ fc-cache -f
$ fc-list | grep Klee
Fontconfig warning: line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
/System/Library/Assets/com_apple_MobileAsset_Font3/56fbb4c7efe470c6f9788fba733e5ec07d97be83.asset/AssetData/Klee.ttc: クレー,Klee,Klee Demibold,クレー デミボールド:style=デミボールド,Demibold,Regular
/System/Library/Assets/com_apple_MobileAsset_Font3/56fbb4c7efe470c6f9788fba733e5ec07d97be83.asset/AssetData/Klee.ttc: クレー,Klee,Klee Medium,クレー ミディアム:style=ミディアム,Medium,Regular

fc-list で出るようになった。が、luaotfload からは見つからず。って、luaotfload のデータベースの更新はこっちか。

$ luaotfload-tool -f -u

変わらず。

あ、TeX は別の場所にデータベースをおいているか。というわけで、TeX の fontconfig に追加してみよう。

$ sudo vim /usr/local/texlive/2016/texmf-var/fonts/conf/texlive-fontconfig.conf

変わらず。変だな、と思って、検索プログラムのコードを調べたら、

$ vim /usr/local/texlive/2016/texmf-dist/tex/luatex/luaotfload/luaotfload-database.lua
--- unit -> string list
local function get_os_dirs ()
    if os.name == 'macosx' then
        return {
            filejoin(kpseexpand_path('~'), "Library/Fonts"),
            "/Library/Fonts",
            "/System/Library/Fonts",
            "/Network/Library/Fonts",
        }
    elseif os.type == "windows" or os.type == "msdos" then
        local windir = osgetenv("WINDIR")
        return { filejoin(windir, 'Fonts') }
    else
        local fonts_conves = { --- plural, much?
            "/usr/local/etc/fonts/fonts.conf",
            "/etc/fonts/fonts.conf",
        }
        if not luaotfload.parsers then
            logreport ("log", 0, "db", "Fatal: no fonts.conf parser.")
        end
        local os_dirs = luaotfload.parsers.read_fonts_conf(fonts_conves, find_files)
        return os_dirs

fontconfig を使うのは Windows でも Mac でもない場合だけ、だった*1。それも、直接設定ファイルを読み込んで。

ソースコードを変えるのは後々忘れそうなので、それなら、texmf-local からシンボリックリンクを貼ってはどうだろうか。

$ sudo mkdir -p /usr/local/texlive/texmf-local/fonts/opentype/apple
$ cd /usr/local/texlive/texmf-local/fonts/opentype/apple
$ sudo find /System/Library/Assets/com_apple_MobileAsset_Font3 -name "*.otf" -o -name "*.ttc" -exec sh -xc 'ln -s "{}" "`basename "{}"`"' \;
$ sudo mktexlsr /usr/local/texlive/texmf-local
$ luaotfload-tool -f -u -vvvvv 
$ luaotfload-tool --find=Klee
luaotfload | resolve : Font "Klee" found!
luaotfload | resolve : Resolved file name "/System/Library/Assets/com_apple_MobileAsset_Font3/56fbb4c7efe470c6f9788fba733e5ec07d97be83.asset/AssetData/Klee.ttc"

mktexlsr が必要なのを忘れてました。

無事見つかるように。やったね。

References

追記

アセトアミノフェン (id:acetaminophen)さん、情報ありがとうございます。

まず fontspec が使用する、フォントの検索に使用する仕組みは

エンジン Windows Mac Linux/UNIX
XeTeX fontconfig AAT fontconfig
LuaTeX luaotfload

となっているわけですね。

XeTeX なら、fontconfig で見つかってなくても(AATを使っているから)「クレー」が使えることを確認できました。

(※ ここで実行している fc-list (fontconfig) のコマンドは、Homebrew のものであって、TeX には含まれていませんでした。Homebrew / MacPorts の他、XQuartz (X11) を入れている人もこのコマンドは存在すると思われます。)

teostra:~ lugia% fc-list | grep  Klee                                   [18:43]
Fontconfig warning: line 146: blank doesn't take any effect anymore. please remove it from your fonts.conf
teostra:~ lugia% xetex                                                  [18:45]
This is XeTeX, Version 3.14159265-2.6-0.99998 (TeX Live 2017) (preloaded format=xetex)
 restricted \write18 enabled.
** \
entering extended mode

*\font\x="Klee:mapping=tex-text,color=0000FF" at 10pt

*\x  

*あいうえお

*\bye
[1]
(see the transcript file for additional information)
Output written on texput.pdf (1 page).
Transcript written on texput.log.
teostra:~ lugia% open texput.pdf                                        [18:46]
teostra:~ lugia% pdffonts texput.pdf                                    [18:46]
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
DSQIAS+AppleKlee-Medium-Identity-H   CID Type 0C       Identity-H       yes yes no       5  0
KDQTVB+CMR10                         Type 1C           Builtin          yes yes no       6  0

このせいなのか、TEXMF にしかない IPAMincho は見つからない *2

teostra:~ lugia% cat test.tex                                           [21:35]
\font\x="IPAMincho:mapping=tex-text" at 10pt
\font\y="Klee:mapping=tex-text" at 10pt
\x あいうえお
\y かきくけこ
\bye

teostra:~ lugia% xetex test.tex                                         [21:35]
This is XeTeX, Version 3.14159265-2.6-0.99998 (TeX Live 2017) (preloaded format=xetex)
 restricted \write18 enabled.
entering extended mode
(./test.tex
kpathsea: Running mktextfm IPAMincho
/usr/local/texlive/2017/texmf-dist/web2c/mktexnam: Could not map source abbreviation I for IPAMincho.
/usr/local/texlive/2017/texmf-dist/web2c/mktexnam: Need to update /usr/local/texlive/2017/texmf-dist/fonts/map/fontname/special.map?
mktextfm: Running mf-nowin -progname=mf \mode:=ljfour; mag:=1; nonstopmode; input IPAMincho
This is METAFONT, Version 2.7182818 (TeX Live 2017) (preloaded base=mf)


kpathsea: Running mktexmf IPAMincho
! I can't find file `IPAMincho'.
<*> ...jfour; mag:=1; nonstopmode; input IPAMincho
                                                  
Please type another input file name
! Emergency stop.
<*> ...jfour; mag:=1; nonstopmode; input IPAMincho
                                                  
Transcript written on mfput.log.
grep: IPAMincho.log: No such file or directory
mktextfm: `mf-nowin -progname=mf \mode:=ljfour; mag:=1; nonstopmode; input IPAMincho' failed to make IPAMincho.tfm.
kpathsea: Appending font creation commands to missfont.log.

! Font \x=IPAMincho:mapping=tex-text at 10.0pt not loadable: Metric (TFM) file 
or installed font not found.
l.1 \font\x="IPAMincho:mapping=tex-text" at 10pt
                                                
? X
No pages of output.
Transcript written on test.log.
teostra:~ lugia%                                                        [21:35]

luaotfload では、システムにインストールされているフォントの検索パスは、上のソースのように Windows なら %WINDIR%\Fonts, Mac なら /Library/Fonts 等といったように、基本的には決め打ちになっているけど、Linux は決められないので、fontconfig の設定ファイルがあればそこからフォントの入っているディレクトリを探してみる、ということになるわけですね。

teostra:~ lugia% cat test.tex                                           [21:18]
\documentclass{standalone}
\usepackage{luatex85}
\usepackage{fontspec}

\begin{document}
\font\x="Klee:mapping=tex-text,color=0000FF" at 10pt
\font\y="HiraMinProN-W3:mapping=text-text,color=0000FF" at 10pt
\x あいうえお
\y かきくけこ
\end{document}

teostra:~ lugia% lualatex test.tex                                      [21:18]
This is LuaTeX, Version 1.0.4 (TeX Live 2017) 
 restricted system commands enabled.
(./test.tex
LaTeX2e <2017-04-15>
(using write cache: /Users/lugia/Library/texlive/2017/texmf-var/luatex-cache/ge
neric)(using read cache: /usr/local/texlive/2017/texmf-var/luatex-cache/generic
 /Users/lugia/Library/texlive/2017/texmf-var/luatex-cache/generic)
luaotfload | main : initialization completed in 0.113 seconds
Babel <3.10> and hyphenation patterns for 1 language(s) loaded.
(/usr/local/texlive/2017/texmf-dist/tex/latex/standalone/standalone.cls
Document Class: standalone 2015/07/15 v1.2 Class to compile TeX sub-files stand
alone
(/usr/local/texlive/2017/texmf-dist/tex/generic/oberdiek/ifluatex.sty)
(/usr/local/texlive/2017/texmf-dist/tex/generic/oberdiek/ifpdf.sty)
(/usr/local/texlive/2017/texmf-dist/tex/generic/ifxetex/ifxetex.sty)
(/usr/local/texlive/2017/texmf-dist/tex/latex/xkeyval/xkeyval.sty
(/usr/local/texlive/2017/texmf-dist/tex/generic/xkeyval/xkeyval.tex
(/usr/local/texlive/2017/texmf-dist/tex/generic/xkeyval/xkvutils.tex
(/usr/local/texlive/2017/texmf-dist/tex/generic/xkeyval/keyval.tex))))
(/usr/local/texlive/2017/texmf-dist/tex/latex/standalone/standalone.cfg)
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/article.cls
Document Class: article 2014/09/29 v1.4h Standard LaTeX document class
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/size10.clo
luaotfload | db : Font names database not found, generating new one.
luaotfload | db : This can take several minutes; please be patient.(save: /Users/lugi
a/Library/texlive/2017/texmf-var/luatex-cache/generic/fonts/otl/lmroman10-regul
ar.lua)(save: /Users/lugia/Library/texlive/2017/texmf-var/luatex-cache/generic/
fonts/otl/lmroman10-regular.luc))))
(/usr/local/texlive/2017/texmf-dist/tex/generic/luatex85/luatex85.sty)
(/usr/local/texlive/2017/texmf-dist/tex/latex/fontspec/fontspec.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3kernel/expl3.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3kernel/expl3-code.tex)
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3kernel/l3pdfmode.def))
(/usr/local/texlive/2017/texmf-dist/tex/latex/l3packages/xparse/xparse.sty)
(/usr/local/texlive/2017/texmf-dist/tex/latex/fontspec/fontspec-luatex.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/fontenc.sty
(/usr/local/texlive/2017/texmf-dist/tex/latex/base/tuenc.def))
(/usr/local/texlive/2017/texmf-dist/tex/latex/fontspec/fontspec.cfg)(save: /Use
rs/lugia/Library/texlive/2017/texmf-var/luatex-cache/generic/fonts/otl/lmroman1
0-bold.lua)(save: /Users/lugia/Library/texlive/2017/texmf-var/luatex-cache/gene
ric/fonts/otl/lmroman10-bold.luc)(save: /Users/lugia/Library/texlive/2017/texmf
-var/luatex-cache/generic/fonts/otl/lmroman10-italic.lua)(save: /Users/lugia/Li
brary/texlive/2017/texmf-var/luatex-cache/generic/fonts/otl/lmroman10-italic.lu
c))) (./test.aux)(save: /Users/lugia/Library/texlive/2017/texmf-var/luatex-cach
e/generic/fonts/otl/klee-2.lua)(save: /Users/lugia/Library/texlive/2017/texmf-v
ar/luatex-cache/generic/fonts/otl/klee-2.luc)(save: /Users/lugia/Library/texliv
e/2017/texmf-var/luatex-cache/generic/fonts/otl/ヒラギノ明朝-pron-w3-1.lu
a)(save: /Users/lugia/Library/texlive/2017/texmf-var/luatex-cache/generic/fonts
/otl/ヒラギノ明朝-pron-w3-1.luc) [1{/usr/local/texlive/2017/texmf-var/fon
ts/map/pdftex/updmap/pdftex.map}] (./test.aux))
 513 words of node memory still in use:
   5 hlist, 1 vlist, 1 rule, 5 glue, 10 glyph, 10 attribute, 49 glue_spec, 8 at
tribute_list, 1 write, 2 pdf_colorstack nodes
   avail lists: 2:23,3:3,4:3,5:21,6:11,7:12,8:1,9:7
</System/Library/Fonts/ヒラギノ明朝 ProN W3.ttc></System/Library/Fonts/
ヒラギノ明朝 ProN W3.ttc></usr/local/texlive/texmf-local/fonts/opentype/a
pple/Klee.ttc></usr/local/texlive/texmf-local/fonts/opentype/apple/Klee.ttc>
Output written on test.pdf (1 page, 6546 bytes).
Transcript written on test.log.
teostra:~ lugia% pdffonts test.pdf                                      [21:19]
name                                 type              encoding         emb sub uni object ID
------------------------------------ ----------------- ---------------- --- --- --- ---------
GHAOPN+Klee-Medium                   CID Type 0C       Identity-H       yes yes yes      4  0
AURRFZ+HiraMinProN-W3                CID Type 0C       Identity-H       yes yes yes      5  0

上の記事で作った texmf-local 内のシンボリックリンクからフォントファイルを読み込んでいるので、「クレー」のある場所は単に luaotfload が検索する場所に含まれていないんですねぇ。

*1:2017では変更になっている可能性あり

*2:このことはマニュアルにもしっかり記載されていました。「システムにインストールしたフォントか、LuaTeX ならば(luaotfload を使っているので)TEXMF 内のフォントをフォント名で指定して読み込める。XeTeX から TEXMF 内のフォントを読み込むには、ファイル名で指定する必要がある」(要約)。TEXMFDIST 内のファイルを漁ると、TEXMF 内のフォントを見つけるための fontconfig の設定ファイルが見つかるけど、この記述を正とするなら、そのファイルは使っていないことになる。推測だけど、AATではこれをすることができないために、そんな仕様になったんだろうな。