Merge pull request #248 from cgjones/workers

#123, part 2: move more PDF-page rendering stuff into pdf.js
This commit is contained in:
Andreas Gal 2011-07-11 01:04:43 -07:00
commit 2a1fd975ff
4 changed files with 62 additions and 82 deletions

34
pdf.js
View File

@ -2923,9 +2923,15 @@ var XRef = (function() {
var Page = (function() { var Page = (function() {
function constructor(xref, pageNumber, pageDict) { function constructor(xref, pageNumber, pageDict) {
this.xref = xref;
this.pageNumber = pageNumber; this.pageNumber = pageNumber;
this.pageDict = pageDict; this.pageDict = pageDict;
this.stats = {
create: Date.now(),
compile: 0.0,
fonts: 0.0,
render: 0.0,
};
this.xref = xref;
} }
constructor.prototype = { constructor.prototype = {
@ -2954,6 +2960,32 @@ var Page = (function() {
return shadow(this, 'mediaBox', return shadow(this, 'mediaBox',
((IsArray(obj) && obj.length == 4) ? obj : null)); ((IsArray(obj) && obj.length == 4) ? obj : null));
}, },
startRendering: function(canvasCtx, continuation) {
var self = this;
var stats = self.stats;
stats.compile = stats.fonts = stats.render = 0;
var gfx = new CanvasGraphics(canvasCtx);
var fonts = [ ];
this.compile(gfx, fonts);
stats.compile = Date.now();
FontLoader.bind(
fonts,
function() {
stats.fonts = Date.now();
// Always defer call to display() to work around bug in
// Firefox error reporting from XHR callbacks.
setTimeout(function () {
self.display(gfx);
stats.render = Date.now();
continuation();
});
});
},
compile: function(gfx, fonts) { compile: function(gfx, fonts) {
if (this.code) { if (this.code) {
// content was compiled // content was compiled

View File

@ -1,8 +1,10 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
/* /*
* A Test Driver for PDF.js * A Test Driver for PDF.js
*/ */
var appPath, browser, canvas, currentTaskIdx, manifest, stdout; var appPath, browser, canvas, currentTaskIdx, manifest, stdout;
function queryParams() { function queryParams() {
@ -21,16 +23,16 @@ function load() {
browser = params.browser; browser = params.browser;
manifestFile = params.manifestFile; 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 " + appPath + "\n"); log("Harness thinks this browser is '"+ browser + "' with path " + 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) {
@ -61,15 +63,15 @@ function nextTask() {
if (r.readyState == 4) { if (r.readyState == 4) {
var data = r.mozResponseArrayBuffer || r.mozResponse || var data = r.mozResponseArrayBuffer || r.mozResponse ||
r.responseArrayBuffer || r.response; r.responseArrayBuffer || r.response;
try { try {
task.pdfDoc = new PDFDoc(new Stream(data)); task.pdfDoc = new PDFDoc(new Stream(data));
} catch(e) { } catch(e) {
failure = 'load PDF doc: '+ e.toString(); failure = 'load PDF doc: '+ e.toString();
} }
task.pageNum = 1, nextPage(task, failure); task.pageNum = 1, nextPage(task, failure);
} }
}; };
r.send(null); r.send(null);
} }
@ -92,25 +94,13 @@ function nextPage(task, loadError) {
var failure = loadError || ''; var failure = loadError || '';
var ctx = null; var ctx = null;
var fonts;
var gfx = null;
var page = null; var page = null;
if (!failure) { if (!failure) {
log(" loading page "+ task.pageNum +"... ");
ctx = canvas.getContext("2d");
fonts = [];
try { try {
gfx = new CanvasGraphics(ctx); log(" loading page "+ task.pageNum +"... ");
ctx = canvas.getContext("2d");
page = task.pdfDoc.getPage(task.pageNum); page = task.pdfDoc.getPage(task.pageNum);
page.compile(gfx, fonts);
} catch(e) {
failure = 'compile: '+ e.toString();
}
}
if (!failure) {
try {
var pdfToCssUnitsCoef = 96.0 / 72.0; var pdfToCssUnitsCoef = 96.0 / 72.0;
// using mediaBox for the canvas size // using mediaBox for the canvas size
var pageWidth = (page.mediaBox[2] - page.mediaBox[0]); var pageWidth = (page.mediaBox[2] - page.mediaBox[0]);
@ -118,42 +108,28 @@ function nextPage(task, loadError) {
canvas.width = pageWidth * pdfToCssUnitsCoef; canvas.width = pageWidth * pdfToCssUnitsCoef;
canvas.height = pageHeight * pdfToCssUnitsCoef; canvas.height = pageHeight * pdfToCssUnitsCoef;
clear(ctx); clear(ctx);
page.startRendering(
ctx,
function() { snapshotCurrentPage(page, task, failure); });
} catch(e) { } catch(e) {
failure = 'page setup: '+ e.toString(); failure = 'page setup: '+ e.toString();
} }
} }
if (!failure) {
try {
FontLoader.bind(fonts, function() {
snapshotCurrentPage(gfx, page, task, failure);
});
} catch(e) {
failure = 'fonts: '+ 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(gfx, page, task, failure); snapshotCurrentPage(page, task, failure);
} }
} }
function snapshotCurrentPage(gfx, page, task, failure) { function snapshotCurrentPage(page, task, failure) {
log("done, snapshotting... "); log("done, snapshotting... ");
if (!failure) {
try {
page.display(gfx);
} catch(e) {
failure = 'render: '+ e.toString();
}
}
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
backoff = (inFlightRequests > 0) ? inFlightRequests * 10 : 0; backoff = (inFlightRequests > 0) ? inFlightRequests * 10 : 0;
setTimeout(function() { setTimeout(function() {

View File

@ -123,14 +123,7 @@ var PDFViewer = {
ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore(); ctx.restore();
var gfx = new CanvasGraphics(ctx); page.startRendering(ctx, function() { });
// page.compile will collect all fonts for us, once we have loaded them
// we can trigger the actual page rendering with page.display
var fonts = [];
page.compile(gfx, fonts);
FontLoader.bind(fonts, function() { page.display(gfx); });
} }
}, },
@ -183,14 +176,7 @@ var PDFViewer = {
ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore(); ctx.restore();
var gfx = new CanvasGraphics(ctx); page.startRendering(ctx, function() { });
// page.compile will collect all fonts for us, once we have loaded them
// we can trigger the actual page rendering with page.display
var fonts = [];
page.compile(gfx, fonts);
FontLoader.bind(fonts, function() { page.display(gfx); });
} }
}, },

View File

@ -72,29 +72,15 @@ function displayPage(num) {
ctx.fillRect(0, 0, canvas.width, canvas.height); ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore(); ctx.restore();
var gfx = new CanvasGraphics(ctx); page.startRendering(
ctx,
// page.compile will collect all fonts for us, once we have loaded them function() {
// we can trigger the actual page rendering with page.display var infoDisplay = document.getElementById('info');
var fonts = []; var stats = page.stats;
page.compile(gfx, fonts); var t2 = stats.compile, t3 = stats.fonts, t4 = stats.render;
var t2 = Date.now(); infoDisplay.innerHTML = 'Time to load/compile/fonts/render: ' +
(t1 - t0) + '/' + (t2 - t1) + '/' + (t3 - t2) + '/' + (t4 - t3) + ' ms';
function displayPage() { });
var t3 = Date.now();
page.display(gfx);
var t4 = Date.now();
var infoDisplay = document.getElementById('info');
infoDisplay.innerHTML = 'Time to load/compile/fonts/render: ' +
(t1 - t0) + '/' + (t2 - t1) + '/' + (t3 - t2) + '/' + (t4 - t3) + ' ms';
}
// Always defer call to displayPage() to work around bug in
// Firefox error reporting from XHR callbacks.
FontLoader.bind(fonts, function() { setTimeout(displayPage, 0); });
} }
function nextPage() { function nextPage() {