Skip to content

FreeType Replacement

metafloor edited this page Feb 11, 2018 · 4 revisions

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, which implement a browser-side require() interface. And the memory requirements (a pre-allocated 16MB buffer) make it too heavy weight for use with embedded servers.

So bwip-js is moving back to bitmapped fonts, which is what the project used before FreeType. But the original fonts were too manually intensive to maintain (the small resolution fonts had to be fixed-up in a graphics editor) and new fonts and new sizes could not be easily added. And they were monochrome only. The bitmapped fonts that roll out with bwip-js 1.5 are auto-generated with freetype.js, and a new tool, available in the develop branch of the project, simplifies adding new fonts and sizes.

The initial font distribution that comes with bwip-js will include:

  • OCR-A
  • OCR-B

Each font is rendered at the default point sizes used by BWIPP (8, 9, 10, and 12) for each of the integral scale factors 1x - 9x.

The Inconsolata font, which was used to demonstrate FreeType's font loading feature in previous versions, is still available but must be downloaded separately.

Node.js Usage

For Node.js users that simply require("bwip-js") and use the default font sizes, there should be no change in behavior. If you use custom font sizes, then you will likely see differences. If you were using FreeType to load a custom font, you definitely will be impacted by this change. Please raise an issue on the github project to force me to document how to add custom fonts. It is not hard to do. I am just curious if anyone is actually using that feature....

If you are using bwip-js with a customized or replacement node-bwipjs.js file, then you will need to make some changes. The font-manager interface has changed so that it is less freetype-specific. And the Bitmap interface has changed, now located in a separated module node-bitmap.js, replacing node-zlibPNG.js.

It may still be possible to use freetype.js with node. The hooks are there (see node-bwipjs.js) but not well tested.

Browser Usage

Using bwip-js in a browser has been simplified (hopefully) by this change. See the project's home page for more details.