From 4e8757eb08d87c93e823a342105b262e83cc7e53 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 4 Nov 2021 10:27:05 +0100 Subject: [PATCH 1/7] Update harfbuzzjs to ^0.2.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5f2e712..94c684c 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "dependencies": { "fontverter": "^2.0.0", - "harfbuzzjs": "^0.1.5", + "harfbuzzjs": "^0.2.0", "lodash": "^4.17.21", "p-limit": "^3.1.0" }, From 68ce78ceb6f53e5933874dbe24cc91e060149a58 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 4 Nov 2021 11:08:37 +0100 Subject: [PATCH 2/7] Adapt to the harfbuzz 3 api https://github.com/papandreou/subset-font/issues/12#issuecomment-960609873 --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 904d287..17785b1 100644 --- a/index.js +++ b/index.js @@ -45,7 +45,10 @@ async function subsetFont( const input = exports.hb_subset_input_create_or_fail(); 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); } @@ -57,7 +60,7 @@ async function subsetFont( exports.hb_set_add(inputUnicodes, c.codePointAt(0)); } - const subset = exports.hb_subset(face, input); + const subset = exports.hb_subset_or_fail(face, input); // Clean up exports.hb_subset_input_destroy(input); From bb2a0c59ab65ef23ee3ff66711a3a2608eed750b Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Thu, 4 Nov 2021 11:21:29 +0100 Subject: [PATCH 3/7] Handle a return value of 0 from the _or_fail functions https://github.com/papandreou/subset-font/pull/13#discussion_r742696732 --- index.js | 21 +++++++++++++++++---- test/index.js | 2 +- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 17785b1..db38798 100644 --- a/index.js +++ b/index.js @@ -43,6 +43,11 @@ async function subsetFont( exports.hb_blob_destroy(blob); 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' + ); + } if (preserveNameIds) { const inputNameIds = exports.hb_subset_input_set( @@ -60,10 +65,18 @@ async function subsetFont( exports.hb_set_add(inputUnicodes, c.codePointAt(0)); } - const subset = exports.hb_subset_or_fail(face, input); - - // Clean up - exports.hb_subset_input_destroy(input); + let subset; + try { + subset = exports.hb_subset_or_fail(face, input); + if (subset === 0) { + 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); diff --git a/test/index.js b/test/index.js index eab4ab9..2918b2c 100644 --- a/test/index.js +++ b/test/index.js @@ -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?' ); }); }); From e2deb41d6a7ff6e6c007b1345368c8f61ef13d02 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Fri, 5 Nov 2021 01:43:49 +0100 Subject: [PATCH 4/7] Destroy face and free fontBuffer whwn hb_subset_or_fail fails https://github.com/papandreou/subset-font/pull/13#discussion_r743094676 --- index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/index.js b/index.js index db38798..dfccf82 100644 --- a/index.js +++ b/index.js @@ -69,6 +69,8 @@ async function subsetFont( 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?' ); From 2e3baa2ae516aebcaf51378d7c40cd96bb925bc0 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Fri, 5 Nov 2021 11:31:12 +0100 Subject: [PATCH 5/7] Move allocations around so there's nothing to clean up when hb_subset_input_create_or_fail returns 0 https://github.com/papandreou/subset-font/pull/13#discussion_r743354527 --- index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index dfccf82..ba08ff2 100644 --- a/index.js +++ b/index.js @@ -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); @@ -42,13 +49,6 @@ 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(); - if (input === 0) { - throw new Error( - 'hb_subset_input_create_or_fail (harfbuzz) returned zero, indicating failure' - ); - } - if (preserveNameIds) { const inputNameIds = exports.hb_subset_input_set( input, From a8786cbbc9648d7159de2e5890153da85fc4b746 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 10 Nov 2021 08:29:08 +0100 Subject: [PATCH 6/7] Update harfbuzzjs to ^0.2.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 94c684c..27f0637 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "dependencies": { "fontverter": "^2.0.0", - "harfbuzzjs": "^0.2.0", + "harfbuzzjs": "^0.2.1", "lodash": "^4.17.21", "p-limit": "^3.1.0" }, From d1275c58f7ebf19667fd98cf3dbedc75b0e81188 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Wed, 10 Nov 2021 08:29:40 +0100 Subject: [PATCH 7/7] Do the equivalent of --font-features=* --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index ba08ff2..e079161 100644 --- a/index.js +++ b/index.js @@ -49,6 +49,14 @@ async function subsetFont( const face = exports.hb_face_create(blob, 0); exports.hb_blob_destroy(blob); + // 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_set( input,