all files / document-editor/implementation/text-helper/ optimized.js

100% Statements 55/55
91.67% Branches 22/24
100% Functions 9/9
100% Lines 55/55
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   772× 772×   14773×   1014× 1014× 64×   1014× 25×   1014×   616× 616× 616×         616× 616×       616× 616× 616× 616×   616× 616× 616× 616× 616× 616× 616× 616× 616× 616× 616×   1014× 1014× 1014× 616× 616× 616× 616×     398× 398× 398×     1024× 1024×        
define(["require", "exports", "@syncfusion/ej2-base"], function (require, exports, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Optimized = (function () {
        function Optimized(documentHelper) {
            this.documentHelper = documentHelper;
            this.optimizedHeightCollection = {};
        }
        Optimized.prototype.getModuleName = function () {
            return 'Optimized';
        };
        Optimized.prototype.getkeyFromCharFormat = function (characterFormat) {
            var formatText = characterFormat.fontFamily.toLocaleLowerCase();
            if (characterFormat.bold) {
                formatText += ';' + 'bold';
            }
            if (characterFormat.italic) {
                formatText += ';' + 'italic';
            }
            return formatText;
        };
        Optimized.prototype.getFontInfo = function (characterFormat) {
            var iframe = ej2_base_1.createElement('iframe');
            document.body.appendChild(iframe);
            var innerHtml = '<!DOCTYPE html>'
                + '<html><head></head>'
                + '<body>'
                + '</body>'
                + '</html>';
            var externalFonts = this.documentHelper.owner.externalFonts;
            if (externalFonts && externalFonts.length > 0) {
                externalFonts.forEach(function (externalFont) {
                    Eif (externalFont.src) {
                        innerHtml += '<style>@font-face {font-family: \'' + externalFont.fontFamily + '\'; src: ' + externalFont.src + '; }</style>';
                    }
                });
            }
            Eif (!ej2_base_1.isNullOrUndefined(iframe.contentDocument)) {
                iframe.contentDocument.open();
                iframe.contentDocument.write(innerHtml);
                iframe.contentDocument.close();
            }
            var container = document.createElement('div');
            container.setAttribute('style', 'position:absolute;top:-1000px;left:-1000px;opacity:0;font-size:0px;line-height:normal;');
            var maxFontHeight = 288;
            var factor = 1.0 / window.devicePixelRatio;
            container.style.transform = 'scale(' + factor.toString() + ',' + factor.toString() + ')';
            container.innerHTML = '<span class="e-de-font-info" style="font-size:0; font-family: ' + ("'" + characterFormat.fontFamily + "'") + '; display: inline-block;">m</span><span class="e-de-font-info" style="font-size:' + maxFontHeight + 'pt; font-family: ' + ("'" + characterFormat.fontFamily + "'") + ';' + ((characterFormat.bold) ? 'font-weight:bold;' : '') + ((characterFormat.italic) ? 'font-style:italic;' : '') + ' display: inline-block;">m</span>';
            iframe.contentDocument.body.appendChild(container);
            var baseLineFactor = container.firstChild.offsetTop / container.lastChild.offsetHeight;
            var heightFactor = parseFloat((container.lastChild.offsetHeight / maxFontHeight).toFixed(2));
            document.body.removeChild(iframe);
            return { HeightFactor: heightFactor, BaselineFactor: baseLineFactor };
        };
        Optimized.prototype.getHeightInternal = function (characterFormat) {
            var key = this.getkeyFromCharFormat(characterFormat);
            var isBidi = characterFormat.bidi || characterFormat.complexScript;
            if (ej2_base_1.isNullOrUndefined(this.optimizedHeightCollection["" + key])) {
                var fontInfo = this.getFontInfo(characterFormat);
                this.optimizedHeightCollection["" + key] = fontInfo;
                var fontHeight = fontInfo.HeightFactor * (isBidi ? characterFormat.fontSizeBidi : characterFormat.fontSize);
                return { Height: fontHeight, BaselineOffset: fontInfo.BaselineFactor * fontHeight };
            }
            else {
                var fontSizeInfo = this.optimizedHeightCollection["" + key];
                var fontHeight = fontSizeInfo.HeightFactor * (isBidi ? characterFormat.fontSizeBidi : characterFormat.fontSize);
                return { Height: fontHeight, BaselineOffset: fontSizeInfo.BaselineFactor * fontHeight };
            }
        };
        Optimized.prototype.destroy = function () {
            this.documentHelper = undefined;
            this.optimizedHeightCollection = undefined;
        };
        return Optimized;
    }());
    exports.Optimized = Optimized;
});