1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | exports.segmentGraphemes = exports.parseTextBounds = exports.TextBounds = void 0;
|
---|
4 | var css_line_break_1 = require("css-line-break");
|
---|
5 | var text_segmentation_1 = require("text-segmentation");
|
---|
6 | var bounds_1 = require("./bounds");
|
---|
7 | var features_1 = require("../../core/features");
|
---|
8 | var TextBounds = /** @class */ (function () {
|
---|
9 | function TextBounds(text, bounds) {
|
---|
10 | this.text = text;
|
---|
11 | this.bounds = bounds;
|
---|
12 | }
|
---|
13 | return TextBounds;
|
---|
14 | }());
|
---|
15 | exports.TextBounds = TextBounds;
|
---|
16 | var parseTextBounds = function (context, value, styles, node) {
|
---|
17 | var textList = breakText(value, styles);
|
---|
18 | var textBounds = [];
|
---|
19 | var offset = 0;
|
---|
20 | textList.forEach(function (text) {
|
---|
21 | if (styles.textDecorationLine.length || text.trim().length > 0) {
|
---|
22 | if (features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
|
---|
23 | var clientRects = createRange(node, offset, text.length).getClientRects();
|
---|
24 | if (clientRects.length > 1) {
|
---|
25 | var subSegments = exports.segmentGraphemes(text);
|
---|
26 | var subOffset_1 = 0;
|
---|
27 | subSegments.forEach(function (subSegment) {
|
---|
28 | textBounds.push(new TextBounds(subSegment, bounds_1.Bounds.fromDOMRectList(context, createRange(node, subOffset_1 + offset, subSegment.length).getClientRects())));
|
---|
29 | subOffset_1 += subSegment.length;
|
---|
30 | });
|
---|
31 | }
|
---|
32 | else {
|
---|
33 | textBounds.push(new TextBounds(text, bounds_1.Bounds.fromDOMRectList(context, clientRects)));
|
---|
34 | }
|
---|
35 | }
|
---|
36 | else {
|
---|
37 | var replacementNode = node.splitText(text.length);
|
---|
38 | textBounds.push(new TextBounds(text, getWrapperBounds(context, node)));
|
---|
39 | node = replacementNode;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | else if (!features_1.FEATURES.SUPPORT_RANGE_BOUNDS) {
|
---|
43 | node = node.splitText(text.length);
|
---|
44 | }
|
---|
45 | offset += text.length;
|
---|
46 | });
|
---|
47 | return textBounds;
|
---|
48 | };
|
---|
49 | exports.parseTextBounds = parseTextBounds;
|
---|
50 | var getWrapperBounds = function (context, node) {
|
---|
51 | var ownerDocument = node.ownerDocument;
|
---|
52 | if (ownerDocument) {
|
---|
53 | var wrapper = ownerDocument.createElement('html2canvaswrapper');
|
---|
54 | wrapper.appendChild(node.cloneNode(true));
|
---|
55 | var parentNode = node.parentNode;
|
---|
56 | if (parentNode) {
|
---|
57 | parentNode.replaceChild(wrapper, node);
|
---|
58 | var bounds = bounds_1.parseBounds(context, wrapper);
|
---|
59 | if (wrapper.firstChild) {
|
---|
60 | parentNode.replaceChild(wrapper.firstChild, wrapper);
|
---|
61 | }
|
---|
62 | return bounds;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | return bounds_1.Bounds.EMPTY;
|
---|
66 | };
|
---|
67 | var createRange = function (node, offset, length) {
|
---|
68 | var ownerDocument = node.ownerDocument;
|
---|
69 | if (!ownerDocument) {
|
---|
70 | throw new Error('Node has no owner document');
|
---|
71 | }
|
---|
72 | var range = ownerDocument.createRange();
|
---|
73 | range.setStart(node, offset);
|
---|
74 | range.setEnd(node, offset + length);
|
---|
75 | return range;
|
---|
76 | };
|
---|
77 | var segmentGraphemes = function (value) {
|
---|
78 | if (features_1.FEATURES.SUPPORT_NATIVE_TEXT_SEGMENTATION) {
|
---|
79 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
80 | var segmenter = new Intl.Segmenter(void 0, { granularity: 'grapheme' });
|
---|
81 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
82 | return Array.from(segmenter.segment(value)).map(function (segment) { return segment.segment; });
|
---|
83 | }
|
---|
84 | return text_segmentation_1.splitGraphemes(value);
|
---|
85 | };
|
---|
86 | exports.segmentGraphemes = segmentGraphemes;
|
---|
87 | var segmentWords = function (value, styles) {
|
---|
88 | if (features_1.FEATURES.SUPPORT_NATIVE_TEXT_SEGMENTATION) {
|
---|
89 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
90 | var segmenter = new Intl.Segmenter(void 0, {
|
---|
91 | granularity: 'word'
|
---|
92 | });
|
---|
93 | // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
---|
94 | return Array.from(segmenter.segment(value)).map(function (segment) { return segment.segment; });
|
---|
95 | }
|
---|
96 | return breakWords(value, styles);
|
---|
97 | };
|
---|
98 | var breakText = function (value, styles) {
|
---|
99 | return styles.letterSpacing !== 0 ? exports.segmentGraphemes(value) : segmentWords(value, styles);
|
---|
100 | };
|
---|
101 | // https://drafts.csswg.org/css-text/#word-separator
|
---|
102 | var wordSeparators = [0x0020, 0x00a0, 0x1361, 0x10100, 0x10101, 0x1039, 0x1091];
|
---|
103 | var breakWords = function (str, styles) {
|
---|
104 | var breaker = css_line_break_1.LineBreaker(str, {
|
---|
105 | lineBreak: styles.lineBreak,
|
---|
106 | wordBreak: styles.overflowWrap === "break-word" /* BREAK_WORD */ ? 'break-word' : styles.wordBreak
|
---|
107 | });
|
---|
108 | var words = [];
|
---|
109 | var bk;
|
---|
110 | var _loop_1 = function () {
|
---|
111 | if (bk.value) {
|
---|
112 | var value = bk.value.slice();
|
---|
113 | var codePoints = css_line_break_1.toCodePoints(value);
|
---|
114 | var word_1 = '';
|
---|
115 | codePoints.forEach(function (codePoint) {
|
---|
116 | if (wordSeparators.indexOf(codePoint) === -1) {
|
---|
117 | word_1 += css_line_break_1.fromCodePoint(codePoint);
|
---|
118 | }
|
---|
119 | else {
|
---|
120 | if (word_1.length) {
|
---|
121 | words.push(word_1);
|
---|
122 | }
|
---|
123 | words.push(css_line_break_1.fromCodePoint(codePoint));
|
---|
124 | word_1 = '';
|
---|
125 | }
|
---|
126 | });
|
---|
127 | if (word_1.length) {
|
---|
128 | words.push(word_1);
|
---|
129 | }
|
---|
130 | }
|
---|
131 | };
|
---|
132 | while (!(bk = breaker.next()).done) {
|
---|
133 | _loop_1();
|
---|
134 | }
|
---|
135 | return words;
|
---|
136 | };
|
---|
137 | //# sourceMappingURL=text.js.map |
---|