]> git.parisson.com Git - pdf.js.git/commitdiff
Make worker support work again after file split. Add PDFJS_WORKER_DIR/PDFJS_WORKER_FI...
authorJulian Viereck <julian.viereck@gmail.com>
Fri, 28 Oct 2011 12:32:36 +0000 (14:32 +0200)
committerJulian Viereck <julian.viereck@gmail.com>
Fri, 28 Oct 2011 12:32:36 +0000 (14:32 +0200)
examples/helloworld/hello.js
examples/helloworld/index.html
src/core.js
src/pdf.js
src/worker.js
src/worker_loader.js
web/viewer-snippet.html
web/viewer.html

index 45e61eb6fad91721f305f47dd6efb16aba6143d6..c97b53c6664138a3567fe07c8bc0543271a53d17 100644 (file)
@@ -7,7 +7,7 @@
 
 'use strict';
 
-PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
+getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
   //
   // Instantiate PDFDoc with PDF data
   //
index b10e9eaeb82a185d2ef5b6ba2775cd1f690ca111..0fa711fe49278fad6b701c19d2cb405e26e3ccce 100644 (file)
@@ -3,27 +3,32 @@
 
 <head>
   <!-- In production, only one script (pdf.js) is necessary -->
-  <script type="text/javascript" src="../../src/core.js"></script> 
-  <script type="text/javascript" src="../../src/util.js"></script>  
-  <script type="text/javascript" src="../../src/canvas.js"></script>  
-  <script type="text/javascript" src="../../src/obj.js"></script>  
-  <script type="text/javascript" src="../../src/function.js"></script> 
-  <script type="text/javascript" src="../../src/charsets.js"></script>  
-  <script type="text/javascript" src="../../src/cidmaps.js"></script>  
-  <script type="text/javascript" src="../../src/colorspace.js"></script>  
-  <script type="text/javascript" src="../../src/crypto.js"></script>  
-  <script type="text/javascript" src="../../src/evaluator.js"></script>  
-  <script type="text/javascript" src="../../src/fonts.js"></script>  
-  <script type="text/javascript" src="../../src/glyphlist.js"></script>  
-  <script type="text/javascript" src="../../src/image.js"></script>  
-  <script type="text/javascript" src="../../src/metrics.js"></script>  
-  <script type="text/javascript" src="../../src/parser.js"></script>  
-  <script type="text/javascript" src="../../src/pattern.js"></script>  
-  <script type="text/javascript" src="../../src/stream.js"></script>  
-  <script type="text/javascript" src="../../src/worker.js"></script>  
+  <script type="text/javascript" src="../../src/core.js"></script>
+  <script type="text/javascript" src="../../src/util.js"></script>
+  <script type="text/javascript" src="../../src/canvas.js"></script>
+  <script type="text/javascript" src="../../src/obj.js"></script>
+  <script type="text/javascript" src="../../src/function.js"></script>
+  <script type="text/javascript" src="../../src/charsets.js"></script>
+  <script type="text/javascript" src="../../src/cidmaps.js"></script>
+  <script type="text/javascript" src="../../src/colorspace.js"></script>
+  <script type="text/javascript" src="../../src/crypto.js"></script>
+  <script type="text/javascript" src="../../src/evaluator.js"></script>
+  <script type="text/javascript" src="../../src/fonts.js"></script>
+  <script type="text/javascript" src="../../src/glyphlist.js"></script>
+  <script type="text/javascript" src="../../src/image.js"></script>
+  <script type="text/javascript" src="../../src/metrics.js"></script>
+  <script type="text/javascript" src="../../src/parser.js"></script>
+  <script type="text/javascript" src="../../src/pattern.js"></script>
+  <script type="text/javascript" src="../../src/stream.js"></script>
+  <script type="text/javascript" src="../../src/worker.js"></script>
 
+  <script type="text/javascript">
+    // Specify the directory of the source files, such that the web worker
+    // knows where to load them.
+    var PDFJS_WORKER_DIR = '../../src/';
+  </script>
   <script type="text/javascript" src="hello.js"></script>
-</head>  
+</head>
 
 <body>
   <canvas id="the-canvas" style="border:1px solid black;"/>
