2011-09-08 02:16:02 +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
|
|
|
var WorkerProcessorHandler = {
|
2011-09-08 02:16:02 +09:00
|
|
|
setup: function(handler) {
|
|
|
|
var pdfDoc = null;
|
|
|
|
|
|
|
|
handler.on("doc", function(data) {
|
2011-10-08 22:19:48 +09:00
|
|
|
// Create only the model of the PDFDoc, which is enough for
|
|
|
|
// processing the content of the pdf.
|
|
|
|
pdfDoc = new PDFDocModel(new Stream(data));
|
2011-09-08 02:16:02 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
handler.on("page_request", function(pageNum) {
|
|
|
|
pageNum = parseInt(pageNum);
|
|
|
|
|
|
|
|
var page = pdfDoc.getPage(pageNum);
|
|
|
|
|
|
|
|
// The following code does quite the same as Page.prototype.startRendering,
|
|
|
|
// but stops at one point and sends the result back to the main thread.
|
|
|
|
var gfx = new CanvasGraphics(null);
|
|
|
|
|
2011-09-10 03:20:55 +09:00
|
|
|
var start = Date.now();
|
2011-09-30 22:38:54 +09:00
|
|
|
|
|
|
|
var dependency = [];
|
|
|
|
|
2011-09-08 02:16:02 +09:00
|
|
|
// Pre compile the pdf page and fetch the fonts/images.
|
2011-09-30 22:38:54 +09:00
|
|
|
var IRQueue = page.getIRQueue(handler, dependency);
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2011-09-10 03:20:55 +09:00
|
|
|
console.log("page=%d - getIRQueue: time=%dms, len=%d", pageNum, Date.now() - start, IRQueue.fnArray.length);
|
2011-09-08 02:16:02 +09:00
|
|
|
|
2011-09-09 11:28:15 +09:00
|
|
|
if (false /* show used commands */) {
|
2011-09-08 02:16:02 +09:00
|
|
|
var cmdMap = {};
|
|
|
|
|
2011-09-08 09:02:01 +09:00
|
|
|
var fnArray = IRQueue .fnArray;
|
2011-09-08 02:16:02 +09:00
|
|
|
for (var i = 0; i < fnArray.length; i++) {
|
|
|
|
var entry = fnArray[i];
|
|
|
|
if (entry == "paintReadyFormXObject") {
|
|
|
|
//console.log(preCompilation.argsArray[i]);
|
|
|
|
}
|
|
|
|
if (cmdMap[entry] == null) {
|
|
|
|
cmdMap[entry] = 1;
|
|
|
|
} else {
|
|
|
|
cmdMap[entry] += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
console.log("cmds", JSON.stringify(cmdMap));
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Filter the dependecies for fonts.
|
|
|
|
var fonts = {};
|
|
|
|
for (var i = 0; i < dependency.length; i++) {
|
|
|
|
var dep = dependency[i];
|
|
|
|
if (dep.indexOf('font_') == 0) {
|
|
|
|
fonts[dep] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-08 02:16:02 +09:00
|
|
|
|
|
|
|
handler.send("page", {
|
2011-09-08 09:02:01 +09:00
|
|
|
pageNum: pageNum,
|
|
|
|
IRQueue: IRQueue,
|
2011-09-30 22:38:54 +09:00
|
|
|
depFonts: Object.keys(fonts)
|
2011-09-08 02:16:02 +09:00
|
|
|
});
|
|
|
|
}, this);
|
2011-10-06 05:24:38 +09:00
|
|
|
|
|
|
|
handler.on("font", function(data) {
|
|
|
|
var objId = data[0];
|
|
|
|
var name = data[1];
|
|
|
|
var file = data[2];
|
|
|
|
var properties = data[3];
|
|
|
|
|
|
|
|
var font = {
|
|
|
|
name: name,
|
|
|
|
file: file,
|
|
|
|
properties: properties
|
|
|
|
};
|
|
|
|
|
|
|
|
// Some fonts don't have a file, e.g. the build in ones like Arial.
|
|
|
|
if (file) {
|
|
|
|
var fontFileDict = new Dict();
|
|
|
|
fontFileDict.map = file.dict.map;
|
|
|
|
|
|
|
|
var fontFile = new Stream(file.bytes, file.start,
|
|
|
|
file.end - file.start, fontFileDict);
|
|
|
|
|
|
|
|
// Check if this is a FlateStream. Otherwise just use the created
|
|
|
|
// Stream one. This makes complex_ttf_font.pdf work.
|
|
|
|
var cmf = file.bytes[0];
|
|
|
|
if ((cmf & 0x0f) == 0x08) {
|
|
|
|
font.file = new FlateStream(fontFile);
|
|
|
|
} else {
|
|
|
|
font.file = fontFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var obj = new Font(font.name, font.file, font.properties);
|
|
|
|
|
|
|
|
var str = '';
|
|
|
|
var data = obj.data;
|
|
|
|
if (data) {
|
|
|
|
var length = data.length;
|
|
|
|
for (var j = 0; j < length; j++)
|
|
|
|
str += String.fromCharCode(data[j]);
|
|
|
|
}
|
|
|
|
|
|
|
|
obj.str = str;
|
|
|
|
|
|
|
|
// Remove the data array form the font object, as it's not needed
|
|
|
|
// anymore as we sent over the ready str.
|
|
|
|
delete obj.data;
|
|
|
|
|
|
|
|
handler.send("font_ready", [objId, obj]);
|
|
|
|
});
|
2011-09-08 02:16:02 +09:00
|
|
|
}
|
2011-09-08 06:45:38 +09:00
|
|
|
}
|