all files / annotations/ pdf-text-web-link.js

19.23% Statements 25/130
20% Branches 11/55
53.33% Functions 8/15
17.32% Lines 22/127
11 statements, 6 functions, 9 branches Ignored     
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                                                                                                                                                                                                                                                                                                                             
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "./../pages/pdf-page", "./../drawing/pdf-drawing", "./../graphics/figures/text-element", "./uri-annotation", "./../graphics/fonts/string-layouter", "./../graphics/fonts/enum", "./../graphics/enum", "./../primitives/pdf-array", "./../primitives/pdf-number"], function (require, exports, pdf_page_1, pdf_drawing_1, text_element_1, uri_annotation_1, string_layouter_1, enum_1, enum_2, pdf_array_1, pdf_number_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PdfTextWebLink = (function (_super) {
        __extends(PdfTextWebLink, _super);
        function PdfTextWebLink() {
            var _this = _super.call(this) || this;
            _this.uniformResourceLocator = '';
            _this.uriAnnotation = null;
            _this.recalculateBounds = false;
            _this.defaultBorder = new pdf_array_1.PdfArray();
            for (var i = 0; i < 3; i++) {
                _this.defaultBorder.add(new pdf_number_1.PdfNumber(0));
            }
            return _this;
        }
        Object.defineProperty(PdfTextWebLink.prototype, "url", {
            get: function () {
                return this.uniformResourceLocator;
            },
            set: function (value) {
                if (value.length === 0) {
                    throw new Error('ArgumentException : Url - string can not be empty');
                }
                this.uniformResourceLocator = value;
            },
            enumerable: true,
            configurable: true
        });
        PdfTextWebLink.prototype.draw = function (arg1, arg2) {
            if (arg1 instanceof pdf_page_1.PdfPage) {
                var layout = new string_layouter_1.PdfStringLayouter();
                var previousFontStyle = this.font.style;
                if (arg2 instanceof pdf_drawing_1.PointF) {
                    this.recalculateBounds = true;
                    this.font.style = enum_1.PdfFontStyle.Underline;
                    var layoutResult = layout.layout(this.value, this.font, this.stringFormat, new pdf_drawing_1.SizeF((arg1.graphics.clientSize.width - arg2.x), 0), true, arg1.graphics.clientSize);
                    if (layoutResult.lines.length === 1) {
                        var textSize = this.font.measureString(this.value);
                        var rect = new pdf_drawing_1.RectangleF(arg2, textSize);
                        rect = this.calculateBounds(rect, textSize.width, arg1.graphics.clientSize.width, arg2.x);
                        this.uriAnnotation = new uri_annotation_1.PdfUriAnnotation(rect, this.url);
                        this.uriAnnotation.dictionary.items.setValue('Border', this.defaultBorder);
                        arg1.annotations.add(this.uriAnnotation);
                        var result = this.drawText(arg1, arg2);
                        this.font.style = previousFontStyle;
                        return result;
                    }
                    else {
                        var result = this.drawMultipleLineWithPoint(layoutResult, arg1, arg2);
                        this.font.style = previousFontStyle;
                        return result;
                    }
                }
                else {
                    var layoutResult = layout.layout(this.value, this.font, this.stringFormat, new pdf_drawing_1.SizeF(arg2.width, 0), false, new pdf_drawing_1.SizeF(0, 0));
                    this.font.style = enum_1.PdfFontStyle.Underline;
                    if (layoutResult.lines.length === 1) {
                        var textSize = this.font.measureString(this.value);
                        var rect = new pdf_drawing_1.RectangleF(new pdf_drawing_1.PointF(arg2.x, arg2.y), textSize);
                        rect = this.calculateBounds(rect, textSize.width, arg2.width, arg2.x);
                        this.uriAnnotation = new uri_annotation_1.PdfUriAnnotation(rect, this.url);
                        this.uriAnnotation.dictionary.items.setValue('Border', this.defaultBorder);
                        arg1.annotations.add(this.uriAnnotation);
                        var returnValue = this.drawText(arg1, arg2);
                        this.font.style = previousFontStyle;
                        return returnValue;
                    }
                    else {
                        var returnValue = this.drawMultipleLineWithBounds(layoutResult, arg1, arg2);
                        this.font.style = previousFontStyle;
                        return returnValue;
                    }
                }
            }
            else {
                var page = new pdf_page_1.PdfPage();
                page = arg1.page;
                return this.draw(page, arg2);
            }
        };
        PdfTextWebLink.prototype.drawMultipleLineWithPoint = function (result, page, location) {
            var layoutResult;
            for (var i = 0; i < result.layoutLines.length; i++) {
                var size = this.font.measureString(result.lines[i].text);
                if (i === result.layoutLines.length - 1 && this.stringFormat && this.stringFormat.alignment === enum_2.PdfTextAlignment.Justify) {
                    this._isLastElement = true;
                }
                var bounds = new pdf_drawing_1.RectangleF(location, size);
                if (i !== 0) {
                    bounds.x = 0;
                }
                this.text = result.lines[i].text;
                if (bounds.y + size.height > page.graphics.clientSize.height) {
                    if (i !== 0) {
                        page = page.graphics.getNextPage();
                        bounds = new pdf_drawing_1.RectangleF(0, 0, page.graphics.clientSize.width, size.height);
                        location.y = 0;
                    }
                    else {
                        break;
                    }
                }
                bounds = this.calculateBounds(bounds, size.width, page.graphics.clientSize.width, bounds.x);
                this.uriAnnotation = new uri_annotation_1.PdfUriAnnotation(bounds, this.url);
                this.uriAnnotation.dictionary.items.setValue('Border', this.defaultBorder);
                page.annotations.add(this.uriAnnotation);
                if (i !== 0) {
                    layoutResult = this.drawText(page, new pdf_drawing_1.PointF(0, bounds.y));
                }
                else {
                    layoutResult = this.drawText(page, bounds.x, bounds.y);
                }
                location.y += size.height;
            }
            return layoutResult;
        };
        PdfTextWebLink.prototype.drawMultipleLineWithBounds = function (result, page, bounds) {
            var layoutResult;
            for (var i = 0; i < result.layoutLines.length; i++) {
                var size = this.font.measureString(result.lines[i].text);
                var internalBounds = new pdf_drawing_1.RectangleF(new pdf_drawing_1.PointF(bounds.x, bounds.y), size);
                internalBounds = this.calculateBounds(internalBounds, size.width, bounds.width, bounds.x);
                this.text = result.lines[i].text;
                if (bounds.y + size.height > page.graphics.clientSize.height) {
                    if (i !== 0) {
                        page = page.graphics.getNextPage();
                        bounds = new pdf_drawing_1.RectangleF(bounds.x, 0, bounds.width, size.height);
                        internalBounds.y = 0;
                    }
                    else {
                        break;
                    }
                }
                this.uriAnnotation = new uri_annotation_1.PdfUriAnnotation(internalBounds, this.url);
                this.uriAnnotation.dictionary.items.setValue('Border', this.defaultBorder);
                page.annotations.add(this.uriAnnotation);
                layoutResult = this.drawText(page, bounds);
                bounds.y += size.height;
            }
            return layoutResult;
        };
        PdfTextWebLink.prototype.calculateBounds = function (currentBounds, lineWidth, maximumWidth, startPosition) {
            var shift = 0;
            if (this.stringFormat != null && typeof this.stringFormat !== 'undefined' && this.stringFormat.alignment === enum_2.PdfTextAlignment.Center) {
                currentBounds.x = startPosition + (maximumWidth - lineWidth) / 2;
                currentBounds.width = lineWidth;
            }
            else if (this.stringFormat != null && typeof this.stringFormat !== 'undefined' && this.stringFormat.alignment === enum_2.PdfTextAlignment.Right) {
                currentBounds.x = startPosition + (maximumWidth - lineWidth);
                currentBounds.width = lineWidth;
            }
            else if (this.stringFormat != null && typeof this.stringFormat !== 'undefined' && this.stringFormat.alignment === enum_2.PdfTextAlignment.Justify) {
                currentBounds.x = startPosition;
                currentBounds.width = maximumWidth;
            }
            else {
                currentBounds.width = startPosition;
                currentBounds.width = lineWidth;
            }
            return currentBounds;
        };
        return PdfTextWebLink;
    }(text_element_1.PdfTextElement));
    exports.PdfTextWebLink = PdfTextWebLink;
});