0.5.0 • Published 1 year ago

fontconfig v0.5.0

Weekly downloads
2,041
License
MIT
Repository
github
Last release
1 year ago

fontconfigjs

This is essentially a stripped-down version of FontConfig compiled to WebAssembly with a Javascript API that works in nodejs and the browser.

The FontConfig source tree is mostly intact for ease of merging with future versions.

How?

const FontConfig = await require('fontconfig');

FontConfig.addFont('/Library/Fonts/Comic Sans MS.ttf');
FontConfig.addFont('/Library/Fonts/Futura.ttc');
FontConfig.addFont('/Library/Fonts/GillSans.ttc');

FontConfig.sort({
  family: "Gill Sans",
  weight: FontConfig.FC_WEIGHT_LIGHT
});

// returns this:
[
  { file: '/Library/Fonts/GillSans.ttc', index: 7 },
  { file: '/Library/Fonts/GillSans.ttc', index: 6 },
  { file: '/Library/Fonts/Comic Sans MS.ttf', index: 0 },
  { file: '/Library/Fonts/Futura.ttc', index: 2 }
]

Unlike the full FontConfig library, it does not read configuration files for font lookup rules or font search directories. The FontConfig APIs for adding fonts and querying fonts are exposed instead, which the Javascript wrapper makes calls to.

Instead of using FreeType to read fonts, fontkit is used.

API

FontConfig.sort()

Returns the best possible cascade list of fonts based on a description (with later fonts suited to use when previous fonts don't have proper coverage).

The family object passed to FontConfig.sort looks like:

  • family (string) (required)
  • weight (number)

For more details on the individual values, continue below.

Weights

  • FontConfig.FC_WEIGHT_THIN
  • FontConfig.FC_WEIGHT_EXTRALIGHT
  • FontConfig.FC_WEIGHT_ULTRALIGHT
  • FontConfig.FC_WEIGHT_LIGHT
  • FontConfig.FC_WEIGHT_DEMILIGHT
  • FontConfig.FC_WEIGHT_SEMILIGHT
  • FontConfig.FC_WEIGHT_BOOK
  • FontConfig.FC_WEIGHT_REGULAR
  • FontConfig.FC_WEIGHT_NORMAL
  • FontConfig.FC_WEIGHT_MEDIUM
  • FontConfig.FC_WEIGHT_DEMIBOLD
  • FontConfig.FC_WEIGHT_SEMIBOLD
  • FontConfig.FC_WEIGHT_BOLD
  • FontConfig.FC_WEIGHT_EXTRABOLD
  • FontConfig.FC_WEIGHT_ULTRABOLD
  • FontConfig.FC_WEIGHT_BLACK
  • FontConfig.FC_WEIGHT_HEAVY
  • FontConfig.FC_WEIGHT_EXTRABLACK
  • FontConfig.FC_WEIGHT_ULTRABLACK

Enabling debugging

Thanks to WASI, you can set the environment variable FC_DEBUG1 just like regular font config. This will print information to stdout, but only if

  1. You built with -DWASI_RUNTIME (see below)
  2. You're using nodejs 13.3.0 or newer
  3. You run nodejs with --experimental-wasi-unstable-preview1 --experimental-wasm-bigint

The WASM distributed in NPM is not built with debugging calls, though there are probably WASI runtimes available for web browsers.

1As of node 13.10 this doesn't seem to be implemented yet, so you have to manually edit src/fcdbg.c until it's fixed.

Building the C parts

These instructions are for macOS, but they should be similar for Linux. The steps are familiar to anyone who has done cross-compilation before, but the first few are WebAssembly-specific.

  1. LLVM_PATH=/usr/local/Cellar/llvm/9.0.0/bin. or somewhere else appropriate. You do need a recent version of LLVM that supports WebAssembly.
  2. git clone git@github.com:chearon/wasi-libc.git. The upstream version builds with symbols that introduce runtime dependencies.
  3. cd wasi-libc
  4. make WASM_CC=$LLVM_PATH/clang WASM_AR=$LLVM_PATH/llvm-ar WASM_NM=$LLVM_PATH/llvm-nm
  5. WASI_SYSROOT=$(pwd)/sysroot
  6. Download wasi-sdk builtins and copy libclang_rt.builtins-wasm32.a to /usr/local/Cellar/llvm/9.0.0/lib/clang/9.0.0/lib/wasi/ (or wherever you installed your compiler in step 1).
  7. export LIBTOOLIZE=glibtoolize (these next 3 steps might be macOS-specific)
  8. export GETTEXTIZE=/usr/local/opt/gettext/bin/gettextize
  9. export AUTOPOINT=/usr/local/opt/gettext/bin/autopoint
  10. export CC=$LLVM_PATH/clang
  11. export CFLAGS=-target wasm32-wasi --sysroot=$WASI_SYSROOT (for debugging, you can also add -DWASI_RUNTIME to see FontConfig logging)
  12. export AR=$LLVM_PATH/llvm-ar
  13. export RANLIB=$LLVM_PATH/llvm-ranlib
  14. Patch config.sub: grep -q -F -- '-wasi' config.sub || sed -i -e 's/-nacl\*)/-nacl*|-wasi)/' config.sub (thank you so much, Frank Denis)
  15. ./autogen.sh --host=wasm32-wasi
  16. make
  17. $LLVM_PATH/wasm-ld -L$WASI_SYSROOT/lib/wasm32-wasi --no-entry --export-all -lc --whole-archive src/.libs/libfontconfig.a -o lib.wasm
0.5.0

1 year ago

0.4.0

1 year ago

0.3.0

2 years ago

0.3.1

2 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.1

4 years ago

0.1.0

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago