Pure-Go (CGO=0) reader for TeX Font Metric (.tfm) files, with scaling that is
bit-identical to TeX's own: every dimension a Sized font reports is the
scaled-point value TeX stores for the same font at the same size.
data, _ := os.ReadFile("cmsy10.tfm")
f, err := tfm.Parse(data) // validates like TeX and tftopl
if err != nil { /* errors.Is(err, tfm.ErrMalformed) */ }
s, _ := f.Scale(10 * tfm.Unity) // \font\f=cmsy10 at 10pt
axis := s.AxisHeight() // sigma22, in sp
w, h, d, ic, ok := s.Char('A') // scaled character box
k, ok := s.Kern('A', 'V') // scaled kern of a pairParsereturns a*Font(fix_words, char_info, lig/kern program, charlist, extensible recipes, parameters) and refuses malformed input before any allocation sized by the file.Font.Scale(at),ScaleDesign()andScaleMag(mag)give aSizedwithWidth/Height/Depth/Italic/Kern/Paramin sp.Sized.Sigma(n)/Sized.Xi(n)and named accessors (XHeight,Quad,AxisHeight,DefaultRuleThickness,BigOpSpacing1..5, ...) expose the TeXbook Appendix G parameters of math symbols (22) and math extension (13) fonts.
The scaling is store_scaled of tex.web (§571-§572, alpha/beta, slant not
scaled). The oracle is TeX itself: testdata/tectonic-0.17.0.oracle holds 7816
values printed by tectonic 0.17.0 (\showthe\fontdimen, \wd, \fontcharht,
\fontchardp, \fontcharic, kern pairs) for cmsy10 (7 to 2047pt), cmsy7, cmsy5,
cmex10 (10 to 2047pt), cmmi10 and cmr10; every one is equal to ours, to the sp.
go test -run TestOracle -update-oracle regenerates it (needs tectonic).
Deleting one of the divisions in storeScaled makes 938 values differ.
Input is untrusted. The header's lf is checked against the actual length
before any allocation, so a 24-byte file cannot cause a large allocation;
every index (char_info, lig/kern, extensible, charlist incl. cycles) is checked
as tex.web §560-§573 and tftopl do. FuzzParse is a coverage-guided fuzz
target (60 s run: 620k executions, no failure; CI runs it for 10 s), and
govulncheck runs in CI.
tftopl.web (file format, checks), tex.web read_font_info (§560-§575), §107 (xn_over_d), §103 (print_scaled).
BSD-3-Clause.