opentypejs / opentype.js

Read and write OpenType fonts using JavaScript.
https://opentype.js.org/
MIT License
4.36k stars 467 forks source link

getEnglishName('fontFamily') returns undefined, font.names.macintosh is incomplete #570

Open Connum opened 1 year ago

Connum commented 1 year ago

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)

  1. Download this font and load into opentype.js
  2. call font.getEnglishName('fontFamily')
  3. call font.download()

Your Environment

ILOVEPIE commented 1 year ago

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.

Connum commented 1 year ago

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

Connum commented 1 year ago

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.

ollimeier commented 1 year ago

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.

ollimeier commented 1 year ago

For fontwerk I wrote this fontbakery test: https://github.com/googlefonts/fontbakery/blob/24d58a4f1b127f4a0d1f36f9d55aae5829778ad5/Lib/fontbakery/profiles/fontwerk.py#L65

Connum commented 2 months ago

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