@tangram.gl/basemap-layers 0.0.0 → 10.0.0-alpha.3

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.
Files changed (139) hide show
  1. package/README.md +12 -7
  2. package/THIRD-PARTY-NOTICES.md +7 -5
  3. package/dist/geojson-task-worker.js +3166 -0
  4. package/dist/geojson-task-worker.js.map +6 -0
  5. package/dist/geojson-worker.js +1089 -7
  6. package/dist/geojson-worker.js.map +3 -3
  7. package/dist/index.cjs +9588 -2093
  8. package/dist/index.cjs.map +3 -3
  9. package/dist/index.js +9599 -2090
  10. package/dist/index.js.map +3 -3
  11. package/dist/map-style.cjs +456 -20
  12. package/dist/map-style.cjs.map +3 -3
  13. package/dist/map-style.js +456 -19
  14. package/dist/map-style.js.map +2 -2
  15. package/dist/types/basemap-error.d.ts +52 -0
  16. package/dist/types/basemap-layer.d.ts +378 -25
  17. package/dist/types/basemap-protocol.d.ts +91 -0
  18. package/dist/types/basemap-request.d.ts +73 -0
  19. package/dist/types/circle-layer.d.ts +65 -8
  20. package/dist/types/color-relief-layer.d.ts +184 -0
  21. package/dist/types/dem-pixels.d.ts +40 -0
  22. package/dist/types/feature-state.d.ts +55 -0
  23. package/dist/types/fill-extrusion-lighting-extension.d.ts +1 -2
  24. package/dist/types/fill-outline-extension.d.ts +2 -3
  25. package/dist/types/fill-pattern.d.ts +35 -3
  26. package/dist/types/fill-style-extensions.d.ts +11 -59
  27. package/dist/types/geojson-resource.d.ts +18 -0
  28. package/dist/types/geojson-source.d.ts +13 -2
  29. package/dist/types/geojson-task-handler.d.ts +2 -0
  30. package/dist/types/geojson-task.d.ts +26 -0
  31. package/dist/types/geojson-tiler.d.ts +29 -2
  32. package/dist/types/geojson-worker.d.ts +5 -1
  33. package/dist/types/globe-horizon-extension.d.ts +2 -45
  34. package/dist/types/globe-layers.d.ts +141 -45
  35. package/dist/types/globe-line-angle-extension.d.ts +20 -17
  36. package/dist/types/glyph-store.d.ts +10 -1
  37. package/dist/types/heatmap-layer.d.ts +99 -0
  38. package/dist/types/hillshade-layer.d.ts +159 -27
  39. package/dist/types/icon-text-fit.d.ts +92 -0
  40. package/dist/types/image-source-layer.d.ts +142 -0
  41. package/dist/types/index.d.ts +25 -1
  42. package/dist/types/line-blur-extension.d.ts +3 -11
  43. package/dist/types/line-dash-extension.d.ts +53 -0
  44. package/dist/types/line-gradient-extension.d.ts +69 -0
  45. package/dist/types/line-metrics.d.ts +24 -0
  46. package/dist/types/line-pattern-extension.d.ts +50 -0
  47. package/dist/types/line-shape-extension.d.ts +76 -0
  48. package/dist/types/map-style-loader.d.ts +1 -1
  49. package/dist/types/map-style.d.ts +2 -2
  50. package/dist/types/mvt-label-layer.d.ts +111 -20
  51. package/dist/types/pmtiles-source.d.ts +38 -0
  52. package/dist/types/projected-background-layer.d.ts +63 -0
  53. package/dist/types/projected-bitmap-layer.d.ts +58 -0
  54. package/dist/types/projected-tile-data.d.ts +16 -0
  55. package/dist/types/projected-tile-selection.d.ts +72 -0
  56. package/dist/types/projected-tileset.d.ts +92 -0
  57. package/dist/types/projected-viewport.d.ts +76 -0
  58. package/dist/types/raster-color-extension.d.ts +4 -7
  59. package/dist/types/raster-fade.d.ts +155 -0
  60. package/dist/types/raster-tile-fingerprint.d.ts +11 -0
  61. package/dist/types/raster-tile-loader.d.ts +80 -0
  62. package/dist/types/shader-injection-plugin.d.ts +8 -0
  63. package/dist/types/source-features.d.ts +58 -0
  64. package/dist/types/source-tiles.d.ts +32 -0
  65. package/dist/types/sprite.d.ts +37 -3
  66. package/dist/types/stretch-icon-layer.d.ts +28 -0
  67. package/dist/types/style-accessor.d.ts +50 -12
  68. package/dist/types/style-expression.d.ts +22 -2
  69. package/dist/types/style-resolver.d.ts +47 -2
  70. package/dist/types/terrain-query.d.ts +39 -0
  71. package/dist/types/zoom-load-gate.d.ts +22 -0
  72. package/dist/types/zoom-paint-extension.d.ts +80 -11
  73. package/package.json +24 -21
  74. package/src/basemap-error.ts +82 -0
  75. package/src/basemap-layer.ts +1289 -101
  76. package/src/basemap-protocol.ts +353 -0
  77. package/src/basemap-request.ts +212 -0
  78. package/src/circle-layer.ts +430 -54
  79. package/src/color-relief-layer.ts +572 -0
  80. package/src/dem-pixels.ts +174 -0
  81. package/src/feature-state.ts +164 -0
  82. package/src/fill-extrusion-lighting-extension.ts +126 -9
  83. package/src/fill-outline-extension.ts +4 -4
  84. package/src/fill-pattern.ts +87 -9
  85. package/src/fill-style-extensions.ts +114 -44
  86. package/src/geojson-resource.ts +58 -0
  87. package/src/geojson-source.ts +106 -85
  88. package/src/geojson-task-handler.ts +11 -0
  89. package/src/geojson-task-worker.ts +8 -0
  90. package/src/geojson-task.ts +34 -0
  91. package/src/geojson-tiler.ts +270 -4
  92. package/src/geojson-worker.ts +36 -6
  93. package/src/globe-horizon-extension.ts +14 -4
  94. package/src/globe-layers.ts +1905 -406
  95. package/src/globe-line-angle-extension.ts +183 -23
  96. package/src/glyph-store.ts +28 -6
  97. package/src/heatmap-layer.ts +591 -0
  98. package/src/hillshade-layer.ts +309 -88
  99. package/src/icon-text-fit.ts +358 -0
  100. package/src/image-source-layer.ts +380 -0
  101. package/src/index.ts +58 -1
  102. package/src/line-blur-extension.ts +14 -12
  103. package/src/line-dash-extension.ts +303 -0
  104. package/src/line-gradient-extension.ts +191 -0
  105. package/src/line-metrics.ts +90 -0
  106. package/src/line-pattern-extension.ts +173 -0
  107. package/src/line-shape-extension.ts +272 -0
  108. package/src/map-style-loader.ts +5 -4
  109. package/src/map-style.ts +2 -1
  110. package/src/mvt-label-layer.ts +568 -144
  111. package/src/pmtiles-source.ts +276 -0
  112. package/src/projected-background-layer.ts +202 -0
  113. package/src/projected-bitmap-layer.ts +262 -0
  114. package/src/projected-tile-data.ts +132 -0
  115. package/src/projected-tile-selection.ts +605 -0
  116. package/src/projected-tileset.ts +183 -0
  117. package/src/projected-viewport.ts +254 -0
  118. package/src/raster-color-extension.ts +43 -18
  119. package/src/raster-fade.ts +319 -0
  120. package/src/raster-tile-fingerprint.ts +31 -0
  121. package/src/raster-tile-loader.ts +211 -0
  122. package/src/shader-injection-plugin.ts +25 -0
  123. package/src/source-features.ts +170 -0
  124. package/src/source-tiles.ts +102 -0
  125. package/src/sprite.ts +122 -15
  126. package/src/stretch-icon-layer.ts +87 -0
  127. package/src/style-accessor.ts +105 -17
  128. package/src/style-expression.ts +80 -3
  129. package/src/style-resolver.ts +200 -22
  130. package/src/terrain-query.ts +86 -0
  131. package/src/text-layout.ts +2 -3
  132. package/src/zoom-load-gate.ts +57 -0
  133. package/src/zoom-paint-extension.ts +245 -47
  134. package/dist/types/arabic-shaping.d.ts +0 -7
  135. package/dist/types/bidi.d.ts +0 -8
  136. package/dist/types/polylabel.d.ts +0 -17
  137. package/src/arabic-shaping.ts +0 -366
  138. package/src/bidi.ts +0 -199
  139. package/src/polylabel.ts +0 -312
