File size: 5,577 Bytes
1aeabca | 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 | "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.segmentGraphemes = exports.parseTextBounds = exports.TextBounds = void 0;
var css_line_break_1 = require("css-line-break");
var text_segmentation_1 = require("text-segmentation");
var bounds_1 = require("./bounds");
var features_1 = require("../../core/features");
var TextBounds = /** @class */ (function () {
function TextBounds(text, bounds) {
this.text = text;
this.bounds = bounds;
}
return TextBounds;
}());
exports.TextBounds = TextBounds;
var parseTextBounds = function (context, value, styles, node) {
var textList = breakText(value, styles);
var textBounds = [];
var offset = 0;
textList.forEach(function (text) {
if (styles.textDecorationLine.length || text.trim().length > 0) {
if (features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
var clientRects = createRange(node, offset, text.length).getClientRects();
if (clientRects.length > 1) {
var subSegments = exports.segmentGraphemes(text);
var subOffset_1 = 0;
subSegments.forEach(function (subSegment) {
textBounds.push(new TextBounds(subSegment, bounds_1.Bounds.fromDOMRectList(context, createRange(node, subOffset_1 + offset, subSegment.length).getClientRects())));
subOffset_1 += subSegment.length;
});
}
else {
textBounds.push(new TextBounds(text, bounds_1.Bounds.fromDOMRectList(context, clientRects)));
}
}
else {
var replacementNode = node.splitText(text.length);
textBounds.push(new TextBounds(text, getWrapperBounds(context, node)));
node = replacementNode;
}
}
else if (!features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
node = node.splitText(text.length);
}
offset += text.length;
});
return textBounds;
};
exports.parseTextBounds = parseTextBounds;
var getWrapperBounds = function (context, node) {
var ownerDocument = node.ownerDocument;
if (ownerDocument) {
var wrapper = ownerDocument.createElement('html2canvaswrapper');
wrapper.appendChild(node.cloneNode(true));
var parentNode = node.parentNode;
if (parentNode) {
parentNode.replaceChild(wrapper, node);
var bounds = bounds_1.parseBounds(context, wrapper);
if (wrapper.firstChild) {
parentNode.replaceChild(wrapper.firstChild, wrapper);
}
return bounds;
}
}
return bounds_1.Bounds.EMPTY;
};
var createRange = function (node, offset, length) {
var ownerDocument = node.ownerDocument;
if (!ownerDocument) {
throw new Error('Node has no owner document');
}
var range = ownerDocument.createRange();
range.setStart(node, offset);
range.setEnd(node, offset + length);
return range;
};
var segmentGraphemes = function (value) {
if (features_1.FEATURES.SUPPORT_NATIVE_TEXT_SEGMENTATION) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var segmenter = new Intl.Segmenter(void 0, { granularity: 'grapheme' });
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Array.from(segmenter.segment(value)).map(function (segment) { return segment.segment; });
}
return text_segmentation_1.splitGraphemes(value);
};
exports.segmentGraphemes = segmentGraphemes;
var segmentWords = function (value, styles) {
if (features_1.FEATURES.SUPPORT_NATIVE_TEXT_SEGMENTATION) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
var segmenter = new Intl.Segmenter(void 0, {
granularity: 'word'
});
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return Array.from(segmenter.segment(value)).map(function (segment) { return segment.segment; });
}
return breakWords(value, styles);
};
var breakText = function (value, styles) {
return styles.letterSpacing !== 0 ? exports.segmentGraphemes(value) : segmentWords(value, styles);
};
// https://drafts.csswg.org/css-text/#word-separator
var wordSeparators = [0x0020, 0x00a0, 0x1361, 0x10100, 0x10101, 0x1039, 0x1091];
var breakWords = function (str, styles) {
var breaker = css_line_break_1.LineBreaker(str, {
lineBreak: styles.lineBreak,
wordBreak: styles.overflowWrap === "break-word" /* BREAK_WORD */ ? 'break-word' : styles.wordBreak
});
var words = [];
var bk;
var _loop_1 = function () {
if (bk.value) {
var value = bk.value.slice();
var codePoints = css_line_break_1.toCodePoints(value);
var word_1 = '';
codePoints.forEach(function (codePoint) {
if (wordSeparators.indexOf(codePoint) === -1) {
word_1 += css_line_break_1.fromCodePoint(codePoint);
}
else {
if (word_1.length) {
words.push(word_1);
}
words.push(css_line_break_1.fromCodePoint(codePoint));
word_1 = '';
}
});
if (word_1.length) {
words.push(word_1);
}
}
};
while (!(bk = breaker.next()).done) {
_loop_1();
}
return words;
};
//# sourceMappingURL=text.js.map |