diff --git a/examples/helloworld/index.html b/examples/helloworld/index.html index 1f8de7637..2aa30b426 100644 --- a/examples/helloworld/index.html +++ b/examples/helloworld/index.html @@ -7,7 +7,6 @@ - diff --git a/pdf.js b/pdf.js index 4448cb8a9..4161ec493 100644 --- a/pdf.js +++ b/pdf.js @@ -5,6 +5,8 @@ var ERRORS = 0, WARNINGS = 1, TODOS = 5; var verbosity = WARNINGS; +// Set this to true if you want to use workers. +var useWorker = false; function log(msg) { if (console && console.log) @@ -7409,3 +7411,208 @@ var PDFFunction = (function() { }; })(); +/** + * A PDF document and page is build up of many objects. E.g. there are objects + * for fonts, images, rendering code and such. These objects might get processed + * inside of a worker. The `PDFObjects` implements some basic functions to + * manage these objects. + */ +var PDFObjects = (function() { + function PDFObjects() { + this.objs = {}; + } + + PDFObjects.prototype = { + objs: null, + + /** + * Internal function. + * Ensures there is an object defined for `objId`. Stores `data` on the + * object *if* it is created. + */ + ensureObj: function(objId, data) { + if (!this.objs[objId]) { + return this.objs[objId] = new Promise(objId, data); + } else { + return this.objs[objId]; + } + }, + + /** + * If called *without* callback, this returns the data of `objId` but the + * object needs to be resolved. If it isn't, this function throws. + * + * If called *with* a callback, the callback is called with the data of the + * object once the object is resolved. That means, if you call this + * function and the object is already resolved, the callback gets called + * right away. + */ + get: function(objId, callback) { + // If there is a callback, then the get can be async and the object is + // not required to be resolved right now + if (callback) { + this.ensureObj(objId).then(callback); + } + // If there isn't a callback, the user expects to get the resolved data + // directly. + else { + var obj = this.objs[objId]; + + // If there isn't an object yet or the object isn't resolved, then the + // data isn't ready yet! + if (!obj || !obj.isResolved) { + debugger; + throw 'Requesting object that isn\'t resolved yet ' + objId; + } + // Direct access. + else { + return obj.data; + } + } + }, + + /** + * Resolves the object `objId` with optional `data`. + */ + resolve: function(objId, data) { + var objs = this.objs; + + // In case there is a promise already on this object, just resolve it. + if (objs[objId]) { + objs[objId].resolve(data); + } else { + this.ensureObj(objId, data); + } + }, + + onData: function(objId, callback) { + this.ensureObj(objId).onData(callback); + }, + + isResolved: function(objId) { + var objs = this.objs; + if (!objs[objId]) { + return false; + } else { + return objs[objId].isResolved; + } + }, + + hasData: function(objId) { + var objs = this.objs; + if (!objs[objId]) { + return false; + } else { + return objs[objId].hasData; + } + }, + + /** + * Sets the data of an object but *doesn't* resolve it. + */ + setData: function(objId, data) { + // Watchout! If you call `this.ensureObj(objId, data)` you'll gonna create + // a *resolved* promise which shouldn't be the case! + this.ensureObj(objId).data = data; + } + }; + return PDFObjects; +})(); + +/** + * 'Promise' object. + * Each object that is stored in PDFObjects is based on a Promise object that + * contains the status of the object and the data. There migth be situations, + * where a function want to use the value of an object, but it isn't ready at + * that time. To get a notification, once the object is ready to be used, s.o. + * can add a callback using the `then` method on the promise that then calls + * the callback once the object gets resolved. + * A promise can get resolved only once and only once the data of the promise + * can be set. If any of these happens twice or the data is required before + * it was set, an exception is throw. + */ +var Promise = (function() { + var EMPTY_PROMISE = {}; + + /** + * If `data` is passed in this constructor, the promise is created resolved. + * If there isn't data, it isn't resolved at the beginning. + */ + function Promise(name, data) { + this.name = name; + // If you build a promise and pass in some data it's already resolved. + if (data != null) { + this.isResolved = true; + this._data = data; + this.hasData = true; + } else { + this.isResolved = false; + this._data = EMPTY_PROMISE; + } + this.callbacks = []; + }; + + Promise.prototype = { + hasData: false, + + set data(data) { + if (data === undefined) { + return; + } + if (this._data !== EMPTY_PROMISE) { + throw 'Promise ' + this.name + + ': Cannot set the data of a promise twice'; + } + this._data = data; + this.hasData = true; + + if (this.onDataCallback) { + this.onDataCallback(data); + } + }, + + get data() { + if (this._data === EMPTY_PROMISE) { + throw 'Promise ' + this.name + ': Cannot get data that isn\'t set'; + } + return this._data; + }, + + onData: function(callback) { + if (this._data !== EMPTY_PROMISE) { + callback(this._data); + } else { + this.onDataCallback = callback; + } + }, + + resolve: function(data) { + if (this.isResolved) { + throw 'A Promise can be resolved only once ' + this.name; + } + + this.isResolved = true; + this.data = data; + var callbacks = this.callbacks; + + for (var i = 0; i < callbacks.length; i++) { + callbacks[i].call(null, data); + } + }, + + then: function(callback) { + if (!callback) { + throw 'Requiring callback' + this.name; + } + + // If the promise is already resolved, call the callback directly. + if (this.isResolved) { + var data = this.data; + callback.call(null, data); + } else { + this.callbacks.push(callback); + } + } + }; + return Promise; +})(); diff --git a/test/test_slave.html b/test/test_slave.html index fd6428d2f..124537a88 100644 --- a/test/test_slave.html +++ b/test/test_slave.html @@ -11,7 +11,6 @@ - diff --git a/web/viewer.html b/web/viewer.html index e79539359..f1f5efc78 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -13,7 +13,6 @@ - diff --git a/worker.js b/worker.js deleted file mode 100644 index 5ced1169d..000000000 --- a/worker.js +++ /dev/null @@ -1,215 +0,0 @@ -/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */ - -'use strict'; - -// Set this to true if you want to use workers. -var useWorker = true; - -/** - * A PDF document and page is build up of many objects. E.g. there are objects - * for fonts, images, rendering code and such. These objects might get processed - * inside of a worker. The `PDFObjects` implements some basic functions to - * manage these objects. - */ -var PDFObjects = (function() { - function PDFObjects() { - this.objs = {}; - } - - PDFObjects.prototype = { - objs: null, - - /** - * Internal function. - * Ensures there is an object defined for `objId`. Stores `data` on the - * object *if* it is created. - */ - ensureObj: function(objId, data) { - if (!this.objs[objId]) { - return this.objs[objId] = new Promise(objId, data); - } else { - return this.objs[objId]; - } - }, - - /** - * If called *without* callback, this returns the data of `objId` but the - * object needs to be resolved. If it isn't, this function throws. - * - * If called *with* a callback, the callback is called with the data of the - * object once the object is resolved. That means, if you call this - * function and the object is already resolved, the callback gets called - * right away. - */ - get: function(objId, callback) { - // If there is a callback, then the get can be async and the object is - // not required to be resolved right now - if (callback) { - this.ensureObj(objId).then(callback); - } - // If there isn't a callback, the user expects to get the resolved data - // directly. - else { - var obj = this.objs[objId]; - - // If there isn't an object yet or the object isn't resolved, then the - // data isn't ready yet! - if (!obj || !obj.isResolved) { - throw 'Requesting object that isn\'t resolved yet ' + objId; - } - // Direct access. - else { - return obj.data; - } - } - }, - - /** - * Resolves the object `objId` with optional `data`. - */ - resolve: function(objId, data) { - var objs = this.objs; - - // In case there is a promise already on this object, just resolve it. - if (objs[objId]) { - objs[objId].resolve(data); - } else { - this.ensureObj(objId, data); - } - }, - - onData: function(objId, callback) { - this.ensureObj(objId).onData(callback); - }, - - isResolved: function(objId) { - var objs = this.objs; - if (!objs[objId]) { - return false; - } else { - return objs[objId].isResolved; - } - }, - - hasData: function(objId) { - var objs = this.objs; - if (!objs[objId]) { - return false; - } else { - return objs[objId].hasData; - } - }, - - /** - * Sets the data of an object but *doesn't* resolve it. - */ - setData: function(objId, data) { - // Watchout! If you call `this.ensureObj(objId, data)` you'll gonna create - // a *resolved* promise which shouldn't be the case! - this.ensureObj(objId).data = data; - } - }; - return PDFObjects; -})(); - - -/** - * 'Promise' object. - * Each object that is stored in PDFObjects is based on a Promise object that - * contains the status of the object and the data. There migth be situations, - * where a function want to use the value of an object, but it isn't ready at - * that time. To get a notification, once the object is ready to be used, s.o. - * can add a callback using the `then` method on the promise that then calls - * the callback once the object gets resolved. - * A promise can get resolved only once and only once the data of the promise - * can be set. If any of these happens twice or the data is required before - * it was set, an exception is throw. - */ -var Promise = (function() { - var EMPTY_PROMISE = {}; - - /** - * If `data` is passed in this constructor, the promise is created resolved. - * If there isn't data, it isn't resolved at the beginning. - */ - function Promise(name, data) { - this.name = name; - // If you build a promise and pass in some data it's already resolved. - if (data != null) { - this.isResolved = true; - this._data = data; - this.hasData = true; - } else { - this.isResolved = false; - this._data = EMPTY_PROMISE; - } - this.callbacks = []; - }; - - Promise.prototype = { - hasData: false, - - set data(data) { - if (data === undefined) { - return; - } - if (this._data !== EMPTY_PROMISE) { - throw 'Promise ' + this.name + - ': Cannot set the data of a promise twice'; - } - this._data = data; - this.hasData = true; - - if (this.onDataCallback) { - this.onDataCallback(data); - } - }, - - get data() { - if (this._data === EMPTY_PROMISE) { - throw 'Promise ' + this.name + ': Cannot get data that isn\'t set'; - } - return this._data; - }, - - onData: function(callback) { - if (this._data !== EMPTY_PROMISE) { - callback(this._data); - } else { - this.onDataCallback = callback; - } - }, - - resolve: function(data) { - if (this.isResolved) { - throw 'A Promise can be resolved only once ' + this.name; - } - - this.isResolved = true; - this.data = data; - var callbacks = this.callbacks; - - for (var i = 0; i < callbacks.length; i++) { - callbacks[i].call(null, data); - } - }, - - then: function(callback) { - if (!callback) { - throw 'Requiring callback' + this.name; - } - - // If the promise is already resolved, call the callback directly. - if (this.isResolved) { - var data = this.data; - callback.call(null, data); - } else { - this.callbacks.push(callback); - } - } - }; - return Promise; -})(); - -