From e0e59eaf010140495d1f3e2251b472adb101e30c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 31 Mar 2025 10:29:29 +0200 Subject: [PATCH] Define the global cache-data once in `buildPaintImageXObject` Currently we duplicate the same identical code three times, which seems both unnecessary and error prone. --- src/core/evaluator.js | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 574cb9981..1f99e1db5 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -742,7 +742,8 @@ class PartialEvaluator { // If there is no imageMask, create the PDFImage and a lot // of image processing can be done here. let objId = `img_${this.idFactory.createObjId()}`, - cacheGlobally = false; + cacheGlobally = false, + globalCacheData = null; if (this.parsingType3Font) { objId = `${this.idFactory.getDocId()}_type3_${objId}`; @@ -767,15 +768,17 @@ class PartialEvaluator { operatorList.addImageOps(fn, args, optionalContent, hasMask); if (cacheGlobally) { + globalCacheData = { + objId, + fn, + args, + optionalContent, + hasMask, + byteSize: 0, // Temporary entry, to avoid `setData` returning early. + }; + if (this.globalImageCache.hasDecodeFailed(imageRef)) { - this.globalImageCache.setData(imageRef, { - objId, - fn, - args, - optionalContent, - hasMask, - byteSize: 0, // Data is `null`, since decoding failed previously. - }); + this.globalImageCache.setData(imageRef, globalCacheData); this._sendImgData(objId, /* imgData = */ null, cacheGlobally); return; @@ -792,14 +795,7 @@ class PartialEvaluator { ]); if (localLength) { - this.globalImageCache.setData(imageRef, { - objId, - fn, - args, - optionalContent, - hasMask, - byteSize: 0, // Temporary entry, to avoid `setData` returning early. - }); + this.globalImageCache.setData(imageRef, globalCacheData); this.globalImageCache.addByteSize(imageRef, localLength); return; } @@ -848,14 +844,8 @@ class PartialEvaluator { this._regionalImageCache.set(/* name = */ null, imageRef, cacheData); if (cacheGlobally) { - this.globalImageCache.setData(imageRef, { - objId, - fn, - args, - optionalContent, - hasMask, - byteSize: 0, // Temporary entry, note `addByteSize` above. - }); + assert(globalCacheData, "The global cache-data must be available."); + this.globalImageCache.setData(imageRef, globalCacheData); } } }