opentypejs / opentype.js

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

advanceWidth of Glyph is NaN after call to Glyph.getBoundingBox #440

Open superelchi opened 4 years ago

superelchi commented 4 years ago

After writing a font to disk and reading it back advanceWidth for all Glyphs is set to NaN after calling Glyph.getBoundingBox.

Expected Behavior

Glyph.advanceWidth should return a number.

Current Behavior

Glyph.advanceWidth returns NaN for all Glyphs.

Steps to Reproduce (for bugs)

const fs = require('fs');
const opentype = require('opentype.js');
const assert = require('assert');

// Load font here: 
// https://google-webfonts-helper.herokuapp.com/api/fonts/open-sans?download=zip&subsets=latin&variants=regular
let font = opentype.loadSync('./open-sans-v17-latin-regular.woff');
const advanceWidthOne = font.charToGlyph('1').advanceWidth;

assert.strictEqual(font.charToGlyph('1').advanceWidth, advanceWidthOne, "advanceWidth before getBoundingBox");
font.charToGlyph('1').getBoundingBox();
// works
assert.strictEqual(font.charToGlyph('1').advanceWidth, advanceWidthOne, "advanceWidth after getBoundingBox");

const fileName = 'test.woff';
fs.writeFileSync(fileName, Buffer.from(font.toArrayBuffer()));
let newFont = opentype.loadSync(fileName, {});

assert.strictEqual(newFont.charToGlyph('1').advanceWidth, advanceWidthOne, "advanceWidth before getBoundingBox");
newFont.charToGlyph('1').getBoundingBox();
// does not work 
assert.strictEqual(newFont.charToGlyph('1').advanceWidth, advanceWidthOne, "advanceWidth after getBoundingBox");

Your Environment

mattlag commented 4 years ago

I am also running into this issue. Some fonts seem to be fine, while others return NaN for all advance widths. Attached is an OTF file that has this issue. BellweatherSans-Regular.zip

For the attached OTF, I investigated with TTX and the width information seems to be correct there (Looking for Latin Capital A to have an Advance Width of 1407): image

Also appears correctly in Font Forge: image

The Advance Width is not coming through when I try it locally, but here it is not working on https://opentype.js.org/glyph-inspector.html As you can see, Advance Width is having issues... but the maxes are as well, and the glyph is not drawing. In my local environment I do have access to the path information which is what I primarily use... but it seems like there is some bug with some calculation. image

Edit Oh snap locally I'm still using v0.11.0 😄 but the Glyph Inspector is using v1.3.3 - the issue appears for both versions... and probably all the versions in between?

azaslonov commented 4 years ago

Is this project abandoned?

zbryikt commented 3 years ago

Encountered similar issue here. I checked the code & CFF docs and found the root cause - basically it's the uninitialized _nominalWidthXthat caused this issue. Then I just realized it has been solved by @ZeroUltimax's pull request https://github.com/opentypejs/opentype.js/pull/445

Since the pull request is merged after 1.3.3, the fix is not released yet. Perhaps @Jolg42 can make a release v1.3.4?