mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-26 00:47:21 +02:00
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.
This commit is contained in:
parent
dd7e3731d1
commit
b2cf8527c6
@ -781,6 +781,10 @@ class Commands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class CompiledFont {
|
class CompiledFont {
|
||||||
|
#compiledGlyphs = new Map();
|
||||||
|
|
||||||
|
#compiledCharCodeToGlyphId = new Map();
|
||||||
|
|
||||||
constructor(fontMatrix) {
|
constructor(fontMatrix) {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
|
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
|
||||||
@ -789,9 +793,6 @@ class CompiledFont {
|
|||||||
unreachable("Cannot initialize CompiledFont.");
|
unreachable("Cannot initialize CompiledFont.");
|
||||||
}
|
}
|
||||||
this.fontMatrix = fontMatrix;
|
this.fontMatrix = fontMatrix;
|
||||||
|
|
||||||
this.compiledGlyphs = Object.create(null);
|
|
||||||
this.compiledCharCodeToGlyphId = Object.create(null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static get NOOP() {
|
static get NOOP() {
|
||||||
@ -807,24 +808,20 @@ class CompiledFont {
|
|||||||
|
|
||||||
getPathJs(unicode) {
|
getPathJs(unicode) {
|
||||||
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
|
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
|
||||||
let fn = this.compiledGlyphs[glyphId],
|
|
||||||
compileEx;
|
const path = this.#compiledGlyphs.getOrInsertComputed(glyphId, () => {
|
||||||
if (fn === undefined) {
|
|
||||||
try {
|
try {
|
||||||
fn = this.compileGlyph(this.glyphs[glyphId], glyphId);
|
return this.compileGlyph(this.glyphs[glyphId], glyphId);
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
fn = CompiledFont.NOOP; // Avoid attempting to re-compile a corrupt glyph.
|
return ex; // Avoid attempting to re-compile a corrupt glyph.
|
||||||
|
|
||||||
compileEx = ex;
|
|
||||||
}
|
}
|
||||||
this.compiledGlyphs[glyphId] = fn;
|
});
|
||||||
}
|
this.#compiledCharCodeToGlyphId.getOrInsert(charCode, glyphId);
|
||||||
this.compiledCharCodeToGlyphId[charCode] ??= glyphId;
|
|
||||||
|
|
||||||
if (compileEx) {
|
if (path instanceof Error) {
|
||||||
throw compileEx;
|
throw path;
|
||||||
}
|
}
|
||||||
return fn;
|
return path;
|
||||||
}
|
}
|
||||||
|
|
||||||
compileGlyph(code, glyphId) {
|
compileGlyph(code, glyphId) {
|
||||||
@ -861,8 +858,8 @@ class CompiledFont {
|
|||||||
hasBuiltPath(unicode) {
|
hasBuiltPath(unicode) {
|
||||||
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
|
const { charCode, glyphId } = lookupCmap(this.cmap, unicode);
|
||||||
return (
|
return (
|
||||||
this.compiledGlyphs[glyphId] !== undefined &&
|
this.#compiledGlyphs.has(glyphId) &&
|
||||||
this.compiledCharCodeToGlyphId[charCode] !== undefined
|
this.#compiledCharCodeToGlyphId.has(charCode)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user