Shorten various static ColorSpace method signatures

By passing around common parameters, i.e. everything that's not the ColorSpace-data, in an object we can shorten this code a little bit.
This commit is contained in:
Jonas Jenwald 2025-03-03 11:08:05 +01:00
parent 33dfc4dd5f
commit ba640e5cdc

View File

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