2011-09-06 08:07:03 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var WorkerPage = (function() {
|
|
|
|
function constructor(workerPDF, page) {
|
|
|
|
this.workerPDF = workerPDF;
|
|
|
|
this.page = page;
|
|
|
|
|
|
|
|
this.ref = page.ref;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
|
|
|
get width() {
|
|
|
|
return this.page.width;
|
|
|
|
},
|
|
|
|
|
|
|
|
get height() {
|
|
|
|
return this.page.height;
|
|
|
|
},
|
|
|
|
|
|
|
|
get stats() {
|
|
|
|
return this.page.stats;
|
|
|
|
},
|
|
|
|
|
|
|
|
startRendering: function(ctx, callback, errback) {
|
2011-09-06 09:42:58 +09:00
|
|
|
this.ctx = ctx;
|
|
|
|
this.callback = callback;
|
2011-09-06 08:07:03 +09:00
|
|
|
// TODO: Place the worker magic HERE.
|
2011-09-06 09:42:58 +09:00
|
|
|
// this.page.startRendering(ctx, callback, errback);
|
|
|
|
|
2011-09-10 03:20:55 +09:00
|
|
|
this.startRenderingTime = Date.now();
|
2011-09-06 09:42:58 +09:00
|
|
|
this.workerPDF.startRendering(this)
|
|
|
|
},
|
|
|
|
|
2011-09-10 03:20:55 +09:00
|
|
|
startRenderingFromIRQueue: function(IRQueue, fonts) {
|
2011-09-06 09:42:58 +09:00
|
|
|
var gfx = new CanvasGraphics(this.ctx);
|
|
|
|
|
2011-09-10 03:20:55 +09:00
|
|
|
var startTime = Date.now();
|
|
|
|
var callback = function(err) {
|
|
|
|
var pageNum = this.page.pageNumber + 1;
|
|
|
|
console.log("page=%d - rendering time: time=%dms",
|
|
|
|
pageNum, Date.now() - startTime);
|
|
|
|
console.log("page=%d - total time: time=%dms",
|
|
|
|
pageNum, Date.now() - this.startRenderingTime);
|
|
|
|
|
|
|
|
this.callback(err);
|
|
|
|
}.bind(this);
|
|
|
|
this.page.startRenderingFromIRQueue(gfx, IRQueue, fonts, callback);
|
2011-09-06 08:07:03 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
getLinks: function() {
|
|
|
|
return this.page.getLinks();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|
|
|
|
|
2011-09-08 06:45:38 +09:00
|
|
|
// This holds a list of objects the IR queue depends on.
|
2011-09-08 11:50:05 +09:00
|
|
|
var Objects = {
|
2011-09-16 06:06:24 +09:00
|
|
|
hash: {},
|
|
|
|
|
2011-09-13 23:49:35 +09:00
|
|
|
getPromise: function(objId) {
|
2011-09-16 06:06:24 +09:00
|
|
|
var hash = this.hash;
|
|
|
|
if (hash[objId]) {
|
|
|
|
return hash[objId];
|
2011-09-13 23:49:35 +09:00
|
|
|
} else {
|
2011-09-16 06:06:24 +09:00
|
|
|
return hash[objId] = new Promise(objId);
|
2011-09-13 23:49:35 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setData: function(objId, data) {
|
|
|
|
var promise = this.getPromise(objId);
|
|
|
|
promise.data = data;
|
|
|
|
},
|
|
|
|
|
2011-09-08 11:50:05 +09:00
|
|
|
resolve: function(objId, data) {
|
2011-09-16 06:06:24 +09:00
|
|
|
var hash = this.hash;
|
2011-09-08 11:50:05 +09:00
|
|
|
// In case there is a promise already on this object, just resolve it.
|
2011-09-16 06:06:24 +09:00
|
|
|
if (hash[objId]) {
|
|
|
|
hash[objId].resolve(data);
|
2011-09-08 11:50:05 +09:00
|
|
|
} else {
|
2011-09-16 06:06:24 +09:00
|
|
|
hash[objId] = new Promise(objId, data);
|
2011-09-08 11:50:05 +09:00
|
|
|
}
|
2011-09-10 08:15:51 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
get: function(objId) {
|
2011-09-16 06:06:24 +09:00
|
|
|
var obj = this.hash[objId];
|
2011-09-10 08:15:51 +09:00
|
|
|
if (!obj || !obj.isResolved) {
|
2011-09-15 02:08:10 +09:00
|
|
|
throw "Requesting object that isn't resolved yet " + objId;
|
2011-09-10 08:15:51 +09:00
|
|
|
}
|
|
|
|
return obj.data;
|
2011-09-16 06:06:24 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
clear: function() {
|
|
|
|
delete this.hash;
|
|
|
|
this.hash = {};
|
2011-09-08 11:50:05 +09:00
|
|
|
}
|
|
|
|
};
|
2011-09-08 06:45:38 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
var Promise = (function() {
|
2011-09-13 23:49:35 +09:00
|
|
|
var EMPTY_PROMISE = {};
|
|
|
|
|
2011-09-10 08:15:51 +09:00
|
|
|
function Promise(name, data) {
|
|
|
|
this.name = name;
|
2011-09-08 11:50:05 +09:00
|
|
|
// If you build a promise and pass in some data it's already resolved.
|
|
|
|
if (data != null) {
|
|
|
|
this.isResolved = true;
|
2011-09-13 23:49:35 +09:00
|
|
|
this.$data = data;
|
2011-09-08 11:50:05 +09:00
|
|
|
} else {
|
|
|
|
this.isResolved = false;
|
2011-09-13 23:49:35 +09:00
|
|
|
this.$data = EMPTY_PROMISE;
|
2011-09-08 11:50:05 +09:00
|
|
|
}
|
|
|
|
this.callbacks = [];
|
2011-09-08 09:15:35 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
Promise.prototype = {
|
2011-09-13 23:49:35 +09:00
|
|
|
set data(data) {
|
|
|
|
if (data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.$data !== EMPTY_PROMISE) {
|
|
|
|
throw "Promise " + this.name + ": Cannot set the data of a promise twice";
|
|
|
|
}
|
|
|
|
this.$data = data;
|
|
|
|
},
|
|
|
|
|
|
|
|
get data() {
|
|
|
|
if (this.$data === EMPTY_PROMISE) {
|
|
|
|
throw "Promise " + this.name + ": Cannot get data that isn't set";
|
|
|
|
}
|
|
|
|
return this.$data;
|
|
|
|
},
|
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
resolve: function(data) {
|
|
|
|
if (this.isResolved) {
|
2011-09-16 15:27:22 +09:00
|
|
|
throw "A Promise can be resolved only once " + this.name;
|
2011-09-08 09:15:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
this.isResolved = true;
|
|
|
|
this.data = data;
|
|
|
|
var callbacks = this.callbacks;
|
|
|
|
|
|
|
|
for (var i = 0; i < callbacks.length; i++) {
|
|
|
|
callbacks[i].call(null, data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
then: function(callback) {
|
2011-09-16 16:11:23 +09:00
|
|
|
if (!callback) {
|
|
|
|
throw "Requiring callback" + this.name;
|
|
|
|
}
|
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
// If the promise is already resolved, call the callback directly.
|
|
|
|
if (this.isResolved) {
|
2011-09-13 23:49:35 +09:00
|
|
|
var data = this.data;
|
|
|
|
callback.call(null, data);
|
2011-09-08 09:15:35 +09:00
|
|
|
} else {
|
|
|
|
this.callbacks.push(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-08 11:50:05 +09:00
|
|
|
return Promise;
|
2011-09-08 09:15:35 +09:00
|
|
|
})();
|
|
|
|
|
2011-09-06 08:07:03 +09:00
|
|
|
var WorkerPDFDoc = (function() {
|
|
|
|
function constructor(data) {
|
2011-09-16 06:06:24 +09:00
|
|
|
// For now, as we create a new WorkerPDFDoc, we clear all objects.
|
|
|
|
// TODO: Have the objects per WorkerPDFDoc.
|
|
|
|
Objects.clear();
|
|
|
|
|
2011-09-06 08:07:03 +09:00
|
|
|
this.data = data;
|
|
|
|
this.stream = new Stream(data);
|
|
|
|
this.pdf = new PDFDoc(this.stream);
|
|
|
|
|
|
|
|
this.catalog = this.pdf.catalog;
|
|
|
|
|
|
|
|
this.pageCache = [];
|
2011-09-06 09:42:58 +09:00
|
|
|
|
2011-09-15 02:08:10 +09:00
|
|
|
var useWorker = true;
|
2011-09-08 02:16:02 +09:00
|
|
|
|
|
|
|
if (useWorker) {
|
2011-09-17 00:09:58 +09:00
|
|
|
var worker = this.worker = new Worker("../worker/boot_processor.js");
|
2011-09-08 02:16:02 +09:00
|
|
|
} else {
|
|
|
|
// If we don't use a worker, just post/sendMessage to the main thread.
|
|
|
|
var worker = {
|
|
|
|
postMessage: function(obj) {
|
|
|
|
worker.onmessage({data: obj});
|
2011-09-06 10:12:03 +09:00
|
|
|
}
|
2011-09-08 02:16:02 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-10 10:17:56 +09:00
|
|
|
var fontWorker = new Worker('../worker/boot_font.js');
|
|
|
|
var fontHandler = this.fontHandler = new MessageHandler('font', fontWorker);
|
|
|
|
|
2011-09-08 02:16:02 +09:00
|
|
|
var handler = this.handler = new MessageHandler("main", worker);
|
|
|
|
handler.on("page", function(data) {
|
|
|
|
var pageNum = data.pageNum;
|
|
|
|
var page = this.pageCache[pageNum];
|
|
|
|
|
2011-09-08 06:45:38 +09:00
|
|
|
|
2011-09-10 03:20:55 +09:00
|
|
|
page.startRenderingFromIRQueue(data.IRQueue, data.fonts);
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
handler.on("obj", function(data) {
|
|
|
|
var objId = data[0];
|
|
|
|
var objType = data[1];
|
|
|
|
|
|
|
|
switch (objType) {
|
|
|
|
case "JpegStream":
|
|
|
|
var IR = data[2];
|
|
|
|
new JpegStreamIR(objId, IR);
|
|
|
|
break;
|
2011-09-10 08:15:51 +09:00
|
|
|
case "Font":
|
|
|
|
var name = data[2];
|
|
|
|
var file = data[3];
|
|
|
|
var properties = data[4];
|
|
|
|
|
2011-09-10 10:17:56 +09:00
|
|
|
fontHandler.send("font", [objId, name, file, properties]);
|
2011-09-10 08:15:51 +09:00
|
|
|
break;
|
2011-09-10 03:20:55 +09:00
|
|
|
default:
|
|
|
|
throw "Got unkown object type " + objType;
|
2011-09-08 06:45:38 +09:00
|
|
|
}
|
2011-09-08 02:16:02 +09:00
|
|
|
}, this);
|
2011-09-10 10:17:56 +09:00
|
|
|
|
|
|
|
fontHandler.on('font_ready', function(data) {
|
|
|
|
var objId = data[0];
|
|
|
|
var fontObj = new FontShape(data[1]);
|
|
|
|
// If there is no string, then there is nothing to attach to the DOM.
|
|
|
|
if (!fontObj.str) {
|
|
|
|
Objects.resolve(objId, fontObj);
|
|
|
|
} else {
|
2011-09-13 23:49:35 +09:00
|
|
|
Objects.setData(objId, fontObj);
|
2011-09-16 15:27:22 +09:00
|
|
|
FontLoader.bind(fontObj);
|
2011-09-10 10:17:56 +09:00
|
|
|
}
|
|
|
|
});
|
2011-09-08 02:16:02 +09:00
|
|
|
|
|
|
|
if (!useWorker) {
|
|
|
|
// If the main thread is our worker, setup the handling for the messages
|
|
|
|
// the main thread sends to it self.
|
|
|
|
WorkerHandler.setup(handler);
|
|
|
|
}
|
2011-09-06 09:42:58 +09:00
|
|
|
|
2011-09-08 02:16:02 +09:00
|
|
|
handler.send("doc", data);
|
2011-09-06 08:07:03 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor.prototype = {
|
|
|
|
get numPages() {
|
|
|
|
return this.pdf.numPages;
|
|
|
|
},
|
|
|
|
|
2011-09-06 09:42:58 +09:00
|
|
|
startRendering: function(page) {
|
2011-09-08 02:16:02 +09:00
|
|
|
this.handler.send("page_request", page.page.pageNumber + 1);
|
2011-09-06 09:42:58 +09:00
|
|
|
},
|
|
|
|
|
2011-09-06 08:07:03 +09:00
|
|
|
getPage: function(n) {
|
|
|
|
if (this.pageCache[n]) {
|
|
|
|
return this.pageCache[n];
|
|
|
|
}
|
|
|
|
|
|
|
|
var page = this.pdf.getPage(n);
|
|
|
|
return this.pageCache[n] = new WorkerPage(this, page);
|
2011-09-17 00:09:58 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy: function() {
|
|
|
|
console.log("destroy worker");
|
|
|
|
if (this.worker) {
|
|
|
|
this.worker.terminate();
|
|
|
|
}
|
|
|
|
if (this.fontWorker) {
|
|
|
|
this.fontWorker.terminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var n in this.pageCache) {
|
|
|
|
delete this.pageCache[n];
|
|
|
|
}
|
|
|
|
delete this.data;
|
|
|
|
delete this.stream;
|
|
|
|
delete this.pdf;
|
|
|
|
delete this.catalog;
|
2011-09-06 08:07:03 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|