Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also cache "sub" ColorSpaces globally (PR 19583 follow-up) #19598

Merged
merged 2 commits into from
Mar 3, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 52 additions & 29 deletions src/core/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,8 @@ class ColorSpace {

static #cache(
cacheKey,
xref,
globalColorSpaceCache,
localColorSpaceCache,
parsedCS
parsedCS,
{ xref, globalColorSpaceCache, localColorSpaceCache }
) {
if (!globalColorSpaceCache || !localColorSpaceCache) {
throw new Error(
Expand Down Expand Up @@ -389,16 +387,18 @@ class ColorSpace {
"before calling `ColorSpace.parseAsync`."
);
}
const parsedCS = this.#parse(cs, xref, resources, pdfFunctionFactory);

// Attempt to cache the parsed ColorSpace, by name and/or reference.
this.#cache(
cs,
const options = {
xref,
resources,
pdfFunctionFactory,
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;
}
Expand All @@ -420,21 +420,49 @@ class ColorSpace {
if (cachedCS) {
return cachedCS;
}
const parsedCS = this.#parse(cs, xref, resources, pdfFunctionFactory);

// Attempt to cache the parsed ColorSpace, by name and/or reference.
this.#cache(
cs,
const options = {
xref,
resources,
pdfFunctionFactory,
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;
}

static #parse(cs, xref, resources = null, pdfFunctionFactory) {
/**
* 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;

cs = xref.fetchIfRef(cs);
if (cs instanceof Name) {
switch (cs.name) {
Expand All @@ -458,12 +486,7 @@ class ColorSpace {
const resourcesCS = colorSpaces.get(cs.name);
if (resourcesCS) {
if (resourcesCS instanceof Name) {
return this.#parse(
resourcesCS,
xref,
resources,
pdfFunctionFactory
);
return this.#parse(resourcesCS, options);
}
cs = resourcesCS;
break;
Expand Down Expand Up @@ -506,9 +529,9 @@ class ColorSpace {
const stream = xref.fetchIfRef(cs[1]);
const dict = stream.dict;
numComps = dict.get("N");
const alt = dict.get("Alternate");
if (alt) {
const altCS = this.#parse(alt, xref, resources, pdfFunctionFactory);
const altRaw = dict.getRaw("Alternate");
if (altRaw) {
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) {
Expand All @@ -527,20 +550,20 @@ class ColorSpace {
case "Pattern":
baseCS = cs[1] || null;
if (baseCS) {
baseCS = this.#parse(baseCS, xref, resources, pdfFunctionFactory);
baseCS = this.#subParse(baseCS, options);
}
return new PatternCS(baseCS);
case "I":
case "Indexed":
baseCS = this.#parse(cs[1], xref, resources, pdfFunctionFactory);
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);
case "Separation":
case "DeviceN":
const name = xref.fetchIfRef(cs[1]);
numComps = Array.isArray(name) ? name.length : 1;
baseCS = this.#parse(cs[2], xref, resources, pdfFunctionFactory);
baseCS = this.#subParse(cs[2], options);
const tintFn = pdfFunctionFactory.create(cs[3]);
return new AlternateCS(numComps, baseCS, tintFn);
case "Lab":
Expand Down