Skip to content

Commit

Permalink
Revert "Revert me: Support shelling out to the hb-subset binary inste…
Browse files Browse the repository at this point in the history
…ad of using the wasm build"

This reverts commit 77e833d.
  • Loading branch information
papandreou committed Jul 17, 2020
1 parent 77e833d commit d545459
Showing 1 changed file with 38 additions and 78 deletions.
116 changes: 38 additions & 78 deletions lib/subsetLocalFontWithHarfbuzz.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const readFileAsync = require('util').promisify(fs.readFile);
const _ = require('lodash');
const wawoff2 = require('wawoff2');
const woffTool = require('woff2sfnt-sfnt2woff');
const childProcess = require('child_process');
const getTemporaryFilePath = require('gettemporaryfilepath');
const { readFile, writeFile, unlink } = require('fs').promises;
const execFile = require('util').promisify(childProcess.execFile);

const loadAndInitializeHarfbuzz = _.once(async () => {
const {
Expand All @@ -31,93 +27,57 @@ module.exports = async (originalFont, targetFormat, text) => {
originalFont = await wawoff2.decompress(originalFont);
}

let subsetFont;
if (true) {
text = text || '*';

const tempInputFileName = getTemporaryFilePath({
prefix: 'input-',
suffix: `.ttf`,
});
const tempOutputFileName = getTemporaryFilePath({
prefix: 'output-',
suffix: `.ttf`,
});

const args = [
`--output-file=${tempOutputFileName}`,
tempInputFileName,
text,
];

try {
await writeFile(tempInputFileName, originalFont);
await execFile('../harfbuzz/build/util/hb-subset', args);
// Await to make sure the output file has been consumed before we delete it in the finally block below:
subsetFont = await readFile(tempOutputFileName);
} finally {
unlink(tempInputFileName).then(
() => {},
() => {}
);
unlink(tempOutputFileName).then(
() => {},
() => {}
);
}
} else {
const fontBuffer = exports.malloc(originalFont.byteLength);
heapu8.set(new Uint8Array(originalFont), fontBuffer);
const fontBuffer = exports.malloc(originalFont.byteLength);
heapu8.set(new Uint8Array(originalFont), fontBuffer);

// Create the face
const blob = exports.hb_blob_create(
fontBuffer,
originalFont.byteLength,
2, // HB_MEMORY_MODE_WRITABLE
0,
0
);
const face = exports.hb_face_create(blob, 0);
exports.hb_blob_destroy(blob);
// Create the face
const blob = exports.hb_blob_create(
fontBuffer,
originalFont.byteLength,
2, // HB_MEMORY_MODE_WRITABLE
0,
0
);
const face = exports.hb_face_create(blob, 0);
exports.hb_blob_destroy(blob);

// Add glyph indices and subset
const glyphs = exports.hb_set_create();
// Add glyph indices and subset
const glyphs = exports.hb_set_create();

for (let i = 0; i < text.length; i += 1) {
exports.hb_set_add(glyphs, text.charCodeAt(i));
}
for (let i = 0; i < text.length; i += 1) {
exports.hb_set_add(glyphs, text.charCodeAt(i));
}

const input = exports.hb_subset_input_create_or_fail();
const inputGlyphs = exports.hb_subset_input_unicode_set(input);
exports.hb_set_union(inputGlyphs, glyphs);
exports.hb_subset_input_set_drop_hints(input, true);
const subset = exports.hb_subset(face, input);
const input = exports.hb_subset_input_create_or_fail();
const inputGlyphs = exports.hb_subset_input_unicode_set(input);
exports.hb_set_union(inputGlyphs, glyphs);
exports.hb_subset_input_set_drop_hints(input, true);
const subset = exports.hb_subset(face, input);

// Clean up
exports.hb_subset_input_destroy(input);
// Clean up
exports.hb_subset_input_destroy(input);

// Get result blob
const result = exports.hb_face_reference_blob(subset);
// Get result blob
const result = exports.hb_face_reference_blob(subset);

const offset = exports.hb_blob_get_data(result, 0);
const subsetFontBlob = heapu8.slice(
offset,
offset + exports.hb_blob_get_length(result)
);
const offset = exports.hb_blob_get_data(result, 0);
const subsetFontBlob = heapu8.slice(
offset,
offset + exports.hb_blob_get_length(result)
);

// Clean up
exports.hb_blob_destroy(result);
exports.hb_face_destroy(subset);
subsetFont = subsetFontBlob;
}
// Clean up
exports.hb_blob_destroy(result);
exports.hb_face_destroy(subset);

let subsetFont;
if (targetFormat === 'woff2') {
subsetFont = Buffer.from(await wawoff2.compress(subsetFont));
subsetFont = Buffer.from(await wawoff2.compress(subsetFontBlob));
} else if (targetFormat === 'woff') {
subsetFont = woffTool.toWoff(subsetFont);
subsetFont = woffTool.toWoff(subsetFontBlob);
} else {
// targetFormat === 'truetype'
subsetFont = Buffer.from(subsetFont);
subsetFont = Buffer.from(subsetFontBlob);
}
return subsetFont;
};

0 comments on commit d545459

Please sign in to comment.