mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
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:
parent
ac4a5c3ace
commit
e22bc483a5
5 changed files with 124 additions and 52 deletions
|
@ -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":
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue