Skip to content

Commit

Permalink
Merge pull request mozilla#9659 from yurydelendik/rm-createFromIR
Browse files Browse the repository at this point in the history
Remove createFromIR from PDFFunctionFactory
  • Loading branch information
brendandahl authored Apr 12, 2018
2 parents f864d69 + 8378fe5 commit d3336ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 51 deletions.
20 changes: 9 additions & 11 deletions src/core/colorspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,10 @@ var ColorSpace = (function ColorSpaceClosure() {

ColorSpace.parse = function(cs, xref, res, pdfFunctionFactory) {
let IR = ColorSpace.parseToIR(cs, xref, res, pdfFunctionFactory);
return ColorSpace.fromIR(IR, pdfFunctionFactory);
return ColorSpace.fromIR(IR);
};

ColorSpace.fromIR = function(IR, pdfFunctionFactory) {
ColorSpace.fromIR = function(IR) {
var name = Array.isArray(IR) ? IR[0] : IR;
var whitePoint, blackPoint, gamma;

Expand All @@ -231,23 +231,21 @@ var ColorSpace = (function ColorSpaceClosure() {
case 'PatternCS':
var basePatternCS = IR[1];
if (basePatternCS) {
basePatternCS = ColorSpace.fromIR(basePatternCS, pdfFunctionFactory);
basePatternCS = ColorSpace.fromIR(basePatternCS);
}
return new PatternCS(basePatternCS);
case 'IndexedCS':
var baseIndexedCS = IR[1];
var hiVal = IR[2];
var lookup = IR[3];
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS,
pdfFunctionFactory),
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS),
hiVal, lookup);
case 'AlternateCS':
var numComps = IR[1];
var alt = IR[2];
var tintFnIR = IR[3];
return new AlternateCS(numComps, ColorSpace.fromIR(alt,
pdfFunctionFactory),
pdfFunctionFactory.createFromIR(tintFnIR));
var tintFn = IR[3];
return new AlternateCS(numComps, ColorSpace.fromIR(alt),
tintFn);
case 'LabCS':
whitePoint = IR[1];
blackPoint = IR[2];
Expand Down Expand Up @@ -364,8 +362,8 @@ var ColorSpace = (function ColorSpaceClosure() {
var name = xref.fetchIfRef(cs[1]);
numComps = Array.isArray(name) ? name.length : 1;
alt = ColorSpace.parseToIR(cs[2], xref, res, pdfFunctionFactory);
let tintFnIR = pdfFunctionFactory.createIR(xref.fetchIfRef(cs[3]));
return ['AlternateCS', numComps, alt, tintFnIR];
let tintFn = pdfFunctionFactory.create(xref.fetchIfRef(cs[3]));
return ['AlternateCS', numComps, alt, tintFn];
case 'Lab':
params = xref.fetchIfRef(cs[1]);
whitePoint = params.getArray('WhitePoint');
Expand Down
72 changes: 32 additions & 40 deletions src/core/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,24 @@ class PDFFunctionFactory {
fnObj,
});
}
}

createFromIR(IR) {
return PDFFunction.fromIR({
xref: this.xref,
isEvalSupported: this.isEvalSupported,
IR,
});
function toNumberArray(arr) {
if (!Array.isArray(arr)) {
return null;
}

createIR(fn) {
return PDFFunction.getIR({
xref: this.xref,
isEvalSupported: this.isEvalSupported,
fn,
});
const length = arr.length;
for (let i = 0; i < length; i++) {
if (typeof arr[i] !== 'number') {
// Non-number is found -- convert all items to numbers.
const result = new Array(length);
for (let i = 0; i < length; i++) {
result[i] = +arr[i];
}
return result;
}
}
return arr;
}

var PDFFunction = (function PDFFunctionClosure() {
Expand Down Expand Up @@ -171,8 +173,8 @@ var PDFFunction = (function PDFFunctionClosure() {
}
return out;
}
var domain = dict.getArray('Domain');
var range = dict.getArray('Range');
var domain = toNumberArray(dict.getArray('Domain'));
var range = toNumberArray(dict.getArray('Range'));

if (!domain || !range) {
throw new FormatError('No domain or range');
Expand All @@ -184,7 +186,7 @@ var PDFFunction = (function PDFFunctionClosure() {
domain = toMultiArray(domain);
range = toMultiArray(range);

var size = dict.get('Size');
var size = toNumberArray(dict.get('Size'));
var bps = dict.get('BitsPerSample');
var order = dict.get('Order') || 1;
if (order !== 1) {
Expand All @@ -193,17 +195,17 @@ var PDFFunction = (function PDFFunctionClosure() {
info('No support for cubic spline interpolation: ' + order);
}

var encode = dict.getArray('Encode');
var encode = toNumberArray(dict.getArray('Encode'));
if (!encode) {
encode = [];
for (var i = 0; i < inputSize; ++i) {
encode.push(0);
encode.push(size[i] - 1);
encode.push([0, size[i] - 1]);
}
} else {
encode = toMultiArray(encode);
}
encode = toMultiArray(encode);

var decode = dict.getArray('Decode');
var decode = toNumberArray(dict.getArray('Decode'));
if (!decode) {
decode = range;
} else {
Expand Down Expand Up @@ -304,15 +306,10 @@ var PDFFunction = (function PDFFunctionClosure() {
},

constructInterpolated({ xref, isEvalSupported, fn, dict, }) {
var c0 = dict.getArray('C0') || [0];
var c1 = dict.getArray('C1') || [1];
var c0 = toNumberArray(dict.getArray('C0')) || [0];
var c1 = toNumberArray(dict.getArray('C1')) || [1];
var n = dict.get('N');

if (!Array.isArray(c0) || !Array.isArray(c1)) {
throw new FormatError(
'Illegal dictionary for interpolated function');
}

var length = c0.length;
var diff = [];
for (var i = 0; i < length; ++i) {
Expand Down Expand Up @@ -340,7 +337,7 @@ var PDFFunction = (function PDFFunctionClosure() {
},

constructStiched({ xref, isEvalSupported, fn, dict, }) {
var domain = dict.getArray('Domain');
var domain = toNumberArray(dict.getArray('Domain'));

if (!domain) {
throw new FormatError('No domain');
Expand All @@ -354,12 +351,12 @@ var PDFFunction = (function PDFFunctionClosure() {
var fnRefs = dict.get('Functions');
var fns = [];
for (var i = 0, ii = fnRefs.length; i < ii; ++i) {
fns.push(this.getIR({ xref, isEvalSupported,
fns.push(this.parse({ xref, isEvalSupported,
fn: xref.fetchIfRef(fnRefs[i]), }));
}

var bounds = dict.getArray('Bounds');
var encode = dict.getArray('Encode');
var bounds = toNumberArray(dict.getArray('Bounds'));
var encode = toNumberArray(dict.getArray('Encode'));

return [CONSTRUCT_STICHED, domain, bounds, encode, fns];
},
Expand All @@ -368,14 +365,9 @@ var PDFFunction = (function PDFFunctionClosure() {
var domain = IR[1];
var bounds = IR[2];
var encode = IR[3];
var fnsIR = IR[4];
var fns = [];
var fns = IR[4];
var tmpBuf = new Float32Array(1);

for (var i = 0, ii = fnsIR.length; i < ii; i++) {
fns.push(this.fromIR({ xref, isEvalSupported, IR: fnsIR[i], }));
}

return function constructStichedFromIRResult(src, srcOffset,
dest, destOffset) {
var clip = function constructStichedFromIRClip(v, min, max) {
Expand Down Expand Up @@ -420,8 +412,8 @@ var PDFFunction = (function PDFFunctionClosure() {
},

constructPostScript({ xref, isEvalSupported, fn, dict, }) {
var domain = dict.getArray('Domain');
var range = dict.getArray('Range');
var domain = toNumberArray(dict.getArray('Domain'));
var range = toNumberArray(dict.getArray('Range'));

if (!domain) {
throw new FormatError('No domain.');
Expand Down

0 comments on commit d3336ca

Please sign in to comment.