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

View File

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

View File

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

View File

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

View File

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