The Web Assembly Shaper
github.com
github.com
> The WASM code inside a font is expected to export a function called shape which takes five int32 arguments and returns an int32 status value.
> ..The general goal of WASM shaping involves receiving and manipulating a buffer contents structure, which is an array of infos and positions (as defined below). Initially this buffer will represent an input string in Unicode codepoints. By the end of your shape function, it should represent a set of glyph IDs and their positions.
> Specifically, Harfbuzz is purely responsible for shaping; although Harfbuzz does have APIs for accessing glyph outlines, typically other libraries in the free software text rendering stack are responsible for text segmentation into runs, outline scaling and rasterizing, setting text on lines, and so on.
> Harfbuzz is therefore restricted to turning a buffer of codepoints for a segmented run of the same script, language, font, and variation settings, into glyphs and positioning them. This is also all that you can do with the WASM shaper; you can influence the process of mapping a string of characters into an array of glyphs, you can determine how those glyphs are positioned and their advance widths, but you cannot manipulate outlines, variations, line breaks, or affect text layout between texts of different font, variation, language, script or OpenType feature selection.
It already required execution of a virtual machine:
> TrueType systems include a virtual machine that executes programs inside the font, processing the "hints" of the glyphs, in TrueType called “instructions”. These distort the control points which define the outline, with the intention that the rasterizer produce fewer undesirable features on the glyph.
Now it just _also_ requires potential execution of arbitrary WASM code, too.
The proposed WASM shaping implementation would be a replacement for traditional OpenType layout[1] which is already turing complete[2]), and in this sense using WASM is significantly safer than the alternative. Your comment suggests that "execution of arbitrary WASM code" is something that we should be scared of, but the entire point of WASM is that it allows for safe/isolated compilation of arbitrary code.
[1] https://simoncozens.github.io/fonts-and-layout/features.html
[2] https://litherum.blogspot.com/2019/03/addition-font.html
> The proposed WASM shaping implementation would be a replacement for traditional OpenType layout
It's not a proposal AFAIK, it's already implemented today in the harfbuzz codebase.
I also do not think it is a 'replacement' for the traditional system, I doubt anyone will disable the ability to render the entire ecosystem of fonts out there which have not been updated to use WASM - so it seems likely it's just another point of complexity added to font rendering in general.
Not to dispute harfbuzz's significance -- it's a crucial component of much of the software we use every day -- but are you sure about Safari? I thought it relied on Apple's Core Text framework to handle font shaping.
OpenType shaping is currently defined in terms of a whole bunch of rewrite rules, with some script-specific knowledge (such as reordering vowels in Indic) added in by the shaping engine. The original idea is that it would be declarative and fairly easy for font designers to work with, but in practice it's pretty clunky, and doesn't scale well as complexity goes up. It's also slow to evaluate on modern computers because sequence matching is very branchy, and it's not unusual to require dozens of passes.
A particularly striking example is hieroglyphics. These compose multiple elements together into a block, with rules not unlike CSS grid. Sometimes there's vertical stacking, sometimes horizontal, occasionally more complex interactions like nestling inside an L. It is possible to encode this into OpenType rules, but it's very much a hack - at heart you need to do fairly simple geometry calculations to add up the total widths and divide them proportionally, but think about writing that as a regex and you'll get some idea how it comes out.
This proposal replaces the OpenType shaping rules with a call into WASM, where you can do these sorts of calculations straightforwardly and in a single pass. It is another Turing complete language (as is OpenType shaping, as proved by Behdad a few years ago, and TrueType hints), but there are excellent off-the-shelf implementations and it's well known how to run it securely sandboxed.
Even for Latin, this kind of thing would be useful for making a font that looks like real handwriting, for example. You can fake that in OpenType to a certain extent, but it goes beyond what the format was designed to handle.
This is a first cut, as it only affects positioning of premade glyphs. One thing I'd like to see going forward is adjusting variation parameters. As an example, a typical Devanagari (Hindi) font has 6 or so different widths of "ि" depending on the width of the consonant cluster it composes with (so रि or ल्कि). With variation connected to shaping, you could have one glyph of variable width, and the shaping engine could just set the variation to the right value, and with greater precision to boot.
If I were designing a new font format from scratch, this is definitely the way I'd do it. I'm excited to see where it goes.
A sentence that also applies to CSS. Declarative systems for design tend to fail.
I dream of the day when baroque CSS layout rules can also be replaced with tiny WASM programs for computing the exact layout your application needs.