From 80bf6ed96b0969c526e8b81be2d020cebe0cd703 Mon Sep 17 00:00:00 2001 From: Mikael Tollefsen Date: Mon, 19 Oct 2026 14:00:16 +0200 Subject: [PATCH] Screen hinting: tricky fonts run their instructions with the Mono target FreeType's "tricky" fonts (old CJK fonts that assemble their glyphs with their instructions) were hinted with the light smooth target, whose backward compatibility forbids the instructions' sideways moves, so their glyphs could come apart. skrifa requires the interpreter with Target::Mono for them (OutlineGlyphCollection::require_interpreter), as the unhinted path already does; the screen hinter now does the same. Test: a bundled font renamed into FreeType's list ("cpop") is hinted with the Mono target and drawn as it hints; any other font keeps the light target. --- crates/fonts/src/fontdb.rs | 18 ++++++++++------ crates/fonts/src/tests_sysfonts.rs | 33 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/crates/fonts/src/fontdb.rs b/crates/fonts/src/fontdb.rs index 72f48bdb91..7e35cbee08 100644 --- a/crates/fonts/src/fontdb.rs +++ b/crates/fonts/src/fontdb.rs @@ -221,16 +221,22 @@ impl FontFace { /// A light hinting instance for `ppem` device pixels per em, with the engine FreeType's light /// hinting picks: the autohinter for TrueType outlines (their own instructions are ignored; /// measured against Firefox on Linux, the autohinter matches its rows of pixels and the - /// TrueType instructions don't), the font's own hints for PostScript (CFF) outlines and for - /// fonts whose glyphs need their instructions to be assembled at all. + /// TrueType instructions don't), the font's own hints for PostScript (CFF) outlines, and for + /// fonts whose glyphs need their instructions to be assembled at all their instructions with + /// the monochrome target, as skrifa requires for them. fn screen_hinter(&self, ppem: f32) -> Option { let f = self.skrifa()?; let outlines = f.outline_glyphs(); - let target = Target::Smooth { mode: SmoothMode::Light, symmetric_rendering: true, preserve_linear_metrics: true }; - let engine = if outlines.format() == Some(OutlineGlyphFormat::Glyf) && !outlines.require_interpreter() { - Engine::Auto(Some(self.glyph_styles.get_or_init(|| GlyphStyles::new(&outlines)).clone())) + let light = Target::Smooth { mode: SmoothMode::Light, symmetric_rendering: true, preserve_linear_metrics: true }; + let (engine, target) = if outlines.require_interpreter() { + // FreeType's "tricky" fonts (old CJK fonts that assemble glyphs with their + // instructions): skrifa asks for the interpreter with the monochrome target, under + // which the instructions may move points sideways, or glyphs come apart. + (Engine::Interpreter, Target::Mono) + } else if outlines.format() == Some(OutlineGlyphFormat::Glyf) { + (Engine::Auto(Some(self.glyph_styles.get_or_init(|| GlyphStyles::new(&outlines)).clone())), light) } else { - Engine::Interpreter + (Engine::Interpreter, light) }; HintingInstance::new(&outlines, Size::new(ppem), self.location(), HintingOptions { engine, target }) .map_err(|e| log::warn!("Cannot hint {} at {ppem} px: {e}", self.family)) diff --git a/crates/fonts/src/tests_sysfonts.rs b/crates/fonts/src/tests_sysfonts.rs index cfd5f4421a..b282a1461c 100644 --- a/crates/fonts/src/tests_sysfonts.rs +++ b/crates/fonts/src/tests_sysfonts.rs @@ -335,6 +335,39 @@ fn fallback_reads_each_installed_font_file_once_however_many_characters_miss() { assert_eq!(index.reads, 2, "each file read once"); } +#[test] +fn screen_hinting_runs_a_tricky_fonts_instructions_with_the_monochrome_target() { + // FreeType's "tricky" fonts (old CJK fonts listed by name, such as DynaLab's "cpop") assemble + // their glyphs with their instructions; skrifa requires the interpreter with `Target::Mono` + // for them. A bundled font renamed into the list stands in for one. + let dir = font_dir("tricky"); + std::fs::write(dir.join("Tricky.ttf"), renamed_to("SourceSans3-Regular.ttf", "Sysfont cpop1")).unwrap(); + let db = FontDb::with_font_dirs(vec![dir]); + let face = db.face("Sysfont cpop1", "Regular"); + assert_eq!(face.family, "Sysfont cpop1"); + let font = face.skrifa().unwrap(); + let outlines = font.outline_glyphs(); + assert!(outlines.require_interpreter(), "the name makes it tricky"); + let ppem = 11.0 * 96.0 / 72.0 * 1.45; + let drawn = |target: Target, gid: u32| { + let h = HintingInstance::new(&outlines, Size::new(ppem), face.location(), HintingOptions { engine: Engine::Interpreter, target }).unwrap(); + let mut pen = FlipPen(BezPath::new()); + outlines.get(GlyphId::new(gid)).unwrap().draw(DrawSettings::hinted(&h, false), &mut pen).unwrap(); + pen.0 + }; + // The target decides whether the instructions may move points sideways (the light target's + // backward compatibility forbids it): this font's instructions only move points vertically, + // so the target itself is checked, and the outlines drawn with it. + assert!(matches!(face.screen_hinter(ppem).unwrap().target(), Target::Mono)); + for c in "Every Thursday".chars().filter(|c| !c.is_whitespace()) { + let gid = face.glyph_for(c); + assert_eq!(*db.hinted_outline(&face, gid, ppem).unwrap(), drawn(Target::Mono, gid), "{c}: the monochrome target"); + } + // Any other font keeps the light target. + let plain = db.face(FALLBACK_FAMILY, "Regular"); + assert!(matches!(plain.screen_hinter(ppem).unwrap().target(), Target::Smooth { mode: SmoothMode::Light, preserve_linear_metrics: true, .. })); +} + #[test] fn coverage_ranges_hold_exactly_the_mapped_characters() { let data = renamed("SourceSans3-Regular.ttf");