all files / input-output/ pdf-writer.js

43.27% Statements 45/104
15% Branches 3/20
42.31% Functions 11/26
44.55% Lines 45/101
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                                                                                                                       126×               214×     214×                               3935×     3935×       3935× 3935×                                                                      
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfChunkBuffer = (function () {
        function _PdfChunkBuffer(chunkSize) {
            if (chunkSize === void 0) { chunkSize = 1048576; }
            this.chunks = [];
            this.offset = 0;
            this.committed = 0;
            this.chunkSize = chunkSize;
            this.current = new Uint8Array(this.chunkSize);
            this.chunks.push(this.current);
        }
        Object.defineProperty(_PdfChunkBuffer.prototype, "length", {
            get: function () {
                return this.committed + this.offset;
            },
            enumerable: true,
            configurable: true
        });
        _PdfChunkBuffer.prototype.grow = function () {
            this.committed += this.offset;
            this.current = new Uint8Array(this.chunkSize);
            this.chunks.push(this.current);
            this.offset = 0;
        };
        _PdfChunkBuffer.prototype.writeAscii = function (str) {
            var idx = 0;
            while (idx < str.length) {
                if (this.offset >= this.current.byteLength) {
                    this.grow();
                }
                var space = this.current.byteLength - this.offset;
                var toWrite = Math.min(space, str.length - idx);
                for (var i = 0; i < toWrite; i++) {
                    this.current[this.offset + i] = str.charCodeAt(idx + i) & 0xFF;
                }
                this.offset += toWrite;
                idx += toWrite;
            }
        };
        _PdfChunkBuffer.prototype.toUint8Array = function () {
            var total = this.length;
            var out = new Uint8Array(total);
            var pos = 0;
            var lastIdx = this.chunks.length - 1;
            for (var i = 0; i <= lastIdx; i++) {
                var chunk = this.chunks[i];
                if (i === lastIdx) {
                    out.set(chunk.subarray(0, this.offset), pos);
                    pos += this.offset;
                }
                else {
                    out.set(chunk, pos);
                    pos += chunk.byteLength;
                }
            }
            return out;
        };
        _PdfChunkBuffer.prototype.destroy = function () {
            this.chunks = [];
            this.current = undefined;
            this.offset = 0;
            this.committed = 0;
        };
        return _PdfChunkBuffer;
    }());
    exports._PdfChunkBuffer = _PdfChunkBuffer;
    var PdfWriter = (function () {
        function PdfWriter(stream) {
            this.byteCountForStreamWriter = 0;
            this.streamWriter = stream;
        }
        Object.defineProperty(PdfWriter.prototype, "document", {
            get: function () {
                return this.pdfDocument;
            },
            set: function (value) {
                this.pdfDocument = value;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfWriter.prototype, "position", {
            get: function () {
                Iif (this.streamWriter instanceof PdfWriterHelper) {
                    return this.streamWriter.buffer.size;
                }
                return this.byteCountForStreamWriter;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfWriter.prototype, "length", {
            get: function () {
                return this.position;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(PdfWriter.prototype, "stream", {
            get: function () {
                return this.streamWriter;
            },
            enumerable: true,
            configurable: true
        });
        PdfWriter.prototype.write = function (data) {
            Iif (typeof data === 'number') {
                data = String.fromCharCode(data);
            }
            Iif (this.streamWriter instanceof PdfWriterHelper) {
                this.streamWriter.write(data);
            }
            else {
                this.streamWriter.write(data);
                this.byteCountForStreamWriter += data.length;
            }
        };
        return PdfWriter;
    }());
    exports.PdfWriter = PdfWriter;
    var PdfWriterHelper = (function () {
        function PdfWriterHelper(chunkSize) {
            if (chunkSize === void 0) { chunkSize = 1048576; }
            this.buffer = new PdfArrayBuffer(chunkSize);
        }
        PdfWriterHelper.prototype.write = function (data) {
            this.buffer.write(data);
        };
        PdfWriterHelper.prototype.destroy = function () {
            if (this.buffer) {
                this.buffer.destroy();
                this.buffer = undefined;
            }
        };
        return PdfWriterHelper;
    }());
    exports.PdfWriterHelper = PdfWriterHelper;
    var PdfArrayBuffer = (function () {
        function PdfArrayBuffer(chunkSize) {
            if (chunkSize === void 0) { chunkSize = 1048576; }
            this.buf = new _PdfChunkBuffer(chunkSize);
        }
        Object.defineProperty(PdfArrayBuffer.prototype, "size", {
            get: function () {
                return this.buf.length;
            },
            enumerable: true,
            configurable: true
        });
        PdfArrayBuffer.prototype.write = function (value) {
            this.buf.writeAscii(value);
        };
        PdfArrayBuffer.prototype.toUint8Array = function () {
            return this.buf.toUint8Array();
        };
        PdfArrayBuffer.prototype.destroy = function () {
            if (this.buf) {
                this.buf.destroy();
                this.buf = undefined;
            }
        };
        return PdfArrayBuffer;
    }());
    exports.PdfArrayBuffer = PdfArrayBuffer;
});