@antv/g-lite 2.2.10 → 2.2.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  /*!
2
2
  * @antv/g-lite
3
3
  * @description A core module for rendering engine implements DOM API.
4
- * @version 2.2.10
5
- * @date 12/20/2024, 6:51:24 AM
4
+ * @version 2.2.12
5
+ * @date 12/26/2024, 12:57:25 PM
6
6
  * @author AntVis
7
7
  * @docs https://g.antv.antgroup.com/
8
8
  */
@@ -11499,99 +11499,123 @@ var TextService = /*#__PURE__*/function () {
11499
11499
  } else if (textOverflow && textOverflow !== 'clip') {
11500
11500
  ellipsis = textOverflow;
11501
11501
  }
11502
+ var chars = Array.from(text);
11502
11503
  var lines = [];
11503
- var currentIndex = 0;
11504
- var currentWidth = 0;
11504
+ var currentLineIndex = 0;
11505
+ var currentLineWidth = 0;
11505
11506
  var cache = {};
11506
- var calcWidth = function calcWidth(_char2) {
11507
- return _this2.getFromCache(_char2, letterSpacing, cache, context);
11507
+ var calcWidth = function calcWidth(txt) {
11508
+ return _this2.getFromCache(txt, letterSpacing, cache, context);
11508
11509
  };
11509
- var ellipsisWidth = Array.from(ellipsis).reduce(function (prev, cur) {
11510
- return prev + calcWidth(cur);
11511
- }, 0);
11512
- function appendEllipsis(lineIndex) {
11510
+ var ellipsisWidth = calcWidth(ellipsis);
11511
+
11512
+ /**
11513
+ * Find text fragments that will take up as much of the given line width as possible when rendered.
11514
+ *
11515
+ * @see https://github.com/antvis/G/issues/1833
11516
+ *
11517
+ * @param txt - Current line of text
11518
+ * @param textCharIndex - The index of the last character of the current line in the entire text
11519
+ */
11520
+ function findCharIndexClosestWidthThreshold(txt, textCharIndex, widthThreshold) {
11521
+ while (calcWidth(txt) < widthThreshold && textCharIndex < chars.length - 1) {
11522
+ textCharIndex += 1;
11523
+ txt += chars[textCharIndex];
11524
+ }
11525
+ while (calcWidth(txt) > widthThreshold && textCharIndex > 0) {
11526
+ textCharIndex -= 1;
11527
+ txt = txt.slice(0, -1);
11528
+ }
11529
+ return {
11530
+ txt: txt,
11531
+ textCharIndex: textCharIndex
11532
+ };
11533
+ }
11534
+ function appendEllipsis(lineIndex, textCharIndex) {
11513
11535
  // If there is not enough space to display the string itself, it is clipped.
11514
11536
  // @see https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow#values
11515
11537
  if (ellipsisWidth <= 0 || ellipsisWidth > maxWidth) {
11516
11538
  return;
11517
11539
  }
11518
-
11519
- // Backspace from line's end.
11520
- var currentLineLength = lines[lineIndex] ? lines[lineIndex].length : 0;
11521
- var lastLineWidth = 0;
11522
- var lastLineIndex = currentLineLength;
11523
- for (var i = 0; i < currentLineLength; i++) {
11524
- var width = calcWidth(lines[lineIndex][i]);
11525
- if (lastLineWidth + width + ellipsisWidth > maxWidth) {
11526
- lastLineIndex = i;
11527
- break;
11528
- }
11529
- lastLineWidth += width;
11540
+ if (!lines[lineIndex]) {
11541
+ lines[lineIndex] = ellipsis;
11542
+ return;
11530
11543
  }
11531
- lines[lineIndex] = (lines[lineIndex] || '').slice(0, lastLineIndex) + ellipsis;
11544
+ var result = findCharIndexClosestWidthThreshold(lines[lineIndex], textCharIndex, maxWidth - ellipsisWidth);
11545
+ lines[lineIndex] = result.txt + ellipsis;
11532
11546
  }
11533
- var chars = Array.from(text);
11534
11547
  for (var i = 0; i < chars.length; i++) {
11535
- var _char3 = chars[i];
11536
- var prevChar = text[i - 1];
11537
- var nextChar = text[i + 1];
11538
- var charWidth = calcWidth(_char3);
11539
- if (this.isNewline(_char3)) {
11540
- currentIndex++;
11541
-
11548
+ var _char2 = chars[i];
11549
+ var prevChar = chars[i - 1];
11550
+ var nextChar = chars[i + 1];
11551
+ var charWidth = calcWidth(_char2);
11552
+ if (this.isNewline(_char2)) {
11542
11553
  // exceed maxLines, break immediately
11543
- if (currentIndex >= maxLines) {
11554
+ if (currentLineIndex + 1 >= maxLines) {
11544
11555
  parsedStyle.isOverflowing = true;
11545
11556
  if (i < chars.length - 1) {
11546
- appendEllipsis(currentIndex - 1);
11557
+ appendEllipsis(currentLineIndex, i - 1);
11547
11558
  }
11548
11559
  break;
11549
11560
  }
11550
- currentWidth = 0;
11551
- lines[currentIndex] = '';
11561
+ currentLineIndex += 1;
11562
+ currentLineWidth = 0;
11563
+ lines[currentLineIndex] = '';
11552
11564
  continue;
11553
11565
  }
11554
- if (currentWidth > 0 && currentWidth + charWidth > maxWidth) {
11555
- if (currentIndex + 1 >= maxLines) {
11566
+ if (currentLineWidth > 0 && currentLineWidth + charWidth > maxWidth) {
11567
+ var result = findCharIndexClosestWidthThreshold(lines[currentLineIndex], i - 1, maxWidth);
11568
+ if (result.textCharIndex !== i - 1) {
11569
+ lines[currentLineIndex] = result.txt;
11570
+ if (result.textCharIndex === chars.length - 1) {
11571
+ break;
11572
+ }
11573
+ i = result.textCharIndex + 1;
11574
+ _char2 = chars[i];
11575
+ prevChar = chars[i - 1];
11576
+ nextChar = chars[i + 1];
11577
+ charWidth = calcWidth(_char2);
11578
+ }
11579
+ if (currentLineIndex + 1 >= maxLines) {
11556
11580
  parsedStyle.isOverflowing = true;
11557
- appendEllipsis(currentIndex);
11581
+ appendEllipsis(currentLineIndex, i - 1);
11558
11582
  break;
11559
11583
  }
11560
- currentIndex++;
11561
- currentWidth = 0;
11562
- lines[currentIndex] = '';
11563
- if (this.isBreakingSpace(_char3)) {
11584
+ currentLineIndex += 1;
11585
+ currentLineWidth = 0;
11586
+ lines[currentLineIndex] = '';
11587
+ if (this.isBreakingSpace(_char2)) {
11564
11588
  continue;
11565
11589
  }
11566
- if (!this.canBreakInLastChar(_char3)) {
11590
+ if (!this.canBreakInLastChar(_char2)) {
11567
11591
  lines = this.trimToBreakable(lines);
11568
- currentWidth = this.sumTextWidthByCache(lines[currentIndex] || '', cache);
11592
+ currentLineWidth = this.sumTextWidthByCache(lines[currentLineIndex] || '', cache);
11569
11593
  }
11570
- if (this.shouldBreakByKinsokuShorui(_char3, nextChar)) {
11594
+ if (this.shouldBreakByKinsokuShorui(_char2, nextChar)) {
11571
11595
  lines = this.trimByKinsokuShorui(lines);
11572
- currentWidth += calcWidth(prevChar || '');
11596
+ currentLineWidth += calcWidth(prevChar || '');
11573
11597
  }
11574
11598
  }
11575
- currentWidth += charWidth;
11576
- lines[currentIndex] = (lines[currentIndex] || '') + _char3;
11599
+ currentLineWidth += charWidth;
11600
+ lines[currentLineIndex] = (lines[currentLineIndex] || '') + _char2;
11577
11601
  }
11578
11602
  return lines.join('\n');
11579
11603
  }
11580
11604
  }, {
11581
11605
  key: "isBreakingSpace",
11582
- value: function isBreakingSpace(_char4) {
11583
- if (typeof _char4 !== 'string') {
11606
+ value: function isBreakingSpace(_char3) {
11607
+ if (typeof _char3 !== 'string') {
11584
11608
  return false;
11585
11609
  }
11586
- return TEXT_METRICS.BreakingSpaces.indexOf(_char4.charCodeAt(0)) >= 0;
11610
+ return TEXT_METRICS.BreakingSpaces.indexOf(_char3.charCodeAt(0)) >= 0;
11587
11611
  }
11588
11612
  }, {
11589
11613
  key: "isNewline",
11590
- value: function isNewline(_char5) {
11591
- if (typeof _char5 !== 'string') {
11614
+ value: function isNewline(_char4) {
11615
+ if (typeof _char4 !== 'string') {
11592
11616
  return false;
11593
11617
  }
11594
- return TEXT_METRICS.Newlines.indexOf(_char5.charCodeAt(0)) >= 0;
11618
+ return TEXT_METRICS.Newlines.indexOf(_char4.charCodeAt(0)) >= 0;
11595
11619
  }
11596
11620
  }, {
11597
11621
  key: "trimToBreakable",
@@ -11610,8 +11634,8 @@ var TextService = /*#__PURE__*/function () {
11610
11634
  }
11611
11635
  }, {
11612
11636
  key: "canBreakInLastChar",
11613
- value: function canBreakInLastChar(_char6) {
11614
- if (_char6 && LATIN_REGEX.test(_char6)) return false;
11637
+ value: function canBreakInLastChar(_char5) {
11638
+ if (_char5 && LATIN_REGEX.test(_char5)) return false;
11615
11639
  return true;
11616
11640
  }
11617
11641
  }, {
@@ -11636,7 +11660,8 @@ var TextService = /*#__PURE__*/function () {
11636
11660
  var width = cache[key];
11637
11661
  if (typeof width !== 'number') {
11638
11662
  var spacing = key.length * letterSpacing;
11639
- width = context.measureText(key).width + spacing;
11663
+ var metrics = context.measureText(key);
11664
+ width = metrics.width + spacing;
11640
11665
  cache[key] = width;
11641
11666
  }
11642
11667
  return width;