Skip to content

corefonts: black weight font variant is selected when regular weight was requested #3213

Closed

Description

@mkroening

Description

On Linux, with the core fonts for the Web installed with https://corefonts.sourceforge.net, Typst selects Arial Black, although Arial would be correct.

Dockerfile for reproduction:

FROM ghcr.io/typst/typst:latest

ENV TYPST_FONT_PATHS=/usr/share/fonts/truetype/msttcorefonts

RUN set -eux; \
    apk add --no-cache msttcorefonts-installer; \
    update-ms-fonts; \
    apk del --no-cache msttcorefonts-installer;

Typst file:

#text(font: "Arial", weight: "regular")[regular]

#text(font: "Arial", weight: "black")[black]

Typst falsely classifies Arial_Black.ttf as regular weight, resulting in both lines being written in Arial Black, when you compile this.

This issue does not occur on macOS with the natively installed Microsoft fonts.

Reproduction URL

No response

Operating system

Linux

Typst version

  • I am using the latest version of Typst

Activity

  1. added
    bug
    on Jan 17, 2024
  2. Enivex commented on Jan 17, 2024

    @Enivex
    Collaborator

    Duplicate of #576

  3. added
    duplicate
    on Jan 17, 2024
  4. laurmaedje commented on Jan 17, 2024

    @laurmaedje
    Member

    Where can I get ahold of the raw Arial Black ttf file to inspect it?

  5. mkroening commented on Jan 17, 2024

    @mkroening
    ContributorAuthor

    Duplicate of #576

    Is this a duplicate, though? That issue seems to be about style, mainly, not about weight.

    Where can I get ahold of the raw Arial Black ttf file to inspect it?

    I think the best way is to build and run the Dockerfile from the issue description and copying /usr/share/fonts/truetype/msttcorefonts to a mounted host path. That's how I did it, anyway.

  6. laurmaedje commented on Jan 17, 2024

    @laurmaedje
    Member

    Ok, I've managed to extract it. The OS/2 table of the font specifies a usWeightClass of 400, which is clearly wrong.

  7. mkroening commented on Jan 17, 2024

    @mkroening
    ContributorAuthor

    I suspected something like that, but was not familiar enough with fonts to confirm. I don't think, we should wait for this to be fixed upstream, since this the de-facto standard way of installing MS fonts on Linux since forever. That means an upstream fix would take years to reach users.

    What do you think is the best way forward here?

  8. laurmaedje commented on Jan 17, 2024

    @laurmaedje
    Member

    I'm not sure. Perhaps there is no way but to ship exceptions, but if so, we should definitely apply them very carefully, e.g. maybe not for any font ending with "black", but rather specifically for Arial Black. I'll discuss this with @reknih.

  9. peng1999 commented on Jan 18, 2024

    @peng1999
    Contributor

    Typst already have some ad-hoc exceptions for font names. Maybe we can generalize that and introduce a font map mechanism into typst. That can be a mapping embedded into typst binary like this:

    NewComputerModern08-Italic:
      - family: "New Comupter Modern"
    NewComputerModern08-BookItalic:
      - family: "New Comupter Modern"
      - weight: 450

    The keys correspond to the FULL_NAME record in the font file. Users encountering fonts with incorrect metadata can simply submit a pull request to update this file accordingly. For instance:

    "Arial Black":
      - family: Arial
      - weight: 900

    In the future, we can offer an API that allows users to supply their own fontmap.yaml, which would override the default one built into Typst.

  10. laurmaedje commented on Jan 18, 2024

    @laurmaedje
    Member

    I wonder whether should just decide based on the FULL_NAME or perhaps also on the checksum. The problem with the latter is that some fonts will continue to retain their "wrong" metadata across multiple versions, so FULL_NAME is probably the better albeit less safe choice.

  11. mkroening commented on Jan 18, 2024

    @mkroening
    ContributorAuthor

    I wonder whether should just decide based on the FULL_NAME or perhaps also on the checksum. The problem with the latter is that some fonts will continue to retain their "wrong" metadata across multiple versions, so FULL_NAME is probably the better albeit less safe choice.

    I agree. Can I help with this in any way? Is someone experienced willing to work on this, or should I give this a go?

  12. laurmaedje commented on Jan 18, 2024

    @laurmaedje
    Member

    I'd be happy for you to work on this, but let's first nail down the design.

    There are various things going on in FontInfo::from_ttf:

    1. We check italic, oblique and slanted with metadata + fallback to full name (the latter is very hacky)
    2. We have an extra hack for New CM Book weights
    3. Noto & New CM use full name instead of family because of Display variants
    4. We have this very hacky function typographic_family which turns full names into nice names (it has false positives)

    1+2 would be easily handled by the exception list. 3 is a little less nice to handle with a list because there are soo many Noto fonts. We'd maybe need a regex or starts_with mechanism to define exceptions for those.

    4 is the worst of all hacks imo. Technically, it should be replaced by just using "Typographic Family", but that makes certain variants inaccessible because more than just style/weight/stretch is grouped (e.g. Display). This is related to #2098.
    There's been quite some discussion about that issue on Discord.

    I think that would should solve all of the problems in one sweep. One possible solution, which is quite close to the latest state of the Discord discussion is to support two ways to select a font:

    • Typst-prettified: We should move away from the current style trimming as much as possible and just use "Typographic Family" with fallback to "Family". To access some variants that become inaccessible, the Raw ways exist. This is also where we'd use an exception list to get a nice name and correct metadata for the font. I'm not sure how well just a list would suffice for this though. Apart from the Noto stuff, many Apple font names for example randomly start with '.' for some reason, which I'd like to keep getting rid of.
    • Raw: A user can directly supply the complete FULL_NAME (or FAMILY_NAME, or both) to pick one particular face.

    Any font selection will result in a list of candidate font faces. In the first case, there might be multiple and the metadata will be used to find the best pick. In the second case, there will most likely be just one.

  13. laurmaedje commented on Jan 18, 2024

    @laurmaedje
    Member

    While this is sort of a dupe, I've reopened so that we can properly use it for further discussion and close it once we've implemented something to this effect.

  14. peng1999 commented on Jan 18, 2024

    @peng1999
    Contributor

    The problem

    My understanding about the current situation:

    Typst uses (family, style, weight, stretch, size) 5-tuple to identify a font. This has many advantages, for example typst can select nearest font in space if the coordinate specified by user not exists. However, the OpenType standard has different coordinate system to select a font. In an OpenType font, a font can be identified by: (I'm using names from ttf_parser)

    (The size axis is not really relevant here so I just drop it in the following discussion)

    • (a: FAMILY, SUBFAMILY). There should be at most 4 SUBFAMILY for a FAMILY. Usually the 4 SUBFAMILY is the same as typst's style, but it's not always the case.
    • (b: TYPOGRAPHIC_FAMILY, TYPOGRAPHIC_SUBFAMILY)
    • (c: FULL_NAME)
    • POST_SCRIPT_NAME and UNIQUE_ID. But these two way is less relevant here since FULL_NAME is usually more nice to read.

    OpenType font formats also includes style and weight, but font designers make fonts without aware of the typst coordinate system. That's why typst is using a very hacking algorithm to calculate the correct family-coordinate of a font. This part is described above by @laurmaedje.

    Ideally, all information other than italic, weight and stretch should go into the family axis of typst coordinate, and it should only contain information thereof. But that's a rather hard task. For example:

    • Latin Modern encodes the optical size information into the TYPOGRAPHIC_SUBFAMILY field. (b: Latin Modern Roman, 10 Regular) and (b: Latin Modern Roman, 12 Regular) both be mapped to (Latin Modern Roman, normal, 400, 100%) in typst.
    • Arial encodes it's weight information only into the FAMILY field. That makes (a: Arial, Regular) and (a: Arial Black, Regular) both be mapped to (Arial, normal, 400, 100%) in typst.

    It's clear that the typst coordinate system will not work for all font without some manual intervention.

    Proposed solution

    In #3213 (comment) @laurmaedje metioned a simpler approach: Do not try very hard to make the typst coordinate working. Just fallback to the OpenType coordinate system (c: FULL_NAME) if the typst coordinate system fails.

    I think that is not optimal because when using FULL_NAME, you drop the italic, weight and stretch info. emph and strong will not work. Thus I proposed another approach in #3213 (comment) : Design a systematic approach to map OpenType coordinate to the typst coordinate. This should combine automatic procedure and manual whitelist. This approach has extra future possibilities: We can include small caps in the style axis and map Some Font and Some Font SmallCaps into a single family, to enable smallcaps function to work for more font families.

  15. laurmaedje commented on Jan 18, 2024

    @laurmaedje
    Member

    Typst uses (family, style, weight, stretch, size) 5-tuple to identify a font.

    OpenType font formats also includes style and weight, but font designers make fonts without aware of the typst coordinate system. That's why typst is using a very hacking algorithm to calculate the correct family-coordinate of a font.

    Typst only uses the first four, not the size for font selection. The family-style-weight-stretch coordinate system is pretty common, e.g. in CSS. It is not Typst-specific. The only thing that's specific is the (hacky) effort Typst makes to have a nicer family name and the few exceptions where Typst patches the metadata based on name.

    I think that is not optimal because when using FULL_NAME ... emph and strong will not work.

    That's a fair criticism.

    Thus I proposed another approach in #3213 (comment): Design a systematic approach to map OpenType coordinate to the typst coordinate.

    To be clear: I am not against adding a general exception mechanism. But as you said, it would require not just a list, but also an automatic produce to get a "nice" uniform family consisting of only fonts differing in style/weight/stretch from the available data in the font. In theory, font names could also be selected in such a way that this property is enforced for a given font set, but that might be weird because an existing font name might change when a new font is installed.

    That's why I proposed doing something more predictable and mostly relying on TYPOGRAPHIC_FAMILY. I'd be all on board with a more systematic approach to map OpenType coordinate to the CSS coordinate system if we find a good one.

  16. mkroening commented on Jan 19, 2024

    @mkroening
    ContributorAuthor

    Typst only uses the first four, not the size for font selection.

    Wouldn't it make sense for Typst to also use size for font selection? New Computer Modern (and probably others) has different font files optimized for 8 point and 10 point sizes.

  17. reknih commented on Jan 19, 2024

    @reknih
    Member

    Computer Modern is actually pretty unique in that respect. The differences in the two variants are due to the Optical Size difference of the fonts, consisting of small weight and detail differences that make the fonts more legible at their respective sizes. Some modern font families include "Display" variants which are intended for display font sizes (>~18pt) but don't make such a clear distinction. Variable fonts have begun to ship the opsz variation axis for optical sizing. The units on that scale are standardized with regards to point size.

    Typst does not yet support variable fonts (#185) but once we do, we should automatically adjust the opsz axis based on font size. In my opinion, support for legacy approaches for optical sizing (dedicated denominated fonts or Display variants) belongs in the same work package instead of here.

  18. peng1999 commented on Jan 19, 2024

    @peng1999
    Contributor

    I implemented #3228. Feedback appreciated!

  19. changed the title [-]Black weight font variant is selected when regular weight was requested[/-] [+]corefonts: black weight font variant is selected when regular weight was requested[/+] on Feb 1, 2024
  20. peng1999 commented on Feb 1, 2024

    @peng1999
    Contributor

    @mkroening #3228 Has been merged. Would you like to submit a PR to fix your Arial Black font issue?

  21. mkroening commented on Feb 1, 2024

    @mkroening
    ContributorAuthor

    Ok, I've managed to extract it. The OS/2 table of the font specifies a usWeightClass of 400, which is clearly wrong.

    @laurmaedje, can you point me to a tool that can be used to extract such information? I'd like my PR fixing this to be comprehensive for all corefonts.

  22. laurmaedje commented on Feb 1, 2024

    @laurmaedje
    Member

    I use opentype.js.org. You need the font inspector tab rather than the glyph inspector and then check the OS/2 table.

  23. mkroening commented on Feb 2, 2024

    @mkroening
    ContributorAuthor

    I just opened #3321.

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

    bugduplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions