-
Notifications
You must be signed in to change notification settings - Fork 324
FreeType Replacement
Since version 1.0, bwip-js has used an emscripten-compiled version of the FreeType font library for rendering fonts. FreeType provided a fully scalable, PostScript-like font interface but came with several unforeseen issues. It turns out that emscripten-compiled modules have problems with compatibility in a lot of popular JavaScript frameworks, such as React, that implement a browser-side require()
interface. And the memory requirements (a pre-allocated 16MB buffer) made it too heavy weight for use with embedded servers.
But the final straw became the difficulty in cross-compiling it. The emscripten project only tests against an ancient version of FreeType (version 2.5.5, circa 2014). It is extremely difficult (I never succeeded) to get a recent release of FreeType to compile with a recent release of emscripten. The emscripten issues pages and other online resources are full of questions regarding how to compile FreeType, but with no solutions.
So with the release of bwip-js 2.0, the project is moving to a new font manager. It is a port of the code available at stb_truetype.h. The C code was converted to JavaScript by first identifying the API surface needed by bwip-js, then using some custom C parsing tools to perform tree-shaking on the code. The output of the tree-shaking was a list of functions in stb_truetype.h that needed to be converted to JavaScript. From there, it was a solid day's work to manually convert the C code to JavaScript. All in all, surprising easy to do.
The size of stb_truetype.js is amazing - 57KiB (15KiB minified). Compare that to freetype.js at 1.2MiB minified. Of course, there is no comparison between the functionality available in freetype.js verses stb_truetype.js. FreeType is a fully functional font renderer that can handle the most esoteric of fonts. It is the Rolls Royce of font rendering. stb_truetype is more like a low-end Kia. It can reliably handle 7-bit ASCII and most non-composite glyphs but there are numerous fonts it cannot fully parse and glyphs it cannot render correctly.
The fonts that come with bwip-js include ASCII-only versions of the OCR-A and OCR-B fonts. These fonts have been fully vetted against stb_truetype and have been tuned to produce near identical character metrics as with FreeType.