foliojs / fontkit

An advanced font engine for Node and the browser
1.45k stars 213 forks source link

RangeError [ERR_OUT_OF_RANGE] when using only the ideographic space from a font #242

Open arista opened 3 years ago

arista commented 3 years ago

When using a font, but only using the ideographic space (\u3000) from that font, an exception is thrown. It seems to be an interaction between svg-to-pdfkit and fontkit, and it's not clear where the bug belongs, but I'm filing it here since this package is closest to the thrown exception.

Minimal node test case:

const PDFDocument = require("pdfkit");
const SVGtoPDF = require("svg-to-pdfkit");
const fs = require("fs");
const doc = new PDFDocument();
doc.registerFont("NotoSansKR", fs.readFileSync("./NotoSansKR-Regular.otf"));
const svg = `<svg xmlns="http://www.w3.org/2000/svg"><text x="200" y="200" font-family="NotoSansKR">&#x3000;</text></svg>`;
SVGtoPDF(doc, svg);
doc.pipe(fs.createWriteStream("output.pdf"));
doc.end();

Note that &#x3000; is an "ideographic space" from the korean font, and counts as a space for JS's regexp matching (i.e., \s will match it). It's the only character used here. When run, it exits with this error:

(node:11822) 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:724:11)
    at Buffer.writeUInt8 (internal/buffer.js:734:10)
    at EncodeStream.<computed> [as writeUInt8] (/home/arista/tmp/noteflight/bug-8729/node_modules/restructure/src/EncodeStream.js:37:29)
    at NumberT.encode (/home/arista/tmp/noteflight/bug-8729/node_modules/restructure/src/Number.js:28:39)
    at Struct.encode (/home/arista/tmp/noteflight/bug-8729/node_modules/restructure/src/Struct.js:110:16)
    at RangeArray.ArrayT.encode (/home/arista/tmp/noteflight/bug-8729/node_modules/restructure/src/Array.js:88:19)
    at VersionedStruct.encode (/home/arista/tmp/noteflight/bug-8729/node_modules/restructure/src/VersionedStruct.js:120:16)
    at VersionedStruct.encode (/home/arista/tmp/noteflight/bug-8729/node_modules/restructure/src/VersionedStruct.js:126:18)
    at CFFSubset.encode (/home/arista/tmp/noteflight/bug-8729/node_modules/fontkit/index.js:13585:12)
    at /home/arista/tmp/noteflight/bug-8729/node_modules/fontkit/index.js:13048:13

What I think is happening: