photopea / Typr.js

Typr.js - process fonts in Javascript
MIT License
914 stars 73 forks source link

[WIP] Reverse font encoder #39

Open lunakv opened 3 years ago

lunakv commented 3 years ago

This PR introduces a new class called Untypr that serves as the reverse to Typr; given a parsed JS font object, it serializes it back into an OTF font file.

The API consists of a single method: Untypr.encode(). It takes a font object as a parameter and returns an ArrayBuffer containing the resulting file. The font parameter is assumed to be in a format equivalent to the result of Typr.parse().

Because of this, the output is limited by which tables Typr supports. In addition to tables required by the OTF specification, Untypr is only able to include glyf, loca, kern, and SVG tables. Notably, it currently doesn't support the CFF table, neither for generation nor for reading. A font with data stored in the CFF table therefore can't be properly encoded by Untypr, because Untypr ignores the contents of that table.

There are a few other idiosyncrasies related to the format produced by Typr. For example, some fields are ignored and Untypr has to guess their value, and some data can become mangled in a way that cannot be recovered. These format-specific quirks and workarounds are annotated with a FIXME comment.