2011-09-06 08:07:03 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2011-09-17 01:04:12 +09:00
|
|
|
// Set this to true if you want to use workers.
|
2011-10-20 04:42:33 +09:00
|
|
|
var useWorker = true;
|
2011-09-06 08:07:03 +09:00
|
|
|
|
2011-10-09 00:08:17 +09:00
|
|
|
/**
|
|
|
|
* 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
|
2011-10-09 17:37:53 +09:00
|
|
|
* inside of a worker. The `PDFObjects` implements some basic functions to
|
|
|
|
* manage these objects.
|
2011-10-09 00:08:17 +09:00
|
|
|
*/
|
2011-10-03 04:48:40 +09:00
|
|
|
var PDFObjects = (function() {
|
|
|
|
function PDFObjects() {
|
|
|
|
this.objs = {};
|
|
|
|
}
|
2011-09-10 08:15:51 +09:00
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
PDFObjects.prototype = {
|
|
|
|
objs: null,
|
|
|
|
|
|
|
|
/**
|
2011-10-09 00:08:17 +09:00
|
|
|
* Internal function.
|
2011-10-03 04:48:40 +09:00
|
|
|
* 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
|
2011-10-09 17:37:53 +09:00
|
|
|
* object once the object is resolved. That means, if you call this
|
2011-10-03 04:48:40 +09:00
|
|
|
* function and the object is already resolved, the callback gets called
|
|
|
|
* right away.
|
|
|
|
*/
|
|
|
|
get: function(objId, callback) {
|
2011-10-09 17:37:53 +09:00
|
|
|
// If there is a callback, then the get can be async and the object is
|
2011-10-03 04:48:40 +09:00
|
|
|
// not required to be resolved right now
|
|
|
|
if (callback) {
|
|
|
|
this.ensureObj(objId).then(callback);
|
2011-10-09 17:37:53 +09:00
|
|
|
}
|
2011-10-03 04:48:40 +09:00
|
|
|
// 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) {
|
2011-10-09 17:37:53 +09:00
|
|
|
throw 'Requesting object that isn\'t resolved yet ' + objId;
|
|
|
|
}
|
2011-10-03 04:48:40 +09:00
|
|
|
// Direct access.
|
|
|
|
else {
|
|
|
|
return obj.data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolves the object `objId` with optional `data`.
|
|
|
|
*/
|
|
|
|
resolve: function(objId, data) {
|
|
|
|
var objs = this.objs;
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
// 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) {
|
2011-10-14 03:30:55 +09:00
|
|
|
// Watchout! If you call `this.ensureObj(objId, data)` you'll gonna create
|
2011-10-03 04:48:40 +09:00
|
|
|
// a *resolved* promise which shouldn't be the case!
|
|
|
|
this.ensureObj(objId).data = data;
|
2011-09-10 08:15:51 +09:00
|
|
|
}
|
2011-10-09 17:37:53 +09:00
|
|
|
};
|
2011-10-03 04:48:40 +09:00
|
|
|
return PDFObjects;
|
|
|
|
})();
|
|
|
|
|
2011-09-08 06:45:38 +09:00
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
/**
|
2011-10-09 17:37:53 +09:00
|
|
|
* 'Promise' object.
|
2011-10-09 00:08:17 +09:00
|
|
|
* 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.
|
2011-10-03 04:48:40 +09:00
|
|
|
*/
|
2011-09-08 09:15:35 +09:00
|
|
|
var Promise = (function() {
|
2011-09-13 23:49:35 +09:00
|
|
|
var EMPTY_PROMISE = {};
|
|
|
|
|
2011-10-03 04:48:40 +09:00
|
|
|
/**
|
|
|
|
* If `data` is passed in this constructor, the promise is created resolved.
|
|
|
|
* If there isn't data, it isn't resolved at the beginning.
|
|
|
|
*/
|
2011-09-10 08:15:51 +09:00
|
|
|
function Promise(name, data) {
|
|
|
|
this.name = name;
|
2011-09-08 11:50:05 +09:00
|
|
|
// If you build a promise and pass in some data it's already resolved.
|
|
|
|
if (data != null) {
|
|
|
|
this.isResolved = true;
|
2011-10-02 00:16:11 +09:00
|
|
|
this._data = data;
|
2011-10-01 18:44:01 +09:00
|
|
|
this.hasData = true;
|
2011-09-08 11:50:05 +09:00
|
|
|
} else {
|
2011-10-09 17:37:53 +09:00
|
|
|
this.isResolved = false;
|
2011-10-02 00:16:11 +09:00
|
|
|
this._data = EMPTY_PROMISE;
|
2011-09-08 11:50:05 +09:00
|
|
|
}
|
|
|
|
this.callbacks = [];
|
2011-09-08 09:15:35 +09:00
|
|
|
};
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
Promise.prototype = {
|
2011-09-30 22:38:54 +09:00
|
|
|
hasData: false,
|
|
|
|
|
2011-09-13 23:49:35 +09:00
|
|
|
set data(data) {
|
|
|
|
if (data === undefined) {
|
|
|
|
return;
|
|
|
|
}
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this._data !== EMPTY_PROMISE) {
|
2011-10-09 17:37:53 +09:00
|
|
|
throw 'Promise ' + this.name +
|
|
|
|
': Cannot set the data of a promise twice';
|
2011-09-13 23:49:35 +09:00
|
|
|
}
|
2011-10-02 00:16:11 +09:00
|
|
|
this._data = data;
|
2011-09-30 22:38:54 +09:00
|
|
|
this.hasData = true;
|
|
|
|
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this.onDataCallback) {
|
|
|
|
this.onDataCallback(data);
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
2011-09-13 23:49:35 +09:00
|
|
|
},
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-09-13 23:49:35 +09:00
|
|
|
get data() {
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this._data === EMPTY_PROMISE) {
|
2011-10-09 17:37:53 +09:00
|
|
|
throw 'Promise ' + this.name + ': Cannot get data that isn\'t set';
|
2011-09-13 23:49:35 +09:00
|
|
|
}
|
2011-10-02 00:16:11 +09:00
|
|
|
return this._data;
|
2011-09-13 23:49:35 +09:00
|
|
|
},
|
2011-09-30 22:38:54 +09:00
|
|
|
|
|
|
|
onData: function(callback) {
|
2011-10-02 00:16:11 +09:00
|
|
|
if (this._data !== EMPTY_PROMISE) {
|
|
|
|
callback(this._data);
|
2011-09-30 22:38:54 +09:00
|
|
|
} else {
|
2011-10-02 00:16:11 +09:00
|
|
|
this.onDataCallback = callback;
|
2011-09-30 22:38:54 +09:00
|
|
|
}
|
|
|
|
},
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
resolve: function(data) {
|
|
|
|
if (this.isResolved) {
|
2011-10-09 17:37:53 +09:00
|
|
|
throw 'A Promise can be resolved only once ' + this.name;
|
2011-09-08 09:15:35 +09:00
|
|
|
}
|
2011-10-03 04:48:40 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
this.isResolved = true;
|
|
|
|
this.data = data;
|
|
|
|
var callbacks = this.callbacks;
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
for (var i = 0; i < callbacks.length; i++) {
|
|
|
|
callbacks[i].call(null, data);
|
|
|
|
}
|
|
|
|
},
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
then: function(callback) {
|
2011-09-16 16:11:23 +09:00
|
|
|
if (!callback) {
|
2011-10-09 17:37:53 +09:00
|
|
|
throw 'Requiring callback' + this.name;
|
2011-09-16 16:11:23 +09:00
|
|
|
}
|
2011-10-09 17:37:53 +09:00
|
|
|
|
2011-09-08 09:15:35 +09:00
|
|
|
// If the promise is already resolved, call the callback directly.
|
|
|
|
if (this.isResolved) {
|
2011-09-13 23:49:35 +09:00
|
|
|
var data = this.data;
|
|
|
|
callback.call(null, data);
|
2011-09-08 09:15:35 +09:00
|
|
|
} else {
|
2011-10-09 17:37:53 +09:00
|
|
|
this.callbacks.push(callback);
|
2011-09-08 09:15:35 +09:00
|
|
|
}
|
|
|
|
}
|
2011-10-09 17:37:53 +09:00
|
|
|
};
|
2011-09-08 11:50:05 +09:00
|
|
|
return Promise;
|
2011-09-08 09:15:35 +09:00
|
|
|
})();
|
|
|
|
|
2011-09-06 08:07:03 +09:00
|
|
|
|