Skip to content

global fontloader

The fontloader library is sort of independent of the rest in the sense that it can load font into a Lua table that then can be converted into a table suitable for TeX. The library is an adapted subset of FontForge and as such gives a similar view on a font (which has advantages when you want to debug).

😱 Types incomplete or incorrect? 🙏 Please contribute!


methods


fontloader.info


function fontloader.info(filename: string) ->  FontInfo?
@param filename - The path a font file.

Get various information fields from an font file.

This function returns either nil, or a table, or an array of small tables (in the case of a TrueType collection). The returned table(s) will contain some fairly interesting information items from the font(s) defined by the file.

Getting information through this function is (sometimes much) more efficient than loading the font properly, and is therefore handy when you want to create a dictionary of available fonts based on a directory contents.

Reference:

fontloader.open


function fontloader.open(
  filename: string,
  fontname: string?
)
 -> font FontloaderFont {
    table_version = string,
    fontname = string,
    fullname = string,
    familyname = string,
    weight = string,
    copyright = string,
    filename = string,
    version = string,
    italicangle = integer,
    units_per_em = integer,
    ascent = integer,
    descent = integer,
    ...(+60)
}
 -> warnings table

@return font - The first return value is a userdata representation of the font.

@return warnings - The second return value is a table containing any warnings and errors reported by fontloader while opening the font. In normal typesetting, you would probably ignore the second argument, but it can be useful for debugging purposes.

😱 Types incomplete or incorrect? 🙏 Please contribute!

Parse a font file and return a table representing its contents. The optional argument is the name of the desired font in case of font collection files. The optional return value contains any parser error strings.

Open an OpenType font.

If you want to use an OpenType font, you have to get the metric information from somewhere. Using the fontloader library, the simplest way to get that information is thus:

function load_font (filename)
  local metrics = nil
  local font = fontloader.open(filename)
  if font then
     metrics = fontloader.to_table(font)
     fontloader.close(font)
  end
  return metrics
end

myfont = load_font('/opt/tex/texmf/fonts/data/arial.ttf')

Reference:

fontloader.to_table


function fontloader.to_table(font: FontloaderFont {
    table_version = string,
    fontname = string,
    fullname = string,
    familyname = string,
    weight = string,
    copyright = string,
    filename = string,
    version = string,
    italicangle = integer,
    units_per_em = integer,
    ascent = integer,
    descent = integer,
    ...(+60)
}) -> f (FontloaderFont|false)

If you want to use an \OPENTYPE\ font, you have to get the metric information from somewhere. Using the fontloader library, the simplest way to get that information is thus:

Example:

function load_font (filename)
  local metrics = nil
  local font = fontloader.open(filename)
  if font then
     metrics = fontloader.to_table(font)
     fontloader.close(font)
  end
  return metrics
end

myfont = load_font('/opt/tex/texmf/fonts/data/arial.ttf')

Reference:

fontloader.close


function fontloader.close(font: FontloaderFont {
    table_version = string,
    fontname = string,
    fullname = string,
    familyname = string,
    weight = string,
    copyright = string,
    filename = string,
    version = string,
    italicangle = integer,
    units_per_em = integer,
    ascent = integer,
    descent = integer,
    ...(+60)
})

Discard a loaded font.

Example:

local f = fontloader.open(
              '/usr/share/fonts/opentype/urw-base35/NimbusRoman-Regular.otf')
print(fontloader.to_table(f)) -- table: 0x3d23b50
fontloader.close(f)
print(fontloader.to_table(f)) -- false

Reference:

fontloader.apply_featurefile


function fontloader.apply_featurefile(
  font: userdata,
  filename: string
) -> errors table

Apply a feature file to a fontloader table.

Reference:

fontloader.apply_afmfile


function fontloader.apply_afmfile(
  font: userdata,
  filename: string
) -> errors table

Apply an AFM file to a fontloader table.

Reference:

fontloader.fields


function fontloader.fields(font: FontloaderFont {
    table_version = string,
    fontname = string,
    fullname = string,
    familyname = string,
    weight = string,
    copyright = string,
    filename = string,
    version = string,
    italicangle = integer,
    units_per_em = integer,
    ascent = integer,
    descent = integer,
    ...(+60)
}) -> fields FontloaderFontField[]

Example:

local f = fontloader.open('/usr/share/fonts/opentype/urw-base35/NimbusRoman-Regular.otf')
for _, value in ipairs(fontloader.fields(f)) do
    print(value, f[value])
end

Reference:

fontloader.fields


function fontloader.fields(glyph: FontloaderGlyph {
    name = string,
    unicode = integer,
    boundingbox = integer[],
    width = integer,
    vwidth = integer,
    tsidebearing = integer,
    lsidebearing = integer,
    class = string,
    kerns = FontloaderGlyphKern[],
    vkerns = table,
    dependents = string[],
    lookups = table,
    ...(+12)
}) -> fields FontloaderGlyphField[]

---Example:

lua local f = fontloader.open('/usr/share/fonts/opentype/urw-base35/NimbusRoman-Regular.otf') for _, value in ipairs(fontloader.fields(f.glyphs[1])) do print(value, f[value]) end---

Reference: