Skip to content

Commit

Permalink
Bug fix for #757 - write all name table entries for fonts crafted fro…
Browse files Browse the repository at this point in the history
…m scratch (#758)

* Main bug fix

 - `for...in` loop iterates over keys in `names` object, not indexes of an array of strings
 - if `uniqueID` or `postScriptName` is found to be missing, update the platform that is currently being looped over, not just the `unicode` platform.

* Added tests for new name table entries

Test now checks for defaults for `postScriptName` and also `uniqueID`

Generating a `uniqueID` now provides a fallback empty string if no `manufacturer` exists.
  • Loading branch information
mattlag authored Sep 9, 2024
1 parent 52d167f commit f8108ec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/tables/sfnt.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -292,16 +292,17 @@ function fontToSfntTable(font) {
const fontNamesWindows = font.names.windows || {};

// do this as a loop to reduce redundant code
for (const platform in ['unicode', 'macintosh', 'windows']) {
for (const platform in names) {

names[platform] = names[platform] || {};

if (!names[platform].uniqueID) {
names.unicode.uniqueID = {en: font.getEnglishName('manufacturer') + ':' + englishFullName};
const manufacturer = font.getEnglishName('manufacturer') || '';
names[platform].uniqueID = { en: `${manufacturer}: ${englishFullName}` };
}

if (!names[platform].postScriptName) {
names.unicode.postScriptName = {en: postScriptName};
names[platform].postScriptName = {en: postScriptName};
}
}

Expand Down
16 changes: 11 additions & 5 deletions test/tables/sfnt.spec.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'assert';
import Font from '../../src/font.mjs';
import sfnt from '../../src/tables/sfnt.mjs';
import name from '../../src/tables/name.mjs';
import sfnt from '../../src/tables/sfnt.mjs';
import { encode } from '../../src/types.mjs';

function encodeAndParseTable(table, parser) {
Expand Down Expand Up @@ -60,7 +60,8 @@ describe('tables/sfnt.mjs', function () {
license: { en: ' ' },
licenseURL: { en: ' ' },
preferredFamily: { en: defaultFont.familyName }, // 'MyFont'
preferredSubfamily: { en: defaultFont.styleName } // 'Medium'
preferredSubfamily: { en: defaultFont.styleName }, // 'Medium'
uniqueID: { en: ` : ${defaultFont.familyName} ${defaultFont.styleName}` },
},
windows: {
copyright: { en: ' ' },
Expand All @@ -78,7 +79,8 @@ describe('tables/sfnt.mjs', function () {
license: { en: ' ' },
licenseURL: { en: ' ' },
preferredFamily: { en: defaultFont.familyName }, // 'MyFont'
preferredSubfamily: { en: defaultFont.styleName } // 'Medium'
preferredSubfamily: { en: defaultFont.styleName }, // 'Medium'
uniqueID: { en: ` : ${defaultFont.familyName} ${defaultFont.styleName}` },
}
});
});
Expand Down Expand Up @@ -121,15 +123,19 @@ describe('tables/sfnt.mjs', function () {
fullName: { en: fullName },
version: { en: version },
preferredFamily: { en: preferredFamily },
preferredSubfamily: { en: preferredSubfamily}
preferredSubfamily: { en: preferredSubfamily },
postScriptName: { en: `${fontFamily.replaceAll(' ', '')}-${fontSubfamily}` },
uniqueID: { en: `: ${fontFamily} ${fontSubfamily}` },
},
windows: {
fontFamily: { en: fontFamily },
fontSubfamily: { en: fontSubfamily},
fullName: { en: fullName },
version: { en: version },
preferredFamily: { en: preferredFamily },
preferredSubfamily: { en: preferredSubfamily}
preferredSubfamily: { en: preferredSubfamily},
postScriptName: { en: `${fontFamily.replaceAll(' ', '')}-${fontSubfamily}` },
uniqueID: { en: `: ${fontFamily} ${fontSubfamily}` },
}
});
});
Expand Down

0 comments on commit f8108ec

Please sign in to comment.