@antv/g-webgpu 1.7.42 → 1.7.44

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.umd.js CHANGED
@@ -2906,149 +2906,6 @@
2906
2906
  };
2907
2907
  earcut_1.default = _default;
2908
2908
 
2909
- const INF = 1e20;
2910
-
2911
- class TinySDF {
2912
- constructor({
2913
- fontSize = 24,
2914
- buffer = 3,
2915
- radius = 8,
2916
- cutoff = 0.25,
2917
- fontFamily = 'sans-serif',
2918
- fontWeight = 'normal',
2919
- fontStyle = 'normal'
2920
- } = {}) {
2921
- this.buffer = buffer;
2922
- this.cutoff = cutoff;
2923
- this.radius = radius;
2924
-
2925
- // make the canvas size big enough to both have the specified buffer around the glyph
2926
- // for "halo", and account for some glyphs possibly being larger than their font size
2927
- const size = this.size = fontSize + buffer * 4;
2928
-
2929
- const canvas = this._createCanvas(size);
2930
- const ctx = this.ctx = canvas.getContext('2d', {willReadFrequently: true});
2931
- ctx.font = `${fontStyle} ${fontWeight} ${fontSize}px ${fontFamily}`;
2932
-
2933
- ctx.textBaseline = 'alphabetic';
2934
- ctx.textAlign = 'left'; // Necessary so that RTL text doesn't have different alignment
2935
- ctx.fillStyle = 'black';
2936
-
2937
- // temporary arrays for the distance transform
2938
- this.gridOuter = new Float64Array(size * size);
2939
- this.gridInner = new Float64Array(size * size);
2940
- this.f = new Float64Array(size);
2941
- this.z = new Float64Array(size + 1);
2942
- this.v = new Uint16Array(size);
2943
- }
2944
-
2945
- _createCanvas(size) {
2946
- const canvas = document.createElement('canvas');
2947
- canvas.width = canvas.height = size;
2948
- return canvas;
2949
- }
2950
-
2951
- draw(char) {
2952
- const {
2953
- width: glyphAdvance,
2954
- actualBoundingBoxAscent,
2955
- actualBoundingBoxDescent,
2956
- actualBoundingBoxLeft,
2957
- actualBoundingBoxRight
2958
- } = this.ctx.measureText(char);
2959
-
2960
- // The integer/pixel part of the top alignment is encoded in metrics.glyphTop
2961
- // The remainder is implicitly encoded in the rasterization
2962
- const glyphTop = Math.ceil(actualBoundingBoxAscent);
2963
- const glyphLeft = 0;
2964
-
2965
- // If the glyph overflows the canvas size, it will be clipped at the bottom/right
2966
- const glyphWidth = Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft));
2967
- const glyphHeight = Math.min(this.size - this.buffer, glyphTop + Math.ceil(actualBoundingBoxDescent));
2968
-
2969
- const width = glyphWidth + 2 * this.buffer;
2970
- const height = glyphHeight + 2 * this.buffer;
2971
-
2972
- const len = Math.max(width * height, 0);
2973
- const data = new Uint8ClampedArray(len);
2974
- const glyph = {data, width, height, glyphWidth, glyphHeight, glyphTop, glyphLeft, glyphAdvance};
2975
- if (glyphWidth === 0 || glyphHeight === 0) return glyph;
2976
-
2977
- const {ctx, buffer, gridInner, gridOuter} = this;
2978
- ctx.clearRect(buffer, buffer, glyphWidth, glyphHeight);
2979
- ctx.fillText(char, buffer, buffer + glyphTop);
2980
- const imgData = ctx.getImageData(buffer, buffer, glyphWidth, glyphHeight);
2981
-
2982
- // Initialize grids outside the glyph range to alpha 0
2983
- gridOuter.fill(INF, 0, len);
2984
- gridInner.fill(0, 0, len);
2985
-
2986
- for (let y = 0; y < glyphHeight; y++) {
2987
- for (let x = 0; x < glyphWidth; x++) {
2988
- const a = imgData.data[4 * (y * glyphWidth + x) + 3] / 255; // alpha value
2989
- if (a === 0) continue; // empty pixels
2990
-
2991
- const j = (y + buffer) * width + x + buffer;
2992
-
2993
- if (a === 1) { // fully drawn pixels
2994
- gridOuter[j] = 0;
2995
- gridInner[j] = INF;
2996
-
2997
- } else { // aliased pixels
2998
- const d = 0.5 - a;
2999
- gridOuter[j] = d > 0 ? d * d : 0;
3000
- gridInner[j] = d < 0 ? d * d : 0;
3001
- }
3002
- }
3003
- }
3004
-
3005
- edt(gridOuter, 0, 0, width, height, width, this.f, this.v, this.z);
3006
- edt(gridInner, buffer, buffer, glyphWidth, glyphHeight, width, this.f, this.v, this.z);
3007
-
3008
- for (let i = 0; i < len; i++) {
3009
- const d = Math.sqrt(gridOuter[i]) - Math.sqrt(gridInner[i]);
3010
- data[i] = Math.round(255 - 255 * (d / this.radius + this.cutoff));
3011
- }
3012
-
3013
- return glyph;
3014
- }
3015
- }
3016
-
3017
- // 2D Euclidean squared distance transform by Felzenszwalb & Huttenlocher https://cs.brown.edu/~pff/papers/dt-final.pdf
3018
- function edt(data, x0, y0, width, height, gridSize, f, v, z) {
3019
- for (let x = x0; x < x0 + width; x++) edt1d(data, y0 * gridSize + x, gridSize, height, f, v, z);
3020
- for (let y = y0; y < y0 + height; y++) edt1d(data, y * gridSize + x0, 1, width, f, v, z);
3021
- }
3022
-
3023
- // 1D squared distance transform
3024
- function edt1d(grid, offset, stride, length, f, v, z) {
3025
- v[0] = 0;
3026
- z[0] = -INF;
3027
- z[1] = INF;
3028
- f[0] = grid[offset];
3029
-
3030
- for (let q = 1, k = 0, s = 0; q < length; q++) {
3031
- f[q] = grid[offset + q * stride];
3032
- const q2 = q * q;
3033
- do {
3034
- const r = v[k];
3035
- s = (f[q] - f[r] + q2 - r * r) / (q - r) / 2;
3036
- } while (s <= z[k] && --k > -1);
3037
-
3038
- k++;
3039
- v[k] = q;
3040
- z[k] = s;
3041
- z[k + 1] = INF;
3042
- }
3043
-
3044
- for (let q = 0, k = 0; q < length; q++) {
3045
- while (z[k + 1] < q) k++;
3046
- const r = v[k];
3047
- const qr = q - r;
3048
- grid[offset + q * stride] = f[r] + qr * qr;
3049
- }
3050
- }
3051
-
3052
2909
  function _regeneratorRuntime$1() {
3053
2910
  _regeneratorRuntime$1 = function _regeneratorRuntime() {
3054
2911
  return exports;
@@ -10780,6 +10637,174 @@
10780
10637
  points.push(dt3 * fromX + 3 * dt2 * j * cpX + 3 * dt * t2 * cpX2 + t3 * toX, dt3 * fromY + 3 * dt2 * j * cpY + 3 * dt * t2 * cpY2 + t3 * toY);
10781
10638
  }
10782
10639
  }
10640
+
10641
+ /**
10642
+ * Borrow from https://github.com/mapbox/tiny-sdf
10643
+ */
10644
+ var INF = 1e20;
10645
+ var TinySDF = /*#__PURE__*/function () {
10646
+ function TinySDF(options) {
10647
+ this.buffer = void 0;
10648
+ this.radius = void 0;
10649
+ this.cutoff = void 0;
10650
+ this.size = void 0;
10651
+ this.f = void 0;
10652
+ this.z = void 0;
10653
+ this.v = void 0;
10654
+ this.gridOuter = void 0;
10655
+ this.gridInner = void 0;
10656
+ this.ctx = void 0;
10657
+ var _options$fontSize = options.fontSize,
10658
+ fontSize = _options$fontSize === void 0 ? 24 : _options$fontSize,
10659
+ _options$buffer = options.buffer,
10660
+ buffer = _options$buffer === void 0 ? 3 : _options$buffer,
10661
+ _options$radius = options.radius,
10662
+ radius = _options$radius === void 0 ? 8 : _options$radius,
10663
+ _options$cutoff = options.cutoff,
10664
+ cutoff = _options$cutoff === void 0 ? 0.25 : _options$cutoff,
10665
+ _options$fontFamily = options.fontFamily,
10666
+ fontFamily = _options$fontFamily === void 0 ? 'sans-serif' : _options$fontFamily,
10667
+ _options$fontWeight = options.fontWeight,
10668
+ fontWeight = _options$fontWeight === void 0 ? 'normal' : _options$fontWeight,
10669
+ _options$fontStyle = options.fontStyle,
10670
+ fontStyle = _options$fontStyle === void 0 ? 'normal' : _options$fontStyle,
10671
+ canvas = options.canvas;
10672
+ this.buffer = buffer;
10673
+ this.cutoff = cutoff;
10674
+ this.radius = radius;
10675
+ // make the canvas size big enough to both have the specified buffer around the glyph
10676
+ // for "halo", and account for some glyphs possibly being larger than their font size
10677
+ var size = this.size = fontSize + buffer * 4;
10678
+ var $offscreenCanvas = gLite.runtime.offscreenCanvas.getOrCreateCanvas(canvas);
10679
+ // canvas.width = canvas.height = size;
10680
+ // $offscreenCanvas.width = width * dpr;
10681
+ // $offscreenCanvas.height = height * dpr;
10682
+ $offscreenCanvas.width = size;
10683
+ $offscreenCanvas.height = size;
10684
+ var ctx = gLite.runtime.offscreenCanvas.getOrCreateContext(canvas);
10685
+ this.ctx = ctx;
10686
+ // const ctx = (this.ctx = canvas.getContext('2d', {
10687
+ // willReadFrequently: true,
10688
+ // }));
10689
+ ctx.font = fontStyle + " " + fontWeight + " " + fontSize + "px " + fontFamily;
10690
+ ctx.textBaseline = 'alphabetic';
10691
+ ctx.textAlign = 'left'; // Necessary so that RTL text doesn't have different alignment
10692
+ ctx.fillStyle = 'black';
10693
+ // temporary arrays for the distance transform
10694
+ this.gridOuter = new Float64Array(size * size);
10695
+ this.gridInner = new Float64Array(size * size);
10696
+ this.f = new Float64Array(size);
10697
+ this.z = new Float64Array(size + 1);
10698
+ this.v = new Uint16Array(size);
10699
+ }
10700
+ // _createCanvas(size) {
10701
+ // const canvas = document.createElement('canvas');
10702
+ // canvas.width = canvas.height = size;
10703
+ // return canvas;
10704
+ // }
10705
+ var _proto = TinySDF.prototype;
10706
+ _proto.draw = function draw(char) {
10707
+ var _this$ctx$measureText = this.ctx.measureText(char),
10708
+ glyphAdvance = _this$ctx$measureText.width,
10709
+ actualBoundingBoxAscent = _this$ctx$measureText.actualBoundingBoxAscent,
10710
+ actualBoundingBoxDescent = _this$ctx$measureText.actualBoundingBoxDescent,
10711
+ actualBoundingBoxLeft = _this$ctx$measureText.actualBoundingBoxLeft,
10712
+ actualBoundingBoxRight = _this$ctx$measureText.actualBoundingBoxRight;
10713
+ // The integer/pixel part of the top alignment is encoded in metrics.glyphTop
10714
+ // The remainder is implicitly encoded in the rasterization
10715
+ var glyphTop = Math.ceil(actualBoundingBoxAscent);
10716
+ var glyphLeft = 0;
10717
+ // If the glyph overflows the canvas size, it will be clipped at the bottom/right
10718
+ var glyphWidth = Math.max(0, Math.min(this.size - this.buffer, Math.ceil(actualBoundingBoxRight - actualBoundingBoxLeft)));
10719
+ var glyphHeight = Math.min(this.size - this.buffer, glyphTop + Math.ceil(actualBoundingBoxDescent));
10720
+ var width = glyphWidth + 2 * this.buffer;
10721
+ var height = glyphHeight + 2 * this.buffer;
10722
+ var len = Math.max(width * height, 0);
10723
+ var data = new Uint8ClampedArray(len);
10724
+ var glyph = {
10725
+ data: data,
10726
+ width: width,
10727
+ height: height,
10728
+ glyphWidth: glyphWidth,
10729
+ glyphHeight: glyphHeight,
10730
+ glyphTop: glyphTop,
10731
+ glyphLeft: glyphLeft,
10732
+ glyphAdvance: glyphAdvance
10733
+ };
10734
+ if (glyphWidth === 0 || glyphHeight === 0) return glyph;
10735
+ var ctx = this.ctx,
10736
+ buffer = this.buffer,
10737
+ gridInner = this.gridInner,
10738
+ gridOuter = this.gridOuter;
10739
+ ctx.clearRect(buffer, buffer, glyphWidth, glyphHeight);
10740
+ ctx.fillText(char, buffer, buffer + glyphTop);
10741
+ var imgData = ctx.getImageData(buffer, buffer, glyphWidth, glyphHeight);
10742
+ // Initialize grids outside the glyph range to alpha 0
10743
+ gridOuter.fill(INF, 0, len);
10744
+ gridInner.fill(0, 0, len);
10745
+ for (var y = 0; y < glyphHeight; y++) {
10746
+ for (var x = 0; x < glyphWidth; x++) {
10747
+ var a = imgData.data[4 * (y * glyphWidth + x) + 3] / 255; // alpha value
10748
+ if (a === 0) continue; // empty pixels
10749
+ var j = (y + buffer) * width + x + buffer;
10750
+ if (a === 1) {
10751
+ // fully drawn pixels
10752
+ gridOuter[j] = 0;
10753
+ gridInner[j] = INF;
10754
+ } else {
10755
+ // aliased pixels
10756
+ var d = 0.5 - a;
10757
+ gridOuter[j] = d > 0 ? d * d : 0;
10758
+ gridInner[j] = d < 0 ? d * d : 0;
10759
+ }
10760
+ }
10761
+ }
10762
+ edt(gridOuter, 0, 0, width, height, width, this.f, this.v, this.z);
10763
+ edt(gridInner, buffer, buffer, glyphWidth, glyphHeight, width, this.f, this.v, this.z);
10764
+ for (var i = 0; i < len; i++) {
10765
+ var _d = Math.sqrt(gridOuter[i]) - Math.sqrt(gridInner[i]);
10766
+ data[i] = Math.round(255 - 255 * (_d / this.radius + this.cutoff));
10767
+ }
10768
+ return glyph;
10769
+ };
10770
+ return TinySDF;
10771
+ }();
10772
+ // 2D Euclidean squared distance transform by Felzenszwalb & Huttenlocher https://cs.brown.edu/~pff/papers/dt-final.pdf
10773
+ function edt(data, x0, y0, width, height, gridSize, f, v, z) {
10774
+ for (var x = x0; x < x0 + width; x++) {
10775
+ edt1d(data, y0 * gridSize + x, gridSize, height, f, v, z);
10776
+ }
10777
+ for (var y = y0; y < y0 + height; y++) {
10778
+ edt1d(data, y * gridSize + x0, 1, width, f, v, z);
10779
+ }
10780
+ }
10781
+ // 1D squared distance transform
10782
+ function edt1d(grid, offset, stride, length, f, v, z) {
10783
+ v[0] = 0;
10784
+ z[0] = -INF;
10785
+ z[1] = INF;
10786
+ f[0] = grid[offset];
10787
+ for (var q = 1, k = 0, s = 0; q < length; q++) {
10788
+ f[q] = grid[offset + q * stride];
10789
+ var q2 = q * q;
10790
+ do {
10791
+ var r = v[k];
10792
+ s = (f[q] - f[r] + q2 - r * r) / (q - r) / 2;
10793
+ } while (s <= z[k] && --k > -1);
10794
+ k++;
10795
+ v[k] = q;
10796
+ z[k] = s;
10797
+ z[k + 1] = INF;
10798
+ }
10799
+ for (var _q = 0, _k = 0; _q < length; _q++) {
10800
+ while (z[_k + 1] < _q) {
10801
+ _k++;
10802
+ }
10803
+ var _r = v[_k];
10804
+ var qr = _q - _r;
10805
+ grid[offset + _q * stride] = f[_r] + qr * qr;
10806
+ }
10807
+ }
10783
10808
  var JOINT_TYPE;
