This is essentially a stripped-down version of FontConfig compiled to WebAssembly with a Javascript API that works in nodejs and the browser.
import FontConfigInit from 'fontconfig';
import fs from 'fs';
const FontConfig = await FontConfigInit(/* wasm URL if you're in the browser */);
const cfg = new FontConfig();
const fonts = [
'/Library/Fonts/Comic Sans MS.ttf',
'/Library/Fonts/Futura.ttc',
'/Library/Fonts/GillSans.ttc'
];
for (const filename of fonts) {
cfg.addFont(new Uint8Array(fs.readFileSync(filename)), filename);
}
cfg.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 part of FontConfig for adding and querying fonts (the matching module) is exposed instead, which the Javascript wrapper makes calls to. At some point support for FontConfig rules and aliases (the configuration module) could be added. I don't expect it to be very useful since consumers of this library are typically adding and selecting fonts on their own, rather than users doing it.
Instead of using FreeType to read fonts, fontkit is used.
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 pattern object passed to FontConfig.prototype.sort looks like:
family(string|string[]) (required)weight(number|string) (a CSS2 weight like'200'orFontConfig.FC_WEIGHT_EXTRALIGHT)width(number|string) (a CSS3 width like'condensed'orFontConfig.FC_WIDTH_CONDENSED)slant(number|string) (a CSS2 slant like'oblique'orFontConfig.FC_SLANT_OBLIQUE)lang(string|string[])coverage(number[])
When the family is an array of more than one family, it is a prioritized list. If the first family isn't found, FontConfig tries the second family. If more than one is found, they will all appear in the results in prioritized order.
For more details on other properties' individual values, continue below.
Note you can also use CSS2 font-weight values as a string like '300'
FontConfig.FC_WEIGHT_THINFontConfig.FC_WEIGHT_EXTRALIGHTFontConfig.FC_WEIGHT_ULTRALIGHTFontConfig.FC_WEIGHT_LIGHTFontConfig.FC_WEIGHT_DEMILIGHTFontConfig.FC_WEIGHT_SEMILIGHTFontConfig.FC_WEIGHT_BOOKFontConfig.FC_WEIGHT_REGULARFontConfig.FC_WEIGHT_NORMALFontConfig.FC_WEIGHT_MEDIUMFontConfig.FC_WEIGHT_DEMIBOLDFontConfig.FC_WEIGHT_SEMIBOLDFontConfig.FC_WEIGHT_BOLDFontConfig.FC_WEIGHT_EXTRABOLDFontConfig.FC_WEIGHT_ULTRABOLDFontConfig.FC_WEIGHT_BLACKFontConfig.FC_WEIGHT_HEAVYFontConfig.FC_WEIGHT_EXTRABLACKFontConfig.FC_WEIGHT_ULTRABLACK
Note you can also use CSS3 font-stretch values as a string like 'condensed'
FontConfig.FC_WIDTH_ULTRACONDENSEDFontConfig.FC_WIDTH_EXTRACONDENSEDFontConfig.FC_WIDTH_CONDENSEDFontConfig.FC_WIDTH_SEMICONDENSEDFontConfig.FC_WIDTH_NORMALFontConfig.FC_WIDTH_SEMIEXPANDEDFontConfig.FC_WIDTH_EXPANDEDFontConfig.FC_WIDTH_EXTRAEXPANDEDFontConfig.FC_WIDTH_ULTRAEXPANDED
Note you can also use CSS2 font-style values as a string like 'italic'
FontConfig.FC_SLANT_ROMANFontConfig.FC_SLANT_ITALICFontConfig.FC_SLANT_OBLIQUE
An RFC3366 language tag such as "en" or "zh-tw"
The coverage option is an array of unicode codepoints. For example, if you pass {coverage: [0x1780]}, a Khmer font will be at the top of the list.
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
- You built with
-DWASI_RUNTIME(see below) - You're using nodejs 13.3.0 or newer
- 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.
Make sure you clone with --recurse-submodules
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.
cd fontconfig; patch -p1 < ../pure-wasm-compat-fontconfig.patch; cd..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.git clone [email protected]:chearon/wasi-libc.git. The upstream version builds with symbols that introduce runtime dependencies.cd wasi-libcmake WASM_CC=$LLVM_PATH/clang WASM_AR=$LLVM_PATH/llvm-ar WASM_NM=$LLVM_PATH/llvm-nmWASI_SYSROOT=$(pwd)/sysroot- Download
wasi-sdkbuiltins and copylibclang_rt.builtins-wasm32.ato/usr/local/Cellar/llvm/9.0.0/lib/clang/9.0.0/lib/wasi/(or wherever you installed your compiler in step 1). export LIBTOOLIZE=glibtoolize(these next 3 steps might be macOS-specific)export GETTEXTIZE=/usr/local/opt/gettext/bin/gettextizeexport AUTOPOINT=/usr/local/opt/gettext/bin/autopointexport CC=$LLVM_PATH/clangexport CFLAGS=-target wasm32-wasi --sysroot=$WASI_SYSROOT(for debugging, you can also add-DWASI_RUNTIMEto see FontConfig logging)export AR=$LLVM_PATH/llvm-arexport RANLIB=$LLVM_PATH/llvm-ranlib- Patch config.sub:
grep -q -F -- '-wasi' config.sub || sed -i -e 's/-nacl\*)/-nacl*|-wasi)/' config.sub(thank you so much, Frank Denis) ./autogen.sh --host=wasm32-wasimake$LLVM_PATH/wasm-ld -L$WASI_SYSROOT/lib/wasm32-wasi --no-entry --export-all -lc --whole-archive src/.libs/libfontconfig.a -o ../packages/core/lib.wasm