all files / core/ pdf-primitives.js

67.74% Statements 147/217
54.43% Branches 43/79
78.18% Functions 43/55
68.22% Lines 146/214
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310   518×   1126×     58×   3176×     190× 190× 190×   170×   287× 287×     12× 12×   35×   42×   22×       10×               16×   27×   14×     10×                                                                                       293× 293× 293× 293×               116×       380× 380× 67×   380×   46× 46× 46× 38× 130×       1083×   457×   19× 60×     80×           76× 76×                                                                                                         293× 293× 293× 293× 293× 293× 143×     380× 380× 72× 72× 14×   58×       380×               143×   5402×      
define(["require", "exports", "./utils"], function (require, exports, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var nameCache = Object.create(null);
    var cmdCache = Object.create(null);
    var refCache = Object.create(null);
    var _PdfName = (function () {
        function _PdfName(name) {
            this.name = name;
        }
        _PdfName.get = function (name) {
            return nameCache[name] || (nameCache[name] = new _PdfName(name));
        };
        return _PdfName;
    }());
    exports._PdfName = _PdfName;
    var _PdfCommand = (function () {
        function _PdfCommand(command) {
            this.command = command;
        }
        _PdfCommand.get = function (command) {
            return cmdCache[command] || (cmdCache[command] = new _PdfCommand(command));
        };
        return _PdfCommand;
    }());
    exports._PdfCommand = _PdfCommand;
    var _PdfReference = (function () {
        function _PdfReference(objectNumber, gen) {
            this._isNew = false;
            this.objectNumber = objectNumber;
            this.generationNumber = gen;
        }
        _PdfReference.prototype.toString = function () {
            return this.objectNumber + " " + this.generationNumber;
        };
        _PdfReference.get = function (objectNumber, generationNumber) {
            var key = generationNumber === 0 ? objectNumber + "R" : objectNumber + "R" + generationNumber;
            return refCache[key] || (refCache[key] = new _PdfReference(objectNumber, generationNumber));
        };
        return _PdfReference;
    }());
    exports._PdfReference = _PdfReference;
    var _PdfReferenceSet = (function () {
        function _PdfReferenceSet(parent) {
            Eif (parent === void 0) { parent = null; }
            this._set = new Set(parent && parent._set);
        }
        _PdfReferenceSet.prototype.has = function (ref) {
            return this._set.has(ref.toString());
        };
        _PdfReferenceSet.prototype.put = function (ref) {
            this._set.add(ref.toString());
        };
        _PdfReferenceSet.prototype.remove = function (ref) {
            this._set.delete(ref.toString());
        };
        _PdfReferenceSet.prototype.clear = function () {
            this._set.clear();
        };
        return _PdfReferenceSet;
    }());
    exports._PdfReferenceSet = _PdfReferenceSet;
    var _PdfReferenceSetCache = (function () {
        function _PdfReferenceSetCache() {
            this._map = new Map();
        }
        Object.defineProperty(_PdfReferenceSetCache.prototype, "size", {
            get: function () {
                return this._map.size;
            },
            enumerable: true,
            configurable: true
        });
        _PdfReferenceSetCache.prototype.get = function (ref) {
            return this._map.get(ref.toString());
        };
        _PdfReferenceSetCache.prototype.has = function (ref) {
            return this._map.has(ref.toString());
        };
        _PdfReferenceSetCache.prototype.put = function (ref, obj) {
            this._map.set(ref.toString(), obj);
        };
        _PdfReferenceSetCache.prototype.set = function (objId, obj) {
            this._map.set(objId, obj);
        };
        _PdfReferenceSetCache.prototype.clear = function () {
            this._map.clear();
        };
        return _PdfReferenceSetCache;
    }());
    exports._PdfReferenceSetCache = _PdfReferenceSetCache;
    var Dictionary = (function () {
        function Dictionary(toStringFunction) {
            this.table = {};
            this.nElements = 0;
            this.toStr = toStringFunction || utils_1._defaultToString;
        }
        Dictionary.prototype.getValue = function (key) {
            var pair = this.table['$' + this.toStr(key)];
            if (typeof pair === 'undefined') {
                return undefined;
            }
            return pair.value;
        };
        Dictionary.prototype.setValue = function (key, value) {
            var ret;
            var k = '$' + this.toStr(key);
            var previousElement = this.table[k];
            this.nElements++;
            ret = undefined;
            this.table[k] = {
                key: key,
                value: value
            };
            return ret;
        };
        Dictionary.prototype.keys = function () {
            var keysArray = [];
            var namesOfKeys = Object.keys(this.table);
            for (var index1 = 0; index1 < namesOfKeys.length; index1++) {
                var pair1 = this.table[namesOfKeys[index1]];
                keysArray.push(pair1.key);
            }
            return keysArray;
        };
        Dictionary.prototype.containsKey = function (key) {
            var retutnValue = true;
            if (typeof this.getValue(key) === 'undefined') {
                retutnValue = true;
            }
            else {
                retutnValue = false;
            }
            return !retutnValue;
        };
        Dictionary.prototype._size = function () {
            return this.nElements;
        };
        return Dictionary;
    }());
    exports.Dictionary = Dictionary;
    var _PdfDictionary = (function () {
        function _PdfDictionary(xref) {
            this._isFont = false;
            this._isProcessed = false;
            this._isSignature = false;
            this._initialize(xref);
        }
        Object.defineProperty(_PdfDictionary.prototype, "size", {
            get: function () {
                return Object.keys(this._map).length;
            },
            enumerable: true,
            configurable: true
        });
        _PdfDictionary.prototype.assignXref = function (xref) {
            this._crossReference = xref;
        };
        _PdfDictionary.prototype.getRaw = function (key) {
            return this._map[key];
        };
        _PdfDictionary.prototype.getRawValues = function () {
            return this._map.values;
        };
        _PdfDictionary.prototype.get = function (key1, key2, key3) {
            var value = this._get(key1, key2, key3);
            if (this._crossReference && typeof value !== 'undefined' && value instanceof _PdfReference) {
                value = this._crossReference._fetch(value);
            }
            return value;
        };
        _PdfDictionary.prototype.getArray = function (key1, key2, key3) {
            var _this = this;
            var rawValue = this.get(key1, key2, key3);
            if (this._crossReference && Array.isArray(rawValue)) {
                return rawValue.map(function (item) {
                    return item instanceof _PdfReference ? _this._crossReference._fetch(item) : item;
                });
            }
            return rawValue;
        };
        _PdfDictionary.prototype.set = function (key, value) {
            this._map[key] = value;
        };
        _PdfDictionary.prototype.has = function (key) {
            return typeof this._map[key] !== 'undefined';
        };
        _PdfDictionary.prototype.forEach = function (callback) {
            for (var key in this._map) {
                callback(key, this.getRaw(key));
            }
        };
        _PdfDictionary.prototype.update = function (key, value) {
            if (this.has(key)) {
                var prevValue = this._map[key];
                Iif (prevValue !== null && typeof prevValue !== 'undefined' && prevValue instanceof _PdfReference && this._crossReference) {
                    prevValue = this._crossReference._fetch(prevValue);
                }
                if (prevValue !== value) {
                    this._map[key] = value;
                    this._updated = true;
                }
            }
            else {
                this._map[key] = value;
                this._updated = true;
            }
        };
        _PdfDictionary.getEmpty = function (xref) {
            var emptyDict = new _PdfDictionary(xref);
            emptyDict.set = function (key, value) {
                throw new Error('Should not call set on the empty dictionary.');
            };
            return emptyDict;
        };
        _PdfDictionary.merge = function (xref, dictionaryArray, mergeSubDictionary) {
            if (mergeSubDictionary === void 0) { mergeSubDictionary = false; }
            var mergedDictionary = new _PdfDictionary(xref);
            var properties = Object.create(null);
            for (var _i = 0, dictionaryArray_1 = dictionaryArray; _i < dictionaryArray_1.length; _i++) {
                var dictionary = dictionaryArray_1[_i];
                if (!(dictionary instanceof _PdfDictionary)) {
                    continue;
                }
                for (var _a = 0, _b = dictionary._map; _a < _b.length; _a++) {
                    var _c = _b[_a], key = _c[0], value = _c[1];
                    var property = properties.get(key);
                    if (typeof property === 'undefined') {
                        property = [];
                        properties.set(key, property);
                    }
                    else if (!mergeSubDictionary || !(value instanceof _PdfDictionary)) {
                        continue;
                    }
                    property.push(value);
                }
            }
            for (var _d = 0, properties_1 = properties; _d < properties_1.length; _d++) {
                var _e = properties_1[_d], name_1 = _e[0], values = _e[1];
                if (values.length === 1 || !(values[0] instanceof _PdfDictionary)) {
                    mergedDictionary._map[name_1] = values[0];
                    continue;
                }
                var subDict = new _PdfDictionary(xref);
                for (var _f = 0, values_1 = values; _f < values_1.length; _f++) {
                    var dictionary = values_1[_f];
                    for (var _g = 0, _h = dictionary._map; _g < _h.length; _g++) {
                        var _j = _h[_g], key = _j[0], value = _j[1];
                        if (typeof subDict._map[key] === 'undefined') {
                            subDict._map[key] = value;
                        }
                    }
                }
                if (subDict.size > 0) {
                    mergedDictionary._map[name_1] = subDict;
                }
            }
            properties.clear();
            return mergedDictionary.size > 0 ? mergedDictionary : _PdfDictionary.getEmpty(xref);
        };
        _PdfDictionary.prototype._initialize = function (xref) {
            this._map = Object.create(null);
            this.suppressEncryption = false;
            this._updated = false;
            this.isCatalog = false;
            this._isNew = false;
            if (xref) {
                this._crossReference = xref;
            }
        };
        _PdfDictionary.prototype._get = function (key1, key2, key3) {
            var value = this._map[key1];
            if (typeof value === 'undefined') {
                value = this._map[key2];
                if (typeof key2 !== 'undefined' && key2 !== null) {
                    value = this._map[key2];
                }
                else Iif (typeof key3 !== 'undefined' && key3 !== null) {
                    value = this._map[key3];
                }
            }
            return value;
        };
        return _PdfDictionary;
    }());
    exports._PdfDictionary = _PdfDictionary;
    var _PdfNull = (function () {
        function _PdfNull(value) {
            if (value === void 0) { value = []; }
            this.value = value;
        }
        return _PdfNull;
    }());
    exports._PdfNull = _PdfNull;
    function _clearPrimitiveCaches() {
        nameCache = Object.create(null);
        cmdCache = Object.create(null);
        refCache = Object.create(null);
    }
    exports._clearPrimitiveCaches = _clearPrimitiveCaches;
    function _isName(value, name) {
        return value instanceof _PdfName && (typeof name === 'undefined' || value.name === name);
    }
    exports._isName = _isName;
    function _isCommand(value, command) {
        return value instanceof _PdfCommand && (typeof command === 'undefined' || value.command === command);
    }
    exports._isCommand = _isCommand;
});