2011-10-26 10:18:22 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
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) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var length = 1;
|
2011-11-03 04:08:19 +09:00
|
|
|
for (var i = 0, ii = size.length; i < ii; i++)
|
2011-10-25 08:55:23 +09:00
|
|
|
length *= size[i];
|
|
|
|
length *= outputSize;
|
|
|
|
|
|
|
|
var array = [];
|
|
|
|
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;
|
|
|
|
for (var i = 0; i < length; i++) {
|
|
|
|
while (codeSize < bps) {
|
|
|
|
codeBuf <<= 8;
|
|
|
|
codeBuf |= strBytes[strIdx++];
|
|
|
|
codeSize += 8;
|
|
|
|
}
|
|
|
|
codeSize -= bps;
|
2011-11-12 07:44:47 +09:00
|
|
|
array.push((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;
|
|
|
|
if (!dict)
|
|
|
|
dict = fn;
|
|
|
|
|
|
|
|
var types = [this.constructSampled,
|
|
|
|
null,
|
|
|
|
this.constructInterpolated,
|
|
|
|
this.constructStiched,
|
|
|
|
this.constructPostScript];
|
|
|
|
|
|
|
|
var typeNum = dict.get('FunctionType');
|
|
|
|
var typeFn = types[typeNum];
|
|
|
|
if (!typeFn)
|
|
|
|
error('Unknown type of function');
|
|
|
|
|
|
|
|
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);
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
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;
|
|
|
|
var outputLength = arr.length / 2;
|
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;
|
|
|
|
}
|
2011-10-25 08:55:23 +09:00
|
|
|
var domain = dict.get('Domain');
|
|
|
|
var range = dict.get('Range');
|
|
|
|
|
|
|
|
if (!domain || !range)
|
|
|
|
error('No domain or range');
|
|
|
|
|
|
|
|
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');
|
|
|
|
var order = dict.get('Order');
|
|
|
|
if (!order)
|
|
|
|
order = 1;
|
|
|
|
if (order !== 1)
|
|
|
|
error('No support for cubic spline interpolation: ' + order);
|
|
|
|
|
|
|
|
var encode = dict.get('Encode');
|
|
|
|
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);
|
|
|
|
|
2011-10-25 08:55:23 +09:00
|
|
|
var decode = dict.get('Decode');
|
|
|
|
if (!decode)
|
|
|
|
decode = range;
|
2011-11-12 07:44:47 +09:00
|
|
|
else
|
|
|
|
decode = toMultiArray(decode);
|
|
|
|
|
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
|
|
|
|
2011-10-30 02:59:49 +09:00
|
|
|
return function constructSampledFromIRResult(args) {
|
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];
|
|
|
|
var mask = IR[8];
|
|
|
|
var range = IR[9];
|
|
|
|
|
|
|
|
if (m != args.length)
|
2011-10-25 08:55:23 +09:00
|
|
|
error('Incorrect number of arguments: ' + inputSize + ' != ' +
|
|
|
|
args.length);
|
|
|
|
|
2012-01-30 18:38:28 +09:00
|
|
|
var x = args;
|
2011-10-25 08:55:23 +09:00
|
|
|
|
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);
|
|
|
|
for (var j = 0; j < cubeVertices; j++)
|
|
|
|
cubeN[j] = 1;
|
|
|
|
|
|
|
|
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.
|
|
|
|
for (var i = 0; i < m; ++i) {
|
|
|
|
// 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];
|
2012-01-30 18:38:28 +09:00
|
|
|
var xi = Math.min(Math.max(x[i], domain_2i), domain_2i_1);
|
|
|
|
|
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
|
|
|
|
for (var j = 0; j < cubeVertices; j++) {
|
|
|
|
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
|
|
|
|
2012-02-01 11:19:44 +09:00
|
|
|
var y = new Float64Array(n);
|
|
|
|
for (var j = 0; j < n; ++j) {
|
|
|
|
// Sum all cube vertices' samples portions
|
|
|
|
var rj = 0;
|
|
|
|
for (var i = 0; i < cubeVertices; i++)
|
|
|
|
rj += samples[cubeVertex[i] + j] * cubeN[i];
|
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)
|
|
|
|
y[j] = Math.min(Math.max(rj, range[j][0]), range[j][1]);
|
2011-10-25 08:55:23 +09:00
|
|
|
}
|
2012-01-30 18:38:28 +09:00
|
|
|
|
|
|
|
return y;
|
2011-10-25 08:55:23 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructInterpolated: function PDFFunction_constructInterpolated(str,
|
|
|
|
dict) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var c0 = dict.get('C0') || [0];
|
|
|
|
var c1 = dict.get('C1') || [1];
|
|
|
|
var n = dict.get('N');
|
|
|
|
|
|
|
|
if (!isArray(c0) || !isArray(c1))
|
|
|
|
error('Illegal dictionary for interpolated function');
|
|
|
|
|
|
|
|
var length = c0.length;
|
|
|
|
var diff = [];
|
|
|
|
for (var i = 0; i < length; ++i)
|
|
|
|
diff.push(c1[i] - c0[i]);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2011-10-30 02:59:49 +09:00
|
|
|
return function constructInterpolatedFromIRResult(args) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var x = n == 1 ? args[0] : Math.pow(args[0], n);
|
|
|
|
|
|
|
|
var out = [];
|
|
|
|
for (var j = 0; j < length; ++j)
|
|
|
|
out.push(c0[j] + (x * diff[j]));
|
|
|
|
|
|
|
|
return out;
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructStiched: function PDFFunction_constructStiched(fn, dict, xref) {
|
2011-10-25 08:55:23 +09:00
|
|
|
var domain = dict.get('Domain');
|
|
|
|
|
|
|
|
if (!domain)
|
|
|
|
error('No domain');
|
|
|
|
|
|
|
|
var inputSize = domain.length / 2;
|
|
|
|
if (inputSize != 1)
|
|
|
|
error('Bad domain for stiched function');
|
|
|
|
|
2012-04-05 03:43:04 +09:00
|
|
|
var fnRefs = dict.get('Functions');
|
2011-10-25 08:55:23 +09:00
|
|
|
var fns = [];
|
|
|
|
for (var i = 0, ii = fnRefs.length; i < ii; ++i)
|
|
|
|
fns.push(PDFFunction.getIR(xref, xref.fetchIfRef(fnRefs[i])));
|
|
|
|
|
2012-04-05 03:43:04 +09:00
|
|
|
var bounds = dict.get('Bounds');
|
|
|
|
var encode = dict.get('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 = [];
|
|
|
|
|
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]));
|
|
|
|
}
|
|
|
|
|
2011-10-30 02:59:49 +09:00
|
|
|
return function constructStichedFromIRResult(args) {
|
|
|
|
var clip = function constructStichedFromIRClip(v, min, max) {
|
2011-10-25 08:55:23 +09:00
|
|
|
if (v > max)
|
|
|
|
v = max;
|
|
|
|
else if (v < min)
|
|
|
|
v = min;
|
|
|
|
return v;
|
|
|
|
};
|
|
|
|
|
|
|
|
// clip to domain
|
|
|
|
var v = clip(args[0], domain[0], domain[1]);
|
|
|
|
// calulate which bound the value is in
|
|
|
|
for (var i = 0, ii = bounds.length; i < ii; ++i) {
|
|
|
|
if (v < bounds[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// encode value into domain of function
|
|
|
|
var dmin = domain[0];
|
|
|
|
if (i > 0)
|
|
|
|
dmin = bounds[i - 1];
|
|
|
|
var dmax = domain[1];
|
|
|
|
if (i < bounds.length)
|
|
|
|
dmax = bounds[i];
|
|
|
|
|
|
|
|
var rmin = encode[2 * i];
|
|
|
|
var rmax = encode[2 * i + 1];
|
|
|
|
|
|
|
|
var v2 = rmin + (v - dmin) * (rmax - rmin) / (dmax - dmin);
|
|
|
|
|
|
|
|
// call the appropropriate function
|
|
|
|
return fns[i]([v2]);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
constructPostScript: function PDFFunction_constructPostScript(fn, dict,
|
2011-12-24 14:19:15 +09:00
|
|
|
xref) {
|
2011-12-24 12:41:12 +09:00
|
|
|
var domain = dict.get('Domain');
|
|
|
|
var range = dict.get('Range');
|
|
|
|
|
|
|
|
if (!domain)
|
|
|
|
error('No domain.');
|
|
|
|
|
2011-12-24 14:19:15 +09:00
|
|
|
if (!range)
|
|
|
|
error('No range.');
|
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];
|
|
|
|
var numOutputs = range.length / 2;
|
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.
|
|
|
|
var cache = new FunctionCache();
|
|
|
|
return function constructPostScriptFromIRResult(args) {
|
|
|
|
var initialStack = [];
|
|
|
|
for (var i = 0, ii = (domain.length / 2); i < ii; ++i) {
|
|
|
|
initialStack.push(args[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
var key = initialStack.join('_');
|
|
|
|
if (cache.has(key))
|
|
|
|
return cache.get(key);
|
|
|
|
|
|
|
|
var stack = evaluator.execute(initialStack);
|
2012-03-22 22:15:27 +09:00
|
|
|
var transformed = [];
|
2011-12-24 12:41:12 +09:00
|
|
|
for (i = numOutputs - 1; i >= 0; --i) {
|
|
|
|
var out = stack.pop();
|
|
|
|
var rangeIndex = 2 * i;
|
|
|
|
if (out < range[rangeIndex])
|
|
|
|
out = range[rangeIndex];
|
|
|
|
else if (out > range[rangeIndex + 1])
|
|
|
|
out = range[rangeIndex + 1];
|
|
|
|
transformed[i] = out;
|
|
|
|
}
|
|
|
|
cache.set(key, transformed);
|
|
|
|
return transformed;
|
2011-10-25 08:55:23 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2011-10-28 03:51:10 +09:00
|
|
|
|
2011-12-31 02:24:13 +09:00
|
|
|
var FunctionCache = (function FunctionCacheClosure() {
|
2011-12-30 06:41:54 +09:00
|
|
|
// Of 10 PDF's with type4 functions the maxium number of distinct values seen
|
|
|
|
// was 256. This still may need some tweaking in the future though.
|
2011-12-24 12:41:12 +09:00
|
|
|
var MAX_CACHE_SIZE = 1024;
|
|
|
|
function FunctionCache() {
|
|
|
|
this.cache = {};
|
|
|
|
this.total = 0;
|
|
|
|
}
|
|
|
|
FunctionCache.prototype = {
|
2012-04-05 05:43:26 +09:00
|
|
|
has: function FunctionCache_has(key) {
|
2011-12-24 14:19:15 +09:00
|
|
|
return key in this.cache;
|
2011-12-24 12:41:12 +09:00
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
get: function FunctionCache_get(key) {
|
2011-12-24 12:41:12 +09:00
|
|
|
return this.cache[key];
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
set: function FunctionCache_set(key, value) {
|
2011-12-24 12:41:12 +09:00
|
|
|
if (this.total < MAX_CACHE_SIZE) {
|
|
|
|
this.cache[key] = value;
|
|
|
|
this.total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return FunctionCache;
|
|
|
|
})();
|
|
|
|
|
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) {
|
|
|
|
this.stack = initialStack || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
PostScriptStack.prototype = {
|
2012-04-05 05:43:26 +09:00
|
|
|
push: function PostScriptStack_push(value) {
|
2011-12-24 12:41:12 +09:00
|
|
|
if (this.stack.length >= MAX_STACK_SIZE)
|
|
|
|
error('PostScript function stack overflow.');
|
|
|
|
this.stack.push(value);
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
pop: function PostScriptStack_pop() {
|
2011-12-24 12:41:12 +09:00
|
|
|
if (this.stack.length <= 0)
|
|
|
|
error('PostScript function stack underflow.');
|
|
|
|
return this.stack.pop();
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
copy: function PostScriptStack_copy(n) {
|
2011-12-24 12:41:12 +09:00
|
|
|
if (this.stack.length + n >= MAX_STACK_SIZE)
|
|
|
|
error('PostScript function stack overflow.');
|
2011-12-31 07:59:00 +09:00
|
|
|
var stack = this.stack;
|
|
|
|
for (var i = stack.length - n, j = n - 1; j >= 0; j--, i++)
|
|
|
|
stack.push(stack[i]);
|
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;
|
|
|
|
}
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
return PostScriptStack;
|
|
|
|
})();
|
2011-12-31 02:24:13 +09:00
|
|
|
var PostScriptEvaluator = (function PostScriptEvaluatorClosure() {
|
2011-12-29 13:08:18 +09:00
|
|
|
function PostScriptEvaluator(operators, operands) {
|
|
|
|
this.operators = operators;
|
|
|
|
this.operands = operands;
|
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++];
|
|
|
|
if (typeof operator == 'number') {
|
|
|
|
// 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();
|
|
|
|
if (!a)
|
2011-12-30 06:41:54 +09:00
|
|
|
counter = b;
|
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();
|
|
|
|
if (isBool(a) && isBool(b))
|
|
|
|
stack.push(a && b);
|
|
|
|
else
|
|
|
|
stack.push(a & b);
|
|
|
|
break;
|
|
|
|
case 'atan':
|
|
|
|
a = stack.pop();
|
|
|
|
stack.push(Math.atan(a));
|
|
|
|
break;
|
|
|
|
case 'bitshift':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
if (a > 0)
|
|
|
|
stack.push(a << b);
|
|
|
|
else
|
|
|
|
stack.push(a >> b);
|
|
|
|
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();
|
|
|
|
stack.push(a == b);
|
|
|
|
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();
|
|
|
|
stack.push(a != b);
|
|
|
|
break;
|
|
|
|
case 'neg':
|
|
|
|
a = stack.pop();
|
2011-12-31 02:24:13 +09:00
|
|
|
stack.push(-b);
|
2011-12-24 12:41:12 +09:00
|
|
|
break;
|
|
|
|
case 'not':
|
|
|
|
a = stack.pop();
|
|
|
|
if (isBool(a) && isBool(b))
|
|
|
|
stack.push(a && b);
|
|
|
|
else
|
|
|
|
stack.push(a & b);
|
|
|
|
break;
|
|
|
|
case 'or':
|
|
|
|
b = stack.pop();
|
|
|
|
a = stack.pop();
|
|
|
|
if (isBool(a) && isBool(b))
|
|
|
|
stack.push(a || b);
|
|
|
|
else
|
|
|
|
stack.push(a | b);
|
|
|
|
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();
|
|
|
|
if (isBool(a) && isBool(b))
|
2011-12-31 02:24:13 +09:00
|
|
|
stack.push(a != b);
|
2011-12-24 12:41:12 +09:00
|
|
|
else
|
|
|
|
stack.push(a ^ b);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error('Unknown operator ' + operator);
|
2011-12-24 14:19:15 +09:00
|
|
|
break;
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return stack.stack;
|
|
|
|
}
|
2011-12-24 14:19:15 +09:00
|
|
|
};
|
2011-12-24 12:41:12 +09:00
|
|
|
return PostScriptEvaluator;
|
|
|
|
})();
|
|
|
|
|
2011-12-31 02:24:13 +09:00
|
|
|
var PostScriptParser = (function PostScriptParserClosure() {
|
2011-12-24 12:41:12 +09:00
|
|
|
function PostScriptParser(lexer) {
|
|
|
|
this.lexer = lexer;
|
2011-12-29 13:08:18 +09:00
|
|
|
this.operators = [];
|
2011-12-24 12:41:12 +09:00
|
|
|
this.token;
|
|
|
|
this.prev;
|
|
|
|
}
|
|
|
|
PostScriptParser.prototype = {
|
2012-04-05 05:43:26 +09:00
|
|
|
nextToken: function PostScriptParser_nextToken() {
|
2011-12-24 12:41:12 +09:00
|
|
|
this.prev = this.token;
|
|
|
|
this.token = this.lexer.getToken();
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
accept: function PostScriptParser_accept(type) {
|
2011-12-24 12:41:12 +09:00
|
|
|
if (this.token.type == type) {
|
|
|
|
this.nextToken();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
expect: function PostScriptParser_expect(type) {
|
2011-12-24 12:41:12 +09:00
|
|
|
if (this.accept(type))
|
|
|
|
return true;
|
|
|
|
error('Unexpected symbol: found ' + this.token.type + ' expected ' +
|
|
|
|
type + '.');
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
parse: function PostScriptParser_parse() {
|
2011-12-24 12:41:12 +09:00
|
|
|
this.nextToken();
|
|
|
|
this.expect(PostScriptTokenTypes.LBRACE);
|
|
|
|
this.parseBlock();
|
|
|
|
this.expect(PostScriptTokenTypes.RBRACE);
|
2011-12-30 06:41:54 +09:00
|
|
|
return this.operators;
|
2011-12-24 12:41:12 +09:00
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
parseBlock: function PostScriptParser_parseBlock() {
|
2011-12-24 12:41:12 +09:00
|
|
|
while (true) {
|
|
|
|
if (this.accept(PostScriptTokenTypes.NUMBER)) {
|
2011-12-30 06:41:54 +09:00
|
|
|
this.operators.push(this.prev.value);
|
2011-12-24 12:41:12 +09:00
|
|
|
} else if (this.accept(PostScriptTokenTypes.OPERATOR)) {
|
2011-12-29 13:08:18 +09:00
|
|
|
this.operators.push(this.prev.value);
|
2011-12-24 12:41:12 +09:00
|
|
|
} else if (this.accept(PostScriptTokenTypes.LBRACE)) {
|
|
|
|
this.parseCondition();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
parseCondition: function PostScriptParser_parseCondition() {
|
2011-12-29 13:08:18 +09:00
|
|
|
// Add two place holders that will be updated later
|
|
|
|
var conditionLocation = this.operators.length;
|
2011-12-30 06:41:54 +09:00
|
|
|
this.operators.push(null, null);
|
2011-12-29 13:08:18 +09:00
|
|
|
|
2011-12-24 12:41:12 +09:00
|
|
|
this.parseBlock();
|
|
|
|
this.expect(PostScriptTokenTypes.RBRACE);
|
|
|
|
if (this.accept(PostScriptTokenTypes.IF)) {
|
|
|
|
// The true block is right after the 'if' so it just falls through on
|
|
|
|
// true else it jumps and skips the true block.
|
2011-12-30 06:41:54 +09:00
|
|
|
this.operators[conditionLocation] = this.operators.length;
|
|
|
|
this.operators[conditionLocation + 1] = 'jz';
|
2011-12-24 14:19:15 +09:00
|
|
|
} else if (this.accept(PostScriptTokenTypes.LBRACE)) {
|
2011-12-29 13:08:18 +09:00
|
|
|
var jumpLocation = this.operators.length;
|
2011-12-30 06:41:54 +09:00
|
|
|
this.operators.push(null, null);
|
2011-12-29 13:08:18 +09:00
|
|
|
var endOfTrue = this.operators.length;
|
2011-12-24 12:41:12 +09:00
|
|
|
this.parseBlock();
|
|
|
|
this.expect(PostScriptTokenTypes.RBRACE);
|
|
|
|
this.expect(PostScriptTokenTypes.IFELSE);
|
|
|
|
// The jump is added at the end of the true block to skip the false
|
|
|
|
// block.
|
2011-12-30 06:41:54 +09:00
|
|
|
this.operators[jumpLocation] = this.operators.length;
|
|
|
|
this.operators[jumpLocation + 1] = 'j';
|
2011-12-29 13:08:18 +09:00
|
|
|
|
2011-12-30 06:41:54 +09:00
|
|
|
this.operators[conditionLocation] = endOfTrue;
|
|
|
|
this.operators[conditionLocation + 1] = 'jz';
|
2011-12-24 12:41:12 +09:00
|
|
|
} else {
|
|
|
|
error('PS Function: error parsing conditional.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return PostScriptParser;
|
|
|
|
})();
|
|
|
|
|
|
|
|
var PostScriptTokenTypes = {
|
|
|
|
LBRACE: 0,
|
|
|
|
RBRACE: 1,
|
|
|
|
NUMBER: 2,
|
|
|
|
OPERATOR: 3,
|
|
|
|
IF: 4,
|
|
|
|
IFELSE: 5
|
|
|
|
};
|
|
|
|
|
2011-12-31 02:24:13 +09:00
|
|
|
var PostScriptToken = (function PostScriptTokenClosure() {
|
2011-12-24 12:41:12 +09:00
|
|
|
function PostScriptToken(type, value) {
|
|
|
|
this.type = type;
|
|
|
|
this.value = value;
|
|
|
|
}
|
2011-12-31 02:24:13 +09:00
|
|
|
|
|
|
|
var opCache = {};
|
|
|
|
|
2012-04-05 05:43:26 +09:00
|
|
|
PostScriptToken.getOperator = function PostScriptToken_getOperator(op) {
|
2011-12-31 02:24:13 +09:00
|
|
|
var opValue = opCache[op];
|
|
|
|
if (opValue)
|
|
|
|
return opValue;
|
|
|
|
|
|
|
|
return opCache[op] = new PostScriptToken(PostScriptTokenTypes.OPERATOR, op);
|
|
|
|
};
|
|
|
|
|
|
|
|
PostScriptToken.LBRACE = new PostScriptToken(PostScriptTokenTypes.LBRACE,
|
|
|
|
'{');
|
|
|
|
PostScriptToken.RBRACE = new PostScriptToken(PostScriptTokenTypes.RBRACE,
|
|
|
|
'}');
|
|
|
|
PostScriptToken.IF = new PostScriptToken(PostScriptTokenTypes.IF, 'IF');
|
|
|
|
PostScriptToken.IFELSE = new PostScriptToken(PostScriptTokenTypes.IFELSE,
|
|
|
|
'IFELSE');
|
2011-12-24 12:41:12 +09:00
|
|
|
return PostScriptToken;
|
|
|
|
})();
|
|
|
|
|
2011-12-31 02:24:13 +09:00
|
|
|
var PostScriptLexer = (function PostScriptLexerClosure() {
|
2011-12-24 12:41:12 +09:00
|
|
|
function PostScriptLexer(stream) {
|
|
|
|
this.stream = stream;
|
|
|
|
}
|
|
|
|
PostScriptLexer.prototype = {
|
2012-04-05 05:43:26 +09:00
|
|
|
getToken: function PostScriptLexer_getToken() {
|
2011-12-24 12:41:12 +09:00
|
|
|
var s = '';
|
|
|
|
var ch;
|
|
|
|
var comment = false;
|
|
|
|
var stream = this.stream;
|
|
|
|
|
|
|
|
// skip comments
|
|
|
|
while (true) {
|
|
|
|
if (!(ch = stream.getChar()))
|
|
|
|
return EOF;
|
|
|
|
|
|
|
|
if (comment) {
|
|
|
|
if (ch == '\x0a' || ch == '\x0d')
|
|
|
|
comment = false;
|
|
|
|
} else if (ch == '%') {
|
|
|
|
comment = true;
|
|
|
|
} else if (!Lexer.isSpace(ch)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (ch) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
case '+': case '-': case '.':
|
|
|
|
return new PostScriptToken(PostScriptTokenTypes.NUMBER,
|
|
|
|
this.getNumber(ch));
|
|
|
|
case '{':
|
2011-12-31 02:24:13 +09:00
|
|
|
return PostScriptToken.LBRACE;
|
2011-12-24 12:41:12 +09:00
|
|
|
case '}':
|
2011-12-31 02:24:13 +09:00
|
|
|
return PostScriptToken.RBRACE;
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
// operator
|
|
|
|
var str = ch.toLowerCase();
|
|
|
|
while (true) {
|
2012-04-19 01:48:28 +09:00
|
|
|
ch = stream.lookChar();
|
|
|
|
if (ch === null)
|
|
|
|
break;
|
2012-04-19 01:50:47 +09:00
|
|
|
ch = ch.toLowerCase();
|
2011-12-24 12:41:12 +09:00
|
|
|
if (ch >= 'a' && ch <= 'z')
|
|
|
|
str += ch;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
stream.skip();
|
|
|
|
}
|
|
|
|
switch (str) {
|
|
|
|
case 'if':
|
2011-12-31 02:24:13 +09:00
|
|
|
return PostScriptToken.IF;
|
2011-12-24 12:41:12 +09:00
|
|
|
case 'ifelse':
|
2011-12-31 02:24:13 +09:00
|
|
|
return PostScriptToken.IFELSE;
|
2011-12-24 12:41:12 +09:00
|
|
|
default:
|
2011-12-31 02:24:13 +09:00
|
|
|
return PostScriptToken.getOperator(str);
|
2011-12-24 12:41:12 +09:00
|
|
|
}
|
|
|
|
},
|
2012-04-05 05:43:26 +09:00
|
|
|
getNumber: function PostScriptLexer_getNumber(ch) {
|
2011-12-24 12:41:12 +09:00
|
|
|
var str = ch;
|
|
|
|
var stream = this.stream;
|
|
|
|
while (true) {
|
|
|
|
ch = stream.lookChar();
|
|
|
|
if ((ch >= '0' && ch <= '9') || ch == '-' || ch == '.')
|
|
|
|
str += ch;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
stream.skip();
|
|
|
|
}
|
|
|
|
var value = parseFloat(str);
|
|
|
|
if (isNaN(value))
|
|
|
|
error('Invalid floating point number: ' + value);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return PostScriptLexer;
|
|
|
|
})();
|
|
|
|
|