@antv/g-lite 2.3.0 → 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.0
5
- * @date 6/27/2025, 8:31:48 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]);
@@ -10546,6 +10590,7 @@ var RenderingService = /*#__PURE__*/function () {
10546
10590
  }, {
10547
10591
  key: "render",
10548
10592
  value: function render(canvasConfig, frame, rerenderCallback) {
10593
+ var _this2 = this;
10549
10594
  this.stats.total = 0;
10550
10595
  this.stats.rendered = 0;
10551
10596
  this.zIndexCounter = 0;
@@ -10557,16 +10602,22 @@ var RenderingService = /*#__PURE__*/function () {
10557
10602
  renderingContext.dirtyRectangleRenderingDisabled = this.disableDirtyRectangleRendering();
10558
10603
  var onlyCameraChanged = renderingContext.renderReasons.size === 1 && renderingContext.renderReasons.has(RenderReason.CAMERA_CHANGED);
10559
10604
  var shouldTriggerRenderHooks = !canvasConfig.disableRenderHooks || !onlyCameraChanged;
10560
- this.hooks.beginFrame.call(frame);
10561
10605
  if (shouldTriggerRenderHooks) {
10562
10606
  this.renderDisplayObject(renderingContext.root, canvasConfig, renderingContext);
10563
10607
  }
10608
+ this.hooks.beginFrame.call(frame);
10609
+ if (shouldTriggerRenderHooks) {
10610
+ renderingContext.renderListCurrentFrame.forEach(function (object) {
10611
+ _this2.hooks.beforeRender.call(object);
10612
+ _this2.hooks.render.call(object);
10613
+ _this2.hooks.afterRender.call(object);
10614
+ });
10615
+ }
10564
10616
  this.hooks.endFrame.call(frame);
10617
+ renderingContext.renderListCurrentFrame = [];
10565
10618
  renderingContext.renderReasons.clear();
10566
10619
  rerenderCallback();
10567
10620
  }
10568
-
10569
- // console.log('stats', this.stats);
10570
10621
  }
10571
10622
  }, {
10572
10623
  key: "renderDisplayObject",
@@ -10590,6 +10641,7 @@ var RenderingService = /*#__PURE__*/function () {
10590
10641
  objectToRender = enableCulling ? self.hooks.cull.call(objectChanged, self.context.camera) : objectChanged;
10591
10642
  if (objectToRender) {
10592
10643
  self.stats.rendered += 1;
10644
+ renderingContext.renderListCurrentFrame.push(objectToRender);
10593
10645
  }
10594
10646
  }
10595
10647
  object.dirty(false);
@@ -10604,13 +10656,6 @@ var RenderingService = /*#__PURE__*/function () {
10604
10656
  sortable.dirtyChildren = [];
10605
10657
  sortable.dirtyReason = undefined;
10606
10658
  }
10607
-
10608
- // trigger render hooks
10609
- if (objectToRender) {
10610
- self.hooks.beforeRender.call(objectToRender);
10611
- self.hooks.render.call(objectToRender);
10612
- self.hooks.afterRender.call(objectToRender);
10613
- }
10614
10659
  }
10615
10660
  var stack = [displayObject];
10616
10661
  while (stack.length > 0) {
@@ -11817,6 +11862,94 @@ var DefaultSceneGraphService = /*#__PURE__*/function () {
11817
11862
  }]);
11818
11863
  }();
11819
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
+
11820
11953
  var TEXT_METRICS = {
11821
11954
  MetricsString: '|ÉqÅ',
11822
11955
  BaselineSymbol: 'M',
@@ -11903,10 +12036,16 @@ var TextService = /*#__PURE__*/function () {
11903
12036
  return next;
11904
12037
  };
11905
12038
  this.runtime = runtime;
12039
+ this.charWidthCache = new LRU(100);
11906
12040
  }
