Skip to content

Failure to select right font variant #576

Closed

Description

@reknih

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 #185 (comment)

Activity

  1. added
    bug
    text
    on Apr 4, 2023
  2. ararunaufc commented on Apr 5, 2023

    @ararunaufc

    This could be (somehow) related to a problem I'm facing and that prompted #569 .

  3. florg-32 commented on Apr 28, 2023

    @florg-32

    I have a similar problem with googles Rubik font. It shows up as installed in Windows with numerous variants, but typst fonts --variants only finds one normal and italic. Consequently, specifying the font weight changes nothing.

  4. navh commented on Aug 3, 2023

    @navh

    Same thing, even after installing STIX Two manually, I still only see

    STIX Two Text
    - Style: Italic, Weight: 400, Stretch: 100%
    - Style: Normal, Weight: 400, Stretch: 100%
    
  5. mareikep commented on Aug 4, 2023

    @mareikep

    as a temporary fix, you can set the TYPST_FONT_PATHS environment variable to point to a directory where you store the other font variants. I am using that as a workaround until this issue is fixed

  6. JuliDi commented on Oct 2, 2023

    @JuliDi

    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:

    Source Sans Pro
    - Style: Normal, Weight: 400, Stretch: 100%
    - Style: Normal, Weight: 900, Stretch: 100%
    - Style: Italic, Weight: 900, Stretch: 100%
    - Style: Normal, Weight: 700, Stretch: 100%
    - Style: Italic, Weight: 700, Stretch: 100%
    - Style: Normal, Weight: 200, Stretch: 100%
    - Style: Italic, Weight: 200, Stretch: 100%
    - Style: Italic, Weight: 400, Stretch: 100%
    - Style: Normal, Weight: 300, Stretch: 100%
    - Style: Italic, Weight: 300, Stretch: 100%
    - Style: Normal, Weight: 600, Stretch: 100%
    - Style: Italic, Weight: 600, Stretch: 100%
    
  7. JuliDi commented on Oct 4, 2023

    @JuliDi

    Seems like this might have been some macos hiccup after installing the font, I guess tonights reboot is what fixed it.

  8. owiecc commented on Oct 6, 2023

    @owiecc
    SponsorContributor

    To follow-up. The fonts from the original comment are shown as:

    Quadraat Pro
    - Style: Normal, Weight: 700, Stretch: 100%
    - Style: Normal, Weight: 700, Stretch: 100%
    - Style: Normal, Weight: 600, Stretch: 100%
    - Style: Italic, Weight: 600, Stretch: 100%
    - Style: Normal, Weight: 400, Stretch: 100%
    - Style: Normal, Weight: 400, Stretch: 100%
    
  9. CircuitCoder commented on Oct 17, 2023

    @CircuitCoder

    Sorry, there was some other configuration that overrode the weight setting. Typst was apparently working fine with Source Han Sans.


    Seeing the same problem with some CJK fonts. The fonts is installed as an OTC.

    typst fonts --variants gives:

    Source Han Sans SC
    - Style: Normal, Weight: 250, Stretch: 100%
    - Style: Normal, Weight: 300, Stretch: 100%
    - Style: Normal, Weight: 350, Stretch: 100%
    - Style: Normal, Weight: 500, Stretch: 100%
    - Style: Normal, Weight: 900, Stretch: 100%
    - Style: Normal, Weight: 400, Stretch: 100%
    - Style: Normal, Weight: 700, Stretch: 100%
    
  10. peng1999 commented on Feb 1, 2024

    @peng1999
    Contributor

    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

  11. owiecc commented on Feb 2, 2024

    @owiecc
    SponsorContributor

    @peng1999 Is there a way to see what metadata Typst sees for each font? I did not know which parameters were detected wrong so I had to blindly set overrides until the fonts were selected correctly. It did not take a lot of time but it would be nice not to fly blind.

    E.g. for the fonts from the original post (#576 (comment))

    ❯ fc-list | grep Quadraat
    /Users/user/Library/Fonts/QuadraatPro-Ita.otf: Quadraat Pro,QuadraatPro\-Ita:style=Italic,Regular
    /Users/user/Library/Fonts/QuadraatPro-Demibold.otf: Quadraat Pro,QuadraatPro\-Demibold:style=Demibold,Regular
    /Users/user/Library/Fonts/QuadraatPro-Bold.otf: Quadraat Pro,QuadraatPro\-Bold:style=Bold,Regular
    /Users/user/Library/Fonts/QuadraatPro-DemiboldItalic.otf: Quadraat Pro,QuadraatPro\-DemiboldItalic:style=Demibold Italic,Regular
    /Users/user/Library/Fonts/QuadraatPro.otf: Quadraat Pro:style=Regular
    /Users/user/Library/Fonts/QuadraatPro-BoldIta.otf: Quadraat Pro,QuadraatPro\-BoldIta:style=Bold Italic,Regular

    I had to add

    "QuadraatPro-Ita" => Exception::new()
        .style(FontStyle::Italic),
    "QuadraatPro-BoldIta" => Exception::new()
        .style(FontStyle::Italic),

    to fix Quadraat Pro selection.

    What is more surprising is that originally, when I wrote #185 (comment), Typst would show an regular-italic version when regular-normal was set. Now (for normal style) regular, medium and bold weights work ok but instead of regular-italic and bold-italic Typst uses a semibold-italic.

    Quadrat Pro fix

    #set page(width: 5cm, height: 10cm)
    
    #set text(font: "Quadraat Pro")
    Quadraat Pro
    
    #text(weight: "regular")[regular] \
    #text(weight: "medium")[medium] \
    #text(weight: "bold")[bold]
    
    #text(style: "normal", weight: "regular")[normal + regular] \
    #text(style: "normal", weight: 400)[normal + 400] \
    #text(style: "normal", weight: "medium")[normal + medium] \
    #text(style: "normal", weight: 600)[normal + 600] \
    #text(style: "normal", weight: "bold")[normal + bold] \
    #text(style: "normal", weight: 700)[normal + 700]
    
    #text(style: "italic", weight: "regular")[italic + regular] \
    #text(style: "italic", weight: 400)[italic + 400] \
    #text(style: "italic", weight: "medium")[italic + medium] \
    #text(style: "italic", weight: 600)[italic + 600] \
    #text(style: "italic", weight: "bold")[italic + bold] \
    #text(style: "italic", weight: 700)[italic + 700]
  12. mkroening commented on Feb 2, 2024

    @mkroening
    Contributor
  13. owiecc commented on Feb 2, 2024

    @owiecc
    SponsorContributor

    I do not see anything obviously wrong here but I do not know how styles are matched. If on fontSubfamily then it looks correct. Note also windows and mac platforms have different conventions on fontFamily and fontSubfamily. I will try to test this font on windows and see if the behaviour is different.

    Naming table Quadraat Pro QuadraatPro-Ita
  14. peng1999 commented on Feb 3, 2024

    @peng1999
    Contributor

    Is there a way to see what metadata Typst sees for each font?

    I believe the most straightforward approach is to insert a debug line in book.rs:

    --- a/crates/typst/src/text/font/book.rs
    +++ b/crates/typst/src/text/font/book.rs
    @@ -286,6 +286,7 @@ impl FontInfo {
                     flags.insert(FontFlags::SERIF);
                 }
             }
    +        eprintln!("{:?}: {:?} {:?} {:?} {:?}", ps_name, family, variant.style, variant.weight, variant.stretch);
    
             Some(FontInfo {
                 family,

    Then you can easily see which font to patch.

  15. 1 remaining item

  16. laurmaedje commented on Feb 14, 2024

    @laurmaedje
    Member

    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.

    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.

  17. inferiorhumanorgans commented on Aug 12, 2024

    @inferiorhumanorgans
    Contributor

    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

    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 PANOSE field of the OS/2 table, but the ttf-parser only presents usWeightClass. Google's Roboto Condensed (both the static and variable versions) lists the correct stretch in the STAT table, but only the incorrect value in the OS/2 table's usWidthClass is presented.

    As far as Quadraat is concerned, is_italic only checks the fsSelection value in the OS/2 table. If is_italic is 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 STAT support in ttf-parser soonish, and from the looks of it the PANOSE bits shouldn't be too difficult either.

    For #3321, I used https://opentype.js.org/ as suggested in #3213 (comment).

    @owiecc I'd recommend this:

    https://github.com/fonttools/fonttools

    On macOS it's available via homebrew and macports as well. The included ttx tool 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.

  18. laurmaedje commented on Aug 12, 2024

    @laurmaedje
    Member
  19. inferiorhumanorgans commented on Aug 13, 2024

    @inferiorhumanorgans
    Contributor

    PR for STAT table:

    harfbuzz/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. ttx will 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.

  20. RazrFalcon commented on Aug 14, 2024

    @RazrFalcon

    Just to clarify this one:

    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 PANOSE field of the OS/2 table, but the ttf-parser only presents usWeightClass. Google's Roboto Condensed (both the static and variable versions) lists the correct stretch in the STAT table, but only the incorrect value in the OS/2 table's usWidthClass is presented.

    The logic behind getting "proper" values from TrueType fonts is a completely undocumented behavior to my knowledge. At least I haven't found anything useful. ttf-parser's high-level API is loosely based on FreeType2 and harfbuzz behavior.
    And it seems like every system library simply does whatever it wants.
    Not to mention that the same value can be stored in multiple places in a font, meaning that priority is a mystery is well.

    For example, Face::is_italic is a bit wrong right now, since it ignores the Face::italic_angle, which does in fact affect some fonts. But it would be a breaking change, so I haven't done it yet.

    I don't mind updating ttf-parser's logic, but each change would be a serious breaking change...

  21. inferiorhumanorgans commented on Aug 14, 2024

    @inferiorhumanorgans
    Contributor

    For example, Face::is_italic is a bit wrong right now, since it ignores the Face::italic_angle, which does in fact affect some fonts. But it would be a breaking change, so I haven't done it yet.

    Yeah fonts are a bit of an unorganized mess. I suppose the first thing is to define what an italic font is. Sounds like there at least four places to check: panose and fsSelection in the OS2 table, italicAngle in the post table, and the STAT table.

    Are there any open source (or open source friendly) fonts that demonstrate this? I'd like to build a corpus of fonts to test against (with the expectation that this would probably live in a repository outside of typst or ttf-parser).

    (later, rinse, and repeat with the weight and width axes)

    I don't mind updating ttf-parser's logic, but each change would be a serious breaking change...

    Beyond breaking compatibility I wonder how much of a performance hit scanning multiple tables will incur.

  22. RazrFalcon commented on Aug 15, 2024

    @RazrFalcon

    Note that the STAT is relatively new and not many fonts use it.

    As for a corpus of open source/free fonts - I wish there were something like that, but unfortunately no.
    Your best bet is to simply scrape all open/free fonts until you find one that match your needs or generate one yourself via fonttools. In both cases that's a large time investment.

    Beyond breaking compatibility I wonder how much of a performance hit scanning multiple tables will incur.

    Performance impact will be neglectable. No need to worry about it.

  23. navh commented on Aug 30, 2024

    @navh
    $ typst  fonts --variants
    Adobe Caslon Pro
    - Style: Normal, Weight: 600, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 700, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 400, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 400, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 600, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 700, Stretch: FontStretch(1000)
    

    Here's another one, these are all the 'italic' variant. Maybe supporting a way to manually punch in a bunch of paths would be nice? Anyway, thanks for making progress on this :)

  24. Enivex commented on Aug 30, 2024

    @Enivex
    Collaborator
    $ typst  fonts --variants
    Adobe Caslon Pro
    - Style: Normal, Weight: 600, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 700, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 400, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 400, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 600, Stretch: FontStretch(1000)
    - Style: Normal, Weight: 700, Stretch: FontStretch(1000)
    

    Here's another one, these are all the 'italic' variant. Maybe supporting a way to manually punch in a bunch of paths would be nice? Anyway, thanks for making progress on this :)

    I suspect this is a variable font, which typst does not currently support.

    (But I may be wrong)

  25. navh commented on Aug 30, 2024

    @navh

    I don't think so, ls Fonts/Adobe Calson Pro* spits out individual files for bold and italic, to use these in fontspec I've got to load them all in individually.

    'Adobe Caslon Pro Bold.ttf'
    'Adobe Caslon Pro Bold Italic.ttf'
    'Adobe Caslon Pro Italic.ttf'
    'Adobe Caslon Pro Regular.ttf'
    'Adobe Caslon Pro Semibold.ttf'
    'Adobe Caslon Pro Semibold Italic.ttf'
    
  26. inferiorhumanorgans commented on Aug 30, 2024

    @inferiorhumanorgans
    Contributor

    @navh Can you run the variant-inspect utility on one or more of the italic files and post the results? The version of Caslon Pro I was able to find is an OpenType version that ttf-parser is correctly able to identify as italic.

    https://git.sr.ht/~az1/variant-inspect

    e.g.

    Screenshot 2024-08-30 at 15 29 59
  27. navh commented on Sep 1, 2024

    @navh
    ╭────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
    │ Adobe Caslon Pro Italic.ttf: Adobe Caslon Pro (Version 2.093;PS Version 2.000;hotconv 1.0.68;makeotf.lib2.5.35818) │
    ├──────────────────────┬───────────────────┬─────────────────────────────────────────────────────────┬───────┬───────┤
    │                      │ ttf_parser::Face  │                          OS/2                           │ post  │ STAT  │
    │                      │                   │ fsSelection  │ PANOSE  │ usWeightClass  │ usWidthClass  │       │       │
    ├──────────────────────┼───────────────────┼──────────────┼─────────┼────────────────┼───────────────┼───────┼───────┤
    │ bold                 │ F                 │ F            │ F       │                │               │       │       │
    │ weight               │ Normal            │              │ Book    │ Normal         │               │       │       │
    │ italic / oblique     │ F / F             │ F / F        │ T       │                │               │ -22   │       │
    │ monospaced           │ F                 │              │ F       │                │               │ F     │       │
    │ stretch              │ Normal            │ Normal       │         │                │ Normal        │       │       │
    ╰──────────────────────┴───────────────────┴──────────────┴─────────┴────────────────┴───────────────┴───────┴───────╯
    
  28. owiecc commented on Sep 1, 2024

    @owiecc
    SponsorContributor

    That is what I got for my Quadraat Pro (bold+italic version). I am not sure what I should see here. If necessary, I can raise the issue with the foundry if this is an issue with the font.

    ❯ ./variant-inspect QuadraatPro-BoldIta.otf
    ╭─────────────────────────────────────────────────────────────────────────────────────────────────────────╮
    │ QuadraatPro-BoldIta.otf: QuadraatPro-BoldIta (Version 7.504; 2011; Build 1024)                          │
    ├──────────────────┬──────────────────┬─────────────────────────────────────────────────────┬──────┬──────┤
    │                  │ ttf_parser::Face │                        OS/2                         │ post │ STAT │
    │                  │                  │ fsSelection │ PANOSE │ usWeightClass │ usWidthClass │      │      │
    ├──────────────────┼──────────────────┼─────────────┼────────┼───────────────┼──────────────┼──────┼──────┤
    │ bold             │ F                │ F           │ T      │               │              │      │      │
    │ weight           │ Bold             │             │ Bold   │ Bold          │              │      │      │
    │ italic / oblique │ F / F            │ F / F       │ F      │               │              │ -3.5 │      │
    │ monospaced       │ F                │             │ F      │               │              │ F    │      │
    │ stretch          │ Normal           │ Normal      │        │               │ Normal       │      │      │
    ╰──────────────────┴──────────────────┴─────────────┴────────┴───────────────┴──────────────┴──────┴──────╯
    
  29. inferiorhumanorgans commented on Sep 1, 2024

    @inferiorhumanorgans
    Contributor

    @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 two OS/2 fields, although a non-zero slant angle is set in the post table.

    For ttf-parser (and/or typst) the is_italic logic should also check the post table and the PANOSE field on the OS/2 table.

    For the foundry the fsSelection[2] field in the OS/2 table should have bit 0 or 1 (depending on the head table) set, and the PANOSE[3] field should have the letterform sub-digit set to indicate an italic/oblique variant. If they ever add a STAT[4] table it should have a non-zero entry for the ital axis. 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.rs that 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 PANOSE field (which ttf-parser should, IMO, be checking).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugtext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions