'use strict';
-if (typeof console.time == 'undefined') {
+var consoleUtils = (function() {
var consoleTimer = {};
- console.time = function(name) {
+
+ var obj = {};
+ obj.time = function(name) {
consoleTimer[name] = Date.now();
};
-
- console.timeEnd = function(name) {
+ obj.timeEnd = function(name) {
var time = consoleTimer[name];
if (time == null) {
throw 'Unkown timer name ' + name;
}
- this.log('Timer:', name, Date.now() - time);
+ console.log('Timer:', name, Date.now() - time);
};
-}
+
+ return obj;
+})();
function FontWorker() {
- this.worker = new Worker('worker/font.js');
+ this.worker = new Worker('../worker/font.js');
this.fontsWaiting = 0;
this.fontsWaitingCallbacks = [];
this.fontsWaiting++;
}
- console.time('ensureFonts');
+ consoleUtils.time('ensureFonts');
// If there are fonts, that need to get loaded, tell the FontWorker to get
// started and push the callback on the waiting-callback-stack.
if (notLoaded.length != 0) {
this.ctx = canvas.getContext('2d');
this.canvas = canvas;
- this.worker = new Worker('worker/pdf.js');
+ this.worker = new Worker('../worker/pdf.js');
this.fontWorker = new FontWorker();
this.waitingForFonts = false;
this.waitingForFontsCallback = [];
},
'$putImageData': function(imageData, x, y) {
- var imgData = this.getImageData(0, 0, imageData.width, imageData.height);
+ var imgData = this.getImageData(0, 0,
+ imageData.width, imageData.height);
// Store the .data property to avaid property lookups.
var imageRealData = imageData.data;
var renderData = function() {
if (id == 0) {
- console.time('main canvas rendering');
+ consoleUtils.time('main canvas rendering');
var ctx = this.ctx;
ctx.save();
ctx.fillStyle = 'rgb(255, 255, 255)';
}
renderProxyCanvas(canvasList[id], cmdQueue);
if (id == 0) {
- console.timeEnd('main canvas rendering');
- console.timeEnd('>>> total page display time:');
+ consoleUtils.timeEnd('main canvas rendering');
+ consoleUtils.timeEnd('>>> total page display time:');
}
}.bind(this);
};
// Listen to the WebWorker for data and call actionHandler on it.
- this.worker.onmessage = function(event) {
+ this.worker.addEventListener('message', function(event) {
var data = event.data;
if (data.action in actionHandler) {
actionHandler[data.action].call(this, data.data);
} else {
throw 'Unkown action from worker: ' + data.action;
}
- }.bind(this);
+ }.bind(this));
}
-WorkerPDFDoc.prototype.open = function(url, callback) {
- var req = new XMLHttpRequest();
- req.open('GET', url);
- req.mozResponseType = req.responseType = 'arraybuffer';
- req.expected = (document.URL.indexOf('file:') == 0) ? 0 : 200;
- req.onreadystatechange = function() {
- if (req.readyState == 4 && req.status == req.expected) {
- var data = req.mozResponseArrayBuffer || req.mozResponse ||
- req.responseArrayBuffer || req.response;
-
- this.loadCallback = callback;
- this.worker.postMessage(data);
- this.showPage(this.numPage);
- }
- }.bind(this);
- req.send(null);
-};
+WorkerPDFDoc.prototype = {
+ open: function(url, callback) {
+ var req = new XMLHttpRequest();
+ req.open('GET', url);
+ req.mozResponseType = req.responseType = 'arraybuffer';
+ req.expected = (document.URL.indexOf('file:') == 0) ? 0 : 200;
+ req.onreadystatechange = function() {
+ if (req.readyState == 4 && req.status == req.expected) {
+ var data = req.mozResponseArrayBuffer || req.mozResponse ||
+ req.responseArrayBuffer || req.response;
+
+ this.loadCallback = callback;
+ this.worker.postMessage(data);
+ this.showPage(this.numPage);
+ }
+ }.bind(this);
+ req.send(null);
+ },
-WorkerPDFDoc.prototype.showPage = function(numPage) {
- this.numPage = parseInt(numPage, 10);
- console.log('=== start rendering page ' + numPage + ' ===');
- console.time('>>> total page display time:');
- this.worker.postMessage(numPage);
- if (this.onChangePage) {
- this.onChangePage(numPage);
- }
-};
+ showPage: function(numPage) {
+ this.numPage = parseInt(numPage, 10);
+ console.log('=== start rendering page ' + numPage + ' ===');
+ consoleUtils.time('>>> total page display time:');
+ this.worker.postMessage(numPage);
+ if (this.onChangePage) {
+ this.onChangePage(numPage);
+ }
+ },
-WorkerPDFDoc.prototype.nextPage = function() {
- if (this.numPage != this.numPages)
- this.showPage(++this.numPage);
-};
+ nextPage: function() {
+ if (this.numPage != this.numPages)
+ this.showPage(++this.numPage);
+ },
-WorkerPDFDoc.prototype.prevPage = function() {
- if (this.numPage != 1)
- this.showPage(--this.numPage);
+ prevPage: function() {
+ if (this.numPage != 1)
+ this.showPage(--this.numPage);
+ }
};
-
action: 'log',
data: args
});
- },
+ }
+};
+var consoleUtils = {
time: function(name) {
consoleTimer[name] = Date.now();
},
if (time == null) {
throw 'Unkown timer name ' + name;
}
- this.log('Timer:', name, Date.now() - time);
+ console.log('Timer:', name, Date.now() - time);
}
};
// Listen for messages from the main thread.
var pdfDocument = null;
-onmessage = function(event) {
+addEventListener('message', function(event) {
var data = event.data;
// If there is no pdfDocument yet, then the sent data is the PDFDocument.
if (!pdfDocument) {
}
// User requested to render a certain page.
else {
- console.time('compile');
+ consoleUtils.time('compile');
// Let's try to render the first page...
var page = pdfDocument.getPage(parseInt(data, 10));
var fonts = [];
var gfx = new CanvasGraphics(canvas.getContext('2d'), CanvasProxy);
page.compile(gfx, fonts);
- console.timeEnd('compile');
+ consoleUtils.timeEnd('compile');
// Send fonts to the main thread.
- console.time('fonts');
+ consoleUtils.time('fonts');
postMessage({
action: 'fonts',
data: fonts
});
- console.timeEnd('fonts');
+ consoleUtils.timeEnd('fonts');
- console.time('display');
+ consoleUtils.time('display');
page.display(gfx);
canvas.flush();
- console.timeEnd('display');
+ consoleUtils.timeEnd('display');
}
-};
+});