2012-05-11 06:11:27 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2014-05-10 10:21:15 +09:00
|
|
|
/* globals expect, it, describe, PartialEvaluator, StringStream, OPS,
|
2015-10-21 10:50:32 +09:00
|
|
|
OperatorList, waitsFor, runs, Dict, Name, Stream, WorkerTask */
|
2012-05-11 06:11:27 +09:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('evaluator', function() {
|
|
|
|
function XrefMock(queue) {
|
2015-10-21 10:50:32 +09:00
|
|
|
this.queue = queue || [];
|
2012-05-11 06:11:27 +09:00
|
|
|
}
|
|
|
|
XrefMock.prototype = {
|
|
|
|
fetchIfRef: function() {
|
|
|
|
return this.queue.shift();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
function HandlerMock() {
|
|
|
|
this.inputs = [];
|
|
|
|
}
|
|
|
|
HandlerMock.prototype = {
|
|
|
|
send: function(name, data) {
|
2014-05-15 15:07:43 +09:00
|
|
|
this.inputs.push({name: name, data: data});
|
2012-05-11 06:11:27 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
function ResourcesMock() { }
|
|
|
|
ResourcesMock.prototype = {
|
|
|
|
get: function(name) {
|
|
|
|
return this[name];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-19 02:41:33 +09:00
|
|
|
function PdfManagerMock() { }
|
|
|
|
|
2014-05-10 10:21:15 +09:00
|
|
|
function runOperatorListCheck(evaluator, stream, resources, check) {
|
|
|
|
var done = false;
|
|
|
|
runs(function () {
|
|
|
|
var result = new OperatorList();
|
2015-10-21 10:50:32 +09:00
|
|
|
var task = new WorkerTask('OperatorListCheck');
|
|
|
|
evaluator.getOperatorList(stream, task, resources, result).then(
|
|
|
|
function () {
|
2014-05-10 10:21:15 +09:00
|
|
|
check(result);
|
|
|
|
done = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
waitsFor(function () {
|
|
|
|
return done;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2012-05-11 06:11:27 +09:00
|
|
|
describe('splitCombinedOperations', function() {
|
|
|
|
it('should reject unknown operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
2014-01-17 22:16:52 +09:00
|
|
|
var stream = new StringStream('fTT');
|
2014-05-10 10:21:15 +09:00
|
|
|
|
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function(result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(1);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.fill);
|
2014-06-20 12:52:39 +09:00
|
|
|
expect(result.argsArray[0]).toEqual(null);
|
2014-05-10 10:21:15 +09:00
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle one operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('Q');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function(result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(1);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.restore);
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle two glued operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
|
|
|
var resources = new ResourcesMock();
|
|
|
|
resources.Res1 = {};
|
|
|
|
var stream = new StringStream('/Res1 DoQ');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, resources, function (result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(2);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.paintXObject);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.restore);
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle tree glued operations', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-11 06:11:27 +09:00
|
|
|
'prefix');
|
2014-01-17 22:16:52 +09:00
|
|
|
var stream = new StringStream('fff');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.fill);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.fill);
|
|
|
|
expect(result.fnArray[2]).toEqual(OPS.fill);
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
2012-05-21 03:44:03 +09:00
|
|
|
|
|
|
|
it('should handle three glued operations #2', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-21 03:44:03 +09:00
|
|
|
'prefix');
|
|
|
|
var resources = new ResourcesMock();
|
|
|
|
resources.Res1 = {};
|
2013-01-11 11:00:44 +09:00
|
|
|
var stream = new StringStream('B*Bf*');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, resources, function (result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.eoFillStroke);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.fillStroke);
|
|
|
|
expect(result.fnArray[2]).toEqual(OPS.eoFill);
|
|
|
|
});
|
2012-05-21 03:44:03 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle glued operations and operands', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-21 03:44:03 +09:00
|
|
|
'prefix');
|
2014-01-17 22:16:52 +09:00
|
|
|
var stream = new StringStream('f5 Ts');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(2);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.fill);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.setTextRise);
|
|
|
|
expect(result.argsArray.length).toEqual(2);
|
|
|
|
expect(result.argsArray[1].length).toEqual(1);
|
|
|
|
expect(result.argsArray[1][0]).toEqual(5);
|
|
|
|
});
|
2012-05-21 03:44:03 +09:00
|
|
|
});
|
2012-05-21 04:05:23 +09:00
|
|
|
|
|
|
|
it('should handle glued operations and literals', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2012-05-21 04:05:23 +09:00
|
|
|
'prefix');
|
2014-04-30 23:09:04 +09:00
|
|
|
var stream = new StringStream('trueifalserinulln');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.setFlatness);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.setRenderingIntent);
|
|
|
|
expect(result.fnArray[2]).toEqual(OPS.endPath);
|
|
|
|
expect(result.argsArray.length).toEqual(3);
|
|
|
|
expect(result.argsArray[0].length).toEqual(1);
|
|
|
|
expect(result.argsArray[0][0]).toEqual(true);
|
|
|
|
expect(result.argsArray[1].length).toEqual(1);
|
|
|
|
expect(result.argsArray[1][0]).toEqual(false);
|
2014-06-20 12:52:39 +09:00
|
|
|
expect(result.argsArray[2]).toEqual(null);
|
2014-05-10 10:21:15 +09:00
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('validateNumberOfArgs', function() {
|
|
|
|
it('should execute if correct number of arguments', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2013-01-11 11:00:44 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('5 1 d0');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(result.argsArray[0][0]).toEqual(5);
|
|
|
|
expect(result.argsArray[0][1]).toEqual(1);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
|
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
});
|
|
|
|
it('should execute if too many arguments', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2013-01-11 11:00:44 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('5 1 4 d0');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(result.argsArray[0][0]).toEqual(1);
|
|
|
|
expect(result.argsArray[0][1]).toEqual(4);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.setCharWidth);
|
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
});
|
2014-05-15 15:07:43 +09:00
|
|
|
it('should execute if nested commands', function() {
|
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('/F2 /GS2 gs 5.711 Tf');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(result.fnArray.length).toEqual(3);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.setGState);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.dependency);
|
|
|
|
expect(result.fnArray[2]).toEqual(OPS.setFont);
|
|
|
|
expect(result.argsArray.length).toEqual(3);
|
|
|
|
expect(result.argsArray[0].length).toEqual(1);
|
|
|
|
expect(result.argsArray[1].length).toEqual(1);
|
|
|
|
expect(result.argsArray[2].length).toEqual(2);
|
|
|
|
});
|
2014-05-15 15:07:43 +09:00
|
|
|
});
|
2013-01-11 11:00:44 +09:00
|
|
|
it('should skip if too few arguments', function() {
|
2013-04-19 02:41:33 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
2013-01-11 11:00:44 +09:00
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('5 d0');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(result.argsArray).toEqual([]);
|
|
|
|
expect(result.fnArray).toEqual([]);
|
|
|
|
});
|
2012-05-21 04:05:23 +09:00
|
|
|
});
|
2014-01-17 22:16:52 +09:00
|
|
|
it('should close opened saves', function() {
|
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('qq');
|
2014-05-10 10:21:15 +09:00
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(4);
|
|
|
|
expect(result.fnArray[0]).toEqual(OPS.save);
|
|
|
|
expect(result.fnArray[1]).toEqual(OPS.save);
|
|
|
|
expect(result.fnArray[2]).toEqual(OPS.restore);
|
|
|
|
expect(result.fnArray[3]).toEqual(OPS.restore);
|
|
|
|
});
|
2014-01-17 22:16:52 +09:00
|
|
|
});
|
2015-08-30 01:21:00 +09:00
|
|
|
it('should skip paintXObject if name is missing', function() {
|
2015-06-22 20:33:15 +09:00
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('/ Do');
|
|
|
|
runOperatorListCheck(evaluator, stream, new ResourcesMock(),
|
|
|
|
function (result) {
|
|
|
|
expect(result.argsArray).toEqual([]);
|
|
|
|
expect(result.fnArray).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2015-08-30 01:21:00 +09:00
|
|
|
it('should skip paintXObject if subtype is PS', function() {
|
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
|
|
|
'prefix');
|
|
|
|
var xobjStreamDict = new Dict();
|
|
|
|
xobjStreamDict.set('Subtype', Name.get('PS'));
|
|
|
|
var xobjStream = new Stream([], 0, 0, xobjStreamDict);
|
|
|
|
|
|
|
|
var xobjs = new Dict();
|
|
|
|
xobjs.set('Res1', xobjStream);
|
|
|
|
|
|
|
|
var resources = new Dict();
|
|
|
|
resources.set('XObject', xobjs);
|
|
|
|
|
|
|
|
var stream = new StringStream('/Res1 Do');
|
|
|
|
runOperatorListCheck(evaluator, stream, resources, function (result) {
|
|
|
|
expect(result.argsArray).toEqual([]);
|
|
|
|
expect(result.fnArray).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|
2015-10-21 10:50:32 +09:00
|
|
|
|
|
|
|
describe('thread control', function() {
|
|
|
|
it('should abort operator list parsing', function () {
|
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('qqQQ');
|
|
|
|
var resources = new ResourcesMock();
|
|
|
|
var done = false;
|
|
|
|
runs(function () {
|
|
|
|
var result = new OperatorList();
|
|
|
|
var task = new WorkerTask('OperatorListAbort');
|
|
|
|
task.terminate();
|
|
|
|
evaluator.getOperatorList(stream, task, resources, result).catch(
|
|
|
|
function () {
|
|
|
|
done = true;
|
|
|
|
expect(!!result.fnArray && !!result.argsArray).toEqual(true);
|
|
|
|
expect(result.fnArray.length).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
waitsFor(function () {
|
|
|
|
return done;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it('should abort text parsing parsing', function () {
|
|
|
|
var resources = new ResourcesMock();
|
|
|
|
var evaluator = new PartialEvaluator(new PdfManagerMock(),
|
|
|
|
new XrefMock(), new HandlerMock(),
|
|
|
|
'prefix');
|
|
|
|
var stream = new StringStream('qqQQ');
|
|
|
|
var done = false;
|
|
|
|
runs(function () {
|
|
|
|
var task = new WorkerTask('TextContentAbort');
|
|
|
|
task.terminate();
|
|
|
|
evaluator.getTextContent(stream, task, resources).catch(
|
|
|
|
function () {
|
|
|
|
done = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
waitsFor(function () {
|
|
|
|
return done;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2012-05-11 06:11:27 +09:00
|
|
|
});
|