return "DeviceCmykCS";
break;
case 'Pattern':
+ console.log("ColorSpace Pattern");
// TODO: IMPLEMENT ME
// var baseCS = cs[1];
<script type="text/javascript" src="../metrics.js"></script>
<script type="text/javascript" src="../worker.js"></script>
<script type="text/javascript" src="../worker/message_handler.js"></script>
+ <script type="text/javascript" src="../worker/handler.js"></script>
</head>
<body>
this.pageCache = [];
- this.worker = new Worker("../worker/boot.js");
- this.handler = new MessageHandler("main", {
- "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];
-
- var fontFileDict = new Dict();
- fontFileDict.map = font.file.dict.map;
-
- var fontFile = new Stream(font.file.bytes, font.file.start,
- font.file.end - font.file.start, fontFileDict);
- font.file = new FlateStream(fontFile);
+ var useWorker = false;
+
+ 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});
}
+ }
+ }
+
+ 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];
- console.log("startRenderingFromPreCompilation:", "numberOfFonts", fonts.length);
- page.startRenderingFromPreCompilation(data.preCompilation, data.fonts, data.images);
+ var fontFileDict = new Dict();
+ fontFileDict.map = font.file.dict.map;
+
+ var fontFile = new Stream(font.file.bytes, font.file.start,
+ font.file.end - font.file.start, fontFileDict);
+ font.file = new FlateStream(fontFile);
}
- }, this.worker, this);
+
+ console.log("startRenderingFromPreCompilation:", "numberOfFonts", fonts.length);
+ page.startRenderingFromPreCompilation(data.preCompilation, data.fonts, data.images);
+ }, 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);
+ }
- this.handler.send("doc", data);
+ handler.send("doc", data);
}
constructor.prototype = {
},
startRendering: function(page) {
- this.handler.send("page", page.page.pageNumber + 1);
+ this.handler.send("page_request", page.page.pageNumber + 1);
},
getPage: function(n) {
importScripts('../fonts.js');
importScripts('../crypto.js');
importScripts('../glyphlist.js');
+importScripts('handler.js');
// Listen for messages from the main thread.
var pdfDoc = null;
-var handler = new MessageHandler("worker", {
- "doc": function(data) {
- pdfDoc = new PDFDoc(new Stream(data));
- console.log("setup pdfDoc");
- },
-
- "page": function(pageNum) {
- pageNum = parseInt(pageNum);
- console.log("about to process page", 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);
- var fonts = [];
- // TODO: Figure out how image loading is handled inside the worker.
- var images = new ImagesLoader();
-
- // Pre compile the pdf page and fetch the fonts/images.
- var preCompilation = page.preCompile(gfx, fonts, images);
-
- // Extract the minimum of font data that is required to build all required
- // font stuff on the main thread.
- var fontsMin = [];
- for (var i = 0; i < fonts.length; i++) {
- var font = fonts[i];
-
- fontsMin.push({
- name: font.name,
- file: font.file,
- properties: font.properties
- });
- }
-
- // TODO: Handle images here.
-
- console.log("about to send page", pageNum);
-
- if (true /* show used commands */) {
- var cmdMap = {};
-
- var fnArray = preCompilation.fnArray;
- 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;
- }
- }
-
- // // Make a copy of the fnArray and show all cmds it has.
- // var fnArray = preCompilation.fnArray.slice(0).sort();
- // for (var i = 0; i < fnArray.length; true) {
- // if (fnArray[i] == fnArray[i + 1]) {
- // fnArray.splice(i, 1);
- // } else {
- // i++;
- // }
- // }
- console.log("cmds", JSON.stringify(cmdMap));
- }
-
- handler.send("page", {
- pageNum: pageNum,
- fonts: fontsMin,
- images: [],
- preCompilation: preCompilation,
- });
- }
-}, this);
+var handler = new MessageHandler("worker", this);
+WorkerHandler.setup(handler);
--- /dev/null
+/* -*- 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 WorkerHandler = {
+ setup: function(handler) {
+ var pdfDoc = null;
+
+ handler.on("doc", function(data) {
+ pdfDoc = new PDFDoc(new Stream(data));
+ console.log("setup pdfDoc");
+ });
+
+ handler.on("page_request", function(pageNum) {
+ pageNum = parseInt(pageNum);
+ console.log("about to process page", 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);
+ var fonts = [];
+ // TODO: Figure out how image loading is handled inside the worker.
+ var images = new ImagesLoader();
+
+ // Pre compile the pdf page and fetch the fonts/images.
+ var preCompilation = page.preCompile(gfx, fonts, images);
+
+ // Extract the minimum of font data that is required to build all required
+ // font stuff on the main thread.
+ var fontsMin = [];
+ for (var i = 0; i < fonts.length; i++) {
+ var font = fonts[i];
+
+ fontsMin.push({
+ name: font.name,
+ file: font.file,
+ properties: font.properties
+ });
+ }
+
+ // TODO: Handle images here.
+
+ console.log("about to send page", pageNum);
+
+ if (true /* show used commands */) {
+ var cmdMap = {};
+
+ var fnArray = preCompilation.fnArray;
+ 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;
+ }
+ }
+
+ // // Make a copy of the fnArray and show all cmds it has.
+ // var fnArray = preCompilation.fnArray.slice(0).sort();
+ // for (var i = 0; i < fnArray.length; true) {
+ // if (fnArray[i] == fnArray[i + 1]) {
+ // fnArray.splice(i, 1);
+ // } else {
+ // i++;
+ // }
+ // }
+ console.log("cmds", JSON.stringify(cmdMap));
+ }
+
+ handler.send("page", {
+ pageNum: pageNum,
+ fonts: fontsMin,
+ images: [],
+ preCompilation: preCompilation,
+ });
+ }, this);
+ }
+}
\ No newline at end of file
'use strict';
-function MessageHandler(name, actionHandler, comObj, scope) {
+function MessageHandler(name, comObj) {
this.name = name;
+ this.comObj = comObj;
+ var ah = this.actionHandler = {};
- actionHandler["console_log"] = function(data) {
+ ah["console_log"] = [function(data) {
console.log.apply(console, data);
- }
- actionHandler["console_error"] = function(data) {
+ }]
+ ah["console_error"] = [function(data) {
console.error.apply(console, data);
- }
-
+ }]
comObj.onmessage = function(event) {
var data = event.data;
- if (data.action in actionHandler) {
- actionHandler[data.action].call(scope, data.data);
+ if (data.action in ah) {
+ var action = ah[data.action];
+ action[0].call(action[1], data.data);
} else {
throw 'Unkown action from worker: ' + data.action;
}
};
-
- this.send = function(actionName, data) {
+}
+
+MessageHandler.prototype = {
+ on: function(actionName, handler, scope) {
+ var ah = this.actionHandler;
+ if (ah[actionName]) {
+ throw "There is already an actionName called '" + actionName + "'";
+ }
+ ah[actionName] = [handler, scope];
+ },
+
+ send: function(actionName, data) {
try {
- comObj.postMessage({
+ this.comObj.postMessage({
action: actionName,
data: data
});
throw e;
}
}
-}
\ No newline at end of file
+
+}
+