Skip to content

Commit

Permalink
Implement support for a preserveNameIds array
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Jun 27, 2021
1 parent f4f508c commit 00816d7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const loadAndInitializeHarfbuzz = _.once(async () => {
async function subsetFont(
originalFont,
text,
{ targetFormat = fontverter.detectFormat(originalFont) } = {}
{ targetFormat = fontverter.detectFormat(originalFont), preserveNameIds } = {}
) {
const [exports, heapu8] = await loadAndInitializeHarfbuzz();

Expand All @@ -40,6 +40,13 @@ async function subsetFont(

const input = exports.hb_subset_input_create_or_fail();

if (preserveNameIds) {
const inputNameIds = exports.hb_subset_input_nameid_set(input);
for (const nameId of preserveNameIds) {
exports.hb_set_add(inputNameIds, nameId);
}
}

// Add unicodes indices
const inputUnicodes = exports.hb_subset_input_unicode_set(input);
for (const c of text) {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^4.0.1",
"eslint-plugin-standard": "^5.0.0",
"fast-xml-parser": "^3.19.0",
"gettemporaryfilepath": "^1.0.1",
"mocha": "^7.0.0",
"nyc": "^15.0.0",
"offline-github-changelog": "^2.0.0",
Expand Down
45 changes: 44 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,28 @@
const expect = require('unexpected');
const childProcess = require('child_process');
const getTemporaryFilePath = require('gettemporaryfilepath');
const fs = require('fs').promises;

const expect = require('unexpected')
.clone()
.addAssertion(
'<Buffer> [not] to have name id <number>',
async (expect, fontBuffer, nameId) => {
const outputFile = getTemporaryFilePath({ suffix: '.ttf' });
await fs.writeFile(outputFile, fontBuffer);
const xmlBuffer = childProcess.execSync(`ttx -t name -o - ${outputFile}`);
const nameRecords = require('fast-xml-parser').parse(
xmlBuffer.toString(),
{
ignoreAttributes: false,
parseAttributeValue: true,
}
).ttFont.name.namerecord;
expect.errorMode = 'nested';
expect(nameRecords, '[not] to have an item satisfying', {
'@_nameID': nameId,
});
}
);
const subsetFont = require('..');
const fontverter = require('fontverter');
const { readFile } = require('fs').promises;
Expand Down Expand Up @@ -51,6 +75,25 @@ describe('subset-font', function () {
expect(result.length, 'to be less than', this.sfntFont.length);
expect(result.slice(0, 4).toString(), 'to equal', 'wOF2');
});

describe('when not preserving any name ids', function () {
it('should not preserve name id 14', async function () {
const result = await subsetFont(this.sfntFont, 'abcd');
await expect(result, 'not to have name id', 14);
});
});

describe('when preserving only name id 14', function () {
before(async function () {
this.result = await subsetFont(this.sfntFont, 'abcd', {
preserveNameIds: [14],
});
});

it('should preserve name id 14', async function () {
await expect(this.result, 'to have name id', 14);
});
});
});

describe('with a woff font', function () {
Expand Down

0 comments on commit 00816d7

Please sign in to comment.