typst: Failure to select right font variant
When using the following fonts, Typst fails to select the right variant.
QuadraatPro.otf
QuadraatPro-Ita.otf
QuadraatPro-Demibold.otf
QuadraatPro-DemiboldItalic.otf
QuadraatPro-Bold.otf
QuadraatPro-BoldIta.otf
#set text(font: "Quadraat Pro") selects Quadraat Pro Italic. I did not manage to get the Regular style. Names in the system are: Regular, Italic, Demibold, Demibold Italic, Bold, Bold Italic.
_Originally posted by @owiecc in https://github.com/typst/typst/issues/185#issuecomment-1483411869_
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 28 (15 by maintainers)
I have a similar problem with googles Rubik font. It shows up as installed in Windows with numerous variants, but
typst fonts --variantsonly finds one normal and italic. Consequently, specifying the font weight changes nothing.PR for
STATtable:RazrFalcon/ttf-parser#166
Next steps
PANOSE support. In progress.
Update typst to compile with ttf-parser 0.24+. I tried this the other day and it looks like some stuff got moved around? I’m not prepared to go too much further on this as it seems like it’s got a pretty broad scope.
Collect more detailed information from the fonts in the exceptions list so we know where the correct information typically lives.
ttxwill dump the whole font (including bytecode and whatnot that’s likely to be copyrighted) and so with commercial fonts its output shouldn’t be shared in its entirety. However for the purposes of paring down the exceptions list there are probably only a few tables that are relevant. I’ll come up with an xpath query for this, but I don’t have access to some/all/many? of those fonts.Determine where the additional logic should live. The main issue is that having to check multiple tables to catch the edge cases will slow down parsing and thus may not be suitable for ttf-parser. On the other hand having a more thorough check for e.g. stretch and slant is useful for other consumers beyond typst.
@owiecc The second column (
ttf_parser::Face) indicates how the font is identified with ttf-parser’s high level functions (e.g.Face::is_bold,Face::is_italic,Face::width, etc.). This should match the other columns and the reality of what style the font actually is. If you look at the italic row you should see true values across the board (ignoring the distinction between italic and oblique for now).The are two problems. First, ttf-parser’s
is_italic[1] function only checks one of the four (or more?) places where a font can indicate that it’s slanted. The other is that Quadraat Pro has the wrong information in the twoOS/2fields, although a non-zero slant angle is set in theposttable.For ttf-parser (and/or typst) the
is_italiclogic should also check theposttable and thePANOSEfield on theOS/2table.For the foundry the
fsSelection[2] field in theOS/2table should have bit 0 or 1 (depending on theheadtable) set, and thePANOSE[3] field should have the letterform sub-digit set to indicate an italic/oblique variant. If they ever add aSTAT[4] table it should have a non-zero entry for theitalaxis. I’d also consider updating the full name to be a properly formatted, human readible string e.g. Quadraat Pro Bold Italic instead of just the filename.For a short-term work around you can add an exception in
typst/src/text/font/exceptions.rsthat sets the italic style.1: https://docs.rs/ttf-parser/latest/ttf_parser/struct.Face.html#method.is_italic
2: https://learn.microsoft.com/en-us/typography/opentype/spec/os2#fsselection
3: https://monotype.github.io/panose/pan2.htm
4: https://learn.microsoft.com/en-us/typography/opentype/spec/stat
@navh Same as above, this looks like a regression or a bug in the TrueType versions of the font. However Adobe does set the
PANOSEfield (which ttf-parser should, IMO, be checking).After a bit of poking at things there appears to be correct information in many of these fonts, but not where ttf-parser and Typst are looking. Arial Black, for instance, lists a proper weight in the
PANOSEfield of the OS/2 table, but the ttf-parser only presentsusWeightClass. Google’s Roboto Condensed (both the static and variable versions) lists the correct stretch in theSTATtable, but only the incorrect value in the OS/2 table’susWidthClassis presented.As far as Quadraat is concerned,
is_italiconly checks thefsSelectionvalue in the OS/2 table. Ifis_italicis failing then the italic bit isn’t set, but there’s probably an italic indicator somewhere else. Parsing human readable names is brittle, so it’s to be expected that ttf-parser isn’t going through the name table.Rather than play whack-a-mole with exceptions, I think a more ideal solution would be to improve ttf-parser so that the desired information is exposed and then settle upon some sort of hierarchy (in ttf-parser or Typst) of authority for the various fields. I expect that would cut down on the number of exceptions greatly (although I wouldn’t mind being able to opt-out of the font family elision).
To that end I’ll have a PR for
STATsupport in ttf-parser soonish, and from the looks of it thePANOSEbits shouldn’t be too difficult either.@owiecc I’d recommend this:
https://github.com/fonttools/fonttools
On macOS it’s available via homebrew and macports as well. The included
ttxtool dumps an XML representation of a font that’s more complete than what’s presented with opentype.js. If you want to attach a ttx file I’d recommend limiting the dump to whatever tables are relevant.That makes sense. I don’t really know which of these fonts are fixed and which are not, so if someone knows that a particular fonts is still unfixed, feel free to open an issue for that one.
This issue has become the-issue-to-points-to of font selecting problem, and PR #3228 now provides a mechanism to solve the it. I propose to close this issue, and open per-font issue separately, to track whether a font with wrong metadata has been added to exception list. cc @Enivex
Using “Source Sans Pro” in the online editor works, i.e.,
*strong*causes a bold font. On my machine, the same code does not produce a bold font (I even manually installed the bold variant).The following fonts are found with
typst fonts --variants, which looks a little odd to me: