Skip to content

Conversation

@oetiker
Copy link
Contributor

@oetiker oetiker commented Jan 9, 2026

Implement optional font hinting using Google's skrifa library for better
text rendering at small sizes. Hinting grid-fits glyph outlines to pixel
boundaries, improving legibility.

Features:

  • New hinting cargo feature flag (requires text feature)
  • When compiled with hinting, enabled by default (matching browser behavior)
  • CSS text-rendering property controls per-element hinting:
    • optimizeLegibility/optimizeSpeed: hinting enabled
    • geometricPrecision: hinting disabled (preserve exact outlines)
  • Works with variable fonts
  • Hinting options propagated to nested/embedded SVGs

Implementation:

  • SkrifaPen adapter converts skrifa OutlinePen to tiny_skia_path
  • Hinted outlines drawn at ppem scale, then scaled back to font units
    to work with existing transform pipeline
  • ppem calculation: font_size * dpi / 72.0

Tests:

  • 7 new integration tests demonstrating hinting effects
  • Example script for generating comparison images
  • Demo SVG showing hinted vs unhinted text at various sizes

@LaurenzV
Copy link
Collaborator

LaurenzV commented Jan 9, 2026

I appreciate the effort, but unfortunately I don't think we can pull in skrifa as a dependency right now, otherwise we have ttf-parser AND skrifa in the dependency tree. We have plans for replacing ttf-parser, but it requires quite a few changes and I don't know yet when that will happen.

@nicoburns
Copy link
Collaborator

The transition to Skrifa/Fontations is implemented in #922 (except for fontdb). I suggest we merge that after the next release at which point we could look at this PR again.

@RazrFalcon
Copy link
Collaborator

I agree. #922 should be merged first.

Supersedes linebender#922 (nicoburns' "Port resvg to harfrust and fontations")

This patch builds upon the excellent groundwork laid out in PR linebender#922,
which identified harfrust and skrifa as the path forward for long-term
maintainability. As noted in that discussion, these actively-maintained
libraries offer significant advantages: harfrust passes substantially
more shaping tests than rustybuzz, and skrifa provides a modern,
well-designed API for font parsing.

Core migration (shared with linebender#922):
- Replace rustybuzz with harfrust for OpenType text shaping
- Replace ttf-parser with skrifa for font parsing and metrics
- Update font metrics extraction to use skrifa's MetadataProvider

Where this implementation extends linebender#922:

1. Font Hinting Support
   - Add optional `hinting` feature and HintingOptions in parser options
   - Implement hinted outline extraction via skrifa's HintingInstance
   - Honor CSS text-rendering property: geometricPrecision disables
     hinting, optimizeLegibility/optimizeSpeed enable full hinting
   - Configurable DPI for ppem calculation

2. Full Variable Font Support
   - Parse font-variation-settings CSS property
   - Parse font-optical-sizing CSS property (auto/none)
   - Auto-map standard properties to variation axes:
     * font-weight → wght axis
     * font-stretch → wdth axis
     * font-style: italic → ital axis
     * font-style: oblique → slnt axis
   - Auto-set opsz axis based on font size when font-optical-sizing: auto
   - Store per-glyph variation context in PositionedGlyph for correct
     outline extraction with variations applied

3. Modular Skrifa Integration
   - New skrifa_metrics.rs: Font metrics via skrifa's MetadataProvider
   - New skrifa_colr.rs: COLR rendering via skrifa's ColorPainter trait
   - Delete legacy colr.rs (ttf-parser based implementation)

4. Enhanced COLR Support
   - Implement skrifa's ColorPainter trait for SVG output
   - Support COLRv1 features: transforms, clips, composite modes
   - Handle linear, radial gradients with proper spread methods

5. Test Infrastructure
   - Add tests-hinted/ directory with hinted reference images
   - Add font-variation-settings test suite
   - Include RobotoFlex variable font for variation testing

We appreciate the careful analysis in linebender#922 regarding the ecosystem
trade-offs, particularly around compile times and binary size. This
implementation accepts those trade-offs in exchange for the additional
capabilities (hinting, variable fonts) that skrifa enables.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@oetiker
Copy link
Contributor Author

oetiker commented Jan 10, 2026

closing and opening fresh

@oetiker oetiker closed this Jan 10, 2026
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.

4 participants