2

I have a package that includes (basically) the following code (provided by a contributor after asking this question on tex.stackoverflow).

\begin{luacode}
  documentdata = documentdata or { }
  documentdata.fontchar = function (chr)
    local chr = luaotfload.aux.slot_of_name(font.current(), chr, false)
    print(chr)
    if chr and type(chr) == "number" then
      tex.sprint(string.format([[\char"%X]], chr))
    end
  end
\end{luacode}
\def\fontchar#1{\directlua{documentdata.fontchar "#1"}}

This function locates a glyph from a font by its name and prints it to the TeX document.

I am starting to convert the package to a Lua-only package and want to move that code in a separate Lua file. (Eventually the function should not use tex.sprint but return a string, and I would like to know if it's possible to pass around such glyphs through Lua, but that's a follow-up issue).

However, when I do what I thought was a correct translation I get an error:

local character = {}

function character.char_by_name(name)
    local chr = luaotfload.aux.slot_of_name(font.current(), name, false)
    if chr and type(chr) == "number" then
      tex.sprint(string.format([[\char"%X]], chr))
    else
        tex.sprint('f') -- 'forte' character to show "not found"
    end
end

return character

It says luaotfload | aux : invalid parameters to slot_of_name (16, nil)false. Actually this sounds reasonable, given that the documentation of aux.slot_of_name in the luaotfload manual states aux.slot_of_name(name : string) as the function's signature. Given that documentation I'm wondering why this did even work in the first place, in the luacode environment.

Here is the complete MWE. I assume it can be compiled when lilyglyphs (or the music collection) is installed in TeX Live, because it installs the used special font with it. This is the .tex file, with the above Lua example saved in character.lua:

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

\begin{luacode}
  documentdata = documentdata or { }
  documentdata.fontchar = function (chr)
    local chr = luaotfload.aux.slot_of_name(font.current(), chr, false)
    if chr and type(chr) == "number" then
      tex.sprint(string.format([[\char"%X]], chr))
    end
  end
\end{luacode}
\def\fontchar#1{\directlua{documentdata.fontchar "#1"}}

% Alternative implementation in Lua file
\directlua{lua_char = require('character.lua')}
\newcommand{\luafontchar}[1]{\directlua{lua_char.char_by_name(#1)}}

\font\mainfont="emmentaler-11" at 20pt

\begin{document}
    \mainfont
    \noindent
    \fontchar{clefs.C}\\
    \fontchar{clefs.G}\\
    \fontchar{flags.u7}

    \bigskip
    \luafontchar{Something}
\end{document}

The calls to \fontchar produce the expected glyphs from the Emmentaler font, while \luafontchar causes the mentioned error and prints the fallback glyph.

Results

uli_1973
  • 1,920
  • 4
    You are not passing the glyph name as string. It should be lua_char.char_by_name("#1") – Ulrike Fischer Aug 05 '19 at 10:21
  • WHAT, that was the culprit? Oh my. – uli_1973 Aug 05 '19 at 12:17
  • Should this question be marked invalid, because the solution/error is totally unrelated to what I was seemingly asking about? – uli_1973 Aug 05 '19 at 12:17
  • I was desperately looking for a luaLaTeX-equivalent of the \namedglyph command provided by xltxtra for XeLaTeX. I use it to access alternative glyphs or ligatures that don't have a Unicode-codepoint attached. The first block of code in your question does exactly this. I am writing this description here so others might find it more easily. Thanks and please don't delete the question! – Florian Aug 20 '20 at 09:35

0 Answers0