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';
|
|
|
|
|
2011-09-17 01:04:12 +09:00
|
|
|
// Set this to true if you want to use workers.
|
|
|
|
var useWorker = false;
|
|
|
|
|
2011-09-06 08:07:03 +09:00
|
|
|
var WorkerPage = (function() {
|
2011-10-03 05:17:32 +09:00
|
|
|
function constructor(workerPDF, page, objs) {
|
2011-09-06 08:07:03 +09:00
|
|
|
this.workerPDF = workerPDF;
|
|
|
|
this.page = page;
|
2011-10-03 05:17:32 +09:00
|
|
|
this.objs = objs;
|
2011-09-06 08:07:03 +09:00
|
|
|
|
|
|
|
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-10-03 05:17:32 +09:00
|
|
|
var gfx = new CanvasGraphics(this.ctx, this.objs);
|
2011-09-06 09:42:58 +09:00
|
|
|
|
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-10-03 04:48:40 +09:00
|
|
|
var PDFObjects = (function() {
|
|
|
|
function PDFObjects() {
|
|
|
|
this.objs = {};
|
|
|
|
}
|
2011-09-10 08:15:51 +09:00
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
PDFObjects.prototype = {
|
|
|
|
objs: null,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensures there is an object defined for `objId`. Stores `data` on the
|
|
|
|
* object *if* it is created.
|
|
|
|
*/
|
|
|
|
ensureObj: function(objId, data) {
|
|
|
|
if (!this.objs[objId]) {
|
|
|
|
return this.objs[objId] = new Promise(objId, data);
|
|
|
|
} else {
|
|
|
|
return this.objs[objId];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If called *without* callback, this returns the data of `objId` but the
|
|
|
|
* object needs to be resolved. If it isn't, this function throws.
|
|
|
|
*
|
|
|
|
* If called *with* a callback, the callback is called with the data of the
|
|
|
|
* object once the object is resolved. That means, if you call this
|
|
|
|
* function and the object is already resolved, the callback gets called
|
|
|
|
* right away.
|
|
|
|
*/
|
|
|
|
get: function(objId, callback) {
|
|
|
|
// If there is a callback, then the get can be async and the object is
|
|
|
|
// not required to be resolved right now
|
|
|
|
if (callback) {
|
|
|
|
this.ensureObj(objId).then(callback);
|
|
|
|
}
|
|
|
|
// If there isn't a callback, the user expects to get the resolved data
|
|
|
|
// directly.
|
|
|
|
else {
|
|
|
|
var obj = this.objs[objId];
|
|
|
|
|
|
|
|
// If there isn't an object yet or the object isn't resolved, then the
|
|
|
|
// data isn't ready yet!
|
|
|
|
if (!obj || !obj.isResolved) {
|
|
|
|
throw "Requesting object that isn't resolved yet " + objId;
|
|
|
|
}
|
|
|
|
// Direct access.
|
|
|
|
else {
|
|
|
|
return obj.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the object `objId` with optional `data`.
|
|
|
|
*/
|
|
|
|
resolve: function(objId, data) {
|
|
|
|
var objs = this.objs;
|
|
|
|
|
|
|
|
// In case there is a promise already on this object, just resolve it.
|
|
|
|
if (objs[objId]) {
|
|
|
|
objs[objId].resolve(data);
|
|
|
|
} else {
|
|
|
|
this.ensureObj(objId, data);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
onData: function(objId, callback) {
|
|
|
|
this.ensureObj(objId).onData(callback);
|
|
|
|
},
|
|
|
|
|
|
|
|
isResolved: function(objId) {
|
|
|
|
var objs = this.objs;
|
|
|
|
if (!objs[objId]) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return objs[objId].isResolved;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
hasData: function(objId) {
|
|
|
|
var objs = this.objs;
|
|
|
|
if (!objs[objId]) {
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return objs[objId].hasData;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the data of an object but *doesn't* resolve it.
|
|
|
|
*/
|
|
|
|
setData: function(objId, data) {
|
|
|
|
// Watchout! If you call `this.ensureObj(objId, data)` you'll gone create
|
|
|
|
// a *resolved* promise which shouldn't be the case!
|
|
|
|
this.ensureObj(objId).data = data;
|
2011-09-10 08:15:51 +09:00
|
|
|
}
|
2011-09-08 11:50:05 +09:00
|
|
|
}
|
2011-10-03 04:48:40 +09:00
|
|
|
return PDFObjects;
|
|
|
|
})();
|
|
|
|
|
2011-09-08 06:45:38 +09:00
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
/**
|
|
|
|
* "Promise" object.
|
|
|
|
*/
|
2011-09-08 09:15:35 +09:00
|
|
|
var Promise = (function() {
|
2011-09-13 23:49:35 +09:00
|
|
|
var EMPTY_PROMISE = {};
|
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
/**
|
|
|
|
* If `data` is passed in this constructor, the promise is created resolved.
|
|
|
|
* If there isn't data, it isn't resolved at the beginning.
|
|
|
|
*/
|
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-10-02 00:16:11 +09:00
|
|
|
this._data = data;
|
2011-10-01 18:44:01 +09:00
|
|
|
this.hasData = true;
|
2011-09-08 11:50:05 +09:00
|
|
|
} else {
|
|
|
|
this.isResolved = false;
|
2011-10-02 00:16:11 +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-30 22:38:54 +09:00
|
|
|
hasData: false,
|
|
|
|
|
2011-09-13 23:49:35 +09:00
|
|
|
set data(data) {
|
|
|
|
if (data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this._data !== EMPTY_PROMISE) {
|
2011-09-13 23:49:35 +09:00
|
|
|
throw "Promise " + this.name + ": Cannot set the data of a promise twice";
|
|
|
|
}
|
2011-10-02 00:16:11 +09:00
|
|
|
this._data = data;
|
2011-09-30 22:38:54 +09:00
|
|
|
this.hasData = true;
|
|
|
|
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this.onDataCallback) {
|
|
|
|
this.onDataCallback(data);
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
2011-09-13 23:49:35 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
get data() {
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this._data === EMPTY_PROMISE) {
|
2011-09-13 23:49:35 +09:00
|
|
|
throw "Promise " + this.name + ": Cannot get data that isn't set";
|
|
|
|
}
|
2011-10-02 00:16:11 +09:00
|
|
|
return this._data;
|
2011-09-13 23:49:35 +09:00
|
|
|
},
|
2011-09-30 22:38:54 +09:00
|
|
|
|
|
|
|
onData: function(callback) {
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this._data !== EMPTY_PROMISE) {
|
|
|
|
callback(this._data);
|
2011-09-30 22:38:54 +09:00
|
|
|
} else {
|
2011-10-02 00:16:11 +09:00
|
|
|
this.onDataCallback = callback;
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
|
|
|
},
|
2011-09-13 23:49:35 +09:00
|
|
|
|
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
|
|
|
}
|
2011-10-03 04:48:40 +09:00
|
|
|
|
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
|
|
|
|
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;
|
2011-10-03 05:17:32 +09:00
|
|
|
this.objs = new PDFObjects();
|
2011-09-06 08:07:03 +09:00
|
|
|
|
|
|
|
this.pageCache = [];
|
2011-09-06 09:42:58 +09:00
|
|
|
|
2011-09-08 02:16:02 +09:00
|
|
|
if (useWorker) {
|
2011-10-06 05:24:38 +09:00
|
|
|
var worker = this.worker = new Worker("../worker/pdf_worker_loader.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-10-06 05:24:38 +09:00
|
|
|
|
|
|
|
this.fontsLoading = {};
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2011-09-17 01:04:12 +09:00
|
|
|
var processorHandler = this.processorHandler = new MessageHandler("main", worker);
|
|
|
|
processorHandler.on("page", function(data) {
|
2011-09-08 02:16:02 +09:00
|
|
|
var pageNum = data.pageNum;
|
|
|
|
var page = this.pageCache[pageNum];
|
2011-09-30 22:38:54 +09:00
|
|
|
|
2011-10-01 18:44:01 +09:00
|
|
|
// DepFonts are all fonts are required to render the page. `fontsToLoad`
|
|
|
|
// are all the fonts that are required to render the page AND that
|
|
|
|
// aren't loaded on the page yet.
|
2011-09-30 22:38:54 +09:00
|
|
|
var depFonts = data.depFonts;
|
2011-10-03 05:17:32 +09:00
|
|
|
var objs = this.objs;
|
2011-10-06 05:24:38 +09:00
|
|
|
var fontsToLoad = [];
|
|
|
|
var fontsLoading = this.fontsLoading;
|
|
|
|
// The `i` for the checkFontData is stored here to keep the state in
|
|
|
|
// the closure.
|
|
|
|
var i = 0;
|
|
|
|
|
2011-09-30 22:38:54 +09:00
|
|
|
|
|
|
|
function checkFontData() {
|
|
|
|
// Check if all fontObjs have been processed. If not, shedule a
|
|
|
|
// callback that is called once the data arrives and that checks
|
|
|
|
// the next fonts.
|
2011-10-06 05:24:38 +09:00
|
|
|
for (i; i < depFonts.length; i++) {
|
2011-09-30 22:38:54 +09:00
|
|
|
var fontName = depFonts[i];
|
2011-10-03 05:17:32 +09:00
|
|
|
if (!objs.hasData(fontName)) {
|
2011-10-01 18:44:01 +09:00
|
|
|
console.log('need to wait for fontData', fontName);
|
2011-10-06 05:24:38 +09:00
|
|
|
objs.onData(fontName, checkFontData);
|
2011-10-01 18:44:01 +09:00
|
|
|
return;
|
2011-10-03 05:17:32 +09:00
|
|
|
} else if (!objs.isResolved(fontName)) {
|
2011-10-01 18:44:01 +09:00
|
|
|
fontsToLoad.push(fontName);
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
|
|
|
}
|
2011-10-06 05:24:38 +09:00
|
|
|
|
|
|
|
// There can be edge cases where two pages wait for one font and then
|
|
|
|
// call startRenderingFromIRQueue twice with the same font. That makes
|
|
|
|
// the font getting loaded twice and throw an error later as the font
|
|
|
|
// promise gets resolved twice.
|
|
|
|
// This prevents thats fonts are loaded really only once.
|
|
|
|
for (var j = 0; j < fontsToLoad.length; j++) {
|
|
|
|
var fontName = fontsToLoad[j];
|
|
|
|
if (fontsLoading[fontName]) {
|
|
|
|
fontsToLoad.splice(j, 1);
|
|
|
|
j--;
|
|
|
|
} else {
|
|
|
|
fontsLoading[fontName] = true;
|
|
|
|
}
|
|
|
|
}
|
2011-09-30 22:38:54 +09:00
|
|
|
|
|
|
|
// At this point, all font data ia loaded. Start the actuall rendering.
|
2011-10-01 18:44:01 +09:00
|
|
|
page.startRenderingFromIRQueue(data.IRQueue, fontsToLoad);
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
checkFontData();
|
2011-09-10 03:20:55 +09:00
|
|
|
}, this);
|
|
|
|
|
2011-09-17 01:04:12 +09:00
|
|
|
processorHandler.on("obj", function(data) {
|
2011-09-10 03:20:55 +09:00
|
|
|
var objId = data[0];
|
|
|
|
var objType = data[1];
|
|
|
|
|
|
|
|
switch (objType) {
|
|
|
|
case "JpegStream":
|
|
|
|
var IR = data[2];
|
2011-10-03 05:17:32 +09:00
|
|
|
new JpegStreamIR(objId, IR, this.objs);
|
2011-10-01 23:15:51 +09:00
|
|
|
console.log('got image');
|
2011-09-10 03:20:55 +09:00
|
|
|
break;
|
2011-09-10 08:15:51 +09:00
|
|
|
case "Font":
|
|
|
|
var name = data[2];
|
|
|
|
var file = data[3];
|
|
|
|
var properties = data[4];
|
|
|
|
|
2011-10-06 05:24:38 +09:00
|
|
|
processorHandler.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
|
|
|
|
2011-10-06 05:24:38 +09:00
|
|
|
processorHandler.on('font_ready', function(data) {
|
2011-09-10 10:17:56 +09:00
|
|
|
var objId = data[0];
|
|
|
|
var fontObj = new FontShape(data[1]);
|
2011-10-01 18:44:01 +09:00
|
|
|
|
|
|
|
console.log('got fontData', objId);
|
|
|
|
|
2011-09-10 10:17:56 +09:00
|
|
|
// If there is no string, then there is nothing to attach to the DOM.
|
|
|
|
if (!fontObj.str) {
|
2011-10-03 05:17:32 +09:00
|
|
|
this.objs.resolve(objId, fontObj);
|
2011-09-10 10:17:56 +09:00
|
|
|
} else {
|
2011-10-03 05:17:32 +09:00
|
|
|
this.objs.setData(objId, fontObj);
|
2011-09-10 10:17:56 +09:00
|
|
|
}
|
2011-10-03 05:17:32 +09:00
|
|
|
}.bind(this));
|
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.
|
2011-09-17 01:04:12 +09:00
|
|
|
WorkerProcessorHandler.setup(processorHandler);
|
2011-09-08 02:16:02 +09:00
|
|
|
}
|
2011-09-06 09:42:58 +09:00
|
|
|
|
2011-09-17 01:04:12 +09:00
|
|
|
processorHandler.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-17 01:04:12 +09:00
|
|
|
this.processorHandler.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);
|
2011-10-03 05:17:32 +09:00
|
|
|
// Add a reference to the objects such that Page can forward the reference
|
|
|
|
// to the CanvasGraphics and so on.
|
|
|
|
page.objs = this.objs;
|
|
|
|
return this.pageCache[n] = new WorkerPage(this, page, this.objs);
|
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;
|
|
|
|
})();
|