Skip to content

Commit 2dc4af5

Browse files
authored
Merge pull request #9659 from yurydelendik/rm-createFromIR
Remove createFromIR from PDFFunctionFactory
2 parents 7d7bc80 + 20085aa commit 2dc4af5

File tree

2 files changed

+41
-51
lines changed

2 files changed

+41
-51
lines changed

src/core/colorspace.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,10 @@ var ColorSpace = (function ColorSpaceClosure() {
203203

204204
ColorSpace.parse = function(cs, xref, res, pdfFunctionFactory) {
205205
let IR = ColorSpace.parseToIR(cs, xref, res, pdfFunctionFactory);
206-
return ColorSpace.fromIR(IR, pdfFunctionFactory);
206+
return ColorSpace.fromIR(IR);
207207
};
208208

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

@@ -231,23 +231,21 @@ var ColorSpace = (function ColorSpaceClosure() {
231231
case 'PatternCS':
232232
var basePatternCS = IR[1];
233233
if (basePatternCS) {
234-
basePatternCS = ColorSpace.fromIR(basePatternCS, pdfFunctionFactory);
234+
basePatternCS = ColorSpace.fromIR(basePatternCS);
235235
}
236236
return new PatternCS(basePatternCS);
237237
case 'IndexedCS':
238238
var baseIndexedCS = IR[1];
239239
var hiVal = IR[2];
240240
var lookup = IR[3];
241-
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS,
242-
pdfFunctionFactory),
241+
return new IndexedCS(ColorSpace.fromIR(baseIndexedCS),
243242
hiVal, lookup);
244243
case 'AlternateCS':
245244
var numComps = IR[1];
246245
var alt = IR[2];
247-
var tintFnIR = IR[3];
248-
return new AlternateCS(numComps, ColorSpace.fromIR(alt,
249-
pdfFunctionFactory),
250-
pdfFunctionFactory.createFromIR(tintFnIR));
246+
var tintFn = IR[3];
247+
return new AlternateCS(numComps, ColorSpace.fromIR(alt),
248+
tintFn);
251249
case 'LabCS':
252250
whitePoint = IR[1];
253251
blackPoint = IR[2];
@@ -364,8 +362,8 @@ var ColorSpace = (function ColorSpaceClosure() {
364362
var name = xref.fetchIfRef(cs[1]);
365363
numComps = Array.isArray(name) ? name.length : 1;
366364
alt = ColorSpace.parseToIR(cs[2], xref, res, pdfFunctionFactory);
367-
let tintFnIR = pdfFunctionFactory.createIR(xref.fetchIfRef(cs[3]));
368-
return ['AlternateCS', numComps, alt, tintFnIR];
365+
let tintFn = pdfFunctionFactory.create(xref.fetchIfRef(cs[3]));
366+
return ['AlternateCS', numComps, alt, tintFn];
369367
case 'Lab':
370368
params = xref.fetchIfRef(cs[1]);
371369
whitePoint = params.getArray('WhitePoint');

src/core/function.js

+32-40
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,24 @@ class PDFFunctionFactory {
4646
fnObj,
4747
});
4848
}
49+
}
4950

50-
createFromIR(IR) {
51-
return PDFFunction.fromIR({
52-
xref: this.xref,
53-
isEvalSupported: this.isEvalSupported,
54-
IR,
55-
});
51+
function toNumberArray(arr) {
52+
if (!Array.isArray(arr)) {
53+
return null;
5654
}
57-
58-
createIR(fn) {
59-
return PDFFunction.getIR({
60-
xref: this.xref,
61-
isEvalSupported: this.isEvalSupported,
62-
fn,
63-
});
55+
const length = arr.length;
56+
for (let i = 0; i < length; i++) {
57+
if (typeof arr[i] !== 'number') {
58+
// Non-number is found -- convert all items to numbers.
59+
const result = new Array(length);
60+
for (let i = 0; i < length; i++) {
61+
result[i] = +arr[i];
62+
}
63+
return result;
64+
}
6465
}
66+
return arr;
6567
}
6668

6769
var PDFFunction = (function PDFFunctionClosure() {
@@ -171,8 +173,8 @@ var PDFFunction = (function PDFFunctionClosure() {
171173
}
172174
return out;
173175
}
174-
var domain = dict.getArray('Domain');
175-
var range = dict.getArray('Range');
176+
var domain = toNumberArray(dict.getArray('Domain'));
177+
var range = toNumberArray(dict.getArray('Range'));
176178

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

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

196-
var encode = dict.getArray('Encode');
198+
var encode = toNumberArray(dict.getArray('Encode'));
197199
if (!encode) {
198200
encode = [];
199201
for (var i = 0; i < inputSize; ++i) {
200-
encode.push(0);
201-
encode.push(size[i] - 1);
202+
encode.push([0, size[i] - 1]);
202203
}
204+
} else {
205+
encode = toMultiArray(encode);
203206
}
204-
encode = toMultiArray(encode);
205207

206-
var decode = dict.getArray('Decode');
208+
var decode = toNumberArray(dict.getArray('Decode'));
207209
if (!decode) {
208210
decode = range;
209211
} else {
@@ -304,15 +306,10 @@ var PDFFunction = (function PDFFunctionClosure() {
304306
},
305307

306308
constructInterpolated({ xref, isEvalSupported, fn, dict, }) {
307-
var c0 = dict.getArray('C0') || [0];
308-
var c1 = dict.getArray('C1') || [1];
309+
var c0 = toNumberArray(dict.getArray('C0')) || [0];
310+
var c1 = toNumberArray(dict.getArray('C1')) || [1];
309311
var n = dict.get('N');
310312

311-
if (!Array.isArray(c0) || !Array.isArray(c1)) {
312-
throw new FormatError(
313-
'Illegal dictionary for interpolated function');
314-
}
315-
316313
var length = c0.length;
317314
var diff = [];
318315
for (var i = 0; i < length; ++i) {
@@ -340,7 +337,7 @@ var PDFFunction = (function PDFFunctionClosure() {
340337
},
341338

342339
constructStiched({ xref, isEvalSupported, fn, dict, }) {
343-
var domain = dict.getArray('Domain');
340+
var domain = toNumberArray(dict.getArray('Domain'));
344341

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

361-
var bounds = dict.getArray('Bounds');
362-
var encode = dict.getArray('Encode');
358+
var bounds = toNumberArray(dict.getArray('Bounds'));
359+
var encode = toNumberArray(dict.getArray('Encode'));
363360

364361
return [CONSTRUCT_STICHED, domain, bounds, encode, fns];
365362
},
@@ -368,14 +365,9 @@ var PDFFunction = (function PDFFunctionClosure() {
368365
var domain = IR[1];
369366
var bounds = IR[2];
370367
var encode = IR[3];
371-
var fnsIR = IR[4];
372-
var fns = [];
368+
var fns = IR[4];
373369
var tmpBuf = new Float32Array(1);
374370

375-
for (var i = 0, ii = fnsIR.length; i < ii; i++) {
376-
fns.push(this.fromIR({ xref, isEvalSupported, IR: fnsIR[i], }));
377-
}
378-
379371
return function constructStichedFromIRResult(src, srcOffset,
380372
dest, destOffset) {
381373
var clip = function constructStichedFromIRClip(v, min, max) {
@@ -420,8 +412,8 @@ var PDFFunction = (function PDFFunctionClosure() {
420412
},
421413

422414
constructPostScript({ xref, isEvalSupported, fn, dict, }) {
423-
var domain = dict.getArray('Domain');
424-
var range = dict.getArray('Range');
415+
var domain = toNumberArray(dict.getArray('Domain'));
416+
var range = toNumberArray(dict.getArray('Range'));
425417

426418
if (!domain) {
427419
throw new FormatError('No domain.');

0 commit comments

Comments
 (0)