Skip to content

Commit 4bae676

Browse files
Merge pull request #20835 from Snuffleupagus/return-iterators
Use iterators a little bit more, to avoid creating temporary Arrays
2 parents cc680f6 + dbb6ffb commit 4bae676

3 files changed

Lines changed: 13 additions & 11 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
/**

src/display/annotation_layer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4100,7 +4100,7 @@ class AnnotationLayer {
41004100
}
41014101

41024102
getEditableAnnotations() {
4103-
return Array.from(this.#editableAnnotations.values());
4103+
return this.#editableAnnotations.values();
41044104
}
41054105

41064106
getEditableAnnotation(id) {

src/display/editor/annotation_editor_layer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,7 @@ class AnnotationEditorLayer {
416416
}
417417

418418
// Show the annotations that were hidden in enable().
419-
const editables = annotationLayer.getEditableAnnotations();
420-
for (const editable of editables) {
419+
for (const editable of annotationLayer.getEditableAnnotations()) {
421420
const { id } = editable.data;
422421
if (this.#uiManager.isDeletedAnnotationElement(id)) {
423422
editable.updateEdited({ deleted: true });

0 commit comments

Comments
 (0)