Skip to content

Commit

Permalink
Add a small bit of error handling, copied from hb-subset
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jul 2, 2021
1 parent f4a5297 commit 72b5b99
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,18 @@ async function subsetFont(
const result = exports.hb_face_reference_blob(subset);

const offset = exports.hb_blob_get_data(result, 0);
const subsetByteLength = exports.hb_blob_get_length(result);
if (subsetByteLength === 0) {
exports.hb_blob_destroy(result);
exports.hb_face_destroy(subset);
exports.free(fontBuffer);
throw new Error(
'Failed to create subset font, maybe the input file is corrupted?'
);
}

const subsetFont = Buffer.from(
heapu8.subarray(offset, offset + exports.hb_blob_get_length(result))
heapu8.subarray(offset, offset + subsetByteLength)
);

// Clean up
Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,22 @@ describe('subset-font', function () {
expect(result.slice(0, 4).toString(), 'to equal', 'wOF2');
});
});

describe('with a truncated font', function () {
before(async function () {
this.truncatedTtfFont = await readFile(
pathModule.resolve(__dirname, '..', 'testdata', 'FZZJ-ZSXKJW.ttf')
);
});

it('should error out', async function () {
await expect(
subsetFont(this.truncatedTtfFont, 'abcd', {
targetFormat: 'woff',
}),
'to be rejected with',
'Failed to create subset font, maybe the input file is corrupted?'
);
});
});
});
Binary file added testdata/FZZJ-ZSXKJW.ttf
Binary file not shown.

0 comments on commit 72b5b99

Please sign in to comment.