Merge pull request #20835 from Snuffleupagus/return-iterators

Use iterators a little bit more, to avoid creating temporary Arrays
This commit is contained in:
Jonas Jenwald 2026-03-09 20:07:19 +01:00 committed by GitHub
commit 4bae676af1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 13 additions and 11 deletions

View File

@ -969,6 +969,10 @@ function createNameTable(name, proto) {
* decoding logics whatever type it is (assuming the font type is supported). * decoding logics whatever type it is (assuming the font type is supported).
*/ */
class Font { class Font {
#charsCache = new Map();
#glyphCache = new Map();
constructor(name, file, properties, evaluatorOptions) { constructor(name, file, properties, evaluatorOptions) {
this.name = name; this.name = name;
this.psName = null; this.psName = null;
@ -981,9 +985,6 @@ class Font {
this.missingFile = false; this.missingFile = false;
this.cssFontInfo = properties.cssFontInfo; this.cssFontInfo = properties.cssFontInfo;
this._charsCache = Object.create(null);
this._glyphCache = Object.create(null);
let isSerifFont = !!(properties.flags & FontFlags.Serif); let isSerifFont = !!(properties.flags & FontFlags.Serif);
// Fallback to checking the font name, in order to improve text-selection, // Fallback to checking the font name, in order to improve text-selection,
// since the /Flags-entry is often wrong (fixes issue13845.pdf). // since the /Flags-entry is often wrong (fixes issue13845.pdf).
@ -3385,7 +3386,7 @@ class Font {
* @private * @private
*/ */
_charToGlyph(charcode, isSpace = false) { _charToGlyph(charcode, isSpace = false) {
let glyph = this._glyphCache[charcode]; let glyph = this.#glyphCache.get(charcode);
// All `Glyph`-properties, except `isSpace` in multi-byte strings, // All `Glyph`-properties, except `isSpace` in multi-byte strings,
// depend indirectly on the `charcode`. // depend indirectly on the `charcode`.
if (glyph?.isSpace === isSpace) { if (glyph?.isSpace === isSpace) {
@ -3480,12 +3481,13 @@ class Font {
isSpace, isSpace,
isInFont isInFont
); );
return (this._glyphCache[charcode] = glyph); this.#glyphCache.set(charcode, glyph);
return glyph;
} }
charsToGlyphs(chars) { charsToGlyphs(chars) {
// If we translated this string before, just grab it from the cache. // If we translated this string before, just grab it from the cache.
let glyphs = this._charsCache[chars]; let glyphs = this.#charsCache.get(chars);
if (glyphs) { if (glyphs) {
return glyphs; return glyphs;
} }
@ -3517,7 +3519,8 @@ class Font {
} }
// Enter the translated string into the cache. // Enter the translated string into the cache.
return (this._charsCache[chars] = glyphs); this.#charsCache.set(chars, glyphs);
return glyphs;
} }
/** /**
@ -3549,7 +3552,7 @@ class Font {
} }
get glyphCacheValues() { get glyphCacheValues() {
return Object.values(this._glyphCache); return this.#glyphCache.values();
} }
/** /**

View File

@ -4100,7 +4100,7 @@ class AnnotationLayer {
} }
getEditableAnnotations() { getEditableAnnotations() {
return Array.from(this.#editableAnnotations.values()); return this.#editableAnnotations.values();
} }
getEditableAnnotation(id) { getEditableAnnotation(id) {

View File

@ -416,8 +416,7 @@ class AnnotationEditorLayer {
} }
// Show the annotations that were hidden in enable(). // Show the annotations that were hidden in enable().
const editables = annotationLayer.getEditableAnnotations(); for (const editable of annotationLayer.getEditableAnnotations()) {
for (const editable of editables) {
const { id } = editable.data; const { id } = editable.data;
if (this.#uiManager.isDeletedAnnotationElement(id)) { if (this.#uiManager.isDeletedAnnotationElement(id)) {
editable.updateEdited({ deleted: true }); editable.updateEdited({ deleted: true });