@antv/g-lite 2.3.1 → 2.3.2

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.3.1
5
- * @date 7/18/2025, 7:12:41 AM
4
+ * @version 2.3.2
5
+ * @date 7/30/2025, 1:34:19 PM
6
6
  * @author AntVis
7
7
  * @docs https://g.antv.antgroup.com/
8
8
  */
@@ -7988,7 +7988,7 @@ var SyncWaterfallHook = /*#__PURE__*/function () {
7988
7988
 
7989
7989
  var genericFontFamilies = ['serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui'];
7990
7990
  var stringRegExp = /([\"\'])[^\'\"]+\1/;
7991
- function toFontString(attributes) {
7991
+ function getFontAttr(attributes) {
7992
7992
  var _attributes$fontSize = attributes.fontSize,
7993
7993
  fontSize = _attributes$fontSize === void 0 ? 16 : _attributes$fontSize,
7994
7994
  _attributes$fontFamil = attributes.fontFamily,
@@ -7999,6 +7999,21 @@ function toFontString(attributes) {
7999
7999
  fontVariant = _attributes$fontVaria === void 0 ? 'normal' : _attributes$fontVaria,
8000
8000
  _attributes$fontWeigh = attributes.fontWeight,
8001
8001
  fontWeight = _attributes$fontWeigh === void 0 ? 'normal' : _attributes$fontWeigh;
8002
+ return {
8003
+ fontSize: fontSize,
8004
+ fontFamily: fontFamily,
8005
+ fontStyle: fontStyle,
8006
+ fontVariant: fontVariant,
8007
+ fontWeight: fontWeight
8008
+ };
8009
+ }
8010
+ var toFontString = memoize(function toFontStringRaw(attributes) {
8011
+ var _getFontAttr = getFontAttr(attributes),
8012
+ fontSize = _getFontAttr.fontSize,
8013
+ fontFamily = _getFontAttr.fontFamily,
8014
+ fontStyle = _getFontAttr.fontStyle,
8015
+ fontVariant = _getFontAttr.fontVariant,
8016
+ fontWeight = _getFontAttr.fontWeight;
8002
8017
 
8003
8018
  // build canvas api font setting from individual components. Convert a numeric this.fontSize to px
8004
8019
  // const fontSizeString: string = isNumber(fontSize) ? `${fontSize}px` : fontSize.toString();
@@ -8017,8 +8032,27 @@ function toFontString(attributes) {
8017
8032
  fontFamilies[i] = _fontFamily;
8018
8033
  }
8019
8034
  return "".concat(fontStyle, " ").concat(fontVariant, " ").concat(fontWeight, " ").concat(fontSizeString, " ").concat(fontFamilies.join(','));
8020
- }
8035
+ }, function (attributes) {
8036
+ var _getFontAttr2 = getFontAttr(attributes),
8037
+ fontSize = _getFontAttr2.fontSize,
8038
+ fontFamily = _getFontAttr2.fontFamily,
8039
+ fontStyle = _getFontAttr2.fontStyle,
8040
+ fontVariant = _getFontAttr2.fontVariant,
8041
+ fontWeight = _getFontAttr2.fontWeight;
8042
+ return "".concat(fontStyle, "_").concat(fontVariant, "_").concat(fontWeight, "_").concat(fontSize, "_").concat(fontFamily);
8043
+ });
8021
8044
 
8045
+ /**
8046
+ * Minimum scaling limit
8047
+ *
8048
+ * When `scale` is 0, the matrix will become an irreversible matrix,
8049
+ * and `NaN` will appear during matrix calculation and decomposition,
8050
+ * causing exceptions
8051
+ */
8052
+ var MIN_SCALE = 0.000001;
8053
+ var clampScale = function clampScale(item) {
8054
+ return Math.max(item, MIN_SCALE);
8055
+ };
8022
8056
  function createSkewMatrix(skewMatrix, skewX, skewY) {
8023
8057
  // Create an identity matrix
8024
8058
  glMatrix.mat4.identity(skewMatrix);
@@ -8032,19 +8066,29 @@ var $mat4_1 = glMatrix.mat4.create();
8032
8066
  var $mat4_2 = glMatrix.mat4.create();
8033
8067
  var parser = {
8034
8068
  scale: function scale(d) {
8035
- glMatrix.mat4.fromScaling($mat4_1, [d[0].value, d[1].value, 1]);
8069
+ glMatrix.mat4.fromScaling($mat4_1, [d[0].value, d[1].value, 1].map(function (item) {
8070
+ return clampScale(item);
8071
+ }));
8036
8072
  },
8037
8073
  scaleX: function scaleX(d) {
8038
- glMatrix.mat4.fromScaling($mat4_1, [d[0].value, 1, 1]);
8074
+ glMatrix.mat4.fromScaling($mat4_1, [d[0].value, 1, 1].map(function (item) {
8075
+ return clampScale(item);
8076
+ }));
8039
8077
  },
8040
8078
  scaleY: function scaleY(d) {
8041
- glMatrix.mat4.fromScaling($mat4_1, [1, d[0].value, 1]);
8079
+ glMatrix.mat4.fromScaling($mat4_1, [1, d[0].value, 1].map(function (item) {
8080
+ return clampScale(item);
8081
+ }));
8042
8082
  },
8043
8083
  scaleZ: function scaleZ(d) {
8044
- glMatrix.mat4.fromScaling($mat4_1, [1, 1, d[0].value]);
8084
+ glMatrix.mat4.fromScaling($mat4_1, [1, 1, d[0].value].map(function (item) {
8085
+ return clampScale(item);
8086
+ }));
8045
8087
  },
8046
8088
  scale3d: function scale3d(d) {
8047
- glMatrix.mat4.fromScaling($mat4_1, [d[0].value, d[1].value, d[2].value]);
8089
+ glMatrix.mat4.fromScaling($mat4_1, [d[0].value, d[1].value, d[2].value].map(function (item) {
8090
+ return clampScale(item);
8091
+ }));
8048
8092
  },
8049
8093
  translate: function translate(d) {
8050
8094
  glMatrix.mat4.fromTranslation($mat4_1, [d[0].value, d[1].value, 0]);
@@ -11820,6 +11864,94 @@ var DefaultSceneGraphService = /*#__PURE__*/function () {
11820
11864
  }]);
11821
11865
  }();
11822
11866
 
11867
+ /**
11868
+ * A classic LRU (Least Recently Used) cache implementation.
11869
+ * It evicts the least recently used item when the cache is full.
11870
+ *
11871
+ * It uses a Map for O(1) key-based access
11872
+ * to maintain the usage order of items.
11873
+ *
11874
+ * 通过利用 JavaScript 内置 Map 的特性(按插入顺序迭代),我们不再需要手动维护一个双向链表
11875
+ */
11876
+ var LRU = /*#__PURE__*/function () {
11877
+ function LRU(capacity) {
11878
+ _classCallCheck(this, LRU);
11879
+ if (capacity <= 0) {
11880
+ throw new Error('LRU capacity must be a positive number.');
11881
+ }
11882
+ this.capacity = capacity;
11883
+ // Using a Map directly simplifies the implementation significantly.
11884
+ // A Map in modern JS engines iterates in insertion order, which is exactly what we need.
11885
+ this.cache = new Map();
11886
+ }
11887
+
11888
+ /**
11889
+ * Retrieves an item from the cache. Marks the item as recently used.
11890
+ * @param key The key of the item to retrieve.
11891
+ * @returns The value of the item, or undefined if not found.
11892
+ */
11893
+ return _createClass(LRU, [{
11894
+ key: "get",
11895
+ value: function get(key) {
11896
+ if (!this.cache.has(key)) {
11897
+ return undefined;
11898
+ }
11899
+
11900
+ // Get the value.
11901
+ var value = this.cache.get(key);
11902
+
11903
+ // Mark as recently used by deleting and re-setting the key.
11904
+ // This moves the key to the end of the Map's internal order.
11905
+ this.cache["delete"](key);
11906
+ this.cache.set(key, value);
11907
+ return value;
11908
+ }
11909
+
11910
+ /**
11911
+ * Adds or updates an item in the cache. Marks the item as recently used.
11912
+ * If the cache is full, it removes the least recently used item.
11913
+ * @param key The key of the item.
11914
+ * @param value The value of the item.
11915
+ */
11916
+ }, {
11917
+ key: "put",
11918
+ value: function put(key, value) {
11919
+ // If the key already exists, delete it first to ensure it's moved to the end.
11920
+ if (this.cache.has(key)) {
11921
+ this.cache["delete"](key);
11922
+ }
11923
+
11924
+ // Add the new item. It will be the most recently used.
11925
+ this.cache.set(key, value);
11926
+
11927
+ // Check if the cache has exceeded its capacity.
11928
+ if (this.cache.size > this.capacity) {
11929
+ // Evict the least recently used item, which is the first one in the Map's iteration order.
11930
+ var leastRecentlyUsedKey = this.cache.keys().next().value;
11931
+ this.cache["delete"](leastRecentlyUsedKey);
11932
+ }
11933
+ }
11934
+
11935
+ /**
11936
+ * Returns the current number of items in the cache.
11937
+ */
11938
+ }, {
11939
+ key: "len",
11940
+ value: function len() {
11941
+ return this.cache.size;
11942
+ }
11943
+
11944
+ /**
11945
+ * Clears all items from the cache.
11946
+ */
11947
+ }, {
11948
+ key: "clear",
11949
+ value: function clear() {
11950
+ this.cache.clear();
11951
+ }
11952
+ }]);
11953
+ }();
11954
+
11823
11955
  var TEXT_METRICS = {
11824
11956
  MetricsString: '|ÉqÅ',
11825
11957
  BaselineSymbol: 'M',
@@ -11906,10 +12038,16 @@ var TextService = /*#__PURE__*/function () {
11906
12038
  return next;
11907
12039
  };
11908
12040
  this.runtime = runtime;
12041
+ this.charWidthCache = new LRU(100);
11909
12042
  }
11910
12043
  return _createClass(TextService, [{
11911
12044
  key: "measureFont",
11912
12045
  value:
12046
+ /**
12047
+ * A global cache for character widths, keyed by font string.
12048
+ * e.g. { '16px Arial': { 'a': 8, 'b': 9 } }
12049
+ */
12050
+
11913
12051
  /**
11914
12052
  * Calculates the ascent, descent and fontSize of a given font-style.
11915
12053
  */
@@ -12131,9 +12269,19 @@ var TextService = /*#__PURE__*/function () {
12131
12269
  var currentLineWidth = 0;
12132
12270
  // @see https://github.com/antvis/G/issues/1932
12133
12271
  var prevLineLastCharIndex = -1;
12134
- var cache = {};
12135
- var calcWidth = function calcWidth(txt) {
12136
- return _this2.getFromCache(txt, letterSpacing, cache, context);
12272
+
12273
+ // --- 优化核心 ---
12274
+ // 1. 获取或创建当前字体对应的字符缓存
12275
+ var font = toFontString(parsedStyle);
12276
+ var charCache = this.charWidthCache.get(font);
12277
+ if (!charCache) {
12278
+ charCache = new LRU(500);
12279
+ this.charWidthCache.put(font, charCache);
12280
+ }
12281
+
12282
+ // 2. calcWidth 现在直接使用持久化的 charCache
12283
+ var calcWidth = function calcWidth(_char2) {
12284
+ return _this2.getFromCache(_char2, letterSpacing, charCache, context);
12137
12285
  };
12138
12286
  var ellipsisWidth = calcWidth(ellipsis);
12139
12287
 
@@ -12179,11 +12327,11 @@ var TextService = /*#__PURE__*/function () {
12179
12327
  lines[lineIndex] = result.lineTxt + ellipsis;
12180
12328
  }
12181
12329
  for (var i = 0; i < chars.length; i++) {
12182
- var _char2 = chars[i];
12330
+ var _char3 = chars[i];
12183
12331
  var prevChar = chars[i - 1];
12184
12332
  var nextChar = chars[i + 1];
12185
- var charWidth = calcWidth(_char2);
12186
- if (this.isNewline(_char2)) {
12333
+ var charWidth = calcWidth(_char3);
12334
+ if (this.isNewline(_char3)) {
12187
12335
  // exceed maxLines, break immediately
12188
12336
  if (currentLineIndex + 1 >= maxLines) {
12189
12337
  if (i < chars.length - 1) {
@@ -12213,10 +12361,10 @@ var TextService = /*#__PURE__*/function () {
12213
12361
  break;
12214
12362
  }
12215
12363
  i = result.txtLastCharIndex + 1;
12216
- _char2 = chars[i];
12364
+ _char3 = chars[i];
12217
12365
  prevChar = chars[i - 1];
12218
12366
  nextChar = chars[i + 1];
12219
- charWidth = calcWidth(_char2);
12367
+ charWidth = calcWidth(_char3);
12220
12368
  }
12221
12369
  if (currentLineIndex + 1 >= maxLines) {
12222
12370
  appendEllipsis(currentLineIndex, i - 1);
@@ -12227,38 +12375,38 @@ var TextService = /*#__PURE__*/function () {
12227
12375
  currentLineIndex += 1;
12228
12376
  currentLineWidth = 0;
12229
12377
  lines[currentLineIndex] = '';
12230
- if (this.isBreakingSpace(_char2)) {
12378
+ if (this.isBreakingSpace(_char3)) {
12231
12379
  continue;
12232
12380
  }
12233
- if (!this.canBreakInLastChar(_char2)) {
12381
+ if (!this.canBreakInLastChar(_char3)) {
12234
12382
  lines = this.trimToBreakable(lines);
12235
12383
  currentLineWidth = this.sumTextWidthByCache(lines[currentLineIndex] || '', calcWidth);
12236
12384
  }
12237
- if (this.shouldBreakByKinsokuShorui(_char2, nextChar)) {
12385
+ if (this.shouldBreakByKinsokuShorui(_char3, nextChar)) {
12238
12386
  lines = this.trimByKinsokuShorui(lines);
12239
12387
  currentLineWidth += calcWidth(prevChar || '');
12240
12388
  }
12241
12389
  }
12242
12390
  currentLineWidth += charWidth;
12243
- lines[currentLineIndex] += _char2;
12391
+ lines[currentLineIndex] += _char3;
12244
12392
  }
12245
12393
  return lines.join('\n');
12246
12394
  }
12247
12395
  }, {
12248
12396
  key: "isBreakingSpace",
12249
- value: function isBreakingSpace(_char3) {
12250
- if (typeof _char3 !== 'string') {
12397
+ value: function isBreakingSpace(_char4) {
12398
+ if (typeof _char4 !== 'string') {
12251
12399
  return false;
12252
12400
  }
12253
- return TEXT_METRICS.BreakingSpaces.indexOf(_char3.charCodeAt(0)) >= 0;
12401
+ return TEXT_METRICS.BreakingSpaces.indexOf(_char4.charCodeAt(0)) >= 0;
12254
12402
  }
12255
12403
  }, {
12256
12404
  key: "isNewline",
12257
- value: function isNewline(_char4) {
12258
- if (typeof _char4 !== 'string') {
12405
+ value: function isNewline(_char5) {
12406
+ if (typeof _char5 !== 'string') {
12259
12407
  return false;
12260
12408
  }
12261
- return TEXT_METRICS.Newlines.indexOf(_char4.charCodeAt(0)) >= 0;
12409
+ return TEXT_METRICS.Newlines.indexOf(_char5.charCodeAt(0)) >= 0;
12262
12410
  }
12263
12411
  }, {
12264
12412
  key: "trimToBreakable",
@@ -12277,8 +12425,8 @@ var TextService = /*#__PURE__*/function () {
12277
12425
  }
12278
12426
  }, {
12279
12427
  key: "canBreakInLastChar",
12280
- value: function canBreakInLastChar(_char5) {
12281
- if (_char5 && LATIN_REGEX.test(_char5)) return false;
12428
+ value: function canBreakInLastChar(_char6) {
12429
+ if (_char6 && LATIN_REGEX.test(_char6)) return false;
12282
12430
  return true;
12283
12431
  }
12284
12432
  }, {
@@ -12299,15 +12447,21 @@ var TextService = /*#__PURE__*/function () {
12299
12447
  }, {
12300
12448
  key: "getFromCache",
12301
12449
  value: function getFromCache(key, letterSpacing, cache, context) {
12302
- var width = cache[key];
12450
+ var width = cache.get(key);
12303
12451
  if (typeof width !== 'number') {
12304
12452
  var spacing = key.length * letterSpacing;
12305
12453
  var metrics = context.measureText(key);
12306
12454
  width = metrics.width + spacing;
12307
- cache[key] = width;
12455
+ cache.put(key, width);
12308
12456
  }
12309
12457
  return width;
12310
12458
  }
12459
+ }, {
12460
+ key: "clearCache",
12461
+ value: function clearCache() {
12462
+ this.fontMetricsCache = {};
12463
+ this.charWidthCache.clear();
12464
+ }
12311
12465
  }]);
12312
12466
  }();
12313
12467
 
@@ -16300,6 +16454,7 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
16300
16454
  clearEventRetain(insertedEvent);
16301
16455
  clearEventRetain(removedEvent);
16302
16456
  clearEventRetain(destroyEvent);
16457
+ runtime.textService.clearCache();
16303
16458
  }
16304
16459
 
16305
16460
  /**