Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update harfbuzzjs to ^0.2.0 (harfbuzz 3.0.0) #13

Merged
merged 7 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ async function subsetFont(

originalFont = await fontverter.convert(originalFont, 'truetype');

const input = exports.hb_subset_input_create_or_fail();
if (input === 0) {
throw new Error(
'hb_subset_input_create_or_fail (harfbuzz) returned zero, indicating failure'
);
}

const fontBuffer = exports.malloc(originalFont.byteLength);
heapu8.set(new Uint8Array(originalFont), fontBuffer);

Expand All @@ -42,10 +49,19 @@ async function subsetFont(
const face = exports.hb_face_create(blob, 0);
exports.hb_blob_destroy(blob);

const input = exports.hb_subset_input_create_or_fail();
// Do the equivalent of --font-features=*
const layoutFeatures = exports.hb_subset_input_set(
input,
6 // HB_SUBSET_SETS_LAYOUT_FEATURE_TAG
);
exports.hb_set_clear(layoutFeatures);
exports.hb_set_invert(layoutFeatures);

if (preserveNameIds) {
const inputNameIds = exports.hb_subset_input_nameid_set(input);
const inputNameIds = exports.hb_subset_input_set(
input,
4 // HB_SUBSET_SETS_NAME_ID
);
for (const nameId of preserveNameIds) {
exports.hb_set_add(inputNameIds, nameId);
}
Expand All @@ -57,10 +73,20 @@ async function subsetFont(
exports.hb_set_add(inputUnicodes, c.codePointAt(0));
}

const subset = exports.hb_subset(face, input);

// Clean up
exports.hb_subset_input_destroy(input);
let subset;
try {
subset = exports.hb_subset_or_fail(face, input);
if (subset === 0) {
exports.hb_face_destroy(face);
exports.free(fontBuffer);
throw new Error(
'hb_subset_or_fail (harfbuzz) returned zero, indicating failure. Maybe the input file is corrupted?'
);
}
} finally {
// Clean up
exports.hb_subset_input_destroy(input);
}

// Get result blob
const result = exports.hb_face_reference_blob(subset);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "index.js",
"dependencies": {
"fontverter": "^2.0.0",
"harfbuzzjs": "^0.1.5",
"harfbuzzjs": "^0.2.1",
"lodash": "^4.17.21",
"p-limit": "^3.1.0"
},
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('subset-font', function () {
targetFormat: 'woff',
}),
'to be rejected with',
'Failed to create subset font, maybe the input file is corrupted?'
'hb_subset_or_fail (harfbuzz) returned zero, indicating failure. Maybe the input file is corrupted?'
);
});
});
Expand Down