Browse Source

Merge branch 'master' into hmm

sbarman 14 years ago
parent
commit
881af4695e
  1. 71
      pdf.js
  2. 228
      test/driver.js

71
pdf.js

@ -4149,7 +4149,11 @@ var CanvasExtraState = (function() {
constructor.prototype = { constructor.prototype = {
clone: function canvasextra_clone() { clone: function canvasextra_clone() {
return Object.create(this); return Object.create(this);
} },
setCurrentPoint: function canvasextra_setCurrentPoint(x, y) {
this.x = x;
this.y = y;
},
}; };
return constructor; return constructor;
})(); })();
@ -4276,18 +4280,24 @@ var CanvasGraphics = (function() {
// Path // Path
moveTo: function(x, y) { moveTo: function(x, y) {
this.ctx.moveTo(x, y); this.ctx.moveTo(x, y);
this.current.setCurrentPoint(x, y);
}, },
lineTo: function(x, y) { lineTo: function(x, y) {
this.ctx.lineTo(x, y); this.ctx.lineTo(x, y);
this.current.setCurrentPoint(x, y);
}, },
curveTo: function(x1, y1, x2, y2, x3, y3) { curveTo: function(x1, y1, x2, y2, x3, y3) {
this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3); this.ctx.bezierCurveTo(x1, y1, x2, y2, x3, y3);
this.current.setCurrentPoint(x3, y3);
}, },
curveTo2: function(x2, y2, x3, y3) { curveTo2: function(x2, y2, x3, y3) {
TODO("'v' operator: need current point in gfx context"); var current = this.current;
this.ctx.bezierCurveTo(current.x, current.y, x2, y2, x3, y3);
current.setCurrentPoint(x3, y3);
}, },
curveTo3: function(x1, y1, x3, y3) { curveTo3: function(x1, y1, x3, y3) {
this.curveTo(x1, y1, x3, y3, x3, y3); this.curveTo(x1, y1, x3, y3, x3, y3);
this.current.setCurrentPoint(x3, y3);
}, },
closePath: function() { closePath: function() {
this.ctx.closePath(); this.ctx.closePath();
@ -5704,7 +5714,7 @@ var PDFFunction = (function() {
if (!typeFn) if (!typeFn)
error('Unknown type of function'); error('Unknown type of function');
typeFn.call(this, fn, dict); typeFn.call(this, fn, dict, xref);
}; };
constructor.prototype = { constructor.prototype = {
@ -5849,9 +5859,58 @@ var PDFFunction = (function() {
return out; return out;
} }
}, },
constructStiched: function() { constructStiched: function(fn, dict, xref) {
TODO('unhandled type of function'); var domain = dict.get('Domain');
this.func = function() { return [255, 105, 180]; } var range = dict.get('Range');
if (!domain)
error('No domain');
var inputSize = domain.length / 2;
if (inputSize != 1)
error('Bad domain for stiched function');
var fnRefs = dict.get('Functions');
var fns = [];
for (var i = 0, ii = fnRefs.length; i < ii; ++i)
fns.push(new PDFFunction(xref, xref.fetchIfRef(fnRefs[i])));
var bounds = dict.get('Bounds');
var encode = dict.get('Encode');
this.func = function(args) {
var clip = function(v, min, max) {
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].func([v2]);
}
}, },
constructPostScript: function() { constructPostScript: function() {
TODO('unhandled type of function'); TODO('unhandled type of function');

228
test/driver.js

@ -10,137 +10,146 @@
var appPath, browser, canvas, currentTaskIdx, manifest, stdout; var appPath, browser, canvas, currentTaskIdx, manifest, stdout;
function queryParams() { function queryParams() {
var qs = window.location.search.substring(1); var qs = window.location.search.substring(1);
var kvs = qs.split('&'); var kvs = qs.split('&');
var params = { }; var params = { };
for (var i = 0; i < kvs.length; ++i) { for (var i = 0; i < kvs.length; ++i) {
var kv = kvs[i].split('='); var kv = kvs[i].split('=');
params[unescape(kv[0])] = unescape(kv[1]); params[unescape(kv[0])] = unescape(kv[1]);
} }
return params; return params;
} }
function load() { function load() {
var params = queryParams(); var params = queryParams();
browser = params.browser; browser = params.browser;
var manifestFile = params.manifestFile; var manifestFile = params.manifestFile;
appPath = params.path; appPath = params.path;
canvas = document.createElement('canvas'); canvas = document.createElement('canvas');
canvas.mozOpaque = true; canvas.mozOpaque = true;
stdout = document.getElementById('stdout'); stdout = document.getElementById('stdout');
log('load...\n'); log('load...\n');
log('Harness thinks this browser is "' + browser + '" with path "' + log('Harness thinks this browser is "' + browser + '" with path "' +
appPath + '"\n'); appPath + '"\n');
log('Fetching manifest "' + manifestFile + '"... '); log('Fetching manifest "' + manifestFile + '"... ');
var r = new XMLHttpRequest(); var r = new XMLHttpRequest();
r.open('GET', manifestFile, false); r.open('GET', manifestFile, false);
r.onreadystatechange = function(e) { r.onreadystatechange = function(e) {
if (r.readyState == 4) { if (r.readyState == 4) {
log('done\n'); log('done\n');
manifest = JSON.parse(r.responseText); manifest = JSON.parse(r.responseText);
currentTaskIdx = 0, nextTask(); currentTaskIdx = 0, nextTask();
} }
}; };
r.send(null); r.send(null);
} }
window.onload = load; window.onload = load;
function nextTask() { function nextTask() {
if (currentTaskIdx == manifest.length) { if (currentTaskIdx == manifest.length) {
return done(); return done();
}
var task = manifest[currentTaskIdx];
task.round = 0;
log('Loading file "' + task.file + '"\n');
var r = new XMLHttpRequest();
r.open('GET', task.file);
r.mozResponseType = r.responseType = 'arraybuffer';
r.onreadystatechange = function() {
var failure;
if (r.readyState == 4) {
var data = r.mozResponseArrayBuffer || r.mozResponse ||
r.responseArrayBuffer || r.response;
try {
task.pdfDoc = new PDFDoc(new Stream(data));
} catch (e) {
failure = 'load PDF doc : ' + e.toString();
}
task.pageNum = 1, nextPage(task, failure);
} }
var task = manifest[currentTaskIdx]; };
task.round = 0; r.send(null);
log('Loading file "' + task.file + '"\n');
var r = new XMLHttpRequest();
r.open('GET', task.file);
r.mozResponseType = r.responseType = 'arraybuffer';
r.onreadystatechange = function() {
var failure;
if (r.readyState == 4) {
var data = r.mozResponseArrayBuffer || r.mozResponse ||
r.responseArrayBuffer || r.response;
try {
task.pdfDoc = new PDFDoc(new Stream(data));
} catch (e) {
failure = 'load PDF doc : ' + e.toString();
}
task.pageNum = 1, nextPage(task, failure);
}
};
r.send(null);
} }
function isLastPage(task) { function isLastPage(task) {
return (!task.pdfDoc || (task.pageNum > task.pdfDoc.numPages)); return (task.pageNum > task.pdfDoc.numPages);
} }
function nextPage(task, loadError) { function nextPage(task, loadError) {
if (isLastPage(task)) { var failure = loadError || '';
if (++task.round < task.rounds) {
log(' Round ' + (1 + task.round) + '\n'); if (!task.pdfDoc) {
task.pageNum = 1; sendTaskResult(canvas.toDataURL('image/png'), task, failure);
} else { log('done' + (failure ? ' (failed !: ' + failure + ')' : '') + '\n');
++currentTaskIdx, nextTask(); ++currentTaskIdx, nextTask();
return; return;
} }
if (isLastPage(task)) {
if (++task.round < task.rounds) {
log(' Round ' + (1 + task.round) + '\n');
task.pageNum = 1;
} else {
++currentTaskIdx, nextTask();
return;
} }
}
var failure = loadError || ''; var page = null;
var ctx = null; if (!failure) {
var page = null; try {
if (!failure) { log(' loading page ' + task.pageNum + '/' + task.pdfDoc.numPages +
try { '... ');
log(' loading page ' + task.pageNum + '/' + task.pdfDoc.numPages + var ctx = canvas.getContext('2d');
'... '); page = task.pdfDoc.getPage(task.pageNum);
ctx = canvas.getContext('2d');
page = task.pdfDoc.getPage(task.pageNum); var pdfToCssUnitsCoef = 96.0 / 72.0;
// using mediaBox for the canvas size
var pdfToCssUnitsCoef = 96.0 / 72.0; var pageWidth = page.width;
// using mediaBox for the canvas size var pageHeight = page.height;
var pageWidth = page.width; canvas.width = pageWidth * pdfToCssUnitsCoef;
var pageHeight = page.height; canvas.height = pageHeight * pdfToCssUnitsCoef;
canvas.width = pageWidth * pdfToCssUnitsCoef; clear(ctx);
canvas.height = pageHeight * pdfToCssUnitsCoef;
clear(ctx); page.startRendering(
ctx,
page.startRendering( function(e) {
ctx, snapshotCurrentPage(page, task, (!failure && e) ?
function(e) { ('render : ' + e) : failure);
snapshotCurrentPage(page, task, (!failure && e) ?
('render : ' + e) : failure);
});
} catch (e) {
failure = 'page setup : ' + e.toString();
} }
);
} catch (e) {
failure = 'page setup : ' + e.toString();
} }
}
if (failure) { if (failure) {
// Skip right to snapshotting if there was a failure, since the // Skip right to snapshotting if there was a failure, since the
// fonts might be in an inconsistent state. // fonts might be in an inconsistent state.
snapshotCurrentPage(page, task, failure); snapshotCurrentPage(page, task, failure);
} }
} }
function snapshotCurrentPage(page, task, failure) { function snapshotCurrentPage(page, task, failure) {
log('done, snapshotting... '); log('done, snapshotting... ');
sendTaskResult(canvas.toDataURL('image/png'), task, failure); sendTaskResult(canvas.toDataURL('image/png'), task, failure);
log('done' + (failure ? ' (failed !: ' + failure + ')' : '') + '\n'); log('done' + (failure ? ' (failed !: ' + failure + ')' : '') + '\n');
// Set up the next request // Set up the next request
var backoff = (inFlightRequests > 0) ? inFlightRequests * 10 : 0; var backoff = (inFlightRequests > 0) ? inFlightRequests * 10 : 0;
setTimeout(function() { setTimeout(
++task.pageNum, nextPage(task); function() {
++task.pageNum, nextPage(task);
}, },
backoff backoff
); );
@ -154,7 +163,8 @@ function sendQuitRequest() {
function quitApp() { function quitApp() {
log('Done !'); log('Done !');
document.body.innerHTML = 'Tests are finished. <h1>CLOSE ME!</h1>'; document.body.innerHTML = 'Tests are finished. <h1>CLOSE ME!</h1>' +
document.body.innerHTML;
if (window.SpecialPowers) { if (window.SpecialPowers) {
SpecialPowers.quitApplication(); SpecialPowers.quitApplication();
} else { } else {
@ -176,7 +186,7 @@ var inFlightRequests = 0;
function sendTaskResult(snapshot, task, failure) { function sendTaskResult(snapshot, task, failure) {
var result = { browser: browser, var result = { browser: browser,
id: task.id, id: task.id,
numPages: task.pdfDoc.numPages, numPages: task.pdfDoc ? task.pdfDoc.numPages : 0,
failure: failure, failure: failure,
file: task.file, file: task.file,
round: task.round, round: task.round,

Loading…
Cancel
Save