-
Notifications
You must be signed in to change notification settings - Fork 311
FreeType Versus STBTT
bwip-js version 1.0 used the FreeType font engine, using emscripten to cross-compile to JavaScript. FreeType is an amazing piece of software, with built in auto-hinting and tons of special font rendering strategies.
bwip-js version 2.0 uses stb_truetype.h, compiled using the cheerp cross-compiler. STBTT is equally amazing software. It is a single, ~4500 line file (a large majority being comments), yet it produces excellent letter forms, even at small sizes.
Here is a comparison at scale=2
, which corresponds to a 20px/15pt effective font size:
The STBTT letter forms are slightly heavier stroked than FreeType's and a bit fuzzier, but both are highly legible. Considering the complexity of font rendering, that is a pretty respectable result for STBTT. Now lets look at compiled code size:
$ ls -l freetype.js fontlib.js
-rw-rw-r-x 1 bwip-js users 1394208 Sep 19 2016 freetype.js
-rw-rw-r-- 1 bwip-js users 103734 Oct 16 14:51 fontlib.js
The above js-files both contain the embedded OCR fonts (about 54K bytes), plus the respective font rendering libraries.
Run time memory usage is drastically different as well. Emscripten pre-allocates a 16MB block of memory (as an ArrayBuffer
) that caused issues with using bwip-js on embedded servers, while cheerp uses a traditional allocate-as-you-go memory model that is considerably easier on resources.