3

The following code writes the character width (cursor advance width), and the x and y coordinates of the glyph bounding box, to the log file. Here it is for letter "a" :

\documentclass[12pt]{article}
% Compile with lualatex.
\usepackage{fontspec}
\setmainfont{Latin Modern Roman}
\begin{document}
\directlua{
  local fontdata = font.getfont (font.current ())
  local descriptions = fontdata.shared.rawdata.descriptions                                                              
  local glyphdata = descriptions [utf.byte ("a")]
  inspect (glyphdata)
}
\end{document}

Relevant part of the log file:

table={
 ["boundingbox"]={ 34, -10, 474, 446 },
 ["depth"]=10,
 ["height"]=446,
 ["index"]=28,
 ["name"]="a",
 ["unicode"]=97,
 ["width"]=490,
}

That is correct, as determined by opening the font in Fontforge. The numerical value of the units is in terms of the design grid units. That would normally be 1000 per em for an OpenType font, but could be 1024, 2048, or 4096 for a TrueType font. It could even be none of those.

My question: Using code similar to the above, how can I determine what font design grid units were used?

1 Answer 1

5

You can print unit_per_em value from the font table:

\directlua{
  local fontdata = font.getfont (font.current ())
  local unit = fontdata.units_per_em
  texio.write_nl("unit per em: ") texio.write(unit)  texio.write_nl("")
  local descriptions = fontdata.shared.rawdata.descriptions                                                      local glyphdata = descriptions [utf.byte ("a")]
  inspect (glyphdata)
}
1
  • Ah, so simple. Just needed to know the right code. My prior brute-force method was to \sbox the character, measure width, compare to font point size, calculate.
    – user287367
    Commented May 8, 2023 at 15:43

You must log in to answer this question.