all files / pdfviewer/accessibility-tags/ accessibility-tags.js

16.44% Statements 24/146
0% Branches 0/87
35.29% Functions 6/17
16.44% Lines 24/146
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 2331×   1× 1× 1× 2×                                                                                     2× 2×   1×                                           1×                                 1×                                                                                                                                                                   1×                                     1×     1×     1× 1×   1×   1× 1× 1×   1×   1× 1×                                     1×   1×    
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var AccessibilityTags = (function () {
        function AccessibilityTags(pdfViewer, pdfViewerBase) {
            this.createTag = function (taggedTextResponse) {
                var _this = this;
                var tagType = taggedTextResponse.TagType;
                var parentTagType = taggedTextResponse.ParentTagType;
                var text = taggedTextResponse.Text;
                var altText = taggedTextResponse.AltText;
                var bounds = taggedTextResponse.Bounds;
                var childTags = taggedTextResponse.ChildElements;
                var textTag = document.createElement(this.getTag(tagType));
                textTag.style.padding = '0px';
                textTag.style.margin = '0px';
                if (parentTagType !== 'Document' && parentTagType !== 'Part') {
                    textTag.style.position = 'absolute';
                }
                if (bounds) {
                    this.setStyleToTaggedTextDiv(textTag, bounds, taggedTextResponse.FontSize, taggedTextResponse.FontName, taggedTextResponse.FontStyle);
                    this.setTextElementProperties(textTag);
                }
                if (text.trim() !== '') {
                    textTag.innerText = text;
                }
                if (tagType === 'Image' || tagType === 'Figure') {
                    if (altText && altText.trim() !== '') {
                        textTag.alt = altText;
                    }
                    textTag.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNgYAAAAAMAASsJTYQAAAAASUVORK5CYII=';
                }
                if (childTags && childTags.length > 0) {
                    childTags.forEach(function (element) {
                        if (tagType === 'Table') {
                            if (element.ChildElements) {
                                element.ChildElements.forEach(function (newelement) {
                                    textTag.appendChild(_this.createTag(newelement));
                                });
                            }
                        }
                        else {
                            textTag.appendChild(_this.createTag(element));
                        }
                    });
                }
                return textTag;
            };
            this.pdfViewer = pdfViewer;
            this.pdfViewerBase = pdfViewerBase;
        }
        AccessibilityTags.prototype.addTaggedLayer = function (pageIndex) {
            var taggedLayer;
            if (this.pdfViewer.enableAccessibilityTags && this.pdfViewerBase.isTaggedPdf) {
                var pageDiv = document.getElementById(this.pdfViewer.element.id + '_pageDiv_' + pageIndex);
                taggedLayer = document.getElementById(this.pdfViewer.element.id + '_taggedLayer_' + pageIndex);
                var textLayer = document.getElementById(this.pdfViewer.element.id + '_textLayer_' + pageIndex);
                if (textLayer) {
                    textLayer.setAttribute('aria-hidden', 'true');
                }
                if (!taggedLayer) {
                    taggedLayer = ej2_base_1.createElement('div', { id: this.pdfViewer.element.id + '_taggedLayer_' + pageIndex, className: 'e-pv-tagged-layer e-pv-text-layer' });
                }
                taggedLayer.innerHTML = '';
                taggedLayer.style.width = this.pdfViewerBase.pageSize[parseInt(pageIndex.toString(), 10)].width * this.pdfViewerBase.getZoomFactor() + 'px';
                taggedLayer.style.height = this.pdfViewerBase.pageSize[parseInt(pageIndex.toString(), 10)].height * this.pdfViewerBase.getZoomFactor() + 'px';
                taggedLayer.style.pointerEvents = 'none';
                if (pageDiv) {
                    pageDiv.appendChild(taggedLayer);
                }
            }
            return taggedLayer;
        };
        AccessibilityTags.prototype.renderAccessibilityTags = function (pageIndex, taggedTextResponse) {
            var taggedLayer = this.addTaggedLayer(pageIndex);
            for (var i = 0; i < taggedTextResponse.length; i++) {
                var textDiv = ej2_base_1.createElement('div', { id: this.pdfViewer.element.id + '_taggedText_' + pageIndex + '_' + parseInt(i.toString(), 10), className: 'e-pv-text', attrs: { 'tabindex': '-1' } });
                var bounds = taggedTextResponse[parseInt(i.toString(), 10)].Bounds;
                if (taggedTextResponse[parseInt(parseInt(i.toString(), 10).toString(), 10)].TagType === 'Paragraph' && taggedTextResponse[parseInt(i.toString(), 10)].ChildElements === null && taggedTextResponse[parseInt(i.toString(), 10)].Text.trim() === '') {
                    continue;
                }
                else {
                    textDiv.appendChild(this.createTag(taggedTextResponse[parseInt(i.toString(), 10)]));
                }
                textDiv.style.display = 'inline';
                this.setStyleToTaggedTextDiv(textDiv, bounds, taggedTextResponse[parseInt(i.toString(), 10)].FontSize, taggedTextResponse[parseInt(i.toString(), 10)].FontName, taggedTextResponse[parseInt(i.toString(), 10)].FontStyle);
                this.setTextElementProperties(textDiv);
                taggedLayer.appendChild(textDiv);
            }
        };
        AccessibilityTags.prototype.getTag = function (tagType) {
            switch (tagType) {
                case 'Paragraph':
                    return 'p';
                case 'Figure':
                    return 'img';
                case 'Article':
                    return 'art';
                case 'Annotation':
                    return 'annot';
                case 'BibliographyEntry':
                    return 'bibentry';
                case 'BlockQuotation':
                    return 'blockQuote';
                case 'Caption':
                    return 'caption';
                case 'Code':
                    return 'code';
                case 'Division':
                    return 'div';
                case 'Document':
                    return 'document';
                case 'Form':
                    return 'form';
                case 'Formula':
                    return 'formula';
                case 'Index':
                    return 'index';
                case 'Heading':
                    return 'h';
                case 'HeadingLevel1':
                    return 'h1';
                case 'HeadingLevel2':
                    return 'h2';
                case 'HeadingLevel3':
                    return 'h3';
                case 'HeadingLevel4':
                    return 'h4';
                case 'HeadingLevel5':
                    return 'h5';
                case 'HeadingLevel6':
                    return 'h6';
                case 'Label':
                    return 'label';
                case 'Link':
                    return 'a';
                case 'List':
                    return 'ul';
                case 'ListItem':
                    return 'li';
                case 'ListBody':
                    return 'p';
                case 'Note':
                    return 'note';
                case 'Part':
                    return 'part';
                case 'Quotation':
                    return 'quote';
                case 'Reference':
                    return 'reference';
                case 'Section':
                    return 'sect';
                case 'Span':
                    return 'span';
                case 'Table':
                    return 'table';
                case 'TableDataCell':
                    return 'td';
                case 'TableHeader':
                    return 'th';
                case 'TableOfContent':
                    return 'toc';
                case 'TableOfContentItem':
                    return 'toci';
                case 'TableRow':
                    return 'tr';
                case 'Image':
                    return 'img';
                default:
                    return 'p';
            }
        };
        AccessibilityTags.prototype.setStyleToTaggedTextDiv = function (textDiv, bounds, fontSize, fontName, fontStyle) {
            var zoomFactor = this.pdfViewerBase.getZoomFactor();
            textDiv.style.left = this.pdfViewerBase.ConvertPointToPixel(bounds.X) * zoomFactor + 'px';
            textDiv.style.top = this.pdfViewerBase.ConvertPointToPixel(bounds.Y) * zoomFactor + 'px';
            textDiv.style.width = this.pdfViewerBase.ConvertPointToPixel(bounds.Width) * zoomFactor + 'px';
            var textHeight = this.pdfViewerBase.ConvertPointToPixel(bounds.Height) * zoomFactor;
            textDiv.style.height = textHeight + 'px';
            textDiv.style.fontSize = this.pdfViewerBase.ConvertPointToPixel(fontSize) * zoomFactor + 'px';
            if (fontName && (fontName === 'Wingdings' || fontName === 'Symbol')) {
                textDiv.style.fontFamily = 'Verdana';
            }
            else if (fontName) {
                textDiv.style.fontFamily = fontName;
            }
            if (fontStyle) {
                textDiv.style.fontWeight = fontStyle;
            }
            textDiv.style.color = 'transparent';
        };
        AccessibilityTags.prototype.setTextElementProperties = function (textDiv) {
            textDiv.style.transformOrigin = '0%';
        };
        AccessibilityTags.prototype.getModuleName = function () {
            return 'AccessibilityTags';
        };
        AccessibilityTags.prototype.destroy = function () {
            return true;
        };
        return AccessibilityTags;
    }());
    exports.AccessibilityTags = AccessibilityTags;
    var TaggedElements = (function () {
        function TaggedElements() {
        }
        return TaggedElements;
    }());
    var RectBounds = (function () {
        function RectBounds(_X, _Y, _Width, _Height) {
            this.X = _X;
            this.Y = _Y;
            this.Width = _Width;
            this.Height = _Height;
            this.Location = {
                X: _X,
                Y: _Y
            };
            this.Size = {
                IsEmpty: false,
                Width: _Width,
                Height: _Height
            };
            this.Left = _X;
            this.Top = _Y;
            this.Right = _X + _Width;
            this.Bottom = _Y + _Height;
        }
        return RectBounds;
    }());
    exports.RectBounds = RectBounds;
});