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)?

2
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

3
  • Thank you very much for taking the time to answer. I now need some time to analyse and understand all that :)
    – cjorssen
    Oct 21 '16 at 11:06
  • What is the 42*2^16 in the function call? Oct 21 '16 at 21:36
  • @UlrikeFischer Sizes are in sp. Oct 21 '16 at 22:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.