Change postMessage to send only one object that holds the action and data.
This commit is contained in:
parent
a3d815074d
commit
da7f555fd7
@ -11,10 +11,9 @@ var JpegStreamProxy = (function() {
|
|||||||
this.dict = dict;
|
this.dict = dict;
|
||||||
|
|
||||||
// Tell the main thread to create an image.
|
// Tell the main thread to create an image.
|
||||||
postMessage("jpeg_stream");
|
|
||||||
postMessage({
|
postMessage({
|
||||||
id: this.id,
|
action: jpeg_stream,
|
||||||
str: bytesToString(bytes)
|
data: bytesToString(bytes)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,12 +234,14 @@ function CanvasProxy(width, height) {
|
|||||||
* resets the cmdQueue.
|
* resets the cmdQueue.
|
||||||
*/
|
*/
|
||||||
CanvasProxy.prototype.flush = function() {
|
CanvasProxy.prototype.flush = function() {
|
||||||
postMessage("canvas_proxy_cmd_queue");
|
|
||||||
postMessage({
|
postMessage({
|
||||||
id: this.id,
|
action: "canvas_proxy_cmd_queue",
|
||||||
cmdQueue: this.cmdQueue,
|
data: {
|
||||||
width: this.width,
|
id: this.id,
|
||||||
height: this.height
|
cmdQueue: this.cmdQueue,
|
||||||
|
width: this.width,
|
||||||
|
height: this.height
|
||||||
|
}
|
||||||
});
|
});
|
||||||
this.cmdQueue.length = 0;
|
this.cmdQueue.length = 0;
|
||||||
}
|
}
|
||||||
|
10
fonts.js
10
fonts.js
@ -768,12 +768,14 @@ var Font = (function () {
|
|||||||
// Insert the font-face css on the page. In a web worker, this needs to
|
// Insert the font-face css on the page. In a web worker, this needs to
|
||||||
// be forwareded on the main thread.
|
// be forwareded on the main thread.
|
||||||
if (typeof window == "undefined") {
|
if (typeof window == "undefined") {
|
||||||
postMessage("font");
|
postMessage({
|
||||||
postMessage(JSON.stringify({
|
action: "font",
|
||||||
str: str,
|
data: {
|
||||||
|
raw: str,
|
||||||
fontName: fontName,
|
fontName: fontName,
|
||||||
mimetype: this.mimetype
|
mimetype: this.mimetype
|
||||||
}));
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
var base64 = window.btoa(str);
|
var base64 = window.btoa(str);
|
||||||
|
|
||||||
|
@ -15,8 +15,10 @@ function toc(msg) {
|
|||||||
|
|
||||||
function log() {
|
function log() {
|
||||||
var args = Array.prototype.slice.call(arguments);
|
var args = Array.prototype.slice.call(arguments);
|
||||||
postMessage("log");
|
postMessage({
|
||||||
postMessage(JSON.stringify(args))
|
action: "log",
|
||||||
|
args: args
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
var console = {
|
var console = {
|
||||||
@ -42,8 +44,10 @@ onmessage = function(event) {
|
|||||||
// If there is no pdfDocument yet, then the sent data is the PDFDocument.
|
// If there is no pdfDocument yet, then the sent data is the PDFDocument.
|
||||||
if (!pdfDocument) {
|
if (!pdfDocument) {
|
||||||
pdfDocument = new PDFDoc(new Stream(data));
|
pdfDocument = new PDFDoc(new Stream(data));
|
||||||
postMessage("pdf_num_page");
|
postMessage({
|
||||||
postMessage(pdfDocument.numPages)
|
action: "pdf_num_pages",
|
||||||
|
data: pdfDocument.numPages
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// User requested to render a certain page.
|
// User requested to render a certain page.
|
||||||
|
132
worker_client.js
132
worker_client.js
@ -15,7 +15,7 @@ function WorkerPDFDoc(canvas) {
|
|||||||
|
|
||||||
this.ctx = canvas.getContext("2d");
|
this.ctx = canvas.getContext("2d");
|
||||||
this.canvas = canvas;
|
this.canvas = canvas;
|
||||||
this.worker = new Worker('worker.js');
|
this.worker = new Worker('pdf_worker.js');
|
||||||
|
|
||||||
this.numPage = 1;
|
this.numPage = 1;
|
||||||
this.numPages = null;
|
this.numPages = null;
|
||||||
@ -147,90 +147,44 @@ function WorkerPDFDoc(canvas) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* onMessage state machine.
|
* Functions to handle data sent by the WebWorker.
|
||||||
*/
|
*/
|
||||||
const WAIT = 0;
|
var actionHandler = {
|
||||||
const CANVAS_PROXY_CMD_QUEUE = 1;
|
"log": function(data) {
|
||||||
const LOG = 2;
|
console.log.apply(console, data);
|
||||||
const FONT = 3;
|
},
|
||||||
const PDF_NUM_PAGE = 4;
|
|
||||||
const JPEG_STREAM = 5;
|
"pdf_num_pages": function(data) {
|
||||||
|
this.numPages = parseInt(data);
|
||||||
|
if (this.loadCallback) {
|
||||||
|
this.loadCallback();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"font": function(data) {
|
||||||
|
var base64 = window.btoa(data.raw);
|
||||||
|
|
||||||
var onMessageState = WAIT;
|
// Add the @font-face rule to the document
|
||||||
this.worker.onmessage = function(event) {
|
var url = "url(data:" + data.mimetype + ";base64," + base64 + ");";
|
||||||
var data = event.data;
|
var rule = "@font-face { font-family:'" + data.fontName + "';src:" + url + "}";
|
||||||
// console.log("onMessageRaw", data);
|
var styleSheet = document.styleSheets[0];
|
||||||
switch (onMessageState) {
|
styleSheet.insertRule(rule, styleSheet.length);
|
||||||
case WAIT:
|
|
||||||
if (typeof data != "string") {
|
|
||||||
throw "expecting to get an string";
|
|
||||||
}
|
|
||||||
switch (data) {
|
|
||||||
case "pdf_num_page":
|
|
||||||
onMessageState = PDF_NUM_PAGE;
|
|
||||||
return;
|
|
||||||
|
|
||||||
case "log":
|
// Just adding the font-face to the DOM doesn't make it load. It
|
||||||
onMessageState = LOG;
|
// seems it's loaded once Gecko notices it's used. Therefore,
|
||||||
return;
|
// add a div on the page using the loaded font.
|
||||||
|
var div = document.createElement("div");
|
||||||
|
document.getElementById("fonts").innerHTML += "<div style='font-family:" + data.fontName + "'>j</div>";
|
||||||
|
},
|
||||||
|
|
||||||
case "canvas_proxy_cmd_queue":
|
"jpeg_stream": function(data) {
|
||||||
onMessageState = CANVAS_PROXY_CMD_QUEUE;
|
var img = new Image();
|
||||||
return;
|
img.src = "data:image/jpeg;base64," + window.btoa(data);
|
||||||
|
imagesList[data.id] = img;
|
||||||
case "font":
|
console.log("got image", data.id)
|
||||||
onMessageState = FONT;
|
},
|
||||||
return;
|
|
||||||
|
"canvas_proxy_cmd_queue": function(data) {
|
||||||
case "jpeg_stream":
|
|
||||||
onMessageState = JPEG_STREAM;
|
|
||||||
return;
|
|
||||||
|
|
||||||
default:
|
|
||||||
throw "unkown state: " + data
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case JPEG_STREAM:
|
|
||||||
var img = new Image();
|
|
||||||
img.src = "data:image/jpeg;base64," + window.btoa(data.str);
|
|
||||||
imagesList[data.id] = img;
|
|
||||||
console.log("got image", data.id)
|
|
||||||
break;
|
|
||||||
|
|
||||||
case PDF_NUM_PAGE:
|
|
||||||
this.numPages = parseInt(data);
|
|
||||||
if (this.loadCallback) {
|
|
||||||
this.loadCallback();
|
|
||||||
}
|
|
||||||
onMessageState = WAIT;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case FONT:
|
|
||||||
data = JSON.parse(data);
|
|
||||||
var base64 = window.btoa(data.str);
|
|
||||||
|
|
||||||
// Add the @font-face rule to the document
|
|
||||||
var url = "url(data:" + data.mimetype + ";base64," + base64 + ");";
|
|
||||||
var rule = "@font-face { font-family:'" + data.fontName + "';src:" + url + "}";
|
|
||||||
var styleSheet = document.styleSheets[0];
|
|
||||||
styleSheet.insertRule(rule, styleSheet.length);
|
|
||||||
|
|
||||||
// Just adding the font-face to the DOM doesn't make it load. It
|
|
||||||
// seems it's loaded once Gecko notices it's used. Therefore,
|
|
||||||
// add a div on the page using the loaded font.
|
|
||||||
var div = document.createElement("div");
|
|
||||||
document.getElementById("fonts").innerHTML += "<div style='font-family:" + data.fontName + "'>j</div>";
|
|
||||||
|
|
||||||
onMessageState = WAIT;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case LOG:
|
|
||||||
console.log.apply(console, JSON.parse(data));
|
|
||||||
onMessageState = WAIT;
|
|
||||||
break;
|
|
||||||
|
|
||||||
case CANVAS_PROXY_CMD_QUEUE:
|
|
||||||
var id = data.id;
|
var id = data.id;
|
||||||
var cmdQueue = data.cmdQueue;
|
var cmdQueue = data.cmdQueue;
|
||||||
|
|
||||||
@ -250,13 +204,21 @@ function WorkerPDFDoc(canvas) {
|
|||||||
renderProxyCanvas(canvasList[id], cmdQueue);
|
renderProxyCanvas(canvasList[id], cmdQueue);
|
||||||
if (id == 0) toc("canvas rendering")
|
if (id == 0) toc("canvas rendering")
|
||||||
}, 0);
|
}, 0);
|
||||||
onMessageState = WAIT;
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}.bind(this);
|
}
|
||||||
|
|
||||||
|
// List to the WebWorker for data and call actionHandler on it.
|
||||||
|
this.worker.onmessage = function(event) {
|
||||||
|
var data = event.data;
|
||||||
|
if (data.action in actionHandler) {
|
||||||
|
actionHandler[data.action].call(this, data.data);
|
||||||
|
} else {
|
||||||
|
throw "Unkown action from worker: " + data.action;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
WorkerPDFDoc.prototype.open = function(url, callback) {
|
WorkerPDFDoc.prototype.open = function(url, callback) {
|
||||||
var req = new XMLHttpRequest();
|
var req = new XMLHttpRequest();
|
||||||
req.open("GET", url);
|
req.open("GET", url);
|
||||||
req.mozResponseType = req.responseType = "arraybuffer";
|
req.mozResponseType = req.responseType = "arraybuffer";
|
||||||
|
Loading…
Reference in New Issue
Block a user