9

I'm creating a glyph node (a capital A) on lua side:

\directlua{%
  n = node.new('glyph')
  n.font = font.current()
  n.char = 65 % ASCII code for capital A
}

I know how to select current font (font.current()). How can I access other fonts that are in luaotfload database (say texgyreschola-regular.otf as an example)?

cjorssen
  • 10,032
  • 4
  • 36
  • 126

1 Answers1

5

How can I access other fonts that are in luaotfload database (say texgyreschola-regular.otf as an example)?

In order to use a font with Luatex, it needs to be defined first. The fontloader has a function fonts.definers.read() for that which has the same signature as the define_font callback. Its font lookup calls into Luaotfload’s font index functionality so you can pass it any valid definition string.

On success, you will receive an object that satisfies the description in the Luatex manual, ch. 5 “Font structure” so you can feed it directly into font.define(). It returns the font id that was assigned by TeX to this particular font which you can use in the situation you describe.

An example that assigns the font in a callback would be:

local glyph_t     = nodes.nodecodes.glyph

local fontify = function (id)
  return function (hd)
    for n in node.traverse (hd) do
      if n.id == glyph_t then n.font = id end
    end
    return hd
  end
end

local main = function ()
  local tfmdata = fonts.definers.read
                    ("file:iwona-regular.otf:mode=base", 42*2^16)
  local id = font.define (tfmdata)
  luatexbase.add_to_callback("pre_linebreak_filter", fontify (id),
                             "userdata.stackexchange.fontify")
  tex.sprint "some text"
end

return main ()
-- vim:ft=lua:sw=2:et

The entire TeX document:

\input luaotfload.sty
\directlua {dofile "\jobname.lua"}
\bye

font definition