10784
10809
  (function (JOINT_TYPE) {
10785
10810
  JOINT_TYPE[JOINT_TYPE["NONE"] = 0] = "NONE";
@@ -11896,7 +11921,7 @@
11896
11921
  });
11897
11922
  return positionedGlyphs;
11898
11923
  };
11899
- _proto.generateAtlas = function generateAtlas(fontStack, fontFamily, fontWeight, fontStyle, text, device) {
11924
+ _proto.generateAtlas = function generateAtlas(canvas, fontStack, fontFamily, fontWeight, fontStyle, text, device) {
11900
11925
  var _this2 = this;
11901
11926
  if (fontStack === void 0) {
11902
11927
  fontStack = '';
@@ -11916,7 +11941,7 @@
11916
11941
  });
11917
11942
  if (newChars.length) {
11918
11943
  var glyphMap = newChars.map(function (char) {
11919
- return _this2.generateSDF(fontStack, fontFamily, fontWeight, fontStyle, char);
11944
+ return _this2.generateSDF(canvas, fontStack, fontFamily, fontWeight, fontStyle, char);
11920
11945
  }).reduce(function (prev, cur) {
11921
11946
  // @ts-ignore
11922
11947
  prev[cur.id] = cur;
@@ -11941,7 +11966,7 @@
11941
11966
  this.glyphAtlasTexture.setImageData([data], 0);
11942
11967
  }
11943
11968
  };
11944
- _proto.generateSDF = function generateSDF(fontStack, fontFamily, fontWeight, fontStyle, char) {
11969
+ _proto.generateSDF = function generateSDF(canvas, fontStack, fontFamily, fontWeight, fontStyle, char) {
11945
11970
  if (fontStack === void 0) {
11946
11971
  fontStack = '';
11947
11972
  }
@@ -11952,6 +11977,7 @@
11952
11977
  sdfGenerator = this.sdfGeneratorCache[fontStack] =
11953
11978
  // TODO: use OffscreenCanvas in TextService
11954
11979
  new TinySDF({
11980
+ canvas: canvas,
11955
11981
  fontSize: fontSize,
11956
11982
  fontFamily: fontFamily,
11957
11983
  fontWeight: fontWeight,
@@ -12261,7 +12287,7 @@
12261
12287
  var allText = objects.map(function (object) {
12262
12288
  return object.parsedStyle.text;
12263
12289
  }).join('');
12264
- this.glyphManager.generateAtlas(font, fontFamily, fontWeight.toString(), fontStyle, allText, this.context.device);
12290
+ this.glyphManager.generateAtlas(this.texturePool.context.config.offscreenCanvas, font, fontFamily, fontWeight.toString(), fontStyle, allText, this.context.device);
12265
12291
  var glyphAtlasTexture = this.glyphManager.getAtlasTexture();
12266
12292
  var glyphAtlas = this.glyphManager.getAtlas();
12267
12293
  this.context.device.setResourceName(glyphAtlasTexture, 'TextSDF Texture');
@@ -13233,6 +13259,7 @@
13233
13259
  TextureMapping: TextureMapping,
13234
13260
  TexturePool: TexturePool,
13235
13261
  get TextureUsage () { return TextureUsage; },
13262
+ TinySDF: TinySDF,
13236
13263
  TransparentBlack: TransparentBlack,
13237
13264
  TransparentWhite: TransparentWhite,
13238
13265
  UNIFORM_SETTERS: UNIFORM_SETTERS,
@@ -16192,7 +16219,9 @@
16192
16219
  resource: gpuSampler
16193
16220
  });
16194
16221
  }
16195
- _this.gpuBindGroup = gpuBindGroupEntries.map(function (gpuBindGroupEntry, i) {
16222
+ _this.gpuBindGroup = gpuBindGroupEntries.filter(function (entries) {
16223
+ return entries.length > 0;
16224
+ }).map(function (gpuBindGroupEntry, i) {
16196
16225
  return _this.device.device.createBindGroup({
16197
16226
  layout: pipeline.getBindGroupLayout(i),
16198
16227
  // layout: bindGroupLayout.gpuBindGroupLayout[i],