all files / core/graphics/ pdf-stream-writer.js

59.24% Statements 109/184
36.17% Branches 17/47
61.22% Functions 30/49
59.24% Lines 109/184
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     101× 101×                                                             10× 10×     12×                                                                                                                                                                                                                                              
define(["require", "exports", "./../utils", "../enumerator"], function (require, exports, utils_1, enumerator_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var _PdfStreamWriter = (function () {
        function _PdfStreamWriter(stream) {
            this._newLine = '\r\n';
            this._whiteSpace = ' ';
            this._stream = stream;
        }
        _PdfStreamWriter.prototype._writeOperator = function (value) {
            this._stream.write(value);
            this._stream.write(this._newLine);
        };
        _PdfStreamWriter.prototype._saveGraphicsState = function () {
            this._writeOperator('q');
        };
        _PdfStreamWriter.prototype._restoreGraphicsState = function () {
            this._writeOperator('Q');
        };
        _PdfStreamWriter.prototype._writeComment = function (comment) {
            Eif (comment && comment.length > 0) {
                this._writeOperator('% ' + comment);
            }
        };
        _PdfStreamWriter.prototype._setGraphicsState = function (value) {
            this._stream.write("/" + utils_1._escapePdfName(value.name) + " ");
            this._writeOperator('gs');
        };
        _PdfStreamWriter.prototype._modifyCtm = function (matrix) {
            this._stream.write(matrix._toString() + " ");
            this._writeOperator('cm');
        };
        _PdfStreamWriter.prototype._modifyTM = function (matrix) {
            this._stream.write(matrix._toString() + " ");
            this._writeOperator('Tm');
        };
        _PdfStreamWriter.prototype._setColorSpace = function (value, arg2, arg3) {
            Eif (typeof value === 'string' && typeof arg2 === 'boolean') {
                this._stream.write("/" + value + " ");
                this._writeOperator(arg2 ? 'CS' : 'cs');
            }
            else if (Array.isArray(value) && typeof arg2 === 'number' && typeof arg3 === 'boolean') {
                var colorSpaceName = void 0;
                switch (arg2) {
                    case enumerator_1._PdfColorSpace.rgb:
                        colorSpaceName = 'DeviceRGB';
                        break;
                    case enumerator_1._PdfColorSpace.cmyk:
                        colorSpaceName = 'DeviceCMYK';
                        break;
                    case enumerator_1._PdfColorSpace.grayScale:
                        colorSpaceName = 'DeviceGray';
                        break;
                    default:
                        colorSpaceName = 'DeviceRGB';
                        break;
                }
                this._stream.write("/" + colorSpaceName + " ");
                this._writeOperator(arg3 ? 'CS' : 'cs');
                this._setColor(value, arg3);
            }
        };
        _PdfStreamWriter.prototype._setColor = function (color, forStroking) {
            this._stream.write((color[0] / 255).toFixed(3) + " " + (color[1] / 255).toFixed(3) + " " + (color[2] / 255).toFixed(3) + " ");
            this._writeOperator(forStroking ? 'RG' : 'rg');
        };
        _PdfStreamWriter.prototype._appendRectangle = function (x, y, width, height) {
            this._writePoint(x, y);
            this._writePoint(width, height);
            this._writeOperator('re');
        };
        _PdfStreamWriter.prototype._writePoint = function (x, y) {
            this._stream.write(x.toFixed(3) + " " + (-y).toFixed(3) + " ");
        };
        _PdfStreamWriter.prototype._clipPath = function (isEvenOdd) {
            this._stream.write((isEvenOdd ? 'W*' : 'W') + " n" + this._newLine);
        };
        _PdfStreamWriter.prototype._fillPath = function (isEvenOdd) {
            this._writeOperator(isEvenOdd ? 'f*' : 'f');
        };
        _PdfStreamWriter.prototype._closeFillPath = function (isEvenOdd) {
            this._writeOperator('h');
            this._fillPath(isEvenOdd);
        };
        _PdfStreamWriter.prototype._strokePath = function () {
            this._writeOperator('S');
        };
        _PdfStreamWriter.prototype._closeStrokePath = function () {
            this._writeOperator('s');
        };
        _PdfStreamWriter.prototype._fillStrokePath = function (isEvenOdd) {
            this._writeOperator(isEvenOdd ? 'B*' : 'B');
        };
        _PdfStreamWriter.prototype._closeFillStrokePath = function (isEvenOdd) {
            this._writeOperator(isEvenOdd ? 'b*' : 'b');
        };
        _PdfStreamWriter.prototype._endPath = function () {
            this._writeOperator('n');
        };
        _PdfStreamWriter.prototype._setFont = function (name, size) {
            this._stream.write("/" + name + " " + size.toFixed(3) + " ");
            this._writeOperator('Tf');
        };
        _PdfStreamWriter.prototype._setTextScaling = function (textScaling) {
            this._stream.write(textScaling.toFixed(3) + " ");
            this._writeOperator('Tz');
        };
        _PdfStreamWriter.prototype._closePath = function () {
            this._writeOperator('h');
        };
        _PdfStreamWriter.prototype._startNextLine = function (x, y) {
            Iif (typeof x === 'undefined') {
                this._writeOperator('T*');
            }
            else {
                this._writePoint(x, y);
                this._writeOperator('Td');
            }
        };
        _PdfStreamWriter.prototype._setLeading = function (leading) {
            this._write(leading.toFixed(3) + " ");
            this._write(this._whiteSpace);
            this._writeOperator('TL');
        };
        _PdfStreamWriter.prototype._showText = function (text) {
            this._writeText(text);
            this._writeOperator('Tj');
        };
        _PdfStreamWriter.prototype._write = function (string) {
            var builder = '';
            builder += string;
            builder += '\r\n';
            this._writeOperator(builder);
        };
        _PdfStreamWriter.prototype._writeText = function (text) {
            var result = '';
            var data = this._escapeSymbols(text);
            for (var i = 0; i < data.length; i++) {
                result += String.fromCharCode(data[i]);
            }
            result = '(' + result + ')';
            this._stream.write(result);
        };
        _PdfStreamWriter.prototype._beginText = function () {
            this._writeOperator('BT');
        };
        _PdfStreamWriter.prototype._endText = function () {
            this._writeOperator('ET');
        };
        _PdfStreamWriter.prototype._beginPath = function (x, y) {
            this._writePoint(x, y);
            this._writeOperator('m');
        };
        _PdfStreamWriter.prototype._appendLineSegment = function (x, y) {
            this._writePoint(x, y);
            this._writeOperator('l');
        };
        _PdfStreamWriter.prototype._appendBezierSegment = function (x1, y1, x2, y2, x3, y3) {
            this._writePoint(x1, y1);
            this._writePoint(x2, y2);
            this._writePoint(x3, y3);
            this._writeOperator('c');
        };
        _PdfStreamWriter.prototype._setTextRenderingMode = function (renderingMode) {
            this._stream.write(renderingMode.toString() + " ");
            this._writeOperator('Tr');
        };
        _PdfStreamWriter.prototype._setCharacterSpacing = function (charSpacing) {
            this._stream.write(charSpacing.toFixed(3) + " ");
            this._writeOperator('Tc');
        };
        _PdfStreamWriter.prototype._setWordSpacing = function (wordSpacing) {
            this._stream.write(wordSpacing.toFixed(3) + " ");
            this._writeOperator('Tw');
        };
        _PdfStreamWriter.prototype._showNextLineText = function (text, unicode) {
            Iif (unicode !== null && typeof unicode !== 'undefined' && unicode) {
                this._writeText(text);
                this._writeOperator('\'');
            }
            else {
                this._stream.write(text);
                this._writeOperator('\'');
            }
        };
        _PdfStreamWriter.prototype._setLineDashPattern = function (pattern, patternOffset) {
            var tempPattern = '[';
            Iif (pattern.length >= 1) {
                for (var index = 0; index < pattern.length; index++) {
                    if (index === pattern.length - 1) {
                        tempPattern += pattern[index].toString();
                    }
                    else {
                        tempPattern += pattern[index].toString() + ' ';
                    }
                }
            }
            tempPattern += '] ';
            tempPattern += patternOffset.toString();
            tempPattern += ' d';
            this._writeOperator(tempPattern);
        };
        _PdfStreamWriter.prototype._setMiterLimit = function (miterLimit) {
            this._stream.write(miterLimit.toFixed(3) + " ");
            this._writeOperator('M');
        };
        _PdfStreamWriter.prototype._setLineWidth = function (width) {
            this._stream.write(width.toFixed(3) + " ");
            this._writeOperator('w');
        };
        _PdfStreamWriter.prototype._setLineCap = function (lineCapStyle) {
            this._stream.write(lineCapStyle + " ");
            this._writeOperator('J');
        };
        _PdfStreamWriter.prototype._setLineJoin = function (lineJoinStyle) {
            this._stream.write(lineJoinStyle + " ");
            this._writeOperator('j');
        };
        _PdfStreamWriter.prototype._executeObject = function (name) {
            this._stream.write("/" + name.name + " ");
            this._writeOperator('Do');
        };
        _PdfStreamWriter.prototype._beginMarkupSequence = function (name) {
            this._stream.write("/" + name + " ");
            this._writeOperator('BMC');
        };
        _PdfStreamWriter.prototype._endMarkupSequence = function () {
            this._writeOperator('EMC');
        };
        _PdfStreamWriter.prototype._clear = function () {
            this._stream._bytes = [];
        };
        _PdfStreamWriter.prototype._escapeSymbols = function (value) {
            var data = [];
            for (var i = 0; i < value.length; i++) {
                var currentData = value.charCodeAt(i);
                switch (currentData) {
                    case 40:
                    case 41:
                        data.push(92);
                        data.push(currentData);
                        break;
                    case 13:
                        data.push(92);
                        data.push(114);
                        break;
                    case 92:
                        data.push(92);
                        data.push(currentData);
                        break;
                    default:
                        data.push(currentData);
                        break;
                }
            }
            return data;
        };
        return _PdfStreamWriter;
    }());
    exports._PdfStreamWriter = _PdfStreamWriter;
});