@@ -0,0 +1,358 @@
1
+ // tangram-layers
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+
5
+ /**
6
+ * `icon-text-fit`: an icon stretched to cover its label, and the stretchable images that sprites
7
+ * describe with `stretchX`, `stretchY` and `content`, laid out as MapLibre GL JS lays them out
8
+ * (`fitIconToText` and `applyTextFit` in its `symbol/shaping.ts`, `getIconQuads` in
9
+ * `symbol/quads.ts`). Boxes are in CSS pixels from the symbol's anchor, y down.
10
+ */
11
+
12
+ /** `icon-text-fit`: which sides of the label the icon is stretched to. */
13
+ export type IconTextFit = 'none' | 'width' | 'height' | 'both';
14
+
15
+ /** `textFitWidth` / `textFitHeight` of a sprite image. */
16
+ export type ImageTextFit = 'stretchOrShrink' | 'stretchOnly' | 'proportional';
17
+
18
+ /** A box in pixels from the symbol's anchor, y down. */
19
+ export type SymbolBox = {left: number; top: number; right: number; bottom: number};
20
+
21
+ /**
22
+ * The size and stretch metadata of a sprite image, as its JSON index gives them: sizes and zones
23
+ * in image pixels, which are `pixelRatio` per CSS pixel.
24
+ */
25
+ export type StretchableImage = {
26
+ width: number;
27
+ height: number;
28
+ pixelRatio: number;
29
+ /** Ranges `[x1, x2]` of image columns that stretch. */
30
+ stretchX?: [number, number][];
31
+ /** Ranges `[y1, y2]` of image rows that stretch. */
32
+ stretchY?: [number, number][];
33
+ /** `[x1, y1, x2, y2]`: the part of the image that the label covers. */
34
+ content?: [number, number, number, number];
35
+ textFitWidth?: ImageTextFit;
36
+ textFitHeight?: ImageTextFit;
37
+ };
38
+
39
+ /** An icon box fitted to its label, and how much of the image lies outside it (see `content`). */
40
+ export type FittedIcon = SymbolBox & {
41
+ /** CSS pixels of the image outside `content`: left, top, right, bottom. */
42
+ collisionPadding?: [number, number, number, number];
43
+ };
44
+
45
+ /**
46
+ * One rectangle of a stretched icon, drawn from one part of its image. Its corners are
47
+ * `em * max(minScale, iconSize) + px` (see {@link getIconQuadBox}): the `em` part stretches with
48
+ * the label and scales with `icon-size`, the `px` part keeps the fixed zones at their size.
49
+ */
50
+ export type IconQuad = {
51
+ /** The part of the image, in image pixels from its top left corner. */
52
+ image: {x: number; y: number; width: number; height: number};
53
+ em: SymbolBox;
54
+ px: SymbolBox;
55
+ /** The least scale of `em` on each axis, so that the stretched zones never shrink below zero. */
56
+ minScale: [number, number];
57
+ };
58
+
59
+ /**
60
+ * MapLibre's `fitIconToText`: the box of an icon stretched to the box of its label, padded by
61
+ * `icon-text-fit-padding` (`[top, right, bottom, left]`), and moved by `icon-offset`. On an axis it
62
+ * is not stretched along, the icon keeps its size, centered on the label. `icon-anchor` is not
63
+ * read. The box is at `icon-size` 1; {@link getIconQuads} and {@link getFittedIconCollisionBox}
64
+ * scale it.
65
+ */
66
+ export function fitIconToText(
67
+ image: StretchableImage,
68
+ text: SymbolBox,
69
+ textFit: IconTextFit,
70
+ padding: readonly number[],
71
+ iconOffset: readonly number[]
72
+ ): FittedIcon {
73
+ const width = image.width / image.pixelRatio;
74
+ const height = image.height / image.pixelRatio;
75
+ const [padTop, padRight, padBottom, padLeft] = padding;
76
+ const [offsetX, offsetY] = iconOffset;
77
+ let left: number;
78
+ let right: number;
79
+ if (textFit === 'width' || textFit === 'both') {
80
+ left = offsetX + text.left - padLeft;
81
+ right = offsetX + text.right + padRight;
82
+ } else {
83
+ left = offsetX + (text.left + text.right - width) / 2;
84
+ right = left + width;
85
+ }
86
+ let top: number;
87
+ let bottom: number;
88
+ if (textFit === 'height' || textFit === 'both') {
89
+ top = offsetY + text.top - padTop;
90
+ bottom = offsetY + text.bottom + padBottom;
91
+ } else {
92
+ top = offsetY + (text.top + text.bottom - height) / 2;
93
+ bottom = top + height;
94
+ }
95
+ const {content, pixelRatio} = image;
96
+ const collisionPadding: FittedIcon['collisionPadding'] = content
97
+ ? [
98
+ content[0] / pixelRatio,
99
+ content[1] / pixelRatio,
100
+ width - content[2] / pixelRatio,
101
+ height - content[3] / pixelRatio
102
+ ]
103
+ : undefined;
104
+ return {left, top, right, bottom, collisionPadding};
105
+ }
106
+
107
+ /**
108
+ * MapLibre's `applyTextFit`: a fitted icon box widened or heightened to keep the aspect ratio of
109
+ * its image's `content`, as the image's `textFitWidth` / `textFitHeight` ask. Only for images with
110
+ * `content` and one of them.
111
+ */
112
+ export function applyTextFit(image: StretchableImage, icon: SymbolBox): SymbolBox {
113
+ let {left, top} = icon;
114
+ let width = icon.right - left;
115
+ let height = icon.bottom - top;
116
+ const content = image.content!;
117
+ const contentAspectRatio = (content[2] - content[0]) / (content[3] - content[1]);
118
+ const textFitWidth = image.textFitWidth ?? 'stretchOrShrink';
119
+ const textFitHeight = image.textFitHeight ?? 'stretchOrShrink';
120
+ // Height wins when both axes are proportional.
121
+ if (textFitHeight === 'proportional') {
122
+ if (
123
+ (textFitWidth === 'stretchOnly' && width / height < contentAspectRatio) ||
124
+ textFitWidth === 'proportional'
125
+ ) {
126
+ const newWidth = Math.ceil(height * contentAspectRatio);
127
+ left *= newWidth / width;
128
+ width = newWidth;
129
+ }
130
+ } else if (textFitWidth === 'proportional') {
131
+ if (
132
+ textFitHeight === 'stretchOnly' &&
133
+ contentAspectRatio !== 0 &&
134
+ width / height > contentAspectRatio
135
+ ) {
136
+ const newHeight = Math.ceil(width / contentAspectRatio);
137
+ top *= newHeight / height;
138
+ height = newHeight;
139
+ }
140
+ }
141
+ return {left, top, right: left + width, bottom: top + height};
142
+ }
143
+
144
+ /** Whether an image's `textFitWidth` / `textFitHeight` change its fitted box. */
145
+ function hasImageTextFit(image: StretchableImage): boolean {
146
+ return Boolean(image.content && (image.textFitWidth || image.textFitHeight));
147
+ }
148
+
149
+ /** A position in an image along one axis: image pixels in fixed zones plus in stretched zones. */
150
+ type Cut = {fixed: number; stretch: number};
151
+
152
+ function sumWithinRange(ranges: [number, number][], min: number, max: number): number {
153
+ let sum = 0;
154
+ for (const [start, end] of ranges) {
155
+ sum += Math.max(min, Math.min(max, end)) - Math.max(min, Math.min(max, start));
156
+ }
157
+ return sum;
158
+ }
159
+
160
+ /**
161
+ * The edges between the fixed and the stretched zones of one axis. MapLibre also widens the first
162
+ * and last by a pixel of the padding it puts around each image in its atlas; a sprite sheet has
163
+ * none, so here the outer edges are the image's own.
164
+ */
165
+ function stretchZonesToCuts(
166
+ zones: [number, number][],
167
+ fixedSize: number,
168
+ stretchSize: number
169
+ ): Cut[] {
170
+ const cuts: Cut[] = [{fixed: 0, stretch: 0}];
171
+ for (const [start, end] of zones) {
172
+ const last = cuts[cuts.length - 1];
173
+ cuts.push({fixed: start - last.stretch, stretch: last.stretch});
174
+ cuts.push({fixed: start - last.stretch, stretch: last.stretch + (end - start)});
175
+ }
176
+ cuts.push({fixed: fixedSize, stretch: stretchSize});
177
+ return cuts;
178
+ }
179
+
180
+ /**
181
+ * MapLibre's `getIconQuads` for an icon fitted with {@link fitIconToText}: the rectangles that draw
182
+ * it. An image without stretch zones is one rectangle, its `content` (or all of it) stretched over
183
+ * the box. An image with `stretchX` / `stretchY` is cut at the zones' edges: the stretched zones
184
+ * share what the box adds, and the fixed zones keep their size. Empty rectangles are left out.
185
+ */
186
+ export function getIconQuads(image: StretchableImage, fitted: FittedIcon): IconQuad[] {
187
+ const {width: imageWidth, height: imageHeight, pixelRatio, content} = image;
188
+ const stretchX = image.stretchX?.length ? image.stretchX : null;
189
+ const stretchY = image.stretchY?.length ? image.stretchY : null;
190
+ const zonesX = stretchX || [[0, imageWidth] as [number, number]];
191
+ const zonesY = stretchY || [[0, imageHeight] as [number, number]];
192
+ const stretchWidth = sumWithinRange(zonesX, 0, imageWidth);
193
+ const stretchHeight = sumWithinRange(zonesY, 0, imageHeight);
194
+ const fixedWidth = imageWidth - stretchWidth;
195
+ const fixedHeight = imageHeight - stretchHeight;
196
+
197
+ let stretchOffsetX = 0;
198
+ let stretchContentWidth = stretchWidth;
199
+ let stretchOffsetY = 0;
200
+ let stretchContentHeight = stretchHeight;
201
+ let fixedOffsetX = 0;
202
+ let fixedContentWidth = fixedWidth;
203
+ let fixedOffsetY = 0;
204
+ let fixedContentHeight = fixedHeight;
205
+ let icon: SymbolBox = fitted;
206
+ if (content) {
207
+ if (hasImageTextFit(image)) {
208
+ icon = applyTextFit(image, fitted);
209
+ }
210
+ stretchOffsetX = sumWithinRange(zonesX, 0, content[0]);
211
+ stretchOffsetY = sumWithinRange(zonesY, 0, content[1]);
212
+ stretchContentWidth = sumWithinRange(zonesX, content[0], content[2]);
213
+ stretchContentHeight = sumWithinRange(zonesY, content[1], content[3]);
214
+ fixedOffsetX = content[0] - stretchOffsetX;
215
+ fixedOffsetY = content[1] - stretchOffsetY;
216
+ fixedContentWidth = content[2] - content[0] - stretchContentWidth;
217
+ fixedContentHeight = content[3] - content[1] - stretchContentHeight;
218
+ }
219
+ const iconWidth = icon.right - icon.left;
220
+ const iconHeight = icon.bottom - icon.top;
221
+ // A zone of no size stretches by nothing rather than by NaN.
222
+ const getEmOffset = (offset: number, size: number, iconSize: number, iconOffset: number) =>
223
+ (size ? (offset / size) * iconSize : 0) + iconOffset;
224
+ const getPxOffset = (offset: number, size: number, stretch: number, stretchSize: number) =>
225
+ offset - (stretchSize ? (size * stretch) / stretchSize : 0);
226
+ const minScale: [number, number] = [
227
+ iconWidth ? fixedContentWidth / pixelRatio / iconWidth : 0,
228
+ iconHeight ? fixedContentHeight / pixelRatio / iconHeight : 0
229
+ ];
230
+
231
+ const makeQuad = (left: Cut, top: Cut, right: Cut, bottom: Cut): IconQuad | null => {
232
+ const x1 = left.stretch + left.fixed;
233
+ const x2 = right.stretch + right.fixed;
234
+ const y1 = top.stretch + top.fixed;
235
+ const y2 = bottom.stretch + bottom.fixed;
236
+ if (!(x2 > x1 && y2 > y1)) {
237
+ return null;
238
+ }
239
+ const emX = (cut: Cut) =>
240
+ getEmOffset(cut.stretch - stretchOffsetX, stretchContentWidth, iconWidth, icon.left);
241
+ const emY = (cut: Cut) =>
242
+ getEmOffset(cut.stretch - stretchOffsetY, stretchContentHeight, iconHeight, icon.top);
243
+ const pxX = (cut: Cut) =>
244
+ getPxOffset(cut.fixed - fixedOffsetX, fixedContentWidth, cut.stretch, stretchWidth) /
245
+ pixelRatio;
246
+ const pxY = (cut: Cut) =>
247
+ getPxOffset(cut.fixed - fixedOffsetY, fixedContentHeight, cut.stretch, stretchHeight) /
248
+ pixelRatio;
249
+ return {
250
+ image: {x: x1, y: y1, width: x2 - x1, height: y2 - y1},
251
+ em: {left: emX(left), top: emY(top), right: emX(right), bottom: emY(bottom)},
252
+ px: {left: pxX(left), top: pxY(top), right: pxX(right), bottom: pxY(bottom)},
253
+ minScale
254
+ };
255
+ };
256
+
257
+ if (!stretchX && !stretchY) {
258
+ const quad = makeQuad(
259
+ {fixed: 0, stretch: 0},
260
+ {fixed: 0, stretch: 0},
261
+ {fixed: 0, stretch: imageWidth},
262
+ {fixed: 0, stretch: imageHeight}
263
+ );
264
+ return quad ? [quad] : [];
265
+ }
266
+ const cutsX = stretchZonesToCuts(zonesX, fixedWidth, stretchWidth);
267
+ const cutsY = stretchZonesToCuts(zonesY, fixedHeight, stretchHeight);
268
+ const quads: IconQuad[] = [];
269
+ for (let xi = 0; xi < cutsX.length - 1; xi++) {
270
+ for (let yi = 0; yi < cutsY.length - 1; yi++) {
271
+ const quad = makeQuad(cutsX[xi], cutsY[yi], cutsX[xi + 1], cutsY[yi + 1]);
272
+ if (quad) {
273
+ quads.push(quad);
274
+ }
275
+ }
276
+ }
277
+ return quads;
278
+ }
279
+
280
+ /** Where a quad is drawn at `icon-size`, as MapLibre's icon shader places its corners. */
281
+ export function getIconQuadBox(quad: IconQuad, iconSize: number): SymbolBox {
282
+ const scaleX = Math.max(quad.minScale[0], iconSize);
283
+ const scaleY = Math.max(quad.minScale[1], iconSize);
284
+ return {
285
+ left: quad.em.left * scaleX + quad.px.left,
286
+ top: quad.em.top * scaleY + quad.px.top,
287
+ right: quad.em.right * scaleX + quad.px.right,
288
+ bottom: quad.em.bottom * scaleY + quad.px.bottom
289
+ };
290
+ }
291
+
292
+ /**
293
+ * The collision box of a fitted icon, `[x1, y1, x2, y2]`, as MapLibre's `CollisionFeature` makes
294
+ * it: the fitted box at `icon-size`, padded by `icon-padding` (`[top, right, bottom, left]`), and
295
+ * widened by the image outside its `content`.
296
+ */
297
+ export function getFittedIconCollisionBox(
298
+ image: StretchableImage,
299
+ fitted: FittedIcon,
300
+ iconSize: number,
301
+ padding: readonly number[]
302
+ ): [number, number, number, number] {
303
+ const box = hasImageTextFit(image) ? applyTextFit(image, fitted) : fitted;
304
+ const [padTop, padRight, padBottom, padLeft] = padding;
305
+ const [outLeft, outTop, outRight, outBottom] = fitted.collisionPadding || [0, 0, 0, 0];
306
+ return [
307
+ box.left * iconSize - padLeft - outLeft * iconSize,
308
+ box.top * iconSize - padTop - outTop * iconSize,
309
+ box.right * iconSize + padRight + outRight * iconSize,
310
+ box.bottom * iconSize + padBottom + outBottom * iconSize
311
+ ];
312
+ }
313
+
314
+ /**
315
+ * Whether a sprite image's stretch metadata is valid, as MapLibre's image manager checks it: zones
316
+ * in order, each inside the image, and `content` inside the image with its corners in order.
317
+ * MapLibre does not draw an image that fails.
318
+ */
319
+ export function isValidStretchMetadata(image: StretchableImage): boolean {
320
+ const isValidZones = (zones: unknown, size: number) => {
321
+ if (zones === undefined) {
322
+ return true;
323
+ }
324
+ if (!Array.isArray(zones)) {
325
+ return false;
326
+ }
327
+ let last = 0;
328
+ for (const zone of zones) {
329
+ if (
330
+ !Array.isArray(zone) ||
331
+ zone.length !== 2 ||
332
+ !zone.every(Number.isFinite) ||
333
+ zone[0] < last ||
334
+ zone[1] < zone[0] ||
335
+ size < zone[1]
336
+ ) {
337
+ return false;
338
+ }
339
+ last = zone[1];
340
+ }
341
+ return true;
342
+ };
343
+ const {content, width, height} = image;
344
+ const isValidContent =
345
+ content === undefined ||
346
+ (Array.isArray(content) &&
347
+ content.length === 4 &&
348
+ content.every(Number.isFinite) &&
349
+ content[0] >= 0 &&
350
+ content[0] <= width &&
351
+ content[1] >= 0 &&
352
+ content[1] <= height &&
353
+ content[2] >= content[0] &&
354
+ content[2] <= width &&
355
+ content[3] >= content[1] &&
356
+ content[3] <= height);
357
+ return isValidZones(image.stretchX, width) && isValidZones(image.stretchY, height) && isValidContent;
358
+ }