Skip to content

Commit 0ded85e

Browse files
committed
Add a Page helper method to create a PartialEvaluator-instance
Currently we repeat the same identical code five times in the `Page`-class when creating a `PartialEvaluator`-instance, which given the number of parameters it needs seems like unnecessary duplication.
1 parent 6f05231 commit 0ded85e

File tree

1 file changed

+21
-66
lines changed

1 file changed

+21
-66
lines changed

src/core/document.js

Lines changed: 21 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,22 @@ class Page {
125125
};
126126
}
127127

128+
#createPartialEvaluator(handler) {
129+
return new PartialEvaluator({
130+
xref: this.xref,
131+
handler,
132+
pageIndex: this.pageIndex,
133+
idFactory: this._localIdFactory,
134+
fontCache: this.fontCache,
135+
builtInCMapCache: this.builtInCMapCache,
136+
standardFontDataCache: this.standardFontDataCache,
137+
globalColorSpaceCache: this.globalColorSpaceCache,
138+
globalImageCache: this.globalImageCache,
139+
systemFontCache: this.systemFontCache,
140+
options: this.evaluatorOptions,
141+
});
142+
}
143+
128144
/**
129145
* @private
130146
*/
@@ -322,20 +338,7 @@ class Page {
322338
if (this.xfaFactory) {
323339
throw new Error("XFA: Cannot save new annotations.");
324340
}
325-
326-
const partialEvaluator = new PartialEvaluator({
327-
xref: this.xref,
328-
handler,
329-
pageIndex: this.pageIndex,
330-
idFactory: this._localIdFactory,
331-
fontCache: this.fontCache,
332-
builtInCMapCache: this.builtInCMapCache,
333-
standardFontDataCache: this.standardFontDataCache,
334-
globalColorSpaceCache: this.globalColorSpaceCache,
335-
globalImageCache: this.globalImageCache,
336-
systemFontCache: this.systemFontCache,
337-
options: this.evaluatorOptions,
338-
});
341+
const partialEvaluator = this.#createPartialEvaluator(handler);
339342

340343
const deletedAnnotations = new RefSetCache();
341344
const existingAnnotations = new RefSet();
@@ -378,19 +381,7 @@ class Page {
378381
}
379382

380383
async save(handler, task, annotationStorage, changes) {
381-
const partialEvaluator = new PartialEvaluator({
382-
xref: this.xref,
383-
handler,
384-
pageIndex: this.pageIndex,
385-
idFactory: this._localIdFactory,
386-
fontCache: this.fontCache,
387-
builtInCMapCache: this.builtInCMapCache,
388-
standardFontDataCache: this.standardFontDataCache,
389-
globalColorSpaceCache: this.globalColorSpaceCache,
390-
globalImageCache: this.globalImageCache,
391-
systemFontCache: this.systemFontCache,
392-
options: this.evaluatorOptions,
393-
});
384+
const partialEvaluator = this.#createPartialEvaluator(handler);
394385

395386
// Fetch the page's annotations and save the content
396387
// in case of interactive form fields.
@@ -450,19 +441,7 @@ class Page {
450441
const contentStreamPromise = this.getContentStream();
451442
const resourcesPromise = this.loadResources(RESOURCES_KEYS_OPERATOR_LIST);
452443

453-
const partialEvaluator = new PartialEvaluator({
454-
xref: this.xref,
455-
handler,
456-
pageIndex: this.pageIndex,
457-
idFactory: this._localIdFactory,
458-
fontCache: this.fontCache,
459-
builtInCMapCache: this.builtInCMapCache,
460-
standardFontDataCache: this.standardFontDataCache,
461-
globalColorSpaceCache: this.globalColorSpaceCache,
462-
globalImageCache: this.globalImageCache,
463-
systemFontCache: this.systemFontCache,
464-
options: this.evaluatorOptions,
465-
});
444+
const partialEvaluator = this.#createPartialEvaluator(handler);
466445

467446
const newAnnotsByPage = !this.xfaFactory
468447
? getNewAnnotationsMap(annotationStorage)
@@ -670,19 +649,7 @@ class Page {
670649
RESOURCES_KEYS_TEXT_CONTENT
671650
);
672651

673-
const partialEvaluator = new PartialEvaluator({
674-
xref: this.xref,
675-
handler,
676-
pageIndex: this.pageIndex,
677-
idFactory: this._localIdFactory,
678-
fontCache: this.fontCache,
679-
builtInCMapCache: this.builtInCMapCache,
680-
standardFontDataCache: this.standardFontDataCache,
681-
globalColorSpaceCache: this.globalColorSpaceCache,
682-
globalImageCache: this.globalImageCache,
683-
systemFontCache: this.systemFontCache,
684-
options: this.evaluatorOptions,
685-
});
652+
const partialEvaluator = this.#createPartialEvaluator(handler);
686653

687654
return partialEvaluator.getTextContent({
688655
stream: contentStream,
@@ -751,19 +718,7 @@ class Page {
751718
}
752719

753720
if (annotation.hasTextContent && isVisible) {
754-
partialEvaluator ||= new PartialEvaluator({
755-
xref: this.xref,
756-
handler,
757-
pageIndex: this.pageIndex,
758-
idFactory: this._localIdFactory,
759-
fontCache: this.fontCache,
760-
builtInCMapCache: this.builtInCMapCache,
761-
standardFontDataCache: this.standardFontDataCache,
762-
globalColorSpaceCache: this.globalColorSpaceCache,
763-
globalImageCache: this.globalImageCache,
764-
systemFontCache: this.systemFontCache,
765-
options: this.evaluatorOptions,
766-
});
721+
partialEvaluator ??= this.#createPartialEvaluator(handler);
767722

768723
textContentPromises.push(
769724
annotation

0 commit comments

Comments
 (0)