simoncozens.github.io

Simon Cozens technical blog

WASM Shaping

Many of my fonts have bumped up against the limits of font technology. Nastaliq Urdu is the classic example:

There are two extra actions that a Nastaliq font needs to do in addition to the standard shaping of character forms and basic diacritic/nuqta attachment. The first is collision avoidance. Due to the sloping of the syllable, the horizontal space for a character is compressed and this greatly increases the likelihood that nuqtas will collide either with other nuqtas or with other base characters. The second action is word kerning. This moves words (or more strictly, clusters) closer together so that one word may finish underneath the start of another word. Comparison of OpenType and Graphite shaping speeds in a Nastaliq context

But other scripts with very complex layout requirements include SignWriting and Egyptian (and now Mayan) hieroglyphs. These are practically impossible to implement in OpenType; OpenType layout rules require enumerating all the possible combinations, and they aren’t able to reason about the best layout given a set of glyphs.

So a couple of years back, Behdad Esfabod and I started playing around with an idea: if we could embed a small computer program inside a font, then it could reason about the glyphs and determine the best layout dynamically. The idea is that most fonts would continue as they are, but that this technology would be available for the small minority of cases which need it - a kind of “escape hatch” away from the OpenType Layout model. Font-specific shaping engines could be written in WASM, a binary program format designed for running in web browsers and other environments. WASM is safe - your browser runs arbitrary code in WASM already - and it’s portable between different platforms; a “WASM host” runs the program inside a well-established sandbox, so it’s an ideal format for little embedded programs. Behdad came up with the idea and wrote the initial implementation, and I produced a bunch of motivating examples to show what the technology could do. We demoed it at ATypI in Paris, where it was quickly forgotten.

I think one of the reasons why it didn’t catch on is that, well, very few people actually really need it. There are only a handful of people working on Nastaliq fonts, and they can sort of get by with OpenType - not well, not up to printed metal standards, but sort of get by. And there’s literally one person working on Egyptian hieroglyph layout. Additionally, it requires writing your own shaping engine, which is something I guess very few of the people who do actually need it would want to learn how to do.

But what about if there were a bunch of script-specific programs available that get fed from data embedded in a font table, and you could take one of these script-specific shapers off the shelf and add them to your font, instead of writing one from scratch. This would actually be super useful because then designers working in things like Indic scripts wouldn’t have to write any OTL rules themselves - they’d just declare how their font works, give some parameters, and the WASM shaper would handle the complex layout for them. That would save a lot of people a lot of work.

But we don’t have those off-the-shelf shapers yet. So we tried to come up with some examples of how you could use the technology for creative purposes as well as complex layout. That’s hard, though - it’s tricky to anticipate what creative people can do with a new technology. I think there’s a lot more people could play with in this space.

But another reason is it was still pretty hard to explain to people, because playing with this new technology required a custom build of the Harfbuzz shaping engine, plus a custom build of whatever application you want to view your font in. That’s quite a lot of investment to see a cool demo. As I said already, WASM does run in your browser - but the shaping engine part of browser doesn’t (and can’t) talk to the WASM host part of your browser. And although it’s possible to port Harfbuzz to WASM, there are complex technical reasons why we couldn’t put a WASM host inside Harfbuzz and compile that to WASM.

But! Last year, the Harfbuzz shaping engine got ported to Rust as Harfrust, and last month, I added support for WASM-based shaping. And Rust, as a programming language, is very good at being able to be compiled to WASM. In fact, the WASM host that I used is the Harfrust port is “self-hosting”, which means it can run inside a WASM binary. Which means we can now put a WASM shaper inside a WASM Harfrust, inside your browser. WASM inception!

This gave me the motivation to a little bit more work on the Nastaliq shaping engine I wrote for the Paris examples. It’s still not perfect, but it’s better; and it’s better than what we have already. In fact, it’s pretty good. Here is some of the best hand-set Urdu metal typography I know:

(This is from “Selected Ghazals and Other Poems” by Mir Taqi Mir, in the Murty Classical Library of India.)

And here’s what we can do now - compared to what you can do in OpenType. Note particular the improved kerning (tighter cluster in صانع and اوپر), and bari ye dot justification, but particularly the even spacing. And now that things are being positioned by logic rather than exhaustive rules, we can change things and it’ll just work - for example, we can finally have nice chonky dots! And I think I can improve it even more - now that I can tell the font how to lay out the glyphs, not just what to lay out.

You can edit the text in the “Current Noto Nastaliq” box below, and it will be shaped in the WASM engine as well; you can also turn off the WASM shaping engine and see the (broken) base font, to satisfy yourself that the shaping is indeed happening in the WASM engine.

Current Noto Nastaliq OpenType (browser shaping)

اب ایسے ہیں کہ صانع کے مزاج اوپر بہم پہنچے

Noto Nastaliq WASM (WebAssembly shaping)

Written on November 2, 2026