Compare commits

..

No commits in common. "5e6cfbe1637d39e67d0c524240cb2c78774f129f" and "0c1ac54f4847d94292d391a7e54ae174d64191bc" have entirely different histories.

View File

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