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