Also cache "sub" ColorSpaces globally (PR 19583 follow-up)
Some ColorSpaces can reference other ColorSpaces, and since it's fairly common for many pages to use the same ColorSpaces (see e.g. `issue17061.pdf`) this can help reduce a lot of unnecessary re-parsing especially for e.g. `ICCBased` ColorSpaces.
This commit is contained in:
parent
0c1ac54f48
commit
33dfc4dd5f
@ -389,7 +389,13 @@ class ColorSpace {
|
|||||||
"before calling `ColorSpace.parseAsync`."
|
"before calling `ColorSpace.parseAsync`."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const parsedCS = this.#parse(cs, xref, resources, pdfFunctionFactory);
|
const parsedCS = this.#parse(
|
||||||
|
cs,
|
||||||
|
xref,
|
||||||
|
resources,
|
||||||
|
pdfFunctionFactory,
|
||||||
|
globalColorSpaceCache
|
||||||
|
);
|
||||||
|
|
||||||
// Attempt to cache the parsed ColorSpace, by name and/or reference.
|
// Attempt to cache the parsed ColorSpace, by name and/or reference.
|
||||||
this.#cache(
|
this.#cache(
|
||||||
@ -420,7 +426,13 @@ class ColorSpace {
|
|||||||
if (cachedCS) {
|
if (cachedCS) {
|
||||||
return cachedCS;
|
return cachedCS;
|
||||||
}
|
}
|
||||||
const parsedCS = this.#parse(cs, xref, resources, pdfFunctionFactory);
|
const parsedCS = this.#parse(
|
||||||
|
cs,
|
||||||
|
xref,
|
||||||
|
resources,
|
||||||
|
pdfFunctionFactory,
|
||||||
|
globalColorSpaceCache
|
||||||
|
);
|
||||||
|
|
||||||
// Attempt to cache the parsed ColorSpace, by name and/or reference.
|
// Attempt to cache the parsed ColorSpace, by name and/or reference.
|
||||||
this.#cache(
|
this.#cache(
|
||||||
@ -434,7 +446,47 @@ class ColorSpace {
|
|||||||
return parsedCS;
|
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,
|
||||||
|
xref,
|
||||||
|
resources,
|
||||||
|
pdfFunctionFactory,
|
||||||
|
globalColorSpaceCache
|
||||||
|
) {
|
||||||
|
let csRef;
|
||||||
|
if (cs instanceof Ref) {
|
||||||
|
const cachedCS = globalColorSpaceCache.getByRef(cs);
|
||||||
|
if (cachedCS) {
|
||||||
|
return cachedCS;
|
||||||
|
}
|
||||||
|
csRef = cs;
|
||||||
|
}
|
||||||
|
const parsedCS = this.#parse(
|
||||||
|
cs,
|
||||||
|
xref,
|
||||||
|
resources,
|
||||||
|
pdfFunctionFactory,
|
||||||
|
globalColorSpaceCache
|
||||||
|
);
|
||||||
|
|
||||||
|
// Only cache the parsed ColorSpace globally, by reference.
|
||||||
|
if (csRef) {
|
||||||
|
globalColorSpaceCache.set(/* name = */ null, csRef, parsedCS);
|
||||||
|
}
|
||||||
|
return parsedCS;
|
||||||
|
}
|
||||||
|
|
||||||
|
static #parse(
|
||||||
|
cs,
|
||||||
|
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) {
|
||||||
@ -462,7 +514,8 @@ class ColorSpace {
|
|||||||
resourcesCS,
|
resourcesCS,
|
||||||
xref,
|
xref,
|
||||||
resources,
|
resources,
|
||||||
pdfFunctionFactory
|
pdfFunctionFactory,
|
||||||
|
globalColorSpaceCache
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
cs = resourcesCS;
|
cs = resourcesCS;
|
||||||
@ -506,9 +559,15 @@ class ColorSpace {
|
|||||||
const stream = xref.fetchIfRef(cs[1]);
|
const stream = xref.fetchIfRef(cs[1]);
|
||||||
const dict = stream.dict;
|
const dict = stream.dict;
|
||||||
numComps = dict.get("N");
|
numComps = dict.get("N");
|
||||||
const alt = dict.get("Alternate");
|
const altRaw = dict.getRaw("Alternate");
|
||||||
if (alt) {
|
if (altRaw) {
|
||||||
const altCS = this.#parse(alt, xref, resources, pdfFunctionFactory);
|
const altCS = this.#subParse(
|
||||||
|
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) {
|
||||||
@ -527,12 +586,24 @@ class ColorSpace {
|
|||||||
case "Pattern":
|
case "Pattern":
|
||||||
baseCS = cs[1] || null;
|
baseCS = cs[1] || null;
|
||||||
if (baseCS) {
|
if (baseCS) {
|
||||||
baseCS = this.#parse(baseCS, xref, resources, pdfFunctionFactory);
|
baseCS = this.#subParse(
|
||||||
|
baseCS,
|
||||||
|
xref,
|
||||||
|
resources,
|
||||||
|
pdfFunctionFactory,
|
||||||
|
globalColorSpaceCache
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return new PatternCS(baseCS);
|
return new PatternCS(baseCS);
|
||||||
case "I":
|
case "I":
|
||||||
case "Indexed":
|
case "Indexed":
|
||||||
baseCS = this.#parse(cs[1], xref, resources, pdfFunctionFactory);
|
baseCS = this.#subParse(
|
||||||
|
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);
|
||||||
@ -540,7 +611,13 @@ 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.#parse(cs[2], xref, resources, pdfFunctionFactory);
|
baseCS = this.#subParse(
|
||||||
|
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":
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user