Skip to content

Only scan for color glyphs in is_color_glyph. - #4929

Closed
inferiorhumanorgans wants to merge 2 commits into
typst:mainfrom
inferiorhumanorgans:ignore-mono-bitmaps
Closed

inferiorhumanorgans wants to merge 2 commits into
typst:mainfrom
inferiorhumanorgans:ignore-mono-bitmaps

Conversation

@inferiorhumanorgans

Copy link
Copy Markdown
Contributor

This will prevent typst from picking up (unsupported) monochrome bitmaps in Windows system fonts e.g. Cambria Math and SimSun and the resulting panic (see #4335). One path leading to panic is eliminated, however:

  • unwrap is supplemented with an explicit panic that provides some debugging info. Longer-term it would be nice to have access to logging context from draw_raster_glyph and to bubble up some error handling.
  • unsupported color glyphs (e.g. JPEG and TIFF) will still cause a panic in PNG generation

@laurmaedje laurmaedje left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do they need to be unsupported though? Can't we handle monochrome bitmap fonts just like other color fonts?

Comment thread crates/typst/src/text/font/color.rs Outdated
Comment on lines +26 to +28
if sbix.best_strike(pixels_per_em).is_some() {
return true;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we also need to check for the glyph ID somewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

@inferiorhumanorgans

inferiorhumanorgans commented Sep 26, 2024 •

Copy link
Copy Markdown
Contributor Author

Why do they need to be unsupported though? Can't we handle monochrome bitmap fonts just like other color fonts?

It doesn't need to be unsupported but the fonts that've been reported to have monochrome bitmaps also have outline glyphs. What I took away from #4335 is that outline glyphs are preferred unless we're dealing with emoji (color) fonts that have color bitmaps and monochrome outlines.

I have code to parse the monochrome bitmaps but it's not clear what logic should apply when choosing between monochrome outline and monochrome bitmap. As-is this unbreaks PNG rendering for the aforementioned fonts and preserves the preference for color bitmaps.

@laurmaedje

Copy link
Copy Markdown
Member

I see. So we have the problem that the priority chain is color bitmap -> outline glyph -> mono bitmap?

@inferiorhumanorgans

Copy link
Copy Markdown
Contributor Author

No, the current order places mono bitmaps at the same priority as color bitmaps because glyph_raster_image returns Some for both mono and color bitmaps. The immediate problem is that most mono bitmaps are currently unsupported and typst panics as a result. The discussion at #4335 lead me to believe there's little-to-no interest in preferring mono bitmaps over mono outlines in any situation.

Specifically the order is:

  • sbix color bitmap (panic if JPEG or TIFF)
  • bdat mono bitmap (panic?)
  • ebdt mono bitmap (panic)
  • cbdt color bitmap (panic if not PNG)
  • colr color outline
  • SVG color outline
  • gvar mono outline
  • glyf mono outline
  • CFF mono outline
  • CFF2 mono outline

My proposal is:

  • sbix color bitmap (panic if JPEG or TIFF)
  • bdat mono bitmap (panic?)
  • ebdt mono bitmap (panic)
  • cbdt color bitmap (panic if not PNG)
  • colr color outline
  • SVG color outline
  • gvar mono outline
  • glyf mono outline
  • CFF mono outline
  • CFF2 mono outline

The fonts so far that have been reported to trigger the mono bitmap code path also have mono outlines for those glyphs. This change will allow typst to use/prefer those mono outlines without interfering with e.g. Apple's emoji font (color bitmaps + mono outlines) or Noto Sans (color outlines only).

// crates/typst-render/src/text.rs for PNG rendering
pub fn render_text(canvas: &mut sk::Pixmap, state: State, text: &TextItem) {
  // This should return false for mono bitmaps that we don't support
  if is_color_glyph(&text.font, glyph) {
    let glyph_frame = frame_for_glyph(&text.font, glyph.id);
      crate::render_frame(canvas, state, &glyph_frame);
    } else {
      render_outline_glyph(canvas, state, text, id);
    }
}

@laurmaedje

Copy link
Copy Markdown
Member

No, the current order places mono bitmaps at the same priority as color bitmaps because glyph_raster_image returns Some for both mono and color bitmaps.

What I meant is that the desired order is color bitmap -> outline glyph -> mono bitmap.

I think the changes you propose make sense in principle, but while at it, I'd like to eliminate all possible panics. Also, unless I'm mistaken, we can skip the low-level table lookups by simply checking for PNG format in the output of glyph_raster_image. Only color tables will generate PNGs.

I thought for the past weeks that the panic is very rare and this rather low priority, but it appears that 0.12 testing crashes on some emoji fonts in the web app, so it's more urgent than I thought. For this reason, I've implemented the changes I mention above myself: #5159. Feel free to take a look and give your input there.

@laurmaedje

Copy link
Copy Markdown
Member

Closing in favor of #5159

@laurmaedje laurmaedje closed this Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants