2012-09-01 07:48:21 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-10-26 10:18:22 +09:00
|
|
|
|
2017-06-29 05:51:31 +09:00
|
|
|
import { FormatError, info, isArray, isBool } from '../shared/util';
|
2017-04-02 23:14:30 +09:00
|
|
|
import { isDict, isStream } from './primitives';
|
|
|
|
import { PostScriptLexer, PostScriptParser } from './ps_parser';
|
2015-11-22 01:32:47 +09:00
|
|
|
|
2011-12-09 07:18:43 +09:00
|
|
|
var PDFFunction = (function PDFFunctionClosure() {
|
2011-10-25 08:55:23 +09:00
|
|
|
var CONSTRUCT_SAMPLED = 0;
|
|
|
|
var CONSTRUCT_INTERPOLATED = 2;
|
|
|
|
var CONSTRUCT_STICHED = 3;
|
|
|
|
var CONSTRUCT_POSTSCRIPT = 4;
|
|
|
|
|
|
|
|
return {
|
2012-04-05 05:43:26 +09:00
|
|
|
getSampleArray: function PDFFunction_getSampleArray(size, outputSize, bps,
|
2011-10-30 02:59:49 +09:00
|
|
|
str) {
|
2014-04-08 04:05:17 +09:00
|
|
|
var i, ii;
|
2011-10-25 08:55:23 +09:00
|
|
|
var length = 1;
|
2014-04-08 04:05:17 +09:00
|
|
|
for (i = 0, ii = size.length; i < ii; i++) {
|
2011-10-25 08:55:23 +09:00
|
|
|
length *= size[i];
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
length *= outputSize;
|
|
|
|
|
2014-08-12 13:26:09 +09:00
|
|
|
var array = new Array(length);
|
2011-10-25 08:55:23 +09:00
|
|
|
var codeSize = 0;
|
|
|
|
var codeBuf = 0;
|
2011-11-22 10:23:54 +09:00
|
|
|
// 32 is a valid bps so shifting won't work
|
2011-11-12 07:44:47 +09:00
|
|
|
var sampleMul = 1.0 / (Math.pow(2.0, bps) - 1);
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var strBytes = str.getBytes((length * bps + 7) / 8);
|
|
|
|
var strIdx = 0;
|
2014-04-08 04:05:17 +09:00
|
|
|
for (i = 0; i < length; i++) {
|
2011-10-25 08:55:23 +09:00
|
|
|
while (codeSize < bps) {
|
|
|
|
codeBuf <<= 8;
|
|
|
|
codeBuf |= strBytes[strIdx++];
|
|
|
|
codeSize += 8;
|
|
|
|
}
|
|
|
|
codeSize -= bps;
|
2014-08-12 13:26:09 +09:00
|
|
|
array[i] = (codeBuf >> codeSize) * sampleMul;
|
2011-10-25 08:55:23 +09:00
|
|
|
codeBuf &= (1 << codeSize) - 1;
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
getIR: function PDFFunction_getIR(xref, fn) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var dict = fn.dict;
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!dict) {
|
2011-10-25 08:55:23 +09:00
|
|
|
dict = fn;
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var types = [this.constructSampled,
|
|
|
|
null,
|
|
|
|
this.constructInterpolated,
|
|
|
|
this.constructStiched,
|
|
|
|
this.constructPostScript];
|
|
|
|
|
|
|
|
var typeNum = dict.get('FunctionType');
|
|
|
|
var typeFn = types[typeNum];
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!typeFn) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError('Unknown type of function');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
return typeFn.call(this, fn, dict, xref);
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
fromIR: function PDFFunction_fromIR(IR) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var type = IR[0];
|
|
|
|
switch (type) {
|
|
|
|
case CONSTRUCT_SAMPLED:
|
|
|
|
return this.constructSampledFromIR(IR);
|
|
|
|
case CONSTRUCT_INTERPOLATED:
|
|
|
|
return this.constructInterpolatedFromIR(IR);
|
|
|
|
case CONSTRUCT_STICHED:
|
|
|
|
return this.constructStichedFromIR(IR);
|
2017-01-20 00:26:32 +09:00
|
|
|
// case CONSTRUCT_POSTSCRIPT:
|
2011-10-27 04:10:58 +09:00
|
|
|
default:
|
2011-10-25 08:55:23 +09:00
|
|
|
return this.constructPostScriptFromIR(IR);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
parse: function PDFFunction_parse(xref, fn) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var IR = this.getIR(xref, fn);
|
|
|
|
return this.fromIR(IR);
|
|
|
|
},
|
|
|
|
|
2014-08-05 01:12:51 +09:00
|
|
|
parseArray: function PDFFunction_parseArray(xref, fnObj) {
|
|
|
|
if (!isArray(fnObj)) {
|
|
|
|
// not an array -- parsing as regular function
|
|
|
|
return this.parse(xref, fnObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
var fnArray = [];
|
|
|
|
for (var j = 0, jj = fnObj.length; j < jj; j++) {
|
|
|
|
var obj = xref.fetchIfRef(fnObj[j]);
|
|
|
|
fnArray.push(PDFFunction.parse(xref, obj));
|
|
|
|
}
|
|
|
|
return function (src, srcOffset, dest, destOffset) {
|
|
|
|
for (var i = 0, ii = fnArray.length; i < ii; i++) {
|
|
|
|
fnArray[i](src, srcOffset, dest, destOffset + i);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructSampled: function PDFFunction_constructSampled(str, dict) {
|
2011-11-12 07:44:47 +09:00
|
|
|
function toMultiArray(arr) {
|
|
|
|
var inputLength = arr.length;
|
2012-03-22 22:15:27 +09:00
|
|
|
var out = [];
|
2011-11-12 07:44:47 +09:00
|
|
|
var index = 0;
|
|
|
|
for (var i = 0; i < inputLength; i += 2) {
|
|
|
|
out[index] = [arr[i], arr[i + 1]];
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
2016-05-06 02:16:35 +09:00
|
|
|
var domain = dict.getArray('Domain');
|
|
|
|
var range = dict.getArray('Range');
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!domain || !range) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError('No domain or range');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var inputSize = domain.length / 2;
|
|
|
|
var outputSize = range.length / 2;
|
|
|
|
|
2011-11-12 07:44:47 +09:00
|
|
|
domain = toMultiArray(domain);
|
|
|
|
range = toMultiArray(range);
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var size = dict.get('Size');
|
|
|
|
var bps = dict.get('BitsPerSample');
|
2012-08-10 22:30:20 +09:00
|
|
|
var order = dict.get('Order') || 1;
|
|
|
|
if (order !== 1) {
|
|
|
|
// No description how cubic spline interpolation works in PDF32000:2008
|
|
|
|
// As in poppler, ignoring order, linear interpolation may work as good
|
2014-01-04 02:34:13 +09:00
|
|
|
info('No support for cubic spline interpolation: ' + order);
|
2012-08-10 22:30:20 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2016-05-06 02:16:35 +09:00
|
|
|
var encode = dict.getArray('Encode');
|
2011-10-25 08:55:23 +09:00
|
|
|
if (!encode) {
|
|
|
|
encode = [];
|
|
|
|
for (var i = 0; i < inputSize; ++i) {
|
|
|
|
encode.push(0);
|
|
|
|
encode.push(size[i] - 1);
|
|
|
|
}
|
|
|
|
}
|
2011-11-12 07:44:47 +09:00
|
|
|
encode = toMultiArray(encode);
|
|
|
|
|
2016-05-06 02:16:35 +09:00
|
|
|
var decode = dict.getArray('Decode');
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!decode) {
|
2011-10-25 08:55:23 +09:00
|
|
|
decode = range;
|
2014-03-03 21:34:12 +09:00
|
|
|
} else {
|
2011-11-12 07:44:47 +09:00
|
|
|
decode = toMultiArray(decode);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-11-12 07:44:47 +09:00
|
|
|
|
2011-10-25 08:55:23 +09:00
|
|
|
var samples = this.getSampleArray(size, outputSize, bps, str);
|
|
|
|
|
|
|
|
return [
|
|
|
|
CONSTRUCT_SAMPLED, inputSize, domain, encode, decode, samples, size,
|
2012-01-30 18:38:28 +09:00
|
|
|
outputSize, Math.pow(2, bps) - 1, range
|
2011-10-25 08:55:23 +09:00
|
|
|
];
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructSampledFromIR: function PDFFunction_constructSampledFromIR(IR) {
|
2012-01-30 18:38:28 +09:00
|
|
|
// See chapter 3, page 109 of the PDF reference
|
|
|
|
function interpolate(x, xmin, xmax, ymin, ymax) {
|
|
|
|
return ymin + ((x - xmin) * ((ymax - ymin) / (xmax - xmin)));
|
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2014-08-05 01:12:51 +09:00
|
|
|
return function constructSampledFromIRResult(src, srcOffset,
|
|
|
|
dest, destOffset) {
|
2012-01-30 18:38:28 +09:00
|
|
|
// See chapter 3, page 110 of the PDF reference.
|
|
|
|
var m = IR[1];
|
|
|
|
var domain = IR[2];
|
|
|
|
var encode = IR[3];
|
|
|
|
var decode = IR[4];
|
|
|
|
var samples = IR[5];
|
|
|
|
var size = IR[6];
|
|
|
|
var n = IR[7];
|
2017-01-20 00:26:32 +09:00
|
|
|
// var mask = IR[8];
|
2012-01-30 18:38:28 +09:00
|
|
|
var range = IR[9];
|
|
|
|
|
2012-02-01 11:19:44 +09:00
|
|
|
// Building the cube vertices: its part and sample index
|
|
|
|
// http://rjwagner49.com/Mathematics/Interpolation.pdf
|
|
|
|
var cubeVertices = 1 << m;
|
|
|
|
var cubeN = new Float64Array(cubeVertices);
|
|
|
|
var cubeVertex = new Uint32Array(cubeVertices);
|
2014-04-08 04:05:17 +09:00
|
|
|
var i, j;
|
|
|
|
for (j = 0; j < cubeVertices; j++) {
|
2012-02-01 11:19:44 +09:00
|
|
|
cubeN[j] = 1;
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2012-02-01 11:19:44 +09:00
|
|
|
|
|
|
|
var k = n, pos = 1;
|
2012-01-30 18:38:28 +09:00
|
|
|
// Map x_i to y_j for 0 <= i < m using the sampled function.
|
2014-04-08 04:05:17 +09:00
|
|
|
for (i = 0; i < m; ++i) {
|
2012-01-30 18:38:28 +09:00
|
|
|
// x_i' = min(max(x_i, Domain_2i), Domain_2i+1)
|
2012-02-01 11:19:44 +09:00
|
|
|
var domain_2i = domain[i][0];
|
|
|
|
var domain_2i_1 = domain[i][1];
|
2016-11-01 23:04:21 +09:00
|
|
|
var xi = Math.min(Math.max(src[srcOffset + i], domain_2i),
|
2014-08-05 01:12:51 +09:00
|
|
|
domain_2i_1);
|
2012-01-30 18:38:28 +09:00
|
|
|
|
2012-02-01 11:19:44 +09:00
|
|
|
// e_i = Interpolate(x_i', Domain_2i, Domain_2i+1,
|
|
|
|
// Encode_2i, Encode_2i+1)
|
|
|
|
var e = interpolate(xi, domain_2i, domain_2i_1,
|
|
|
|
encode[i][0], encode[i][1]);
|
2012-01-30 18:38:28 +09:00
|
|
|
|
|
|
|
// e_i' = min(max(e_i, 0), Size_i - 1)
|
2012-02-01 11:19:44 +09:00
|
|
|
var size_i = size[i];
|
|
|
|
e = Math.min(Math.max(e, 0), size_i - 1);
|
|
|
|
|
|
|
|
// Adjusting the cube: N and vertex sample index
|
|
|
|
var e0 = e < size_i - 1 ? Math.floor(e) : e - 1; // e1 = e0 + 1;
|
|
|
|
var n0 = e0 + 1 - e; // (e1 - e) / (e1 - e0);
|
|
|
|
var n1 = e - e0; // (e - e0) / (e1 - e0);
|
|
|
|
var offset0 = e0 * k;
|
|
|
|
var offset1 = offset0 + k; // e1 * k
|
2014-04-08 04:05:17 +09:00
|
|
|
for (j = 0; j < cubeVertices; j++) {
|
2012-02-01 11:19:44 +09:00
|
|
|
if (j & pos) {
|
|
|
|
cubeN[j] *= n1;
|
|
|
|
cubeVertex[j] += offset1;
|
|
|
|
} else {
|
|
|
|
cubeN[j] *= n0;
|
|
|
|
cubeVertex[j] += offset0;
|
|
|
|
}
|
|
|
|
}
|
2012-01-30 18:38:28 +09:00
|
|
|
|
2012-02-01 11:19:44 +09:00
|
|
|
k *= size_i;
|
|
|
|
pos <<= 1;
|
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2014-04-08 04:05:17 +09:00
|
|
|
for (j = 0; j < n; ++j) {
|
2012-02-01 11:19:44 +09:00
|
|
|
// Sum all cube vertices' samples portions
|
|
|
|
var rj = 0;
|
2014-04-08 04:05:17 +09:00
|
|
|
for (i = 0; i < cubeVertices; i++) {
|
2012-02-01 11:19:44 +09:00
|
|
|
rj += samples[cubeVertex[i] + j] * cubeN[i];
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2012-01-30 18:38:28 +09:00
|
|
|
|
2012-02-01 11:19:44 +09:00
|
|
|
// r_j' = Interpolate(r_j, 0, 2^BitsPerSample - 1,
|
|
|
|
// Decode_2j, Decode_2j+1)
|
|
|
|
rj = interpolate(rj, 0, 1, decode[j][0], decode[j][1]);
|
2012-01-30 18:38:28 +09:00
|
|
|
|
2012-02-01 11:19:44 +09:00
|
|
|
// y_j = min(max(r_j, range_2j), range_2j+1)
|
2014-08-05 01:12:51 +09:00
|
|
|
dest[destOffset + j] = Math.min(Math.max(rj, range[j][0]),
|
|
|
|
range[j][1]);
|
2011-10-25 08:55:23 +09:00
|
|
|
}
|
2013-02-01 08:32:01 +09:00
|
|
|
};
|
2011-10-25 08:55:23 +09:00
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructInterpolated: function PDFFunction_constructInterpolated(str,
|
|
|
|
dict) {
|
2016-05-06 02:16:35 +09:00
|
|
|
var c0 = dict.getArray('C0') || [0];
|
|
|
|
var c1 = dict.getArray('C1') || [1];
|
2011-10-25 08:55:23 +09:00
|
|
|
var n = dict.get('N');
|
|
|
|
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!isArray(c0) || !isArray(c1)) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError(
|
|
|
|
'Illegal dictionary for interpolated function');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var length = c0.length;
|
|
|
|
var diff = [];
|
2014-03-03 21:34:12 +09:00
|
|
|
for (var i = 0; i < length; ++i) {
|
2011-10-25 08:55:23 +09:00
|
|
|
diff.push(c1[i] - c0[i]);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
return [CONSTRUCT_INTERPOLATED, c0, diff, n];
|
|
|
|
},
|
|
|
|
|
|
|
|
constructInterpolatedFromIR:
|
2012-04-05 05:43:26 +09:00
|
|
|
function PDFFunction_constructInterpolatedFromIR(IR) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var c0 = IR[1];
|
|
|
|
var diff = IR[2];
|
|
|
|
var n = IR[3];
|
|
|
|
|
|
|
|
var length = diff.length;
|
|
|
|
|
2014-08-05 01:12:51 +09:00
|
|
|
return function constructInterpolatedFromIRResult(src, srcOffset,
|
|
|
|
dest, destOffset) {
|
|
|
|
var x = n === 1 ? src[srcOffset] : Math.pow(src[srcOffset], n);
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2014-03-03 21:34:12 +09:00
|
|
|
for (var j = 0; j < length; ++j) {
|
2014-08-05 01:12:51 +09:00
|
|
|
dest[destOffset + j] = c0[j] + (x * diff[j]);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2013-02-01 08:32:01 +09:00
|
|
|
};
|
2011-10-25 08:55:23 +09:00
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructStiched: function PDFFunction_constructStiched(fn, dict, xref) {
|
2016-05-06 02:16:35 +09:00
|
|
|
var domain = dict.getArray('Domain');
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!domain) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError('No domain');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var inputSize = domain.length / 2;
|
2014-08-02 04:02:56 +09:00
|
|
|
if (inputSize !== 1) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError('Bad domain for stiched function');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2012-04-05 03:43:04 +09:00
|
|
|
var fnRefs = dict.get('Functions');
|
2011-10-25 08:55:23 +09:00
|
|
|
var fns = [];
|
2014-03-03 21:34:12 +09:00
|
|
|
for (var i = 0, ii = fnRefs.length; i < ii; ++i) {
|
2011-10-25 08:55:23 +09:00
|
|
|
fns.push(PDFFunction.getIR(xref, xref.fetchIfRef(fnRefs[i])));
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2016-05-06 02:16:35 +09:00
|
|
|
var bounds = dict.getArray('Bounds');
|
|
|
|
var encode = dict.getArray('Encode');
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
return [CONSTRUCT_STICHED, domain, bounds, encode, fns];
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructStichedFromIR: function PDFFunction_constructStichedFromIR(IR) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var domain = IR[1];
|
|
|
|
var bounds = IR[2];
|
|
|
|
var encode = IR[3];
|
|
|
|
var fnsIR = IR[4];
|
|
|
|
var fns = [];
|
2014-08-05 01:12:51 +09:00
|
|
|
var tmpBuf = new Float32Array(1);
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2011-11-03 04:08:19 +09:00
|
|
|
for (var i = 0, ii = fnsIR.length; i < ii; i++) {
|
2011-10-25 08:55:23 +09:00
|
|
|
fns.push(PDFFunction.fromIR(fnsIR[i]));
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:12:51 +09:00
|
|
|
return function constructStichedFromIRResult(src, srcOffset,
|
|
|
|
dest, destOffset) {
|
2011-10-30 02:59:49 +09:00
|
|
|
var clip = function constructStichedFromIRClip(v, min, max) {
|
2014-03-03 21:34:12 +09:00
|
|
|
if (v > max) {
|
2011-10-25 08:55:23 +09:00
|
|
|
v = max;
|
2014-03-03 21:34:12 +09:00
|
|
|
} else if (v < min) {
|
2011-10-25 08:55:23 +09:00
|
|
|
v = min;
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
return v;
|
|
|
|
};
|
|
|
|
|
|
|
|
// clip to domain
|
2014-08-05 01:12:51 +09:00
|
|
|
var v = clip(src[srcOffset], domain[0], domain[1]);
|
2016-07-17 21:33:41 +09:00
|
|
|
// calculate which bound the value is in
|
2011-10-25 08:55:23 +09:00
|
|
|
for (var i = 0, ii = bounds.length; i < ii; ++i) {
|
2014-03-03 21:34:12 +09:00
|
|
|
if (v < bounds[i]) {
|
2011-10-25 08:55:23 +09:00
|
|
|
break;
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// encode value into domain of function
|
|
|
|
var dmin = domain[0];
|
2014-03-03 21:34:12 +09:00
|
|
|
if (i > 0) {
|
2011-10-25 08:55:23 +09:00
|
|
|
dmin = bounds[i - 1];
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
var dmax = domain[1];
|
2014-03-03 21:34:12 +09:00
|
|
|
if (i < bounds.length) {
|
2011-10-25 08:55:23 +09:00
|
|
|
dmax = bounds[i];
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
|
|
|
|
var rmin = encode[2 * i];
|
|
|
|
var rmax = encode[2 * i + 1];
|
|
|
|
|
2015-06-14 20:44:43 +09:00
|
|
|
// Prevent the value from becoming NaN as a result
|
|
|
|
// of division by zero (fixes issue6113.pdf).
|
|
|
|
tmpBuf[0] = dmin === dmax ? rmin :
|
|
|
|
rmin + (v - dmin) * (rmax - rmin) / (dmax - dmin);
|
2011-10-25 08:55:23 +09:00
|
|
|
|
2014-03-09 06:40:19 +09:00
|
|
|
// call the appropriate function
|
2014-08-05 01:12:51 +09:00
|
|
|
fns[i](tmpBuf, 0, dest, destOffset);
|
2011-10-25 08:55:23 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructPostScript: function PDFFunction_constructPostScript(fn, dict,
|
2011-12-24 14:19:15 +09:00
|
|
|
xref) {
|
2016-05-06 02:16:35 +09:00
|
|
|
var domain = dict.getArray('Domain');
|
|
|
|
var range = dict.getArray('Range');
|
2011-12-24 12:41:12 +09:00
|
|
|
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!domain) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError('No domain.');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!range) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError('No range.');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
|
|
|
|
var lexer = new PostScriptLexer(fn);
|
|
|
|
var parser = new PostScriptParser(lexer);
|
|
|
|
var code = parser.parse();
|
|
|
|
|
|
|
|
return [CONSTRUCT_POSTSCRIPT, domain, range, code];
|
2011-10-25 08:55:23 +09:00
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructPostScriptFromIR: function PDFFunction_constructPostScriptFromIR(
|
|
|
|
IR) {
|
2011-12-24 12:41:12 +09:00
|
|
|
var domain = IR[1];
|
|
|
|
var range = IR[2];
|
|
|
|
var code = IR[3];
|
2014-08-04 23:49:05 +09:00
|
|
|
|
|
|
|
var compiled = (new PostScriptCompiler()).compile(code, domain, range);
|
|
|
|
if (compiled) {
|
|
|
|
// Compiled function consists of simple expressions such as addition,
|
|
|
|
// subtraction, Math.max, and also contains 'var' and 'return'
|
|
|
|
// statements. See the generation in the PostScriptCompiler below.
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
// eslint-disable-next-line no-new-func
|
2014-08-05 01:12:51 +09:00
|
|
|
return new Function('src', 'srcOffset', 'dest', 'destOffset', compiled);
|
2014-08-04 23:49:05 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
info('Unable to compile PS function');
|
|
|
|
|
2014-05-14 23:06:28 +09:00
|
|
|
var numOutputs = range.length >> 1;
|
|
|
|
var numInputs = domain.length >> 1;
|
2011-12-30 06:41:54 +09:00
|
|
|
var evaluator = new PostScriptEvaluator(code);
|
2011-12-24 12:41:12 +09:00
|
|
|
// Cache the values for a big speed up, the cache size is limited though
|
|
|
|
// since the number of possible values can be huge from a PS function.
|
2016-01-28 02:04:13 +09:00
|
|
|
var cache = Object.create(null);
|
2014-05-14 23:06:28 +09:00
|
|
|
// The MAX_CACHE_SIZE is set to ~4x the maximum number of distinct values
|
|
|
|
// seen in our tests.
|
|
|
|
var MAX_CACHE_SIZE = 2048 * 4;
|
|
|
|
var cache_available = MAX_CACHE_SIZE;
|
2014-08-05 01:12:51 +09:00
|
|
|
var tmpBuf = new Float32Array(numInputs);
|
|
|
|
|
|
|
|
return function constructPostScriptFromIRResult(src, srcOffset,
|
|
|
|
dest, destOffset) {
|
2014-05-14 23:06:28 +09:00
|
|
|
var i, value;
|
|
|
|
var key = '';
|
2014-08-05 01:12:51 +09:00
|
|
|
var input = tmpBuf;
|
2014-05-14 23:06:28 +09:00
|
|
|
for (i = 0; i < numInputs; i++) {
|
2014-08-05 01:12:51 +09:00
|
|
|
value = src[srcOffset + i];
|
2014-05-14 23:06:28 +09:00
|
|
|
input[i] = value;
|
|
|
|
key += value + '_';
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
|
2014-05-14 23:06:28 +09:00
|
|
|
var cachedValue = cache[key];
|
|
|
|
if (cachedValue !== undefined) {
|
2014-11-04 20:17:27 +09:00
|
|
|
dest.set(cachedValue, destOffset);
|
2014-08-05 01:12:51 +09:00
|
|
|
return;
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
|
2014-08-05 01:12:51 +09:00
|
|
|
var output = new Float32Array(numOutputs);
|
2014-05-14 23:06:28 +09:00
|
|
|
var stack = evaluator.execute(input);
|
|
|
|
var stackIndex = stack.length - numOutputs;
|
|
|
|
for (i = 0; i < numOutputs; i++) {
|
|
|
|
value = stack[stackIndex + i];
|
|
|
|
var bound = range[i * 2];
|
|
|
|
if (value < bound) {
|
|
|
|
value = bound;
|
|
|
|
} else {
|
2016-11-01 23:04:21 +09:00
|
|
|
bound = range[i * 2 + 1];
|
2014-05-14 23:06:28 +09:00
|
|
|
if (value > bound) {
|
|
|
|
value = bound;
|
|
|
|
}
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2014-05-14 23:06:28 +09:00
|
|
|
output[i] = value;
|
|
|
|
}
|
|
|
|
if (cache_available > 0) {
|
|
|
|
cache_available--;
|
|
|
|
cache[key] = output;
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
2014-11-04 20:17:27 +09:00
|
|
|
dest.set(output, destOffset);
|
2011-10-25 08:55:23 +09:00
|
|
|
};
|
Fix inconsistent spacing and trailing commas in objects in `src/core/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
*Unfortunately this patch is fairly big, even though it only covers the `src/core` folder, but splitting it even further seemed difficult.*
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
Please note: This patch was created automatically, using the ESLint --fix command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index abab9027..dcd3594b 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2785,7 +2785,8 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
t['Tz'] = { id: OPS.setHScale, numArgs: 1, variableArgs: false, };
t['TL'] = { id: OPS.setLeading, numArgs: 1, variableArgs: false, };
t['Tf'] = { id: OPS.setFont, numArgs: 2, variableArgs: false, };
- t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false, };
+ t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1,
+ variableArgs: false, };
t['Ts'] = { id: OPS.setTextRise, numArgs: 1, variableArgs: false, };
t['Td'] = { id: OPS.moveText, numArgs: 2, variableArgs: false, };
t['TD'] = { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false, };
diff --git a/src/core/jbig2.js b/src/core/jbig2.js
index 5a17d482..71671541 100644
--- a/src/core/jbig2.js
+++ b/src/core/jbig2.js
@@ -123,19 +123,22 @@ var Jbig2Image = (function Jbig2ImageClosure() {
{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -2, y: 0, },
{ x: -1, y: 0, }],
[{ x: -3, y: -1, }, { x: -2, y: -1, }, { x: -1, y: -1, }, { x: 0, y: -1, },
- { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, { x: -1, y: 0, }]
+ { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, },
+ { x: -1, y: 0, }]
];
var RefinementTemplates = [
{
coding: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
- { x: 1, y: 0, }, { x: -1, y: 1, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
+ reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, },
+ { x: 0, y: 0, }, { x: 1, y: 0, }, { x: -1, y: 1, },
+ { x: 0, y: 1, }, { x: 1, y: 1, }],
},
{
- coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, { x: 1, y: 0, },
- { x: 0, y: 1, }, { x: 1, y: 1, }],
+ coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, },
+ { x: -1, y: 0, }],
+ reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
+ { x: 1, y: 0, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
}
];
```
2017-06-02 18:16:24 +09:00
|
|
|
},
|
2011-10-25 08:55:23 +09:00
|
|
|
};
|
|
|
|
})();
|
2011-10-28 03:51:10 +09:00
|
|
|
|
2014-05-22 03:06:23 +09:00
|
|
|
function isPDFFunction(v) {
|
|
|
|
var fnDict;
|
2014-08-02 04:02:56 +09:00
|
|
|
if (typeof v !== 'object') {
|
2014-05-22 03:06:23 +09:00
|
|
|
return false;
|
|
|
|
} else if (isDict(v)) {
|
|
|
|
fnDict = v;
|
|
|
|
} else if (isStream(v)) {
|
|
|
|
fnDict = v.dict;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return fnDict.has('FunctionType');
|
|
|
|
}
|
|
|
|
|
2011-12-31 02:24:13 +09:00
|
|
|
var PostScriptStack = (function PostScriptStackClosure() {
|
2011-12-24 12:41:12 +09:00
|
|
|
var MAX_STACK_SIZE = 100;
|
|
|
|
function PostScriptStack(initialStack) {
|
2014-08-05 01:12:51 +09:00
|
|
|
this.stack = !initialStack ? [] :
|
|
|
|
Array.prototype.slice.call(initialStack, 0);
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
PostScriptStack.prototype = {
|
2012-04-05 05:43:26 +09:00
|
|
|
push: function PostScriptStack_push(value) {
|
2014-03-03 21:34:12 +09:00
|
|
|
if (this.stack.length >= MAX_STACK_SIZE) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new Error('PostScript function stack overflow.');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
this.stack.push(value);
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
pop: function PostScriptStack_pop() {
|
2014-03-03 21:34:12 +09:00
|
|
|
if (this.stack.length <= 0) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new Error('PostScript function stack underflow.');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
return this.stack.pop();
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
copy: function PostScriptStack_copy(n) {
|
2014-03-03 21:34:12 +09:00
|
|
|
if (this.stack.length + n >= MAX_STACK_SIZE) {
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new Error('PostScript function stack overflow.');
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-31 07:59:00 +09:00
|
|
|
var stack = this.stack;
|
2014-03-03 21:34:12 +09:00
|
|
|
for (var i = stack.length - n, j = n - 1; j >= 0; j--, i++) {
|
2011-12-31 07:59:00 +09:00
|
|
|
stack.push(stack[i]);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
index: function PostScriptStack_index(n) {
|
2011-12-24 12:41:12 +09:00
|
|
|
this.push(this.stack[this.stack.length - n - 1]);
|
|
|
|
},
|
2011-12-30 06:41:54 +09:00
|
|
|
// rotate the last n stack elements p times
|
2012-04-05 05:43:26 +09:00
|
|
|
roll: function PostScriptStack_roll(n, p) {
|
2011-12-31 02:24:13 +09:00
|
|
|
var stack = this.stack;
|
|
|
|
var l = stack.length - n;
|
|
|
|
var r = stack.length - 1, c = l + (p - Math.floor(p / n) * n), i, j, t;
|
|
|
|
for (i = l, j = r; i < j; i++, j--) {
|
|
|
|
t = stack[i]; stack[i] = stack[j]; stack[j] = t;
|
|
|
|
}
|
|
|
|
for (i = l, j = c - 1; i < j; i++, j--) {
|
|
|
|
t = stack[i]; stack[i] = stack[j]; stack[j] = t;
|
|
|
|
}
|
|
|
|
for (i = c, j = r; i < j; i++, j--) {
|
|
|
|
t = stack[i]; stack[i] = stack[j]; stack[j] = t;
|
|
|
|
}
|
Fix inconsistent spacing and trailing commas in objects in `src/core/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
*Unfortunately this patch is fairly big, even though it only covers the `src/core` folder, but splitting it even further seemed difficult.*
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
Please note: This patch was created automatically, using the ESLint --fix command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index abab9027..dcd3594b 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2785,7 +2785,8 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
t['Tz'] = { id: OPS.setHScale, numArgs: 1, variableArgs: false, };
t['TL'] = { id: OPS.setLeading, numArgs: 1, variableArgs: false, };
t['Tf'] = { id: OPS.setFont, numArgs: 2, variableArgs: false, };
- t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false, };
+ t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1,
+ variableArgs: false, };
t['Ts'] = { id: OPS.setTextRise, numArgs: 1, variableArgs: false, };
t['Td'] = { id: OPS.moveText, numArgs: 2, variableArgs: false, };
t['TD'] = { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false, };
diff --git a/src/core/jbig2.js b/src/core/jbig2.js
index 5a17d482..71671541 100644
--- a/src/core/jbig2.js
+++ b/src/core/jbig2.js
@@ -123,19 +123,22 @@ var Jbig2Image = (function Jbig2ImageClosure() {
{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -2, y: 0, },
{ x: -1, y: 0, }],
[{ x: -3, y: -1, }, { x: -2, y: -1, }, { x: -1, y: -1, }, { x: 0, y: -1, },
- { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, { x: -1, y: 0, }]
+ { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, },
+ { x: -1, y: 0, }]
];
var RefinementTemplates = [
{
coding: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
- { x: 1, y: 0, }, { x: -1, y: 1, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
+ reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, },
+ { x: 0, y: 0, }, { x: 1, y: 0, }, { x: -1, y: 1, },
+ { x: 0, y: 1, }, { x: 1, y: 1, }],
},
{
- coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, { x: 1, y: 0, },
- { x: 0, y: 1, }, { x: 1, y: 1, }],
+ coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, },
+ { x: -1, y: 0, }],
+ reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
+ { x: 1, y: 0, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
}
];
```
2017-06-02 18:16:24 +09:00
|
|
|
},
|
2011-12-24 12:41:12 +09:00
|
|
|
};
|
|
|
|
return PostScriptStack;
|
|
|
|
})();
|
2011-12-31 02:24:13 +09:00
|
|
|
var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
|
2014-01-28 04:50:30 +09:00
|
|
|
function PostScriptEvaluator(operators) {
|
2011-12-29 13:08:18 +09:00
|
|
|
this.operators = operators;
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
PostScriptEvaluator.prototype = {
|
2012-04-05 05:43:26 +09:00
|
|
|
execute: function PostScriptEvaluator_execute(initialStack) {
|
2011-12-24 12:41:12 +09:00
|
|
|
var stack = new PostScriptStack(initialStack);
|
|
|
|
var counter = 0;
|
2011-12-29 13:08:18 +09:00
|
|
|
var operators = this.operators;
|
|
|
|
var length = operators.length;
|
2011-12-30 06:41:54 +09:00
|
|
|
var operator, a, b;
|
2011-12-29 13:08:18 +09:00
|
|
|
while (counter < length) {
|
2011-12-30 06:41:54 +09:00
|
|
|
operator = operators[counter++];
|
2014-08-02 04:02:56 +09:00
|
|
|
if (typeof operator === 'number') {
|
2011-12-30 06:41:54 +09:00
|
|
|
// Operator is really an operand and should be pushed to the stack.
|
|
|
|
stack.push(operator);
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
switch (operator) {
|
|
|
|
// non standard ps operators
|
|
|
|
case 'jz': // jump if false
|
2011-12-30 06:41:54 +09:00
|
|
|
b = stack.pop();
|
2011-12-24 12:41:12 +09:00
|
|
|
a = stack.pop();
|
2014-03-03 21:34:12 +09:00
|
|
|
if (!a) {
|
2011-12-30 06:41:54 +09:00
|
|
|
counter = b;
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'j': // jump
|
2011-12-30 06:41:54 +09:00
|
|
|
a = stack.pop();
|
|
|
|
counter = a;
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
|
|
|
|
// all ps operators in alphabetical order (excluding if/ifelse)
|
|
|
|
case 'abs':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.abs(a));
|
|
|
|
break;
|
|
|
|
case 'add':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a + b);
|
|
|
|
break;
|
|
|
|
case 'and':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2014-03-03 21:34:12 +09:00
|
|
|
if (isBool(a) && isBool(b)) {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a && b);
|
2014-03-03 21:34:12 +09:00
|
|
|
} else {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a & b);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'atan':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.atan(a));
|
|
|
|
break;
|
|
|
|
case 'bitshift':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2014-03-03 21:34:12 +09:00
|
|
|
if (a > 0) {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a << b);
|
2014-03-03 21:34:12 +09:00
|
|
|
} else {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a >> b);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'ceiling':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.ceil(a));
|
|
|
|
break;
|
|
|
|
case 'copy':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.copy(a);
|
|
|
|
break;
|
|
|
|
case 'cos':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.cos(a));
|
|
|
|
break;
|
|
|
|
case 'cvi':
|
2011-12-31 06:25:34 +09:00
|
|
|
a = stack.pop() | 0;
|
2011-12-31 02:24:13 +09:00
|
|
|
stack.push(a);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'cvr':
|
|
|
|
// noop
|
|
|
|
break;
|
|
|
|
case 'div':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a / b);
|
|
|
|
break;
|
|
|
|
case 'dup':
|
|
|
|
stack.copy(1);
|
|
|
|
break;
|
|
|
|
case 'eq':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2014-08-02 04:02:56 +09:00
|
|
|
stack.push(a === b);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'exch':
|
|
|
|
stack.roll(2, 1);
|
|
|
|
break;
|
|
|
|
case 'exp':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.pow(a, b));
|
|
|
|
break;
|
|
|
|
case 'false':
|
|
|
|
stack.push(false);
|
|
|
|
break;
|
|
|
|
case 'floor':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.floor(a));
|
|
|
|
break;
|
|
|
|
case 'ge':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a >= b);
|
|
|
|
break;
|
|
|
|
case 'gt':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a > b);
|
|
|
|
break;
|
|
|
|
case 'idiv':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2011-12-31 06:25:34 +09:00
|
|
|
stack.push((a / b) | 0);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'index':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.index(a);
|
|
|
|
break;
|
|
|
|
case 'le':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a <= b);
|
|
|
|
break;
|
|
|
|
case 'ln':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.log(a));
|
|
|
|
break;
|
|
|
|
case 'log':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.log(a) / Math.LN10);
|
|
|
|
break;
|
|
|
|
case 'lt':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a < b);
|
|
|
|
break;
|
|
|
|
case 'mod':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a % b);
|
|
|
|
break;
|
|
|
|
case 'mul':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a * b);
|
|
|
|
break;
|
|
|
|
case 'ne':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2014-08-02 04:02:56 +09:00
|
|
|
stack.push(a !== b);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'neg':
|
|
|
|
a = stack.pop();
|
2014-05-03 06:52:26 +09:00
|
|
|
stack.push(-a);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'not':
|
|
|
|
a = stack.pop();
|
2014-05-03 06:52:26 +09:00
|
|
|
if (isBool(a)) {
|
|
|
|
stack.push(!a);
|
2014-03-03 21:34:12 +09:00
|
|
|
} else {
|
2014-05-03 06:52:26 +09:00
|
|
|
stack.push(~a);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'or':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2014-03-03 21:34:12 +09:00
|
|
|
if (isBool(a) && isBool(b)) {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a || b);
|
2014-03-03 21:34:12 +09:00
|
|
|
} else {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a | b);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'pop':
|
|
|
|
stack.pop();
|
|
|
|
break;
|
|
|
|
case 'roll':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.roll(a, b);
|
|
|
|
break;
|
|
|
|
case 'round':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.round(a));
|
|
|
|
break;
|
|
|
|
case 'sin':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.sin(a));
|
|
|
|
break;
|
|
|
|
case 'sqrt':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.sqrt(a));
|
|
|
|
break;
|
|
|
|
case 'sub':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(a - b);
|
|
|
|
break;
|
|
|
|
case 'true':
|
|
|
|
stack.push(true);
|
|
|
|
break;
|
|
|
|
case 'truncate':
|
|
|
|
a = stack.pop();
|
2011-12-31 06:38:09 +09:00
|
|
|
a = a < 0 ? Math.ceil(a) : Math.floor(a);
|
|
|
|
stack.push(a);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'xor':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
2014-03-03 21:34:12 +09:00
|
|
|
if (isBool(a) && isBool(b)) {
|
2014-08-02 04:02:56 +09:00
|
|
|
stack.push(a !== b);
|
2014-03-03 21:34:12 +09:00
|
|
|
} else {
|
2011-12-24 12:41:12 +09:00
|
|
|
stack.push(a ^ b);
|
2014-03-03 21:34:12 +09:00
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
default:
|
2017-06-29 05:51:31 +09:00
|
|
|
throw new FormatError(`Unknown operator ${operator}`);
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack.stack;
|
Fix inconsistent spacing and trailing commas in objects in `src/core/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
*Unfortunately this patch is fairly big, even though it only covers the `src/core` folder, but splitting it even further seemed difficult.*
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
Please note: This patch was created automatically, using the ESLint --fix command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index abab9027..dcd3594b 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2785,7 +2785,8 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
t['Tz'] = { id: OPS.setHScale, numArgs: 1, variableArgs: false, };
t['TL'] = { id: OPS.setLeading, numArgs: 1, variableArgs: false, };
t['Tf'] = { id: OPS.setFont, numArgs: 2, variableArgs: false, };
- t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false, };
+ t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1,
+ variableArgs: false, };
t['Ts'] = { id: OPS.setTextRise, numArgs: 1, variableArgs: false, };
t['Td'] = { id: OPS.moveText, numArgs: 2, variableArgs: false, };
t['TD'] = { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false, };
diff --git a/src/core/jbig2.js b/src/core/jbig2.js
index 5a17d482..71671541 100644
--- a/src/core/jbig2.js
+++ b/src/core/jbig2.js
@@ -123,19 +123,22 @@ var Jbig2Image = (function Jbig2ImageClosure() {
{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -2, y: 0, },
{ x: -1, y: 0, }],
[{ x: -3, y: -1, }, { x: -2, y: -1, }, { x: -1, y: -1, }, { x: 0, y: -1, },
- { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, { x: -1, y: 0, }]
+ { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, },
+ { x: -1, y: 0, }]
];
var RefinementTemplates = [
{
coding: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
- { x: 1, y: 0, }, { x: -1, y: 1, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
+ reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, },
+ { x: 0, y: 0, }, { x: 1, y: 0, }, { x: -1, y: 1, },
+ { x: 0, y: 1, }, { x: 1, y: 1, }],
},
{
- coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, { x: 1, y: 0, },
- { x: 0, y: 1, }, { x: 1, y: 1, }],
+ coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, },
+ { x: -1, y: 0, }],
+ reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
+ { x: 1, y: 0, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
}
];
```
2017-06-02 18:16:24 +09:00
|
|
|
},
|
2011-12-24 14:19:15 +09:00
|
|
|
};
|
2011-12-24 12:41:12 +09:00
|
|
|
return PostScriptEvaluator;
|
|
|
|
})();
|
2014-08-04 23:49:05 +09:00
|
|
|
|
|
|
|
// Most of the PDFs functions consist of simple operations such as:
|
|
|
|
// roll, exch, sub, cvr, pop, index, dup, mul, if, gt, add.
|
|
|
|
//
|
|
|
|
// We can compile most of such programs, and at the same moment, we can
|
|
|
|
// optimize some expressions using basic math properties. Keeping track of
|
|
|
|
// min/max values will allow us to avoid extra Math.min/Math.max calls.
|
|
|
|
var PostScriptCompiler = (function PostScriptCompilerClosure() {
|
|
|
|
function AstNode(type) {
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
AstNode.prototype.visit = function (visitor) {
|
|
|
|
throw new Error('abstract method');
|
|
|
|
};
|
|
|
|
|
|
|
|
function AstArgument(index, min, max) {
|
|
|
|
AstNode.call(this, 'args');
|
|
|
|
this.index = index;
|
|
|
|
this.min = min;
|
|
|
|
this.max = max;
|
|
|
|
}
|
|
|
|
AstArgument.prototype = Object.create(AstNode.prototype);
|
|
|
|
AstArgument.prototype.visit = function (visitor) {
|
|
|
|
visitor.visitArgument(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
function AstLiteral(number) {
|
|
|
|
AstNode.call(this, 'literal');
|
|
|
|
this.number = number;
|
|
|
|
this.min = number;
|
|
|
|
this.max = number;
|
|
|
|
}
|
|
|
|
AstLiteral.prototype = Object.create(AstNode.prototype);
|
|
|
|
AstLiteral.prototype.visit = function (visitor) {
|
|
|
|
visitor.visitLiteral(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
function AstBinaryOperation(op, arg1, arg2, min, max) {
|
|
|
|
AstNode.call(this, 'binary');
|
|
|
|
this.op = op;
|
|
|
|
this.arg1 = arg1;
|
|
|
|
this.arg2 = arg2;
|
|
|
|
this.min = min;
|
|
|
|
this.max = max;
|
|
|
|
}
|
|
|
|
AstBinaryOperation.prototype = Object.create(AstNode.prototype);
|
|
|
|
AstBinaryOperation.prototype.visit = function (visitor) {
|
|
|
|
visitor.visitBinaryOperation(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
function AstMin(arg, max) {
|
|
|
|
AstNode.call(this, 'max');
|
|
|
|
this.arg = arg;
|
|
|
|
this.min = arg.min;
|
|
|
|
this.max = max;
|
|
|
|
}
|
|
|
|
AstMin.prototype = Object.create(AstNode.prototype);
|
|
|
|
AstMin.prototype.visit = function (visitor) {
|
|
|
|
visitor.visitMin(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
function AstVariable(index, min, max) {
|
|
|
|
AstNode.call(this, 'var');
|
|
|
|
this.index = index;
|
|
|
|
this.min = min;
|
|
|
|
this.max = max;
|
|
|
|
}
|
|
|
|
AstVariable.prototype = Object.create(AstNode.prototype);
|
|
|
|
AstVariable.prototype.visit = function (visitor) {
|
|
|
|
visitor.visitVariable(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
function AstVariableDefinition(variable, arg) {
|
|
|
|
AstNode.call(this, 'definition');
|
|
|
|
this.variable = variable;
|
|
|
|
this.arg = arg;
|
|
|
|
}
|
|
|
|
AstVariableDefinition.prototype = Object.create(AstNode.prototype);
|
|
|
|
AstVariableDefinition.prototype.visit = function (visitor) {
|
|
|
|
visitor.visitVariableDefinition(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
function ExpressionBuilderVisitor() {
|
|
|
|
this.parts = [];
|
|
|
|
}
|
|
|
|
ExpressionBuilderVisitor.prototype = {
|
2017-04-27 19:58:44 +09:00
|
|
|
visitArgument(arg) {
|
2014-08-04 23:49:05 +09:00
|
|
|
this.parts.push('Math.max(', arg.min, ', Math.min(',
|
2014-08-05 01:12:51 +09:00
|
|
|
arg.max, ', src[srcOffset + ', arg.index, ']))');
|
2014-08-04 23:49:05 +09:00
|
|
|
},
|
2017-04-27 19:58:44 +09:00
|
|
|
visitVariable(variable) {
|
2014-08-04 23:49:05 +09:00
|
|
|
this.parts.push('v', variable.index);
|
|
|
|
},
|
2017-04-27 19:58:44 +09:00
|
|
|
visitLiteral(literal) {
|
2014-08-04 23:49:05 +09:00
|
|
|
this.parts.push(literal.number);
|
|
|
|
},
|
2017-04-27 19:58:44 +09:00
|
|
|
visitBinaryOperation(operation) {
|
2014-08-04 23:49:05 +09:00
|
|
|
this.parts.push('(');
|
|
|
|
operation.arg1.visit(this);
|
|
|
|
this.parts.push(' ', operation.op, ' ');
|
|
|
|
operation.arg2.visit(this);
|
|
|
|
this.parts.push(')');
|
|
|
|
},
|
2017-04-27 19:58:44 +09:00
|
|
|
visitVariableDefinition(definition) {
|
2014-08-04 23:49:05 +09:00
|
|
|
this.parts.push('var ');
|
|
|
|
definition.variable.visit(this);
|
|
|
|
this.parts.push(' = ');
|
|
|
|
definition.arg.visit(this);
|
|
|
|
this.parts.push(';');
|
|
|
|
},
|
2017-04-27 19:58:44 +09:00
|
|
|
visitMin(max) {
|
2014-08-04 23:49:05 +09:00
|
|
|
this.parts.push('Math.min(');
|
|
|
|
max.arg.visit(this);
|
|
|
|
this.parts.push(', ', max.max, ')');
|
|
|
|
},
|
2017-04-27 19:58:44 +09:00
|
|
|
toString() {
|
2014-08-04 23:49:05 +09:00
|
|
|
return this.parts.join('');
|
Fix inconsistent spacing and trailing commas in objects in `src/core/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
*Unfortunately this patch is fairly big, even though it only covers the `src/core` folder, but splitting it even further seemed difficult.*
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
Please note: This patch was created automatically, using the ESLint --fix command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index abab9027..dcd3594b 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2785,7 +2785,8 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
t['Tz'] = { id: OPS.setHScale, numArgs: 1, variableArgs: false, };
t['TL'] = { id: OPS.setLeading, numArgs: 1, variableArgs: false, };
t['Tf'] = { id: OPS.setFont, numArgs: 2, variableArgs: false, };
- t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false, };
+ t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1,
+ variableArgs: false, };
t['Ts'] = { id: OPS.setTextRise, numArgs: 1, variableArgs: false, };
t['Td'] = { id: OPS.moveText, numArgs: 2, variableArgs: false, };
t['TD'] = { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false, };
diff --git a/src/core/jbig2.js b/src/core/jbig2.js
index 5a17d482..71671541 100644
--- a/src/core/jbig2.js
+++ b/src/core/jbig2.js
@@ -123,19 +123,22 @@ var Jbig2Image = (function Jbig2ImageClosure() {
{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -2, y: 0, },
{ x: -1, y: 0, }],
[{ x: -3, y: -1, }, { x: -2, y: -1, }, { x: -1, y: -1, }, { x: 0, y: -1, },
- { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, { x: -1, y: 0, }]
+ { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, },
+ { x: -1, y: 0, }]
];
var RefinementTemplates = [
{
coding: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
- { x: 1, y: 0, }, { x: -1, y: 1, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
+ reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, },
+ { x: 0, y: 0, }, { x: 1, y: 0, }, { x: -1, y: 1, },
+ { x: 0, y: 1, }, { x: 1, y: 1, }],
},
{
- coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, { x: 1, y: 0, },
- { x: 0, y: 1, }, { x: 1, y: 1, }],
+ coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, },
+ { x: -1, y: 0, }],
+ reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
+ { x: 1, y: 0, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
}
];
```
2017-06-02 18:16:24 +09:00
|
|
|
},
|
2014-08-04 23:49:05 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
function buildAddOperation(num1, num2) {
|
|
|
|
if (num2.type === 'literal' && num2.number === 0) {
|
|
|
|
// optimization: second operand is 0
|
|
|
|
return num1;
|
|
|
|
}
|
|
|
|
if (num1.type === 'literal' && num1.number === 0) {
|
|
|
|
// optimization: first operand is 0
|
|
|
|
return num2;
|
|
|
|
}
|
|
|
|
if (num2.type === 'literal' && num1.type === 'literal') {
|
|
|
|
// optimization: operands operand are literals
|
|
|
|
return new AstLiteral(num1.number + num2.number);
|
|
|
|
}
|
|
|
|
return new AstBinaryOperation('+', num1, num2,
|
|
|
|
num1.min + num2.min, num1.max + num2.max);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildMulOperation(num1, num2) {
|
|
|
|
if (num2.type === 'literal') {
|
|
|
|
// optimization: second operands is a literal...
|
|
|
|
if (num2.number === 0) {
|
|
|
|
return new AstLiteral(0); // and it's 0
|
|
|
|
} else if (num2.number === 1) {
|
|
|
|
return num1; // and it's 1
|
|
|
|
} else if (num1.type === 'literal') {
|
|
|
|
// ... and first operands is a literal too
|
|
|
|
return new AstLiteral(num1.number * num2.number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num1.type === 'literal') {
|
|
|
|
// optimization: first operands is a literal...
|
|
|
|
if (num1.number === 0) {
|
|
|
|
return new AstLiteral(0); // and it's 0
|
|
|
|
} else if (num1.number === 1) {
|
|
|
|
return num2; // and it's 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var min = Math.min(num1.min * num2.min, num1.min * num2.max,
|
|
|
|
num1.max * num2.min, num1.max * num2.max);
|
|
|
|
var max = Math.max(num1.min * num2.min, num1.min * num2.max,
|
|
|
|
num1.max * num2.min, num1.max * num2.max);
|
|
|
|
return new AstBinaryOperation('*', num1, num2, min, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildSubOperation(num1, num2) {
|
|
|
|
if (num2.type === 'literal') {
|
|
|
|
// optimization: second operands is a literal...
|
|
|
|
if (num2.number === 0) {
|
|
|
|
return num1; // ... and it's 0
|
|
|
|
} else if (num1.type === 'literal') {
|
|
|
|
// ... and first operands is a literal too
|
|
|
|
return new AstLiteral(num1.number - num2.number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (num2.type === 'binary' && num2.op === '-' &&
|
|
|
|
num1.type === 'literal' && num1.number === 1 &&
|
|
|
|
num2.arg1.type === 'literal' && num2.arg1.number === 1) {
|
|
|
|
// optimization for case: 1 - (1 - x)
|
|
|
|
return num2.arg2;
|
|
|
|
}
|
|
|
|
return new AstBinaryOperation('-', num1, num2,
|
|
|
|
num1.min - num2.max, num1.max - num2.min);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildMinOperation(num1, max) {
|
|
|
|
if (num1.min >= max) {
|
|
|
|
// optimization: num1 min value is not less than required max
|
|
|
|
return new AstLiteral(max); // just returning max
|
|
|
|
} else if (num1.max <= max) {
|
|
|
|
// optimization: num1 max value is not greater than required max
|
|
|
|
return num1; // just returning an argument
|
|
|
|
}
|
|
|
|
return new AstMin(num1, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
function PostScriptCompiler() {}
|
|
|
|
PostScriptCompiler.prototype = {
|
|
|
|
compile: function PostScriptCompiler_compile(code, domain, range) {
|
|
|
|
var stack = [];
|
|
|
|
var i, ii;
|
|
|
|
var instructions = [];
|
|
|
|
var inputSize = domain.length >> 1, outputSize = range.length >> 1;
|
|
|
|
var lastRegister = 0;
|
2015-12-17 06:31:30 +09:00
|
|
|
var n, j;
|
2014-08-04 23:49:05 +09:00
|
|
|
var num1, num2, ast1, ast2, tmpVar, item;
|
|
|
|
for (i = 0; i < inputSize; i++) {
|
|
|
|
stack.push(new AstArgument(i, domain[i * 2], domain[i * 2 + 1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0, ii = code.length; i < ii; i++) {
|
|
|
|
item = code[i];
|
|
|
|
if (typeof item === 'number') {
|
|
|
|
stack.push(new AstLiteral(item));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (item) {
|
|
|
|
case 'add':
|
|
|
|
if (stack.length < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
num2 = stack.pop();
|
|
|
|
num1 = stack.pop();
|
|
|
|
stack.push(buildAddOperation(num1, num2));
|
|
|
|
break;
|
|
|
|
case 'cvr':
|
|
|
|
if (stack.length < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'mul':
|
|
|
|
if (stack.length < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
num2 = stack.pop();
|
|
|
|
num1 = stack.pop();
|
|
|
|
stack.push(buildMulOperation(num1, num2));
|
|
|
|
break;
|
|
|
|
case 'sub':
|
|
|
|
if (stack.length < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
num2 = stack.pop();
|
|
|
|
num1 = stack.pop();
|
|
|
|
stack.push(buildSubOperation(num1, num2));
|
|
|
|
break;
|
|
|
|
case 'exch':
|
|
|
|
if (stack.length < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
ast1 = stack.pop(); ast2 = stack.pop();
|
|
|
|
stack.push(ast1, ast2);
|
|
|
|
break;
|
|
|
|
case 'pop':
|
|
|
|
if (stack.length < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
stack.pop();
|
|
|
|
break;
|
|
|
|
case 'index':
|
|
|
|
if (stack.length < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
num1 = stack.pop();
|
|
|
|
if (num1.type !== 'literal') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
n = num1.number;
|
2016-11-01 23:04:21 +09:00
|
|
|
if (n < 0 || (n | 0) !== n || stack.length < n) {
|
2014-08-04 23:49:05 +09:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
ast1 = stack[stack.length - n - 1];
|
|
|
|
if (ast1.type === 'literal' || ast1.type === 'var') {
|
|
|
|
stack.push(ast1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmpVar = new AstVariable(lastRegister++, ast1.min, ast1.max);
|
|
|
|
stack[stack.length - n - 1] = tmpVar;
|
|
|
|
stack.push(tmpVar);
|
|
|
|
instructions.push(new AstVariableDefinition(tmpVar, ast1));
|
|
|
|
break;
|
|
|
|
case 'dup':
|
|
|
|
if (stack.length < 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (typeof code[i + 1] === 'number' && code[i + 2] === 'gt' &&
|
|
|
|
code[i + 3] === i + 7 && code[i + 4] === 'jz' &&
|
|
|
|
code[i + 5] === 'pop' && code[i + 6] === code[i + 1]) {
|
|
|
|
// special case of the commands sequence for the min operation
|
|
|
|
num1 = stack.pop();
|
|
|
|
stack.push(buildMinOperation(num1, code[i + 1]));
|
|
|
|
i += 6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ast1 = stack[stack.length - 1];
|
|
|
|
if (ast1.type === 'literal' || ast1.type === 'var') {
|
|
|
|
// we don't have to save into intermediate variable a literal or
|
|
|
|
// variable.
|
|
|
|
stack.push(ast1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
tmpVar = new AstVariable(lastRegister++, ast1.min, ast1.max);
|
|
|
|
stack[stack.length - 1] = tmpVar;
|
|
|
|
stack.push(tmpVar);
|
|
|
|
instructions.push(new AstVariableDefinition(tmpVar, ast1));
|
|
|
|
break;
|
|
|
|
case 'roll':
|
|
|
|
if (stack.length < 2) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
num2 = stack.pop();
|
|
|
|
num1 = stack.pop();
|
|
|
|
if (num2.type !== 'literal' || num1.type !== 'literal') {
|
|
|
|
// both roll operands must be numbers
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
j = num2.number;
|
|
|
|
n = num1.number;
|
2016-11-01 23:04:21 +09:00
|
|
|
if (n <= 0 || (n | 0) !== n || (j | 0) !== j || stack.length < n) {
|
2014-08-04 23:49:05 +09:00
|
|
|
// ... and integers
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
j = ((j % n) + n) % n;
|
|
|
|
if (j === 0) {
|
|
|
|
break; // just skipping -- there are nothing to rotate
|
|
|
|
}
|
|
|
|
Array.prototype.push.apply(stack,
|
|
|
|
stack.splice(stack.length - n, n - j));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return null; // unsupported operator
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stack.length !== outputSize) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
var result = [];
|
|
|
|
instructions.forEach(function (instruction) {
|
|
|
|
var statementBuilder = new ExpressionBuilderVisitor();
|
|
|
|
instruction.visit(statementBuilder);
|
|
|
|
result.push(statementBuilder.toString());
|
|
|
|
});
|
2014-08-05 01:12:51 +09:00
|
|
|
stack.forEach(function (expr, i) {
|
2014-08-04 23:49:05 +09:00
|
|
|
var statementBuilder = new ExpressionBuilderVisitor();
|
|
|
|
expr.visit(statementBuilder);
|
|
|
|
var min = range[i * 2], max = range[i * 2 + 1];
|
|
|
|
var out = [statementBuilder.toString()];
|
|
|
|
if (min > expr.min) {
|
|
|
|
out.unshift('Math.max(', min, ', ');
|
|
|
|
out.push(')');
|
|
|
|
}
|
|
|
|
if (max < expr.max) {
|
|
|
|
out.unshift('Math.min(', max, ', ');
|
|
|
|
out.push(')');
|
|
|
|
}
|
2014-08-05 01:12:51 +09:00
|
|
|
out.unshift('dest[destOffset + ', i, '] = ');
|
|
|
|
out.push(';');
|
|
|
|
result.push(out.join(''));
|
|
|
|
});
|
2014-08-04 23:49:05 +09:00
|
|
|
return result.join('\n');
|
Fix inconsistent spacing and trailing commas in objects in `src/core/` files, so we can enable the `comma-dangle` and `object-curly-spacing` ESLint rules later on
*Unfortunately this patch is fairly big, even though it only covers the `src/core` folder, but splitting it even further seemed difficult.*
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
Given that we currently have quite inconsistent object formatting, fixing this in *one* big patch probably wouldn't be feasible (since I cannot imagine anyone wanting to review that); hence I've opted to try and do this piecewise instead.
Please note: This patch was created automatically, using the ESLint --fix command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/src/core/evaluator.js b/src/core/evaluator.js
index abab9027..dcd3594b 100644
--- a/src/core/evaluator.js
+++ b/src/core/evaluator.js
@@ -2785,7 +2785,8 @@ var EvaluatorPreprocessor = (function EvaluatorPreprocessorClosure() {
t['Tz'] = { id: OPS.setHScale, numArgs: 1, variableArgs: false, };
t['TL'] = { id: OPS.setLeading, numArgs: 1, variableArgs: false, };
t['Tf'] = { id: OPS.setFont, numArgs: 2, variableArgs: false, };
- t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1, variableArgs: false, };
+ t['Tr'] = { id: OPS.setTextRenderingMode, numArgs: 1,
+ variableArgs: false, };
t['Ts'] = { id: OPS.setTextRise, numArgs: 1, variableArgs: false, };
t['Td'] = { id: OPS.moveText, numArgs: 2, variableArgs: false, };
t['TD'] = { id: OPS.setLeadingMoveText, numArgs: 2, variableArgs: false, };
diff --git a/src/core/jbig2.js b/src/core/jbig2.js
index 5a17d482..71671541 100644
--- a/src/core/jbig2.js
+++ b/src/core/jbig2.js
@@ -123,19 +123,22 @@ var Jbig2Image = (function Jbig2ImageClosure() {
{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -2, y: 0, },
{ x: -1, y: 0, }],
[{ x: -3, y: -1, }, { x: -2, y: -1, }, { x: -1, y: -1, }, { x: 0, y: -1, },
- { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, }, { x: -1, y: 0, }]
+ { x: 1, y: -1, }, { x: -4, y: 0, }, { x: -3, y: 0, }, { x: -2, y: 0, },
+ { x: -1, y: 0, }]
];
var RefinementTemplates = [
{
coding: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
- { x: 1, y: 0, }, { x: -1, y: 1, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
+ reference: [{ x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, },
+ { x: 0, y: 0, }, { x: 1, y: 0, }, { x: -1, y: 1, },
+ { x: 0, y: 1, }, { x: 1, y: 1, }],
},
{
- coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, }, { x: -1, y: 0, }],
- reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, }, { x: 1, y: 0, },
- { x: 0, y: 1, }, { x: 1, y: 1, }],
+ coding: [{ x: -1, y: -1, }, { x: 0, y: -1, }, { x: 1, y: -1, },
+ { x: -1, y: 0, }],
+ reference: [{ x: 0, y: -1, }, { x: -1, y: 0, }, { x: 0, y: 0, },
+ { x: 1, y: 0, }, { x: 0, y: 1, }, { x: 1, y: 1, }],
}
];
```
2017-06-02 18:16:24 +09:00
|
|
|
},
|
2014-08-04 23:49:05 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
return PostScriptCompiler;
|
|
|
|
})();
|
2015-11-22 01:32:47 +09:00
|
|
|
|
2017-04-02 23:14:30 +09:00
|
|
|
export {
|
|
|
|
isPDFFunction,
|
|
|
|
PDFFunction,
|
|
|
|
PostScriptEvaluator,
|
|
|
|
PostScriptCompiler,
|
|
|
|
};
|