Skip to content

Support bitmap fonts #4335

Open
Open

Description

@Nerixyz

Description

Currently, attempting to use glyphs from fonts that have the raster image type other than PNG panics with:

failed to decode image (Format error decoding Png: Invalid PNG signature.)

in draw_raster_glyph, because it assumes every color glyph has the image type PNG.

I'm not exactly sure what these fonts are called, as FontForge refuses to open it, because its name isn't ASCII 🙃

I made a patch to at least add support for BitmapMonoPacked (the type of glyphs in SimSun) in Nerixyz@4188128, however I don't think that's the correct approach. Since the bitmap is monochrome, it should be fine to color it (→ it should be rendered as a "regular" glyph in render_text). Since I'm new to the codebase, I wanted to ask where I should add support for the monochrome glyphs.

Use Case

At least on Windows, Typst falls back to the SimSun font for Chinese characters. When rendering to PNG, Typst currently panics (not really a compiler bug, I'd say, more like a missing feature).

For example, a Typst file having just 年 compiled as PNG will panic on Windows.

Activity

  1. added
    feature request
    on Jun 5, 2024
  2. LaurenzV commented on Jun 5, 2024

    @LaurenzV
    Collaborator

    At least on Windows, Typst falls back to the SimSun font for Chinese characters.

    Damn... Is the default font for CJK characters on Windows really a bitmap font? I think what should be changed first is that for non-emoji glyphs, Typst avoids falling back to a bitmap font unless absolutely necessary.

  3. Enivex commented on Jun 5, 2024

    @Enivex
    Collaborator

    Are we talking Windows without a CJK language pack? I can't imagine Chinese Windows uses a bitmap font.

  4. Nerixyz commented on Jun 5, 2024

    @Nerixyz
    ContributorAuthor

    Are we talking Windows without a CJK language pack?

    Yes. I suppose a CJK language pack installs some better fonts, as I couldn't find a decent preinstalled font. Microsoft JhengHei looks fine (although I don't think it's a serif font).

  5. added
    text
    on Jun 21, 2024
  6. LaurenzV commented on Aug 25, 2024

    @LaurenzV
    Collaborator

    I made a patch to at least add support for BitmapMonoPacked (the type of glyphs in SimSun) in Nerixyz@4188128, however I don't think that's the correct approach. Since the bitmap is monochrome, it should be fine to color it (→ it should be rendered as a "regular" glyph in render_text)

    I think at least as a start, it would be fine to just assume black. If we want to color them, we would probably have to create a new Type3 font for each color, which sounds a bit painful, I'm not sure it's worth the effort for now.

  7. added
    crash
    bug
    on Aug 25, 2024
  8. inferiorhumanorgans commented on Aug 26, 2024

    @inferiorhumanorgans
    Contributor

    Cambria Math[a] uses the byte aligned (BitmapMono) format, which is largely the same idea. The approach I ended up with was to expand the bitmap into a PNG in draw_raster_glyph, leaving everything else alone. I've no strong feelings one way or another, but am willing to roll up both of these into a PR with a bit of guidance.

    The IBM BIOS console font[b] was turned into a freely licensed font with bit aligned glyphs (.ttf is scalable, .otb is bitmap only).

    However… Cambria Math raises two issues:

    1.) There are both outline (glyf) and bitmap (ebdt) versions of most (all?) of the glyphs. This is different from Noto Color Emoji which has SVG and colr (also SVG?) variants. In thinking about this the approach of preferring a bitmap when the strike size matches the output size desired by typst otherwise using the outline instead seems appropriate. Ttf-parser currently does not expose any way of enumerating the available strike sizes but does return the strike size in the RasterGlyphImage struct. Thoughts?

    2.) (apologies as I'm not super familiar with the appropriate terminology) The glyphs in Cambria Math (e.g. 12x13) are smaller than the bounding box for the glyph (pixels per em = 19x19). By projecting the bitmap onto a 19x19 grid and positioning it at the lower left I get results that look about right. The CBLC/EBLC table allows different values for the X and Y axes, but ttf-parser only exposes one 'pixels per em' value. Does this seem like a reasonable approach?

    Screenshot 2024-08-25 at 17 40 55 Screenshot 2024-08-25 at 21 58 45

    a: https://download.cnet.com/download/powerpoint-viewer/3000-2075_4-75450958.html
    b: https://int10h.org/oldschool-pc-fonts/fontlist/?1#ibm-bios

  9. LaurenzV commented on Aug 26, 2024

    @LaurenzV
    Collaborator

    In thinking about this the approach of preferring a bitmap when the strike size matches the output size desired by typst otherwise using the outline instead seems appropriate. Ttf-parser currently does not expose any way of enumerating the available strike sizes but does return the strike size in the RasterGlyphImage struct.

    I still think that the best approrach would be to somehow detect whether the glyph we are dealing with is an emoji or not. If it is, we prefer the bitmap version, if it is not, we prefer the outline version. I don't see a reason why we would ever want to prefer the bitmap version over the outlined version for non-emoji chararcters. But the question is how easy it is to detect this, especially since emojis can consist of multiple Unicode code points.

  10. inferiorhumanorgans commented on Aug 26, 2024

    @inferiorhumanorgans
    Contributor

    I still think that the best approrach would be to somehow detect whether the glyph we are dealing with is an emoji or not. If it is, we prefer the bitmap version, if it is not, we prefer the outline version.

    I don't think specific exceptions ought to be made for emoji, however preferring color glyphs (bitmap or outline) seems like a good idea.

    I've two emoji fonts at my disposal: Apple Color Emoji has both monochrome outlines (glyf) and color bitmaps (sbix). Noto Color Emoji only has outline glyphs (glyf, svg, and COLR tables).

    OTOH SimSun and Cambria Math have both outline (glyf) and bitmap (ebdt) glyphs.

    I don't see a reason why we would ever want to prefer the bitmap version over the outlined version for non-emoji chararcters.

    At small sizes on low-resolution devices the bitmap glyphs are intended to look better than the outlines.

  11. LaurenzV commented on Aug 26, 2024

    @LaurenzV
    Collaborator

    At small sizes on low-resolution devices the bitmap glyphs are intended to look better than the outlines.

    I guess that could apply to the PNG output, but our main output target is PDF, and I don't think anyone will view those on low-resolution devices.

  12. Enivex commented on Aug 26, 2024

    @Enivex
    Collaborator

    I'm surprised that anyone actually wants to use bitmap fonts in 2024

  13. inferiorhumanorgans commented on Aug 26, 2024

    @inferiorhumanorgans
    Contributor

    I guess that could apply to the PNG output, but our main output target is PDF, and I don't think anyone will view those on low-resolution devices.

    I've been in the Apple ecosystem for a while, but a 72 dpi screen is suitably low resolution even when viewing a PDF.

    I'm surprised that anyone actually wants to use bitmap fonts in 2024

    Well we're talking about system fonts here. But, yes, some folks still prefer bitmap fonts as they're inherently less blurry.

    https://lobste.rs/s/reqhua/why_not_just_use_bitmap_fonts

  14. Enivex commented on Aug 26, 2024

    @Enivex
    Collaborator

    Well we're talking about system fonts here. But, yes, some folks still prefer bitmap fonts as they're inherently less blurry.

    Only in very specific situations that aren't particularly relevant for typst

  15. inferiorhumanorgans commented on Aug 26, 2024

    @inferiorhumanorgans
    Contributor

    Only in very specific situations that aren't particularly relevant for typst

    Right, perhaps more relevant to typst is not crashing on system fonts (e.g. Cambria Math, SimSun) that use bitmap glyphs in some capacity. The way the current code works raster glyphs are preferred even if they're not in a supported format.

  16. inferiorhumanorgans commented on Aug 27, 2024

    @inferiorhumanorgans
    Contributor

    I just got my hands on SimSun. It's got the same problem as Cambria Math, the bitmap glyphs are being preferred over the included outline glyphs and typst panics because the bitmap glyphs are not PNGs and thus unsupported.

    For future questions about how specific fonts are getting rendered I've created a small tool to give some insight into arbitrary fonts on my sourcehut account: https://git.sr.ht/~az1/glyph-inspect

    Screenshot 2024-08-27 at 15 19 29

    A (much) longer explanation follows.

    Background

    Behavior

    Currently typst prefers bitmap glyphs unconditionally, falling back on color outline glyphs and monochrome outline glyphs in that order.

    Problem

    This behavior works for e.g. Apple Color Emoji because the only color glyphs it has are bitmaps.

    This is problematic because:

    • a bitmap glyph will be used even if it has to be scaled
    • a bitmap glyph will be used even if a more suitable outline glyph is available
    • the fixed strike size means that under most cases the bitmaps will need to be scaled even if there is a more optimal strike available

    Fonts

    Font mono outlines mono bitmaps color outlines color bitmaps
    Apple Color Emoji glyf — — sbix 72 dpi @ 20, 26, 32, 40, 48, 52, 64, 96, and 160 px
    Cambria Math glyf ebdt @ 12, 13, 15, 16, 17, and 19 px — —
    Noto Color Emoji — — colr, svg —
    Segoe UI Emoji glyf — colr —
    SimSun glyf ebdt @ 12, 13, 14, 15, 16, and 17 px — —

    Proposal

    If a color glyph exists (colr, svg, sbix, cbdt), use it preferring color outlines over color bitmaps. If a mono bitmap exists at the desired strike size use it, otherwise use the mono outline glyph.

    This logic should result in hi-DPI output being rendered with outlines and low-DPI output being
    rendered with bitmaps only if they can be used without scaling. This would avoid having to come up
    with a heuristic to detect emojis specifically and avoid having to check the output format e.g.
    PDF or PNG.

    In Practice

    The largest strike size in Cambria Math is 19 pixels, which at 300 dpi would be ~4.5 points, but for a 72 dpi PNG would be 19 points. For most use cases the outline would be selected.

    Font color outline color bitmap mono outline mono bitmap current behavior proposed
    Apple Color Emoji N Y Y N color bitmap color bitmap
    Noto Color Emoji Y N N N color outline color outline
    Segoe UI Emoji Y N Y N color outline color outline
    Cambria Math 19pt @ 72dpi N N Y Y mono bitmap mono bitmap
    Cambria Math 19pt @ 300 dpi N N Y Y mono bitmap mono outline
    SimSun 19pt @ 72dpi N N Y Y mono bitmap mono outline
    SimSun 19pt @ 300 dpi N N Y Y mono bitmap mono outline
  17. LaurenzV commented on Aug 28, 2024

    @LaurenzV
    Collaborator

    If a mono bitmap exists at the desired strike size use it, otherwise use the mono outline glyph.

    How do you determine the desired strike size, though? For PNG that works, because you know the resolution of the output image, but what about PDF? A PDF doesn't have an inherent size, because the user can zoom manually inside of the PDF viewer. If the user zooms to something like 300%, then a bitmap is probably always going to look bad, while if they go to 30%, then maybe it looks better than the outline glyph. But you can't determine this while creating the PDF.

  18. removed
    bug
    crash
    on Oct 8, 2024
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

    feature requesttext

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions