#123, part 2: move more PDF-page rendering stuff into pdf.js

This commit is contained in:
Chris Jones 2011-07-10 23:21:13 -07:00
parent 47c29974c4
commit 452ba3d85b
4 changed files with 62 additions and 82 deletions

34
pdf.js
View File

@ -2923,9 +2923,15 @@ var XRef = (function() {
var Page = (function() {
function constructor(xref, pageNumber, pageDict) {
this.xref = xref;
this.pageNumber = pageNumber;
this.pageDict = pageDict;
this.stats = {
create: Date.now(),
compile: 0.0,
fonts: 0.0,
render: 0.0,
};
this.xref = xref;
}
constructor.prototype = {
@ -2954,6 +2960,32 @@ var Page = (function() {
return shadow(this, 'mediaBox',
((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) {
if (this.code) {
// 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
*/
var appPath, browser, canvas, currentTaskIdx, manifest, stdout;
function queryParams() {
@ -21,16 +23,16 @@ function load() {
browser = params.browser;
manifestFile = params.manifestFile;
appPath = params.path;
canvas = document.createElement("canvas");
canvas.mozOpaque = true;
stdout = document.getElementById("stdout");
log("load...\n");
log("Harness thinks this browser is '"+ browser + "' with path " + appPath + "\n");
log("Fetching manifest "+ manifestFile +"...");
var r = new XMLHttpRequest();
r.open("GET", manifestFile, false);
r.onreadystatechange = function(e) {
@ -61,15 +63,15 @@ function nextTask() {
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);
}
@ -92,25 +94,13 @@ function nextPage(task, loadError) {
var failure = loadError || '';
var ctx = null;
var fonts;
var gfx = null;
var page = null;
if (!failure) {
log(" loading page "+ task.pageNum +"... ");
ctx = canvas.getContext("2d");
fonts = [];
try {
gfx = new CanvasGraphics(ctx);
log(" loading page "+ task.pageNum +"... ");
ctx = canvas.getContext("2d");
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;
// using mediaBox for the canvas size
var pageWidth = (page.mediaBox[2] - page.mediaBox[0]);
@ -118,42 +108,28 @@ function nextPage(task, loadError) {
canvas.width = pageWidth * pdfToCssUnitsCoef;
canvas.height = pageHeight * pdfToCssUnitsCoef;
clear(ctx);
page.startRendering(
ctx,
function() { snapshotCurrentPage(page, task, failure); });
} catch(e) {
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) {
// Skip right to snapshotting if there was a failure, since the
// 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... ");
if (!failure) {
try {
page.display(gfx);
} catch(e) {
failure = 'render: '+ e.toString();
}
}
sendTaskResult(canvas.toDataURL("image/png"), task, failure);
log("done"+ (failure ? " (failed!: "+ failure +")" : "") +"\n");
// Set up the next request
backoff = (inFlightRequests > 0) ? inFlightRequests * 10 : 0;
setTimeout(function() {

View File

@ -123,14 +123,7 @@ var PDFViewer = {
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
var gfx = new CanvasGraphics(ctx);
// 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); });
page.startRendering(ctx, function() { });
}
},
@ -183,14 +176,7 @@ var PDFViewer = {
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
var gfx = new CanvasGraphics(ctx);
// 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); });
page.startRendering(ctx, function() { });
}
},

View File

@ -72,29 +72,15 @@ function displayPage(num) {
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.restore();
var gfx = new CanvasGraphics(ctx);
// 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);
var t2 = Date.now();
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); });
page.startRendering(
ctx,
function() {
var infoDisplay = document.getElementById('info');
var stats = page.stats;
var t2 = stats.compile, t3 = stats.fonts, t4 = stats.render;
infoDisplay.innerHTML = 'Time to load/compile/fonts/render: ' +
(t1 - t0) + '/' + (t2 - t1) + '/' + (t3 - t2) + '/' + (t4 - t3) + ' ms';
});
}
function nextPage() {