all files / core/graphics/ pdf-template.js

51.61% Statements 48/93
37.5% Branches 21/56
50% Functions 5/10
51.61% Lines 48/93
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                                                                                                                                                                                                
define(["require", "exports", "./../pdf-primitives", "./../base-stream", "./pdf-graphics", "./../utils", "./../import-export/json-document"], function (require, exports, pdf_primitives_1, base_stream_1, pdf_graphics_1, utils_1, json_document_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PdfTemplate = (function () {
        function PdfTemplate(value, crossReference) {
            this._isExported = false;
            this._isResourceExport = false;
            this._isSignature = false;
            this._isNew = false;
            Eif (crossReference) {
                this._crossReference = crossReference;
            }
            Iif (value === null || typeof value === 'undefined') {
                this._isReadOnly = true;
            }
            else {
                Iif (value instanceof base_stream_1._PdfBaseStream) {
                    this._content = value;
                    if (!this._content.dictionary.has('Type') || !this._content.dictionary.has('Subtype')) {
                        this._initialize();
                    }
                    var bounds = this._content.dictionary.getArray('BBox');
                    if (bounds && bounds.length > 3) {
                        var rect = utils_1._toRectangle(bounds);
                        this._size = { width: rect.width, height: rect.height };
                        this._templateOriginalSize = this._size;
                    }
                    this._isReadOnly = true;
                }
                else if (Array.isArray(value)) {
                    this._size = { width: value[2], height: value[3] };
                    this._content = new base_stream_1._PdfContentStream([]);
                    this._content.dictionary._crossReference = this._crossReference;
                    this._initialize();
                    this._content.dictionary.set('BBox', [value[0], value[1], value[0] + value[2], value[1] + value[3]]);
                }
                else Eif (value.width !== null && value.height !== null && typeof value.width !== 'undefined' && typeof value.height !== 'undefined') {
                    var bounds = void 0;
                    var values = value;
                    Eif (values.x !== null && typeof values.x !== 'undefined' && values.y !== null && typeof values.y !== 'undefined') {
                        bounds = { x: values.x, y: values.y, width: values.width, height: values.height };
                    }
                    else {
                        bounds = { x: 0, y: 0, width: values.width, height: values.height };
                    }
                    this._size = { width: bounds.width, height: bounds.height };
                    this._content = new base_stream_1._PdfContentStream([]);
                    Eif (this._crossReference) {
                        this._content.dictionary._crossReference = this._crossReference;
                    }
                    else {
                        this._isNew = true;
                    }
                    this._initialize();
                    this._content.dictionary.set('BBox', [bounds.x, bounds.y, bounds.x + bounds.width, bounds.y + bounds.height]);
                }
            }
            this._writeTransformation = true;
        }
        Object.defineProperty(PdfTemplate.prototype, "graphics", {
            get: function () {
                Iif (this._isReadOnly) {
                    return null;
                }
                Eif (typeof this._g === 'undefined') {
                    this._g = new pdf_graphics_1.PdfGraphics(this._size, this._content, this._crossReference, this);
                    Eif (this._writeTransformation) {
                        this._g._initializeCoordinates();
                    }
                    this._g._isTemplateGraphics = true;
                }
                return this._g;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfTemplate.prototype, "size", {
            get: function () {
                return this._size;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfTemplate.prototype, "_originalSize", {
            get: function () {
                return this._templateOriginalSize;
            },
            enumerable: true,
            configurable: true
        });
        PdfTemplate.prototype._initialize = function () {
            this._content.dictionary.set('Type', pdf_primitives_1._PdfName.get('XObject'));
            this._content.dictionary.set('Subtype', pdf_primitives_1._PdfName.get('Form'));
        };
        PdfTemplate.prototype._exportStream = function (dictionary, crossReference, key) {
            var jsonDocument = new json_document_1._JsonDocument();
            jsonDocument._crossReference = crossReference;
            jsonDocument._isAnnotationExport = true;
            var resourceTable = new Map();
            jsonDocument._writeObject(resourceTable, dictionary.get(key), dictionary, 'normal');
            this._appearance = jsonDocument._convertToJson(resourceTable);
            jsonDocument._dispose();
        };
        PdfTemplate.prototype._importStream = function (hasCrossReference, isResourceExport) {
            var jsonDocument = new json_document_1._JsonDocument();
            if (hasCrossReference) {
                jsonDocument._crossReference = this._crossReference;
            }
            var json = JSON.parse(this._appearance);
            if (json) {
                var entryKey = isResourceExport ? 'resources' : 'normal';
                var entry = json[entryKey];
                if (entry) {
                    if (isResourceExport) {
                        var resourceDictionary = jsonDocument._parseDictionary(entry['dict']);
                        if (hasCrossReference) {
                            this._content.dictionary.update('Resources', resourceDictionary);
                        }
                    }
                    else {
                        this._content = jsonDocument._parseStream(entry['stream']);
                        if (hasCrossReference) {
                            this._content.dictionary._crossReference = this._crossReference;
                            this._content.dictionary._updated = true;
                        }
                    }
                }
            }
            jsonDocument._dispose();
        };
        PdfTemplate.prototype._updatePendingResource = function (crossReference) {
            if (this._content._pendingResources && this._content._pendingResources !== '') {
                var jsonDocument = new json_document_1._JsonDocument();
                jsonDocument._crossReference = crossReference;
                jsonDocument._parseStreamElements(this._content);
                this._content._pendingResources = '';
                jsonDocument._dispose();
            }
        };
        return PdfTemplate;
    }());
    exports.PdfTemplate = PdfTemplate;
});