@versatiles/svg-renderer 1.1.0 → 1.2.0

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
@@ -310,10 +310,7 @@ var v8_default = {
310
310
  "type": "enum",
311
311
  "values": { "image": {} }
312
312
  },
313
- "url": {
314
- "required": true,
315
- "type": "string"
316
- },
313
+ "url": { "type": "string" },
317
314
  "coordinates": {
318
315
  "required": true,
319
316
  "type": "array",
@@ -653,9 +650,9 @@ var v8_default = {
653
650
  "requires": ["icon-image"],
654
651
  "expression": {
655
652
  "interpolated": false,
656
- "parameters": ["zoom"]
653
+ "parameters": ["zoom", "feature"]
657
654
  },
658
- "property-type": "data-constant"
655
+ "property-type": "data-driven"
659
656
  },
660
657
  "icon-size": {
661
658
  "type": "number",
@@ -1116,6 +1113,31 @@ var v8_default = {
1116
1113
  },
1117
1114
  "property-type": "data-constant"
1118
1115
  },
1116
+ "symbol-height-offset": {
1117
+ "type": "number",
1118
+ "default": 0,
1119
+ "units": "meters",
1120
+ "requires": [{ "symbol-placement": ["point"] }],
1121
+ "expression": {
1122
+ "interpolated": true,
1123
+ "parameters": ["zoom", "feature"]
1124
+ },
1125
+ "property-type": "data-driven"
1126
+ },
1127
+ "symbol-height-anchor": {
1128
+ "type": "enum",
1129
+ "values": {
1130
+ "ground": {},
1131
+ "absolute": {}
1132
+ },
1133
+ "default": "ground",
1134
+ "requires": ["symbol-height-offset"],
1135
+ "expression": {
1136
+ "interpolated": false,
1137
+ "parameters": ["zoom"]
1138
+ },
1139
+ "property-type": "data-constant"
1140
+ },
1119
1141
  "visibility": {
1120
1142
  "type": "enum",
1121
1143
  "values": {
@@ -1591,7 +1613,6 @@ var v8_default = {
1591
1613
  "fill-extrusion-height": {
1592
1614
  "type": "number",
1593
1615
  "default": 0,
1594
- "minimum": 0,
1595
1616
  "units": "meters",
1596
1617
  "transition": true,
1597
1618
  "expression": {
@@ -1607,7 +1628,6 @@ var v8_default = {
1607
1628
  "fill-extrusion-base": {
1608
1629
  "type": "number",
1609
1630
  "default": 0,
1610
- "minimum": 0,
1611
1631
  "units": "meters",
1612
1632
  "transition": true,
1613
1633
  "requires": ["fill-extrusion-height"],
@@ -2642,44 +2662,10 @@ var v8_default = {
2642
2662
  }
2643
2663
  }
2644
2664
  };
2645
- //#endregion
2665
+
2646
2666
  //#region src/reference/latest.ts
2647
2667
  const latest = v8_default;
2648
- //#endregion
2649
- //#region src/expression/parsing_error.ts
2650
- var ExpressionParsingError = class extends Error {
2651
- constructor(key, message) {
2652
- super(message);
2653
- this.message = message;
2654
- this.key = key;
2655
- }
2656
- };
2657
- //#endregion
2658
- //#region src/expression/scope.ts
2659
- /**
2660
- * Tracks `let` bindings during expression parsing.
2661
- * @private
2662
- */
2663
- var Scope = class Scope {
2664
- constructor(parent, bindings = []) {
2665
- this.parent = parent;
2666
- this.bindings = {};
2667
- for (const [name, expression] of bindings) this.bindings[name] = expression;
2668
- }
2669
- concat(bindings) {
2670
- return new Scope(this, bindings);
2671
- }
2672
- get(name) {
2673
- if (this.bindings[name]) return this.bindings[name];
2674
- if (this.parent) return this.parent.get(name);
2675
- throw new Error(`${name} not found in scope.`);
2676
- }
2677
- has(name) {
2678
- if (this.bindings[name]) return true;
2679
- return this.parent ? this.parent.has(name) : false;
2680
- }
2681
- };
2682
- //#endregion
2668
+
2683
2669
  //#region src/expression/types.ts
2684
2670
  const NullType = { kind: "null" };
2685
2671
  const NumberType = { kind: "number" };
@@ -2775,7 +2761,7 @@ function verifyType(provided, sample) {
2775
2761
  if (provided.kind === "array" && sample.kind === "array") return provided.itemType.kind === sample.itemType.kind && typeof provided.N === "number";
2776
2762
  return provided.kind === sample.kind;
2777
2763
  }
2778
- //#endregion
2764
+
2779
2765
  //#region src/expression/types/color_spaces.ts
2780
2766
  const Xn = .96422;
2781
2767
  const Yn = 1;
@@ -2870,7 +2856,7 @@ function hslToRgb([h, s, l, alpha]) {
2870
2856
  alpha
2871
2857
  ];
2872
2858
  }
2873
- //#endregion
2859
+
2874
2860
  //#region src/util/get_own.ts
2875
2861
  const hasOwnProperty = Object.hasOwn || function hasOwnProperty(object, key) {
2876
2862
  return Object.prototype.hasOwnProperty.call(object, key);
@@ -2878,7 +2864,7 @@ const hasOwnProperty = Object.hasOwn || function hasOwnProperty(object, key) {
2878
2864
  function getOwn(object, key) {
2879
2865
  return hasOwnProperty(object, key) ? object[key] : void 0;
2880
2866
  }
2881
- //#endregion
2867
+
2882
2868
  //#region src/expression/types/parse_css_color.ts
2883
2869
  /**
2884
2870
  * CSS color parser compliant with CSS Color 4 Specification.
@@ -2957,9 +2943,9 @@ function parseCssColor(input) {
2957
2943
  const maxValue = valFormat === "%%%" ? 100 : valFormat === "" ? 255 : 0;
2958
2944
  if (maxValue) {
2959
2945
  const rgba = [
2960
- clamp(+r / maxValue, 0, 1),
2961
- clamp(+g / maxValue, 0, 1),
2962
- clamp(+b / maxValue, 0, 1),
2946
+ clamp$1(+r / maxValue, 0, 1),
2947
+ clamp$1(+g / maxValue, 0, 1),
2948
+ clamp$1(+b / maxValue, 0, 1),
2963
2949
  a ? parseAlpha(+a, ap) : 1
2964
2950
  ];
2965
2951
  if (validateNumbers(rgba)) return rgba;
@@ -2979,8 +2965,8 @@ function parseCssColor(input) {
2979
2965
  if (argFormat === " " || argFormat === " /" || argFormat === ",," || argFormat === ",,,") {
2980
2966
  const hsla = [
2981
2967
  +h,
2982
- clamp(+s, 0, 100),
2983
- clamp(+l, 0, 100),
2968
+ clamp$1(+s, 0, 100),
2969
+ clamp$1(+l, 0, 100),
2984
2970
  a ? parseAlpha(+a, ap) : 1
2985
2971
  ];
2986
2972
  if (validateNumbers(hsla)) return hslToRgb(hsla);
@@ -2991,9 +2977,9 @@ function parseHex(hex) {
2991
2977
  return parseInt(hex.padEnd(2, hex), 16) / 255;
2992
2978
  }
2993
2979
  function parseAlpha(a, asPercentage) {
2994
- return clamp(asPercentage ? a / 100 : a, 0, 1);
2980
+ return clamp$1(asPercentage ? a / 100 : a, 0, 1);
2995
2981
  }
2996
- function clamp(n, min, max) {
2982
+ function clamp$1(n, min, max) {
2997
2983
  return Math.min(Math.max(min, n), max);
2998
2984
  }
2999
2985
  /**
@@ -3756,7 +3742,7 @@ const namedColors = {
3756
3742
  50
3757
3743
  ]
3758
3744
  };
3759
- //#endregion
3745
+
3760
3746
  //#region src/util/interpolate-primitives.ts
3761
3747
  function interpolateNumber(from, to, t) {
3762
3748
  return from + t * (to - from);
@@ -3766,7 +3752,7 @@ function interpolateArray(from, to, t) {
3766
3752
  return interpolateNumber(d, to[i], t);
3767
3753
  });
3768
3754
  }
3769
- //#endregion
3755
+
3770
3756
  //#region src/expression/types/color.ts
3771
3757
  /**
3772
3758
  * Checks whether the specified color space is one of the supported interpolation color spaces.
@@ -3950,26 +3936,7 @@ var Color$1 = class Color {
3950
3936
  }
3951
3937
  }
3952
3938
  };
3953
- //#endregion
3954
- //#region src/expression/types/collator.ts
3955
- var Collator = class {
3956
- constructor(caseSensitive, diacriticSensitive, locale) {
3957
- if (caseSensitive) this.sensitivity = diacriticSensitive ? "variant" : "case";
3958
- else this.sensitivity = diacriticSensitive ? "accent" : "base";
3959
- this.locale = locale;
3960
- this.collator = new Intl.Collator(this.locale ? this.locale : [], {
3961
- sensitivity: this.sensitivity,
3962
- usage: "search"
3963
- });
3964
- }
3965
- compare(lhs, rhs) {
3966
- return this.collator.compare(lhs, rhs);
3967
- }
3968
- resolvedLocale() {
3969
- return new Intl.Collator(this.locale ? this.locale : []).resolvedOptions().locale;
3970
- }
3971
- };
3972
- //#endregion
3939
+
3973
3940
  //#region src/expression/types/formatted.ts
3974
3941
  const VERTICAL_ALIGN_OPTIONS = [
3975
3942
  "bottom",
@@ -4006,7 +3973,7 @@ var Formatted = class Formatted {
4006
3973
  return this.sections.map((section) => section.text).join("");
4007
3974
  }
4008
3975
  };
4009
- //#endregion
3976
+
4010
3977
  //#region src/expression/types/padding.ts
4011
3978
  /**
4012
3979
  * A set of four numbers representing padding around a box. Create instances from
@@ -4050,14 +4017,12 @@ var Padding = class Padding {
4050
4017
  input[1]
4051
4018
  ];
4052
4019
  break;
4053
- case 3:
4054
- input = [
4055
- input[0],
4056
- input[1],
4057
- input[2],
4058
- input[1]
4059
- ];
4060
- break;
4020
+ case 3: input = [
4021
+ input[0],
4022
+ input[1],
4023
+ input[2],
4024
+ input[1]
4025
+ ];
4061
4026
  }
4062
4027
  return new Padding(input);
4063
4028
  }
@@ -4068,7 +4033,7 @@ var Padding = class Padding {
4068
4033
  return new Padding(interpolateArray(from.values, to.values, t));
4069
4034
  }
4070
4035
  };
4071
- //#endregion
4036
+
4072
4037
  //#region src/expression/types/number_array.ts
4073
4038
  /**
4074
4039
  * An array of numbers. Create instances from
@@ -4098,7 +4063,7 @@ var NumberArray = class NumberArray {
4098
4063
  return new NumberArray(interpolateArray(from.values, to.values, t));
4099
4064
  }
4100
4065
  };
4101
- //#endregion
4066
+
4102
4067
  //#region src/expression/types/color_array.ts
4103
4068
  /**
4104
4069
  * An array of colors. Create instances from
@@ -4141,7 +4106,7 @@ var ColorArray = class ColorArray {
4141
4106
  return new ColorArray(colors);
4142
4107
  }
4143
4108
  };
4144
- //#endregion
4109
+
4145
4110
  //#region src/expression/runtime_error.ts
4146
4111
  var RuntimeError = class extends Error {
4147
4112
  constructor(message, path) {
@@ -4153,7 +4118,7 @@ var RuntimeError = class extends Error {
4153
4118
  return this.message;
4154
4119
  }
4155
4120
  };
4156
- //#endregion
4121
+
4157
4122
  //#region src/expression/types/variable_anchor_offset_collection.ts
4158
4123
  /** Set of valid anchor positions, as a set for validation */
4159
4124
  const anchors = /* @__PURE__ */ new Set([
@@ -4205,7 +4170,7 @@ var VariableAnchorOffsetCollection = class VariableAnchorOffsetCollection {
4205
4170
  return new VariableAnchorOffsetCollection(output);
4206
4171
  }
4207
4172
  };
4208
- //#endregion
4173
+
4209
4174
  //#region src/expression/types/resolved_image.ts
4210
4175
  var ResolvedImage = class ResolvedImage {
4211
4176
  constructor(options) {
@@ -4223,7 +4188,7 @@ var ResolvedImage = class ResolvedImage {
4223
4188
  });
4224
4189
  }
4225
4190
  };
4226
- //#endregion
4191
+
4227
4192
  //#region src/expression/types/projection_definition.ts
4228
4193
  var ProjectionDefinition = class ProjectionDefinition {
4229
4194
  constructor(from, to, transition) {
@@ -4249,7 +4214,26 @@ var ProjectionDefinition = class ProjectionDefinition {
4249
4214
  if (typeof input === "string") return new ProjectionDefinition(input, input, 1);
4250
4215
  }
4251
4216
  };
4252
- //#endregion
4217
+
4218
+ //#region src/expression/types/collator.ts
4219
+ var Collator = class {
4220
+ constructor(caseSensitive, diacriticSensitive, locale) {
4221
+ if (caseSensitive) this.sensitivity = diacriticSensitive ? "variant" : "case";
4222
+ else this.sensitivity = diacriticSensitive ? "accent" : "base";
4223
+ this.locale = locale;
4224
+ this.collator = new Intl.Collator(this.locale ? this.locale : [], {
4225
+ sensitivity: this.sensitivity,
4226
+ usage: "search"
4227
+ });
4228
+ }
4229
+ compare(lhs, rhs) {
4230
+ return this.collator.compare(lhs, rhs);
4231
+ }
4232
+ resolvedLocale() {
4233
+ return new Intl.Collator(this.locale ? this.locale : []).resolvedOptions().locale;
4234
+ }
4235
+ };
4236
+
4253
4237
  //#region src/expression/values.ts
4254
4238
  function validateRGBA(r, g, b, a) {
4255
4239
  if (!(typeof r === "number" && r >= 0 && r <= 255 && typeof g === "number" && g >= 0 && g <= 255 && typeof b === "number" && b >= 0 && b <= 255)) return `Invalid rgba value [${(typeof a === "number" ? [
@@ -4316,7 +4300,7 @@ function valueToString(value) {
4316
4300
  else if (value instanceof Color$1 || value instanceof ProjectionDefinition || value instanceof Formatted || value instanceof Padding || value instanceof NumberArray || value instanceof ColorArray || value instanceof VariableAnchorOffsetCollection || value instanceof ResolvedImage) return value.toString();
4317
4301
  else return JSON.stringify(value);
4318
4302
  }
4319
- //#endregion
4303
+
4320
4304
  //#region src/expression/definitions/literal.ts
4321
4305
  var Literal = class Literal {
4322
4306
  constructor(type, value) {
@@ -4325,7 +4309,7 @@ var Literal = class Literal {
4325
4309
  }
4326
4310
  static parse(args, context) {
4327
4311
  if (args.length !== 2) return context.error(`'literal' expression requires exactly one argument, but found ${args.length - 1} instead.`);
4328
- if (!isValue(args[1])) return context.error("invalid value");
4312
+ if (!isValue(args[1])) return context.error(`invalid value of type "${typeof args[1]}"`);
4329
4313
  const value = args[1];
4330
4314
  let type = typeOf(value);
4331
4315
  const expected = context.expectedType;
@@ -4340,86 +4324,525 @@ var Literal = class Literal {
4340
4324
  return true;
4341
4325
  }
4342
4326
  };
4343
- //#endregion
4344
- //#region src/expression/definitions/assertion.ts
4345
- const types$1 = {
4346
- string: StringType,
4347
- number: NumberType,
4348
- boolean: BooleanType,
4349
- object: ObjectType
4350
- };
4351
- var Assertion = class Assertion {
4352
- constructor(type, args, key) {
4353
- this.type = type;
4354
- this.args = args;
4355
- this.key = key;
4327
+
4328
+ //#region src/expression/evaluation_context.ts
4329
+ const geometryTypes = [
4330
+ "Unknown",
4331
+ "Point",
4332
+ "LineString",
4333
+ "Polygon"
4334
+ ];
4335
+ var EvaluationContext = class {
4336
+ constructor() {
4337
+ this.globals = null;
4338
+ this.feature = null;
4339
+ this.featureState = null;
4340
+ this.formattedSection = null;
4341
+ this._parseColorCache = /* @__PURE__ */ new Map();
4342
+ this.availableImages = null;
4343
+ this.canonical = null;
4356
4344
  }
4357
- static parse(args, context) {
4358
- if (args.length < 2) return context.error("Expected at least one argument.");
4359
- let i = 1;
4360
- let type;
4361
- const name = args[0];
4362
- if (name === "array") {
4363
- let itemType;
4364
- if (args.length > 2) {
4365
- const type = args[1];
4366
- if (typeof type !== "string" || !(type in types$1) || type === "object") return context.error("The item type argument of \"array\" must be one of string, number, boolean", 1);
4367
- itemType = types$1[type];
4368
- i++;
4369
- } else itemType = ValueType;
4370
- let N;
4371
- if (args.length > 3) {
4372
- if (args[2] !== null && (typeof args[2] !== "number" || args[2] < 0 || args[2] !== Math.floor(args[2]))) return context.error("The length argument to \"array\" must be a positive integer literal", 2);
4373
- N = args[2];
4374
- i++;
4375
- }
4376
- type = array(itemType, N);
4377
- } else {
4378
- if (!types$1[name]) throw new Error(`Types doesn't contain name = ${name}`);
4379
- type = types$1[name];
4380
- }
4381
- const parsed = [];
4382
- for (; i < args.length; i++) {
4383
- const input = context.parse(args[i], i, ValueType);
4384
- if (!input) return null;
4385
- parsed.push(input);
4386
- }
4387
- return new Assertion(type, parsed, context.key);
4345
+ id() {
4346
+ return this.feature && "id" in this.feature ? this.feature.id : null;
4388
4347
  }
4389
- evaluate(ctx) {
4390
- for (let i = 0; i < this.args.length; i++) {
4391
- const value = this.args[i].evaluate(ctx);
4392
- if (!checkSubtype(this.type, typeOf(value))) return value;
4393
- else if (i === this.args.length - 1) throw new RuntimeError(`Expected value to be of type ${typeToString(this.type)}, but found ${typeToString(typeOf(value))} instead.`, this.key);
4394
- }
4395
- throw new Error();
4348
+ geometryType() {
4349
+ return this.feature ? typeof this.feature.type === "number" ? geometryTypes[this.feature.type] : this.feature.type : null;
4396
4350
  }
4397
- eachChild(fn) {
4398
- this.args.forEach(fn);
4351
+ geometry() {
4352
+ return this.feature && "geometry" in this.feature ? this.feature.geometry : null;
4399
4353
  }
4400
- outputDefined() {
4401
- return this.args.every((arg) => arg.outputDefined());
4354
+ canonicalID() {
4355
+ return this.canonical;
4356
+ }
4357
+ properties() {
4358
+ return this.feature && this.feature.properties || {};
4359
+ }
4360
+ parseColor(input) {
4361
+ let cached = this._parseColorCache.get(input);
4362
+ if (!cached) {
4363
+ cached = Color$1.parse(input);
4364
+ this._parseColorCache.set(input, cached);
4365
+ }
4366
+ return cached;
4402
4367
  }
4403
4368
  };
4404
- //#endregion
4405
- //#region src/expression/definitions/coercion.ts
4406
- const types = {
4407
- "to-boolean": BooleanType,
4408
- "to-color": ColorType,
4409
- "to-number": NumberType,
4410
- "to-string": StringType
4411
- };
4369
+
4370
+ //#region src/expression/stops.ts
4412
4371
  /**
4413
- * Special form for error-coalescing coercion expressions "to-number",
4414
- * "to-color". Since these coercions can fail at runtime, they accept multiple
4415
- * arguments, only evaluating one at a time until one succeeds.
4416
- *
4372
+ * Returns the index of the last stop <= input, or 0 if it doesn't exist.
4417
4373
  * @private
4418
4374
  */
4419
- var Coercion = class Coercion {
4420
- constructor(type, args, key) {
4375
+ function findStopLessThanOrEqualTo(stops, input, key) {
4376
+ const lastIndex = stops.length - 1;
4377
+ let lowerIndex = 0;
4378
+ let upperIndex = lastIndex;
4379
+ let currentIndex = 0;
4380
+ let currentValue, nextValue;
4381
+ while (lowerIndex <= upperIndex) {
4382
+ currentIndex = Math.floor((lowerIndex + upperIndex) / 2);
4383
+ currentValue = stops[currentIndex];
4384
+ nextValue = stops[currentIndex + 1];
4385
+ if (currentValue <= input) {
4386
+ if (currentIndex === lastIndex || input < nextValue) return currentIndex;
4387
+ lowerIndex = currentIndex + 1;
4388
+ } else if (currentValue > input) upperIndex = currentIndex - 1;
4389
+ else throw new RuntimeError("Input is not a number.", key);
4390
+ }
4391
+ return 0;
4392
+ }
4393
+
4394
+ //#region src/expression/definitions/step.ts
4395
+ var Step = class Step {
4396
+ constructor(type, input, stops, key) {
4421
4397
  this.type = type;
4422
- this.args = args;
4398
+ this.input = input;
4399
+ this.key = key;
4400
+ this.labels = [];
4401
+ this.outputs = [];
4402
+ for (const [label, expression] of stops) {
4403
+ this.labels.push(label);
4404
+ this.outputs.push(expression);
4405
+ }
4406
+ }
4407
+ static parse(args, context) {
4408
+ if (args.length - 1 < 4) return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
4409
+ if ((args.length - 1) % 2 !== 0) return context.error("Expected an even number of arguments.");
4410
+ const input = context.parse(args[1], 1, NumberType);
4411
+ if (!input) return null;
4412
+ const stops = [];
4413
+ let outputType = null;
4414
+ if (context.expectedType && context.expectedType.kind !== "value") outputType = context.expectedType;
4415
+ for (let i = 1; i < args.length; i += 2) {
4416
+ const label = i === 1 ? -Infinity : args[i];
4417
+ const value = args[i + 1];
4418
+ const labelKey = i;
4419
+ const valueKey = i + 1;
4420
+ if (typeof label !== "number") return context.error("Input/output pairs for \"step\" expressions must be defined using literal numeric values (not computed expressions) for the input values.", labelKey);
4421
+ if (stops.length && stops[stops.length - 1][0] >= label) return context.error("Input/output pairs for \"step\" expressions must be arranged with input values in strictly ascending order.", labelKey);
4422
+ const parsed = context.parse(value, valueKey, outputType);
4423
+ if (!parsed) return null;
4424
+ outputType = outputType || parsed.type;
4425
+ stops.push([label, parsed]);
4426
+ }
4427
+ return new Step(outputType, input, stops, context.key);
4428
+ }
4429
+ evaluate(ctx) {
4430
+ const labels = this.labels;
4431
+ const outputs = this.outputs;
4432
+ if (labels.length === 1) return outputs[0].evaluate(ctx);
4433
+ const value = this.input.evaluate(ctx);
4434
+ if (value <= labels[0]) return outputs[0].evaluate(ctx);
4435
+ const stopCount = labels.length;
4436
+ if (value >= labels[stopCount - 1]) return outputs[stopCount - 1].evaluate(ctx);
4437
+ return outputs[findStopLessThanOrEqualTo(labels, value, this.key)].evaluate(ctx);
4438
+ }
4439
+ eachChild(fn) {
4440
+ fn(this.input);
4441
+ for (const expression of this.outputs) fn(expression);
4442
+ }
4443
+ outputDefined() {
4444
+ return this.outputs.every((out) => out.outputDefined());
4445
+ }
4446
+ };
4447
+
4448
+ //#region node_modules/@mapbox/unitbezier/index.js
4449
+ function unitBezier(p1x, p1y, p2x, p2y) {
4450
+ const cx = 3 * p1x;
4451
+ const bx = 3 * (p2x - p1x) - cx;
4452
+ const ax = 1 - cx - bx;
4453
+ const cy = 3 * p1y;
4454
+ const by = 3 * (p2y - p1y) - cy;
4455
+ const ay = 1 - cy - by;
4456
+ return function solve(x, epsilon = 1e-6) {
4457
+ if (x <= 0) return 0;
4458
+ if (x >= 1) return 1;
4459
+ let t = x;
4460
+ for (let i = 0; i < 8; i++) {
4461
+ const x2 = ((ax * t + bx) * t + cx) * t - x;
4462
+ if (Math.abs(x2) < epsilon) return ((ay * t + by) * t + cy) * t;
4463
+ const d2 = (3 * ax * t + 2 * bx) * t + cx;
4464
+ if (Math.abs(d2) < 1e-6) break;
4465
+ t -= x2 / d2;
4466
+ }
4467
+ let t0 = 0;
4468
+ let t1 = 1;
4469
+ t = x;
4470
+ for (let i = 0; i < 20; i++) {
4471
+ const x2 = ((ax * t + bx) * t + cx) * t;
4472
+ if (Math.abs(x2 - x) < epsilon) break;
4473
+ if (x > x2) t0 = t;
4474
+ else t1 = t;
4475
+ t = (t0 + t1) * .5;
4476
+ }
4477
+ return ((ay * t + by) * t + cy) * t;
4478
+ };
4479
+ }
4480
+
4481
+ //#region src/expression/definitions/interpolate.ts
4482
+ var Interpolate = class Interpolate {
4483
+ constructor(type, operator, interpolation, input, stops, key) {
4484
+ this.type = type;
4485
+ this.operator = operator;
4486
+ this.interpolation = interpolation;
4487
+ this.input = input;
4488
+ this.key = key;
4489
+ this.labels = [];
4490
+ this.outputs = [];
4491
+ for (const [label, expression] of stops) {
4492
+ this.labels.push(label);
4493
+ this.outputs.push(expression);
4494
+ }
4495
+ }
4496
+ static interpolationFactor(interpolation, input, lower, upper) {
4497
+ let t = 0;
4498
+ if (interpolation.name === "exponential") t = exponentialInterpolation(input, interpolation.base, lower, upper);
4499
+ else if (interpolation.name === "linear") t = exponentialInterpolation(input, 1, lower, upper);
4500
+ else if (interpolation.name === "cubic-bezier") {
4501
+ const c = interpolation.controlPoints;
4502
+ t = unitBezier(c[0], c[1], c[2], c[3])(exponentialInterpolation(input, 1, lower, upper));
4503
+ }
4504
+ return t;
4505
+ }
4506
+ static parse(args, context) {
4507
+ let [operator, interpolation, input, ...rest] = args;
4508
+ if (!Array.isArray(interpolation) || interpolation.length === 0) return context.error("Expected an interpolation type expression.", 1);
4509
+ if (interpolation[0] === "linear") interpolation = { name: "linear" };
4510
+ else if (interpolation[0] === "exponential") {
4511
+ const base = interpolation[1];
4512
+ if (typeof base !== "number") return context.error("Exponential interpolation requires a numeric base.", 1, 1);
4513
+ interpolation = {
4514
+ name: "exponential",
4515
+ base
4516
+ };
4517
+ } else if (interpolation[0] === "cubic-bezier") {
4518
+ const controlPoints = interpolation.slice(1);
4519
+ if (controlPoints.length !== 4 || controlPoints.some((t) => typeof t !== "number" || t < 0 || t > 1)) return context.error("Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.", 1);
4520
+ interpolation = {
4521
+ name: "cubic-bezier",
4522
+ controlPoints
4523
+ };
4524
+ } else return context.error(`Unknown interpolation type ${String(interpolation[0])}`, 1, 0);
4525
+ if (args.length - 1 < 4) return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
4526
+ if ((args.length - 1) % 2 !== 0) return context.error("Expected an even number of arguments.");
4527
+ input = context.parse(input, 2, NumberType);
4528
+ if (!input) return null;
4529
+ const stops = [];
4530
+ let outputType = null;
4531
+ if ((operator === "interpolate-hcl" || operator === "interpolate-lab") && context.expectedType != ColorArrayType) outputType = ColorType;
4532
+ else if (context.expectedType && context.expectedType.kind !== "value") outputType = context.expectedType;
4533
+ for (let i = 0; i < rest.length; i += 2) {
4534
+ const label = rest[i];
4535
+ const value = rest[i + 1];
4536
+ const labelKey = i + 3;
4537
+ const valueKey = i + 4;
4538
+ if (typeof label !== "number") return context.error("Input/output pairs for \"interpolate\" expressions must be defined using literal numeric values (not computed expressions) for the input values.", labelKey);
4539
+ if (stops.length && stops[stops.length - 1][0] >= label) return context.error("Input/output pairs for \"interpolate\" expressions must be arranged with input values in strictly ascending order.", labelKey);
4540
+ const parsed = context.parse(value, valueKey, outputType);
4541
+ if (!parsed) return null;
4542
+ outputType = outputType || parsed.type;
4543
+ stops.push([label, parsed]);
4544
+ }
4545
+ if (!verifyType(outputType, NumberType) && !verifyType(outputType, ProjectionDefinitionType) && !verifyType(outputType, ColorType) && !verifyType(outputType, PaddingType) && !verifyType(outputType, NumberArrayType) && !verifyType(outputType, ColorArrayType) && !verifyType(outputType, VariableAnchorOffsetCollectionType) && !verifyType(outputType, array(NumberType))) return context.error(`Type ${typeToString(outputType)} is not interpolatable.`);
4546
+ return new Interpolate(outputType, operator, interpolation, input, stops, context.key);
4547
+ }
4548
+ evaluate(ctx) {
4549
+ const labels = this.labels;
4550
+ const outputs = this.outputs;
4551
+ if (labels.length === 1) return outputs[0].evaluate(ctx);
4552
+ const value = this.input.evaluate(ctx);
4553
+ if (value <= labels[0]) return outputs[0].evaluate(ctx);
4554
+ const stopCount = labels.length;
4555
+ if (value >= labels[stopCount - 1]) return outputs[stopCount - 1].evaluate(ctx);
4556
+ const index = findStopLessThanOrEqualTo(labels, value, this.key);
4557
+ const lower = labels[index];
4558
+ const upper = labels[index + 1];
4559
+ const t = Interpolate.interpolationFactor(this.interpolation, value, lower, upper);
4560
+ const outputLower = outputs[index].evaluate(ctx);
4561
+ const outputUpper = outputs[index + 1].evaluate(ctx);
4562
+ switch (this.operator) {
4563
+ case "interpolate": switch (this.type.kind) {
4564
+ case "number": return interpolateNumber(outputLower, outputUpper, t);
4565
+ case "color": return Color$1.interpolate(outputLower, outputUpper, t);
4566
+ case "padding": return Padding.interpolate(outputLower, outputUpper, t);
4567
+ case "colorArray": return ColorArray.interpolate(outputLower, outputUpper, t);
4568
+ case "numberArray": return NumberArray.interpolate(outputLower, outputUpper, t);
4569
+ case "variableAnchorOffsetCollection": return VariableAnchorOffsetCollection.interpolate(outputLower, outputUpper, t, this.key);
4570
+ case "array": return interpolateArray(outputLower, outputUpper, t);
4571
+ case "projectionDefinition": return ProjectionDefinition.interpolate(outputLower, outputUpper, t);
4572
+ }
4573
+ case "interpolate-hcl": switch (this.type.kind) {
4574
+ case "color": return Color$1.interpolate(outputLower, outputUpper, t, "hcl");
4575
+ case "colorArray": return ColorArray.interpolate(outputLower, outputUpper, t, "hcl");
4576
+ }
4577
+ case "interpolate-lab": switch (this.type.kind) {
4578
+ case "color": return Color$1.interpolate(outputLower, outputUpper, t, "lab");
4579
+ case "colorArray": return ColorArray.interpolate(outputLower, outputUpper, t, "lab");
4580
+ }
4581
+ }
4582
+ }
4583
+ eachChild(fn) {
4584
+ fn(this.input);
4585
+ for (const expression of this.outputs) fn(expression);
4586
+ }
4587
+ outputDefined() {
4588
+ return this.outputs.every((out) => out.outputDefined());
4589
+ }
4590
+ };
4591
+ /**
4592
+ * Returns a ratio that can be used to interpolate between exponential function
4593
+ * stops.
4594
+ * How it works: Two consecutive stop values define a (scaled and shifted) exponential function `f(x) = a * base^x + b`, where `base` is the user-specified base,
4595
+ * and `a` and `b` are constants affording sufficient degrees of freedom to fit
4596
+ * the function to the given stops.
4597
+ *
4598
+ * Here's a bit of algebra that lets us compute `f(x)` directly from the stop
4599
+ * values without explicitly solving for `a` and `b`:
4600
+ *
4601
+ * First stop value: `f(x0) = y0 = a * base^x0 + b`
4602
+ * Second stop value: `f(x1) = y1 = a * base^x1 + b`
4603
+ * => `y1 - y0 = a(base^x1 - base^x0)`
4604
+ * => `a = (y1 - y0)/(base^x1 - base^x0)`
4605
+ *
4606
+ * Desired value: `f(x) = y = a * base^x + b`
4607
+ * => `f(x) = y0 + a * (base^x - base^x0)`
4608
+ *
4609
+ * From the above, we can replace the `a` in `a * (base^x - base^x0)` and do a
4610
+ * little algebra:
4611
+ * ```
4612
+ * a * (base^x - base^x0) = (y1 - y0)/(base^x1 - base^x0) * (base^x - base^x0)
4613
+ * = (y1 - y0) * (base^x - base^x0) / (base^x1 - base^x0)
4614
+ * ```
4615
+ *
4616
+ * If we let `(base^x - base^x0) / (base^x1 base^x0)`, then we have
4617
+ * `f(x) = y0 + (y1 - y0) * ratio`. In other words, `ratio` may be treated as
4618
+ * an interpolation factor between the two stops' output values.
4619
+ *
4620
+ * (Note: a slightly different form for `ratio`,
4621
+ * `(base^(x-x0) - 1) / (base^(x1-x0) - 1) `, is equivalent, but requires fewer
4622
+ * expensive `Math.pow()` operations.)
4623
+ *
4624
+ * @private
4625
+ */
4626
+ function exponentialInterpolation(input, base, lowerValue, upperValue) {
4627
+ const difference = upperValue - lowerValue;
4628
+ const progress = input - lowerValue;
4629
+ if (difference === 0) return 0;
4630
+ else if (base === 1) return progress / difference;
4631
+ else return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1);
4632
+ }
4633
+ const interpolateFactory = {
4634
+ color: Color$1.interpolate,
4635
+ number: interpolateNumber,
4636
+ padding: Padding.interpolate,
4637
+ numberArray: NumberArray.interpolate,
4638
+ colorArray: ColorArray.interpolate,
4639
+ variableAnchorOffsetCollection: VariableAnchorOffsetCollection.interpolate,
4640
+ array: interpolateArray
4641
+ };
4642
+
4643
+ //#region src/expression/definitions/format.ts
4644
+ var FormatExpression = class FormatExpression {
4645
+ constructor(sections) {
4646
+ this.type = FormattedType;
4647
+ this.sections = sections;
4648
+ }
4649
+ static parse(args, context) {
4650
+ if (args.length < 2) return context.error("Expected at least one argument.");
4651
+ const firstArg = args[1];
4652
+ if (!Array.isArray(firstArg) && typeof firstArg === "object") return context.error("First argument must be an image or text section.");
4653
+ const sections = [];
4654
+ let nextTokenMayBeObject = false;
4655
+ for (let i = 1; i <= args.length - 1; ++i) {
4656
+ const arg = args[i];
4657
+ if (nextTokenMayBeObject && typeof arg === "object" && !Array.isArray(arg)) {
4658
+ nextTokenMayBeObject = false;
4659
+ let scale = null;
4660
+ if (arg["font-scale"]) {
4661
+ scale = context.parse(arg["font-scale"], 1, NumberType);
4662
+ if (!scale) return null;
4663
+ }
4664
+ let font = null;
4665
+ if (arg["text-font"]) {
4666
+ font = context.parse(arg["text-font"], 1, array(StringType));
4667
+ if (!font) return null;
4668
+ }
4669
+ let textColor = null;
4670
+ if (arg["text-color"]) {
4671
+ textColor = context.parse(arg["text-color"], 1, ColorType);
4672
+ if (!textColor) return null;
4673
+ }
4674
+ let verticalAlign = null;
4675
+ if (arg["vertical-align"]) {
4676
+ if (typeof arg["vertical-align"] === "string" && !VERTICAL_ALIGN_OPTIONS.includes(arg["vertical-align"])) return context.error(`'vertical-align' must be one of: 'bottom', 'center', 'top' but found '${arg["vertical-align"]}' instead.`);
4677
+ verticalAlign = context.parse(arg["vertical-align"], 1, StringType);
4678
+ if (!verticalAlign) return null;
4679
+ }
4680
+ const lastExpression = sections[sections.length - 1];
4681
+ lastExpression.scale = scale;
4682
+ lastExpression.font = font;
4683
+ lastExpression.textColor = textColor;
4684
+ lastExpression.verticalAlign = verticalAlign;
4685
+ } else {
4686
+ const content = context.parse(args[i], 1, ValueType);
4687
+ if (!content) return null;
4688
+ const kind = content.type.kind;
4689
+ if (kind !== "string" && kind !== "value" && kind !== "null" && kind !== "resolvedImage") return context.error("Formatted text type must be 'string', 'value', 'image' or 'null'.");
4690
+ nextTokenMayBeObject = true;
4691
+ sections.push({
4692
+ content,
4693
+ scale: null,
4694
+ font: null,
4695
+ textColor: null,
4696
+ verticalAlign: null
4697
+ });
4698
+ }
4699
+ }
4700
+ return new FormatExpression(sections);
4701
+ }
4702
+ evaluate(ctx) {
4703
+ const evaluateSection = (section) => {
4704
+ const evaluatedContent = section.content.evaluate(ctx);
4705
+ if (typeOf(evaluatedContent) === ResolvedImageType) return new FormattedSection("", evaluatedContent, null, null, null, section.verticalAlign ? section.verticalAlign.evaluate(ctx) : null);
4706
+ return new FormattedSection(valueToString(evaluatedContent), null, section.scale ? section.scale.evaluate(ctx) : null, section.font ? section.font.evaluate(ctx).join(",") : null, section.textColor ? section.textColor.evaluate(ctx) : null, section.verticalAlign ? section.verticalAlign.evaluate(ctx) : null);
4707
+ };
4708
+ return new Formatted(this.sections.map(evaluateSection));
4709
+ }
4710
+ eachChild(fn) {
4711
+ for (const section of this.sections) {
4712
+ fn(section.content);
4713
+ if (section.scale) fn(section.scale);
4714
+ if (section.font) fn(section.font);
4715
+ if (section.textColor) fn(section.textColor);
4716
+ if (section.verticalAlign) fn(section.verticalAlign);
4717
+ }
4718
+ }
4719
+ outputDefined() {
4720
+ return false;
4721
+ }
4722
+ };
4723
+
4724
+ //#region src/util/classify_rings.ts
4725
+ /**
4726
+ * Classifies an array of rings into polygons with outer rings and holes
4727
+ * @param rings - the rings to classify
4728
+ * @param maxRings - the maximum number of rings to include in a polygon, use 0 to include all rings
4729
+ * @returns an array of polygons with internal rings as holes
4730
+ */
4731
+ function classifyRings$1(rings, maxRings) {
4732
+ if (rings.length <= 1) return [rings];
4733
+ const polygons = [];
4734
+ let polygon;
4735
+ let ccw;
4736
+ for (const ring of rings) {
4737
+ const area = calculateSignedArea(ring);
4738
+ if (area === 0) continue;
4739
+ ring.area = Math.abs(area);
4740
+ if (ccw === void 0) ccw = area < 0;
4741
+ if (ccw === area < 0) {
4742
+ if (polygon) polygons.push(polygon);
4743
+ polygon = [ring];
4744
+ } else polygon.push(ring);
4745
+ }
4746
+ if (polygon) polygons.push(polygon);
4747
+ return polygons;
4748
+ }
4749
+ /**
4750
+ * Returns the signed area for the polygon ring. Positive areas are exterior rings and
4751
+ * have a clockwise winding. Negative areas are interior rings and have a counter clockwise
4752
+ * ordering.
4753
+ *
4754
+ * @param ring - Exterior or interior ring
4755
+ * @returns Signed area
4756
+ */
4757
+ function calculateSignedArea(ring) {
4758
+ let sum = 0;
4759
+ for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
4760
+ p1 = ring[i];
4761
+ p2 = ring[j];
4762
+ sum += (p2.x - p1.x) * (p1.y + p2.y);
4763
+ }
4764
+ return sum;
4765
+ }
4766
+
4767
+ //#region src/expression/definitions/assertion.ts
4768
+ const types$1 = {
4769
+ string: StringType,
4770
+ number: NumberType,
4771
+ boolean: BooleanType,
4772
+ object: ObjectType
4773
+ };
4774
+ var Assertion = class Assertion {
4775
+ constructor(type, args, key) {
4776
+ this.type = type;
4777
+ this.args = args;
4778
+ this.key = key;
4779
+ }
4780
+ static parse(args, context) {
4781
+ if (args.length < 2) return context.error("Expected at least one argument.");
4782
+ let i = 1;
4783
+ let type;
4784
+ const name = args[0];
4785
+ if (name === "array") {
4786
+ let itemType;
4787
+ if (args.length > 2) {
4788
+ const type = args[1];
4789
+ if (typeof type !== "string" || !(type in types$1) || type === "object") return context.error("The item type argument of \"array\" must be one of string, number, boolean", 1);
4790
+ itemType = types$1[type];
4791
+ i++;
4792
+ } else itemType = ValueType;
4793
+ let N;
4794
+ if (args.length > 3) {
4795
+ if (args[2] !== null && (typeof args[2] !== "number" || args[2] < 0 || args[2] !== Math.floor(args[2]))) return context.error("The length argument to \"array\" must be a positive integer literal", 2);
4796
+ N = args[2];
4797
+ i++;
4798
+ }
4799
+ type = array(itemType, N);
4800
+ } else {
4801
+ if (!types$1[name]) throw new Error(`Types doesn't contain name = ${name}`);
4802
+ type = types$1[name];
4803
+ }
4804
+ const parsed = [];
4805
+ for (; i < args.length; i++) {
4806
+ const input = context.parse(args[i], i, ValueType);
4807
+ if (!input) return null;
4808
+ parsed.push(input);
4809
+ }
4810
+ return new Assertion(type, parsed, context.key);
4811
+ }
4812
+ evaluate(ctx) {
4813
+ for (let i = 0; i < this.args.length; i++) {
4814
+ const value = this.args[i].evaluate(ctx);
4815
+ if (!checkSubtype(this.type, typeOf(value))) return value;
4816
+ else if (i === this.args.length - 1) throw new RuntimeError(`Expected value to be of type ${typeToString(this.type)}, but found ${typeToString(typeOf(value))} instead.`, this.key);
4817
+ }
4818
+ throw new Error();
4819
+ }
4820
+ eachChild(fn) {
4821
+ this.args.forEach(fn);
4822
+ }
4823
+ outputDefined() {
4824
+ return this.args.every((arg) => arg.outputDefined());
4825
+ }
4826
+ };
4827
+
4828
+ //#region src/expression/definitions/coercion.ts
4829
+ const types = {
4830
+ "to-boolean": BooleanType,
4831
+ "to-color": ColorType,
4832
+ "to-number": NumberType,
4833
+ "to-string": StringType
4834
+ };
4835
+ /**
4836
+ * Special form for error-coalescing coercion expressions "to-number",
4837
+ * "to-color". Since these coercions can fail at runtime, they accept multiple
4838
+ * arguments, only evaluating one at a time until one succeeds.
4839
+ *
4840
+ * @private
4841
+ */
4842
+ var Coercion = class Coercion {
4843
+ constructor(type, args, key) {
4844
+ this.type = type;
4845
+ this.args = args;
4423
4846
  this.key = key;
4424
4847
  }
4425
4848
  static parse(args, context) {
@@ -4521,171 +4944,7 @@ var Coercion = class Coercion {
4521
4944
  return this.args.every((arg) => arg.outputDefined());
4522
4945
  }
4523
4946
  };
4524
- //#endregion
4525
- //#region src/expression/evaluation_context.ts
4526
- const geometryTypes = [
4527
- "Unknown",
4528
- "Point",
4529
- "LineString",
4530
- "Polygon"
4531
- ];
4532
- var EvaluationContext = class {
4533
- constructor() {
4534
- this.globals = null;
4535
- this.feature = null;
4536
- this.featureState = null;
4537
- this.formattedSection = null;
4538
- this._parseColorCache = /* @__PURE__ */ new Map();
4539
- this.availableImages = null;
4540
- this.canonical = null;
4541
- }
4542
- id() {
4543
- return this.feature && "id" in this.feature ? this.feature.id : null;
4544
- }
4545
- geometryType() {
4546
- return this.feature ? typeof this.feature.type === "number" ? geometryTypes[this.feature.type] : this.feature.type : null;
4547
- }
4548
- geometry() {
4549
- return this.feature && "geometry" in this.feature ? this.feature.geometry : null;
4550
- }
4551
- canonicalID() {
4552
- return this.canonical;
4553
- }
4554
- properties() {
4555
- return this.feature && this.feature.properties || {};
4556
- }
4557
- parseColor(input) {
4558
- let cached = this._parseColorCache.get(input);
4559
- if (!cached) {
4560
- cached = Color$1.parse(input);
4561
- this._parseColorCache.set(input, cached);
4562
- }
4563
- return cached;
4564
- }
4565
- };
4566
- //#endregion
4567
- //#region src/expression/parsing_context.ts
4568
- /**
4569
- * State associated parsing at a given point in an expression tree.
4570
- * @private
4571
- */
4572
- var ParsingContext = class ParsingContext {
4573
- constructor(registry, isConstantFunc, path = [], expectedType, scope = new Scope(), errors = []) {
4574
- this.registry = registry;
4575
- this.path = path;
4576
- this.key = path.map((part) => `[${part}]`).join("");
4577
- this.scope = scope;
4578
- this.errors = errors;
4579
- this.expectedType = expectedType;
4580
- this._isConstant = isConstantFunc;
4581
- }
4582
- /**
4583
- * @param expr the JSON expression to parse
4584
- * @param index the optional argument index if this expression is an argument of a parent expression that's being parsed
4585
- * @param options
4586
- * @param options.omitTypeAnnotations set true to omit inferred type annotations. Caller beware: with this option set, the parsed expression's type will NOT satisfy `expectedType` if it would normally be wrapped in an inferred annotation.
4587
- * @private
4588
- */
4589
- parse(expr, index, expectedType, bindings, options = {}) {
4590
- if (index) return this.concat(index, expectedType, bindings)._parse(expr, options);
4591
- return this._parse(expr, options);
4592
- }
4593
- _parse(expr, options) {
4594
- if (expr === null || typeof expr === "string" || typeof expr === "boolean" || typeof expr === "number") expr = ["literal", expr];
4595
- const key = this.key;
4596
- function annotate(parsed, type, typeAnnotation) {
4597
- if (typeAnnotation === "assert") return new Assertion(type, [parsed], key);
4598
- else if (typeAnnotation === "coerce") return new Coercion(type, [parsed], key);
4599
- else return parsed;
4600
- }
4601
- if (Array.isArray(expr)) {
4602
- if (expr.length === 0) return this.error("Expected an array with at least one element. If you wanted a literal array, use [\"literal\", []].");
4603
- const op = expr[0];
4604
- if (typeof op !== "string") {
4605
- this.error(`Expression name must be a string, but found ${typeof op} instead. If you wanted a literal array, use ["literal", [...]].`, 0);
4606
- return null;
4607
- }
4608
- const Expr = this.registry[op];
4609
- if (Expr) {
4610
- let parsed = Expr.parse(expr, this);
4611
- if (!parsed) return null;
4612
- if (this.expectedType) {
4613
- const expected = this.expectedType;
4614
- const actual = parsed.type;
4615
- if ((expected.kind === "string" || expected.kind === "number" || expected.kind === "boolean" || expected.kind === "object" || expected.kind === "array") && actual.kind === "value") parsed = annotate(parsed, expected, options.typeAnnotation || "assert");
4616
- else if ("projectionDefinition" === expected.kind && [
4617
- "string",
4618
- "array",
4619
- "value"
4620
- ].includes(actual.kind) || [
4621
- "color",
4622
- "formatted",
4623
- "resolvedImage"
4624
- ].includes(expected.kind) && ["value", "string"].includes(actual.kind) || ["padding", "numberArray"].includes(expected.kind) && [
4625
- "value",
4626
- "number",
4627
- "array"
4628
- ].includes(actual.kind) || "colorArray" === expected.kind && [
4629
- "value",
4630
- "string",
4631
- "array"
4632
- ].includes(actual.kind) || "variableAnchorOffsetCollection" === expected.kind && ["value", "array"].includes(actual.kind)) parsed = annotate(parsed, expected, options.typeAnnotation || "coerce");
4633
- else if (this.checkSubtype(expected, actual)) return null;
4634
- }
4635
- if (!(parsed instanceof Literal) && parsed.type.kind !== "resolvedImage" && this._isConstant(parsed)) {
4636
- const ec = new EvaluationContext();
4637
- try {
4638
- parsed = new Literal(parsed.type, parsed.evaluate(ec));
4639
- } catch (e) {
4640
- this.error(e.message);
4641
- return null;
4642
- }
4643
- }
4644
- return parsed;
4645
- }
4646
- return this.error(`Unknown expression "${op}". If you wanted a literal array, use ["literal", [...]].`, 0);
4647
- } else if (typeof expr === "undefined") return this.error("'undefined' value invalid. Use null instead.");
4648
- else if (typeof expr === "object") return this.error("Bare objects invalid. Use [\"literal\", {...}] instead.");
4649
- else return this.error(`Expected an array, but found ${typeof expr} instead.`);
4650
- }
4651
- /**
4652
- * Returns a copy of this context suitable for parsing the subexpression at
4653
- * index `index`, optionally appending to 'let' binding map.
4654
- *
4655
- * Note that `errors` property, intended for collecting errors while
4656
- * parsing, is copied by reference rather than cloned.
4657
- * @private
4658
- */
4659
- concat(index, expectedType, bindings) {
4660
- const path = typeof index === "number" ? this.path.concat(index) : this.path;
4661
- const scope = bindings ? this.scope.concat(bindings) : this.scope;
4662
- return new ParsingContext(this.registry, this._isConstant, path, expectedType || null, scope, this.errors);
4663
- }
4664
- /**
4665
- * Push a parsing (or type checking) error into the `this.errors`
4666
- * @param error The message
4667
- * @param keys Optionally specify the source of the error at a child
4668
- * of the current expression at `this.key`.
4669
- * @private
4670
- */
4671
- error(error, ...keys) {
4672
- const key = `${this.key}${keys.map((k) => `[${k}]`).join("")}`;
4673
- this.errors.push(new ExpressionParsingError(key, error));
4674
- }
4675
- /**
4676
- * Returns null if `t` is a subtype of `expected`; otherwise returns an
4677
- * error message and also pushes it to `this.errors`.
4678
- * @param expected The expected type
4679
- * @param t The actual type
4680
- * @returns null if `t` is a subtype of `expected`; otherwise returns an error message
4681
- */
4682
- checkSubtype(expected, t) {
4683
- const error = checkSubtype(expected, t);
4684
- if (error) this.error(error);
4685
- return error;
4686
- }
4687
- };
4688
- //#endregion
4947
+
4689
4948
  //#region src/expression/definitions/let.ts
4690
4949
  var Let = class Let {
4691
4950
  constructor(bindings, result) {
@@ -4719,7 +4978,7 @@ var Let = class Let {
4719
4978
  return this.result.outputDefined();
4720
4979
  }
4721
4980
  };
4722
- //#endregion
4981
+
4723
4982
  //#region src/expression/definitions/var.ts
4724
4983
  var Var = class Var {
4725
4984
  constructor(name, boundExpression) {
@@ -4741,7 +5000,7 @@ var Var = class Var {
4741
5000
  return false;
4742
5001
  }
4743
5002
  };
4744
- //#endregion
5003
+
4745
5004
  //#region src/expression/definitions/at.ts
4746
5005
  var At = class At {
4747
5006
  constructor(type, index, input, key) {
@@ -4774,7 +5033,7 @@ var At = class At {
4774
5033
  return false;
4775
5034
  }
4776
5035
  };
4777
- //#endregion
5036
+
4778
5037
  //#region src/expression/definitions/in.ts
4779
5038
  var In = class In {
4780
5039
  constructor(needle, haystack, key) {
@@ -4818,7 +5077,7 @@ var In = class In {
4818
5077
  return true;
4819
5078
  }
4820
5079
  };
4821
- //#endregion
5080
+
4822
5081
  //#region src/expression/definitions/index_of.ts
4823
5082
  var IndexOf = class IndexOf {
4824
5083
  constructor(needle, haystack, key, fromIndex) {
@@ -4873,7 +5132,7 @@ var IndexOf = class IndexOf {
4873
5132
  return false;
4874
5133
  }
4875
5134
  };
4876
- //#endregion
5135
+
4877
5136
  //#region src/expression/definitions/match.ts
4878
5137
  var Match = class Match {
4879
5138
  constructor(inputType, outputType, input, cases, outputs, otherwise) {
@@ -4932,366 +5191,93 @@ var Match = class Match {
4932
5191
  return this.outputs.every((out) => out.outputDefined()) && this.otherwise.outputDefined();
4933
5192
  }
4934
5193
  };
4935
- //#endregion
4936
- //#region src/expression/definitions/case.ts
4937
- var Case = class Case {
4938
- constructor(type, branches, otherwise) {
4939
- this.type = type;
4940
- this.branches = branches;
4941
- this.otherwise = otherwise;
4942
- }
4943
- static parse(args, context) {
4944
- if (args.length < 4) return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`);
4945
- if (args.length % 2 !== 0) return context.error("Expected an odd number of arguments.");
4946
- let outputType;
4947
- if (context.expectedType && context.expectedType.kind !== "value") outputType = context.expectedType;
4948
- const branches = [];
4949
- for (let i = 1; i < args.length - 1; i += 2) {
4950
- const test = context.parse(args[i], i, BooleanType);
4951
- if (!test) return null;
4952
- const result = context.parse(args[i + 1], i + 1, outputType);
4953
- if (!result) return null;
4954
- branches.push([test, result]);
4955
- outputType = outputType || result.type;
4956
- }
4957
- const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
4958
- if (!otherwise) return null;
4959
- if (!outputType) throw new Error("Can't infer output type");
4960
- return new Case(outputType, branches, otherwise);
4961
- }
4962
- evaluate(ctx) {
4963
- for (const [test, expression] of this.branches) if (test.evaluate(ctx)) return expression.evaluate(ctx);
4964
- return this.otherwise.evaluate(ctx);
4965
- }
4966
- eachChild(fn) {
4967
- for (const [test, expression] of this.branches) {
4968
- fn(test);
4969
- fn(expression);
4970
- }
4971
- fn(this.otherwise);
4972
- }
4973
- outputDefined() {
4974
- return this.branches.every(([_, out]) => out.outputDefined()) && this.otherwise.outputDefined();
4975
- }
4976
- };
4977
- //#endregion
4978
- //#region src/expression/definitions/slice.ts
4979
- var Slice = class Slice {
4980
- constructor(type, input, beginIndex, key, endIndex) {
4981
- this.type = type;
4982
- this.input = input;
4983
- this.beginIndex = beginIndex;
4984
- this.key = key;
4985
- this.endIndex = endIndex;
4986
- }
4987
- static parse(args, context) {
4988
- if (args.length <= 2 || args.length >= 5) return context.error(`Expected 2 or 3 arguments, but found ${args.length - 1} instead.`);
4989
- const input = context.parse(args[1], 1, ValueType);
4990
- const beginIndex = context.parse(args[2], 2, NumberType);
4991
- if (!input || !beginIndex) return null;
4992
- if (!isValidType(input.type, [
4993
- array(ValueType),
4994
- StringType,
4995
- ValueType
4996
- ])) return context.error(`Expected first argument to be of type array or string, but found ${typeToString(input.type)} instead`);
4997
- if (args.length === 4) {
4998
- const endIndex = context.parse(args[3], 3, NumberType);
4999
- if (!endIndex) return null;
5000
- return new Slice(input.type, input, beginIndex, context.key, endIndex);
5001
- } else return new Slice(input.type, input, beginIndex, context.key);
5002
- }
5003
- evaluate(ctx) {
5004
- const input = this.input.evaluate(ctx);
5005
- const beginIndex = this.beginIndex.evaluate(ctx);
5006
- let endIndex;
5007
- if (this.endIndex) endIndex = this.endIndex.evaluate(ctx);
5008
- if (isValidNativeType(input, ["string"])) return [...input].slice(beginIndex, endIndex).join("");
5009
- else if (isValidNativeType(input, ["array"])) return input.slice(beginIndex, endIndex);
5010
- else throw new RuntimeError(`Expected first argument to be of type array or string, but found ${typeToString(typeOf(input))} instead.`, this.key);
5011
- }
5012
- eachChild(fn) {
5013
- fn(this.input);
5014
- fn(this.beginIndex);
5015
- if (this.endIndex) fn(this.endIndex);
5016
- }
5017
- outputDefined() {
5018
- return false;
5019
- }
5020
- };
5021
- //#endregion
5022
- //#region src/expression/stops.ts
5023
- /**
5024
- * Returns the index of the last stop <= input, or 0 if it doesn't exist.
5025
- * @private
5026
- */
5027
- function findStopLessThanOrEqualTo(stops, input, key) {
5028
- const lastIndex = stops.length - 1;
5029
- let lowerIndex = 0;
5030
- let upperIndex = lastIndex;
5031
- let currentIndex = 0;
5032
- let currentValue, nextValue;
5033
- while (lowerIndex <= upperIndex) {
5034
- currentIndex = Math.floor((lowerIndex + upperIndex) / 2);
5035
- currentValue = stops[currentIndex];
5036
- nextValue = stops[currentIndex + 1];
5037
- if (currentValue <= input) {
5038
- if (currentIndex === lastIndex || input < nextValue) return currentIndex;
5039
- lowerIndex = currentIndex + 1;
5040
- } else if (currentValue > input) upperIndex = currentIndex - 1;
5041
- else throw new RuntimeError("Input is not a number.", key);
5042
- }
5043
- return 0;
5044
- }
5045
- //#endregion
5046
- //#region src/expression/definitions/step.ts
5047
- var Step = class Step {
5048
- constructor(type, input, stops, key) {
5049
- this.type = type;
5050
- this.input = input;
5051
- this.key = key;
5052
- this.labels = [];
5053
- this.outputs = [];
5054
- for (const [label, expression] of stops) {
5055
- this.labels.push(label);
5056
- this.outputs.push(expression);
5057
- }
5058
- }
5059
- static parse(args, context) {
5060
- if (args.length - 1 < 4) return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
5061
- if ((args.length - 1) % 2 !== 0) return context.error("Expected an even number of arguments.");
5062
- const input = context.parse(args[1], 1, NumberType);
5063
- if (!input) return null;
5064
- const stops = [];
5065
- let outputType = null;
5066
- if (context.expectedType && context.expectedType.kind !== "value") outputType = context.expectedType;
5067
- for (let i = 1; i < args.length; i += 2) {
5068
- const label = i === 1 ? -Infinity : args[i];
5069
- const value = args[i + 1];
5070
- const labelKey = i;
5071
- const valueKey = i + 1;
5072
- if (typeof label !== "number") return context.error("Input/output pairs for \"step\" expressions must be defined using literal numeric values (not computed expressions) for the input values.", labelKey);
5073
- if (stops.length && stops[stops.length - 1][0] >= label) return context.error("Input/output pairs for \"step\" expressions must be arranged with input values in strictly ascending order.", labelKey);
5074
- const parsed = context.parse(value, valueKey, outputType);
5075
- if (!parsed) return null;
5076
- outputType = outputType || parsed.type;
5077
- stops.push([label, parsed]);
5078
- }
5079
- return new Step(outputType, input, stops, context.key);
5080
- }
5081
- evaluate(ctx) {
5082
- const labels = this.labels;
5083
- const outputs = this.outputs;
5084
- if (labels.length === 1) return outputs[0].evaluate(ctx);
5085
- const value = this.input.evaluate(ctx);
5086
- if (value <= labels[0]) return outputs[0].evaluate(ctx);
5087
- const stopCount = labels.length;
5088
- if (value >= labels[stopCount - 1]) return outputs[stopCount - 1].evaluate(ctx);
5089
- return outputs[findStopLessThanOrEqualTo(labels, value, this.key)].evaluate(ctx);
5090
- }
5091
- eachChild(fn) {
5092
- fn(this.input);
5093
- for (const expression of this.outputs) fn(expression);
5094
- }
5095
- outputDefined() {
5096
- return this.outputs.every((out) => out.outputDefined());
5097
- }
5098
- };
5099
- //#endregion
5100
- //#region node_modules/@mapbox/unitbezier/index.js
5101
- function unitBezier(p1x, p1y, p2x, p2y) {
5102
- const cx = 3 * p1x;
5103
- const bx = 3 * (p2x - p1x) - cx;
5104
- const ax = 1 - cx - bx;
5105
- const cy = 3 * p1y;
5106
- const by = 3 * (p2y - p1y) - cy;
5107
- const ay = 1 - cy - by;
5108
- return function solve(x, epsilon = 1e-6) {
5109
- if (x <= 0) return 0;
5110
- if (x >= 1) return 1;
5111
- let t = x;
5112
- for (let i = 0; i < 8; i++) {
5113
- const x2 = ((ax * t + bx) * t + cx) * t - x;
5114
- if (Math.abs(x2) < epsilon) return ((ay * t + by) * t + cy) * t;
5115
- const d2 = (3 * ax * t + 2 * bx) * t + cx;
5116
- if (Math.abs(d2) < 1e-6) break;
5117
- t -= x2 / d2;
5118
- }
5119
- let t0 = 0;
5120
- let t1 = 1;
5121
- t = x;
5122
- for (let i = 0; i < 20; i++) {
5123
- const x2 = ((ax * t + bx) * t + cx) * t;
5124
- if (Math.abs(x2 - x) < epsilon) break;
5125
- if (x > x2) t0 = t;
5126
- else t1 = t;
5127
- t = (t0 + t1) * .5;
5128
- }
5129
- return ((ay * t + by) * t + cy) * t;
5130
- };
5131
- }
5132
- //#endregion
5133
- //#region src/expression/definitions/interpolate.ts
5134
- var Interpolate = class Interpolate {
5135
- constructor(type, operator, interpolation, input, stops, key) {
5136
- this.type = type;
5137
- this.operator = operator;
5138
- this.interpolation = interpolation;
5139
- this.input = input;
5140
- this.key = key;
5141
- this.labels = [];
5142
- this.outputs = [];
5143
- for (const [label, expression] of stops) {
5144
- this.labels.push(label);
5145
- this.outputs.push(expression);
5146
- }
5147
- }
5148
- static interpolationFactor(interpolation, input, lower, upper) {
5149
- let t = 0;
5150
- if (interpolation.name === "exponential") t = exponentialInterpolation(input, interpolation.base, lower, upper);
5151
- else if (interpolation.name === "linear") t = exponentialInterpolation(input, 1, lower, upper);
5152
- else if (interpolation.name === "cubic-bezier") {
5153
- const c = interpolation.controlPoints;
5154
- t = unitBezier(c[0], c[1], c[2], c[3])(exponentialInterpolation(input, 1, lower, upper));
5155
- }
5156
- return t;
5157
- }
5158
- static parse(args, context) {
5159
- let [operator, interpolation, input, ...rest] = args;
5160
- if (!Array.isArray(interpolation) || interpolation.length === 0) return context.error("Expected an interpolation type expression.", 1);
5161
- if (interpolation[0] === "linear") interpolation = { name: "linear" };
5162
- else if (interpolation[0] === "exponential") {
5163
- const base = interpolation[1];
5164
- if (typeof base !== "number") return context.error("Exponential interpolation requires a numeric base.", 1, 1);
5165
- interpolation = {
5166
- name: "exponential",
5167
- base
5168
- };
5169
- } else if (interpolation[0] === "cubic-bezier") {
5170
- const controlPoints = interpolation.slice(1);
5171
- if (controlPoints.length !== 4 || controlPoints.some((t) => typeof t !== "number" || t < 0 || t > 1)) return context.error("Cubic bezier interpolation requires four numeric arguments with values between 0 and 1.", 1);
5172
- interpolation = {
5173
- name: "cubic-bezier",
5174
- controlPoints
5175
- };
5176
- } else return context.error(`Unknown interpolation type ${String(interpolation[0])}`, 1, 0);
5177
- if (args.length - 1 < 4) return context.error(`Expected at least 4 arguments, but found only ${args.length - 1}.`);
5178
- if ((args.length - 1) % 2 !== 0) return context.error("Expected an even number of arguments.");
5179
- input = context.parse(input, 2, NumberType);
5180
- if (!input) return null;
5181
- const stops = [];
5182
- let outputType = null;
5183
- if ((operator === "interpolate-hcl" || operator === "interpolate-lab") && context.expectedType != ColorArrayType) outputType = ColorType;
5184
- else if (context.expectedType && context.expectedType.kind !== "value") outputType = context.expectedType;
5185
- for (let i = 0; i < rest.length; i += 2) {
5186
- const label = rest[i];
5187
- const value = rest[i + 1];
5188
- const labelKey = i + 3;
5189
- const valueKey = i + 4;
5190
- if (typeof label !== "number") return context.error("Input/output pairs for \"interpolate\" expressions must be defined using literal numeric values (not computed expressions) for the input values.", labelKey);
5191
- if (stops.length && stops[stops.length - 1][0] >= label) return context.error("Input/output pairs for \"interpolate\" expressions must be arranged with input values in strictly ascending order.", labelKey);
5192
- const parsed = context.parse(value, valueKey, outputType);
5193
- if (!parsed) return null;
5194
- outputType = outputType || parsed.type;
5195
- stops.push([label, parsed]);
5194
+
5195
+ //#region src/expression/definitions/case.ts
5196
+ var Case = class Case {
5197
+ constructor(type, branches, otherwise) {
5198
+ this.type = type;
5199
+ this.branches = branches;
5200
+ this.otherwise = otherwise;
5201
+ }
5202
+ static parse(args, context) {
5203
+ if (args.length < 4) return context.error(`Expected at least 3 arguments, but found only ${args.length - 1}.`);
5204
+ if (args.length % 2 !== 0) return context.error("Expected an odd number of arguments.");
5205
+ let outputType;
5206
+ if (context.expectedType && context.expectedType.kind !== "value") outputType = context.expectedType;
5207
+ const branches = [];
5208
+ for (let i = 1; i < args.length - 1; i += 2) {
5209
+ const test = context.parse(args[i], i, BooleanType);
5210
+ if (!test) return null;
5211
+ const result = context.parse(args[i + 1], i + 1, outputType);
5212
+ if (!result) return null;
5213
+ branches.push([test, result]);
5214
+ outputType = outputType || result.type;
5196
5215
  }
5197
- if (!verifyType(outputType, NumberType) && !verifyType(outputType, ProjectionDefinitionType) && !verifyType(outputType, ColorType) && !verifyType(outputType, PaddingType) && !verifyType(outputType, NumberArrayType) && !verifyType(outputType, ColorArrayType) && !verifyType(outputType, VariableAnchorOffsetCollectionType) && !verifyType(outputType, array(NumberType))) return context.error(`Type ${typeToString(outputType)} is not interpolatable.`);
5198
- return new Interpolate(outputType, operator, interpolation, input, stops, context.key);
5216
+ const otherwise = context.parse(args[args.length - 1], args.length - 1, outputType);
5217
+ if (!otherwise) return null;
5218
+ if (!outputType) throw new Error("Can't infer output type");
5219
+ return new Case(outputType, branches, otherwise);
5199
5220
  }
5200
5221
  evaluate(ctx) {
5201
- const labels = this.labels;
5202
- const outputs = this.outputs;
5203
- if (labels.length === 1) return outputs[0].evaluate(ctx);
5204
- const value = this.input.evaluate(ctx);
5205
- if (value <= labels[0]) return outputs[0].evaluate(ctx);
5206
- const stopCount = labels.length;
5207
- if (value >= labels[stopCount - 1]) return outputs[stopCount - 1].evaluate(ctx);
5208
- const index = findStopLessThanOrEqualTo(labels, value, this.key);
5209
- const lower = labels[index];
5210
- const upper = labels[index + 1];
5211
- const t = Interpolate.interpolationFactor(this.interpolation, value, lower, upper);
5212
- const outputLower = outputs[index].evaluate(ctx);
5213
- const outputUpper = outputs[index + 1].evaluate(ctx);
5214
- switch (this.operator) {
5215
- case "interpolate": switch (this.type.kind) {
5216
- case "number": return interpolateNumber(outputLower, outputUpper, t);
5217
- case "color": return Color$1.interpolate(outputLower, outputUpper, t);
5218
- case "padding": return Padding.interpolate(outputLower, outputUpper, t);
5219
- case "colorArray": return ColorArray.interpolate(outputLower, outputUpper, t);
5220
- case "numberArray": return NumberArray.interpolate(outputLower, outputUpper, t);
5221
- case "variableAnchorOffsetCollection": return VariableAnchorOffsetCollection.interpolate(outputLower, outputUpper, t, this.key);
5222
- case "array": return interpolateArray(outputLower, outputUpper, t);
5223
- case "projectionDefinition": return ProjectionDefinition.interpolate(outputLower, outputUpper, t);
5224
- }
5225
- case "interpolate-hcl": switch (this.type.kind) {
5226
- case "color": return Color$1.interpolate(outputLower, outputUpper, t, "hcl");
5227
- case "colorArray": return ColorArray.interpolate(outputLower, outputUpper, t, "hcl");
5228
- }
5229
- case "interpolate-lab": switch (this.type.kind) {
5230
- case "color": return Color$1.interpolate(outputLower, outputUpper, t, "lab");
5231
- case "colorArray": return ColorArray.interpolate(outputLower, outputUpper, t, "lab");
5232
- }
5222
+ for (const [test, expression] of this.branches) if (test.evaluate(ctx)) return expression.evaluate(ctx);
5223
+ return this.otherwise.evaluate(ctx);
5224
+ }
5225
+ eachChild(fn) {
5226
+ for (const [test, expression] of this.branches) {
5227
+ fn(test);
5228
+ fn(expression);
5233
5229
  }
5230
+ fn(this.otherwise);
5231
+ }
5232
+ outputDefined() {
5233
+ return this.branches.every(([_, out]) => out.outputDefined()) && this.otherwise.outputDefined();
5234
+ }
5235
+ };
5236
+
5237
+ //#region src/expression/definitions/slice.ts
5238
+ var Slice = class Slice {
5239
+ constructor(type, input, beginIndex, key, endIndex) {
5240
+ this.type = type;
5241
+ this.input = input;
5242
+ this.beginIndex = beginIndex;
5243
+ this.key = key;
5244
+ this.endIndex = endIndex;
5245
+ }
5246
+ static parse(args, context) {
5247
+ if (args.length <= 2 || args.length >= 5) return context.error(`Expected 2 or 3 arguments, but found ${args.length - 1} instead.`);
5248
+ const input = context.parse(args[1], 1, ValueType);
5249
+ const beginIndex = context.parse(args[2], 2, NumberType);
5250
+ if (!input || !beginIndex) return null;
5251
+ if (!isValidType(input.type, [
5252
+ array(ValueType),
5253
+ StringType,
5254
+ ValueType
5255
+ ])) return context.error(`Expected first argument to be of type array or string, but found ${typeToString(input.type)} instead`);
5256
+ if (args.length === 4) {
5257
+ const endIndex = context.parse(args[3], 3, NumberType);
5258
+ if (!endIndex) return null;
5259
+ return new Slice(input.type, input, beginIndex, context.key, endIndex);
5260
+ } else return new Slice(input.type, input, beginIndex, context.key);
5261
+ }
5262
+ evaluate(ctx) {
5263
+ const input = this.input.evaluate(ctx);
5264
+ const beginIndex = this.beginIndex.evaluate(ctx);
5265
+ let endIndex;
5266
+ if (this.endIndex) endIndex = this.endIndex.evaluate(ctx);
5267
+ if (isValidNativeType(input, ["string"])) return [...input].slice(beginIndex, endIndex).join("");
5268
+ else if (isValidNativeType(input, ["array"])) return input.slice(beginIndex, endIndex);
5269
+ else throw new RuntimeError(`Expected first argument to be of type array or string, but found ${typeToString(typeOf(input))} instead.`, this.key);
5234
5270
  }
5235
5271
  eachChild(fn) {
5236
5272
  fn(this.input);
5237
- for (const expression of this.outputs) fn(expression);
5273
+ fn(this.beginIndex);
5274
+ if (this.endIndex) fn(this.endIndex);
5238
5275
  }
5239
5276
  outputDefined() {
5240
- return this.outputs.every((out) => out.outputDefined());
5277
+ return false;
5241
5278
  }
5242
5279
  };
5243
- /**
5244
- * Returns a ratio that can be used to interpolate between exponential function
5245
- * stops.
5246
- * How it works: Two consecutive stop values define a (scaled and shifted) exponential function `f(x) = a * base^x + b`, where `base` is the user-specified base,
5247
- * and `a` and `b` are constants affording sufficient degrees of freedom to fit
5248
- * the function to the given stops.
5249
- *
5250
- * Here's a bit of algebra that lets us compute `f(x)` directly from the stop
5251
- * values without explicitly solving for `a` and `b`:
5252
- *
5253
- * First stop value: `f(x0) = y0 = a * base^x0 + b`
5254
- * Second stop value: `f(x1) = y1 = a * base^x1 + b`
5255
- * => `y1 - y0 = a(base^x1 - base^x0)`
5256
- * => `a = (y1 - y0)/(base^x1 - base^x0)`
5257
- *
5258
- * Desired value: `f(x) = y = a * base^x + b`
5259
- * => `f(x) = y0 + a * (base^x - base^x0)`
5260
- *
5261
- * From the above, we can replace the `a` in `a * (base^x - base^x0)` and do a
5262
- * little algebra:
5263
- * ```
5264
- * a * (base^x - base^x0) = (y1 - y0)/(base^x1 - base^x0) * (base^x - base^x0)
5265
- * = (y1 - y0) * (base^x - base^x0) / (base^x1 - base^x0)
5266
- * ```
5267
- *
5268
- * If we let `(base^x - base^x0) / (base^x1 base^x0)`, then we have
5269
- * `f(x) = y0 + (y1 - y0) * ratio`. In other words, `ratio` may be treated as
5270
- * an interpolation factor between the two stops' output values.
5271
- *
5272
- * (Note: a slightly different form for `ratio`,
5273
- * `(base^(x-x0) - 1) / (base^(x1-x0) - 1) `, is equivalent, but requires fewer
5274
- * expensive `Math.pow()` operations.)
5275
- *
5276
- * @private
5277
- */
5278
- function exponentialInterpolation(input, base, lowerValue, upperValue) {
5279
- const difference = upperValue - lowerValue;
5280
- const progress = input - lowerValue;
5281
- if (difference === 0) return 0;
5282
- else if (base === 1) return progress / difference;
5283
- else return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1);
5284
- }
5285
- const interpolateFactory = {
5286
- color: Color$1.interpolate,
5287
- number: interpolateNumber,
5288
- padding: Padding.interpolate,
5289
- numberArray: NumberArray.interpolate,
5290
- colorArray: ColorArray.interpolate,
5291
- variableAnchorOffsetCollection: VariableAnchorOffsetCollection.interpolate,
5292
- array: interpolateArray
5293
- };
5294
- //#endregion
5280
+
5295
5281
  //#region src/expression/definitions/coalesce.ts
5296
5282
  var Coalesce = class Coalesce {
5297
5283
  constructor(type, args) {
@@ -5336,7 +5322,7 @@ var Coalesce = class Coalesce {
5336
5322
  return this.args.every((arg) => arg.outputDefined());
5337
5323
  }
5338
5324
  };
5339
- //#endregion
5325
+
5340
5326
  //#region src/expression/definitions/comparison.ts
5341
5327
  function isComparableType(op, type) {
5342
5328
  if (op === "==" || op === "!=") return type.kind === "boolean" || type.kind === "string" || type.kind === "number" || type.kind === "null" || type.kind === "value";
@@ -5459,7 +5445,7 @@ const LessThan = makeComparison("<", lt, ltCollate);
5459
5445
  const GreaterThan = makeComparison(">", gt, gtCollate);
5460
5446
  const LessThanOrEqual = makeComparison("<=", lteq, lteqCollate);
5461
5447
  const GreaterThanOrEqual = makeComparison(">=", gteq, gteqCollate);
5462
- //#endregion
5448
+
5463
5449
  //#region src/expression/definitions/collator.ts
5464
5450
  var CollatorExpression = class CollatorExpression {
5465
5451
  constructor(caseSensitive, diacriticSensitive, locale) {
@@ -5495,7 +5481,7 @@ var CollatorExpression = class CollatorExpression {
5495
5481
  return false;
5496
5482
  }
5497
5483
  };
5498
- //#endregion
5484
+
5499
5485
  //#region src/expression/definitions/number_format.ts
5500
5486
  var NumberFormat = class NumberFormat {
5501
5487
  constructor(number, locale, currency, unit, minFractionDigits, maxFractionDigits) {
@@ -5546,104 +5532,23 @@ var NumberFormat = class NumberFormat {
5546
5532
  style: this.currency ? "currency" : this.unit ? "unit" : "decimal",
5547
5533
  currency: this.currency ? this.currency.evaluate(ctx) : void 0,
5548
5534
  unit: this.unit ? this.unit.evaluate(ctx) : void 0,
5549
- minimumFractionDigits: this.minFractionDigits ? this.minFractionDigits.evaluate(ctx) : void 0,
5550
- maximumFractionDigits: this.maxFractionDigits ? this.maxFractionDigits.evaluate(ctx) : void 0
5551
- }).format(this.number.evaluate(ctx));
5552
- }
5553
- eachChild(fn) {
5554
- fn(this.number);
5555
- if (this.locale) fn(this.locale);
5556
- if (this.currency) fn(this.currency);
5557
- if (this.unit) fn(this.unit);
5558
- if (this.minFractionDigits) fn(this.minFractionDigits);
5559
- if (this.maxFractionDigits) fn(this.maxFractionDigits);
5560
- }
5561
- outputDefined() {
5562
- return false;
5563
- }
5564
- };
5565
- //#endregion
5566
- //#region src/expression/definitions/format.ts
5567
- var FormatExpression = class FormatExpression {
5568
- constructor(sections) {
5569
- this.type = FormattedType;
5570
- this.sections = sections;
5571
- }
5572
- static parse(args, context) {
5573
- if (args.length < 2) return context.error("Expected at least one argument.");
5574
- const firstArg = args[1];
5575
- if (!Array.isArray(firstArg) && typeof firstArg === "object") return context.error("First argument must be an image or text section.");
5576
- const sections = [];
5577
- let nextTokenMayBeObject = false;
5578
- for (let i = 1; i <= args.length - 1; ++i) {
5579
- const arg = args[i];
5580
- if (nextTokenMayBeObject && typeof arg === "object" && !Array.isArray(arg)) {
5581
- nextTokenMayBeObject = false;
5582
- let scale = null;
5583
- if (arg["font-scale"]) {
5584
- scale = context.parse(arg["font-scale"], 1, NumberType);
5585
- if (!scale) return null;
5586
- }
5587
- let font = null;
5588
- if (arg["text-font"]) {
5589
- font = context.parse(arg["text-font"], 1, array(StringType));
5590
- if (!font) return null;
5591
- }
5592
- let textColor = null;
5593
- if (arg["text-color"]) {
5594
- textColor = context.parse(arg["text-color"], 1, ColorType);
5595
- if (!textColor) return null;
5596
- }
5597
- let verticalAlign = null;
5598
- if (arg["vertical-align"]) {
5599
- if (typeof arg["vertical-align"] === "string" && !VERTICAL_ALIGN_OPTIONS.includes(arg["vertical-align"])) return context.error(`'vertical-align' must be one of: 'bottom', 'center', 'top' but found '${arg["vertical-align"]}' instead.`);
5600
- verticalAlign = context.parse(arg["vertical-align"], 1, StringType);
5601
- if (!verticalAlign) return null;
5602
- }
5603
- const lastExpression = sections[sections.length - 1];
5604
- lastExpression.scale = scale;
5605
- lastExpression.font = font;
5606
- lastExpression.textColor = textColor;
5607
- lastExpression.verticalAlign = verticalAlign;
5608
- } else {
5609
- const content = context.parse(args[i], 1, ValueType);
5610
- if (!content) return null;
5611
- const kind = content.type.kind;
5612
- if (kind !== "string" && kind !== "value" && kind !== "null" && kind !== "resolvedImage") return context.error("Formatted text type must be 'string', 'value', 'image' or 'null'.");
5613
- nextTokenMayBeObject = true;
5614
- sections.push({
5615
- content,
5616
- scale: null,
5617
- font: null,
5618
- textColor: null,
5619
- verticalAlign: null
5620
- });
5621
- }
5622
- }
5623
- return new FormatExpression(sections);
5624
- }
5625
- evaluate(ctx) {
5626
- const evaluateSection = (section) => {
5627
- const evaluatedContent = section.content.evaluate(ctx);
5628
- if (typeOf(evaluatedContent) === ResolvedImageType) return new FormattedSection("", evaluatedContent, null, null, null, section.verticalAlign ? section.verticalAlign.evaluate(ctx) : null);
5629
- return new FormattedSection(valueToString(evaluatedContent), null, section.scale ? section.scale.evaluate(ctx) : null, section.font ? section.font.evaluate(ctx).join(",") : null, section.textColor ? section.textColor.evaluate(ctx) : null, section.verticalAlign ? section.verticalAlign.evaluate(ctx) : null);
5630
- };
5631
- return new Formatted(this.sections.map(evaluateSection));
5632
- }
5633
- eachChild(fn) {
5634
- for (const section of this.sections) {
5635
- fn(section.content);
5636
- if (section.scale) fn(section.scale);
5637
- if (section.font) fn(section.font);
5638
- if (section.textColor) fn(section.textColor);
5639
- if (section.verticalAlign) fn(section.verticalAlign);
5640
- }
5535
+ minimumFractionDigits: this.minFractionDigits ? this.minFractionDigits.evaluate(ctx) : void 0,
5536
+ maximumFractionDigits: this.maxFractionDigits ? this.maxFractionDigits.evaluate(ctx) : void 0
5537
+ }).format(this.number.evaluate(ctx));
5538
+ }
5539
+ eachChild(fn) {
5540
+ fn(this.number);
5541
+ if (this.locale) fn(this.locale);
5542
+ if (this.currency) fn(this.currency);
5543
+ if (this.unit) fn(this.unit);
5544
+ if (this.minFractionDigits) fn(this.minFractionDigits);
5545
+ if (this.maxFractionDigits) fn(this.maxFractionDigits);
5641
5546
  }
5642
5547
  outputDefined() {
5643
5548
  return false;
5644
5549
  }
5645
5550
  };
5646
- //#endregion
5551
+
5647
5552
  //#region src/expression/definitions/image.ts
5648
5553
  var ImageExpression = class ImageExpression {
5649
5554
  constructor(input) {
@@ -5669,7 +5574,7 @@ var ImageExpression = class ImageExpression {
5669
5574
  return false;
5670
5575
  }
5671
5576
  };
5672
- //#endregion
5577
+
5673
5578
  //#region src/expression/definitions/length.ts
5674
5579
  var Length = class Length {
5675
5580
  constructor(input, key) {
@@ -5697,7 +5602,7 @@ var Length = class Length {
5697
5602
  return false;
5698
5603
  }
5699
5604
  };
5700
- //#endregion
5605
+
5701
5606
  //#region src/util/geometry_util.ts
5702
5607
  const EXTENT = 8192;
5703
5608
  function getTileCoordinates(p, canonical) {
@@ -5793,7 +5698,7 @@ function twoSided(p1, p2, q1, q2) {
5793
5698
  if (det1 > 0 && det2 < 0 || det1 < 0 && det2 > 0) return true;
5794
5699
  return false;
5795
5700
  }
5796
- //#endregion
5701
+
5797
5702
  //#region src/expression/definitions/within.ts
5798
5703
  function getTilePolygon(coordinates, bbox, canonical) {
5799
5704
  const polygon = [];
@@ -5955,7 +5860,7 @@ var Within = class Within {
5955
5860
  return true;
5956
5861
  }
5957
5862
  };
5958
- //#endregion
5863
+
5959
5864
  //#region node_modules/tinyqueue/index.js
5960
5865
  var TinyQueue = class {
5961
5866
  constructor(data = [], compare = (a, b) => a < b ? -1 : a > b ? 1 : 0) {
@@ -6008,54 +5913,10 @@ var TinyQueue = class {
6008
5913
  data[pos] = item;
6009
5914
  }
6010
5915
  };
6011
- //#endregion
6012
- //#region src/util/classify_rings.ts
6013
- /**
6014
- * Classifies an array of rings into polygons with outer rings and holes
6015
- * @param rings - the rings to classify
6016
- * @param maxRings - the maximum number of rings to include in a polygon, use 0 to include all rings
6017
- * @returns an array of polygons with internal rings as holes
6018
- */
6019
- function classifyRings$1(rings, maxRings) {
6020
- if (rings.length <= 1) return [rings];
6021
- const polygons = [];
6022
- let polygon;
6023
- let ccw;
6024
- for (const ring of rings) {
6025
- const area = calculateSignedArea(ring);
6026
- if (area === 0) continue;
6027
- ring.area = Math.abs(area);
6028
- if (ccw === void 0) ccw = area < 0;
6029
- if (ccw === area < 0) {
6030
- if (polygon) polygons.push(polygon);
6031
- polygon = [ring];
6032
- } else polygon.push(ring);
6033
- }
6034
- if (polygon) polygons.push(polygon);
6035
- return polygons;
6036
- }
6037
- /**
6038
- * Returns the signed area for the polygon ring. Positive areas are exterior rings and
6039
- * have a clockwise winding. Negative areas are interior rings and have a counter clockwise
6040
- * ordering.
6041
- *
6042
- * @param ring - Exterior or interior ring
6043
- * @returns Signed area
6044
- */
6045
- function calculateSignedArea(ring) {
6046
- let sum = 0;
6047
- for (let i = 0, len = ring.length, j = len - 1, p1, p2; i < len; j = i++) {
6048
- p1 = ring[i];
6049
- p2 = ring[j];
6050
- sum += (p2.x - p1.x) * (p1.y + p2.y);
6051
- }
6052
- return sum;
6053
- }
6054
- //#endregion
5916
+
6055
5917
  //#region src/util/cheap_ruler.ts
6056
5918
  const RE = 6378.137;
6057
- const FE = 1 / 298.257223563;
6058
- const E2 = FE * (2 - FE);
5919
+ const E2 = .0066943799901413165;
6059
5920
  const RAD = Math.PI / 180;
6060
5921
  var CheapRuler = class {
6061
5922
  constructor(lat) {
@@ -6064,7 +5925,7 @@ var CheapRuler = class {
6064
5925
  const w2 = 1 / (1 - E2 * (1 - coslat * coslat));
6065
5926
  const w = Math.sqrt(w2);
6066
5927
  this.kx = m * w * coslat;
6067
- this.ky = m * w * w2 * (1 - E2);
5928
+ this.ky = m * w * w2 * .9933056200098587;
6068
5929
  }
6069
5930
  /**
6070
5931
  * Given two points of the form [longitude, latitude], returns the distance.
@@ -6135,7 +5996,7 @@ var CheapRuler = class {
6135
5996
  return deg;
6136
5997
  }
6137
5998
  };
6138
- //#endregion
5999
+
6139
6000
  //#region src/expression/definitions/distance.ts
6140
6001
  const MinPointsSize = 100;
6141
6002
  const MinLinePointsSize = 50;
@@ -6403,9 +6264,7 @@ function pointToGeometryDistance(ctx, geometries) {
6403
6264
  case "LineString":
6404
6265
  dist = Math.min(dist, pointSetToPointSetDistance(pointPosition, false, geometry.coordinates, true, ruler, dist));
6405
6266
  break;
6406
- case "Polygon":
6407
- dist = Math.min(dist, pointsToPolygonDistance(pointPosition, false, geometry.coordinates, ruler, dist));
6408
- break;
6267
+ case "Polygon": dist = Math.min(dist, pointsToPolygonDistance(pointPosition, false, geometry.coordinates, ruler, dist));
6409
6268
  }
6410
6269
  if (dist === 0) return dist;
6411
6270
  }
@@ -6425,9 +6284,7 @@ function lineStringToGeometryDistance(ctx, geometries) {
6425
6284
  case "LineString":
6426
6285
  dist = Math.min(dist, pointSetToPointSetDistance(linePositions, true, geometry.coordinates, true, ruler, dist));
6427
6286
  break;
6428
- case "Polygon":
6429
- dist = Math.min(dist, pointsToPolygonDistance(linePositions, true, geometry.coordinates, ruler, dist));
6430
- break;
6287
+ case "Polygon": dist = Math.min(dist, pointsToPolygonDistance(linePositions, true, geometry.coordinates, ruler, dist));
6431
6288
  }
6432
6289
  if (dist === 0) return dist;
6433
6290
  }
@@ -6451,9 +6308,7 @@ function polygonToGeometryDistance(ctx, geometries) {
6451
6308
  case "LineString":
6452
6309
  dist = Math.min(dist, pointsToPolygonDistance(geometry.coordinates, true, polygon, ruler, dist));
6453
6310
  break;
6454
- case "Polygon":
6455
- dist = Math.min(dist, polygonToPolygonDistance(polygon, geometry.coordinates, ruler, dist));
6456
- break;
6311
+ case "Polygon": dist = Math.min(dist, polygonToPolygonDistance(polygon, geometry.coordinates, ruler, dist));
6457
6312
  }
6458
6313
  if (dist === 0) return dist;
6459
6314
  }
@@ -6509,7 +6364,48 @@ var Distance = class Distance {
6509
6364
  return true;
6510
6365
  }
6511
6366
  };
6512
- //#endregion
6367
+
6368
+ //#region src/expression/definitions/semiliteral.ts
6369
+ var Semiliteral = class Semiliteral {
6370
+ constructor(arr) {
6371
+ let elementType = null;
6372
+ for (const expr of arr) if (!elementType) elementType = expr.type;
6373
+ else if (elementType === expr.type) continue;
6374
+ else {
6375
+ elementType = ValueType;
6376
+ break;
6377
+ }
6378
+ this.type = array(elementType ?? ValueType, arr.length);
6379
+ this.arr = arr;
6380
+ }
6381
+ static parse(args, context) {
6382
+ if (args.length !== 2) return context.error(`'semiliteral' expression requires exactly one argument, but found ${args.length - 1} instead.`);
6383
+ if (!isValue(args[1])) return context.error(`invalid value of type "${typeof args[1]}"`);
6384
+ const value = args[1];
6385
+ const type = typeOf(value);
6386
+ if (type.kind === "array") {
6387
+ const arr = value;
6388
+ const arrayContext = context.concat(1);
6389
+ const parsed = [];
6390
+ for (let i = 0; i < arr.length; i++) {
6391
+ const item = arrayContext.parse(arr[i], i, ValueType);
6392
+ if (!item) return null;
6393
+ parsed.push(item);
6394
+ }
6395
+ return new Semiliteral(parsed);
6396
+ } else return new Literal(type, value);
6397
+ }
6398
+ evaluate(ctx) {
6399
+ return this.arr.map((arg) => arg.evaluate(ctx));
6400
+ }
6401
+ eachChild(fn) {
6402
+ this.arr.forEach(fn);
6403
+ }
6404
+ outputDefined() {
6405
+ return this.arr.every((arg) => arg.outputDefined());
6406
+ }
6407
+ };
6408
+
6513
6409
  //#region src/expression/definitions/global_state.ts
6514
6410
  var GlobalState = class GlobalState {
6515
6411
  constructor(key) {
@@ -6533,7 +6429,7 @@ var GlobalState = class GlobalState {
6533
6429
  return false;
6534
6430
  }
6535
6431
  };
6536
- //#endregion
6432
+
6537
6433
  //#region src/expression/definitions/index.ts
6538
6434
  const expressions = {
6539
6435
  "==": Equals,
@@ -6562,6 +6458,7 @@ const expressions = {
6562
6458
  number: Assertion,
6563
6459
  "number-format": NumberFormat,
6564
6460
  object: Assertion,
6461
+ semiliteral: Semiliteral,
6565
6462
  slice: Slice,
6566
6463
  step: Step,
6567
6464
  string: Assertion,
@@ -6574,7 +6471,163 @@ const expressions = {
6574
6471
  distance: Distance,
6575
6472
  "global-state": GlobalState
6576
6473
  };
6577
- //#endregion
6474
+
6475
+ //#region src/expression/parsing_error.ts
6476
+ var ExpressionParsingError = class extends Error {
6477
+ constructor(key, message) {
6478
+ super(message);
6479
+ this.message = message;
6480
+ this.key = key;
6481
+ }
6482
+ };
6483
+
6484
+ //#region src/expression/scope.ts
6485
+ /**
6486
+ * Tracks `let` bindings during expression parsing.
6487
+ * @private
6488
+ */
6489
+ var Scope = class Scope {
6490
+ constructor(parent, bindings = []) {
6491
+ this.parent = parent;
6492
+ this.bindings = {};
6493
+ for (const [name, expression] of bindings) this.bindings[name] = expression;
6494
+ }
6495
+ concat(bindings) {
6496
+ return new Scope(this, bindings);
6497
+ }
6498
+ get(name) {
6499
+ if (this.bindings[name]) return this.bindings[name];
6500
+ if (this.parent) return this.parent.get(name);
6501
+ throw new Error(`${name} not found in scope.`);
6502
+ }
6503
+ has(name) {
6504
+ if (this.bindings[name]) return true;
6505
+ return this.parent ? this.parent.has(name) : false;
6506
+ }
6507
+ };
6508
+
6509
+ //#region src/expression/parsing_context.ts
6510
+ /**
6511
+ * State associated parsing at a given point in an expression tree.
6512
+ * @private
6513
+ */
6514
+ var ParsingContext = class ParsingContext {
6515
+ constructor(registry, isConstantFunc, path = [], expectedType, scope = new Scope(), errors = []) {
6516
+ this.registry = registry;
6517
+ this.path = path;
6518
+ this.key = path.map((part) => `[${part}]`).join("");
6519
+ this.scope = scope;
6520
+ this.errors = errors;
6521
+ this.expectedType = expectedType;
6522
+ this._isConstant = isConstantFunc;
6523
+ }
6524
+ /**
6525
+ * @param expr the JSON expression to parse
6526
+ * @param index the optional argument index if this expression is an argument of a parent expression that's being parsed
6527
+ * @param options
6528
+ * @param options.omitTypeAnnotations set true to omit inferred type annotations. Caller beware: with this option set, the parsed expression's type will NOT satisfy `expectedType` if it would normally be wrapped in an inferred annotation.
6529
+ * @private
6530
+ */
6531
+ parse(expr, index, expectedType, bindings, options = {}) {
6532
+ if (index != null) return this.concat(index, expectedType, bindings)._parse(expr, options);
6533
+ return this._parse(expr, options);
6534
+ }
6535
+ _parse(expr, options) {
6536
+ if (expr === null || typeof expr === "string" || typeof expr === "boolean" || typeof expr === "number") expr = ["literal", expr];
6537
+ const key = this.key;
6538
+ function annotate(parsed, type, typeAnnotation) {
6539
+ if (typeAnnotation === "assert") return new Assertion(type, [parsed], key);
6540
+ else if (typeAnnotation === "coerce") return new Coercion(type, [parsed], key);
6541
+ else return parsed;
6542
+ }
6543
+ if (Array.isArray(expr)) {
6544
+ if (expr.length === 0) return this.error("Expected an array with at least one element. If you wanted a literal array, use [\"literal\", []].");
6545
+ const op = expr[0];
6546
+ if (typeof op !== "string") {
6547
+ this.error(`Expression name must be a string, but found ${typeof op} instead. If you wanted a literal array, use ["literal", [...]].`, 0);
6548
+ return null;
6549
+ }
6550
+ const Expr = this.registry[op];
6551
+ if (Expr) {
6552
+ let parsed = Expr.parse(expr, this);
6553
+ if (!parsed) return null;
6554
+ if (this.expectedType) {
6555
+ const expected = this.expectedType;
6556
+ const actual = parsed.type;
6557
+ if ((expected.kind === "string" || expected.kind === "number" || expected.kind === "boolean" || expected.kind === "object" || expected.kind === "array") && actual.kind === "value") parsed = annotate(parsed, expected, options.typeAnnotation || "assert");
6558
+ else if ("projectionDefinition" === expected.kind && [
6559
+ "string",
6560
+ "array",
6561
+ "value"
6562
+ ].includes(actual.kind) || [
6563
+ "color",
6564
+ "formatted",
6565
+ "resolvedImage"
6566
+ ].includes(expected.kind) && ["value", "string"].includes(actual.kind) || ["padding", "numberArray"].includes(expected.kind) && [
6567
+ "value",
6568
+ "number",
6569
+ "array"
6570
+ ].includes(actual.kind) || "colorArray" === expected.kind && [
6571
+ "value",
6572
+ "string",
6573
+ "array"
6574
+ ].includes(actual.kind) || "variableAnchorOffsetCollection" === expected.kind && ["value", "array"].includes(actual.kind)) parsed = annotate(parsed, expected, options.typeAnnotation || "coerce");
6575
+ else if (this.checkSubtype(expected, actual)) return null;
6576
+ }
6577
+ if (!(parsed instanceof Literal) && parsed.type.kind !== "resolvedImage" && this._isConstant(parsed)) {
6578
+ const ec = new EvaluationContext();
6579
+ try {
6580
+ parsed = new Literal(parsed.type, parsed.evaluate(ec));
6581
+ } catch (e) {
6582
+ this.error(e.message);
6583
+ return null;
6584
+ }
6585
+ }
6586
+ return parsed;
6587
+ }
6588
+ return this.error(`Unknown expression "${op}". If you wanted a literal array, use ["literal", [...]].`, 0);
6589
+ } else if (typeof expr === "undefined") return this.error("'undefined' value invalid. Use null instead.");
6590
+ else if (typeof expr === "object") return this.error("Bare objects invalid. Use [\"literal\", {...}] instead.");
6591
+ else return this.error(`Expected an array, but found ${typeof expr} instead.`);
6592
+ }
6593
+ /**
6594
+ * Returns a copy of this context suitable for parsing the subexpression at
6595
+ * index `index`, optionally appending to 'let' binding map.
6596
+ *
6597
+ * Note that `errors` property, intended for collecting errors while
6598
+ * parsing, is copied by reference rather than cloned.
6599
+ * @private
6600
+ */
6601
+ concat(index, expectedType, bindings) {
6602
+ const path = typeof index === "number" ? this.path.concat(index) : this.path;
6603
+ const scope = bindings ? this.scope.concat(bindings) : this.scope;
6604
+ return new ParsingContext(this.registry, this._isConstant, path, expectedType || null, scope, this.errors);
6605
+ }
6606
+ /**
6607
+ * Push a parsing (or type checking) error into the `this.errors`
6608
+ * @param error The message
6609
+ * @param keys Optionally specify the source of the error at a child
6610
+ * of the current expression at `this.key`.
6611
+ * @private
6612
+ */
6613
+ error(error, ...keys) {
6614
+ const key = `${this.key}${keys.map((k) => `[${k}]`).join("")}`;
6615
+ this.errors.push(new ExpressionParsingError(key, error));
6616
+ }
6617
+ /**
6618
+ * Returns null if `t` is a subtype of `expected`; otherwise returns an
6619
+ * error message and also pushes it to `this.errors`.
6620
+ * @param expected The expected type
6621
+ * @param t The actual type
6622
+ * @returns null if `t` is a subtype of `expected`; otherwise returns an error message
6623
+ */
6624
+ checkSubtype(expected, t) {
6625
+ const error = checkSubtype(expected, t);
6626
+ if (error) this.error(error);
6627
+ return error;
6628
+ }
6629
+ };
6630
+
6578
6631
  //#region src/expression/compound_expression.ts
6579
6632
  var CompoundExpression = class CompoundExpression {
6580
6633
  constructor(name, type, evaluate, args, key) {
@@ -7142,28 +7195,14 @@ function isStateConstant(e) {
7142
7195
  return result;
7143
7196
  }
7144
7197
  function isGlobalPropertyConstant(e, properties) {
7145
- if (e instanceof CompoundExpression && properties.indexOf(e.name) >= 0) return false;
7146
- let result = true;
7147
- e.eachChild((arg) => {
7148
- if (result && !isGlobalPropertyConstant(arg, properties)) result = false;
7149
- });
7150
- return result;
7151
- }
7152
- //#endregion
7153
- //#region src/util/result.ts
7154
- function success(value) {
7155
- return {
7156
- result: "success",
7157
- value
7158
- };
7159
- }
7160
- function error(value) {
7161
- return {
7162
- result: "error",
7163
- value
7164
- };
7198
+ if (e instanceof CompoundExpression && properties.indexOf(e.name) >= 0) return false;
7199
+ let result = true;
7200
+ e.eachChild((arg) => {
7201
+ if (result && !isGlobalPropertyConstant(arg, properties)) result = false;
7202
+ });
7203
+ return result;
7165
7204
  }
7166
- //#endregion
7205
+
7167
7206
  //#region src/util/properties.ts
7168
7207
  function supportsPropertyExpression(spec) {
7169
7208
  return spec["property-type"] === "data-driven" || spec["property-type"] === "cross-faded-data-driven";
@@ -7174,13 +7213,13 @@ function supportsZoomExpression(spec) {
7174
7213
  function supportsInterpolation(spec) {
7175
7214
  return !!spec.expression && spec.expression.interpolated;
7176
7215
  }
7177
- //#endregion
7216
+
7178
7217
  //#region src/util/extend.ts
7179
7218
  function extendBy(output, ...inputs) {
7180
7219
  for (const input of inputs) for (const k in input) output[k] = input[k];
7181
7220
  return output;
7182
7221
  }
7183
- //#endregion
7222
+
7184
7223
  //#region src/util/get_type.ts
7185
7224
  function getType(val) {
7186
7225
  if (val instanceof Number) return "number";
@@ -7190,7 +7229,7 @@ function getType(val) {
7190
7229
  else if (val === null) return "null";
7191
7230
  else return typeof val;
7192
7231
  }
7193
- //#endregion
7232
+
7194
7233
  //#region src/function/index.ts
7195
7234
  function isFunction(value) {
7196
7235
  return typeof value === "object" && value !== null && !Array.isArray(value) && typeOf(value) === ObjectType;
@@ -7288,21 +7327,21 @@ function createFunction(parameters, propertySpec) {
7288
7327
  kind: "source",
7289
7328
  evaluate(_, feature) {
7290
7329
  const value = feature && feature.properties ? feature.properties[parameters.property] : void 0;
7291
- if (value === void 0) return coalesce$1(parameters.default, propertySpec.default);
7330
+ if (value === void 0) return coalesce(parameters.default, propertySpec.default);
7292
7331
  return innerFun(parameters, propertySpec, value, hashedStops, categoricalKeyType);
7293
7332
  }
7294
7333
  };
7295
7334
  }
7296
- function coalesce$1(a, b, c) {
7335
+ function coalesce(a, b, c) {
7297
7336
  if (a !== void 0) return a;
7298
7337
  if (b !== void 0) return b;
7299
7338
  if (c !== void 0) return c;
7300
7339
  }
7301
7340
  function evaluateCategoricalFunction(parameters, propertySpec, input, hashedStops, keyType) {
7302
- return coalesce$1(typeof input === keyType ? hashedStops[input] : void 0, parameters.default, propertySpec.default);
7341
+ return coalesce(typeof input === keyType ? hashedStops[input] : void 0, parameters.default, propertySpec.default);
7303
7342
  }
7304
7343
  function evaluateIntervalFunction(parameters, propertySpec, input) {
7305
- if (getType(input) !== "number") return coalesce$1(parameters.default, propertySpec.default);
7344
+ if (getType(input) !== "number") return coalesce(parameters.default, propertySpec.default);
7306
7345
  const n = parameters.stops.length;
7307
7346
  if (n === 1) return parameters.stops[0][1];
7308
7347
  if (input <= parameters.stops[0][0]) return parameters.stops[0][1];
@@ -7312,13 +7351,13 @@ function evaluateIntervalFunction(parameters, propertySpec, input) {
7312
7351
  }
7313
7352
  function evaluateExponentialFunction(parameters, propertySpec, input) {
7314
7353
  const base = parameters.base !== void 0 ? parameters.base : 1;
7315
- if (getType(input) !== "number") return coalesce$1(parameters.default, propertySpec.default);
7354
+ if (getType(input) !== "number") return coalesce(parameters.default, propertySpec.default);
7316
7355
  const n = parameters.stops.length;
7317
7356
  if (n === 1) return parameters.stops[0][1];
7318
7357
  if (input <= parameters.stops[0][0]) return parameters.stops[0][1];
7319
7358
  if (input >= parameters.stops[n - 1][0]) return parameters.stops[n - 1][1];
7320
7359
  const index = findStopLessThanOrEqualTo(parameters.stops.map((stop) => stop[0]), input, "");
7321
- const t = interpolationFactor(input, base, parameters.stops[index][0], parameters.stops[index + 1][0]);
7360
+ const t = interpolationFactor$1(input, base, parameters.stops[index][0], parameters.stops[index + 1][0]);
7322
7361
  const outputLower = parameters.stops[index][1];
7323
7362
  const outputUpper = parameters.stops[index + 1][1];
7324
7363
  const interp = interpolateFactory[propertySpec.type] || identityFunction;
@@ -7352,7 +7391,7 @@ function evaluateIdentityFunction(parameters, propertySpec, input) {
7352
7391
  break;
7353
7392
  default: if (getType(input) !== propertySpec.type && (propertySpec.type !== "enum" || !propertySpec.values[input])) input = void 0;
7354
7393
  }
7355
- return coalesce$1(input, parameters.default, propertySpec.default);
7394
+ return coalesce(input, parameters.default, propertySpec.default);
7356
7395
  }
7357
7396
  /**
7358
7397
  * Returns a ratio that can be used to interpolate between exponential function
@@ -7392,14 +7431,28 @@ function evaluateIdentityFunction(parameters, propertySpec, input) {
7392
7431
  *
7393
7432
  * @private
7394
7433
  */
7395
- function interpolationFactor(input, base, lowerValue, upperValue) {
7434
+ function interpolationFactor$1(input, base, lowerValue, upperValue) {
7396
7435
  const difference = upperValue - lowerValue;
7397
7436
  const progress = input - lowerValue;
7398
7437
  if (difference === 0) return 0;
7399
7438
  else if (base === 1) return progress / difference;
7400
7439
  else return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1);
7401
7440
  }
7402
- //#endregion
7441
+
7442
+ //#region src/util/result.ts
7443
+ function success(value) {
7444
+ return {
7445
+ result: "success",
7446
+ value
7447
+ };
7448
+ }
7449
+ function error(value) {
7450
+ return {
7451
+ result: "error",
7452
+ value
7453
+ };
7454
+ }
7455
+
7403
7456
  //#region src/expression/index.ts
7404
7457
  var StyleExpression = class {
7405
7458
  constructor(expression, rootKey, propertySpec, globalState) {
@@ -7681,7 +7734,7 @@ function addGlobalState(globals, globalState) {
7681
7734
  globalState
7682
7735
  };
7683
7736
  }
7684
- //#endregion
7737
+
7685
7738
  //#region src/feature_filter/index.ts
7686
7739
  function classifyChildren(children) {
7687
7740
  let sawLegacy = false;
@@ -7787,12 +7840,10 @@ function findMixedLegacyFilter(filter, path = []) {
7787
7840
  if (diagnostic) return diagnostic;
7788
7841
  break;
7789
7842
  }
7790
- case "case":
7791
- for (let i = 1; i < filter.length - 1; i += 2) {
7792
- const diagnostic = checkChild(i, path, filter);
7793
- if (diagnostic) return diagnostic;
7794
- }
7795
- break;
7843
+ case "case": for (let i = 1; i < filter.length - 1; i += 2) {
7844
+ const diagnostic = checkChild(i, path, filter);
7845
+ if (diagnostic) return diagnostic;
7846
+ }
7796
7847
  }
7797
7848
  return null;
7798
7849
  }
@@ -7830,7 +7881,7 @@ function featureFilter(filter, rootKey, globalState) {
7830
7881
  needGeometry: false,
7831
7882
  getGlobalStateRefs: () => /* @__PURE__ */ new Set()
7832
7883
  };
7833
- if (!isExpressionFilter(filter)) filter = convertFilter$1(filter);
7884
+ if (!isExpressionFilter(filter)) filter = convertFilter(filter);
7834
7885
  else warnAboutMixedLegacyFilter(filter, rootKey);
7835
7886
  const compiled = createExpression(filter, rootKey, filterSpec, globalState);
7836
7887
  if (compiled.result === "error") throw new Error(compiled.value.map((err) => `${err.key}: ${err.message}`).join(", "));
@@ -7849,13 +7900,13 @@ function geometryNeeded(filter) {
7849
7900
  for (let index = 1; index < filter.length; index++) if (geometryNeeded(filter[index])) return true;
7850
7901
  return false;
7851
7902
  }
7852
- function convertFilter$1(filter) {
7903
+ function convertFilter(filter) {
7853
7904
  if (!filter) return true;
7854
7905
  const op = filter[0];
7855
7906
  if (filter.length <= 1) return op !== "any";
7856
- return op === "==" ? convertComparisonOp$1(filter[1], filter[2], "==") : op === "!=" ? convertNegation(convertComparisonOp$1(filter[1], filter[2], "==")) : op === "<" || op === ">" || op === "<=" || op === ">=" ? convertComparisonOp$1(filter[1], filter[2], op) : op === "any" ? convertDisjunctionOp(filter.slice(1)) : op === "all" ? ["all"].concat(filter.slice(1).map(convertFilter$1)) : op === "none" ? ["all"].concat(filter.slice(1).map(convertFilter$1).map(convertNegation)) : op === "in" ? convertInOp$1(filter[1], filter.slice(2)) : op === "!in" ? convertNegation(convertInOp$1(filter[1], filter.slice(2))) : op === "has" ? convertHasOp$1(filter[1]) : op === "!has" ? convertNegation(convertHasOp$1(filter[1])) : true;
7907
+ return op === "==" ? convertComparisonOp(filter[1], filter[2], "==") : op === "!=" ? convertNegation(convertComparisonOp(filter[1], filter[2], "==")) : op === "<" || op === ">" || op === "<=" || op === ">=" ? convertComparisonOp(filter[1], filter[2], op) : op === "any" ? convertDisjunctionOp(filter.slice(1)) : op === "all" ? ["all"].concat(filter.slice(1).map(convertFilter)) : op === "none" ? ["all"].concat(filter.slice(1).map(convertFilter).map(convertNegation)) : op === "in" ? convertInOp(filter[1], filter.slice(2)) : op === "!in" ? convertNegation(convertInOp(filter[1], filter.slice(2))) : op === "has" ? convertHasOp(filter[1]) : op === "!has" ? convertNegation(convertHasOp(filter[1])) : true;
7857
7908
  }
7858
- function convertComparisonOp$1(property, value, op) {
7909
+ function convertComparisonOp(property, value, op) {
7859
7910
  switch (property) {
7860
7911
  case "$type": return [`filter-type-${op}`, value];
7861
7912
  case "$id": return [`filter-id-${op}`, value];
@@ -7867,9 +7918,9 @@ function convertComparisonOp$1(property, value, op) {
7867
7918
  }
7868
7919
  }
7869
7920
  function convertDisjunctionOp(filters) {
7870
- return ["any"].concat(filters.map(convertFilter$1));
7921
+ return ["any"].concat(filters.map(convertFilter));
7871
7922
  }
7872
- function convertInOp$1(property, values) {
7923
+ function convertInOp(property, values) {
7873
7924
  if (values.length === 0) return false;
7874
7925
  switch (property) {
7875
7926
  case "$type": return ["filter-type-in", ["literal", values]];
@@ -7886,7 +7937,7 @@ function convertInOp$1(property, values) {
7886
7937
  ];
7887
7938
  }
7888
7939
  }
7889
- function convertHasOp$1(property) {
7940
+ function convertHasOp(property) {
7890
7941
  switch (property) {
7891
7942
  case "$type": return true;
7892
7943
  case "$id": return ["filter-has-id"];
@@ -8202,21 +8253,29 @@ class SVGRenderer {
8202
8253
  width;
8203
8254
  height;
8204
8255
  #svg;
8205
- #backgroundColor;
8206
8256
  #spriteSheetDefs = new Map();
8207
8257
  #spriteSymbolDefs = new Map();
8208
8258
  #sdfFilterDefs = new Map();
8209
8259
  #blurFilterDefs = new Map();
8260
+ #rasterDefs = [];
8261
+ #rasterClipCount = 0;
8262
+ #clipCircle;
8210
8263
  constructor(opt) {
8211
8264
  this.width = opt.width;
8212
8265
  this.height = opt.height;
8213
8266
  this.#svg = [];
8214
- this.#backgroundColor = Color.transparent;
8267
+ }
8268
+ setClipCircle(circle) {
8269
+ this.#clipCircle = circle;
8215
8270
  }
8216
8271
  drawBackgroundFill(style) {
8272
+ // Every background layer paints over what is below it, like any other layer. (A
8273
+ // transparent one, e.g. an empty "slot" layer, must not erase an earlier background.)
8217
8274
  const color = new Color(style.color);
8218
8275
  color.alpha *= style.opacity;
8219
- this.#backgroundColor = color;
8276
+ if (color.alpha <= 0)
8277
+ return;
8278
+ this.#svg.push(`<rect x="-1" y="-1" width="${(this.width + 2).toFixed(0)}" height="${(this.height + 2).toFixed(0)}" ${fillAttr(color)} />`);
8220
8279
  }
8221
8280
  drawPolygons(id, features) {
8222
8281
  if (features.length === 0)
@@ -8224,9 +8283,11 @@ class SVGRenderer {
8224
8283
  // Merge only *consecutive* features with identical attributes, so the paint
8225
8284
  // order of overlapping features matches MapLibre's source order. A global
8226
8285
  // merge would reorder interleaved features (e.g. data-driven fill colors).
8227
- // Fills and their antialias outlines are collected in one pass but emitted
8228
- // as two: MapLibre draws every fill first, then every outline, so a lower
8229
- // feature's outline composites on top of a later overlapping fill.
8286
+ // Translucent features are never merged: MapLibre blends each one separately,
8287
+ // so where they overlap, their opacity adds up — within one merged <path> it
8288
+ // would not. Fills and their antialias outlines are collected in one pass but
8289
+ // emitted as two: MapLibre draws every fill first, then every outline, so a
8290
+ // lower feature's outline composites on top of a later overlapping fill.
8230
8291
  const fillGroups = [];
8231
8292
  const outlineGroups = [];
8232
8293
  let currentFillKey;
@@ -8241,37 +8302,48 @@ class SVGRenderer {
8241
8302
  // Round each ring once; shared by the fill and the outline pass.
8242
8303
  const rings = feature.geometry.map((ring) => ring.map((p) => roundXY(p.x, p.y)));
8243
8304
  const color = new Color(style.color);
8305
+ const translucent = style.opacity < 1 || color.alpha < 255;
8244
8306
  if (color.alpha > 0) {
8245
8307
  const key = color.hex + translate + opacityAttr;
8246
- if (key !== currentFillKey) {
8308
+ if (translucent || key !== currentFillKey) {
8247
8309
  fillGroups.push({ segments: [], attrs: `${fillAttr(color)}${translate}${opacityAttr}` });
8248
- currentFillKey = key;
8310
+ currentFillKey = translucent ? undefined : key;
8249
8311
  }
8250
8312
  const group = fillGroups[fillGroups.length - 1];
8251
8313
  for (const ring of rings)
8252
8314
  group.segments.push(ring);
8253
8315
  }
8254
- // fill-antialias outline in an explicit fill-outline-color. MapLibre also
8255
- // draws a *default* outline in the fill color, but that outline IS its fill-
8256
- // edge antialiasing (draw_fill.ts clips it to just outside the shape) — and
8257
- // an SVG <path fill> is already rasterizer-antialiased, so redrawing it is
8258
- // redundant: measured identical pixel diff for +46–88% SVG size. We only
8259
- // draw the outline when a distinct color is set — the visible-border case
8260
- // (choropleths), which the browser's fill AA does not cover.
8261
- if (style.antialias && style.outlineColor !== undefined) {
8262
- const outlineColor = new Color(style.outlineColor);
8316
+ // fill-antialias outline, in fill-outline-color or else the fill color. For an
8317
+ // opaque fill, MapLibre's default outline IS its fill-edge antialiasing
8318
+ // (draw_fill.ts keeps it outside of the shape), and an SVG <path fill> is already
8319
+ // rasterizer-antialiased, so redrawing it is redundant: measured identical pixel
8320
+ // diff for +46–88% SVG size. It is drawn when it shows: in a distinct
8321
+ // fill-outline-color (choropleths), or for a translucent fill, where MapLibre
8322
+ // draws the outline over the fill's edge (e.g. buildings fading in).
8323
+ if (style.antialias && (style.outlineColor !== undefined || translucent)) {
8324
+ const outlineColor = style.outlineColor !== undefined ? new Color(style.outlineColor) : color;
8263
8325
  if (outlineColor.alpha > 0) {
8326
+ const outlineTranslucent = translucent || outlineColor.alpha < 255;
8264
8327
  const key = outlineColor.hex + translate + opacityAttr;
8265
- if (key !== currentOutlineKey) {
8328
+ if (outlineTranslucent || key !== currentOutlineKey) {
8266
8329
  outlineGroups.push({
8267
- segments: [],
8330
+ closed: [],
8331
+ open: [],
8268
8332
  attrs: `fill="none" ${strokeAttr(outlineColor, formatScaled(FILL_OUTLINE_WIDTH_PX))}${translate}${opacityAttr}`,
8269
8333
  });
8270
- currentOutlineKey = key;
8334
+ currentOutlineKey = outlineTranslucent ? undefined : key;
8271
8335
  }
8272
8336
  const group = outlineGroups[outlineGroups.length - 1];
8273
- for (const ring of rings)
8274
- group.segments.push(ring);
8337
+ // A polygon clipped to its tile has an outline without the clipped edges.
8338
+ if (feature.outline) {
8339
+ for (const line of feature.outline) {
8340
+ group.open.push(line.map((p) => roundXY(p.x, p.y)));
8341
+ }
8342
+ }
8343
+ else {
8344
+ for (const ring of rings)
8345
+ group.closed.push(ring);
8346
+ }
8275
8347
  }
8276
8348
  }
8277
8349
  });
@@ -8279,8 +8351,9 @@ class SVGRenderer {
8279
8351
  for (const { segments, attrs } of fillGroups) {
8280
8352
  this.#svg.push(`<path d="${segmentsToPath(segments, true)}" ${attrs} />`);
8281
8353
  }
8282
- for (const { segments, attrs } of outlineGroups) {
8283
- this.#svg.push(`<path d="${segmentsToPath(segments, true)}" ${attrs} />`);
8354
+ for (const { closed, open, attrs } of outlineGroups) {
8355
+ const d = segmentsToPath(closed, true) + segmentsToPath(open, false);
8356
+ this.#svg.push(`<path d="${d}" ${attrs} />`);
8284
8357
  }
8285
8358
  this.#svg.push('</g>');
8286
8359
  }
@@ -8288,7 +8361,12 @@ class SVGRenderer {
8288
8361
  if (features.length === 0)
8289
8362
  return;
8290
8363
  // Merge only *consecutive* same-attribute features (see drawPolygons) so the
8291
- // paint order of overlapping lines matches MapLibre's source order.
8364
+ // paint order of overlapping lines matches MapLibre's source order. Translucent
8365
+ // lines are never merged: MapLibre blends every line separately, so where lines
8366
+ // overlap (e.g. parallel rail tracks fading in), their opacity adds up — even
8367
+ // between the parts of one feature (tiles merge equal ways into one multi-line
8368
+ // feature). One <path> covers its overlaps only once, so a translucent line gets
8369
+ // a <path> per part.
8292
8370
  const groups = [];
8293
8371
  let currentKey;
8294
8372
  features.forEach(([feature, style]) => {
@@ -8323,7 +8401,8 @@ class SVGRenderer {
8323
8401
  translate,
8324
8402
  filterAttr,
8325
8403
  ].join('\0');
8326
- if (key !== currentKey) {
8404
+ const translucent = effectiveOpacity < 1 || color.alpha < 255;
8405
+ if (translucent || key !== currentKey) {
8327
8406
  const attrs = [
8328
8407
  'fill="none"',
8329
8408
  strokeAttr(color, roundedWidth),
@@ -8336,8 +8415,9 @@ class SVGRenderer {
8336
8415
  groups.push({
8337
8416
  segments: [],
8338
8417
  attrs: attrs.join(' ') + translate + opacityAttr + filterAttr,
8418
+ separate: translucent,
8339
8419
  });
8340
- currentKey = key;
8420
+ currentKey = translucent ? undefined : key;
8341
8421
  }
8342
8422
  const group = groups[groups.length - 1];
8343
8423
  feature.geometry.forEach((line) => {
@@ -8346,7 +8426,13 @@ class SVGRenderer {
8346
8426
  });
8347
8427
  });
8348
8428
  this.#svg.push(`<g id="${escapeXml(id)}">`);
8349
- for (const { segments, attrs } of groups) {
8429
+ for (const { segments, attrs, separate } of groups) {
8430
+ if (separate) {
8431
+ for (const segment of segments) {
8432
+ this.#svg.push(`<path d="${segmentsToPath([segment])}" ${attrs} />`);
8433
+ }
8434
+ continue;
8435
+ }
8350
8436
  const chains = chainSegments(segments);
8351
8437
  const d = segmentsToPath(chains);
8352
8438
  this.#svg.push(`<path d="${d}" ${attrs} />`);
@@ -8603,7 +8689,10 @@ class SVGRenderer {
8603
8689
  gAttrs += ` filter="${filters.join(' ')}"`;
8604
8690
  this.#svg.push(`<g ${gAttrs}>`);
8605
8691
  const pixelated = style.resampling === 'nearest';
8692
+ this.#drawRasterMeshes(tiles, pixelated);
8606
8693
  for (const tile of tiles) {
8694
+ if (tile.triangles)
8695
+ continue;
8607
8696
  const overlap = Math.min(tile.width, tile.height) / 10000; // slight overlap to prevent sub-pixel gaps between tiles
8608
8697
  let attrs = `x="${formatScaled(tile.x - overlap)}" y="${formatScaled(tile.y - overlap)}" width="${formatScaled(tile.width + overlap * 2)}" height="${formatScaled(tile.height + overlap * 2)}" xlink:href="${tile.dataUri}"`;
8609
8698
  if (pixelated)
@@ -8612,11 +8701,65 @@ class SVGRenderer {
8612
8701
  }
8613
8702
  this.#svg.push('</g>');
8614
8703
  }
8704
+ /**
8705
+ * Draws raster tiles given as meshes of triangles (globe projection). Every tile image is
8706
+ * defined once (as a square of RASTER_TILE_UNITS); every triangle shows it through the affine
8707
+ * transform that maps its three source corners exactly onto its three screen corners, clipped
8708
+ * to the triangle on screen.
8709
+ *
8710
+ * Seams: within a tile, each clip triangle is grown by RASTER_TRIANGLE_OVERLAP_PX; the same
8711
+ * transform continues there, so the overlap shows (almost) the same pixels as the neighbour.
8712
+ * At tile borders that is not enough, as both images end on the same line and their anti-
8713
+ * aliased edges let a hairline of the background shine through. So first, as an underlay,
8714
+ * the triangles along the tile borders are drawn with their image reaching beyond the border
8715
+ * (see {@link bleedAtTileBorder}); then all triangles are drawn exactly on top of it. The
8716
+ * slightly shifted underlay only shows through the seam.
8717
+ */
8718
+ #drawRasterMeshes(tiles, pixelated) {
8719
+ const meshes = [];
8720
+ for (const tile of tiles) {
8721
+ if (!tile.triangles)
8722
+ continue;
8723
+ const imageId = `raster-${String(this.#rasterDefs.length)}`;
8724
+ const size = String(RASTER_TILE_UNITS);
8725
+ let imageAttrs = `id="${imageId}" width="${size}" height="${size}" preserveAspectRatio="none" xlink:href="${tile.dataUri}"`;
8726
+ if (pixelated)
8727
+ imageAttrs += ' style="image-rendering:pixelated"';
8728
+ this.#rasterDefs.push(`<image ${imageAttrs} />`);
8729
+ meshes.push({ imageId, triangles: tile.triangles });
8730
+ }
8731
+ for (const { imageId, triangles } of meshes) {
8732
+ for (const { source, target } of triangles) {
8733
+ if (!isOnTileBorder(source))
8734
+ continue;
8735
+ this.#drawRasterTriangle(imageId, bleedAtTileBorder(source, target), target);
8736
+ }
8737
+ }
8738
+ for (const { imageId, triangles } of meshes) {
8739
+ for (const { source, target } of triangles) {
8740
+ this.#drawRasterTriangle(imageId, source, target);
8741
+ }
8742
+ }
8743
+ }
8744
+ #drawRasterTriangle(imageId, source, target) {
8745
+ const matrix = affineFromTriangles(source, target);
8746
+ if (!matrix)
8747
+ return;
8748
+ const clip = growTriangle(target, RASTER_TRIANGLE_OVERLAP_PX);
8749
+ const clipId = `raster-clip-${String(this.#rasterClipCount++)}`;
8750
+ const d = segmentsToPath([clip.map(([x, y]) => roundXY(x, y))], true);
8751
+ this.#svg.push(`<clipPath id="${clipId}"><path d="${d}"/></clipPath>` +
8752
+ `<g clip-path="url(#${clipId})"><use xlink:href="#${imageId}" transform="matrix(${matrix.map(formatUnit).join(',')})" /></g>`);
8753
+ }
8615
8754
  getString() {
8616
8755
  const w = this.width.toFixed(0);
8617
8756
  const h = this.height.toFixed(0);
8618
8757
  // Build defs content
8619
- const defsContent = [`<clipPath id="vb"><rect width="${w}" height="${h}"/></clipPath>`];
8758
+ const clip = this.#clipCircle;
8759
+ const clipShape = clip
8760
+ ? `<circle cx="${formatUnit(clip.x)}" cy="${formatUnit(clip.y)}" r="${formatUnit(clip.radius)}"/>`
8761
+ : `<rect width="${w}" height="${h}"/>`;
8762
+ const defsContent = [`<clipPath id="vb">${clipShape}</clipPath>`, ...this.#rasterDefs];
8620
8763
  for (const sheet of this.#spriteSheetDefs.values()) {
8621
8764
  defsContent.push(`<image id="${escapeXml(sheet.defId)}" width="${formatNum(sheet.width)}" height="${formatNum(sheet.height)}" xlink:href="${escapeXml(sheet.href)}" />`);
8622
8765
  }
@@ -8643,9 +8786,6 @@ class SVGRenderer {
8643
8786
  `<defs>\n ${defsContent.join('\n ')}\n</defs>`,
8644
8787
  `<g id="map" clip-path="url(#vb)">`,
8645
8788
  ];
8646
- if (this.#backgroundColor.alpha > 0) {
8647
- parts.push(`<rect x="-1" y="-1" width="${(this.width + 2).toFixed(0)}" height="${(this.height + 2).toFixed(0)}" ${fillAttr(this.#backgroundColor)} />`);
8648
- }
8649
8789
  parts.push(...this.#svg, '</g>', '</svg>');
8650
8790
  return parts.join('\n');
8651
8791
  }
@@ -8665,6 +8805,85 @@ function strokeAttr(color, width) {
8665
8805
  function formatScaled(v) {
8666
8806
  return formatNum(Math.round(v * 10));
8667
8807
  }
8808
+ /** Size of a raster tile image in user units, when drawn as a mesh of triangles on the globe. */
8809
+ const RASTER_TILE_UNITS = 256;
8810
+ /** How far (pixels) a raster tile image reaches beyond the tile border in the seam underlay. */
8811
+ const RASTER_TILE_BLEED_PX = 1;
8812
+ /** How far (pixels) each raster triangle is grown beyond its edges to overlap its neighbours. */
8813
+ const RASTER_TRIANGLE_OVERLAP_PX = 0.5;
8814
+ /**
8815
+ * The affine transform [a, b, c, d, e, f] mapping the source triangle (in tile units 0..1,
8816
+ * scaled to RASTER_TILE_UNITS) exactly onto the target triangle, or undefined if either is
8817
+ * degenerate.
8818
+ */
8819
+ function affineFromTriangles(source, target) {
8820
+ const k = RASTER_TILE_UNITS;
8821
+ const [[u0, v0], [u1, v1], [u2, v2]] = source.map(([u, v]) => [u * k, v * k]);
8822
+ const [[x0, y0], [x1, y1], [x2, y2]] = target;
8823
+ const du1 = u1 - u0;
8824
+ const dv1 = v1 - v0;
8825
+ const du2 = u2 - u0;
8826
+ const dv2 = v2 - v0;
8827
+ const det = du1 * dv2 - du2 * dv1;
8828
+ if (Math.abs(det) < 1e-12)
8829
+ return undefined;
8830
+ const dx1 = x1 - x0;
8831
+ const dx2 = x2 - x0;
8832
+ const dy1 = y1 - y0;
8833
+ const dy2 = y2 - y0;
8834
+ // Screen area below a hundredth of a pixel: nothing to draw.
8835
+ if (Math.abs(dx1 * dy2 - dx2 * dy1) < 0.02)
8836
+ return undefined;
8837
+ const a = (dx1 * dv2 - dx2 * dv1) / det;
8838
+ const c = (du1 * dx2 - du2 * dx1) / det;
8839
+ const b = (dy1 * dv2 - dy2 * dv1) / det;
8840
+ const d = (du1 * dy2 - du2 * dy1) / det;
8841
+ return [a, b, c, d, x0 - a * u0 - c * v0, y0 - b * u0 - d * v0];
8842
+ }
8843
+ function isOnTileBorder(source) {
8844
+ return source.some(([u, v]) => u === 0 || u === 1 || v === 0 || v === 1);
8845
+ }
8846
+ /**
8847
+ * Moves the source corners of a triangle that lie on the tile border inwards, by the
8848
+ * equivalent of RASTER_TILE_BLEED_PX on screen, so that the image edge lies beyond the border.
8849
+ * (Used for the underlay that closes the seams between tiles, see `#drawRasterMeshes`.)
8850
+ */
8851
+ function bleedAtTileBorder(source, target) {
8852
+ const [[u0, v0], [u1, v1], [u2, v2]] = source;
8853
+ const [[x0, y0], [x1, y1], [x2, y2]] = target;
8854
+ const sourceArea = Math.abs((u1 - u0) * (v2 - v0) - (u2 - u0) * (v1 - v0));
8855
+ const targetArea = Math.abs((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0));
8856
+ if (sourceArea === 0 || targetArea === 0)
8857
+ return source;
8858
+ // Pixels per tile unit, and the bleed in tile units, kept within the triangle.
8859
+ const scale = Math.sqrt(targetArea / sourceArea);
8860
+ const size = Math.max(Math.abs(u1 - u0), Math.abs(u2 - u0), Math.abs(v1 - v0), Math.abs(v2 - v0));
8861
+ const bleed = Math.min(RASTER_TILE_BLEED_PX / scale, size / 4);
8862
+ const inset = (t) => (t === 0 ? bleed : t === 1 ? 1 - bleed : t);
8863
+ return source.map(([u, v]) => [inset(u), inset(v)]);
8864
+ }
8865
+ /**
8866
+ * Moves every edge of a triangle outwards by `distance`, by scaling it around its incenter.
8867
+ * Growth is limited for very thin triangles, whose corners would otherwise shoot far out.
8868
+ */
8869
+ function growTriangle(triangle, distance) {
8870
+ const [[x0, y0], [x1, y1], [x2, y2]] = triangle;
8871
+ const a = Math.hypot(x2 - x1, y2 - y1);
8872
+ const b = Math.hypot(x2 - x0, y2 - y0);
8873
+ const c = Math.hypot(x1 - x0, y1 - y0);
8874
+ const perimeter = a + b + c;
8875
+ const area = Math.abs((x1 - x0) * (y2 - y0) - (x2 - x0) * (y1 - y0)) / 2;
8876
+ if (perimeter === 0 || area === 0)
8877
+ return triangle;
8878
+ const inradius = (2 * area) / perimeter;
8879
+ const cx = (a * x0 + b * x1 + c * x2) / perimeter;
8880
+ const cy = (a * y0 + b * y1 + c * y2) / perimeter;
8881
+ const scale = 1 + distance / Math.max(inradius, 2 * distance);
8882
+ return triangle.map(([x, y]) => [cx + (x - cx) * scale, cy + (y - cy) * scale]);
8883
+ }
8884
+ function formatUnit(v) {
8885
+ return (Math.round(v * 100000) / 100000).toString();
8886
+ }
8668
8887
  function formatScale(v) {
8669
8888
  return (Math.round(v * 10000) / 10000).toString();
8670
8889
  }
@@ -8758,6 +8977,12 @@ class Feature {
8758
8977
  properties;
8759
8978
  patterns;
8760
8979
  geometry;
8980
+ /**
8981
+ * The boundary of a polygon for the fill-antialias outline, as open polylines — when it
8982
+ * differs from the polygon's rings, i.e. when the polygon was clipped to its tile: the
8983
+ * clipped edges along the tile border are not part of the outline.
8984
+ */
8985
+ outline;
8761
8986
  #bbox;
8762
8987
  constructor(opt) {
8763
8988
  this.type = opt.type;
@@ -8765,6 +8990,7 @@ class Feature {
8765
8990
  this.properties = opt.properties;
8766
8991
  this.patterns = opt.patterns;
8767
8992
  this.geometry = opt.geometry;
8993
+ this.outline = opt.outline;
8768
8994
  }
8769
8995
  getBbox() {
8770
8996
  if (this.#bbox)
@@ -9859,6 +10085,138 @@ function readUtf8(buf, pos, end) {
9859
10085
  return str;
9860
10086
  }
9861
10087
 
10088
+ /**
10089
+ * Clipping of vector tile geometry to the tile's own square.
10090
+ *
10091
+ * Vector tiles contain a buffer: geometry reaches a little beyond the tile, so a polygon on
10092
+ * a tile border exists in both tiles. MapLibre draws each tile clipped to its square (with the
10093
+ * stencil buffer), so nothing is drawn twice. Opaque fills hide the difference, translucent
10094
+ * ones do not: the overlap would be drawn with double opacity.
10095
+ */
10096
+ /** Whether any point lies outside of the square [min, max]². */
10097
+ function exceedsSquare(rings, min, max) {
10098
+ return rings.some((ring) => ring.some(({ x, y }) => x < min || x > max || y < min || y > max));
10099
+ }
10100
+ /**
10101
+ * Clips the rings of a polygon to the square [min, max]² (Sutherland–Hodgman). Rings that
10102
+ * vanish are dropped.
10103
+ */
10104
+ function clipPolygon(rings, min, max) {
10105
+ const result = [];
10106
+ for (const ring of rings) {
10107
+ let points = ring;
10108
+ points = clipAgainst(points, (p) => p.x >= min, (a, b) => lerpAtX(a, b, min));
10109
+ points = clipAgainst(points, (p) => p.x <= max, (a, b) => lerpAtX(a, b, max));
10110
+ points = clipAgainst(points, (p) => p.y >= min, (a, b) => lerpAtY(a, b, min));
10111
+ points = clipAgainst(points, (p) => p.y <= max, (a, b) => lerpAtY(a, b, max));
10112
+ if (points.length >= 3)
10113
+ result.push(points);
10114
+ }
10115
+ return result;
10116
+ }
10117
+ /**
10118
+ * The boundary of a polygon within the square [min, max]², as open polylines: the parts of
10119
+ * its rings inside the square, without the edges the clipping adds along the square's border.
10120
+ * (MapLibre's fill outline follows the original rings, cut off at the tile border.)
10121
+ */
10122
+ function clipPolygonOutline(rings, min, max) {
10123
+ const result = [];
10124
+ for (const ring of rings) {
10125
+ if (ring.length < 2)
10126
+ continue;
10127
+ const first = ring[0];
10128
+ const last = ring[ring.length - 1];
10129
+ const closed = first.x === last.x && first.y === last.y ? ring : [...ring, first];
10130
+ result.push(...clipLine(closed, min, max));
10131
+ }
10132
+ return result;
10133
+ }
10134
+ /** Clips a polyline to the square [min, max]², returning the parts inside of it. */
10135
+ function clipLine(line, min, max) {
10136
+ const parts = [];
10137
+ let current = [];
10138
+ for (let i = 0; i + 1 < line.length; i++) {
10139
+ const segment = clipSegment(line[i], line[i + 1], min, max);
10140
+ if (!segment) {
10141
+ if (current.length > 1)
10142
+ parts.push(current);
10143
+ current = [];
10144
+ continue;
10145
+ }
10146
+ const [a, b] = segment;
10147
+ const last = current[current.length - 1];
10148
+ if (last?.x !== a.x || last.y !== a.y) {
10149
+ if (current.length > 1)
10150
+ parts.push(current);
10151
+ current = [a];
10152
+ }
10153
+ current.push(b);
10154
+ // The segment left the square: the line continues elsewhere.
10155
+ if (b !== line[i + 1]) {
10156
+ parts.push(current);
10157
+ current = [];
10158
+ }
10159
+ }
10160
+ if (current.length > 1)
10161
+ parts.push(current);
10162
+ return parts;
10163
+ }
10164
+ /** Liang–Barsky: the part of the segment a→b inside the square, or undefined. */
10165
+ function clipSegment(a, b, min, max) {
10166
+ const dx = b.x - a.x;
10167
+ const dy = b.y - a.y;
10168
+ let t0 = 0;
10169
+ let t1 = 1;
10170
+ for (const [p, q] of [
10171
+ [-dx, a.x - min],
10172
+ [dx, max - a.x],
10173
+ [-dy, a.y - min],
10174
+ [dy, max - a.y],
10175
+ ]) {
10176
+ if (p === 0) {
10177
+ if (q < 0)
10178
+ return undefined;
10179
+ continue;
10180
+ }
10181
+ const t = q / p;
10182
+ if (p < 0) {
10183
+ if (t > t1)
10184
+ return undefined;
10185
+ if (t > t0)
10186
+ t0 = t;
10187
+ }
10188
+ else {
10189
+ if (t < t0)
10190
+ return undefined;
10191
+ if (t < t1)
10192
+ t1 = t;
10193
+ }
10194
+ }
10195
+ return [
10196
+ t0 === 0 ? a : { x: a.x + dx * t0, y: a.y + dy * t0 },
10197
+ t1 === 1 ? b : { x: a.x + dx * t1, y: a.y + dy * t1 },
10198
+ ];
10199
+ }
10200
+ function clipAgainst(points, inside, intersect) {
10201
+ const result = [];
10202
+ for (let i = 0; i < points.length; i++) {
10203
+ const current = points[i];
10204
+ const previous = points[(i + points.length - 1) % points.length];
10205
+ const currentInside = inside(current);
10206
+ if (currentInside !== inside(previous))
10207
+ result.push(intersect(previous, current));
10208
+ if (currentInside)
10209
+ result.push(current);
10210
+ }
10211
+ return result;
10212
+ }
10213
+ function lerpAtX(a, b, x) {
10214
+ return { x, y: a.y + ((b.y - a.y) * (x - a.x)) / (b.x - a.x) };
10215
+ }
10216
+ function lerpAtY(a, b, y) {
10217
+ return { x: a.x + ((b.x - a.x) * (y - a.y)) / (b.y - a.y), y };
10218
+ }
10219
+
9862
10220
  const TILE_EXTENT = 4096;
9863
10221
  const VTFeatureType = { Unknown: 0, Point: 1, LineString: 2, Polygon: 3 };
9864
10222
  async function loadVectorSource(source, job, layerFeatures) {
@@ -9866,11 +10224,8 @@ async function loadVectorSource(source, job, layerFeatures) {
9866
10224
  if (!tiles)
9867
10225
  return;
9868
10226
  const { width, height } = job.renderer;
9869
- const { zoom, center } = job.view;
9870
- const { zoomLevel, tileSize, tiles: tileCoordinates, } = calculateTileGrid(width, height, center, zoom, source.maxzoom);
9871
- await Promise.all(tileCoordinates.map(async ({ x, y, offsetX, offsetY }) => {
9872
- const offset = new Point2D(offsetX, offsetY);
9873
- const tile = await getTile(tiles[0], zoomLevel, x, y);
10227
+ await Promise.all(getTileProjections(source, job).map(async ({ x, y, z, project, clipToTile }) => {
10228
+ const tile = await getTile(tiles[0], z, x, y);
9874
10229
  if (!tile)
9875
10230
  return;
9876
10231
  const vectorTile = new VectorTile(new PbfReader(tile.buffer));
@@ -9882,9 +10237,6 @@ async function loadVectorSource(source, job, layerFeatures) {
9882
10237
  }
9883
10238
  for (let i = 0; i < layer.length; i++) {
9884
10239
  const featureSrc = layer.feature(i);
9885
- const geometry = featureSrc
9886
- .loadGeometry()
9887
- .map((ring) => ring.map((point) => new Point2D(point.x, point.y).scale(tileSize / TILE_EXTENT).translate(offset)));
9888
10240
  let type;
9889
10241
  let list;
9890
10242
  switch (featureSrc.type) {
@@ -9903,6 +10255,22 @@ async function loadVectorSource(source, job, layerFeatures) {
9903
10255
  list = features.polygons;
9904
10256
  break;
9905
10257
  }
10258
+ let rings = featureSrc.loadGeometry();
10259
+ let outline;
10260
+ // Clip polygons and lines to the tile, like MapLibre's stencil clipping: otherwise
10261
+ // the parts in the tile buffer are drawn twice, which shows when translucent.
10262
+ if (clipToTile && type !== 'Point' && exceedsSquare(rings, 0, TILE_EXTENT)) {
10263
+ if (type === 'Polygon') {
10264
+ outline = project('LineString', clipPolygonOutline(rings, 0, TILE_EXTENT));
10265
+ rings = clipPolygon(rings, 0, TILE_EXTENT);
10266
+ }
10267
+ else {
10268
+ rings = rings.flatMap((line) => clipLine(line, 0, TILE_EXTENT));
10269
+ }
10270
+ }
10271
+ const geometry = project(type, rings);
10272
+ if (geometry.length === 0)
10273
+ continue;
9906
10274
  // Split MultiPoint into individual Point features
9907
10275
  if (type === 'Point' && geometry.length > 1) {
9908
10276
  for (const ring of geometry) {
@@ -9920,6 +10288,7 @@ async function loadVectorSource(source, job, layerFeatures) {
9920
10288
  const feature = new Feature({
9921
10289
  type,
9922
10290
  geometry,
10291
+ outline,
9923
10292
  id: featureSrc.id,
9924
10293
  properties: featureSrc.properties,
9925
10294
  });
@@ -9930,9 +10299,41 @@ async function loadVectorSource(source, job, layerFeatures) {
9930
10299
  }
9931
10300
  }));
9932
10301
  }
10302
+ function getTileProjections(source, job) {
10303
+ const { width, height } = job.renderer;
10304
+ const { zoom, center } = job.view;
10305
+ const projection = job.projection;
10306
+ if (projection?.isGlobe) {
10307
+ // Never zoom level 0 on the globe: the left and right edge of its single tile coincide
10308
+ // (the antimeridian), which the horizon clipping cannot tell apart.
10309
+ const z = Math.max(1, Math.min(Math.floor(zoom), source.maxzoom ?? Infinity));
10310
+ const scale = 1 / (TILE_EXTENT * 2 ** z);
10311
+ return projection.coveringTiles(z).map(({ x, y }) => ({
10312
+ x,
10313
+ y,
10314
+ z,
10315
+ clipToTile: projection.isTileFullyVisible({ x, y, z }),
10316
+ project: (type, rings) => projection.projectGeometry(type, rings.map((ring) => ring.map((p) => [
10317
+ (x * TILE_EXTENT + p.x) * scale,
10318
+ (y * TILE_EXTENT + p.y) * scale,
10319
+ ]))),
10320
+ }));
10321
+ }
10322
+ const { zoomLevel, tileSize, tiles } = calculateTileGrid(width, height, center, zoom, source.maxzoom);
10323
+ return tiles.map(({ x, y, offsetX, offsetY }) => {
10324
+ const offset = new Point2D(offsetX, offsetY);
10325
+ return {
10326
+ x,
10327
+ y,
10328
+ z: zoomLevel,
10329
+ clipToTile: true,
10330
+ project: (_type, rings) => rings.map((ring) => ring.map((point) => new Point2D(point.x, point.y).scale(tileSize / TILE_EXTENT).translate(offset))),
10331
+ };
10332
+ });
10333
+ }
9933
10334
 
9934
10335
  function loadGeoJSONSource(options) {
9935
- const { sourceName, data, width, height, zoom, center, layerFeatures } = options;
10336
+ const { sourceName, data, width, height, zoom, center, layerFeatures, projection } = options;
9936
10337
  const existing = layerFeatures.get(sourceName);
9937
10338
  const features = existing ?? {
9938
10339
  points: [],
@@ -9945,9 +10346,17 @@ function loadGeoJSONSource(options) {
9945
10346
  layerFeatures.set(sourceName, features);
9946
10347
  const worldSize = 512 * 2 ** zoom;
9947
10348
  const centerMercator = new Point2D(center[0], center[1]).getProject2Pixel();
9948
- function projectCoord(coord) {
10349
+ function toMercator(coord) {
9949
10350
  const mercator = new Point2D(coord[0], coord[1]).getProject2Pixel();
9950
- return new Point2D((mercator.x - centerMercator.x) * worldSize + width / 2, (mercator.y - centerMercator.y) * worldSize + height / 2);
10351
+ return [mercator.x, mercator.y];
10352
+ }
10353
+ function project(type, rings) {
10354
+ if (projection?.isGlobe) {
10355
+ if (type === 'Polygon')
10356
+ rings = orientRings(rings);
10357
+ return projection.projectGeometry(type, rings);
10358
+ }
10359
+ return rings.map((ring) => ring.map(([x, y]) => new Point2D((x - centerMercator.x) * worldSize + width / 2, (y - centerMercator.y) * worldSize + height / 2)));
9951
10360
  }
9952
10361
  function makeFeature(type, geometry, id, properties) {
9953
10362
  const feature = new Feature({ type, geometry, id, properties });
@@ -9958,7 +10367,10 @@ function loadGeoJSONSource(options) {
9958
10367
  function extractPoints(geometry) {
9959
10368
  return geometry.flatMap((ring) => ring.map((p) => [p]));
9960
10369
  }
9961
- function addFeature(type, geometry, id, properties) {
10370
+ function addFeature(type, rings, id, properties) {
10371
+ const geometry = project(type, rings);
10372
+ if (geometry.length === 0)
10373
+ return;
9962
10374
  switch (type) {
9963
10375
  case 'Point': {
9964
10376
  const f = makeFeature('Point', geometry, id, properties);
@@ -10001,23 +10413,23 @@ function loadGeoJSONSource(options) {
10001
10413
  function processGeometry(geom, id, properties) {
10002
10414
  switch (geom.type) {
10003
10415
  case 'Point':
10004
- addFeature('Point', [[projectCoord(geom.coordinates)]], id, properties);
10416
+ addFeature('Point', [[toMercator(geom.coordinates)]], id, properties);
10005
10417
  break;
10006
10418
  case 'MultiPoint':
10007
- addFeature('Point', geom.coordinates.map((c) => [projectCoord(c)]), id, properties);
10419
+ addFeature('Point', geom.coordinates.map((c) => [toMercator(c)]), id, properties);
10008
10420
  break;
10009
10421
  case 'LineString':
10010
- addFeature('LineString', [geom.coordinates.map((c) => projectCoord(c))], id, properties);
10422
+ addFeature('LineString', [geom.coordinates.map((c) => toMercator(c))], id, properties);
10011
10423
  break;
10012
10424
  case 'MultiLineString':
10013
- addFeature('LineString', geom.coordinates.map((line) => line.map((c) => projectCoord(c))), id, properties);
10425
+ addFeature('LineString', geom.coordinates.map((line) => line.map((c) => toMercator(c))), id, properties);
10014
10426
  break;
10015
10427
  case 'Polygon':
10016
- addFeature('Polygon', geom.coordinates.map((ring) => ring.map((c) => projectCoord(c))), id, properties);
10428
+ addFeature('Polygon', geom.coordinates.map((ring) => ring.map((c) => toMercator(c))), id, properties);
10017
10429
  break;
10018
10430
  case 'MultiPolygon':
10019
10431
  for (const polygon of geom.coordinates) {
10020
- addFeature('Polygon', polygon.map((ring) => ring.map((c) => projectCoord(c))), id, properties);
10432
+ addFeature('Polygon', polygon.map((ring) => ring.map((c) => toMercator(c))), id, properties);
10021
10433
  }
10022
10434
  break;
10023
10435
  case 'GeometryCollection':
@@ -10041,6 +10453,22 @@ function loadGeoJSONSource(options) {
10041
10453
  break;
10042
10454
  }
10043
10455
  }
10456
+ /**
10457
+ * Orients polygon rings like vector tiles do (in mercator space, y pointing south: the
10458
+ * exterior ring clockwise, holes counter-clockwise), as the globe clipping relies on it.
10459
+ */
10460
+ function orientRings(rings) {
10461
+ return rings.map((ring, index) => {
10462
+ let area = 0;
10463
+ for (let i = 0; i < ring.length; i++) {
10464
+ const [x0, y0] = ring[i];
10465
+ const [x1, y1] = ring[(i + 1) % ring.length];
10466
+ area += x0 * y1 - x1 * y0;
10467
+ }
10468
+ const isExterior = index === 0;
10469
+ return area > 0 === isExterior ? ring : [...ring].reverse();
10470
+ });
10471
+ }
10044
10472
 
10045
10473
  /**
10046
10474
  * Base64-encode an ArrayBuffer (standard alphabet, with padding).
@@ -10078,6 +10506,23 @@ async function getRasterTiles(job, sourceName) {
10078
10506
  throw Error(`Invalid raster source "${sourceName}": expected type "raster" with a "tiles" array`);
10079
10507
  }
10080
10508
  const sourceUrl = source.tiles[0];
10509
+ const projection = job.projection;
10510
+ if (projection?.isGlobe) {
10511
+ // Never zoom level 0 on the globe: the left and right edge of its single tile coincide
10512
+ // (the antimeridian), which the horizon clipping cannot tell apart.
10513
+ const z = Math.max(1, Math.min(Math.floor(zoom), source.maxzoom ?? Infinity));
10514
+ const globeTiles = await Promise.all(projection.coveringTiles(z).map(async (id) => {
10515
+ const tile = await getTile(sourceUrl, id.z, id.x, id.y);
10516
+ if (!tile)
10517
+ return null;
10518
+ const triangles = projection.rasterTriangles(id);
10519
+ if (triangles.length === 0)
10520
+ return null;
10521
+ const dataUri = `data:${tile.contentType};base64,${arrayBufferToBase64(tile.buffer)}`;
10522
+ return { x: 0, y: 0, width: 1, height: 1, dataUri, triangles };
10523
+ }));
10524
+ return globeTiles.filter((tile) => tile !== null);
10525
+ }
10081
10526
  const { zoomLevel, tileSize, tiles } = calculateTileGrid(width, height, center, zoom, source.maxzoom);
10082
10527
  const rasterTiles = await Promise.all(tiles.map(async ({ x, y, offsetX, offsetY }) => {
10083
10528
  const tile = await getTile(sourceUrl, zoomLevel, x, y);
@@ -10181,6 +10626,7 @@ async function getLayerFeatures(job) {
10181
10626
  zoom,
10182
10627
  center,
10183
10628
  layerFeatures,
10629
+ projection: job.projection,
10184
10630
  });
10185
10631
  }
10186
10632
  break;
@@ -10312,6 +10758,594 @@ function getLayerStyles(layers) {
10312
10758
  return layers.map(createStyleLayer);
10313
10759
  }
10314
10760
 
10761
+ /**
10762
+ * Map projection: web mercator, MapLibre's globe ("vertical-perspective") or a blend of both.
10763
+ *
10764
+ * The math follows MapLibre GL JS (`vertical_perspective_transform.ts` and the
10765
+ * `_projection_globe.vertex.glsl` shader) for a camera without pitch, bearing or padding:
10766
+ * the globe is a sphere of radius `worldSize / (2π·cos(centerLat))` (so the map center has
10767
+ * the same scale as in mercator), viewed by a perspective camera `1.5 · height` pixels above
10768
+ * the map center. The globe → mercator transition blends the homogeneous clip-space positions
10769
+ * of both projections (x, y and w) *before* the perspective divide, exactly like the shader.
10770
+ *
10771
+ * All inputs are mercator world coordinates in the range 0..1 (`mx` east, `my` south).
10772
+ */
10773
+ /** MapLibre's default vertical field of view (`TransformHelper._fovInRadians`). */
10774
+ const FOV = 0.6435011087932844;
10775
+ /** What MapLibre expands `projection: { type: 'globe' }` into (`projection_factory.ts`). */
10776
+ const GLOBE_PRESET = [
10777
+ 'interpolate',
10778
+ ['linear'],
10779
+ ['zoom'],
10780
+ 11,
10781
+ 'vertical-perspective',
10782
+ 12,
10783
+ 'mercator',
10784
+ ];
10785
+ /** Below this globeness the far side of the globe is not clipped (MapLibre's `z_globeness_threshold`). */
10786
+ const CLIP_THRESHOLD = 0.2;
10787
+ /** Maximum distance (pixels) a straight segment may deviate from the curved globe surface. */
10788
+ const MAX_CURVE_ERROR_PX = 0.25;
10789
+ /** The same for the affine-mapped triangles of raster tiles (a coarser mesh keeps the SVG small). */
10790
+ const MAX_RASTER_ERROR_PX = 1;
10791
+ /** Upper bound for the number of cells per raster tile side. */
10792
+ const MAX_RASTER_CELLS = 16;
10793
+ /** Angle to go clockwise on screen (decreasing angle in the y-up view frame) from a0 to a1, in [0, 2π). */
10794
+ function clockwiseDelta(a0, a1) {
10795
+ const delta = (a0 - a1) % (2 * Math.PI);
10796
+ return delta < 0 ? delta + 2 * Math.PI : delta;
10797
+ }
10798
+ /**
10799
+ * Returns how "globe-like" the projection is at the given zoom, matching MapLibre's
10800
+ * `GlobeProjection.transitionState`: 0 = mercator, 1 = globe, in between = transition.
10801
+ */
10802
+ function getGlobeness(projection, zoom) {
10803
+ return evaluateGlobeness(projection?.type ?? 'mercator', zoom);
10804
+ }
10805
+ function evaluateGlobeness(value, zoom) {
10806
+ if (typeof value === 'string') {
10807
+ switch (value) {
10808
+ case 'globe':
10809
+ return evaluateGlobeness(GLOBE_PRESET, zoom);
10810
+ case 'vertical-perspective':
10811
+ return 1;
10812
+ default:
10813
+ return 0;
10814
+ }
10815
+ }
10816
+ if (!Array.isArray(value))
10817
+ return 0;
10818
+ const [op, ...args] = value;
10819
+ if (op === 'literal')
10820
+ return evaluateGlobeness(args[0], zoom);
10821
+ if (op === 'step') {
10822
+ // ["step", ["zoom"], output0, stop1, output1, ...]
10823
+ let result = args[1];
10824
+ for (let i = 2; i + 1 < args.length; i += 2) {
10825
+ if (zoom >= args[i])
10826
+ result = args[i + 1];
10827
+ }
10828
+ return evaluateGlobeness(result, zoom);
10829
+ }
10830
+ if (op === 'interpolate') {
10831
+ // ["interpolate", ["linear"] | ["exponential", base], ["zoom"], stop0, output0, ...]
10832
+ const interpolation = args[0];
10833
+ const base = interpolation[0] === 'exponential' ? interpolation[1] : 1;
10834
+ const stops = [];
10835
+ for (let i = 2; i + 1 < args.length; i += 2)
10836
+ stops.push([args[i], args[i + 1]]);
10837
+ if (stops.length === 0)
10838
+ return 0;
10839
+ if (zoom <= stops[0][0])
10840
+ return evaluateGlobeness(stops[0][1], zoom);
10841
+ for (let i = 1; i < stops.length; i++) {
10842
+ const [z1, out1] = stops[i];
10843
+ if (zoom > z1)
10844
+ continue;
10845
+ const [z0, out0] = stops[i - 1];
10846
+ const t = interpolationFactor(zoom, z0, z1, base);
10847
+ const g0 = evaluateGlobeness(out0, zoom);
10848
+ const g1 = evaluateGlobeness(out1, zoom);
10849
+ return g0 + (g1 - g0) * t;
10850
+ }
10851
+ return evaluateGlobeness(stops[stops.length - 1][1], zoom);
10852
+ }
10853
+ // A projection transition literal: [from, to, transition]
10854
+ if (value.length === 3 && typeof value[2] === 'number') {
10855
+ const g0 = evaluateGlobeness(value[0], zoom);
10856
+ const g1 = evaluateGlobeness(value[1], zoom);
10857
+ return g0 + (g1 - g0) * value[2];
10858
+ }
10859
+ return 0;
10860
+ }
10861
+ function interpolationFactor(input, lower, upper, base) {
10862
+ const difference = upper - lower;
10863
+ if (difference === 0)
10864
+ return 0;
10865
+ const progress = input - lower;
10866
+ if (base === 1)
10867
+ return progress / difference;
10868
+ return (Math.pow(base, progress) - 1) / (Math.pow(base, difference) - 1);
10869
+ }
10870
+ class Projection {
10871
+ width;
10872
+ height;
10873
+ zoom;
10874
+ /** 0 = mercator, 1 = globe, in between = transition (see {@link getGlobeness}). */
10875
+ globeness;
10876
+ /** Size of the whole world in pixels at the current zoom (mercator). */
10877
+ worldSize;
10878
+ /**
10879
+ * Maximum length (in mercator units) of a straight segment before it has to be
10880
+ * subdivided to follow the curvature of the globe.
10881
+ */
10882
+ maxSegmentLength;
10883
+ /** Whether geometry on the far side of the globe has to be removed. */
10884
+ clipsHorizon;
10885
+ #centerX;
10886
+ #centerY;
10887
+ #centerLng;
10888
+ #sinLat;
10889
+ #cosLat;
10890
+ /** Globe radius in pixels. */
10891
+ #radius;
10892
+ /** Distance of the camera to the map center in pixels. */
10893
+ #cameraDistance;
10894
+ /** Points of the unit sphere with a view-space z below this are behind the horizon. */
10895
+ #horizonZ;
10896
+ constructor(opt) {
10897
+ this.width = opt.width;
10898
+ this.height = opt.height;
10899
+ this.zoom = opt.zoom;
10900
+ this.globeness = Math.min(1, Math.max(0, opt.globeness ?? 0));
10901
+ this.worldSize = 512 * 2 ** opt.zoom;
10902
+ const center = new Point2D(opt.center[0], opt.center[1]).getProject2Pixel();
10903
+ this.#centerX = center.x;
10904
+ this.#centerY = center.y;
10905
+ const lat = (opt.center[1] * Math.PI) / 180;
10906
+ this.#centerLng = (opt.center[0] * Math.PI) / 180;
10907
+ this.#sinLat = Math.sin(lat);
10908
+ this.#cosLat = Math.cos(lat);
10909
+ this.#radius = this.worldSize / (2 * Math.PI * this.#cosLat);
10910
+ this.#cameraDistance = this.height / 2 / Math.tan(FOV / 2);
10911
+ this.#horizonZ = this.#radius / (this.#radius + this.#cameraDistance);
10912
+ this.clipsHorizon = this.globeness > CLIP_THRESHOLD;
10913
+ // A chord spanning the angle θ deviates R·θ²/8 from the sphere.
10914
+ const maxAngle = Math.sqrt((8 * MAX_CURVE_ERROR_PX) / this.#radius);
10915
+ this.maxSegmentLength = maxAngle / (2 * Math.PI);
10916
+ }
10917
+ static fromStyle(opt) {
10918
+ return new Projection({ ...opt, globeness: getGlobeness(opt.projection, opt.zoom) });
10919
+ }
10920
+ /** True when the globe projection is (at least partially) in effect. */
10921
+ get isGlobe() {
10922
+ return this.globeness > 0;
10923
+ }
10924
+ /**
10925
+ * The silhouette of the globe on screen, if the map has to be clipped to it.
10926
+ * (During the globe → mercator transition the silhouette is far outside the screen.)
10927
+ */
10928
+ get clipCircle() {
10929
+ if (!this.clipsHorizon)
10930
+ return undefined;
10931
+ const r = this.#radius;
10932
+ const d = this.#cameraDistance;
10933
+ const radius = (d * r) / Math.sqrt((d + r) * (d + r) - r * r);
10934
+ return { x: this.width / 2, y: this.height / 2, radius };
10935
+ }
10936
+ /** Mercator world coordinates → unit sphere in the view frame (x right, y up, z towards the camera). */
10937
+ toSphere(mx, my) {
10938
+ const dLng = mx * 2 * Math.PI - Math.PI - this.#centerLng;
10939
+ const lat = 2 * Math.atan(Math.exp(Math.PI - my * 2 * Math.PI)) - Math.PI / 2;
10940
+ const cosLat = Math.cos(lat);
10941
+ const x = Math.sin(dLng) * cosLat;
10942
+ const y = Math.sin(lat);
10943
+ const z = Math.cos(dLng) * cosLat;
10944
+ return [x, y * this.#cosLat - z * this.#sinLat, y * this.#sinLat + z * this.#cosLat];
10945
+ }
10946
+ /** Inverse of {@link toSphere}. */
10947
+ fromSphere(v) {
10948
+ const y = v[1] * this.#cosLat + v[2] * this.#sinLat;
10949
+ const z = -v[1] * this.#sinLat + v[2] * this.#cosLat;
10950
+ const lat = Math.asin(Math.max(-1, Math.min(1, y)));
10951
+ const lng = Math.atan2(v[0], z) + this.#centerLng;
10952
+ const mx = (lng + Math.PI) / (2 * Math.PI);
10953
+ const my = (Math.PI - Math.log(Math.tan(Math.PI / 4 + lat / 2))) / (2 * Math.PI);
10954
+ return [mx - Math.floor(mx), my];
10955
+ }
10956
+ isVisible(v) {
10957
+ return !this.clipsHorizon || v[2] >= this.#horizonZ;
10958
+ }
10959
+ /** Screen position of a mercator point. */
10960
+ project(mx, my, v) {
10961
+ // Flat (mercator) position, relative to the screen center. Use the world copy
10962
+ // nearest to the center.
10963
+ let dx = mx - this.#centerX;
10964
+ dx -= Math.round(dx);
10965
+ const flatX = dx * this.worldSize;
10966
+ const flatY = (my - this.#centerY) * this.worldSize;
10967
+ const t = this.globeness;
10968
+ if (t === 0)
10969
+ return new Point2D(flatX + this.width / 2, flatY + this.height / 2);
10970
+ v ??= this.toSphere(mx, my);
10971
+ const d = this.#cameraDistance;
10972
+ const r = this.#radius;
10973
+ // Homogeneous coordinates (x, y, w) of the globe position; the flat one is (d·x, d·y, d).
10974
+ const globeX = d * r * v[0];
10975
+ const globeY = -d * r * v[1];
10976
+ const globeW = d + r * (1 - v[2]);
10977
+ const w = d + (globeW - d) * t;
10978
+ return new Point2D((d * flatX + (globeX - d * flatX) * t) / w + this.width / 2, (d * flatY + (globeY - d * flatY) * t) / w + this.height / 2);
10979
+ }
10980
+ /**
10981
+ * Projects feature geometry given in mercator world coordinates to screen pixels.
10982
+ * On the globe, segments are subdivided to follow the curvature, lines are cut at
10983
+ * the horizon and polygons are clipped to the visible hemisphere (the clipped part of
10984
+ * the boundary follows the globe's silhouette).
10985
+ */
10986
+ projectGeometry(type, rings) {
10987
+ if (!this.isGlobe)
10988
+ return rings.map((ring) => ring.map(([mx, my]) => this.project(mx, my)));
10989
+ switch (type) {
10990
+ case 'Point':
10991
+ return rings
10992
+ .map((ring) => ring.flatMap(([mx, my]) => {
10993
+ const v = this.toSphere(mx, my);
10994
+ return this.isVisible(v) ? [this.project(mx, my, v)] : [];
10995
+ }))
10996
+ .filter((ring) => ring.length > 0);
10997
+ case 'LineString':
10998
+ return rings.flatMap((ring) => this.#clipLine(this.#densify(ring, false)));
10999
+ case 'Polygon':
11000
+ return this.#clipPolygon(rings.map((ring) => this.#densify(ring, true)));
11001
+ }
11002
+ }
11003
+ /** Subdivides long segments (in mercator space) and lifts the points onto the sphere. */
11004
+ #densify(ring, closed) {
11005
+ const maxLength = this.maxSegmentLength;
11006
+ const result = [];
11007
+ const count = ring.length;
11008
+ const segments = closed ? count : count - 1;
11009
+ for (let i = 0; i < count; i++) {
11010
+ const [x0, y0] = ring[i];
11011
+ result.push({ mx: x0, my: y0, v: this.toSphere(x0, y0) });
11012
+ if (i >= segments)
11013
+ continue;
11014
+ const [x1, y1] = ring[(i + 1) % count];
11015
+ const steps = Math.ceil(Math.max(Math.abs(x1 - x0), Math.abs(y1 - y0)) / maxLength);
11016
+ for (let s = 1; s < steps; s++) {
11017
+ const mx = x0 + ((x1 - x0) * s) / steps;
11018
+ const my = y0 + ((y1 - y0) * s) / steps;
11019
+ result.push({ mx, my, v: this.toSphere(mx, my) });
11020
+ }
11021
+ }
11022
+ return result;
11023
+ }
11024
+ /** Point on the segment a→b where it crosses the horizon plane. */
11025
+ #horizonCrossing(a, b) {
11026
+ const s = (a.v[2] - this.#horizonZ) / (a.v[2] - b.v[2]);
11027
+ const mx = a.mx + (b.mx - a.mx) * s;
11028
+ const my = a.my + (b.my - a.my) * s;
11029
+ return { mx, my, v: this.toSphere(mx, my) };
11030
+ }
11031
+ /** Splits a line into its visible parts. */
11032
+ #clipLine(points) {
11033
+ if (!this.clipsHorizon)
11034
+ return [points.map((p) => this.project(p.mx, p.my, p.v))];
11035
+ const parts = [];
11036
+ let current = [];
11037
+ for (let i = 0; i < points.length; i++) {
11038
+ const p = points[i];
11039
+ const visible = this.isVisible(p.v);
11040
+ const prev = i > 0 ? points[i - 1] : undefined;
11041
+ if (prev && this.isVisible(prev.v) !== visible) {
11042
+ const c = this.#horizonCrossing(prev, p);
11043
+ current.push(this.project(c.mx, c.my, c.v));
11044
+ if (!visible) {
11045
+ parts.push(current);
11046
+ current = [];
11047
+ }
11048
+ }
11049
+ if (visible)
11050
+ current.push(this.project(p.mx, p.my, p.v));
11051
+ }
11052
+ if (current.length > 0)
11053
+ parts.push(current);
11054
+ return parts.filter((part) => part.length > 1);
11055
+ }
11056
+ /**
11057
+ * Clips the rings of a polygon to the visible hemisphere.
11058
+ *
11059
+ * Every ring crossing the horizon is cut into its visible chains, each running from an
11060
+ * entry point to an exit point on the horizon. The chains are then reconnected along the
11061
+ * silhouette: from an exit point, the silhouette is followed to the nearest entry point
11062
+ * (of any chain of any ring). Rings follow the vector tile winding (exterior rings
11063
+ * clockwise on screen, holes counter-clockwise), which keeps the filled area on the right
11064
+ * of every ring, so the silhouette is always walked clockwise.
11065
+ */
11066
+ #clipPolygon(rings) {
11067
+ const project = (p) => this.project(p.mx, p.my, p.v);
11068
+ if (!this.clipsHorizon)
11069
+ return rings.map((ring) => ring.map(project));
11070
+ const result = [];
11071
+ const chains = [];
11072
+ const angleOf = (p) => Math.atan2(p.v[1], p.v[0]);
11073
+ for (const ring of rings) {
11074
+ const count = ring.length;
11075
+ const visible = ring.map((p) => this.isVisible(p.v));
11076
+ // Start walking at an entry point, so every chain is complete.
11077
+ let start = -1;
11078
+ for (let i = 0; i < count; i++) {
11079
+ if (visible[i] && !visible[(i + count - 1) % count]) {
11080
+ start = i;
11081
+ break;
11082
+ }
11083
+ }
11084
+ if (start < 0) {
11085
+ // No crossing: the ring is either completely visible or completely hidden.
11086
+ if (visible[0])
11087
+ result.push(ring.map(project));
11088
+ continue;
11089
+ }
11090
+ let chain;
11091
+ for (let k = 0; k < count; k++) {
11092
+ const i = (start + k) % count;
11093
+ const p = ring[i];
11094
+ const prev = ring[(i + count - 1) % count];
11095
+ if (visible[i]) {
11096
+ if (!chain) {
11097
+ const c = this.#horizonCrossing(prev, p);
11098
+ chain = { entry: angleOf(c), exit: 0, points: [project(c)] };
11099
+ }
11100
+ chain.points.push(project(p));
11101
+ }
11102
+ else if (chain) {
11103
+ const c = this.#horizonCrossing(prev, p);
11104
+ chain.points.push(project(c));
11105
+ chain.exit = angleOf(c);
11106
+ chains.push(chain);
11107
+ chain = undefined;
11108
+ }
11109
+ }
11110
+ }
11111
+ // Reconnect the chains along the silhouette.
11112
+ const used = new Set();
11113
+ for (const first of chains) {
11114
+ if (used.has(first))
11115
+ continue;
11116
+ const ring = [];
11117
+ let chain = first;
11118
+ for (;;) {
11119
+ used.add(chain);
11120
+ ring.push(...chain.points);
11121
+ let next = first;
11122
+ let bestDelta = clockwiseDelta(chain.exit, first.entry);
11123
+ for (const candidate of chains) {
11124
+ if (used.has(candidate))
11125
+ continue;
11126
+ const delta = clockwiseDelta(chain.exit, candidate.entry);
11127
+ if (delta < bestDelta) {
11128
+ bestDelta = delta;
11129
+ next = candidate;
11130
+ }
11131
+ }
11132
+ this.#pushArc(ring, chain.exit, next.entry);
11133
+ if (next === first)
11134
+ break;
11135
+ chain = next;
11136
+ }
11137
+ result.push(ring);
11138
+ }
11139
+ return result;
11140
+ }
11141
+ /**
11142
+ * Appends points along the horizon circle, clockwise on screen (decreasing angle in the
11143
+ * y-up view frame) from angle a0 to a1.
11144
+ */
11145
+ #pushArc(result, a0, a1) {
11146
+ const delta = clockwiseDelta(a0, a1);
11147
+ const steps = Math.ceil(delta / (Math.PI / 90));
11148
+ const h = this.#horizonZ;
11149
+ const rho = Math.sqrt(1 - h * h);
11150
+ for (let s = 1; s < steps; s++) {
11151
+ const a = a0 - (delta * s) / steps;
11152
+ const v = [rho * Math.cos(a), rho * Math.sin(a), h];
11153
+ const [mx, my] = this.fromSphere(v);
11154
+ result.push(this.project(mx, my, v));
11155
+ }
11156
+ }
11157
+ /**
11158
+ * Splits a raster tile into a mesh of triangles, each small enough to be drawn with an
11159
+ * affine transform (see {@link RasterTriangle}). An affine transform is fully determined
11160
+ * by three points, so every triangle meets its projected corners exactly and neighbouring
11161
+ * triangles share their edges. Cells on the far side of the globe are dropped.
11162
+ */
11163
+ rasterTriangles(tile) {
11164
+ const tileSize = 1 / 2 ** tile.z;
11165
+ const maxLength = this.maxSegmentLength * Math.sqrt(MAX_RASTER_ERROR_PX / MAX_CURVE_ERROR_PX);
11166
+ const n = Math.max(1, Math.min(MAX_RASTER_CELLS, Math.ceil(tileSize / maxLength)));
11167
+ // Project the (n+1)² grid corners once.
11168
+ const corners = [];
11169
+ for (let j = 0; j <= n; j++) {
11170
+ for (let i = 0; i <= n; i++) {
11171
+ const mx = (tile.x + i / n) * tileSize;
11172
+ const my = (tile.y + j / n) * tileSize;
11173
+ let v = this.toSphere(mx, my);
11174
+ if (this.isVisible(v)) {
11175
+ corners.push(this.project(mx, my, v));
11176
+ }
11177
+ else {
11178
+ // Pull the corner onto the horizon, so cells at the silhouette end there.
11179
+ if (this.#isBehind(v)) {
11180
+ corners.push(undefined);
11181
+ continue;
11182
+ }
11183
+ v = this.#clampToHorizon(v);
11184
+ const [cx, cy] = this.fromSphere(v);
11185
+ corners.push(this.project(cx, cy, v));
11186
+ }
11187
+ }
11188
+ }
11189
+ const triangles = [];
11190
+ for (let j = 0; j < n; j++) {
11191
+ for (let i = 0; i < n; i++) {
11192
+ const p00 = corners[j * (n + 1) + i];
11193
+ const p10 = corners[j * (n + 1) + i + 1];
11194
+ const p01 = corners[(j + 1) * (n + 1) + i];
11195
+ const p11 = corners[(j + 1) * (n + 1) + i + 1];
11196
+ if (!p00 || !p10 || !p01 || !p11)
11197
+ continue;
11198
+ const minX = Math.min(p00.x, p10.x, p01.x, p11.x);
11199
+ const maxX = Math.max(p00.x, p10.x, p01.x, p11.x);
11200
+ const minY = Math.min(p00.y, p10.y, p01.y, p11.y);
11201
+ const maxY = Math.max(p00.y, p10.y, p01.y, p11.y);
11202
+ if (maxX < 0 || minX > this.width || maxY < 0 || minY > this.height)
11203
+ continue;
11204
+ if (!this.#isCellVisible(tile, n, i, j))
11205
+ continue;
11206
+ const u0 = i / n;
11207
+ const v0 = j / n;
11208
+ const u1 = (i + 1) / n;
11209
+ const v1 = (j + 1) / n;
11210
+ triangles.push({
11211
+ source: [
11212
+ [u0, v0],
11213
+ [u1, v0],
11214
+ [u1, v1],
11215
+ ],
11216
+ target: [
11217
+ [p00.x, p00.y],
11218
+ [p10.x, p10.y],
11219
+ [p11.x, p11.y],
11220
+ ],
11221
+ }, {
11222
+ source: [
11223
+ [u0, v0],
11224
+ [u1, v1],
11225
+ [u0, v1],
11226
+ ],
11227
+ target: [
11228
+ [p00.x, p00.y],
11229
+ [p11.x, p11.y],
11230
+ [p01.x, p01.y],
11231
+ ],
11232
+ });
11233
+ }
11234
+ }
11235
+ return triangles;
11236
+ }
11237
+ /** Whether any part of a raster cell is on the visible side of the globe. */
11238
+ #isCellVisible(tile, n, i, j) {
11239
+ if (!this.clipsHorizon)
11240
+ return true;
11241
+ const tileSize = 1 / 2 ** tile.z;
11242
+ for (let dj = 0; dj <= 2; dj++) {
11243
+ for (let di = 0; di <= 2; di++) {
11244
+ const mx = (tile.x + (i + di / 2) / n) * tileSize;
11245
+ const my = (tile.y + (j + dj / 2) / n) * tileSize;
11246
+ if (this.isVisible(this.toSphere(mx, my)))
11247
+ return true;
11248
+ }
11249
+ }
11250
+ return false;
11251
+ }
11252
+ /** Moves a point on the far side of the globe onto the horizon (keeping its direction on screen). */
11253
+ #clampToHorizon(v) {
11254
+ const h = this.#horizonZ;
11255
+ const rho = Math.sqrt(1 - h * h);
11256
+ const len = Math.hypot(v[0], v[1]);
11257
+ if (len < 1e-12)
11258
+ return [rho, 0, h];
11259
+ return [(v[0] / len) * rho, (v[1] / len) * rho, h];
11260
+ }
11261
+ /** Points almost opposite the view direction have no meaningful place on the horizon. */
11262
+ #isBehind(v) {
11263
+ return Math.hypot(v[0], v[1]) < 1e-9;
11264
+ }
11265
+ /**
11266
+ * Tiles at zoom level `z` that are visible on screen, found by walking down the tile
11267
+ * pyramid and projecting a grid of sample points of each tile.
11268
+ */
11269
+ coveringTiles(z) {
11270
+ const result = [];
11271
+ const visit = (tz, tx, ty) => {
11272
+ if (tz >= 2 && !this.#isTileVisible(tz, tx, ty))
11273
+ return;
11274
+ if (tz === z) {
11275
+ result.push({ x: tx, y: ty, z: tz });
11276
+ return;
11277
+ }
11278
+ for (let dy = 0; dy < 2; dy++) {
11279
+ for (let dx = 0; dx < 2; dx++)
11280
+ visit(tz + 1, tx * 2 + dx, ty * 2 + dy);
11281
+ }
11282
+ };
11283
+ visit(0, 0, 0);
11284
+ return result;
11285
+ }
11286
+ /**
11287
+ * Whether a tile, including a margin around it (the tile buffer, as a fraction of the tile
11288
+ * size), lies completely on the visible side of the globe, so no horizon clipping happens.
11289
+ */
11290
+ isTileFullyVisible(tile, margin = 0.1) {
11291
+ if (!this.clipsHorizon)
11292
+ return true;
11293
+ const size = 1 / 2 ** tile.z;
11294
+ const samples = 8;
11295
+ for (let j = 0; j <= samples; j++) {
11296
+ for (let i = 0; i <= samples; i++) {
11297
+ if (i !== 0 && i !== samples && j !== 0 && j !== samples)
11298
+ continue; // the border only
11299
+ const mx = (tile.x - margin + ((1 + 2 * margin) * i) / samples) * size;
11300
+ const my = (tile.y - margin + ((1 + 2 * margin) * j) / samples) * size;
11301
+ if (!this.isVisible(this.toSphere(mx, my)))
11302
+ return false;
11303
+ }
11304
+ }
11305
+ return true;
11306
+ }
11307
+ #isTileVisible(z, x, y) {
11308
+ const samples = 6;
11309
+ const size = 1 / 2 ** z;
11310
+ let minX = Infinity;
11311
+ let minY = Infinity;
11312
+ let maxX = -Infinity;
11313
+ let maxY = -Infinity;
11314
+ const addSample = (mx, my) => {
11315
+ const v = this.toSphere(mx, my);
11316
+ if (!this.isVisible(v))
11317
+ return;
11318
+ const p = this.project(mx, my, v);
11319
+ if (p.x < minX)
11320
+ minX = p.x;
11321
+ if (p.y < minY)
11322
+ minY = p.y;
11323
+ if (p.x > maxX)
11324
+ maxX = p.x;
11325
+ if (p.y > maxY)
11326
+ maxY = p.y;
11327
+ };
11328
+ for (let j = 0; j <= samples; j++) {
11329
+ for (let i = 0; i <= samples; i++)
11330
+ addSample((x + i / samples) * size, (y + j / samples) * size);
11331
+ }
11332
+ // Zoomed in, the visible part of the globe can be much smaller than the sample spacing.
11333
+ // So also sample the point of the tile closest to the map center.
11334
+ const x0 = x * size;
11335
+ const centerX = this.#centerX - Math.round(this.#centerX - (x0 + size / 2));
11336
+ addSample(clamp(centerX, x0, x0 + size), clamp(this.#centerY, y * size, (y + 1) * size));
11337
+ // A generous margin: the samples may miss the part of the tile at the silhouette.
11338
+ const margin = Math.max(maxX - minX, maxY - minY) / samples + 1;
11339
+ return (minX - margin <= this.width &&
11340
+ maxX + margin >= 0 &&
11341
+ minY - margin <= this.height &&
11342
+ maxY + margin >= 0);
11343
+ }
11344
+ }
11345
+ function clamp(value, min, max) {
11346
+ return Math.min(max, Math.max(min, value));
11347
+ }
11348
+
10315
11349
  function resolveTokens(text, properties) {
10316
11350
  return text.replace(/\{([^}]+)\}/g, (_, key) => {
10317
11351
  const value = properties[key];
@@ -10325,6 +11359,16 @@ function resolveTokens(text, properties) {
10325
11359
  });
10326
11360
  }
10327
11361
  async function renderMap(job) {
11362
+ job.projection ??= Projection.fromStyle({
11363
+ width: job.renderer.width,
11364
+ height: job.renderer.height,
11365
+ center: job.view.center,
11366
+ zoom: job.view.zoom,
11367
+ projection: job.style.projection,
11368
+ });
11369
+ const clipCircle = job.projection.clipCircle;
11370
+ if (clipCircle)
11371
+ job.renderer.setClipCircle?.(clipCircle);
10328
11372
  await render(job);
10329
11373
  return job.renderer.getString();
10330
11374
  }