Pomax / lib-font

This library adds a new Font() object to the JavaScript toolbox, similar to new Image() for images
MIT License
730 stars 72 forks source link

font.opentype.tables is not enumerable #129

Closed darknoon closed 2 years ago

darknoon commented 2 years ago

I tried doing Object.keys(font.opentype.tables) to get the tables that the font contains but it just returns []

I think this is due to lazy.js

export default function lazy(object, property, getter) {
  let val;
  Object.defineProperty(object, property, {
    get: () => {
      if (val) return val;
      val = getter();
      return val;
    },
  });
}

where Object.defineProperty(…) has via MDN:

By default, values added using Object.defineProperty() are immutable and not enumerable

I couldn't find much documentation, so it wasn't clear if this is somehow intended. Is there a better way to accomplish this?

Pomax commented 2 years ago

Aye, that's by design (because loading things immediately can cause memory to blow up, so a lot of stuff is deferred). You can use font.opentype.directory to get all known tables:

  const { directory, tables } = font.opentype;
  const knownTables = directory.map(e => e.tag.trim())
  knownTables.forEach( name => {
    console.log(tables[name]); // still fairly shallow
  });
darknoon commented 2 years ago

Wouldn't making the properties enumerable not impose any performance delta, though? ie Object.keys() is still cheap until you actually access the data in the table.

Pomax commented 2 years ago

we could add enumerable as property option, and see what it does if you want to give that a shot,

Pomax commented 2 years ago

landed in 2.4.0