From 7159e05b253186c58107ade3a3feafe5d55d7a35 Mon Sep 17 00:00:00 2001 From: Andreas Lind Date: Sun, 26 Jul 2020 02:19:45 +0200 Subject: [PATCH] Use the conversion tools introduced in #115 when creating a subset font with harfbuzz --- lib/subsetLocalFontWithHarfbuzz.js | 25 +++++-------------------- test/subsetLocalFontWithHarfbuzz.js | 6 +++--- 2 files changed, 8 insertions(+), 23 deletions(-) diff --git a/lib/subsetLocalFontWithHarfbuzz.js b/lib/subsetLocalFontWithHarfbuzz.js index 25b461ed..18e012bf 100644 --- a/lib/subsetLocalFontWithHarfbuzz.js +++ b/lib/subsetLocalFontWithHarfbuzz.js @@ -2,8 +2,7 @@ const fs = require('fs'); const readFileAsync = require('util').promisify(fs.readFile); const _ = require('lodash'); -const wawoff2 = require('wawoff2'); -const woffTool = require('woff2sfnt-sfnt2woff'); +const convertFontBuffer = require('./convertFontBuffer'); function HB_TAG(chunkName) { return chunkName.split('').reduce(function (a, ch) { @@ -26,12 +25,7 @@ const loadAndInitializeHarfbuzz = _.once(async () => { async function subset(originalFont, targetFormat, text) { const [exports, heapu8] = await loadAndInitializeHarfbuzz(); - const signature = originalFont.slice(0, 4).toString(); - if (signature === 'wOFF') { - originalFont = woffTool.toSfnt(originalFont); - } else if (signature === 'wOF2') { - originalFont = await wawoff2.decompress(originalFont); - } + originalFont = await convertFontBuffer(originalFont, 'truetype'); const fontBuffer = exports.malloc(originalFont.byteLength); heapu8.set(new Uint8Array(originalFont), fontBuffer); @@ -79,24 +73,15 @@ async function subset(originalFont, targetFormat, text) { const result = exports.hb_face_reference_blob(subset); const offset = exports.hb_blob_get_data(result, 0); - let subsetFont = heapu8.slice( - offset, - offset + exports.hb_blob_get_length(result) + const subsetFont = Buffer.from( + heapu8.slice(offset, offset + exports.hb_blob_get_length(result)) ); // Clean up exports.hb_blob_destroy(result); exports.hb_face_destroy(subset); - if (targetFormat === 'woff2') { - subsetFont = Buffer.from(await wawoff2.compress(subsetFont)); - } else if (targetFormat === 'woff') { - subsetFont = woffTool.toWoff(subsetFont); - } else { - // targetFormat === 'truetype' - subsetFont = Buffer.from(subsetFont); - } - return subsetFont; + return await convertFontBuffer(subsetFont, targetFormat, 'truetype'); } const limiter = require('p-limit')(1); diff --git a/test/subsetLocalFontWithHarfbuzz.js b/test/subsetLocalFontWithHarfbuzz.js index 28721015..a80bb032 100644 --- a/test/subsetLocalFontWithHarfbuzz.js +++ b/test/subsetLocalFontWithHarfbuzz.js @@ -18,7 +18,7 @@ describe('subsetLocalFontWithHarfbuzz', function () { ); const result = await subsetLocalFontWithHarfbuzz( openSansTtf, - 'ttf', + 'truetype', 'abcd' ); @@ -84,7 +84,7 @@ describe('subsetLocalFontWithHarfbuzz', function () { ); const result = await subsetLocalFontWithHarfbuzz( openSansWoff, - 'ttf', + 'truetype', 'abcd' ); @@ -147,7 +147,7 @@ describe('subsetLocalFontWithHarfbuzz', function () { ); const result = await subsetLocalFontWithHarfbuzz( openSansWoff, - 'ttf', + 'truetype', 'abcd' );