You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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).
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.
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?
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.
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.
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.
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.
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.
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.
Activity
LaurenzV commentedon Jun 5, 2024
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.
Enivex commentedon Jun 5, 2024
Are we talking Windows without a CJK language pack? I can't imagine Chinese Windows uses a bitmap font.
Nerixyz commentedon Jun 5, 2024
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).fix: use Microsoft JhengHei as a fallback
LaurenzV commentedon Aug 25, 2024
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.
inferiorhumanorgans commentedon Aug 26, 2024
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 (
.ttfis scalable,.otbis 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 andcolr(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 theRasterGlyphImagestruct. 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/EBLCtable 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?a: https://download.cnet.com/download/powerpoint-viewer/3000-2075_4-75450958.html
b: https://int10h.org/oldschool-pc-fonts/fontlist/?1#ibm-bios
LaurenzV commentedon Aug 26, 2024
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.
inferiorhumanorgans commentedon Aug 26, 2024
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, andCOLRtables).OTOH SimSun and Cambria Math have both outline (
glyf) and bitmap (ebdt) glyphs.At small sizes on low-resolution devices the bitmap glyphs are intended to look better than the outlines.
LaurenzV commentedon Aug 26, 2024
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.
Enivex commentedon Aug 26, 2024
I'm surprised that anyone actually wants to use bitmap fonts in 2024
inferiorhumanorgans commentedon Aug 26, 2024
I've been in the Apple ecosystem for a while, but a 72 dpi screen is suitably low resolution even when viewing a PDF.
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
Enivex commentedon Aug 26, 2024
Only in very specific situations that aren't particularly relevant for typst
inferiorhumanorgans commentedon Aug 26, 2024
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.
inferiorhumanorgans commentedon Aug 27, 2024
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
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:
Fonts
glyfsbix72 dpi @ 20, 26, 32, 40, 48, 52, 64, 96, and 160 pxglyfebdt@ 12, 13, 15, 16, 17, and 19 pxcolr,svgglyfcolrglyfebdt@ 12, 13, 14, 15, 16, and 17 pxProposal
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.
LaurenzV commentedon Aug 28, 2024
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.