# Print font name using LuaTeX

I want to print font names (full name, ps name, file name, etc) using LuaTeX's font table. It works fine previously, but recently I encountered an error message from luaotfload

Here is a minimal working example,

\documentclass{article}
\usepackage{fontspec}
% \setmainfont{Minion Pro}

\begin{document}
\directlua{
local str = font.fonts[font.current()].fullname
tex.sprint(-2, str)
}
\end{document}


This example, when compiled with LuaTeX works fine. The output PDF has the string LMRoman10-Regular, which is the default font. However, if I do not comment out the setmainfont line, that is, I load the Minion Pro font, then I got the following error message,

error: ...3/texmf-dist/tex/luatex/luaotfload/luaotfload-merged.lua:7656: attempt to perform arithmetic on local 'factor' (a nil value)
.
\newpage ...k \@nobreakfalse \everypar {}\fi \par
\vfil \penalty -\@M


The relevant line in luaotfload-merged.lua and around is,

function injections.setkern(current,factor,rlmode,x,tfmchr)
local dx=factor*x
if dx~=0 then
local bound=#kerns+1
setattr(current,a_kernpair,bound)
kerns[bound]={ rlmode,dx }
return dx,bound
else
return 0,0
end
end


When I replace the tex.sprint line with inspect, the console displays correctly Minion Pro Regular.

Does anyone has any idea what is going on here?

I am using an up-to-date TeXLive 2013 installation.

Indexing the font.fonts table can cause the data structures that have been layed out by the font loader to be overwritten. Safe access to those fonts is provided by font.getfont(). If you want to retrieve other fonts like e.g. TFM/OFM or virtual fonts, then you will have to write a wrapper like so:

local fontdata = fonts.hashes.identifiers
local my_getfont = function (id)
local f = fontdata[id]
if f then
return f
end
return font.fonts[id]
end


Where fonts.hashes.identifiers is where the fontloader keeps the font it initializes.