foliojs / fontkit

An advanced font engine for Node and the browser
1.46k stars 219 forks source link

RangeError [ERR_OUT_OF_RANGE] when rendering emojis #232

Open canda opened 4 years ago

canda commented 4 years ago

While using pdfkit, every once in a while I would get this error and the pdf would not come out at all

UnhandledPromiseRejectionWarning: RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= 0 and <= 255. Received -1
    at writeU_Int8 (internal/buffer.js:555:11)
    at Buffer.writeUInt8 (internal/buffer.js:565:10)
    at EncodeStream.(anonymous function) [as writeUInt8] (/Users/canda/projects/mural/fontkit/node_modules/restructure/src/EncodeStream.js:37:29)
    at NumberT.encode (/Users/canda/projects/mural/fontkit/node_modules/restructure/src/Number.js:28:39)
    at Struct.encode (/Users/canda/projects/mural/fontkit/node_modules/restructure/src/Struct.js:110:16)
    at RangeArray.ArrayT.encode (/Users/canda/projects/mural/fontkit/node_modules/restructure/src/Array.js:88:19)
    at VersionedStruct.encode (/Users/canda/projects/mural/fontkit/node_modules/restructure/src/VersionedStruct.js:120:16)
    at VersionedStruct.encode (/Users/canda/projects/mural/fontkit/node_modules/restructure/src/VersionedStruct.js:126:18)
    at CFFSubset.encode (/Users/canda/projects/mural/fontkit/index.js:13585:12)
    at /Users/canda/projects/mural/fontkit/index.js:13048:13

I narrowed down to fontkit with this short repro.

const fs = require("fs");
const notosans = require("fontkit").openSync("./NotoSansCJKsc-Regular.otf");
const subsetFor = (text, file) => {
  const run = notosans.layout(text);
  const subset = notosans.createSubset();
  run.glyphs.forEach((glyph) => subset.includeGlyph(glyph));
  subset.encodeStream().pipe(fs.createWriteStream(file));
};
subsetFor("🥘", "output.ttf");

This is the same error I would get if I try to create a subset from an empty array. subsetFor("", "output.ttf");

canda commented 4 years ago

Is it a possible workaround to always add some random glyph to the subset inside the encode function? What do you think?

FlandersBurger commented 3 years ago

Any update on this one? I'm having the same sporadic issue