diff --git a/src/core/document.js b/src/core/document.js index 691ba8802cee7..505cd9a75834b 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -436,77 +436,78 @@ class Page { options: this.evaluatorOptions, }); - const newAnnotationsByPage = !this.xfaFactory + const newAnnotsByPage = !this.xfaFactory ? getNewAnnotationsMap(annotationStorage) : null; + const newAnnots = newAnnotsByPage?.get(this.pageIndex); + let newAnnotationsPromise = Promise.resolve(null); let deletedAnnotations = null; - let newAnnotationsPromise = Promise.resolve(null); - if (newAnnotationsByPage) { - const newAnnotations = newAnnotationsByPage.get(this.pageIndex); - if (newAnnotations) { - const annotationGlobalsPromise = - this.pdfManager.ensureDoc("annotationGlobals"); - let imagePromises; - - // An annotation can contain a reference to a bitmap, but this bitmap - // is defined in another annotation. So we need to find this annotation - // and generate the bitmap. - const missingBitmaps = new Set(); - for (const { bitmapId, bitmap } of newAnnotations) { - if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) { - missingBitmaps.add(bitmapId); - } + if (newAnnots) { + const annotationGlobalsPromise = + this.pdfManager.ensureDoc("annotationGlobals"); + let imagePromises; + + // An annotation can contain a reference to a bitmap, but this bitmap + // is defined in another annotation. So we need to find this annotation + // and generate the bitmap. + const missingBitmaps = new Set(); + for (const { bitmapId, bitmap } of newAnnots) { + if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) { + missingBitmaps.add(bitmapId); } + } - const { isOffscreenCanvasSupported } = this.evaluatorOptions; - if (missingBitmaps.size > 0) { - const annotationWithBitmaps = newAnnotations.slice(); - for (const [key, annotation] of annotationStorage) { - if (!key.startsWith(AnnotationEditorPrefix)) { - continue; - } - if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) { - annotationWithBitmaps.push(annotation); - } + const { isOffscreenCanvasSupported } = this.evaluatorOptions; + if (missingBitmaps.size > 0) { + const annotationWithBitmaps = newAnnots.slice(); + for (const [key, annotation] of annotationStorage) { + if (!key.startsWith(AnnotationEditorPrefix)) { + continue; + } + if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) { + annotationWithBitmaps.push(annotation); } - // The array annotationWithBitmaps cannot be empty: the check above - // makes sure to have at least one annotation containing the bitmap. - imagePromises = AnnotationFactory.generateImages( - annotationWithBitmaps, - this.xref, - isOffscreenCanvasSupported - ); - } else { - imagePromises = AnnotationFactory.generateImages( - newAnnotations, - this.xref, - isOffscreenCanvasSupported - ); } + // The array annotationWithBitmaps cannot be empty: the check above + // makes sure to have at least one annotation containing the bitmap. + imagePromises = AnnotationFactory.generateImages( + annotationWithBitmaps, + this.xref, + isOffscreenCanvasSupported + ); + } else { + imagePromises = AnnotationFactory.generateImages( + newAnnots, + this.xref, + isOffscreenCanvasSupported + ); + } - deletedAnnotations = new RefSet(); - this.#replaceIdByRef(newAnnotations, deletedAnnotations, null); + deletedAnnotations = new RefSet(); + this.#replaceIdByRef(newAnnots, deletedAnnotations, null); - newAnnotationsPromise = annotationGlobalsPromise.then( - annotationGlobals => { - if (!annotationGlobals) { - return null; - } - - return AnnotationFactory.printNewAnnotations( - annotationGlobals, - partialEvaluator, - task, - newAnnotations, - imagePromises - ); + newAnnotationsPromise = annotationGlobalsPromise.then( + annotationGlobals => { + if (!annotationGlobals) { + return null; } - ); - } + + return AnnotationFactory.printNewAnnotations( + annotationGlobals, + partialEvaluator, + task, + newAnnots, + imagePromises + ); + } + ); } - const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]); - const pageListPromise = dataPromises.then(([contentStream]) => { + + const pageListPromise = Promise.all([ + contentStreamPromise, + resourcesPromise, + ]).then(([contentStream]) => { const opList = new OperatorList(intent, sink); handler.send("StartRenderPage", { diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 1ed1c37494304..5e31d0d929d80 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -224,7 +224,7 @@ class PartialEvaluator { this.globalImageCache = globalImageCache; this.systemFontCache = systemFontCache; this.options = options || DefaultPartialEvaluatorOptions; - this.parsingType3Font = false; + this.type3FontRefs = null; this._regionalImageCache = new RegionalImageCache(); this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this); @@ -243,6 +243,10 @@ class PartialEvaluator { return shadow(this, "_pdfFunctionFactory", pdfFunctionFactory); } + get parsingType3Font() { + return !!this.type3FontRefs; + } + clone(newOptions = null) { const newEvaluator = Object.create(this); newEvaluator.options = Object.assign( @@ -1253,7 +1257,7 @@ class PartialEvaluator { } } if (fontRef) { - if (this.parsingType3Font && this.type3FontRefs.has(fontRef)) { + if (this.type3FontRefs?.has(fontRef)) { return errorFont(); } @@ -4633,7 +4637,6 @@ class TranslatedFont { // Compared to the parsing of e.g. an entire page, it doesn't really // make sense to only be able to render a Type3 glyph partially. const type3Evaluator = evaluator.clone({ ignoreErrors: false }); - type3Evaluator.parsingType3Font = true; // Prevent circular references in Type3 fonts. const type3FontRefs = new RefSet(evaluator.type3FontRefs); if (this.dict.objId && !type3FontRefs.has(this.dict.objId)) { diff --git a/src/core/parser.js b/src/core/parser.js index a9b826f40bfd7..c40f36a8298b8 100644 --- a/src/core/parser.js +++ b/src/core/parser.js @@ -710,11 +710,10 @@ class Parser { this.shift(); // 'stream' } else { // Bad stream length, scanning for endstream command. - const actualLength = this.#findStreamLength(startPos); - if (actualLength < 0) { + length = this.#findStreamLength(startPos); + if (length < 0) { throw new FormatError("Missing endstream command."); } - length = actualLength; lexer.nextChar(); this.shift();