From e22bc483a59085ef23add359a8b05cb901cd25ce Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 17 Jun 2020 18:17:43 +0200 Subject: [PATCH] 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. --- src/core/colorspace.js | 38 +++++++++----- src/core/evaluator.js | 8 +-- src/core/image.js | 11 ++-- src/core/pattern.js | 20 +++++--- test/unit/colorspace_spec.js | 99 ++++++++++++++++++++++++++++-------- 5 files changed, 124 insertions(+), 52 deletions(-) diff --git a/src/core/colorspace.js b/src/core/colorspace.js index dbd63a26c..3b0f7e751 100644 --- a/src/core/colorspace.js +++ b/src/core/colorspace.js @@ -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": diff --git a/src/core/evaluator.js b/src/core/evaluator.js index 4d873ec06..8bf2f78bf 100644 --- a/src/core/evaluator.js +++ b/src/core/evaluator.js @@ -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) { diff --git a/src/core/image.js b/src/core/image.js index 5c5bc15bc..3da9f12c5 100644 --- a/src/core/image.js +++ b/src/core/image.js @@ -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; } diff --git a/src/core/pattern.js b/src/core/pattern.js index c380adf0b..384a9bb3a 100644 --- a/src/core/pattern.js +++ b/src/core/pattern.js @@ -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) diff --git a/test/unit/colorspace_spec.js b/test/unit/colorspace_spec.js index 47fd40659..05946d9da 100644 --- a/test/unit/colorspace_spec.js +++ b/test/unit/colorspace_spec.js @@ -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);