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);
|
|
|
|
|
|
|
|
this.workerPDF.startRendering(this)
|
|
|
|
},
|
|
|
|
|
2011-09-08 09:02:01 +09:00
|
|
|
startRenderingFromIRQueue: function(IRQueue, fonts, images) {
|
2011-09-06 09:42:58 +09:00
|
|
|
var gfx = new CanvasGraphics(this.ctx);
|
|
|
|
|
|
|
|
// TODO: Add proper handling for images loaded by the worker.
|
|
|
|
var images = new ImagesLoader();
|
|
|
|
|
2011-09-08 09:02:01 +09:00
|
|
|
this.page.startRenderingFromIRQueue(gfx, IRQueue, fonts, images, this.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 = {
|
|
|
|
resolve: function(objId, data) {
|
|
|
|
// In case there is a promise already on this object, just resolve it.
|
|
|
|
if (Objects[objId] instanceof Promise) {
|
|
|
|
Objects[objId].resolve(data);
|
|
|
|
} else {
|
|
|
|
Objects[objId] = new Promise(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2011-09-08 06:45:38 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
var Promise = (function() {
|
2011-09-08 11:50:05 +09:00
|
|
|
function Promise(data) {
|
|
|
|
// If you build a promise and pass in some data it's already resolved.
|
|
|
|
if (data != null) {
|
|
|
|
this.isResolved = true;
|
|
|
|
this.data = data;
|
|
|
|
} else {
|
|
|
|
this.isResolved = false;
|
|
|
|
}
|
|
|
|
this.callbacks = [];
|
2011-09-08 09:15:35 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
Promise.prototype = {
|
|
|
|
resolve: function(data) {
|
|
|
|
if (this.isResolved) {
|
|
|
|
throw "A Promise can be resolved only once";
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
// If the promise is already resolved, call the callback directly.
|
|
|
|
if (this.isResolved) {
|
|
|
|
callback.call(null, this.data);
|
|
|
|
} 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) {
|
|
|
|
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-09 01:46:01 +09:00
|
|
|
var useWorker = true;
|
2011-09-08 02:16:02 +09:00
|
|
|
|
|
|
|
if (useWorker) {
|
|
|
|
var worker = new Worker("../worker/boot.js");
|
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var handler = this.handler = new MessageHandler("main", worker);
|
|
|
|
handler.on("page", function(data) {
|
|
|
|
var pageNum = data.pageNum;
|
|
|
|
var page = this.pageCache[pageNum];
|
|
|
|
|
|
|
|
// Add necessary shape back to fonts.
|
|
|
|
var fonts = data.fonts;
|
|
|
|
for (var i = 0; i < fonts.length; i++) {
|
|
|
|
var font = fonts[i];
|
2011-09-06 10:12:03 +09:00
|
|
|
|
2011-09-09 10:34:54 +09:00
|
|
|
// Some fonts don't have a file, e.g. the build in ones like Arial.
|
|
|
|
if (font.file) {
|
|
|
|
var fontFileDict = new Dict();
|
|
|
|
fontFileDict.map = font.file.dict.map;
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2011-09-09 10:34:54 +09:00
|
|
|
var fontFile = new Stream(font.file.bytes, font.file.start,
|
|
|
|
font.file.end - font.file.start, fontFileDict);
|
2011-09-09 09:23:38 +09:00
|
|
|
|
2011-09-09 10:34:54 +09:00
|
|
|
// Check if this is a FlateStream. Otherwise just use the created
|
|
|
|
// Stream one. This makes complex_ttf_font.pdf work.
|
|
|
|
var cmf = font.file.bytes[0];
|
|
|
|
if ((cmf & 0x0f) == 0x08) {
|
|
|
|
font.file = new FlateStream(fontFile);
|
|
|
|
} else {
|
|
|
|
font.file = fontFile;
|
|
|
|
}
|
2011-09-09 09:23:38 +09:00
|
|
|
}
|
2011-09-06 09:42:58 +09:00
|
|
|
}
|
2011-09-08 06:45:38 +09:00
|
|
|
|
|
|
|
var images = data.images;
|
2011-09-08 11:50:05 +09:00
|
|
|
for (var i = 0; i < images.length; i++) {
|
|
|
|
var image = images[i];
|
|
|
|
var stream = new JpegStreamIR(image.id, image.IR);
|
2011-09-08 06:45:38 +09:00
|
|
|
}
|
2011-09-08 11:50:05 +09:00
|
|
|
|
|
|
|
var timeStart = new Date();
|
|
|
|
page.startRenderingFromIRQueue(data.IRQueue, data.fonts, data.images);
|
|
|
|
console.log("RenderingTime", (new Date()) - timeStart);
|
2011-09-08 02:16:02 +09:00
|
|
|
}, this);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return constructor;
|
|
|
|
})();
|