Skip to content

Commit b2cf852

Browse files
committed
Change the CompiledFont glyph/charCode caches to use Maps
This code is old enough that it predates the general availability of `Map`, and these changes allow us to shorten the code a tiny bit.
1 parent dd7e373 commit b2cf852

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

src/core/font_renderer.js

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,10 @@ class Commands {
781781
}
782782

783783
class CompiledFont {
784+
#compiledGlyphs = new Map();
785+
786+
#compiledCharCodeToGlyphId = new Map();
787+
784788
constructor(fontMatrix) {
785789
if (
786790
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
@@ -789,9 +793,6 @@ class CompiledFont {
789793
unreachable("Cannot initialize CompiledFont.");
790794
}
791795
this.fontMatrix = fontMatrix;
792-
793-
this.compiledGlyphs = Object.create(null);
794-
this.compiledCharCodeToGlyphId = Object.create(null);
795796
}
796797

797798
static get NOOP() {
@@ -807,24 +808,20 @@ class CompiledFont {
807808

808809
getPathJs(unicode) {
809810
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
810-
let fn = this.compiledGlyphs[glyphId],
811-
compileEx;
812-
if (fn === undefined) {
811+
812+
const path = this.#compiledGlyphs.getOrInsertComputed(glyphId, () => {
813813
try {
814-
fn = this.compileGlyph(this.glyphs[glyphId], glyphId);
814+
return this.compileGlyph(this.glyphs[glyphId], glyphId);
815815
} catch (ex) {
816-
fn = CompiledFont.NOOP; // Avoid attempting to re-compile a corrupt glyph.
817-
818-
compileEx = ex;
816+
return ex; // Avoid attempting to re-compile a corrupt glyph.
819817
}
820-
this.compiledGlyphs[glyphId] = fn;
821-
}
822-
this.compiledCharCodeToGlyphId[charCode] ??= glyphId;
818+
});
819+
this.#compiledCharCodeToGlyphId.getOrInsert(charCode, glyphId);
823820

824-
if (compileEx) {
825-
throw compileEx;
821+
if (path instanceof Error) {
822+
throw path;
826823
}
827-
return fn;
824+
return path;
828825
}
829826

830827
compileGlyph(code, glyphId) {
@@ -861,8 +858,8 @@ class CompiledFont {
861858
hasBuiltPath(unicode) {
862859
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
863860
return (
864-
this.compiledGlyphs[glyphId] !== undefined &&
865-
this.compiledCharCodeToGlyphId[charCode] !== undefined
861+
this.#compiledGlyphs.has(glyphId) &&
862+
this.#compiledCharCodeToGlyphId.has(charCode)
866863
);
867864
}
868865
}

0 commit comments

Comments
 (0)