diff --git a/src/core/colorspace.js b/src/core/colorspace.js index 0c47ec1cf..496dcdd00 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -308,10 +308,8 @@ class ColorSpace { static #cache( cacheKey, - xref, - globalColorSpaceCache, - localColorSpaceCache, - parsedCS + parsedCS, + { xref, globalColorSpaceCache, localColorSpaceCache } ) { if (!globalColorSpaceCache || !localColorSpaceCache) { throw new Error( @@ -389,22 +387,18 @@ class ColorSpace { "before calling `ColorSpace.parseAsync`." ); } - const parsedCS = this.#parse( - cs, + + const options = { xref, resources, pdfFunctionFactory, - globalColorSpaceCache - ); - - // Attempt to cache the parsed ColorSpace, by name and/or reference. - this.#cache( - cs, - xref, globalColorSpaceCache, localColorSpaceCache, - parsedCS - ); + }; + const parsedCS = this.#parse(cs, options); + + // Attempt to cache the parsed ColorSpace, by name and/or reference. + this.#cache(cs, parsedCS, options); return parsedCS; } @@ -426,22 +420,18 @@ class ColorSpace { if (cachedCS) { return cachedCS; } - const parsedCS = this.#parse( - cs, + + const options = { xref, resources, pdfFunctionFactory, - globalColorSpaceCache - ); - - // Attempt to cache the parsed ColorSpace, by name and/or reference. - this.#cache( - cs, - xref, globalColorSpaceCache, localColorSpaceCache, - parsedCS - ); + }; + const parsedCS = this.#parse(cs, options); + + // Attempt to cache the parsed ColorSpace, by name and/or reference. + this.#cache(cs, parsedCS, options); return parsedCS; } @@ -450,13 +440,9 @@ class ColorSpace { * NOTE: This method should *only* be invoked from `this.#parse`, * when parsing "sub" ColorSpaces. */ - static #subParse( - cs, - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ) { + static #subParse(cs, options) { + const { globalColorSpaceCache } = options; + let csRef; if (cs instanceof Ref) { const cachedCS = globalColorSpaceCache.getByRef(cs); @@ -465,13 +451,7 @@ class ColorSpace { } csRef = cs; } - const parsedCS = this.#parse( - cs, - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ); + const parsedCS = this.#parse(cs, options); // Only cache the parsed ColorSpace globally, by reference. if (csRef) { @@ -480,13 +460,9 @@ class ColorSpace { return parsedCS; } - static #parse( - cs, - xref, - resources = null, - pdfFunctionFactory, - globalColorSpaceCache - ) { + static #parse(cs, options) { + const { xref, resources, pdfFunctionFactory } = options; + cs = xref.fetchIfRef(cs); if (cs instanceof Name) { switch (cs.name) { @@ -510,13 +486,7 @@ class ColorSpace { const resourcesCS = colorSpaces.get(cs.name); if (resourcesCS) { if (resourcesCS instanceof Name) { - return this.#parse( - resourcesCS, - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ); + return this.#parse(resourcesCS, options); } cs = resourcesCS; break; @@ -561,13 +531,7 @@ class ColorSpace { numComps = dict.get("N"); const altRaw = dict.getRaw("Alternate"); if (altRaw) { - const altCS = this.#subParse( - altRaw, - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ); + const altCS = this.#subParse(altRaw, options); // Ensure that the number of components are correct, // and also (indirectly) that it is not a PatternCS. if (altCS.numComps === numComps) { @@ -586,24 +550,12 @@ class ColorSpace { case "Pattern": baseCS = cs[1] || null; if (baseCS) { - baseCS = this.#subParse( - baseCS, - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ); + baseCS = this.#subParse(baseCS, options); } return new PatternCS(baseCS); case "I": case "Indexed": - baseCS = this.#subParse( - cs[1], - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ); + baseCS = this.#subParse(cs[1], options); const hiVal = Math.max(0, Math.min(xref.fetchIfRef(cs[2]), 255)); const lookup = xref.fetchIfRef(cs[3]); return new IndexedCS(baseCS, hiVal, lookup); @@ -611,13 +563,7 @@ class ColorSpace { case "DeviceN": const name = xref.fetchIfRef(cs[1]); numComps = Array.isArray(name) ? name.length : 1; - baseCS = this.#subParse( - cs[2], - xref, - resources, - pdfFunctionFactory, - globalColorSpaceCache - ); + baseCS = this.#subParse(cs[2], options); const tintFn = pdfFunctionFactory.create(cs[3]); return new AlternateCS(numComps, baseCS, tintFn); case "Lab":