@react-pdf/textkit drops source character mapping when fontkit returns a glyph with empty codePoints
Summary
@react-pdf/[email protected] appears to drop glyph/source-character mapping when a font layout engine returns a glyph with codePoints: [].
This can happen with [email protected]: if a glyph is first loaded by glyph id via font.getGlyph(gid), a later font.layout(...) call can return that same glyph object with empty codePoints.
When textkit receives that glyph run, it drops the empty-codepoint glyph from glyphs, stringIndices, and glyphIndices.
Reproduction
Repro repo:
https://github.com/brandon-julio-t/react-pdf-textkit-codepoints-repro
Steps:
bun install
bun run repro
The repro uses:
@react-pdf/[email protected][email protected]- a real font file from
@fontsource/roboto
It first opens the font with fontkit, primes one glyph by glyph id, then lays out "AB".
Actual Output
fontkit glyph.codePoints after getGlyph(gid): [ [], [ 66 ] ]
Text: AB
Expected codePoints: [ 65, 66 ]
Actual textkit glyph.codePoints: [ [ 66 ] ]
Actual textkit stringIndices: [ 0 ]
Actual textkit glyphIndices: [ 0 ]
AssertionError [ERR_ASSERTION]: textkit should preserve source character mapping when fontkit returns a glyph with empty codePoints
The first character, A / code point 65, is lost from the final textkit glyph mapping.
Expected Behavior
Textkit should preserve source-character mapping when font.layout(...) returns a glyph with missing/empty codePoints, at least when the glyph sequence can still be aligned unambiguously against the original run string.
Expected output for the repro:
Actual textkit glyph.codePoints: [ [ 65 ], [ 66 ] ]
Actual textkit stringIndices: [ 0, 1 ]
Actual textkit glyphIndices: [ 0, 1 ]
Notes
This repro does not involve PDF rendering. It isolates the behavior to textkit's handling of a fontkit glyph run.
End-to-end PDF reproduction is difficult because the symptom depends on prior fontkit glyph-cache state in a long-running process. The repro makes that state explicit by calling font.getGlyph(gid) before font.layout(...), so the textkit behavior is deterministic.
Production Context
We observed this in a long-running Node/Bun service that generates PDFs with @react-pdf/[email protected], @react-pdf/[email protected], and [email protected].
Our PDFs use NotoSansSC-Regular.ttf for Chinese text. The visible symptom was intermittent missing leading Chinese characters in rendered PDFs, for example the first character of an item name such as 洗水... or 六... disappearing from the PDF text mapping/rendered output.
The source data and React PDF input strings were intact. Regenerating after a process restart restored the missing characters, which made the issue appear random at the PDF level. The isolated repro above makes the underlying fontkit glyph-cache state deterministic.
A local patch that normalizes missing glyph codePoints from the original run string fixes the repro and avoids mutating fontkit's cached glyph objects.
We are experiencing the same issue with a randomly disappearing first letter 🙏 (version 4.4.1)
We're also experiencing this issue, first random letters disappear.
We just finished migrating a working pdf generation with puppeteer to react-pdf/renderer, but now this little issue is blocking us from shipping.
We're experiencing the same issue - the first character is occasionally missing. @brandon-julio-t , are there any plans to merge your fix?
We're experiencing the same issue - the first character is occasionally missing. @brandon-julio-t , are there any plans to merge your fix?
As much as I’m eager to get this merged, PR #3405 is still awaiting maintainer review, and I don’t have merge permissions. @diegomura, when you have a chance, could you take a look?
In the meantime, below is a package-level version of the fix proposed in PR #3405, adapted for the published @react-pdf/[email protected] package. We’ve been running this patch in production and apply it using Bun’s patchedDependencies. If you use another package manager, the same diff can be adapted to its equivalent patching mechanism.
If you try it, please share whether it resolves the issue for you, along with your renderer, textkit, and fontkit versions.
Patch for @react-pdf/[email protected]
diff --git a/lib/textkit.js b/lib/textkit.js
index 0b8b6fa636c48ef0ac9cecb27040d2bac9303ce8..e3e96d869645de17964c6ea63c68067a4e9067b3 100644
--- a/lib/textkit.js
+++ b/lib/textkit.js
@@ -1107,6 +1107,80 @@ const resolve = (glyphs = []) => {
return result;
};
+const codePointsFromString = (string) => {
+ const result = [];
+ for (const char of string) {
+ const codePoint = char.codePointAt(0);
+ if (codePoint !== undefined)
+ result.push(codePoint);
+ }
+ return result;
+};
+const sequenceStartsAt = (values, sequence, start) => sequence.every((value, index) => values[start + index] === value);
+const findSequence = (values, sequence, start) => {
+ if (sequence.length === 0)
+ return start;
+ for (let i = start; i <= values.length - sequence.length; i += 1) {
+ if (sequenceStartsAt(values, sequence, i))
+ return i;
+ }
+ return -1;
+};
+const cloneGlyph = (glyph, codePoints) => Object.assign(Object.create(Object.getPrototypeOf(glyph)), glyph, {
+ codePoints,
+});
+const assignPendingCodePoints = (glyphs, pendingGlyphs, codePoints) => {
+ if (pendingGlyphs.length === 0 || codePoints.length === 0)
+ return;
+ let codePointIndex = 0;
+ for (let i = 0; i < pendingGlyphs.length; i += 1) {
+ const remainingGlyphs = pendingGlyphs.length - i;
+ const remainingCodePoints = codePoints.length - codePointIndex;
+ const length = Math.max(1, remainingCodePoints - remainingGlyphs + 1);
+ const glyphIndex = pendingGlyphs[i];
+ glyphs[glyphIndex] = cloneGlyph(glyphs[glyphIndex], codePoints.slice(codePointIndex, codePointIndex + length));
+ codePointIndex += length;
+ }
+};
+const normalizeGlyphCodePoints = (glyphs, string) => {
+ const codePoints = codePointsFromString(string);
+ const result = [...glyphs];
+ const pendingGlyphs = [];
+ let isAligned = true;
+ let cursor = 0;
+ for (let i = 0; i < glyphs.length; i += 1) {
+ const glyph = glyphs[i];
+ const glyphCodePoints = glyph.codePoints || [];
+ if (!isAligned)
+ continue;
+ if (glyphCodePoints.length === 0) {
+ pendingGlyphs.push(i);
+ continue;
+ }
+ if (pendingGlyphs.length > 0) {
+ const foundIndex = findSequence(codePoints, glyphCodePoints, cursor);
+ if (foundIndex < cursor) {
+ pendingGlyphs.length = 0;
+ isAligned = false;
+ continue;
+ }
+ assignPendingCodePoints(result, pendingGlyphs, codePoints.slice(cursor, foundIndex));
+ pendingGlyphs.length = 0;
+ cursor = foundIndex + glyphCodePoints.length;
+ continue;
+ }
+ if (sequenceStartsAt(codePoints, glyphCodePoints, cursor)) {
+ cursor += glyphCodePoints.length;
+ }
+ else {
+ isAligned = false;
+ }
+ }
+ if (isAligned) {
+ assignPendingCodePoints(result, pendingGlyphs, codePoints.slice(cursor));
+ }
+ return result;
+};
const getCharacterSpacing = (run) => {
return run.attributes?.characterSpacing || 0;
};
@@ -1157,15 +1231,16 @@ const layoutRun = (string) => {
throw new Error('Invalid font');
// passing LTR To force fontkit to not reverse the string
const glyphRun = font[0].layout(runString, undefined, undefined, undefined, 'ltr');
+ const glyphs = normalizeGlyphCodePoints(glyphRun.glyphs, runString);
const positions = scalePositions(run, glyphRun.positions);
- const stringIndices = resolve$1(glyphRun.glyphs);
- const glyphIndices = resolve(glyphRun.glyphs);
+ const stringIndices = resolve$1(glyphs);
+ const glyphIndices = resolve(glyphs);
const result = {
...run,
positions,
stringIndices,
glyphIndices,
- glyphs: glyphRun.glyphs,
+ glyphs,
};
return result;
};
We are suffering, too. Please @diegomura , I send you some good karma - or how can we support you further!?
Hi, we have encountered the same problem while rendering PDFs. A fixed version would be much appreciated!
We have the same issue in browser !
quick n dirty [email protected] patch that fixed it for us for now:
Patch [email protected]
diff --git a/dist/browser-module.mjs b/dist/browser-module.mjs
index 4d0a389de8a3db767f1fef99ed186790431aa844..76e72f1225dedfa0375944412206eaccc85beed8 100644
--- a/dist/browser-module.mjs
+++ b/dist/browser-module.mjs
@@ -12673,7 +12673,12 @@ class $4c1709dee528ea76$export$2e2bcd8739ae039 {
else if (this.directory.tables.COLR && this.directory.tables.CPAL) this._glyphs[glyph] = new (0, $0d411f0165859681$export$2e2bcd8739ae039)(glyph, characters, this);
else this._getBaseGlyph(glyph, characters);
}
- return this._glyphs[glyph] || null;
+ const cachedGlyph = this._glyphs[glyph];
+ if (cachedGlyph && characters.length > 0 && !cachedGlyph.codePoints?.length) cachedGlyph.codePoints = characters;
+ return cachedGlyph || null;
}
/**
* Returns a Subset for this font.
diff --git a/dist/browser.cjs b/dist/browser.cjs
index a5fa901b5608575f84390e3ef0474a7789e8269a..50463a03b6f62c11d31aa1f5dc38ee2487d7c57d 100644
--- a/dist/browser.cjs
+++ b/dist/browser.cjs
@@ -12690,7 +12690,12 @@ class $0a8ef2660a6ce4b6$export$2e2bcd8739ae039 {
else if (this.directory.tables.COLR && this.directory.tables.CPAL) this._glyphs[glyph] = new (0, $42d9dbd2de9ee2d8$export$2e2bcd8739ae039)(glyph, characters, this);
else this._getBaseGlyph(glyph, characters);
}
- return this._glyphs[glyph] || null;
+ const cachedGlyph = this._glyphs[glyph];
+ if (cachedGlyph && characters.length > 0 && !cachedGlyph.codePoints?.length) cachedGlyph.codePoints = characters;
+ return cachedGlyph || null;
}
/**
* Returns a Subset for this font.
diff --git a/dist/main.cjs b/dist/main.cjs
index 851b2d2bbcae17e91f9f2468f61112ee2dc8fbd8..50235306a6b6733aaaa947799aa5debd0ddb029f 100644
--- a/dist/main.cjs
+++ b/dist/main.cjs
@@ -12711,7 +12711,12 @@ class $0a8ef2660a6ce4b6$export$2e2bcd8739ae039 {
else if (this.directory.tables.COLR && this.directory.tables.CPAL) this._glyphs[glyph] = new (0, $42d9dbd2de9ee2d8$export$2e2bcd8739ae039)(glyph, characters, this);
else this._getBaseGlyph(glyph, characters);
}
- return this._glyphs[glyph] || null;
+ const cachedGlyph = this._glyphs[glyph];
+ if (cachedGlyph && characters.length > 0 && !cachedGlyph.codePoints?.length) cachedGlyph.codePoints = characters;
+ return cachedGlyph || null;
}
/**
* Returns a Subset for this font.
diff --git a/dist/module.mjs b/dist/module.mjs
index fde88fc3f23e6fff533da842186fee8402cbcde1..98ed4c6e0f4f5c26dd44832709439f23e040516d 100644
--- a/dist/module.mjs
+++ b/dist/module.mjs
@@ -12694,7 +12694,12 @@ class $4c1709dee528ea76$export$2e2bcd8739ae039 {
else if (this.directory.tables.COLR && this.directory.tables.CPAL) this._glyphs[glyph] = new (0, $0d411f0165859681$export$2e2bcd8739ae039)(glyph, characters, this);
else this._getBaseGlyph(glyph, characters);
}
- return this._glyphs[glyph] || null;
+ const cachedGlyph = this._glyphs[glyph];
+ if (cachedGlyph && characters.length > 0 && !cachedGlyph.codePoints?.length) cachedGlyph.codePoints = characters;
+ return cachedGlyph || null;
}
/**
* Returns a Subset for this font.