LuaTeX – Accessing All Characters in an OpenType Font with LuaLaTeX

fontspecluatex

This is a follow-up to Access specific characters in LuaTeX and How to access fleurons?

With TeX Live 2012, I compiled the following file and got the output in the image:

\documentclass[12pt]{book}
\usepackage{fontspec}
\setmainfont[Numbers=OldStyle]{Garamond Premiere Pro Regular}
\def\fontchar#1{\directlua{fonts.otf.char("#1")}}
\begin{document}
\begin{center}
  \begin{tabular}{lc}
bullet.01 & \fontchar{bullet.01}\\
bullet.02 & \fontchar{bullet.02}\\
bullet.03 & \fontchar{bullet.03}\\
bullet.04 & \fontchar{bullet.04}\\
bullet.05 & \fontchar{bullet.05}\\
bullet.06 & \fontchar{bullet.06}\\
bullet.07 & \fontchar{bullet.07}\\
bullet.08 & \fontchar{bullet.08}\\
bullet.09 & \fontchar{bullet.09}\\
bullet.10 & \fontchar{bullet.10}\\
bullet.11 & \fontchar{bullet.11}\\
bullet.12 & \fontchar{bullet.12}\\
bullet.13 & \fontchar{bullet.13}\\
bullet.14 & \fontchar{bullet.14}
  \end{tabular}
\end{center}
\end{document}

Adobe Garmond fleurons

With TeX Live 2013, compiling the same file produces this error message:

ERROR: LuaTeX error [string "\directlua "]:1: attempt to index field 'otf' (a nil val

--- TeX said ---
ue)
stack traceback:
    [string "\directlua "]:1: in main chunk.
\fontchar #1^^@-\directlua {fonts.otf.char("#1")}

l.14     bullet.01 & \fontchar{bullet.01}
                   \\
--- HELP ---
From the .log file...

The lua interpreter ran into a problem, so the
remainder of this lua chunk will be ignored.

I also tried this approach:

\documentclass[12pt]{book}
\usepackage{fontspec}
\setmainfont[Numbers=OldStyle]{Garamond Premiere Pro Regular}
\begin{document}
\begin{center}
  \begin{tabular}{lc}
bullet.01 & \symbol{\string"051A}\\
bullet.02 & \symbol{\string"051B}\\
bullet.03 & \symbol{\string"051C}\\
bullet.04 & \symbol{\string"051D}\\
bullet.05 & \symbol{\string"051E}\\
bullet.06 & \symbol{\string"051F}\\
bullet.07 & \symbol{\string"0520}\\
bullet.08 & \symbol{\string"0521}\\
bullet.09 & \symbol{\string"0522}\\
bullet.10 & \symbol{\string"0523}\\
bullet.11 & \symbol{\string"0524}\\
bullet.12 & \symbol{\string"0525}\\
bullet.13 & \symbol{\string"0526}\\
bullet.14 & \symbol{\string"0527}
  \end{tabular}
\end{center}
\end{document}

That didn’t produce any errors or warnings, but none of the ornaments appeared in the PDF output.

How should I adjust my source in view of the changes in TeX Live 2013?

Best Answer

The function fonts.otf.char() vanished a long time ago and was replaced by a Context-specific implementation that is not part of the fontloader distribution. However, Luaotfload adds a collection of auxiliary code that allows recreating its functionality:

\documentclass{scrartcl}
\usepackage{luaotfload,luacode}

\begin{luacode}
  documentdata       = documentdata or { }

  local stringformat = string.format
  local texsprint    = tex.sprint
  local slot_of_name = luaotfload.aux.slot_of_name

  documentdata.fontchar = function (chr)
    local chr = slot_of_name(font.current(), chr, false)
    if chr and type(chr) == "number" then
      texsprint
        (stringformat ([[\char"%X]], chr))
    end
  end
\end{luacode}

\def\fontchar#1{\directlua{documentdata.fontchar "#1"}}

\font\mainfont="file:GaramondPremrPro.otf" at 20pt

\begin{document}
  \def\bulletrow#1{bullet.#1&\fontchar{bullet.#1}\\}

  \mainfont

  \begin{tabular}{lc}
    \bulletrow{01}
    \bulletrow{02}
    \bulletrow{03}
    \bulletrow{04}
    \bulletrow{05}
    \bulletrow{06}
    \bulletrow{07}
    \bulletrow{08}
    \bulletrow{09}
    \bulletrow{10}
    \bulletrow{11}
    \bulletrow{12}
    \bulletrow{13}
    \bulletrow{14}
  \end{tabular}
\end{document}
% vim:ft=tex:sw=2:ts=2:expandtab

Result:

output demo

Related Question