[Tex/LaTex] How to use OpenType Collection font with fontspec package and LuaTeX

fontsfontspecluatexopentype

I have trouble setting monospaced font to Source Han Code JP font using \setmonofont. I tried the following code just the same as when I usually use fontspec package.

% file name: example01.tex
\documentclass{article}
\usepackage{fontspec}
\setmonofont[
    UprightFont = Source Han Code JP L,
    BoldFont = Source Han Code JP N
]{Source Han Code JP}
\begin{document}
\texttt{This is Source Han Code JP L.}\par
\texttt{\textbf{This is Source Han Code JP N.}}
\end{document}

Running lualatex example01.tex throws an error:

!LuaTeX error (file /Users/〈UserName〉/Library/Fonts/SourceHanCodeJP.ttc): sfnt:
 table not found...
 ==> Fatal error occurred, no output PDF file produced!

I also tried assigning font by its PostScript name or file name (with Path= and Extension= options like this), only to fail. The latter one gave ! fontspec error: "font-not-found".

How should I modify the code in order to use Source Han Code JP with LuaLaTeX? Is it relevant to this issue, that Source Han Code JP includes non-Latin (or more specifically, CJK) characters?

Note: I am using MacTeX-2015 and OS X Yosemite 10.10.3.


What I have confirmed so far

Using TrueType Collection fonts

Edit: I found this section was meaningless because Source Han Code JP is an OpenType Collection font, not a TrueType Collection font although its extension is .ttc. See
Introducing & Building OpenType Collections (OTCs)
if you are unfamiliar with OTC. As a matter of fact, there seems to be very few fonts adopting this format.

I first thought the cause of this error lies in using font installed by .ttc file. This is because I heard that we cannot embed Hiragino fonts (one of the Japanese fonts and installed by .ttc) into PDF in the conventional way on El Capitan. Then I tested Optima.ttc and Palatino.ttc with this code:

% file name: example02.tex
\documentclass{article}
\usepackage{fontspec}
\setmainfont[
    UprightFont = Palatino-Roman, % assigned by PostScript name
    BoldFont = Palatino Bold
]{Palatino}
\setsansfont[
    UprightFont = Optima Regular,
    BoldFont = Optima Bold
]{Optima}
\begin{document}
    This is Palatino.\par
    \textsf{This is Optima Regular.}
\end{document}

However, lualatex example02.tex worked quite well.

Optima and Palatino

Using OpenType Format fonts (on Windows 7)

I wondered whether sourcehancodejp-***.otf (*** is each weight) is available with LuaLaTeX and tried compiling example01.tex on my Windows where Source Han Code JPs are installed using .otf files. It worked with the following warnings which I think are negligible in this case.

LuaTeX warning (file c:/Windows/fonts/sourcehancodejp-normal.otf): Charset data possibly broken (num_glyphs too high)
LuaTeX warning (file c:/Windows/fonts/sourcehancodejp-light.otf): Charset data possibly broken (num_glyphs too high)

Black list that LuaLaTeX has

Before posting this question, I did several searches for solution and found these threads:

So, I looked into /usr/local/texlive/2015/texmf-dist/tex/luatex/luaotfload/luaotfload-blacklist.cnf. The content is as follows and SourceHanCodeJP.ttc is not written there.

spltfgbd.ttf
spltfgbi.ttf
spltfgit.ttf
spltfgrg.ttf

XeTeX

Unlike LuaLaTeX, example01.tex is compilable with XeLaTeX. It gives the desired PDF with no error.

XeLaTeX

Font name in Japanese

When we use Mac in Japanese, the font name for Source Han Code JP becomes 源ノ角ゴシック Code JP (see my comment about the name).

en-name

jp-name

I tried using this Japanese name.

% file name: example03.tex
\documentclass[a4paper]{article}
\usepackage{fontspec}
\setmonofont[
    UprightFont = 源ノ角ゴシック Code JP L,
    BoldFont = 源ノ角ゴシック Code JP N
]{Source Han Code JP}
\begin{document}
\texttt{This is Source Han Code JP L (in Japanese, 源ノ角ゴシック Code JP L).}\par
\texttt{\textbf{This is Source Han Code JP N (in Japanese, 源ノ角ゴシック Code JP N).}}
\end{document}

This code is also compilable with XeLaTeX but not with LuaLaTeX. Here is the output of xelatex example03.tex.

Japanese


Recent movement

After El Capitan's release, some Japanese TeX developers are striving to make OTC fonts available with LuaTeX and they seem to be successful.

Here are what I did today:

  1. get luaotfload from here,
  2. put necessary files in /usr/local/texlive/texmf-local/tex/luatex/luaotfload and /usr/local/texlive/texmf-local/scripts/luaotfload,
  3. run mktexlsr
  4. build patched LuaTeX, and
  5. replace the conventional luatex and luajittex in /usr/local/texlive/2015/bin/x86_64-darwin/ with the generated ones (also backup the original by adding -bak suffix).

Then, I tried to compile the following file but still get the same error as lualatex example01.tex.

\documentclass{ltjsarticle}
\usepackage{fontspec}
\usepackage{luatexja-fontspec}
\setmonofont{Source Han Code JP}
\begin{document}
\ttfamily test
\end{document}

Is there something wrong in my procedure? Note that I have not updated my Mac to El Capitan yet.

Best Answer

It is only since in April 2015 that xdvipdfmx can embed OpenType Collection fonts into PDF correctly. https://www.tug.org/pipermail/tex-live/2015-April/036628.html However, this improvement has not been applied to LuaTeX, even in the latest TeX Live 2015.

Now Apple's new OS X 10.11 El Capitan has been released, and Japanese fonts (such as Hiragino) are merged into OpenType Collection (OTC) format (though the extension is .ttc, confusingly). Many LaTeX users in Japan are now facing troubles in typesetting Japanese documents due to this change, so there is a high demand for handling OTC fonts. To solve this problem, Hironori KITAGAWA, the developer of LuaTeX-ja (Japanese typesetting system on LuaTeX), is now kindly looking into this problem. When the patch is accepted by LuaTeX developers, LuaTeX will be able to embed OpenType Collection fonts into PDF, as well as xdvipdfmx does.

Update: LuaTeX beta-0.81.0 is now released! This version of LuaTeX can handle OpenType Collection fonts. See r5330 for detail. However, it seems that there is some kind of inconsistency between latest LuaTeX and luaotfload.

Sample LuaLaTeX source:

\documentclass{article}
\usepackage{fontspec}
\setmainfont{SourceHanCodeJP-Regular}
\begin{document}
これは源ノ角ゴシックCode JP Rです。
\end{document}
Related Question