mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-25 09:38:06 +02:00
Adds UMD headers to core, display and shared files.
This commit is contained in:
parent
1b5940edd2
commit
6b60c8f4db
54 changed files with 1876 additions and 572 deletions
367
src/core/obj.js
367
src/core/obj.js
|
@ -12,326 +12,56 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
/* globals assert, bytesToString, CipherTransformFactory, error, info,
|
||||
InvalidPDFException, isArray, isCmd, isDict, isInt, isName, isRef,
|
||||
isStream, Lexer, Page, Parser, Promise, shadow,
|
||||
stringToPDFString, stringToUTF8String, warn, isString,
|
||||
Promise, MissingDataException, XRefParseException, Stream,
|
||||
ChunkedStream, createPromiseCapability */
|
||||
|
||||
'use strict';
|
||||
|
||||
var Name = (function NameClosure() {
|
||||
function Name(name) {
|
||||
this.name = name;
|
||||
(function (root, factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define('pdfjs/core/obj', ['exports', 'pdfjs/shared/util',
|
||||
'pdfjs/core/primitives', 'pdfjs/core/crypto', 'pdfjs/core/parser',
|
||||
'pdfjs/core/chunked_stream'], factory);
|
||||
} else if (typeof exports !== 'undefined') {
|
||||
factory(exports, require('../shared/util.js'), require('./primitives.js'),
|
||||
require('./crypto.js'), require('./parser.js'),
|
||||
require('./chunked_stream.js'));
|
||||
} else {
|
||||
factory((root.pdfjsCoreObj = {}), root.pdfjsSharedUtil,
|
||||
root.pdfjsCorePrimitives, root.pdfjsCoreCrypto, root.pdfjsCoreParser,
|
||||
root.pdfjsCoreChunkedStream);
|
||||
}
|
||||
}(this, function (exports, sharedUtil, corePrimitives, coreCrypto, coreParser,
|
||||
coreChunkedStream) {
|
||||
|
||||
Name.prototype = {};
|
||||
|
||||
var nameCache = {};
|
||||
|
||||
Name.get = function Name_get(name) {
|
||||
var nameValue = nameCache[name];
|
||||
return (nameValue ? nameValue : (nameCache[name] = new Name(name)));
|
||||
};
|
||||
|
||||
return Name;
|
||||
})();
|
||||
|
||||
var Cmd = (function CmdClosure() {
|
||||
function Cmd(cmd) {
|
||||
this.cmd = cmd;
|
||||
}
|
||||
|
||||
Cmd.prototype = {};
|
||||
|
||||
var cmdCache = {};
|
||||
|
||||
Cmd.get = function Cmd_get(cmd) {
|
||||
var cmdValue = cmdCache[cmd];
|
||||
return (cmdValue ? cmdValue : (cmdCache[cmd] = new Cmd(cmd)));
|
||||
};
|
||||
|
||||
return Cmd;
|
||||
})();
|
||||
|
||||
var Dict = (function DictClosure() {
|
||||
var nonSerializable = function nonSerializableClosure() {
|
||||
return nonSerializable; // creating closure on some variable
|
||||
};
|
||||
|
||||
var GETALL_DICTIONARY_TYPES_WHITELIST = {
|
||||
'Background': true,
|
||||
'ExtGState': true,
|
||||
'Halftone': true,
|
||||
'Layout': true,
|
||||
'Mask': true,
|
||||
'Pagination': true,
|
||||
'Printing': true
|
||||
};
|
||||
|
||||
function isRecursionAllowedFor(dict) {
|
||||
if (!isName(dict.Type)) {
|
||||
return true;
|
||||
}
|
||||
var dictType = dict.Type.name;
|
||||
return GETALL_DICTIONARY_TYPES_WHITELIST[dictType] === true;
|
||||
}
|
||||
|
||||
// xref is optional
|
||||
function Dict(xref) {
|
||||
// Map should only be used internally, use functions below to access.
|
||||
this.map = Object.create(null);
|
||||
this.xref = xref;
|
||||
this.objId = null;
|
||||
this.__nonSerializable__ = nonSerializable; // disable cloning of the Dict
|
||||
}
|
||||
|
||||
Dict.prototype = {
|
||||
assignXref: function Dict_assignXref(newXref) {
|
||||
this.xref = newXref;
|
||||
},
|
||||
|
||||
// automatically dereferences Ref objects
|
||||
get: function Dict_get(key1, key2, key3) {
|
||||
var value;
|
||||
var xref = this.xref;
|
||||
if (typeof (value = this.map[key1]) !== 'undefined' || key1 in this.map ||
|
||||
typeof key2 === 'undefined') {
|
||||
return xref ? xref.fetchIfRef(value) : value;
|
||||
}
|
||||
if (typeof (value = this.map[key2]) !== 'undefined' || key2 in this.map ||
|
||||
typeof key3 === 'undefined') {
|
||||
return xref ? xref.fetchIfRef(value) : value;
|
||||
}
|
||||
value = this.map[key3] || null;
|
||||
return xref ? xref.fetchIfRef(value) : value;
|
||||
},
|
||||
|
||||
// Same as get(), but returns a promise and uses fetchIfRefAsync().
|
||||
getAsync: function Dict_getAsync(key1, key2, key3) {
|
||||
var value;
|
||||
var xref = this.xref;
|
||||
if (typeof (value = this.map[key1]) !== 'undefined' || key1 in this.map ||
|
||||
typeof key2 === 'undefined') {
|
||||
if (xref) {
|
||||
return xref.fetchIfRefAsync(value);
|
||||
}
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
if (typeof (value = this.map[key2]) !== 'undefined' || key2 in this.map ||
|
||||
typeof key3 === 'undefined') {
|
||||
if (xref) {
|
||||
return xref.fetchIfRefAsync(value);
|
||||
}
|
||||
return Promise.resolve(value);
|
||||
}
|
||||
value = this.map[key3] || null;
|
||||
if (xref) {
|
||||
return xref.fetchIfRefAsync(value);
|
||||
}
|
||||
return Promise.resolve(value);
|
||||
},
|
||||
|
||||
// Same as get(), but dereferences all elements if the result is an Array.
|
||||
getArray: function Dict_getArray(key1, key2, key3) {
|
||||
var value = this.get(key1, key2, key3);
|
||||
var xref = this.xref;
|
||||
if (!isArray(value) || !xref) {
|
||||
return value;
|
||||
}
|
||||
value = value.slice(); // Ensure that we don't modify the Dict data.
|
||||
for (var i = 0, ii = value.length; i < ii; i++) {
|
||||
if (!isRef(value[i])) {
|
||||
continue;
|
||||
}
|
||||
value[i] = xref.fetch(value[i]);
|
||||
}
|
||||
return value;
|
||||
},
|
||||
|
||||
// no dereferencing
|
||||
getRaw: function Dict_getRaw(key) {
|
||||
return this.map[key];
|
||||
},
|
||||
|
||||
// creates new map and dereferences all Refs
|
||||
getAll: function Dict_getAll() {
|
||||
var all = Object.create(null);
|
||||
var queue = null;
|
||||
var key, obj;
|
||||
for (key in this.map) {
|
||||
obj = this.get(key);
|
||||
if (obj instanceof Dict) {
|
||||
if (isRecursionAllowedFor(obj)) {
|
||||
(queue || (queue = [])).push({target: all, key: key, obj: obj});
|
||||
} else {
|
||||
all[key] = this.getRaw(key);
|
||||
}
|
||||
} else {
|
||||
all[key] = obj;
|
||||
}
|
||||
}
|
||||
if (!queue) {
|
||||
return all;
|
||||
}
|
||||
|
||||
// trying to take cyclic references into the account
|
||||
var processed = Object.create(null);
|
||||
while (queue.length > 0) {
|
||||
var item = queue.shift();
|
||||
var itemObj = item.obj;
|
||||
var objId = itemObj.objId;
|
||||
if (objId && objId in processed) {
|
||||
item.target[item.key] = processed[objId];
|
||||
continue;
|
||||
}
|
||||
var dereferenced = Object.create(null);
|
||||
for (key in itemObj.map) {
|
||||
obj = itemObj.get(key);
|
||||
if (obj instanceof Dict) {
|
||||
if (isRecursionAllowedFor(obj)) {
|
||||
queue.push({target: dereferenced, key: key, obj: obj});
|
||||
} else {
|
||||
dereferenced[key] = itemObj.getRaw(key);
|
||||
}
|
||||
} else {
|
||||
dereferenced[key] = obj;
|
||||
}
|
||||
}
|
||||
if (objId) {
|
||||
processed[objId] = dereferenced;
|
||||
}
|
||||
item.target[item.key] = dereferenced;
|
||||
}
|
||||
return all;
|
||||
},
|
||||
|
||||
getKeys: function Dict_getKeys() {
|
||||
return Object.keys(this.map);
|
||||
},
|
||||
|
||||
set: function Dict_set(key, value) {
|
||||
this.map[key] = value;
|
||||
},
|
||||
|
||||
has: function Dict_has(key) {
|
||||
return key in this.map;
|
||||
},
|
||||
|
||||
forEach: function Dict_forEach(callback) {
|
||||
for (var key in this.map) {
|
||||
callback(key, this.get(key));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Dict.empty = new Dict(null);
|
||||
|
||||
Dict.merge = function Dict_merge(xref, dictArray) {
|
||||
var mergedDict = new Dict(xref);
|
||||
|
||||
for (var i = 0, ii = dictArray.length; i < ii; i++) {
|
||||
var dict = dictArray[i];
|
||||
if (!isDict(dict)) {
|
||||
continue;
|
||||
}
|
||||
for (var keyName in dict.map) {
|
||||
if (mergedDict.map[keyName]) {
|
||||
continue;
|
||||
}
|
||||
mergedDict.map[keyName] = dict.map[keyName];
|
||||
}
|
||||
}
|
||||
return mergedDict;
|
||||
};
|
||||
|
||||
return Dict;
|
||||
})();
|
||||
|
||||
var Ref = (function RefClosure() {
|
||||
function Ref(num, gen) {
|
||||
this.num = num;
|
||||
this.gen = gen;
|
||||
}
|
||||
|
||||
Ref.prototype = {
|
||||
toString: function Ref_toString() {
|
||||
// This function is hot, so we make the string as compact as possible.
|
||||
// |this.gen| is almost always zero, so we treat that case specially.
|
||||
var str = this.num + 'R';
|
||||
if (this.gen !== 0) {
|
||||
str += this.gen;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
};
|
||||
|
||||
return Ref;
|
||||
})();
|
||||
|
||||
// The reference is identified by number and generation.
|
||||
// This structure stores only one instance of the reference.
|
||||
var RefSet = (function RefSetClosure() {
|
||||
function RefSet() {
|
||||
this.dict = {};
|
||||
}
|
||||
|
||||
RefSet.prototype = {
|
||||
has: function RefSet_has(ref) {
|
||||
return ref.toString() in this.dict;
|
||||
},
|
||||
|
||||
put: function RefSet_put(ref) {
|
||||
this.dict[ref.toString()] = true;
|
||||
},
|
||||
|
||||
remove: function RefSet_remove(ref) {
|
||||
delete this.dict[ref.toString()];
|
||||
}
|
||||
};
|
||||
|
||||
return RefSet;
|
||||
})();
|
||||
|
||||
var RefSetCache = (function RefSetCacheClosure() {
|
||||
function RefSetCache() {
|
||||
this.dict = Object.create(null);
|
||||
}
|
||||
|
||||
RefSetCache.prototype = {
|
||||
get: function RefSetCache_get(ref) {
|
||||
return this.dict[ref.toString()];
|
||||
},
|
||||
|
||||
has: function RefSetCache_has(ref) {
|
||||
return ref.toString() in this.dict;
|
||||
},
|
||||
|
||||
put: function RefSetCache_put(ref, obj) {
|
||||
this.dict[ref.toString()] = obj;
|
||||
},
|
||||
|
||||
putAlias: function RefSetCache_putAlias(ref, aliasRef) {
|
||||
this.dict[ref.toString()] = this.get(aliasRef);
|
||||
},
|
||||
|
||||
forEach: function RefSetCache_forEach(fn, thisArg) {
|
||||
for (var i in this.dict) {
|
||||
fn.call(thisArg, this.dict[i]);
|
||||
}
|
||||
},
|
||||
|
||||
clear: function RefSetCache_clear() {
|
||||
this.dict = Object.create(null);
|
||||
}
|
||||
};
|
||||
|
||||
return RefSetCache;
|
||||
})();
|
||||
var InvalidPDFException = sharedUtil.InvalidPDFException;
|
||||
var MissingDataException = sharedUtil.MissingDataException;
|
||||
var XRefParseException = sharedUtil.XRefParseException;
|
||||
var assert = sharedUtil.assert;
|
||||
var bytesToString = sharedUtil.bytesToString;
|
||||
var createPromiseCapability = sharedUtil.createPromiseCapability;
|
||||
var error = sharedUtil.error;
|
||||
var info = sharedUtil.info;
|
||||
var isArray = sharedUtil.isArray;
|
||||
var isInt = sharedUtil.isInt;
|
||||
var isString = sharedUtil.isString;
|
||||
var shadow = sharedUtil.shadow;
|
||||
var stringToPDFString = sharedUtil.stringToPDFString;
|
||||
var stringToUTF8String = sharedUtil.stringToUTF8String;
|
||||
var warn = sharedUtil.warn;
|
||||
var Ref = corePrimitives.Ref;
|
||||
var RefSet = corePrimitives.RefSet;
|
||||
var RefSetCache = corePrimitives.RefSetCache;
|
||||
var isName = corePrimitives.isName;
|
||||
var isCmd = corePrimitives.isCmd;
|
||||
var isDict = corePrimitives.isDict;
|
||||
var isRef = corePrimitives.isRef;
|
||||
var isStream = corePrimitives.isStream;
|
||||
var CipherTransformFactory = coreCrypto.CipherTransformFactory;
|
||||
var Lexer = coreParser.Lexer;
|
||||
var Parser = coreParser.Parser;
|
||||
var ChunkedStream = coreChunkedStream.ChunkedStream;
|
||||
|
||||
var Catalog = (function CatalogClosure() {
|
||||
function Catalog(pdfManager, xref) {
|
||||
function Catalog(pdfManager, xref, pageFactory) {
|
||||
this.pdfManager = pdfManager;
|
||||
this.xref = xref;
|
||||
this.catDict = xref.getCatalogObj();
|
||||
|
@ -339,6 +69,8 @@ var Catalog = (function CatalogClosure() {
|
|||
assert(isDict(this.catDict),
|
||||
'catalog object is not a dictionary');
|
||||
|
||||
// TODO refactor to move getPage() to the PDFDocument.
|
||||
this.pageFactory = pageFactory;
|
||||
this.pagePromises = [];
|
||||
}
|
||||
|
||||
|
@ -617,8 +349,8 @@ var Catalog = (function CatalogClosure() {
|
|||
function (a) {
|
||||
var dict = a[0];
|
||||
var ref = a[1];
|
||||
return new Page(this.pdfManager, this.xref, pageIndex, dict, ref,
|
||||
this.fontCache);
|
||||
return this.pageFactory.createPage(pageIndex, dict, ref,
|
||||
this.fontCache);
|
||||
}.bind(this)
|
||||
);
|
||||
}
|
||||
|
@ -1749,3 +1481,8 @@ var ObjectLoader = (function() {
|
|||
|
||||
return ObjectLoader;
|
||||
})();
|
||||
|
||||
exports.Catalog = Catalog;
|
||||
exports.ObjectLoader = ObjectLoader;
|
||||
exports.XRef = XRef;
|
||||
}));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue