Skip to content

Commit

Permalink
Use the conversion tools introduced in #115 when creating a subset fo…
Browse files Browse the repository at this point in the history
…nt with harfbuzz
  • Loading branch information
papandreou committed Jul 26, 2020
1 parent 4ac28ed commit 7159e05
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
25 changes: 5 additions & 20 deletions lib/subsetLocalFontWithHarfbuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions test/subsetLocalFontWithHarfbuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('subsetLocalFontWithHarfbuzz', function () {
);
const result = await subsetLocalFontWithHarfbuzz(
openSansTtf,
'ttf',
'truetype',
'abcd'
);

Expand Down Expand Up @@ -84,7 +84,7 @@ describe('subsetLocalFontWithHarfbuzz', function () {
);
const result = await subsetLocalFontWithHarfbuzz(
openSansWoff,
'ttf',
'truetype',
'abcd'
);

Expand Down Expand Up @@ -147,7 +147,7 @@ describe('subsetLocalFontWithHarfbuzz', function () {
);
const result = await subsetLocalFontWithHarfbuzz(
openSansWoff,
'ttf',
'truetype',
'abcd'
);

Expand Down

0 comments on commit 7159e05

Please sign in to comment.