Skip to content

Commit dbb6ffb

Browse files
committed
Change the Font.prototype.glyphCacheValues method to return an iterator
This method is only used with loops, and it should be a tiny bit more efficient to use an iterator directly rather than first iterating through the underlying data to create a temporary `Array` that we finally iterate through at the call-site. *Please note:* As port of these changes the chars/glyph caches, on the `Font` instances, are changed to use `Map`s rather than Objects.
1 parent 8bbb7c8 commit dbb6ffb

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

src/core/fonts.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,10 @@ function createNameTable(name, proto) {
969969
* decoding logics whatever type it is (assuming the font type is supported).
970970
*/
971971
class Font {
972+
#charsCache = new Map();
973+
974+
#glyphCache = new Map();
975+
972976
constructor(name, file, properties, evaluatorOptions) {
973977
this.name = name;
974978
this.psName = null;
@@ -981,9 +985,6 @@ class Font {
981985
this.missingFile = false;
982986
this.cssFontInfo = properties.cssFontInfo;
983987

984-
this._charsCache = Object.create(null);
985-
this._glyphCache = Object.create(null);
986-
987988
let isSerifFont = !!(properties.flags & FontFlags.Serif);
988989
// Fallback to checking the font name, in order to improve text-selection,
989990
// since the /Flags-entry is often wrong (fixes issue13845.pdf).
@@ -3385,7 +3386,7 @@ class Font {
33853386
* @private
33863387
*/
33873388
_charToGlyph(charcode, isSpace = false) {
3388-
let glyph = this._glyphCache[charcode];
3389+
let glyph = this.#glyphCache.get(charcode);
33893390
// All `Glyph`-properties, except `isSpace` in multi-byte strings,
33903391
// depend indirectly on the `charcode`.
33913392
if (glyph?.isSpace === isSpace) {
@@ -3480,12 +3481,13 @@ class Font {
34803481
isSpace,
34813482
isInFont
34823483
);
3483-
return (this._glyphCache[charcode] = glyph);
3484+
this.#glyphCache.set(charcode, glyph);
3485+
return glyph;
34843486
}
34853487

34863488
charsToGlyphs(chars) {
34873489
// If we translated this string before, just grab it from the cache.
3488-
let glyphs = this._charsCache[chars];
3490+
let glyphs = this.#charsCache.get(chars);
34893491
if (glyphs) {
34903492
return glyphs;
34913493
}
@@ -3517,7 +3519,8 @@ class Font {
35173519
}
35183520

35193521
// Enter the translated string into the cache.
3520-
return (this._charsCache[chars] = glyphs);
3522+
this.#charsCache.set(chars, glyphs);
3523+
return glyphs;
35213524
}
35223525

35233526
/**
@@ -3549,7 +3552,7 @@ class Font {
35493552
}
35503553

35513554
get glyphCacheValues() {
3552-
return Object.values(this._glyphCache);
3555+
return this.#glyphCache.values();
35533556
}
35543557

35553558
/**

0 commit comments

Comments
 (0)