diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html index b10e9eaeb..a48e3705b 100644 --- a/examples/helloworld/index.html +++ b/examples/helloworld/index.html @@ -3,27 +3,33 @@ - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + - + diff --git a/src/core.js b/src/core.js index 09d665972..2bc4c50aa 100644 --- a/src/core.js +++ b/src/core.js @@ -472,7 +472,18 @@ var PDFDoc = (function pdfDoc() { this.pageCache = []; if (useWorker) { - var worker = new Worker('../src/worker_loader.js'); + var workerSrc = PDFJS.workerSrc; + if (typeof workerSrc === 'undefined') { + throw 'No PDFJS.workerSrc specified'; + } + + var worker = new Worker(workerSrc); + + // Tell the worker the file it was created from. + worker.postMessage({ + action: 'workerSrc', + data: workerSrc + }); } else { // If we don't use a worker, just post/sendMessage to the main thread. var worker = { diff --git a/src/pdf.js b/src/pdf.js index 34e163967..51f606548 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -4,12 +4,11 @@ var PDFJS = {}; (function pdfjsWrapper() { - // Use strict in our context only - users might not want it 'use strict'; // Files are inserted below - see Makefile /* PDFJSSCRIPT_INCLUDE_ALL */ -})(); +}).call((typeof window === 'undefined') ? this : window); diff --git a/src/worker.js b/src/worker.js index d62e0c86b..74a880cae 100644 --- a/src/worker.js +++ b/src/worker.js @@ -47,6 +47,13 @@ var WorkerProcessorHandler = { setup: function wphSetup(handler) { var pdfDoc = null; + handler.on('workerSrc', function wphSetupWorkerSrc(data) { + // In development, the `workerSrc` message is handled in the + // `worker_loader.js` file. In production the workerProcessHandler is + // called for this. This servers as a dummy to prevent calling an + // undefined action `workerSrc`. + }); + handler.on('doc', function wphSetupDoc(data) { // Create only the model of the PDFDoc, which is enough for // processing the content of the pdf. @@ -176,8 +183,7 @@ var workerConsole = { if (typeof window === 'undefined') { globalScope.console = workerConsole; - // Listen for messages from the main thread. - var handler = new MessageHandler('worker_processor', globalScope); + var handler = new MessageHandler('worker_processor', this); WorkerProcessorHandler.setup(handler); } diff --git a/src/worker_loader.js b/src/worker_loader.js index fb37ca9c4..8d342d116 100644 --- a/src/worker_loader.js +++ b/src/worker_loader.js @@ -3,22 +3,50 @@ 'use strict'; -importScripts('../src/core.js'); -importScripts('../src/util.js'); -importScripts('../src/canvas.js'); -importScripts('../src/obj.js'); -importScripts('../src/function.js'); -importScripts('../src/charsets.js'); -importScripts('../src/cidmaps.js'); -importScripts('../src/colorspace.js'); -importScripts('../src/crypto.js'); -importScripts('../src/evaluator.js'); -importScripts('../src/fonts.js'); -importScripts('../src/glyphlist.js'); -importScripts('../src/image.js'); -importScripts('../src/metrics.js'); -importScripts('../src/parser.js'); -importScripts('../src/pattern.js'); -importScripts('../src/stream.js'); -importScripts('../src/worker.js'); +function onMessageLoader(evt) { + // Reset the `onmessage` function as it was only set to call + // this function the first time a message is passed to the worker + // but shouldn't get called anytime afterwards. + this.onmessage = null; + if (evt.data.action !== 'workerSrc') { + throw 'Worker expects first message to be `workerSrc`'; + } + + // Content of `PDFJS.workerSrc` as defined on the main thread. + var workerSrc = evt.data.data; + + // Extract the directory that contains the source files to load. + // Assuming the source files have the same relative possition as the + // `workerSrc` file. + var dir = workerSrc.substring(0, workerSrc.lastIndexOf('/') + 1); + + // List of files to include; + var files = [ + 'core.js', + 'util.js', + 'canvas.js', + 'obj.js', + 'function.js', + 'charsets.js', + 'cidmaps.js', + 'colorspace.js', + 'crypto.js', + 'evaluator.js', + 'fonts.js', + 'glyphlist.js', + 'image.js', + 'metrics.js', + 'parser.js', + 'pattern.js', + 'stream.js', + 'worker.js' + ]; + + // Load all the files. + for (var i = 0; i < files.length; i++) { + importScripts(dir + files[i]); + } +} + +this.onmessage = onMessageLoader.bind(this); diff --git a/test/test_slave.html b/test/test_slave.html index 7ac886769..91852d5a5 100644 --- a/test/test_slave.html +++ b/test/test_slave.html @@ -3,25 +3,29 @@ pdf.js test slave - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + diff --git a/web/viewer-snippet.html b/web/viewer-snippet.html index c99897a44..6b0c8821b 100644 --- a/web/viewer-snippet.html +++ b/web/viewer-snippet.html @@ -1,2 +1,6 @@ + diff --git a/web/viewer.html b/web/viewer.html index 0c6ab385e..3883804f0 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -3,10 +3,10 @@ Simple pdf.js page viewer + - + - @@ -26,6 +26,8 @@ + + @@ -114,7 +116,7 @@ - +
Loading... 0%