2011-10-25 18:18:22 -07:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-09-07 10:16:02 -07:00
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
2012-08-31 15:48:21 -07:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2011-09-07 10:16:02 -07:00
|
|
|
|
2011-10-25 18:18:22 -07:00
|
|
|
'use strict';
|
|
|
|
|
2011-10-25 10:16:20 -07:00
|
|
|
function MessageHandler(name, comObj) {
|
|
|
|
this.name = name;
|
|
|
|
this.comObj = comObj;
|
2011-12-07 15:36:27 -08:00
|
|
|
this.callbackIndex = 1;
|
2011-12-08 14:51:26 -08:00
|
|
|
var callbacks = this.callbacks = {};
|
2011-10-25 10:16:20 -07:00
|
|
|
var ah = this.actionHandler = {};
|
|
|
|
|
2011-10-28 21:23:30 +03:00
|
|
|
ah['console_log'] = [function ahConsoleLog(data) {
|
2012-05-14 17:19:09 -07:00
|
|
|
console.log.apply(console, data);
|
2011-10-25 10:16:20 -07:00
|
|
|
}];
|
2011-10-28 21:23:30 +03:00
|
|
|
ah['console_error'] = [function ahConsoleError(data) {
|
2012-05-14 17:19:09 -07:00
|
|
|
console.error.apply(console, data);
|
|
|
|
}];
|
|
|
|
ah['_warn'] = [function ah_Warn(data) {
|
|
|
|
warn(data);
|
2011-10-25 10:16:20 -07:00
|
|
|
}];
|
2011-12-08 14:51:26 -08:00
|
|
|
|
|
|
|
comObj.onmessage = function messageHandlerComObjOnMessage(event) {
|
|
|
|
var data = event.data;
|
|
|
|
if (data.isReply) {
|
2011-12-07 15:36:27 -08:00
|
|
|
var callbackId = data.callbackId;
|
2011-12-08 14:51:26 -08:00
|
|
|
if (data.callbackId in callbacks) {
|
|
|
|
var callback = callbacks[callbackId];
|
|
|
|
delete callbacks[callbackId];
|
2011-12-07 15:36:27 -08:00
|
|
|
callback(data.data);
|
|
|
|
} else {
|
2012-01-24 22:04:59 +02:00
|
|
|
error('Cannot resolve callback ' + callbackId);
|
2011-12-07 15:36:27 -08:00
|
|
|
}
|
2011-12-08 14:51:26 -08:00
|
|
|
} else if (data.action in ah) {
|
2011-10-25 10:16:20 -07:00
|
|
|
var action = ah[data.action];
|
2011-12-14 14:02:00 -08:00
|
|
|
if (data.callbackId) {
|
|
|
|
var promise = new Promise();
|
|
|
|
promise.then(function(resolvedData) {
|
|
|
|
comObj.postMessage({
|
|
|
|
isReply: true,
|
|
|
|
callbackId: data.callbackId,
|
|
|
|
data: resolvedData
|
|
|
|
});
|
|
|
|
});
|
|
|
|
action[0].call(action[1], data.data, promise);
|
|
|
|
} else {
|
|
|
|
action[0].call(action[1], data.data);
|
|
|
|
}
|
2011-10-25 10:16:20 -07:00
|
|
|
} else {
|
2012-01-24 22:04:59 +02:00
|
|
|
error('Unkown action from worker: ' + data.action);
|
2011-10-25 10:16:20 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageHandler.prototype = {
|
2011-10-28 21:23:30 +03:00
|
|
|
on: function messageHandlerOn(actionName, handler, scope) {
|
2011-10-25 10:16:20 -07:00
|
|
|
var ah = this.actionHandler;
|
|
|
|
if (ah[actionName]) {
|
2012-01-24 22:04:59 +02:00
|
|
|
error('There is already an actionName called "' + actionName + '"');
|
2011-10-25 10:16:20 -07:00
|
|
|
}
|
|
|
|
ah[actionName] = [handler, scope];
|
|
|
|
},
|
2011-12-12 11:38:16 -08:00
|
|
|
/**
|
|
|
|
* Sends a message to the comObj to invoke the action with the supplied data.
|
|
|
|
* @param {String} actionName Action to call.
|
|
|
|
* @param {JSON} data JSON data to send.
|
|
|
|
* @param {function} [callback] Optional callback that will handle a reply.
|
|
|
|
*/
|
2011-12-07 15:36:27 -08:00
|
|
|
send: function messageHandlerSend(actionName, data, callback) {
|
|
|
|
var message = {
|
2011-10-25 10:16:20 -07:00
|
|
|
action: actionName,
|
|
|
|
data: data
|
2011-12-07 15:36:27 -08:00
|
|
|
};
|
|
|
|
if (callback) {
|
|
|
|
var callbackId = this.callbackIndex++;
|
|
|
|
this.callbacks[callbackId] = callback;
|
|
|
|
message.callbackId = callbackId;
|
|
|
|
}
|
|
|
|
this.comObj.postMessage(message);
|
2011-10-25 10:16:20 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-03 15:30:53 +01:00
|
|
|
var WorkerMessageHandler = {
|
2011-10-28 21:23:30 +03:00
|
|
|
setup: function wphSetup(handler) {
|
2012-02-21 14:10:44 +01:00
|
|
|
var pdfModel = null;
|
2011-10-09 10:37:53 +02:00
|
|
|
|
2012-06-23 15:49:17 -05:00
|
|
|
function loadDocument(pdfData, pdfModelSource) {
|
2011-10-08 15:19:48 +02:00
|
|
|
// Create only the model of the PDFDoc, which is enough for
|
|
|
|
// processing the content of the pdf.
|
2012-06-23 14:48:33 -05:00
|
|
|
var pdfPassword = pdfModelSource.password;
|
2012-05-14 20:45:07 +02:00
|
|
|
try {
|
|
|
|
pdfModel = new PDFDocument(new Stream(pdfData), pdfPassword);
|
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof PasswordException) {
|
|
|
|
if (e.code === 'needpassword') {
|
|
|
|
handler.send('NeedPassword', {
|
|
|
|
exception: e
|
|
|
|
});
|
|
|
|
} else if (e.code === 'incorrectpassword') {
|
|
|
|
handler.send('IncorrectPassword', {
|
|
|
|
exception: e
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2012-04-11 15:52:15 -07:00
|
|
|
var doc = {
|
|
|
|
numPages: pdfModel.numPages,
|
2012-04-12 15:14:18 -07:00
|
|
|
fingerprint: pdfModel.getFingerprint(),
|
2012-04-11 15:52:15 -07:00
|
|
|
destinations: pdfModel.catalog.destinations,
|
|
|
|
outline: pdfModel.catalog.documentOutline,
|
2012-04-12 15:56:17 -07:00
|
|
|
info: pdfModel.getDocumentInfo(),
|
2012-05-14 20:45:07 +02:00
|
|
|
metadata: pdfModel.catalog.metadata,
|
|
|
|
encrypted: !!pdfModel.xref.encrypt
|
2012-04-11 15:52:15 -07:00
|
|
|
};
|
2012-04-12 12:11:22 -07:00
|
|
|
handler.send('GetDoc', {pdfInfo: doc});
|
2012-06-23 14:48:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
handler.on('test', function wphSetupTest(data) {
|
|
|
|
handler.send('test', data instanceof Uint8Array);
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('GetDocRequest', function wphSetupDoc(data) {
|
|
|
|
var source = data.source;
|
|
|
|
if (source.data) {
|
|
|
|
// the data is array, instantiating directly from it
|
2012-06-23 15:49:17 -05:00
|
|
|
loadDocument(source.data, source);
|
2012-06-23 14:48:33 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PDFJS.getPdf(
|
|
|
|
{
|
|
|
|
url: source.url,
|
|
|
|
progress: function getPDFProgress(evt) {
|
2012-08-22 10:48:56 -05:00
|
|
|
handler.send('DocProgress', {
|
|
|
|
loaded: evt.loaded,
|
|
|
|
total: evt.lengthComputable ? evt.total : void(0)
|
|
|
|
});
|
2012-06-23 14:48:33 -05:00
|
|
|
},
|
|
|
|
error: function getPDFError(e) {
|
|
|
|
handler.send('DocError', 'Unexpected server response of ' +
|
|
|
|
e.target.status + '.');
|
|
|
|
},
|
|
|
|
headers: source.httpHeaders
|
|
|
|
},
|
|
|
|
function getPDFLoad(data) {
|
2012-06-23 15:49:17 -05:00
|
|
|
loadDocument(data, source);
|
2012-06-23 14:48:33 -05:00
|
|
|
});
|
2012-04-11 15:52:15 -07:00
|
|
|
});
|
|
|
|
|
2012-04-14 13:54:31 -07:00
|
|
|
handler.on('GetPageRequest', function wphSetupGetPage(data) {
|
2012-04-12 10:01:07 -07:00
|
|
|
var pageNumber = data.pageIndex + 1;
|
|
|
|
var pdfPage = pdfModel.getPage(pageNumber);
|
2012-04-11 15:52:15 -07:00
|
|
|
var page = {
|
2012-04-12 10:01:07 -07:00
|
|
|
pageIndex: data.pageIndex,
|
2012-04-11 15:52:15 -07:00
|
|
|
rotate: pdfPage.rotate,
|
|
|
|
ref: pdfPage.ref,
|
2012-04-14 13:54:31 -07:00
|
|
|
view: pdfPage.view
|
2012-04-11 15:52:15 -07:00
|
|
|
};
|
2012-04-12 12:11:22 -07:00
|
|
|
handler.send('GetPage', {pageInfo: page});
|
2011-09-07 10:16:02 -07:00
|
|
|
});
|
2011-10-09 10:37:53 +02:00
|
|
|
|
2012-06-01 14:17:09 -07:00
|
|
|
handler.on('GetData', function wphSetupGetData(data, promise) {
|
|
|
|
promise.resolve(pdfModel.stream.bytes);
|
|
|
|
});
|
|
|
|
|
2012-04-14 13:54:31 -07:00
|
|
|
handler.on('GetAnnotationsRequest', function wphSetupGetAnnotations(data) {
|
|
|
|
var pdfPage = pdfModel.getPage(data.pageIndex + 1);
|
|
|
|
handler.send('GetAnnotations', {
|
|
|
|
pageIndex: data.pageIndex,
|
|
|
|
annotations: pdfPage.getAnnotations()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
handler.on('RenderPageRequest', function wphSetupRenderPage(data) {
|
2012-04-12 10:01:07 -07:00
|
|
|
var pageNum = data.pageIndex + 1;
|
2011-09-07 10:16:02 -07:00
|
|
|
|
2011-10-09 10:37:53 +02:00
|
|
|
// 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.
|
2011-09-07 10:16:02 -07:00
|
|
|
var gfx = new CanvasGraphics(null);
|
|
|
|
|
2011-09-09 11:20:55 -07:00
|
|
|
var start = Date.now();
|
2011-09-30 15:38:54 +02:00
|
|
|
|
|
|
|
var dependency = [];
|
2012-03-12 18:41:40 +01:00
|
|
|
var operatorList = null;
|
2011-11-28 16:55:09 -08:00
|
|
|
try {
|
2012-02-21 14:10:44 +01:00
|
|
|
var page = pdfModel.getPage(pageNum);
|
2011-11-28 16:55:09 -08:00
|
|
|
// Pre compile the pdf page and fetch the fonts/images.
|
2012-03-12 18:41:40 +01:00
|
|
|
operatorList = page.getOperatorList(handler, dependency);
|
2011-11-29 11:32:01 -08:00
|
|
|
} catch (e) {
|
2012-01-19 14:19:19 -05:00
|
|
|
var minimumStackMessage =
|
2012-03-12 18:41:40 +01:00
|
|
|
'worker.js: while trying to getPage() and getOperatorList()';
|
2012-01-19 14:19:19 -05:00
|
|
|
|
2011-11-28 16:55:09 -08:00
|
|
|
// Turn the error into an obj that can be serialized
|
2012-01-19 14:19:19 -05:00
|
|
|
if (typeof e === 'string') {
|
|
|
|
e = {
|
|
|
|
message: e,
|
|
|
|
stack: minimumStackMessage
|
|
|
|
};
|
|
|
|
} else if (typeof e === 'object') {
|
|
|
|
e = {
|
|
|
|
message: e.message || e.toString(),
|
|
|
|
stack: e.stack || minimumStackMessage
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
e = {
|
|
|
|
message: 'Unknown exception type: ' + (typeof e),
|
|
|
|
stack: minimumStackMessage
|
2012-01-20 18:44:51 -05:00
|
|
|
};
|
2012-01-19 14:19:19 -05:00
|
|
|
}
|
|
|
|
|
2012-04-12 12:11:22 -07:00
|
|
|
handler.send('PageError', {
|
2011-11-28 16:55:09 -08:00
|
|
|
pageNum: pageNum,
|
|
|
|
error: e
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2011-09-07 10:16:02 -07:00
|
|
|
|
2012-03-12 18:41:40 +01:00
|
|
|
console.log('page=%d - getOperatorList: time=%dms, len=%d', pageNum,
|
|
|
|
Date.now() - start, operatorList.fnArray.length);
|
2011-09-07 10:16:02 -07:00
|
|
|
|
2011-09-30 15:38:54 +02:00
|
|
|
// Filter the dependecies for fonts.
|
2011-10-08 22:22:30 +02:00
|
|
|
var fonts = {};
|
2011-11-02 15:11:33 -04:00
|
|
|
for (var i = 0, ii = dependency.length; i < ii; i++) {
|
2011-09-30 15:38:54 +02:00
|
|
|
var dep = dependency[i];
|
2011-10-08 22:22:30 +02:00
|
|
|
if (dep.indexOf('font_') == 0) {
|
|
|
|
fonts[dep] = true;
|
2011-09-30 15:38:54 +02:00
|
|
|
}
|
|
|
|
}
|
2012-04-12 12:11:22 -07:00
|
|
|
handler.send('RenderPage', {
|
2012-04-12 10:01:07 -07:00
|
|
|
pageIndex: data.pageIndex,
|
2012-03-12 18:41:40 +01:00
|
|
|
operatorList: operatorList,
|
2011-10-11 19:51:35 +02:00
|
|
|
depFonts: Object.keys(fonts)
|
2011-09-07 10:16:02 -07:00
|
|
|
});
|
|
|
|
}, this);
|
2011-10-09 10:37:53 +02:00
|
|
|
|
2012-04-20 16:49:08 -07:00
|
|
|
handler.on('GetTextContent', function wphExtractText(data, promise) {
|
|
|
|
var pageNum = data.pageIndex + 1;
|
2011-12-11 18:14:52 -06:00
|
|
|
var start = Date.now();
|
|
|
|
|
2012-04-08 16:18:43 -07:00
|
|
|
var textContent = '';
|
|
|
|
try {
|
|
|
|
var page = pdfModel.getPage(pageNum);
|
|
|
|
textContent = page.extractTextContent();
|
2012-04-20 16:49:08 -07:00
|
|
|
promise.resolve(textContent);
|
2012-04-08 16:18:43 -07:00
|
|
|
} catch (e) {
|
|
|
|
// Skip errored pages
|
2012-04-20 16:49:08 -07:00
|
|
|
promise.reject(e);
|
2011-12-11 18:14:52 -06:00
|
|
|
}
|
2011-12-10 17:24:54 -06:00
|
|
|
|
2012-04-08 16:18:43 -07:00
|
|
|
console.log('text indexing: page=%d - time=%dms',
|
|
|
|
pageNum, Date.now() - start);
|
2011-12-10 17:24:54 -06:00
|
|
|
});
|
2011-09-07 10:16:02 -07:00
|
|
|
}
|
2011-10-09 10:37:53 +02:00
|
|
|
};
|
2011-10-25 10:16:20 -07:00
|
|
|
|
2011-10-25 15:43:41 -07:00
|
|
|
var consoleTimer = {};
|
|
|
|
|
2011-10-25 10:16:20 -07:00
|
|
|
var workerConsole = {
|
|
|
|
log: function log() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
postMessage({
|
|
|
|
action: 'console_log',
|
|
|
|
data: args
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
error: function error() {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
postMessage({
|
|
|
|
action: 'console_error',
|
|
|
|
data: args
|
|
|
|
});
|
2012-01-29 22:25:06 +02:00
|
|
|
throw 'pdf.js execution error';
|
2011-10-25 10:16:20 -07:00
|
|
|
},
|
|
|
|
|
2011-10-28 21:23:30 +03:00
|
|
|
time: function time(name) {
|
2011-10-25 10:16:20 -07:00
|
|
|
consoleTimer[name] = Date.now();
|
|
|
|
},
|
|
|
|
|
2011-10-28 21:23:30 +03:00
|
|
|
timeEnd: function timeEnd(name) {
|
2011-10-25 10:16:20 -07:00
|
|
|
var time = consoleTimer[name];
|
|
|
|
if (time == null) {
|
2012-01-24 22:04:59 +02:00
|
|
|
error('Unkown timer name ' + name);
|
2011-10-25 10:16:20 -07:00
|
|
|
}
|
|
|
|
this.log('Timer:', name, Date.now() - time);
|
|
|
|
}
|
2011-10-25 10:43:28 -07:00
|
|
|
};
|
2011-10-25 15:43:41 -07:00
|
|
|
|
|
|
|
// Worker thread?
|
|
|
|
if (typeof window === 'undefined') {
|
2011-11-01 19:32:20 +01:00
|
|
|
globalScope.console = workerConsole;
|
2011-10-25 15:43:41 -07:00
|
|
|
|
2012-05-14 17:19:09 -07:00
|
|
|
// Add a logger so we can pass warnings on to the main thread, errors will
|
|
|
|
// throw an exception which will be forwarded on automatically.
|
|
|
|
PDFJS.LogManager.addLogger({
|
|
|
|
warn: function(msg) {
|
|
|
|
postMessage({
|
|
|
|
action: '_warn',
|
|
|
|
data: msg
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-10-28 14:32:36 +02:00
|
|
|
var handler = new MessageHandler('worker_processor', this);
|
2011-11-03 15:30:53 +01:00
|
|
|
WorkerMessageHandler.setup(handler);
|
2011-10-25 15:43:41 -07:00
|
|
|
}
|
2011-10-27 21:51:10 +03:00
|
|
|
|