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