What you are looking for is a font subsetter. It will return a new font file to you that only has the required characters and kerning pairs. This can be quickly done with subset-font.
// npm i subset-font
const subsetFont = require('subset-font');
const mySfntFontBuffer = Buffer.from(/*...*/);
// Create a new font with only the characters required to render "Hello, world!" in WOFF2 format:
const subsetBuffer = await subsetFont(mySfntFontBuffer, 'Hello, world!', {
targetFormat: 'woff2',
});
That being said, more powerful/flexible libraries exist such as fontkit. You might want this if you are looking for more complicated edits.
// npm install fontkit
var fontkit = require('fontkit');
// open a font synchronously
var font = fontkit.openSync('font.ttf');
// layout a string, using default shaping features.
// returns a GlyphRun, describing glyphs and positions.
var run = font.layout('hello world!');
// create a font subset
var subset = font.createSubset();
run.glyphs.forEach(function(glyph) {
subset.includeGlyph(glyph);
});
let buffer = subset.encode();