mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
Merge pull request #6771 from yurydelendik/requirejs
Removes hardcoded module loading order
This commit is contained in:
commit
e8db825512
25 changed files with 363 additions and 530 deletions
|
@ -291,3 +291,17 @@ var NetworkManager = (function NetworkManagerClosure() {
|
|||
|
||||
return NetworkManager;
|
||||
})();
|
||||
|
||||
//#if !(FIREFOX || MOZCENTRAL)
|
||||
(function (root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define('pdfjs/core/network', ['exports'], factory);
|
||||
} else if (typeof exports !== 'undefined') {
|
||||
factory(exports);
|
||||
} else {
|
||||
factory((root.pdfjsCoreNetwork = {}));
|
||||
}
|
||||
}(this, function (exports) {
|
||||
exports.NetworkManager = NetworkManager;
|
||||
}));
|
||||
//#endif
|
||||
|
|
|
@ -84,7 +84,13 @@ var WorkerTask = (function WorkerTaskClosure() {
|
|||
|
||||
var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
|
||||
setup: function wphSetup(handler, port) {
|
||||
var testMessageProcessed = false;
|
||||
handler.on('test', function wphSetupTest(data) {
|
||||
if (testMessageProcessed) {
|
||||
return; // we already processed 'test' message once
|
||||
}
|
||||
testMessageProcessed = true;
|
||||
|
||||
// check if Uint8Array can be sent to worker
|
||||
if (!(data instanceof Uint8Array)) {
|
||||
handler.send('test', 'main', false);
|
||||
|
@ -611,51 +617,57 @@ var WorkerMessageHandler = PDFJS.WorkerMessageHandler = {
|
|||
}
|
||||
};
|
||||
|
||||
var consoleTimer = {};
|
||||
|
||||
var workerConsole = {
|
||||
log: function log() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
globalScope.postMessage({
|
||||
targetName: 'main',
|
||||
action: 'console_log',
|
||||
data: args
|
||||
});
|
||||
},
|
||||
|
||||
error: function error() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
globalScope.postMessage({
|
||||
targetName: 'main',
|
||||
action: 'console_error',
|
||||
data: args
|
||||
});
|
||||
throw 'pdf.js execution error';
|
||||
},
|
||||
|
||||
time: function time(name) {
|
||||
consoleTimer[name] = Date.now();
|
||||
},
|
||||
|
||||
timeEnd: function timeEnd(name) {
|
||||
var time = consoleTimer[name];
|
||||
if (!time) {
|
||||
error('Unknown timer name ' + name);
|
||||
}
|
||||
this.log('Timer:', name, Date.now() - time);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Worker thread?
|
||||
if (typeof window === 'undefined' &&
|
||||
!(typeof module !== 'undefined' && module.require)) {
|
||||
function initializeWorker() {
|
||||
//#if !MOZCENTRAL
|
||||
if (!('console' in globalScope)) {
|
||||
var consoleTimer = {};
|
||||
|
||||
var workerConsole = {
|
||||
log: function log() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
globalScope.postMessage({
|
||||
targetName: 'main',
|
||||
action: 'console_log',
|
||||
data: args
|
||||
});
|
||||
},
|
||||
|
||||
error: function error() {
|
||||
var args = Array.prototype.slice.call(arguments);
|
||||
globalScope.postMessage({
|
||||
targetName: 'main',
|
||||
action: 'console_error',
|
||||
data: args
|
||||
});
|
||||
throw 'pdf.js execution error';
|
||||
},
|
||||
|
||||
time: function time(name) {
|
||||
consoleTimer[name] = Date.now();
|
||||
},
|
||||
|
||||
timeEnd: function timeEnd(name) {
|
||||
var time = consoleTimer[name];
|
||||
if (!time) {
|
||||
error('Unknown timer name ' + name);
|
||||
}
|
||||
this.log('Timer:', name, Date.now() - time);
|
||||
}
|
||||
};
|
||||
|
||||
globalScope.console = workerConsole;
|
||||
}
|
||||
//#endif
|
||||
|
||||
var handler = new MessageHandler('worker', 'main', self);
|
||||
WorkerMessageHandler.setup(handler, self);
|
||||
handler.send('ready', null);
|
||||
}
|
||||
|
||||
// Worker thread (and not node.js)?
|
||||
if (typeof window === 'undefined' &&
|
||||
!(typeof module !== 'undefined' && module.require)) {
|
||||
initializeWorker();
|
||||
}
|
||||
|
||||
exports.WorkerTask = WorkerTask;
|
||||
|
|
|
@ -414,7 +414,7 @@ PDFJS.getDocument = function getDocument(src,
|
|||
var transport = new WorkerTransport(messageHandler, task, rangeTransport);
|
||||
task._transport = transport;
|
||||
});
|
||||
}, task._capability.reject);
|
||||
}).catch(task._capability.reject);
|
||||
|
||||
return task;
|
||||
};
|
||||
|
@ -1186,14 +1186,14 @@ var PDFWorker = (function PDFWorkerClosure() {
|
|||
// pdf.worker.js file is needed.
|
||||
//#if !PRODUCTION
|
||||
if (typeof amdRequire === 'function') {
|
||||
amdRequire(['pdfjs/core/worker'], function () {
|
||||
amdRequire(['pdfjs/core/network', 'pdfjs/core/worker'], function () {
|
||||
PDFJS.fakeWorkerFilesLoadedCapability.resolve();
|
||||
});
|
||||
} else if (typeof require === 'function') {
|
||||
require('../core/worker.js');
|
||||
PDFJS.fakeWorkerFilesLoadedCapability.resolve();
|
||||
} else {
|
||||
Util.loadScript(PDFJS.workerSrc);
|
||||
throw new Error('AMD or CommonJS must be used to load fake worker.');
|
||||
}
|
||||
//#endif
|
||||
//#if PRODUCTION && SINGLE_FILE
|
||||
|
@ -1253,8 +1253,16 @@ var PDFWorker = (function PDFWorkerClosure() {
|
|||
// https://bugzilla.mozilla.org/show_bug.cgi?id=683280
|
||||
var worker = new Worker(workerSrc);
|
||||
var messageHandler = new MessageHandler('main', 'worker', worker);
|
||||
|
||||
//#if !PRODUCTION
|
||||
// Don't allow worker to be destroyed by Chrome, see:
|
||||
// https://code.google.com/p/chromium/issues/detail?id=572225
|
||||
var jsWorkerId = '_workerKungfuGrip_' + Math.random();
|
||||
window[jsWorkerId] = worker;
|
||||
//#endif
|
||||
messageHandler.on('test', function PDFWorker_test(data) {
|
||||
//#if !PRODUCTION
|
||||
delete window[jsWorkerId];
|
||||
//#endif
|
||||
if (this.destroyed) {
|
||||
this._readyCapability.reject(new Error('Worker was destroyed'));
|
||||
messageHandler.destroy();
|
||||
|
@ -1284,16 +1292,40 @@ var PDFWorker = (function PDFWorkerClosure() {
|
|||
console.error.apply(console, data);
|
||||
});
|
||||
|
||||
var testObj = new Uint8Array([PDFJS.postMessageTransfers ? 255 : 0]);
|
||||
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
||||
// typed array. Also, checking if we can use transfers.
|
||||
try {
|
||||
messageHandler.send('test', testObj, [testObj.buffer]);
|
||||
} catch (ex) {
|
||||
info('Cannot use postMessage transfers');
|
||||
testObj[0] = 0;
|
||||
messageHandler.send('test', testObj);
|
||||
}
|
||||
messageHandler.on('ready', function (data) {
|
||||
if (this.destroyed) {
|
||||
this._readyCapability.reject(new Error('Worker was destroyed'));
|
||||
messageHandler.destroy();
|
||||
worker.terminate();
|
||||
return; // worker was destroyed
|
||||
}
|
||||
try {
|
||||
sendTest();
|
||||
} catch (e) {
|
||||
// We need fallback to a faked worker.
|
||||
this._setupFakeWorker();
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
var sendTest = function () {
|
||||
var testObj = new Uint8Array(
|
||||
[PDFJS.postMessageTransfers ? 255 : 0]);
|
||||
// Some versions of Opera throw a DATA_CLONE_ERR on serializing the
|
||||
// typed array. Also, checking if we can use transfers.
|
||||
try {
|
||||
messageHandler.send('test', testObj, [testObj.buffer]);
|
||||
} catch (ex) {
|
||||
info('Cannot use postMessage transfers');
|
||||
testObj[0] = 0;
|
||||
messageHandler.send('test', testObj);
|
||||
}
|
||||
};
|
||||
|
||||
// It might take time for worker to initialize (especially when AMD
|
||||
// loader is used). We will try to send test immediately, and then
|
||||
// when 'ready' message will arrive. The worker shall process only
|
||||
// first received 'test'.
|
||||
sendTest();
|
||||
return;
|
||||
} catch (e) {
|
||||
info('The worker has been disabled.');
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
'use strict';
|
||||
|
||||
//#if (GENERIC || SINGLE_FILE)
|
||||
(function (root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define('pdfjs/display/svg', ['exports', 'pdfjs/shared/util'], factory);
|
||||
|
@ -25,7 +26,6 @@
|
|||
factory((root.pdfjsDisplaySVG = {}), root.pdfjsSharedUtil);
|
||||
}
|
||||
}(this, function (exports, sharedUtil) {
|
||||
//#if (GENERIC || SINGLE_FILE)
|
||||
|
||||
var FONT_IDENTITY_MATRIX = sharedUtil.FONT_IDENTITY_MATRIX;
|
||||
var IDENTITY_MATRIX = sharedUtil.IDENTITY_MATRIX;
|
||||
|
@ -1209,5 +1209,5 @@ var SVGGraphics = (function SVGGraphicsClosure() {
|
|||
PDFJS.SVGGraphics = SVGGraphics;
|
||||
|
||||
exports.SVGGraphics = SVGGraphics;
|
||||
//#endif
|
||||
}));
|
||||
//#endif
|
||||
|
|
|
@ -12,22 +12,26 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/* globals PDFJS */
|
||||
|
||||
'use strict';
|
||||
|
||||
(function (root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define('pdfjs/display/text_layer', ['exports', 'pdfjs/shared/util'],
|
||||
factory);
|
||||
define('pdfjs/display/text_layer', ['exports', 'pdfjs/shared/util',
|
||||
'pdfjs/display/dom_utils', 'pdfjs/shared/global'], factory);
|
||||
} else if (typeof exports !== 'undefined') {
|
||||
factory(exports, require('../shared/util.js'));
|
||||
factory(exports, require('../shared/util.js'), require('./dom_utils.js'),
|
||||
require('../shared/global.js'));
|
||||
} else {
|
||||
factory((root.pdfjsDisplayTextLayer = {}), root.pdfjsSharedUtil);
|
||||
factory((root.pdfjsDisplayTextLayer = {}), root.pdfjsSharedUtil,
|
||||
root.pdfjsDisplayDOMUtils, root.pdfjsSharedGlobal);
|
||||
}
|
||||
}(this, function (exports, sharedUtil) {
|
||||
}(this, function (exports, sharedUtil, displayDOMUtils, sharedGlobal) {
|
||||
|
||||
var Util = sharedUtil.Util;
|
||||
var createPromiseCapability = sharedUtil.createPromiseCapability;
|
||||
var CustomStyle = displayDOMUtils.CustomStyle;
|
||||
var PDFJS = sharedGlobal.PDFJS;
|
||||
|
||||
/**
|
||||
* Text layer render parameters.
|
||||
|
@ -61,7 +65,7 @@ var renderTextLayer = (function renderTextLayerClosure() {
|
|||
textDiv.dataset.isWhitespace = true;
|
||||
return;
|
||||
}
|
||||
var tx = PDFJS.Util.transform(viewport.transform, geom.transform);
|
||||
var tx = Util.transform(viewport.transform, geom.transform);
|
||||
var angle = Math.atan2(tx[1], tx[0]);
|
||||
if (style.vertical) {
|
||||
angle += Math.PI / 2;
|
||||
|
@ -167,7 +171,7 @@ var renderTextLayer = (function renderTextLayerClosure() {
|
|||
transform = 'rotate(' + rotation + 'deg) ' + transform;
|
||||
}
|
||||
if (transform) {
|
||||
PDFJS.CustomStyle.setProp('transform' , textDiv, transform);
|
||||
CustomStyle.setProp('transform' , textDiv, transform);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
/* Copyright 2015 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
NOTE: This file is created as a helper to expose all loaded internal exported
|
||||
members to global scope.
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
(function (root) {
|
||||
for (var i in root) {
|
||||
if (/^pdfjs(Shared|Core|Display)/.test(i)) {
|
||||
var obj = root[i];
|
||||
for (var j in obj) {
|
||||
if (Object.getOwnPropertyDescriptor(root, j)) {
|
||||
continue; // ignoring if already set
|
||||
}
|
||||
root[j] = obj[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
})(window);
|
|
@ -12,70 +12,13 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/* globals PDFJS, Util */
|
||||
|
||||
'use strict';
|
||||
|
||||
// List of shared files to include;
|
||||
var sharedFiles = [
|
||||
'shared/global.js',
|
||||
'shared/util.js'
|
||||
];
|
||||
importScripts('../node_modules/requirejs/require.js');
|
||||
|
||||
// List of other files to include;
|
||||
var otherFiles = [
|
||||
'core/network.js',
|
||||
'core/arithmetic_decoder.js',
|
||||
'core/charsets.js',
|
||||
'core/glyphlist.js',
|
||||
'core/jpg.js',
|
||||
'core/metrics.js',
|
||||
'core/bidi.js',
|
||||
'core/chunked_stream.js',
|
||||
'core/jbig2.js',
|
||||
'core/jpx.js',
|
||||
'core/murmurhash3.js',
|
||||
'core/primitives.js',
|
||||
'core/stream.js',
|
||||
'core/crypto.js',
|
||||
'core/font_renderer.js',
|
||||
'core/parser.js',
|
||||
'core/cmap.js',
|
||||
'core/obj.js',
|
||||
'core/ps_parser.js',
|
||||
'core/fonts.js',
|
||||
'core/function.js',
|
||||
'core/colorspace.js',
|
||||
'core/image.js',
|
||||
'core/pattern.js',
|
||||
'core/evaluator.js',
|
||||
'core/annotation.js',
|
||||
'core/document.js',
|
||||
'core/pdf_manager.js',
|
||||
'core/worker.js'
|
||||
];
|
||||
|
||||
function loadInOrder(index, path, files) {
|
||||
if (index >= files.length) {
|
||||
PDFJS.fakeWorkerFilesLoadedCapability.resolve();
|
||||
return;
|
||||
}
|
||||
PDFJS.Util.loadScript(path + files[index],
|
||||
loadInOrder.bind(null, ++index, path, files));
|
||||
}
|
||||
|
||||
// Load all the files.
|
||||
if (typeof PDFJS === 'undefined' || !PDFJS.fakeWorkerFilesLoadedCapability) {
|
||||
var files = sharedFiles.concat(otherFiles);
|
||||
for (var i = 0; i < files.length; i++) {
|
||||
importScripts(files[i]);
|
||||
}
|
||||
} else {
|
||||
var src = PDFJS.workerSrc;
|
||||
var path = src.substr(0, src.indexOf('worker_loader.js'));
|
||||
// If Util is available, we assume that shared files are already loaded. Can
|
||||
// happen that they are not if PDF.js is bundled inside a special namespace.
|
||||
var skipShared = typeof Util !== 'undefined';
|
||||
var files = skipShared ? otherFiles : sharedFiles.concat(otherFiles);
|
||||
loadInOrder(0, path, files);
|
||||
}
|
||||
require.config({paths: {'pdfjs': '.'}});
|
||||
require(['pdfjs/core/network', 'pdfjs/core/worker'],
|
||||
function (network, worker) {
|
||||
// Worker is loaded at this point.
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue