From b2cf8527c6cf53051aed1da8a5f46aeed45692e2 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 18 Jul 2026 11:44:27 +0200 Subject: [PATCH] Change the `CompiledFont` glyph/charCode caches to use `Map`s 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. --- src/core/font_renderer.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/src/core/font_renderer.js b/src/core/font_renderer.js index 10cbbac0c..226d43f87 100644 --- a/src/core/font_renderer.js +++ b/src/core/font_renderer.js @@ -781,6 +781,10 @@ class Commands { } class CompiledFont { + #compiledGlyphs = new Map(); + + #compiledCharCodeToGlyphId = new Map(); + constructor(fontMatrix) { if ( (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) && @@ -789,9 +793,6 @@ class CompiledFont { unreachable("Cannot initialize CompiledFont."); } this.fontMatrix = fontMatrix; - - this.compiledGlyphs = Object.create(null); - this.compiledCharCodeToGlyphId = Object.create(null); } static get NOOP() { @@ -807,24 +808,20 @@ class CompiledFont { getPathJs(unicode) { const { charCode, glyphId } = lookupCmap(this.cmap, unicode); - let fn = this.compiledGlyphs[glyphId], - compileEx; - if (fn === undefined) { + + const path = this.#compiledGlyphs.getOrInsertComputed(glyphId, () => { try { - fn = this.compileGlyph(this.glyphs[glyphId], glyphId); + return this.compileGlyph(this.glyphs[glyphId], glyphId); } catch (ex) { - fn = CompiledFont.NOOP; // Avoid attempting to re-compile a corrupt glyph. - - compileEx = ex; + return ex; // Avoid attempting to re-compile a corrupt glyph. } - this.compiledGlyphs[glyphId] = fn; - } - this.compiledCharCodeToGlyphId[charCode] ??= glyphId; + }); + this.#compiledCharCodeToGlyphId.getOrInsert(charCode, glyphId); - if (compileEx) { - throw compileEx; + if (path instanceof Error) { + throw path; } - return fn; + return path; } compileGlyph(code, glyphId) { @@ -861,8 +858,8 @@ class CompiledFont { hasBuiltPath(unicode) { const { charCode, glyphId } = lookupCmap(this.cmap, unicode); return ( - this.compiledGlyphs[glyphId] !== undefined && - this.compiledCharCodeToGlyphId[charCode] !== undefined + this.#compiledGlyphs.has(glyphId) && + this.#compiledCharCodeToGlyphId.has(charCode) ); } }