@antv/g-lite 2.2.10 → 2.2.11

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.esm.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.11
5
+ * @date 12/26/2024, 8:33:47 AM
6
6
  * @author AntVis
7
7
  * @docs https://g.antv.antgroup.com/
8
8
  */
@@ -11478,99 +11478,123 @@ var TextService = /*#__PURE__*/function () {
11478
11478
  } else if (textOverflow && textOverflow !== 'clip') {
11479
11479
  ellipsis = textOverflow;
11480
11480
  }
11481
+ var chars = Array.from(text);
11481
11482
  var lines = [];
11482
- var currentIndex = 0;
11483
- var currentWidth = 0;
11483
+ var currentLineIndex = 0;
11484
+ var currentLineWidth = 0;
11484
11485
  var cache = {};
11485
- var calcWidth = function calcWidth(_char2) {
11486
- return _this2.getFromCache(_char2, letterSpacing, cache, context);
11486
+ var calcWidth = function calcWidth(txt) {
11487
+ return _this2.getFromCache(txt, letterSpacing, cache, context);
11487
11488
  };
11488
- var ellipsisWidth = Array.from(ellipsis).reduce(function (prev, cur) {
11489
- return prev + calcWidth(cur);
11490
- }, 0);
11491
- function appendEllipsis(lineIndex) {
11489
+ var ellipsisWidth = calcWidth(ellipsis);
11490
+
11491
+ /**
11492
+ * Find text fragments that will take up as much of the given line width as possible when rendered.
11493
+ *
11494
+ * @see https://github.com/antvis/G/issues/1833
11495
+ *
11496
+ * @param txt - Current line of text
11497
+ * @param textCharIndex - The index of the last character of the current line in the entire text
11498
+ */
11499
+ function findCharIndexClosestWidthThreshold(txt, textCharIndex, widthThreshold) {
11500
+ while (calcWidth(txt) < widthThreshold && textCharIndex < chars.length - 1) {
11501
+ textCharIndex += 1;
11502
+ txt += chars[textCharIndex];
11503
+ }
11504
+ while (calcWidth(txt) > widthThreshold) {
11505
+ textCharIndex -= 1;
11506
+ txt = txt.slice(0, -1);
11507
+ }
11508
+ return {
11509
+ txt: txt,
11510
+ textCharIndex: textCharIndex
11511
+ };
11512
+ }
11513
+ function appendEllipsis(lineIndex, textCharIndex) {
11492
11514
  // If there is not enough space to display the string itself, it is clipped.
11493
11515
  // @see https://developer.mozilla.org/en-US/docs/Web/CSS/text-overflow#values
11494
11516
  if (ellipsisWidth <= 0 || ellipsisWidth > maxWidth) {
11495
11517
  return;
11496
11518
  }
11497
-
11498
- // Backspace from line's end.
11499
- var currentLineLength = lines[lineIndex] ? lines[lineIndex].length : 0;
11500
- var lastLineWidth = 0;
11501
- var lastLineIndex = currentLineLength;
11502
- for (var i = 0; i < currentLineLength; i++) {
11503
- var width = calcWidth(lines[lineIndex][i]);
11504
- if (lastLineWidth + width + ellipsisWidth > maxWidth) {
11505
- lastLineIndex = i;
11506
- break;
11507
- }
11508
- lastLineWidth += width;
11519
+ if (!lines[lineIndex]) {
11520
+ lines[lineIndex] = ellipsis;
11521
+ return;
11509
11522
  }
11510
- lines[lineIndex] = (lines[lineIndex] || '').slice(0, lastLineIndex) + ellipsis;
11523
+ var result = findCharIndexClosestWidthThreshold(lines[lineIndex], textCharIndex, maxWidth - ellipsisWidth);
11524
+ lines[lineIndex] = result.txt + ellipsis;
11511
11525
  }
11512
- var chars = Array.from(text);
11513
11526
  for (var i = 0; i < chars.length; i++) {
11514
- var _char3 = chars[i];
11515
- var prevChar = text[i - 1];
11516
- var nextChar = text[i + 1];
11517
- var charWidth = calcWidth(_char3);
11518
- if (this.isNewline(_char3)) {
11519
- currentIndex++;
11520
-
11527
+ var _char2 = chars[i];
11528
+ var prevChar = chars[i - 1];
11529
+ var nextChar = chars[i + 1];
11530
+ var charWidth = calcWidth(_char2);
11531
+ if (this.isNewline(_char2)) {
11521
11532
  // exceed maxLines, break immediately
11522
- if (currentIndex >= maxLines) {
11533
+ if (currentLineIndex + 1 >= maxLines) {
11523
11534
  parsedStyle.isOverflowing = true;
11524
11535
  if (i < chars.length - 1) {
11525
- appendEllipsis(currentIndex - 1);
11536
+ appendEllipsis(currentLineIndex, i);
11526
11537
  }
11527
11538
  break;
11528
11539
  }
11529
- currentWidth = 0;
11530
- lines[currentIndex] = '';
11540
+ currentLineIndex += 1;
11541
+ currentLineWidth = 0;
11542
+ lines[currentLineIndex] = '';
11531
11543
  continue;
11532
11544
  }
11533
- if (currentWidth > 0 && currentWidth + charWidth > maxWidth) {
11534
- if (currentIndex + 1 >= maxLines) {
11545
+ if (currentLineWidth > 0 && currentLineWidth + charWidth > maxWidth) {
11546
+ var result = findCharIndexClosestWidthThreshold(lines[currentLineIndex], i, maxWidth);
11547
+ if (result.textCharIndex !== i) {
11548
+ lines[currentLineIndex] = result.txt;
11549
+ if (result.textCharIndex === chars.length - 1) {
11550
+ break;
11551
+ }
11552
+ i = result.textCharIndex;
11553
+ _char2 = chars[i];
11554
+ prevChar = chars[i - 1];
11555
+ nextChar = chars[i + 1];
11556
+ charWidth = calcWidth(_char2);
11557
+ }
11558
+ if (currentLineIndex + 1 >= maxLines) {
11535
11559
  parsedStyle.isOverflowing = true;
11536
- appendEllipsis(currentIndex);
11560
+ appendEllipsis(currentLineIndex, i);
11537
11561
  break;
11538
11562
  }
11539
- currentIndex++;
11540
- currentWidth = 0;
11541
- lines[currentIndex] = '';
11542
- if (this.isBreakingSpace(_char3)) {
11563
+ currentLineIndex += 1;
11564
+ currentLineWidth = 0;
11565
+ lines[currentLineIndex] = '';
11566
+ if (this.isBreakingSpace(_char2)) {
11543
11567
  continue;
11544
11568
  }
11545
- if (!this.canBreakInLastChar(_char3)) {
11569
+ if (!this.canBreakInLastChar(_char2)) {
11546
11570
  lines = this.trimToBreakable(lines);
11547
- currentWidth = this.sumTextWidthByCache(lines[currentIndex] || '', cache);
11571
+ currentLineWidth = this.sumTextWidthByCache(lines[currentLineIndex] || '', cache);
11548
11572
  }
11549
- if (this.shouldBreakByKinsokuShorui(_char3, nextChar)) {
11573
+ if (this.shouldBreakByKinsokuShorui(_char2, nextChar)) {
11550
11574
  lines = this.trimByKinsokuShorui(lines);
11551
- currentWidth += calcWidth(prevChar || '');
11575
+ currentLineWidth += calcWidth(prevChar || '');
11552
11576
  }
11553
11577
  }
11554
- currentWidth += charWidth;
11555
- lines[currentIndex] = (lines[currentIndex] || '') + _char3;
11578
+ currentLineWidth += charWidth;
11579
+ lines[currentLineIndex] = (lines[currentLineIndex] || '') + _char2;
11556
11580
  }
11557
11581
  return lines.join('\n');
11558
11582
  }
11559
11583
  }, {
11560
11584
  key: "isBreakingSpace",
11561
- value: function isBreakingSpace(_char4) {
11562
- if (typeof _char4 !== 'string') {
11585
+ value: function isBreakingSpace(_char3) {
11586
+ if (typeof _char3 !== 'string') {
11563
11587
  return false;
11564
11588
  }
11565
- return TEXT_METRICS.BreakingSpaces.indexOf(_char4.charCodeAt(0)) >= 0;
11589
+ return TEXT_METRICS.BreakingSpaces.indexOf(_char3.charCodeAt(0)) >= 0;
11566
11590
  }
11567
11591
  }, {
11568
11592
  key: "isNewline",
11569
- value: function isNewline(_char5) {
11570
- if (typeof _char5 !== 'string') {
11593
+ value: function isNewline(_char4) {
11594
+ if (typeof _char4 !== 'string') {
11571
11595
  return false;
11572
11596
  }
11573
- return TEXT_METRICS.Newlines.indexOf(_char5.charCodeAt(0)) >= 0;
11597
+ return TEXT_METRICS.Newlines.indexOf(_char4.charCodeAt(0)) >= 0;
11574
11598
  }
11575
11599
  }, {
11576
11600
  key: "trimToBreakable",
@@ -11589,8 +11613,8 @@ var TextService = /*#__PURE__*/function () {
11589
11613
  }
11590
11614
  }, {
11591
11615
  key: "canBreakInLastChar",
11592
- value: function canBreakInLastChar(_char6) {
11593
- if (_char6 && LATIN_REGEX.test(_char6)) return false;
11616
+ value: function canBreakInLastChar(_char5) {
11617
+ if (_char5 && LATIN_REGEX.test(_char5)) return false;
11594
11618
  return true;
11595
11619
  }
11596
11620
  }, {
@@ -11615,7 +11639,8 @@ var TextService = /*#__PURE__*/function () {
11615
11639
  var width = cache[key];
11616
11640
  if (typeof width !== 'number') {
11617
11641
  var spacing = key.length * letterSpacing;
11618
- width = context.measureText(key).width + spacing;
11642
+ var metrics = context.measureText(key);
11643
+ width = metrics.width + spacing;
11619
11644
  cache[key] = width;
11620
11645
  }
11621
11646
  return width;