Skip to content

Instantly share code, notes, and snippets.

@SimplyAhmazing
Created January 4, 2024 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SimplyAhmazing/8c73cef0a345917e74821a6a5fc819f5 to your computer and use it in GitHub Desktop.
Save SimplyAhmazing/8c73cef0a345917e74821a6a5fc819f5 to your computer and use it in GitHub Desktop.
// In a directory, run "npm install harfbuzzjs" to install harfbuzzjs
// then "node main.js" to run this example
var fs = require('fs');
var path = require('path');
function example(hb, fontBlob, text) {
var blob = hb.createBlob(fontBlob);
var face = hb.createFace(blob, 0);
// console.log(face.getAxisInfos());
var font = hb.createFont(face);
// font.setVariations({ wdth: 200, wght: 700 });
font.setScale(1000, 1000); // Optional, if not given will be in font upem
var buffer = hb.createBuffer();
buffer.addText(text || 'abc');
buffer.guessSegmentProperties();
// buffer.setDirection('ltr'); // optional as can be set by guessSegmentProperties also
hb.shape(font, buffer); // features are not supported yet
var result = buffer.json(font);
// returns glyphs paths, totally optional
var glyphs = {};
result.forEach(function (x) {
if (glyphs[x.g]) return;
glyphs[x.g] = {
name: font.glyphName(x.g),
// path: font.glyphToPath(x.g),
json: font.glyphToJson(x.g)
};
});
var unicodes = face.collectUnicodes()
buffer.destroy();
font.destroy();
face.destroy();
blob.destroy();
return { shape: result, glyphs: glyphs, unicodes: unicodes };
}
require('harfbuzzjs').then(function (hbjs) {
console.log(example(hbjs, new Uint8Array(fs.readFileSync(path.resolve(__dirname, 'NotoSans-Regular.ttf')))));
}, console.log);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment