opentype.js
opentype.js copied to clipboard
getEnglishName('fontFamily') returns undefined, font.names.macintosh is incomplete
Expected Behavior
The font download shouldn't fail if font.getEnglishName('fontFamily')
returns undefined
, but even more so, font.getEnglishName('fontFamily')
shouldn't return undefined in the first place.
Current Behavior
For some fonts font.download()
fails because font.getEnglishName('fontFamily')
returns undefined
Possible Solution
The issues seems to be related to #542. The name table.names.macintosh contains several numeric IDs for, but none of the regular entries. In getEnglishName()
we should probably do this (we can't use optional chaining because it cannot be transpiled currently, but just to get the idea):
const translations = (this.names?.unicode[name] || this.names?.macintosh[name] || this.names?.windows[name]);
Also in names.js,
let platform = name[platformName];
if (platform === undefined) {
platform = name[platformName] = {};
}
let translations = platform[property];
if (translations === undefined) {
translations = platform[property] = {};
}
The name/platform shouldn't be initialized to an empty object, but the default name properties should be copied from one of the existing ones.
But also it shouldn't ever happen that the names objects are in such an irregular state.
Steps to Reproduce (for bugs)
- Download this font and load into opentype.js
- call
font.getEnglishName('fontFamily')
- call
font.download()
Your Environment
- Version used: current master
- Font used: Selawik-variable.ttf
- Browser Name and version: -
- Operating System and version (desktop or mobile): -
- Link to your project: -
If font.names.macintosh is incomplete then the font is corrupt, the macintosh names table is required. At least according to the documentation i've read.
OK but then I still think we should handle this more gracefully, by setting the macintosh names to available names, our at least rather log a warning instead of throwing an error
It doesn't seem to be too uncommon by the way, I have another variable font with the same issue. Or it might have been the unicode table there, but I'll have to check again. Anyway we should handle this in a better way.
If font.names.macintosh is incomplete then the font is corrupt, the macintosh names table is required. At least according to the documentation i've read.
The Mac names are not required anymore. I used to work for Monotype and now work for Fontwerk – and we don't produce any Mac name table entries anymore. Even Apple stopped producing mac names in their fonts.
For fontwerk I wrote this fontbakery test: https://github.com/googlefonts/fontbakery/blob/24d58a4f1b127f4a0d1f36f9d55aae5829778ad5/Lib/fontbakery/profiles/fontwerk.py#L65
This will be fixed by #701
Font.prototype.getEnglishName = function(name) {
const translations = (this.names.unicode || this.names.macintosh || this.names.windows)[name];
if(!translations) {
for(let platform of ['unicode', 'macintosh', 'windows']) {
if(this.names[platform] && this.names[platform][name]) {
return this.names[platform][name].en;
}
}
}
if (translations) {
return translations.en;
}
};
getEnglishName()
will not only fall back table-wise, but also property-wise