11907
12041
  return _createClass(TextService, [{
11908
12042
  key: "measureFont",
11909
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
+
11910
12049
  /**
11911
12050
  * Calculates the ascent, descent and fontSize of a given font-style.
11912
12051
  */
@@ -12103,6 +12242,10 @@ var TextService = /*#__PURE__*/function () {
12103
12242
  key: "wordWrap",
12104
12243
  value: function wordWrap(text, parsedStyle, offscreenCanvas) {
12105
12244
  var _this2 = this;
12245
+ var chars = Array.from(text);
12246
+ if (chars.length === 0) {
12247
+ return '';
12248
+ }
12106
12249
  var self = this;
12107
12250
  var _parsedStyle$wordWrap2 = parsedStyle.wordWrapWidth,
12108
12251
  wordWrapWidth = _parsedStyle$wordWrap2 === void 0 ? 0 : _parsedStyle$wordWrap2,
@@ -12119,15 +12262,24 @@ var TextService = /*#__PURE__*/function () {
12119
12262
  } else if (textOverflow && textOverflow !== 'clip') {
12120
12263
  ellipsis = textOverflow;
12121
12264
  }
12122
- var chars = Array.from(text);
12123
- var lines = [];
12265
+ var lines = [''];
12124
12266
  var currentLineIndex = 0;
12125
12267
  var currentLineWidth = 0;
12126
12268
  // @see https://github.com/antvis/G/issues/1932
12127
12269
  var prevLineLastCharIndex = -1;
12128
- var cache = {};
12129
- var calcWidth = function calcWidth(txt) {
12130
- 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);
12131
12283
  };
12132
12284
  var ellipsisWidth = calcWidth(ellipsis);
12133
12285
 
@@ -12173,17 +12325,17 @@ var TextService = /*#__PURE__*/function () {
12173
12325
  lines[lineIndex] = result.lineTxt + ellipsis;
12174
12326
  }
12175
12327
  for (var i = 0; i < chars.length; i++) {
12176
- var _char2 = chars[i];
12328
+ var _char3 = chars[i];
12177
12329
  var prevChar = chars[i - 1];
12178
12330
  var nextChar = chars[i + 1];
12179
- var charWidth = calcWidth(_char2);
12180
- if (this.isNewline(_char2)) {
12331
+ var charWidth = calcWidth(_char3);
12332
+ if (this.isNewline(_char3)) {
12181
12333
  // exceed maxLines, break immediately
12182
12334
  if (currentLineIndex + 1 >= maxLines) {
12183
- parsedStyle.isOverflowing = true;
12184
12335
  if (i < chars.length - 1) {
12185
12336
  appendEllipsis(currentLineIndex, i - 1);
12186
12337
  }
12338
+ parsedStyle.isOverflowing = true;
12187
12339
  break;
12188
12340
  }
12189
12341
  prevLineLastCharIndex = i - 1;
@@ -12193,14 +12345,9 @@ var TextService = /*#__PURE__*/function () {
12193
12345
  continue;
12194
12346
  }
12195
12347
 
12196
- // If current line is empty and char width exceeds max width, no need to continue
12197
- if (currentLineWidth === 0 && charWidth > maxWidth) {
12198
- // Only add ellipsis if it can fit in the line
12199
- if (ellipsis && ellipsisWidth <= maxWidth) {
12200
- lines[currentLineIndex] = ellipsis;
12201
- } else {
12202
- lines[currentLineIndex] = '';
12203
- }
12348
+ // If char width exceeds max width, no need to continue
12349
+ if (charWidth > maxWidth) {
12350
+ appendEllipsis(currentLineIndex, i - 1);
12204
12351
  parsedStyle.isOverflowing = true;
12205
12352
  break;
12206
12353
  }
@@ -12212,52 +12359,52 @@ var TextService = /*#__PURE__*/function () {
12212
12359
  break;
12213
12360
  }
12214
12361
  i = result.txtLastCharIndex + 1;
12215
- _char2 = chars[i];
12362
+ _char3 = chars[i];
12216
12363
  prevChar = chars[i - 1];
12217
12364
  nextChar = chars[i + 1];
12218
- charWidth = calcWidth(_char2);
12365
+ charWidth = calcWidth(_char3);
12219
12366
  }
12220
12367
  if (currentLineIndex + 1 >= maxLines) {
12221
- parsedStyle.isOverflowing = true;
12222
12368
  appendEllipsis(currentLineIndex, i - 1);
12369
+ parsedStyle.isOverflowing = true;
12223
12370
  break;
12224
12371
  }
12225
12372
  prevLineLastCharIndex = i - 1;
12226
12373
  currentLineIndex += 1;
12227
12374
  currentLineWidth = 0;
12228
12375
  lines[currentLineIndex] = '';
12229
- if (this.isBreakingSpace(_char2)) {
12376
+ if (this.isBreakingSpace(_char3)) {
12230
12377
  continue;
12231
12378
  }
12232
- if (!this.canBreakInLastChar(_char2)) {
12379
+ if (!this.canBreakInLastChar(_char3)) {
12233
12380
  lines = this.trimToBreakable(lines);
12234
12381
  currentLineWidth = this.sumTextWidthByCache(lines[currentLineIndex] || '', calcWidth);
12235
12382
  }
12236
- if (this.shouldBreakByKinsokuShorui(_char2, nextChar)) {
12383
+ if (this.shouldBreakByKinsokuShorui(_char3, nextChar)) {
12237
12384
  lines = this.trimByKinsokuShorui(lines);
12238
12385
  currentLineWidth += calcWidth(prevChar || '');
12239
12386
  }
12240
12387
  }
12241
12388
  currentLineWidth += charWidth;
12242
- lines[currentLineIndex] = (lines[currentLineIndex] || '') + _char2;
12389
+ lines[currentLineIndex] += _char3;
12243
12390
  }
12244
12391
  return lines.join('\n');
12245
12392
  }
12246
12393
  }, {
12247
12394
  key: "isBreakingSpace",
12248
- value: function isBreakingSpace(_char3) {
12249
- if (typeof _char3 !== 'string') {
12395
+ value: function isBreakingSpace(_char4) {
12396
+ if (typeof _char4 !== 'string') {
12250
12397
  return false;
12251
12398
  }
12252
- return TEXT_METRICS.BreakingSpaces.indexOf(_char3.charCodeAt(0)) >= 0;
12399
+ return TEXT_METRICS.BreakingSpaces.indexOf(_char4.charCodeAt(0)) >= 0;
12253
12400
  }
12254
12401
  }, {
12255
12402
  key: "isNewline",
12256
- value: function isNewline(_char4) {
12257
- if (typeof _char4 !== 'string') {
12403
+ value: function isNewline(_char5) {
12404
+ if (typeof _char5 !== 'string') {
12258
12405
  return false;
12259
12406
  }
12260
- return TEXT_METRICS.Newlines.indexOf(_char4.charCodeAt(0)) >= 0;
12407
+ return TEXT_METRICS.Newlines.indexOf(_char5.charCodeAt(0)) >= 0;
12261
12408
  }
12262
12409
  }, {
12263
12410
  key: "trimToBreakable",
@@ -12276,8 +12423,8 @@ var TextService = /*#__PURE__*/function () {
12276
12423
  }
12277
12424
  }, {
12278
12425
  key: "canBreakInLastChar",
12279
- value: function canBreakInLastChar(_char5) {
12280
- if (_char5 && LATIN_REGEX.test(_char5)) return false;
12426
+ value: function canBreakInLastChar(_char6) {
12427
+ if (_char6 && LATIN_REGEX.test(_char6)) return false;
12281
12428
  return true;
12282
12429
  }
12283
12430
  }, {
@@ -12298,15 +12445,21 @@ var TextService = /*#__PURE__*/function () {
12298
12445
  }, {
12299
12446
  key: "getFromCache",
12300
12447
  value: function getFromCache(key, letterSpacing, cache, context) {
12301
- var width = cache[key];
12448
+ var width = cache.get(key);
12302
12449
  if (typeof width !== 'number') {
12303
12450
  var spacing = key.length * letterSpacing;
12304
12451
  var metrics = context.measureText(key);
12305
12452
  width = metrics.width + spacing;
12306
- cache[key] = width;
12453
+ cache.put(key, width);
12307
12454
  }
12308
12455
  return width;
12309
12456
  }
12457
+ }, {
12458
+ key: "clearCache",
12459
+ value: function clearCache() {
12460
+ this.fontMetricsCache = {};
12461
+ this.charWidthCache.clear();
12462
+ }
12310
12463
  }]);
12311
12464
  }();
12312
12465
 
@@ -16134,6 +16287,7 @@ var Canvas = /*#__PURE__*/function (_EventTarget) {
16134
16287
  */
16135
16288
  root: this.document.documentElement,
16136
16289
  unculledEntities: [],
16290
+ renderListCurrentFrame: [],
16137
16291
  renderReasons: new Set(),
16138
16292
  force: false,
16139
16293
  dirty: false
@@ -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
  /**