Make worker support work again after file split. Add PDFJS_WORKER_DIR/PDFJS_WORKER_FILE to specify where to load files if worker support is enabled

This commit is contained in:
Julian Viereck 2011-10-28 14:32:36 +02:00
parent 21a6467b23
commit 1e6d1f9922
8 changed files with 91 additions and 46 deletions

View File

@ -7,7 +7,7 @@
'use strict'; 'use strict';
PDFJS.getPdf('helloworld.pdf', function getPdfHelloWorld(data) { getPdf('helloworld.pdf', function getPdfHelloWorld(data) {
// //
// Instantiate PDFDoc with PDF data // Instantiate PDFDoc with PDF data
// //

View File

@ -3,27 +3,32 @@
<head> <head>
<!-- In production, only one script (pdf.js) is necessary --> <!-- In production, only one script (pdf.js) is necessary -->
<script type="text/javascript" src="../../src/core.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/util.js"></script>
<script type="text/javascript" src="../../src/canvas.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/obj.js"></script>
<script type="text/javascript" src="../../src/function.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/charsets.js"></script>
<script type="text/javascript" src="../../src/cidmaps.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/colorspace.js"></script>
<script type="text/javascript" src="../../src/crypto.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/evaluator.js"></script>
<script type="text/javascript" src="../../src/fonts.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/glyphlist.js"></script>
<script type="text/javascript" src="../../src/image.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/metrics.js"></script>
<script type="text/javascript" src="../../src/parser.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/pattern.js"></script>
<script type="text/javascript" src="../../src/stream.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/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> <script type="text/javascript" src="hello.js"></script>
</head> </head>
<body> <body>
<canvas id="the-canvas" style="border:1px solid black;"/> <canvas id="the-canvas" style="border:1px solid black;"/>

View File

@ -471,7 +471,26 @@ var PDFDoc = (function() {
this.pageCache = []; this.pageCache = [];
if (useWorker) { 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 { } else {
// If we don't use a worker, just post/sendMessage to the main thread. // If we don't use a worker, just post/sendMessage to the main thread.
var worker = { var worker = {

View File

@ -4,7 +4,6 @@
var PDFJS = {}; var PDFJS = {};
(function pdfjsWrapper() { (function pdfjsWrapper() {
// Use strict in our context only - users might not want it // Use strict in our context only - users might not want it
'use strict'; 'use strict';

View File

@ -174,10 +174,9 @@ var workerConsole = {
// Worker thread? // Worker thread?
if (typeof window === 'undefined') { if (typeof window === 'undefined') {
globalScope.console = workerConsole; this.console = workerConsole;
// Listen for messages from the main thread. var handler = new MessageHandler('worker_processor', this);
var handler = new MessageHandler('worker_processor', globalScope);
WorkerProcessorHandler.setup(handler); WorkerProcessorHandler.setup(handler);
} }

View File

@ -3,22 +3,40 @@
'use strict'; 'use strict';
importScripts('../src/core.js'); this.onmessage = function(evt) {
importScripts('../src/util.js'); // Reset the `onmessage` function as it was only set to call
importScripts('../src/canvas.js'); // this function the first time a message is passed to the worker
importScripts('../src/obj.js'); // but shouldn't get called anytime afterwards.
importScripts('../src/function.js'); delete this.onmessage;
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');
// 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);

View File

@ -1,2 +1,7 @@
<!-- This snippet is used in production, see Makefile --> <!-- This snippet is used in production, see Makefile -->
<script type="text/javascript" src="../build/pdf.js"></script> <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>

View File

@ -3,9 +3,9 @@
<head> <head>
<title>Simple pdf.js page viewer</title> <title>Simple pdf.js page viewer</title>
<link rel="stylesheet" href="viewer.css"/> <link rel="stylesheet" href="viewer.css"/>
<!-- PDFJSSCRIPT_INCLUDE_BUILD --> <!-- 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/core.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
<script type="text/javascript" src="../src/util.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 --> <script type="text/javascript" src="../src/canvas.js"></script> <!-- PDFJSSCRIPT_REMOVE -->
@ -114,7 +114,7 @@
</div> </div>
</div> </div>
</div> </div>
<div id="loading">Loading... 0%</div> <div id="loading">Loading... 0%</div>
<div id="viewer"></div> <div id="viewer"></div>
</body> </body>