index e7241acfa21b5b8e75087fdddbca68dadad24f71..8d3d6d434cf720bf9baab4058f068dbeb8404c3a 100644 (file)
@@ -471,7 +471,26 @@ var PDFDoc = (function() {
     this.pageCache = [];
 
     if (useWorker) {
-      var worker = new Worker('../src/worker_loader.js');
+      var worker;
+      if (typeof PDFJS_WORKER_DIR !== 'undefined') {
+        // If `PDFJS_WORKER_DIR` is specified, we assume the pdf.js files
+        // located all in that directory. Create a new worker and tell him
+        // the directory, such that he can load the scripts from there.
+        worker = new Worker(PDFJS_WORKER_DIR + 'worker_loader.js');
+        console.log('main: post dir');
+
+        worker.postMessage(PDFJS_WORKER_DIR);
+      } else if (typeof PDFJS_WORKER_FILE !== 'undefined') {
+        // If we build the worker using a worker file, then we assume, that
+        // everything the worker needs is already included in that file.
+        // Therefore the worker doesn't have to call `importScripts` to load
+        // all the single files and therefore it's not necessary to tell the
+        // worker a directory to laod the js files from.
+        // (Which is different from the PDFJS_WORKER_DIR case above.)
+        worker = new Worker(PDFJS_WORKER_FILE);
+      } else {
+        throw 'No worker file or directory specified.';
+      }
     } else {
       // If we don't use a worker, just post/sendMessage to the main thread.
       var worker = {
index 34e163967c5278cb6ca0e531e27618ef2e9618dc..2ecd2fddcf8ab5e9268a8e0fe39075ad55dcd5c1 100644 (file)
@@ -4,7 +4,6 @@
 var PDFJS = {};
 
 (function pdfjsWrapper() {
-
   // Use strict in our context only - users might not want it
   'use strict';
 
index a83f316681ce4bb6c1efe24682c810421da21c3c..5545fc4590585d672e19b911da05a73500153703 100644 (file)
@@ -174,10 +174,9 @@ var workerConsole = {
 
 // Worker thread?
 if (typeof window === 'undefined') {
-  globalScope.console = workerConsole;
+  this.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);
 }
 
index fb37ca9c4c10ffd894f4857bf7127b702f957b0a..69f7d55baa6cbe448c10d08144d0a96e164fed7c 100644 (file)
@@ -3,22 +3,40 @@
 
 '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');
+this.onmessage = function(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.
+  delete this.onmessage;
 
+  // Directory the include files are contained is send as the
+  // first message to the worker.
+  var dir = evt.data;
+
+  // 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]);
+  }
+}.bind(this);
index c99897a44b6d22e8de79cfb7469251c6a9174f0c..4d2a4f7b76cef0549d364d06c47f4c891158b7e1 100644 (file)
@@ -1,2 +1,7 @@
 <!-- This snippet is used in production, see Makefile -->
 <script type="text/javascript" src="../build/pdf.js"></script>
+<script type="text/javascript">
+  // This specifies the location of the pdf.js file. This is necessary to
+  // spawn the web worker.
+  var PDFJS_WORKER_FILE = "../build/pdf.js";
+</script>
index f7a5378ed39a7e0b334e1447fce6253587ff8586..4505331d6723aa96389f69a3a29c6e1d348881fe 100644 (file)
@@ -3,9 +3,9 @@
     <head>
         <title>Simple pdf.js page viewer</title>
         <link rel="stylesheet" href="viewer.css"/>
-        
+
         <!-- PDFJSSCRIPT_INCLUDE_BUILD -->
-        
+        <script type="text/javascript">var PDFJS_WORKER_DIR = "../src/"</script> <!-- PDFJSSCRIPT_REMOVE -->
         <script type="text/javascript" src="../src/core.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
         <script type="text/javascript" src="../src/util.js"></script>  <!-- PDFJSSCRIPT_REMOVE -->
         <script type="text/javascript" src="../src/canvas.js"></script>  <!-- PDFJSSCRIPT_REMOVE -->
         </div>
      </div>
     </div>
-    
+
     <div id="loading">Loading... 0%</div>
     <div id="viewer"></div>
   </body>