2011-09-13 02:37:33 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-06-23 20:09:36 +09:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'use strict';
|
2011-06-23 05:54:16 +09:00
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
if (typeof console.time == 'undefined') {
|
2011-06-24 06:06:33 +09:00
|
|
|
var consoleTimer = {};
|
|
|
|
console.time = function(name) {
|
|
|
|
consoleTimer[name] = Date.now();
|
|
|
|
};
|
2011-07-06 15:06:45 +09:00
|
|
|
|
2011-06-24 06:06:33 +09:00
|
|
|
console.timeEnd = function(name) {
|
|
|
|
var time = consoleTimer[name];
|
|
|
|
if (time == null) {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Unkown timer name ' + name;
|
2011-06-24 06:06:33 +09:00
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
this.log('Timer:', name, Date.now() - time);
|
2011-06-24 06:06:33 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-06-27 03:55:27 +09:00
|
|
|
function FontWorker() {
|
2011-07-06 15:06:45 +09:00
|
|
|
this.worker = new Worker('worker/font.js');
|
2011-06-27 03:55:27 +09:00
|
|
|
this.fontsWaiting = 0;
|
|
|
|
this.fontsWaitingCallbacks = [];
|
|
|
|
|
|
|
|
// Listen to the WebWorker for data and call actionHandler on it.
|
|
|
|
this.worker.onmessage = function(event) {
|
|
|
|
var data = event.data;
|
2011-07-06 15:06:45 +09:00
|
|
|
var actionHandler = this.actionHandler;
|
2011-06-27 03:55:27 +09:00
|
|
|
if (data.action in actionHandler) {
|
|
|
|
actionHandler[data.action].call(this, data.data);
|
|
|
|
} else {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Unkown action from worker: ' + data.action;
|
2011-06-27 03:55:27 +09:00
|
|
|
}
|
|
|
|
}.bind(this);
|
2011-07-06 15:06:45 +09:00
|
|
|
|
2011-06-28 16:51:49 +09:00
|
|
|
this.$handleFontLoadedCallback = this.handleFontLoadedCallback.bind(this);
|
2011-06-27 03:55:27 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
FontWorker.prototype = {
|
2011-06-28 16:51:49 +09:00
|
|
|
handleFontLoadedCallback: function() {
|
|
|
|
// Decrease the number of fonts wainting to be loaded.
|
|
|
|
this.fontsWaiting--;
|
|
|
|
// If all fonts are available now, then call all the callbacks.
|
|
|
|
if (this.fontsWaiting == 0) {
|
|
|
|
var callbacks = this.fontsWaitingCallbacks;
|
|
|
|
for (var i = 0; i < callbacks.length; i++) {
|
|
|
|
callbacks[i]();
|
|
|
|
}
|
|
|
|
this.fontsWaitingCallbacks.length = 0;
|
|
|
|
}
|
|
|
|
},
|
2011-07-06 15:06:45 +09:00
|
|
|
|
2011-06-27 03:55:27 +09:00
|
|
|
actionHandler: {
|
2011-07-06 15:06:45 +09:00
|
|
|
'log': function(data) {
|
2011-06-27 03:58:22 +09:00
|
|
|
console.log.apply(console, data);
|
|
|
|
},
|
2011-07-06 15:06:45 +09:00
|
|
|
|
|
|
|
'fonts': function(data) {
|
2011-06-27 03:55:27 +09:00
|
|
|
// console.log("got processed fonts from worker", Object.keys(data));
|
|
|
|
for (name in data) {
|
2011-06-30 21:25:57 +09:00
|
|
|
// Update the encoding property.
|
|
|
|
var font = Fonts.lookup(name);
|
|
|
|
font.properties = {
|
2011-06-27 03:58:22 +09:00
|
|
|
encoding: data[name].encoding
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|
2011-06-30 21:25:57 +09:00
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
// Call `Font.prototype.bindDOM` to make the font get loaded
|
|
|
|
// on the page.
|
2011-06-28 16:51:49 +09:00
|
|
|
Font.prototype.bindDOM.call(
|
2011-06-30 21:25:57 +09:00
|
|
|
font,
|
2011-06-28 16:51:49 +09:00
|
|
|
data[name].str,
|
|
|
|
// IsLoadedCallback.
|
|
|
|
this.$handleFontLoadedCallback
|
|
|
|
);
|
2011-06-26 08:35:17 +09:00
|
|
|
}
|
2011-06-27 03:55:27 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
ensureFonts: function(data, callback) {
|
|
|
|
var font;
|
|
|
|
var notLoaded = [];
|
|
|
|
for (var i = 0; i < data.length; i++) {
|
|
|
|
font = data[i];
|
|
|
|
if (Fonts[font.name]) {
|
|
|
|
continue;
|
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
|
2011-06-30 21:25:57 +09:00
|
|
|
// Register the font but don't pass in any real data. The idea is to
|
|
|
|
// store as less data as possible to reduce memory usage.
|
|
|
|
Fonts.registerFont(font.name, Object.create(null), Object.create(null));
|
2011-06-27 03:55:27 +09:00
|
|
|
|
|
|
|
// Mark this font to be handled later.
|
|
|
|
notLoaded.push(font);
|
|
|
|
// Increate the number of fonts to wait for.
|
|
|
|
this.fontsWaiting++;
|
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
|
|
|
|
console.time('ensureFonts');
|
2011-06-27 03:55:27 +09:00
|
|
|
// If there are fonts, that need to get loaded, tell the FontWorker to get
|
|
|
|
// started and push the callback on the waiting-callback-stack.
|
|
|
|
if (notLoaded.length != 0) {
|
2011-07-06 15:06:45 +09:00
|
|
|
console.log('fonts -> FontWorker');
|
2011-06-27 03:55:27 +09:00
|
|
|
// Send the worker the fonts to work on.
|
|
|
|
this.worker.postMessage({
|
2011-07-06 15:06:45 +09:00
|
|
|
action: 'fonts',
|
|
|
|
data: notLoaded
|
2011-06-27 03:55:27 +09:00
|
|
|
});
|
|
|
|
if (callback) {
|
|
|
|
this.fontsWaitingCallbacks.push(callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// All fonts are present? Well, then just call the callback if there is one.
|
|
|
|
else {
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
}
|
|
|
|
};
|
2011-06-27 03:55:27 +09:00
|
|
|
|
2011-06-23 05:54:16 +09:00
|
|
|
function WorkerPDFDoc(canvas) {
|
2011-07-06 15:06:45 +09:00
|
|
|
var timer = null;
|
2011-06-23 05:54:16 +09:00
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
this.ctx = canvas.getContext('2d');
|
2011-06-23 05:54:16 +09:00
|
|
|
this.canvas = canvas;
|
2011-06-27 03:44:13 +09:00
|
|
|
this.worker = new Worker('worker/pdf.js');
|
2011-06-27 03:55:27 +09:00
|
|
|
this.fontWorker = new FontWorker();
|
|
|
|
this.waitingForFonts = false;
|
|
|
|
this.waitingForFontsCallback = [];
|
2011-06-23 05:54:16 +09:00
|
|
|
|
|
|
|
this.numPage = 1;
|
|
|
|
this.numPages = null;
|
|
|
|
|
|
|
|
var imagesList = {};
|
|
|
|
var canvasList = {
|
|
|
|
0: canvas
|
|
|
|
};
|
|
|
|
var patternList = {};
|
|
|
|
var gradient;
|
|
|
|
|
|
|
|
var currentX = 0;
|
|
|
|
var currentXStack = [];
|
|
|
|
|
|
|
|
var ctxSpecial = {
|
2011-07-06 15:06:45 +09:00
|
|
|
'$setCurrentX': function(value) {
|
2011-06-23 05:54:16 +09:00
|
|
|
currentX = value;
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$addCurrentX': function(value) {
|
2011-06-23 05:54:16 +09:00
|
|
|
currentX += value;
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$saveCurrentX': function() {
|
2011-06-23 05:54:16 +09:00
|
|
|
currentXStack.push(currentX);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$restoreCurrentX': function() {
|
2011-06-23 05:54:16 +09:00
|
|
|
currentX = currentXStack.pop();
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$showText': function(y, text) {
|
2011-06-27 03:55:27 +09:00
|
|
|
text = Fonts.charsToUnicode(text);
|
2011-06-23 05:54:16 +09:00
|
|
|
this.translate(currentX, -1 * y);
|
2011-06-24 06:55:14 +09:00
|
|
|
this.fillText(text, 0, 0);
|
2011-06-23 05:54:16 +09:00
|
|
|
currentX += this.measureText(text).width;
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$putImageData': function(imageData, x, y) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var imgData = this.getImageData(0, 0, imageData.width, imageData.height);
|
|
|
|
|
|
|
|
// Store the .data property to avaid property lookups.
|
|
|
|
var imageRealData = imageData.data;
|
|
|
|
var imgRealData = imgData.data;
|
|
|
|
|
|
|
|
// Copy over the imageData.
|
|
|
|
var len = imageRealData.length;
|
|
|
|
while (len--)
|
2011-07-06 15:06:45 +09:00
|
|
|
imgRealData[len] = imageRealData[len];
|
2011-06-23 05:54:16 +09:00
|
|
|
|
|
|
|
this.putImageData(imgData, x, y);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$drawImage': function(id, x, y, sx, sy, swidth, sheight) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var image = imagesList[id];
|
|
|
|
if (!image) {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Image not found: ' + id;
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
|
|
|
this.drawImage(image, x, y, image.width, image.height,
|
|
|
|
sx, sy, swidth, sheight);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$drawCanvas': function(id, x, y, sx, sy, swidth, sheight) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var canvas = canvasList[id];
|
|
|
|
if (!canvas) {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Canvas not found';
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
|
|
|
if (sheight != null) {
|
|
|
|
this.drawImage(canvas, x, y, canvas.width, canvas.height,
|
|
|
|
sx, sy, swidth, sheight);
|
|
|
|
} else {
|
|
|
|
this.drawImage(canvas, x, y, canvas.width, canvas.height);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$createLinearGradient': function(x0, y0, x1, y1) {
|
2011-06-23 05:54:16 +09:00
|
|
|
gradient = this.createLinearGradient(x0, y0, x1, y1);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$createPatternFromCanvas': function(patternId, canvasId, kind) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var canvas = canvasList[canvasId];
|
|
|
|
if (!canvas) {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Canvas not found';
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
|
|
|
patternList[patternId] = this.createPattern(canvas, kind);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$addColorStop': function(i, rgba) {
|
2011-06-23 05:54:16 +09:00
|
|
|
gradient.addColorStop(i, rgba);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$fillStyleGradient': function() {
|
2011-06-23 05:54:16 +09:00
|
|
|
this.fillStyle = gradient;
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$fillStylePattern': function(id) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var pattern = patternList[id];
|
|
|
|
if (!pattern) {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Pattern not found';
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
|
|
|
this.fillStyle = pattern;
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$strokeStyleGradient': function() {
|
2011-06-23 05:54:16 +09:00
|
|
|
this.strokeStyle = gradient;
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$strokeStylePattern': function(id) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var pattern = patternList[id];
|
|
|
|
if (!pattern) {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Pattern not found';
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
|
|
|
this.strokeStyle = pattern;
|
2011-06-27 03:55:27 +09:00
|
|
|
},
|
2011-06-29 21:26:38 +09:00
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'$setFont': function(name, size) {
|
2011-06-29 21:26:38 +09:00
|
|
|
this.font = size + 'px "' + name + '"';
|
|
|
|
Fonts.setActive(name, size);
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|
2011-06-23 05:54:16 +09:00
|
|
|
|
2011-06-23 20:09:36 +09:00
|
|
|
function renderProxyCanvas(canvas, cmdQueue) {
|
2011-07-06 15:06:45 +09:00
|
|
|
var ctx = canvas.getContext('2d');
|
2011-06-23 20:09:36 +09:00
|
|
|
var cmdQueueLength = cmdQueue.length;
|
|
|
|
for (var i = 0; i < cmdQueueLength; i++) {
|
|
|
|
var opp = cmdQueue[i];
|
2011-07-06 15:06:45 +09:00
|
|
|
if (opp[0] == '$') {
|
2011-06-23 05:54:16 +09:00
|
|
|
ctx[opp[1]] = opp[2];
|
|
|
|
} else if (opp[0] in ctxSpecial) {
|
|
|
|
ctxSpecial[opp[0]].apply(ctx, opp[1]);
|
|
|
|
} else {
|
|
|
|
ctx[opp[0]].apply(ctx, opp[1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-23 20:25:59 +09:00
|
|
|
* Functions to handle data sent by the WebWorker.
|
2011-06-23 05:54:16 +09:00
|
|
|
*/
|
2011-06-23 20:25:59 +09:00
|
|
|
var actionHandler = {
|
2011-07-06 15:06:45 +09:00
|
|
|
'log': function(data) {
|
2011-06-23 20:25:59 +09:00
|
|
|
console.log.apply(console, data);
|
|
|
|
},
|
2011-07-06 15:06:45 +09:00
|
|
|
|
|
|
|
'pdf_num_pages': function(data) {
|
2011-06-23 20:25:59 +09:00
|
|
|
this.numPages = parseInt(data);
|
|
|
|
if (this.loadCallback) {
|
|
|
|
this.loadCallback();
|
|
|
|
}
|
|
|
|
},
|
2011-07-06 15:06:45 +09:00
|
|
|
|
|
|
|
'font': function(data) {
|
2011-06-23 20:25:59 +09:00
|
|
|
var base64 = window.btoa(data.raw);
|
|
|
|
|
|
|
|
// Add the @font-face rule to the document
|
2011-07-06 15:06:45 +09:00
|
|
|
var url = 'url(data:' + data.mimetype + ';base64,' + base64 + ');';
|
|
|
|
var rule = ("@font-face { font-family:'" + data.fontName +
|
|
|
|
"';src:" + url + '}');
|
2011-06-23 20:25:59 +09:00
|
|
|
var styleSheet = document.styleSheets[0];
|
2011-07-02 06:59:20 +09:00
|
|
|
styleSheet.insertRule(rule, styleSheet.cssRules.length);
|
2011-06-23 20:25:59 +09:00
|
|
|
|
|
|
|
// 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.
|
2011-07-06 15:06:45 +09:00
|
|
|
var div = document.createElement('div');
|
|
|
|
var style = 'font-family:"' + data.fontName +
|
2011-06-24 02:43:01 +09:00
|
|
|
'";position: absolute;top:-99999;left:-99999;z-index:-99999';
|
2011-07-06 15:06:45 +09:00
|
|
|
div.setAttribute('style', style);
|
2011-06-24 02:43:01 +09:00
|
|
|
document.body.appendChild(div);
|
2011-06-23 20:25:59 +09:00
|
|
|
},
|
2011-07-06 15:06:45 +09:00
|
|
|
|
|
|
|
'setup_page': function(data) {
|
|
|
|
var size = data.split(',');
|
2011-06-30 12:43:59 +09:00
|
|
|
var canvas = this.canvas, ctx = this.ctx;
|
|
|
|
canvas.width = parseInt(size[0]);
|
|
|
|
canvas.height = parseInt(size[1]);
|
|
|
|
},
|
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'fonts': function(data) {
|
2011-06-27 03:55:27 +09:00
|
|
|
this.waitingForFonts = true;
|
|
|
|
this.fontWorker.ensureFonts(data, function() {
|
|
|
|
this.waitingForFonts = false;
|
|
|
|
var callbacks = this.waitingForFontsCallback;
|
|
|
|
for (var i = 0; i < callbacks.length; i++) {
|
|
|
|
callbacks[i]();
|
|
|
|
}
|
2011-06-26 08:35:17 +09:00
|
|
|
this.waitingForFontsCallback.length = 0;
|
2011-06-27 03:55:27 +09:00
|
|
|
}.bind(this));
|
|
|
|
},
|
2011-06-23 05:54:16 +09:00
|
|
|
|
2011-07-06 15:06:45 +09:00
|
|
|
'jpeg_stream': function(data) {
|
2011-06-23 20:25:59 +09:00
|
|
|
var img = new Image();
|
2011-07-06 15:06:45 +09:00
|
|
|
img.src = 'data:image/jpeg;base64,' + window.btoa(data.raw);
|
2011-06-23 20:25:59 +09:00
|
|
|
imagesList[data.id] = img;
|
|
|
|
},
|
2011-07-06 15:06:45 +09:00
|
|
|
|
|
|
|
'canvas_proxy_cmd_queue': function(data) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var id = data.id;
|
2011-06-23 20:09:36 +09:00
|
|
|
var cmdQueue = data.cmdQueue;
|
2011-06-23 05:54:16 +09:00
|
|
|
|
|
|
|
// Check if there is already a canvas with the given id. If not,
|
|
|
|
// create a new canvas.
|
|
|
|
if (!canvasList[id]) {
|
2011-07-06 15:06:45 +09:00
|
|
|
var newCanvas = document.createElement('canvas');
|
2011-06-23 05:54:16 +09:00
|
|
|
newCanvas.width = data.width;
|
|
|
|
newCanvas.height = data.height;
|
|
|
|
canvasList[id] = newCanvas;
|
|
|
|
}
|
|
|
|
|
2011-06-27 03:55:27 +09:00
|
|
|
var renderData = function() {
|
2011-06-24 05:24:41 +09:00
|
|
|
if (id == 0) {
|
2011-07-06 15:06:45 +09:00
|
|
|
console.time('main canvas rendering');
|
2011-06-24 05:24:41 +09:00
|
|
|
var ctx = this.ctx;
|
|
|
|
ctx.save();
|
2011-07-06 15:06:45 +09:00
|
|
|
ctx.fillStyle = 'rgb(255, 255, 255)';
|
2011-06-24 05:24:41 +09:00
|
|
|
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
|
|
ctx.restore();
|
|
|
|
}
|
2011-06-23 20:09:36 +09:00
|
|
|
renderProxyCanvas(canvasList[id], cmdQueue);
|
2011-06-26 18:46:58 +09:00
|
|
|
if (id == 0) {
|
2011-07-06 15:06:45 +09:00
|
|
|
console.timeEnd('main canvas rendering');
|
|
|
|
console.timeEnd('>>> total page display time:');
|
2011-06-26 18:46:58 +09:00
|
|
|
}
|
2011-06-27 03:55:27 +09:00
|
|
|
}.bind(this);
|
|
|
|
|
|
|
|
if (this.waitingForFonts) {
|
2011-06-27 03:58:22 +09:00
|
|
|
if (id == 0) {
|
2011-07-06 15:06:45 +09:00
|
|
|
console.log('want to render, but not all fonts are there', id);
|
2011-06-27 03:58:22 +09:00
|
|
|
this.waitingForFontsCallback.push(renderData);
|
|
|
|
} else {
|
|
|
|
// console.log("assume canvas doesn't have fonts", id);
|
|
|
|
renderData();
|
|
|
|
}
|
2011-06-27 03:55:27 +09:00
|
|
|
} else {
|
|
|
|
renderData();
|
|
|
|
}
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|
2011-06-23 20:25:59 +09:00
|
|
|
|
2011-06-27 03:55:27 +09:00
|
|
|
// Listen to the WebWorker for data and call actionHandler on it.
|
2011-06-23 20:25:59 +09:00
|
|
|
this.worker.onmessage = function(event) {
|
|
|
|
var data = event.data;
|
|
|
|
if (data.action in actionHandler) {
|
|
|
|
actionHandler[data.action].call(this, data.data);
|
|
|
|
} else {
|
2011-07-06 15:06:45 +09:00
|
|
|
throw 'Unkown action from worker: ' + data.action;
|
2011-06-23 20:25:59 +09:00
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
}.bind(this);
|
2011-06-23 05:54:16 +09:00
|
|
|
}
|
|
|
|
|
2011-06-23 20:25:59 +09:00
|
|
|
WorkerPDFDoc.prototype.open = function(url, callback) {
|
2011-06-23 05:54:16 +09:00
|
|
|
var req = new XMLHttpRequest();
|
2011-07-06 15:06:45 +09:00
|
|
|
req.open('GET', url);
|
|
|
|
req.mozResponseType = req.responseType = 'arraybuffer';
|
|
|
|
req.expected = (document.URL.indexOf('file:') == 0) ? 0 : 200;
|
2011-06-23 05:54:16 +09:00
|
|
|
req.onreadystatechange = function() {
|
|
|
|
if (req.readyState == 4 && req.status == req.expected) {
|
|
|
|
var data = req.mozResponseArrayBuffer || req.mozResponse ||
|
|
|
|
req.responseArrayBuffer || req.response;
|
|
|
|
|
|
|
|
this.loadCallback = callback;
|
|
|
|
this.worker.postMessage(data);
|
|
|
|
this.showPage(this.numPage);
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
req.send(null);
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|
2011-06-23 05:54:16 +09:00
|
|
|
|
|
|
|
WorkerPDFDoc.prototype.showPage = function(numPage) {
|
|
|
|
this.numPage = parseInt(numPage);
|
2011-07-06 15:06:45 +09:00
|
|
|
console.log('=== start rendering page ' + numPage + ' ===');
|
|
|
|
console.time('>>> total page display time:');
|
2011-06-23 05:54:16 +09:00
|
|
|
this.worker.postMessage(numPage);
|
|
|
|
if (this.onChangePage) {
|
|
|
|
this.onChangePage(numPage);
|
|
|
|
}
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|
2011-06-23 05:54:16 +09:00
|
|
|
|
|
|
|
WorkerPDFDoc.prototype.nextPage = function() {
|
|
|
|
if (this.numPage == this.numPages) return;
|
|
|
|
this.showPage(++this.numPage);
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|
2011-06-23 05:54:16 +09:00
|
|
|
|
|
|
|
WorkerPDFDoc.prototype.prevPage = function() {
|
|
|
|
if (this.numPage == 1) return;
|
|
|
|
this.showPage(--this.numPage);
|
2011-07-06 15:06:45 +09:00
|
|
|
};
|