3

I try to change the font from the Lua side. I tried to change to small caps (see Obtain small caps font from OpenType font when diffining a `\font`, thanks to egreg's code) but, whereas everything works fine with Latin Modern, if I use fourier-otf (but other OpenType packages give same results), the font is changed (numerically) but there is no small caps in the output.

Any ideas?

\documentclass{article}
\usepackage{luacode}

\usepackage{fourier-otf}


\begin{document}

\sbox0{\scshape \global\expandafter\let\expandafter\firstlinefont\the\font}
\font\firstlinefontB={file:lmromancaps10-regular.otf}


\setbox1\hbox{Test}
\setbox2\hbox{Test}
Test
\luadirect{
    local firstlinefont = font.id("firstlinefontB")
    print(firstlinefont)
    glyphn=node.id('glyph')
    h=tex.getbox(1)
    for n in node.traverse_id(glyphn,h.list) do
        if not (n.font == firstlinefont) then
            n.font = firstlinefont
        end 
    end
    tex.print(h)
}

\luadirect{
    firstlinefont = font.id("firstlinefont")
    h=tex.getbox(2)
    print(firstlinefont)
    for n in node.traverse_id(glyphn,h.list) do
        if not (n.font == firstlinefont) then
            n.font = firstlinefont
        end 
    end
    tex.print(h)
}

\end{document}

Result of the code compilation

3
  • 4
    Please link to your previous question as this currently just looks like a duplicate. In particular, you should attribute egreg's code.
    – cfr
    Commented Jul 12 at 20:50
  • tex.stackexchange.com/questions/722391/… is particularly relevant. Since the comment is not visible to most users: Udi Fogiel wrote 'The font is correct. The problem is that the smcp feature maps the characters to a PUA slots, the font is the same font. So you need to change n.char, not n.font.'
    – cfr
    Commented Jul 12 at 20:57
  • 1
    Udi Fogiel wrote why the small caps don't show up, but more fundamentally you don't get reasonable results when replacing the font after shaping, e.g. in a \hbox. The shaping does not get reexecuted, so kerning, ligaturing and all other font features are applied for the wrong font (which caused your current issues) but also the width and other dimension have been calculated for the wrong font. Unless you know that the fonts are 100% metric compatible (and a font and it's small caps is almost guaranteed not to) this will lead to wrongly positioned and maybe even more broken output. Commented Jul 13 at 11:17

1 Answer 1

7

Erewhon Regular does not have a separate font file for small caps like Latin Modern does, it uses the smcp open type feature to apply GSUB substitutions to map small latin letters the glyphs in the PUA.

I'm not sure how luaotfload access this mapping, nor how can you apply the mapping, or reshape the nodes in the shaped list, but you can save the mapping your self in a table, then replace n.char accordingly.

\documentclass{article}

\usepackage{fourier-otf}

\directlua{
smcp_map = smcp_map or {}
local smallcaps = token.scan_list()
local i = 97
for g in node.traverse_id(node.id('glyph'), smallcaps.list) do
      smcp_map[i] = g.char
      i = i + 1
end
}\hbox{\scshape abcdefghijklmnopqrstuvwxyz}

\begin{document}
\setbox2\hbox{Test}
\directlua{
    local h = tex.getbox(2)
    for n in node.traverse_id(node.id('glyph'),h.list) do
         if smcp_map[n.char] then
             n.char = smcp_map[n.char]
         end
    end
    node.write(h)
}
\end{document}

enter image description here

2
  • The token.scan_list() picks up the hbox at the end of luadirect? LuaTeX manual is a bit hard to follow. Thanks!
    – yannisl
    Commented Jul 14 at 6:52
  • 1
    @yannisl Yes. The token library is meant for picking up things from TeX to Lua, and push things back to TeX from Lua. Think of the scanners as a way to define Lua functions that can pick arguments from TeX.
    – Udi Fogiel
    Commented Jul 14 at 8:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .