Skip to content

Migrate from rustybuzz and ttf-parser to harfrust and read-fonts - #8172

Draft
mkorje wants to merge 11 commits into
typst:mainfrom
mkorje:harfrust
Draft

mkorje wants to merge 11 commits into
typst:mainfrom
mkorje:harfrust

Conversation

@mkorje

@mkorje mkorje commented Apr 26, 2026 •

Copy link
Copy Markdown
Collaborator

An attempt at doing this migration. Other PRs: typst/pixglyph#4.

The first (and only, atm) commit does the smallest change possible needed to replace rustybuzz with harfrust. The second commit will try to replace as much of ttf-parser with read-fonts and/or skrifa (tbd).

Note that we won't be able to get rid of ttf-parser completely just yet, since read-fonts lacks support for the MATH table (see googlefonts/fontations#1269).

We may also want to wait for resvg to be ported (see linebender/resvg#922).

Performance/binary size analysis needs to be done.

Similar in spirit to these changes, we might want to investigate migrating to fontique from fontdb (see the discussion in https://xi.zulipchat.com/#narrow/channel/465085-resvg/topic/Moving.20resvg.20to.20fontations.2Fharfrust/with/572344735).

@Enivex

Enivex commented Apr 26, 2026

Copy link
Copy Markdown
Collaborator

The resvg PR has been open for nearly a year, so I wouldn't hold my breath

@laurmaedje

Copy link
Copy Markdown
Member

The resvg PR has been open for nearly a year, so I wouldn't hold my breath

We aren't. We're just switching anyway.

@mkorje
mkorje force-pushed the harfrust branch 2 times, most recently from 93e5ec1 to 84b2bd8 Compare April 27, 2026 12:39
@isuffix isuffix added the dependencies Pull requests that update a dependency file label May 17, 2026
@Enivex

Enivex commented May 26, 2026 •

Copy link
Copy Markdown
Collaborator

@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Jun 4, 2026
@Enivex

Enivex commented Jun 5, 2026

Copy link
Copy Markdown
Collaborator

Looks like there's finally movement in linebender/resvg#922

@Enivex

Enivex commented Jul 29, 2026 •

Copy link
Copy Markdown
Collaborator

linebender/resvg#922 has now been merged

Edit: The 0.48 release includes this change

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels Aug 26, 2026
@dfrg

dfrg commented Sep 5, 2026

Copy link
Copy Markdown

Note that we won't be able to get rid of ttf-parser completely just yet, since read-fonts lacks support for the MATH table (see googlefonts/fontations#1269).

Finally coming in googlefonts/fontations#2105

@mkorje
mkorje force-pushed the harfrust branch 4 times, most recently from 53a3e84 to bd7670d Compare September 24, 2026 00:40

@mkorje mkorje left a comment •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've finished the migration and gotten this PR in a working state. Still a number of things to do though, and I've left some comments.

Note that fontations is currently undergoing an API change (pointed out to me in googlefonts/fontations#2156 (comment)): googlefonts/fontations#1852 (comment). A number of things are moving from skrifa into read-fonts, and this work should be finished in a few weeks time. This PR will probably change a fair bit then once that work is done, so probably best to hold off on a review until then. (I will start to get the code working using this new API, which is under a feature flag, on another branch.)

A bunch of tests are failing. Most are svg tests, due to glyph ids changing. A more important change though is that skrifa doesn't always return None when outlining empty glyphs (e.g. spaces). So we have a bunch of <symbol><path d="M 0 0"/></symbol> showing up now. (I'd imagine this change is wrong, though.)
There are also some render tests and one pdf test failing, which I need to look into.

}
}

struct ColrBuilder<'a>(&'a mut String);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Everything from this point onward was taken from resvg (with minor modifications): linebender/resvg#922. Need to properly attribute it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've changed it up slightly to make use of some new stuff in read-fonts, but is otherwise the same "structure" as the code from resvg.

Comment thread crates/typst-svg/src/paint.rs Resolved
Comment on lines 577 to 592
// TODO: Normalizing the stops into the 0..1 range moves the circles onto the
// first and last stop, which can make `r0` (and in theory `r1`) negative.
// SVG cannot express that, so the color line should be cut where the radius
// reaches zero, with an interpolated stop inserted at the cut and the
// remaining stops reparameterized into the 0..1 range.
self.svg.start_element("radialGradient");
self.svg.write_attribute("id", &gradient_id);
self.svg.write_attribute("cx", &gradient.x1);
self.svg.write_attribute("cy", &gradient.y1);
self.svg.write_attribute("r", &gradient.r1);
self.svg.write_attribute("fr", &gradient.r0);
self.svg.write_attribute("fx", &gradient.x0);
self.svg.write_attribute("fy", &gradient.y0);
self.svg.write_attribute("cx", &c1.x);
self.svg.write_attribute("cy", &c1.y);
self.svg.write_attribute("r", &r1);
self.svg.write_attribute("fr", &r0);
self.svg.write_attribute("fx", &c0.x);
self.svg.write_attribute("fy", &c0.y);
self.svg.write_attribute("gradientUnits", &"userSpaceOnUse");
self.write_spread_method_attribute(gradient.extend);
self.write_spread_method_attribute(extend);
self.write_transform_attribute("gradientTransform", gradient_transform);

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

gpt-6 astra flagged this on review with the following comment. I have no idea about this part of the code (I took it from resvg after all).

Skrifa can supply negative radii when normalizing valid COLR color stops. I reproduced r0 = -256, which this code writes as fr="-256". Usvg clamps that to zero without adjusting the stops, changing the gradient’s colors and geometry. The color line needs truncation and reparameterization before SVG serialization.

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.

I believe the code was originally written by @LaurenzV, so perhaps he can make sense of it?

@laurmaedje laurmaedje added blocked This PR is blocked by something. and removed waiting-on-author Pull request waits on author labels Sep 28, 2026
@mkorje
mkorje force-pushed the harfrust branch 2 times, most recently from 6850b1b to d1e9977 Compare October 25, 2026 05:46
@mkorje mkorje mentioned this pull request Oct 25, 2026
@mkorje mkorje changed the title Migrate from rustybuzz and ttf-parser to harfrust and fontations Migrate from rustybuzz and ttf-parser to harfrust and read-fonts` Oct 25, 2026
@mkorje mkorje changed the title Migrate from rustybuzz and ttf-parser to harfrust and read-fonts` Migrate from rustybuzz and ttf-parser to harfrust and read-fonts Oct 25, 2026
@mkorje
mkorje force-pushed the harfrust branch 3 times, most recently from ecabea6 to 52070c6 Compare October 28, 2026 05:45
@Enivex

Enivex commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

What is it that's actually blocking this?

@laurmaedje

Copy link
Copy Markdown
Member

What is it that's actually blocking this?

It was blocked by this from @mkorje's earlier comment:

Note that fontations is currently undergoing an API change (pointed out to me in googlefonts/fontations#2156 (comment)): googlefonts/fontations#1852 (comment). A number of things are moving from skrifa into read-fonts, and this work should be finished in a few weeks time. This PR will probably change a fair bit then once that work is done, so probably best to hold off on a review until then.

But this has been resolved since.

@laurmaedje laurmaedje removed the blocked This PR is blocked by something. label Nov 4, 2026

@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.

Looks pretty good!

A few things aside from the in-code comments:

  • The text-colr-svg-glyphs-different-size test regressed. Seems to be something with COLRv1 painting.

  • So we have a bunch of <symbol><path d="M 0 0"/></symbol> showing up now.

    Maybe let's just filter these out?

  • We may also want to wait for resvg to be ported

    That has happened since. We'd first need to bump it in krilla though. Currently, we have many versions of the fontations crates in the tree. I suspect that could be streamlined with some changes in upstream crates (mostly krilla and hayro, but resvg also seems to still be on 0.12 of harfrust...). Since the dependency tree is already a mess, we don't necessarily have to block the PR on this, but we could, depending on preference.

  • Glyph bounds now also respect color glyphs. With top and bottom edge "bounds", Noto Color Emoji 😀 goes from 0pt to 11.69pt tall. Yay! This fixes #8074. We should add a test that covers this.

Also, here are some behavior changes Claude found, which are fine, but good to know about:
  • The width class 0 or above 9 now map to UltraCondensed / UltraExpanded instead of Normal.

  • The oblique bit is no longer gated on OS/2 version >= 4.

  • Fonts without an OS/2 table now take bold/italic from the head table's macStyle field. Courier Bold is now correctly 700 instead of 400, and Courier Oblique becomes Italic.

  • Fonts with a units-per-em outside 16-16384 now load (with 1000) instead of being rejected.

  • Glyph bounds now use the metadata from the glyphs' header instead of outlining it when possible (can be wrong or lead to small changes).

  • Empty glyphs now return Some(zero) instead of None, so TextItem::bbox includes the origins of spaces.

  • Text widths change for Apple fonts that have a trak table because we set the point size. That seems like a good thing.

.unwrap_or_default()
let extents = |gid,
f: fn(
read_fonts::model::metrics::GlyphExtents<read_fonts::types::F48Dot16>,

@laurmaedje laurmaedje Nov 5, 2026 •

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.

The formatting is a bit much here. Perhaps we can have a use? You seem to avoid them for read_fonts in some cases (any particular reason like name clashes?), but it could also be a function-local use.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sure, rustfmt is being a bit weird here aha. There's no particular reason, other than I think before the uses weren't entirely consistent, so I just opted to avoid them.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I realise that I am now too using it inconsistently anyway, even within this file!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed regardless.

Comment thread crates/typst-layout/src/math/text.rs Resolved
Comment on lines +119 to +126
let mut coverage = Coverage::default();
if let Some(cmap) = ttf.tables().cmap {
for subtable in cmap.subtables {
if subtable.is_unicode() {
let mut subtable_coverage =
CoverageBuilder::with_capacity(coverage.0.len());
subtable.codepoints(|c| {
// We expect codepoints in CMAP subtables to be already sorted
subtable_coverage.add_codepoint(c);
});
coverage = coverage.union(&subtable_coverage.build());
}
}
}
let coverage =
Coverage::from_vec(font.charmap().unicodes().map(|(c, _)| c).collect());

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.

#8676 very carefully optimized this to use the cmap table structure to reduce sorting overhead, by building coverage per subtable and then unioning. It would be very unfortunate to lose this optimization.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Originally, I decided to just use font.charmap() for convenience. Then I found out that .charmap() is really only selecting a single subtable, and this is what the shaper actually uses (rustybuzz did this before, and harfrust does it too), so walking all the subtables to collect coverage is actually slightly incorrect.

So unioning isn't needed anymore. We can still reduce the sorting overhead though which I did in a commit, but with a fallback in case codepoints aren't sorted (I think this can come up, see https://docs.rs/read-fonts/latest/read_fonts/model/charmap/struct.Charmap.html, due to tables like DMAP for example).

Comment on lines 577 to 592
// TODO: Normalizing the stops into the 0..1 range moves the circles onto the
// first and last stop, which can make `r0` (and in theory `r1`) negative.
// SVG cannot express that, so the color line should be cut where the radius
// reaches zero, with an interpolated stop inserted at the cut and the
// remaining stops reparameterized into the 0..1 range.
self.svg.start_element("radialGradient");
self.svg.write_attribute("id", &gradient_id);
self.svg.write_attribute("cx", &gradient.x1);
self.svg.write_attribute("cy", &gradient.y1);
self.svg.write_attribute("r", &gradient.r1);
self.svg.write_attribute("fr", &gradient.r0);
self.svg.write_attribute("fx", &gradient.x0);
self.svg.write_attribute("fy", &gradient.y0);
self.svg.write_attribute("cx", &c1.x);
self.svg.write_attribute("cy", &c1.y);
self.svg.write_attribute("r", &r1);
self.svg.write_attribute("fr", &r0);
self.svg.write_attribute("fx", &c0.x);
self.svg.write_attribute("fy", &c0.y);
self.svg.write_attribute("gradientUnits", &"userSpaceOnUse");
self.write_spread_method_attribute(gradient.extend);
self.write_spread_method_attribute(extend);
self.write_transform_attribute("gradientTransform", gradient_transform);

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.

I believe the code was originally written by @LaurenzV, so perhaps he can make sense of it?

@laurmaedje laurmaedje added the waiting-on-author Pull request waits on author label Nov 5, 2026
@mkorje

mkorje commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator Author
  • Maybe let's just filter these out?

I've done that.

I will add this soon.

  • The text-colr-svg-glyphs-different-size test regressed. Seems to be something with COLRv1 painting.

And I will look into this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file waiting-on-author Pull request waits on author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants