Re-factor ColorSpace.parse to take a parameter object, rather than a bunch of (randomly) ordered parameters

Given the number of existing parameters, this will avoid needlessly unwieldy call-sites especially with upcoming changes in later patches.
This commit is contained in:
Jonas Jenwald 2020-06-17 18:17:43 +02:00
parent ac4a5c3ace
commit e22bc483a5
5 changed files with 124 additions and 52 deletions

View File

@ -259,8 +259,8 @@ class ColorSpace {
return shadow(this, "usesZeroToOneRange", true);
}
static parse(cs, xref, res, pdfFunctionFactory) {
const IR = this.parseToIR(cs, xref, res, pdfFunctionFactory);
static parse({ cs, xref, resources = null, pdfFunctionFactory }) {
const IR = this.parseToIR(cs, xref, resources, pdfFunctionFactory);
return this.fromIR(IR);
}
@ -312,7 +312,7 @@ class ColorSpace {
}
}
static parseToIR(cs, xref, res = null, pdfFunctionFactory) {
static parseToIR(cs, xref, resources = null, pdfFunctionFactory) {
cs = xref.fetchIfRef(cs);
if (isName(cs)) {
switch (cs.name) {
@ -328,15 +328,20 @@ class ColorSpace {
case "Pattern":
return ["PatternCS", null];
default:
if (isDict(res)) {
const colorSpaces = res.get("ColorSpace");
if (isDict(resources)) {
const colorSpaces = resources.get("ColorSpace");
if (isDict(colorSpaces)) {
const resCS = colorSpaces.get(cs.name);
if (resCS) {
if (isName(resCS)) {
return this.parseToIR(resCS, xref, res, pdfFunctionFactory);
const resourcesCS = colorSpaces.get(cs.name);
if (resourcesCS) {
if (isName(resourcesCS)) {
return this.parseToIR(
resourcesCS,
xref,
resources,
pdfFunctionFactory
);
}
cs = resCS;
cs = resourcesCS;
break;
}
}
@ -377,7 +382,12 @@ class ColorSpace {
numComps = dict.get("N");
alt = dict.get("Alternate");
if (alt) {
const altIR = this.parseToIR(alt, xref, res, pdfFunctionFactory);
const altIR = this.parseToIR(
alt,
xref,
resources,
pdfFunctionFactory
);
// Parse the /Alternate CS to ensure that the number of components
// are correct, and also (indirectly) that it is not a PatternCS.
const altCS = this.fromIR(altIR, pdfFunctionFactory);
@ -400,7 +410,7 @@ class ColorSpace {
basePatternCS = this.parseToIR(
basePatternCS,
xref,
res,
resources,
pdfFunctionFactory
);
}
@ -410,7 +420,7 @@ class ColorSpace {
const baseIndexedCS = this.parseToIR(
cs[1],
xref,
res,
resources,
pdfFunctionFactory
);
const hiVal = xref.fetchIfRef(cs[2]) + 1;
@ -423,7 +433,7 @@ class ColorSpace {
case "DeviceN":
const name = xref.fetchIfRef(cs[1]);
numComps = Array.isArray(name) ? name.length : 1;
alt = this.parseToIR(cs[2], xref, res, pdfFunctionFactory);
alt = this.parseToIR(cs[2], xref, resources, pdfFunctionFactory);
const tintFn = pdfFunctionFactory.create(xref.fetchIfRef(cs[3]));
return ["AlternateCS", numComps, alt, tintFn];
case "Lab":

View File

@ -1136,12 +1136,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
parseColorSpace({ cs, resources, localColorSpaceCache }) {
return new Promise(resolve => {
const parsedColorSpace = ColorSpace.parse(
const parsedColorSpace = ColorSpace.parse({
cs,
this.xref,
xref: this.xref,
resources,
this.pdfFunctionFactory
);
pdfFunctionFactory: this.pdfFunctionFactory,
});
const csName = cs instanceof Name ? cs.name : null;
if (csName) {

View File

@ -179,13 +179,12 @@ var PDFImage = (function PDFImageClosure() {
);
}
}
const resources = isInline ? res : null;
this.colorSpace = ColorSpace.parse(
colorSpace,
this.colorSpace = ColorSpace.parse({
cs: colorSpace,
xref,
resources,
pdfFunctionFactory
);
resources: isInline ? res : null,
pdfFunctionFactory,
});
this.numComps = this.colorSpace.numComps;
}

View File

@ -111,13 +111,17 @@ Shadings.SMALL_NUMBER = 1e-6;
// Radial and axial shading have very similar implementations
// If needed, the implementations can be broken into two classes
Shadings.RadialAxial = (function RadialAxialClosure() {
function RadialAxial(dict, matrix, xref, res, pdfFunctionFactory) {
function RadialAxial(dict, matrix, xref, resources, pdfFunctionFactory) {
this.matrix = matrix;
this.coordsArr = dict.getArray("Coords");
this.shadingType = dict.get("ShadingType");
this.type = "Pattern";
var cs = dict.get("ColorSpace", "CS");
cs = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const cs = ColorSpace.parse({
cs: dict.get("ColorSpace", "CS"),
xref,
resources,
pdfFunctionFactory,
});
this.cs = cs;
const bbox = dict.getArray("BBox");
if (Array.isArray(bbox) && bbox.length === 4) {
@ -830,7 +834,7 @@ Shadings.Mesh = (function MeshClosure() {
}
}
function Mesh(stream, matrix, xref, res, pdfFunctionFactory) {
function Mesh(stream, matrix, xref, resources, pdfFunctionFactory) {
if (!isStream(stream)) {
throw new FormatError("Mesh data is not a stream");
}
@ -844,8 +848,12 @@ Shadings.Mesh = (function MeshClosure() {
} else {
this.bbox = null;
}
var cs = dict.get("ColorSpace", "CS");
cs = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const cs = ColorSpace.parse({
cs: dict.get("ColorSpace", "CS"),
xref,
resources,
pdfFunctionFactory,
});
this.cs = cs;
this.background = dict.has("Background")
? cs.getRgb(dict.get("Background"), 0)

View File

@ -55,12 +55,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
const testSrc = new Uint8Array([27, 125, 250, 131]);
const testDest = new Uint8ClampedArray(4 * 4 * 3);
@ -100,12 +105,17 @@ describe("colorspace", function () {
data: Name.get("DeviceGray"),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
const testSrc = new Uint8Array([27, 125, 250, 131]);
const testDest = new Uint8ClampedArray(3 * 3 * 3);
@ -141,12 +151,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
// prettier-ignore
const testSrc = new Uint8Array([
@ -192,12 +207,17 @@ describe("colorspace", function () {
data: Name.get("DeviceRGB"),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
// prettier-ignore
const testSrc = new Uint8Array([
@ -239,12 +259,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
// prettier-ignore
const testSrc = new Uint8Array([
@ -290,12 +315,17 @@ describe("colorspace", function () {
data: Name.get("DeviceCMYK"),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
// prettier-ignore
const testSrc = new Uint8Array([
@ -342,12 +372,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
const testSrc = new Uint8Array([27, 125, 250, 131]);
const testDest = new Uint8ClampedArray(4 * 4 * 3);
@ -396,12 +431,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
// prettier-ignore
const testSrc = new Uint8Array([
@ -448,12 +488,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
// prettier-ignore
const testSrc = new Uint8Array([
@ -502,12 +547,17 @@ describe("colorspace", function () {
data: new Dict(),
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
const testSrc = new Uint8Array([2, 2, 0, 1]);
const testDest = new Uint8ClampedArray(3 * 3 * 3);
@ -564,12 +614,17 @@ describe("colorspace", function () {
data: fn,
},
]);
const res = new Dict();
const resources = new Dict();
const pdfFunctionFactory = new PDFFunctionFactory({
xref,
});
const colorSpace = ColorSpace.parse(cs, xref, res, pdfFunctionFactory);
const colorSpace = ColorSpace.parse({
cs,
xref,
resources,
pdfFunctionFactory,
});
const testSrc = new Uint8Array([27, 25, 50, 31]);
const testDest = new Uint8ClampedArray(3 * 3 * 3);