@egret/runtime 0.0.0-stage → 7.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (157) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +21 -0
  3. package/README.md +70 -2
  4. package/README.zh-CN.md +71 -0
  5. package/dist/AssetLease.d.ts +23 -0
  6. package/dist/AssetLease.js +69 -0
  7. package/dist/AssetManager.d.ts +58 -0
  8. package/dist/AssetManager.js +451 -0
  9. package/dist/AssetRef.d.ts +21 -0
  10. package/dist/AssetRef.js +33 -0
  11. package/dist/Bitmap.d.ts +40 -0
  12. package/dist/Bitmap.js +545 -0
  13. package/dist/ByteReader.d.ts +32 -0
  14. package/dist/ByteReader.js +178 -0
  15. package/dist/ByteWriter.d.ts +30 -0
  16. package/dist/ByteWriter.js +181 -0
  17. package/dist/CancellationController.d.ts +10 -0
  18. package/dist/CancellationController.js +66 -0
  19. package/dist/DisplayObject.d.ts +97 -0
  20. package/dist/DisplayObject.js +366 -0
  21. package/dist/DisplayObjectContainer.d.ts +28 -0
  22. package/dist/DisplayObjectContainer.js +226 -0
  23. package/dist/EgretError.d.ts +12 -0
  24. package/dist/EgretError.js +36 -0
  25. package/dist/Event.d.ts +47 -0
  26. package/dist/Event.js +84 -0
  27. package/dist/EventDispatcher.d.ts +24 -0
  28. package/dist/EventDispatcher.js +178 -0
  29. package/dist/Graphics.d.ts +117 -0
  30. package/dist/Graphics.js +548 -0
  31. package/dist/Mesh2D.d.ts +27 -0
  32. package/dist/Mesh2D.js +200 -0
  33. package/dist/NineSliceBitmap.d.ts +24 -0
  34. package/dist/NineSliceBitmap.js +164 -0
  35. package/dist/NineSliceGeometry2D.d.ts +19 -0
  36. package/dist/NineSliceGeometry2D.js +126 -0
  37. package/dist/Scope.d.ts +19 -0
  38. package/dist/Scope.js +133 -0
  39. package/dist/SequencePlayer.d.ts +12 -0
  40. package/dist/SequencePlayer.js +69 -0
  41. package/dist/Sprite.d.ts +8 -0
  42. package/dist/Sprite.js +12 -0
  43. package/dist/Stage.d.ts +14 -0
  44. package/dist/Stage.js +31 -0
  45. package/dist/Texture.d.ts +30 -0
  46. package/dist/Texture.js +178 -0
  47. package/dist/assetManagerOwnerState.d.ts +36 -0
  48. package/dist/assetManagerOwnerState.js +115 -0
  49. package/dist/assetState.d.ts +45 -0
  50. package/dist/assetState.js +2 -0
  51. package/dist/atlasFrameLayout.d.ts +21 -0
  52. package/dist/atlasFrameLayout.js +48 -0
  53. package/dist/bitmapDisplayState.d.ts +24 -0
  54. package/dist/bitmapDisplayState.js +22 -0
  55. package/dist/coordinateInverse.d.ts +4 -0
  56. package/dist/coordinateInverse.js +107 -0
  57. package/dist/diagnostics.d.ts +4 -0
  58. package/dist/diagnostics.js +13 -0
  59. package/dist/displayBounds.d.ts +9 -0
  60. package/dist/displayBounds.js +181 -0
  61. package/dist/displayCoordinates.d.ts +8 -0
  62. package/dist/displayCoordinates.js +81 -0
  63. package/dist/displayHitState.d.ts +18 -0
  64. package/dist/displayHitState.js +97 -0
  65. package/dist/displayHitTest.d.ts +8 -0
  66. package/dist/displayHitTest.js +219 -0
  67. package/dist/displayInputOperations.d.ts +12 -0
  68. package/dist/displayInputOperations.js +71 -0
  69. package/dist/displayTransform.d.ts +5 -0
  70. package/dist/displayTransform.js +38 -0
  71. package/dist/displayTreeState.d.ts +90 -0
  72. package/dist/displayTreeState.js +450 -0
  73. package/dist/displayVisualState.d.ts +76 -0
  74. package/dist/displayVisualState.js +141 -0
  75. package/dist/errorHelp.d.ts +15 -0
  76. package/dist/errorHelp.js +435 -0
  77. package/dist/errorHelpZh.d.ts +5 -0
  78. package/dist/errorHelpZh.js +342 -0
  79. package/dist/geometry.d.ts +173 -0
  80. package/dist/geometry.js +311 -0
  81. package/dist/graphicsFill.d.ts +37 -0
  82. package/dist/graphicsFill.js +302 -0
  83. package/dist/graphicsShape.d.ts +170 -0
  84. package/dist/graphicsShape.js +961 -0
  85. package/dist/index.d.ts +53 -0
  86. package/dist/index.js +31 -0
  87. package/dist/input/PointerRouter.d.ts +4 -0
  88. package/dist/input/PointerRouter.js +453 -0
  89. package/dist/input/index.d.ts +3 -0
  90. package/dist/input/index.js +3 -0
  91. package/dist/input/types.d.ts +64 -0
  92. package/dist/input/types.js +3 -0
  93. package/dist/meshCaptureSnapshot.d.ts +5 -0
  94. package/dist/meshCaptureSnapshot.js +13 -0
  95. package/dist/meshDisplayState.d.ts +18 -0
  96. package/dist/meshDisplayState.js +21 -0
  97. package/dist/ownership.d.ts +75 -0
  98. package/dist/ownership.js +157 -0
  99. package/dist/paragraph/Paragraph.d.ts +13 -0
  100. package/dist/paragraph/Paragraph.js +193 -0
  101. package/dist/paragraph/ParagraphCache.d.ts +5 -0
  102. package/dist/paragraph/ParagraphCache.js +354 -0
  103. package/dist/paragraph/geometry.d.ts +4 -0
  104. package/dist/paragraph/geometry.js +180 -0
  105. package/dist/paragraph/index.d.ts +3 -0
  106. package/dist/paragraph/index.js +3 -0
  107. package/dist/paragraph/request.d.ts +6 -0
  108. package/dist/paragraph/request.js +191 -0
  109. package/dist/paragraph/types.d.ts +183 -0
  110. package/dist/paragraph/types.js +2 -0
  111. package/dist/preparedDisplaySubtree.d.ts +7 -0
  112. package/dist/preparedDisplaySubtree.js +205 -0
  113. package/dist/renderEffect.d.ts +57 -0
  114. package/dist/renderEffect.js +109 -0
  115. package/dist/sequenceClip.d.ts +48 -0
  116. package/dist/sequenceClip.js +197 -0
  117. package/dist/sharedFrameCapture.d.ts +24 -0
  118. package/dist/sharedFrameCapture.js +1128 -0
  119. package/dist/spriteSheet.d.ts +34 -0
  120. package/dist/spriteSheet.js +321 -0
  121. package/dist/srgbBytes.d.ts +3 -0
  122. package/dist/srgbBytes.js +64 -0
  123. package/dist/text/TextField.d.ts +13 -0
  124. package/dist/text/TextField.js +261 -0
  125. package/dist/text/TextRasterCache.d.ts +5 -0
  126. package/dist/text/TextRasterCache.js +1191 -0
  127. package/dist/text/budget.d.ts +51 -0
  128. package/dist/text/budget.js +116 -0
  129. package/dist/text/index.d.ts +4 -0
  130. package/dist/text/index.js +3 -0
  131. package/dist/text/plan.d.ts +16 -0
  132. package/dist/text/plan.js +238 -0
  133. package/dist/text/request.d.ts +44 -0
  134. package/dist/text/request.js +303 -0
  135. package/dist/text/types.d.ts +179 -0
  136. package/dist/text/types.js +2 -0
  137. package/dist/textDisplayState.d.ts +21 -0
  138. package/dist/textDisplayState.js +27 -0
  139. package/dist/ui/displayCommit.d.ts +43 -0
  140. package/dist/ui/displayCommit.js +384 -0
  141. package/dist/ui/editing.d.ts +3 -0
  142. package/dist/ui/editing.js +3 -0
  143. package/dist/ui/heightIndex.d.ts +35 -0
  144. package/dist/ui/heightIndex.js +295 -0
  145. package/dist/ui/index.d.ts +6 -0
  146. package/dist/ui/index.js +6 -0
  147. package/dist/ui/layout.d.ts +9 -0
  148. package/dist/ui/layout.js +349 -0
  149. package/dist/ui/managedState.d.ts +12 -0
  150. package/dist/ui/managedState.js +44 -0
  151. package/dist/ui/textEditState.d.ts +4 -0
  152. package/dist/ui/textEditState.js +206 -0
  153. package/dist/ui/textEditTypes.d.ts +48 -0
  154. package/dist/ui/textEditTypes.js +2 -0
  155. package/dist/ui/types.d.ts +99 -0
  156. package/dist/ui/types.js +2 -0
  157. package/package.json +44 -4
@@ -0,0 +1,311 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Egret 5's Point, Rectangle and Matrix, for code that is moved over with them. Each is also a plain object of the
3
+ // shape the engine takes, `{ x, y }`, `{ x, y, width, height }` and `{ a, b, c, d, tx, ty }`, so one can be passed
4
+ // wherever such an object is asked for. Angles are in radians, as they were there.
5
+ /** A place: `x` and `y`. */
6
+ export class Point {
7
+ x;
8
+ y;
9
+ constructor(x = 0, y = 0) {
10
+ this.x = x;
11
+ this.y = y;
12
+ }
13
+ /** The distance from the origin. */
14
+ get length() {
15
+ return Math.hypot(this.x, this.y);
16
+ }
17
+ setTo(x, y) {
18
+ this.x = x;
19
+ this.y = y;
20
+ return this;
21
+ }
22
+ clone() {
23
+ return new Point(this.x, this.y);
24
+ }
25
+ copyFrom(source) {
26
+ this.x = source.x;
27
+ this.y = source.y;
28
+ }
29
+ equals(other) {
30
+ return this.x === other.x && this.y === other.y;
31
+ }
32
+ /** A new point that is this one moved by another. */
33
+ add(other) {
34
+ return new Point(this.x + other.x, this.y + other.y);
35
+ }
36
+ /** A new point that is this one less another. */
37
+ subtract(other) {
38
+ return new Point(this.x - other.x, this.y - other.y);
39
+ }
40
+ /** Moves it by an amount. */
41
+ offset(dx, dy) {
42
+ this.x += dx;
43
+ this.y += dy;
44
+ }
45
+ /** Scales it to a length, keeping its direction. A point at the origin stays there. */
46
+ normalize(thickness) {
47
+ const length = this.length;
48
+ if (length > 0) {
49
+ this.x *= thickness / length;
50
+ this.y *= thickness / length;
51
+ }
52
+ }
53
+ toString() {
54
+ return `(x=${this.x}, y=${this.y})`;
55
+ }
56
+ static distance(a, b) {
57
+ return Math.hypot(a.x - b.x, a.y - b.y);
58
+ }
59
+ /** The point a fraction `f` of the way from `b` to `a`: `a` at 1, `b` at 0, as Egret's. */
60
+ static interpolate(a, b, f) {
61
+ return new Point(b.x + (a.x - b.x) * f, b.y + (a.y - b.y) * f);
62
+ }
63
+ /** The point at a length and an angle in radians from the origin. */
64
+ static polar(length, angle) {
65
+ return new Point(length * Math.cos(angle), length * Math.sin(angle));
66
+ }
67
+ }
68
+ /** A rectangle: its top left corner `x`, `y`, and its `width` and `height`. */
69
+ export class Rectangle {
70
+ x;
71
+ y;
72
+ width;
73
+ height;
74
+ constructor(x = 0, y = 0, width = 0, height = 0) {
75
+ this.x = x;
76
+ this.y = y;
77
+ this.width = width;
78
+ this.height = height;
79
+ }
80
+ get left() {
81
+ return this.x;
82
+ }
83
+ /** Moves the left side, keeping the right side where it is. */
84
+ set left(value) {
85
+ this.width += this.x - value;
86
+ this.x = value;
87
+ }
88
+ get right() {
89
+ return this.x + this.width;
90
+ }
91
+ set right(value) {
92
+ this.width = value - this.x;
93
+ }
94
+ get top() {
95
+ return this.y;
96
+ }
97
+ /** Moves the top side, keeping the bottom side where it is. */
98
+ set top(value) {
99
+ this.height += this.y - value;
100
+ this.y = value;
101
+ }
102
+ get bottom() {
103
+ return this.y + this.height;
104
+ }
105
+ set bottom(value) {
106
+ this.height = value - this.y;
107
+ }
108
+ get topLeft() {
109
+ return new Point(this.left, this.top);
110
+ }
111
+ set topLeft(value) {
112
+ this.top = value.y;
113
+ this.left = value.x;
114
+ }
115
+ get bottomRight() {
116
+ return new Point(this.right, this.bottom);
117
+ }
118
+ set bottomRight(value) {
119
+ this.bottom = value.y;
120
+ this.right = value.x;
121
+ }
122
+ setTo(x, y, width, height) {
123
+ this.x = x;
124
+ this.y = y;
125
+ this.width = width;
126
+ this.height = height;
127
+ return this;
128
+ }
129
+ clone() {
130
+ return new Rectangle(this.x, this.y, this.width, this.height);
131
+ }
132
+ copyFrom(source) {
133
+ this.setTo(source.x, source.y, source.width, source.height);
134
+ }
135
+ equals(other) {
136
+ return this.x === other.x && this.y === other.y && this.width === other.width && this.height === other.height;
137
+ }
138
+ /** Whether it has no area. */
139
+ isEmpty() {
140
+ return this.width <= 0 || this.height <= 0;
141
+ }
142
+ setEmpty() {
143
+ this.setTo(0, 0, 0, 0);
144
+ }
145
+ /** Whether a point is inside it: its left and top sides count, its right and bottom do not. */
146
+ contains(x, y) {
147
+ return x >= this.x && x < this.x + this.width && y >= this.y && y < this.y + this.height;
148
+ }
149
+ containsPoint(point) {
150
+ return this.contains(point.x, point.y);
151
+ }
152
+ /** Whether another rectangle lies wholly inside it. An empty one lies nowhere. */
153
+ containsRect(other) {
154
+ if (other.width <= 0 || other.height <= 0)
155
+ return false;
156
+ return (other.x >= this.x &&
157
+ other.y >= this.y &&
158
+ other.x + other.width <= this.x + this.width &&
159
+ other.y + other.height <= this.y + this.height);
160
+ }
161
+ /** Whether the two share some area. */
162
+ intersects(other) {
163
+ return !this.intersection(other).isEmpty();
164
+ }
165
+ /** The area the two share, or an empty rectangle. */
166
+ intersection(other) {
167
+ const left = Math.max(this.x, other.x), top = Math.max(this.y, other.y), right = Math.min(this.x + this.width, other.x + other.width), bottom = Math.min(this.y + this.height, other.y + other.height);
168
+ return right <= left || bottom <= top ? new Rectangle() : new Rectangle(left, top, right - left, bottom - top);
169
+ }
170
+ /** The least rectangle holding both. An empty one adds nothing. */
171
+ union(other) {
172
+ if (this.isEmpty())
173
+ return new Rectangle(other.x, other.y, other.width, other.height);
174
+ if (other.width <= 0 || other.height <= 0)
175
+ return this.clone();
176
+ const left = Math.min(this.x, other.x), top = Math.min(this.y, other.y);
177
+ return new Rectangle(left, top, Math.max(this.x + this.width, other.x + other.width) - left, Math.max(this.y + this.height, other.y + other.height) - top);
178
+ }
179
+ /** Grows it by `dx` on the left and on the right, and `dy` at the top and at the bottom. */
180
+ inflate(dx, dy) {
181
+ this.x -= dx;
182
+ this.width += 2 * dx;
183
+ this.y -= dy;
184
+ this.height += 2 * dy;
185
+ }
186
+ inflatePoint(point) {
187
+ this.inflate(point.x, point.y);
188
+ }
189
+ offset(dx, dy) {
190
+ this.x += dx;
191
+ this.y += dy;
192
+ }
193
+ offsetPoint(point) {
194
+ this.offset(point.x, point.y);
195
+ }
196
+ toString() {
197
+ return `(x=${this.x}, y=${this.y}, width=${this.width}, height=${this.height})`;
198
+ }
199
+ }
200
+ /**
201
+ * A matrix of the plane, as Egret's and a canvas's: a point (x, y) goes to (a·x + c·y + tx, b·x + d·y + ty).
202
+ * `concat`, `rotate`, `scale` and `translate` add a change after the ones it has; `prepend` adds one before them.
203
+ */
204
+ export class Matrix {
205
+ a;
206
+ b;
207
+ c;
208
+ d;
209
+ tx;
210
+ ty;
211
+ constructor(a = 1, b = 0, c = 0, d = 1, tx = 0, ty = 0) {
212
+ this.a = a;
213
+ this.b = b;
214
+ this.c = c;
215
+ this.d = d;
216
+ this.tx = tx;
217
+ this.ty = ty;
218
+ }
219
+ setTo(a, b, c, d, tx, ty) {
220
+ this.a = a;
221
+ this.b = b;
222
+ this.c = c;
223
+ this.d = d;
224
+ this.tx = tx;
225
+ this.ty = ty;
226
+ return this;
227
+ }
228
+ clone() {
229
+ return new Matrix(this.a, this.b, this.c, this.d, this.tx, this.ty);
230
+ }
231
+ copyFrom(source) {
232
+ return this.setTo(source.a, source.b, source.c, source.d, source.tx, source.ty);
233
+ }
234
+ equals(other) {
235
+ return (this.a === other.a &&
236
+ this.b === other.b &&
237
+ this.c === other.c &&
238
+ this.d === other.d &&
239
+ this.tx === other.tx &&
240
+ this.ty === other.ty);
241
+ }
242
+ identity() {
243
+ this.setTo(1, 0, 0, 1, 0, 0);
244
+ }
245
+ /** Adds another matrix's change after this one's. */
246
+ concat(other) {
247
+ this.setTo(this.a * other.a + this.b * other.c, this.a * other.b + this.b * other.d, this.c * other.a + this.d * other.c, this.c * other.b + this.d * other.d, this.tx * other.a + this.ty * other.c + other.tx, this.tx * other.b + this.ty * other.d + other.ty);
248
+ }
249
+ /** The same as `concat`. */
250
+ append(a, b, c, d, tx, ty) {
251
+ this.concat({ a, b, c, d, tx, ty });
252
+ return this;
253
+ }
254
+ /** Adds a change before this one's. */
255
+ prepend(a, b, c, d, tx, ty) {
256
+ const m = { a: this.a, b: this.b, c: this.c, d: this.d, tx: this.tx, ty: this.ty };
257
+ this.setTo(a, b, c, d, tx, ty);
258
+ this.concat(m);
259
+ return this;
260
+ }
261
+ /** Turns it into its inverse. One with no inverse becomes what Egret made of it: no scale, and its move reversed. */
262
+ invert() {
263
+ const det = this.a * this.d - this.b * this.c;
264
+ if (det === 0 || !Number.isFinite(det)) {
265
+ this.setTo(0, 0, 0, 0, -this.tx, -this.ty);
266
+ return;
267
+ }
268
+ const { a, b, c, d, tx, ty } = this;
269
+ this.setTo(d / det, -b / det, -c / det, a / det, (c * ty - d * tx) / det, (b * tx - a * ty) / det);
270
+ }
271
+ /** Adds a turn by an angle in radians. */
272
+ rotate(angle) {
273
+ if (angle === 0)
274
+ return;
275
+ const cos = Math.cos(angle), sin = Math.sin(angle);
276
+ this.concat({ a: cos, b: sin, c: -sin, d: cos, tx: 0, ty: 0 });
277
+ }
278
+ scale(sx, sy) {
279
+ this.concat({ a: sx, b: 0, c: 0, d: sy, tx: 0, ty: 0 });
280
+ }
281
+ translate(dx, dy) {
282
+ this.tx += dx;
283
+ this.ty += dy;
284
+ }
285
+ /** Where a point goes. */
286
+ transformPoint(x, y, result) {
287
+ const out = result ?? new Point();
288
+ return out.setTo(this.a * x + this.c * y + this.tx, this.b * x + this.d * y + this.ty);
289
+ }
290
+ /** Where a direction goes: the change without its move. */
291
+ deltaTransformPoint(point) {
292
+ return new Point(this.a * point.x + this.c * point.y, this.b * point.x + this.d * point.y);
293
+ }
294
+ /** Sets it to scale, then turn by an angle in radians, then move. */
295
+ createBox(scaleX, scaleY, rotation = 0, tx = 0, ty = 0) {
296
+ const cos = Math.cos(rotation), sin = Math.sin(rotation);
297
+ // Adding 0 makes a zero that came out negative an ordinary one.
298
+ this.setTo(scaleX * cos + 0, scaleX * sin + 0, -scaleY * sin + 0, scaleY * cos + 0, tx, ty);
299
+ }
300
+ /**
301
+ * Sets it to the matrix `beginGradientFill` takes for a box of a width and height at `tx`, `ty`, turned by an
302
+ * angle in radians: the gradient's own square of 1638.4 units fitted to the box.
303
+ */
304
+ createGradientBox(width, height, rotation = 0, tx = 0, ty = 0) {
305
+ this.createBox(width / 1638.4, height / 1638.4, rotation, tx + width / 2, ty + height / 2);
306
+ }
307
+ toString() {
308
+ return `(a=${this.a}, b=${this.b}, c=${this.c}, d=${this.d}, tx=${this.tx}, ty=${this.ty})`;
309
+ }
310
+ }
311
+ //# sourceMappingURL=geometry.js.map
@@ -0,0 +1,37 @@
1
+ export interface GradientStop {
2
+ readonly offset: number;
3
+ readonly color: number;
4
+ readonly alpha: number;
5
+ }
6
+ /**
7
+ * A gradient as the map from a node's units to its own: `u = a·x + c·y + tx` and `v = b·x + d·y + ty`. A linear gradient
8
+ * runs from u = 0 to u = 1; a radial one from the point u = v = 0 to the circle of radius 1.
9
+ */
10
+ export interface ShapeGradient {
11
+ readonly radial: boolean;
12
+ readonly a: number;
13
+ readonly b: number;
14
+ readonly c: number;
15
+ readonly d: number;
16
+ readonly tx: number;
17
+ readonly ty: number;
18
+ readonly stops: readonly GradientStop[];
19
+ }
20
+ /** The linear-light red, green and blue and the alpha of a gradient at a point. Stops are mixed as sRGB values, as a canvas mixes them. */
21
+ export declare function gradientColor(gradient: ShapeGradient, x: number, y: number, out: number[], at?: number): void;
22
+ /** Whether an outline, taken as closed, has two sides that cross. */
23
+ export declare function outlineCrosses(points: readonly number[]): boolean;
24
+ /**
25
+ * The triangles of what closed outlines cover by the non-zero rule, as x, y of each corner, three corners to a
26
+ * triangle; undefined when there would be more than `maxTriangles`. The plane is cut into bands at every corner and
27
+ * crossing; in a band no two sides cross, so what is inside lies between pairs of sides. Triangles that meet along a
28
+ * band's edge share every corner on it, so no seam opens between bands under any transform.
29
+ */
30
+ export declare function fillNonZero(outlines: readonly (readonly number[])[], maxTriangles: number): number[] | undefined;
31
+ /**
32
+ * Cuts triangles until the colours of their corners, mixed across each side, stay within the tolerance of the gradient.
33
+ * A side's test depends on its endpoints. The triangle budget can still stop adjacent subdivisions at different depths.
34
+ * When supplied, boundary masks are replaced in the output order. Only the children of an original boundary inherit it.
35
+ */
36
+ export declare function refineForGradient(triangles: readonly number[], gradient: ShapeGradient, maxTriangles: number, boundary?: number[]): number[];
37
+ //# sourceMappingURL=graphicsFill.d.ts.map
@@ -0,0 +1,302 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ // Filling for Graphics beyond one simple outline: the non-zero rule over any number of outlines, so that holes and
3
+ // outlines that cross themselves are filled as a canvas fills them, and gradients, which are given as vertex colours.
4
+ import { decodeSRGBByte } from './srgbBytes.js';
5
+ /**
6
+ * The linear-light value of an sRGB value from 0 to 255 that need not be whole: between the two bytes it lies between,
7
+ * read from the written table. A power here would differ in its last bit from one engine of JavaScript to another.
8
+ */
9
+ function toLinear(value) {
10
+ const low = Math.floor(value);
11
+ if (low >= 255)
12
+ return decodeSRGBByte(255);
13
+ if (low < 0)
14
+ return decodeSRGBByte(0);
15
+ const from = decodeSRGBByte(low);
16
+ return from + (decodeSRGBByte(low + 1) - from) * (value - low);
17
+ }
18
+ /** The linear-light red, green and blue and the alpha of a gradient at a point. Stops are mixed as sRGB values, as a canvas mixes them. */
19
+ export function gradientColor(gradient, x, y, out, at = 0) {
20
+ const u = gradient.a * x + gradient.c * y + gradient.tx;
21
+ let t = gradient.radial ? Math.hypot(u, gradient.b * x + gradient.d * y + gradient.ty) : u;
22
+ const stops = gradient.stops, last = stops.length - 1;
23
+ if (!(t > 0))
24
+ t = 0;
25
+ else if (t > 1)
26
+ t = 1;
27
+ let i = 0;
28
+ while (i < last && stops[i + 1].offset <= t)
29
+ i++;
30
+ const from = stops[i];
31
+ let color = from.color, alpha = from.alpha, r = (color >> 16) & 255, g = (color >> 8) & 255, b = color & 255;
32
+ if (i < last && t > from.offset) {
33
+ const to = stops[i + 1], f = (t - from.offset) / (to.offset - from.offset);
34
+ color = to.color;
35
+ r += (((color >> 16) & 255) - r) * f;
36
+ g += (((color >> 8) & 255) - g) * f;
37
+ b += ((color & 255) - b) * f;
38
+ alpha += (to.alpha - alpha) * f;
39
+ }
40
+ out[at] = toLinear(r);
41
+ out[at + 1] = toLinear(g);
42
+ out[at + 2] = toLinear(b);
43
+ out[at + 3] = alpha;
44
+ }
45
+ /** Whether an outline, taken as closed, has two sides that cross. */
46
+ export function outlineCrosses(points) {
47
+ const count = points.length / 2;
48
+ for (let i = 0; i < count; i++) {
49
+ const ax = points[2 * i], ay = points[2 * i + 1], bx = points[2 * ((i + 1) % count)], by = points[2 * ((i + 1) % count) + 1];
50
+ const left = Math.min(ax, bx), right = Math.max(ax, bx), top = Math.min(ay, by), bottom = Math.max(ay, by);
51
+ for (let j = i + 2; j < count; j++) {
52
+ if (i === 0 && j === count - 1)
53
+ continue;
54
+ const cx = points[2 * j], cy = points[2 * j + 1], dx = points[2 * ((j + 1) % count)], dy = points[2 * ((j + 1) % count) + 1];
55
+ if (Math.max(cx, dx) < left || Math.min(cx, dx) > right || Math.max(cy, dy) < top || Math.min(cy, dy) > bottom)
56
+ continue;
57
+ const d1 = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax), d2 = (bx - ax) * (dy - ay) - (by - ay) * (dx - ax);
58
+ const d3 = (dx - cx) * (ay - cy) - (dy - cy) * (ax - cx), d4 = (dx - cx) * (by - cy) - (dy - cy) * (bx - cx);
59
+ if (((d1 > 0 && d2 < 0) || (d1 < 0 && d2 > 0)) && ((d3 > 0 && d4 < 0) || (d3 < 0 && d4 > 0)))
60
+ return true;
61
+ // A corner that lies on another side, or sides that run along each other, is left to the non-zero fill too.
62
+ if (d1 === 0 || d2 === 0 || d3 === 0 || d4 === 0)
63
+ return true;
64
+ }
65
+ }
66
+ return false;
67
+ }
68
+ /**
69
+ * The triangles of what closed outlines cover by the non-zero rule, as x, y of each corner, three corners to a
70
+ * triangle; undefined when there would be more than `maxTriangles`. The plane is cut into bands at every corner and
71
+ * crossing; in a band no two sides cross, so what is inside lies between pairs of sides. Triangles that meet along a
72
+ * band's edge share every corner on it, so no seam opens between bands under any transform.
73
+ */
74
+ export function fillNonZero(outlines, maxTriangles) {
75
+ const x0 = [], y0 = [], x1 = [], y1 = [], turn = [];
76
+ for (const points of outlines) {
77
+ const count = points.length / 2;
78
+ if (count < 3)
79
+ continue;
80
+ for (let i = 0; i < count; i++) {
81
+ const ax = points[2 * i], ay = points[2 * i + 1], j = (i + 1) % count, bx = points[2 * j], by = points[2 * j + 1];
82
+ if (ay === by)
83
+ continue;
84
+ if (ay < by) {
85
+ x0.push(ax);
86
+ y0.push(ay);
87
+ x1.push(bx);
88
+ y1.push(by);
89
+ turn.push(1);
90
+ }
91
+ else {
92
+ x0.push(bx);
93
+ y0.push(by);
94
+ x1.push(ax);
95
+ y1.push(ay);
96
+ turn.push(-1);
97
+ }
98
+ }
99
+ }
100
+ const edges = x0.length;
101
+ if (edges === 0)
102
+ return [];
103
+ const order = [];
104
+ for (let i = 0; i < edges; i++)
105
+ order.push(i);
106
+ order.sort((p, q) => y0[p] - y0[q]);
107
+ const levels = new Set();
108
+ for (let i = 0; i < edges; i++) {
109
+ levels.add(y0[i]);
110
+ levels.add(y1[i]);
111
+ }
112
+ for (let p = 0; p < edges; p++) {
113
+ const i = order[p], bottom = y1[i], si = (x1[i] - x0[i]) / (bottom - y0[i]), li = Math.min(x0[i], x1[i]), ri = Math.max(x0[i], x1[i]);
114
+ for (let q = p + 1; q < edges; q++) {
115
+ const j = order[q];
116
+ if (y0[j] >= bottom)
117
+ break;
118
+ if (Math.max(x0[j], x1[j]) < li || Math.min(x0[j], x1[j]) > ri)
119
+ continue;
120
+ const sj = (x1[j] - x0[j]) / (y1[j] - y0[j]);
121
+ if (si === sj)
122
+ continue;
123
+ const y = (x0[j] - x0[i] + y0[i] * si - y0[j] * sj) / (si - sj);
124
+ if (y > y0[j] && y < bottom && y < y1[j])
125
+ levels.add(y);
126
+ }
127
+ }
128
+ // Levels closer than stored coordinates can tell apart are one level, and every side ends on a level, so that no
129
+ // band is thinner than a stored coordinate can say.
130
+ let reach = 1;
131
+ for (let i = 0; i < edges; i++)
132
+ reach = Math.max(reach, Math.abs(y0[i]), Math.abs(y1[i]), Math.abs(x0[i]), Math.abs(x1[i]));
133
+ const ys = [];
134
+ for (const y of [...levels].sort((p, q) => p - q))
135
+ if (ys.length === 0 || y - ys[ys.length - 1] > reach * 1e-6)
136
+ ys.push(y);
137
+ const level = (y) => {
138
+ let low = 0, high = ys.length - 1;
139
+ while (low < high) {
140
+ const middle = (low + high + 1) >> 1;
141
+ if (ys[middle] <= y)
142
+ low = middle;
143
+ else
144
+ high = middle - 1;
145
+ }
146
+ return ys[low];
147
+ };
148
+ for (let i = 0; i < edges; i++) {
149
+ y0[i] = level(y0[i]);
150
+ y1[i] = level(y1[i]);
151
+ }
152
+ const bands = ys.length - 1;
153
+ const at = (edge, y) => y === y0[edge]
154
+ ? x0[edge]
155
+ : y === y1[edge]
156
+ ? x1[edge]
157
+ : x0[edge] + ((y - y0[edge]) * (x1[edge] - x0[edge])) / (y1[edge] - y0[edge]);
158
+ // For each band, the spans inside it: left and right at its top, then left and right at its bottom.
159
+ const spans = [], corners = [];
160
+ for (let k = 0; k <= bands; k++)
161
+ corners.push([]);
162
+ let active = [], next = 0, total = 0;
163
+ for (let k = 0; k < bands; k++) {
164
+ const top = ys[k], bottom = ys[k + 1];
165
+ active = active.filter(edge => y1[edge] > top);
166
+ while (next < edges && y0[order[next]] <= top) {
167
+ if (y1[order[next]] > top)
168
+ active.push(order[next]);
169
+ next++;
170
+ }
171
+ const here = active
172
+ .map(edge => {
173
+ const xt = at(edge, top), xb = at(edge, bottom);
174
+ return { edge, xt, xb, middle: (xt + xb) / 2 };
175
+ })
176
+ .sort((p, q) => p.middle - q.middle || p.xb - q.xb);
177
+ const band = [];
178
+ let winding = 0, left = here[0];
179
+ for (const side of here) {
180
+ const before = winding;
181
+ winding += turn[side.edge];
182
+ if (before === 0 && winding !== 0)
183
+ left = side;
184
+ else if (before !== 0 && winding === 0 && (left.xt !== side.xt || left.xb !== side.xb)) {
185
+ band.push(left.xt, side.xt, left.xb, side.xb);
186
+ corners[k].push(left.xt, side.xt);
187
+ corners[k + 1].push(left.xb, side.xb);
188
+ }
189
+ }
190
+ spans.push(band);
191
+ total += band.length / 2;
192
+ }
193
+ if (total > 2 * maxTriangles)
194
+ return undefined;
195
+ for (const line of corners)
196
+ line.sort((p, q) => p - q);
197
+ const out = [], topRow = [], bottomRow = [];
198
+ const row = (line, from, to, into) => {
199
+ into.length = 0;
200
+ into.push(from);
201
+ for (const x of line)
202
+ if (x > from && x < to && x !== into[into.length - 1])
203
+ into.push(x);
204
+ if (to !== from)
205
+ into.push(to);
206
+ };
207
+ for (let k = 0; k < bands; k++) {
208
+ const band = spans[k], top = ys[k], bottom = ys[k + 1];
209
+ for (let s = 0; s < band.length; s += 4) {
210
+ row(corners[k], band[s], band[s + 1], topRow);
211
+ row(corners[k + 1], band[s + 2], band[s + 3], bottomRow);
212
+ // The two rows are joined like a zip, always from the row whose next corner comes first.
213
+ for (let i = 0, j = 0; i + 1 < topRow.length || j + 1 < bottomRow.length;) {
214
+ if (i + 1 < topRow.length && (j + 1 === bottomRow.length || topRow[i + 1] <= bottomRow[j + 1])) {
215
+ out.push(topRow[i], top, topRow[i + 1], top, bottomRow[j], bottom);
216
+ i++;
217
+ }
218
+ else {
219
+ out.push(topRow[i], top, bottomRow[j + 1], bottom, bottomRow[j], bottom);
220
+ j++;
221
+ }
222
+ }
223
+ if (out.length > 6 * maxTriangles)
224
+ return undefined;
225
+ }
226
+ }
227
+ return out;
228
+ }
229
+ /** How far a colour may be from its gradient, in a measure close to what an eye tells apart: two parts in 255. */
230
+ const TOLERANCE = 2 / 255;
231
+ /**
232
+ * Cuts triangles until the colours of their corners, mixed across each side, stay within the tolerance of the gradient.
233
+ * A side's test depends on its endpoints. The triangle budget can still stop adjacent subdivisions at different depths.
234
+ * When supplied, boundary masks are replaced in the output order. Only the children of an original boundary inherit it.
235
+ */
236
+ export function refineForGradient(triangles, gradient, maxTriangles, boundary) {
237
+ const ca = [0, 0, 0, 0], cb = [0, 0, 0, 0], cm = [0, 0, 0, 0];
238
+ const cut = (ax, ay, bx, by) => {
239
+ if (ax > bx || (ax === bx && ay > by)) {
240
+ const x = ax, y = ay;
241
+ ax = bx;
242
+ ay = by;
243
+ bx = x;
244
+ by = y;
245
+ }
246
+ const dx = bx - ax, dy = by - ay;
247
+ if (dx * dx + dy * dy < 0.25)
248
+ return false;
249
+ gradientColor(gradient, ax, ay, ca);
250
+ gradientColor(gradient, bx, by, cb);
251
+ for (let step = 1; step < 4; step++) {
252
+ const f = step / 4;
253
+ gradientColor(gradient, ax + dx * f, ay + dy * f, cm);
254
+ for (let i = 0; i < 3; i++)
255
+ if (Math.abs(Math.sqrt(cm[i]) - Math.sqrt(ca[i] + (cb[i] - ca[i]) * f)) > TOLERANCE)
256
+ return true;
257
+ if (Math.abs(cm[3] - (ca[3] + (cb[3] - ca[3]) * f)) > TOLERANCE)
258
+ return true;
259
+ }
260
+ return false;
261
+ };
262
+ const stack = triangles.slice(), out = [];
263
+ const boundaries = boundary?.slice();
264
+ if (boundary !== undefined)
265
+ boundary.length = 0;
266
+ let count = triangles.length / 6;
267
+ while (stack.length !== 0) {
268
+ const p = stack.splice(stack.length - 6, 6);
269
+ const mask = boundaries?.pop() ?? 0;
270
+ const flags = count < maxTriangles
271
+ ? [cut(p[0], p[1], p[2], p[3]), cut(p[2], p[3], p[4], p[5]), cut(p[4], p[5], p[0], p[1])]
272
+ : [false, false, false];
273
+ const cuts = (flags[0] ? 1 : 0) + (flags[1] ? 1 : 0) + (flags[2] ? 1 : 0);
274
+ if (cuts === 0) {
275
+ out.push(...p);
276
+ boundary?.push(mask);
277
+ continue;
278
+ }
279
+ // Turned so that with one cut it is the first side, and with two the uncut side is the last.
280
+ const turnBy = cuts === 3 ? 0 : cuts === 1 ? flags.indexOf(true) : (flags.indexOf(false) + 1) % 3;
281
+ const ax = p[2 * turnBy], ay = p[2 * turnBy + 1], bx = p[2 * ((turnBy + 1) % 3)], by = p[2 * ((turnBy + 1) % 3) + 1], cx = p[2 * ((turnBy + 2) % 3)], cy = p[2 * ((turnBy + 2) % 3) + 1];
282
+ const abx = (ax + bx) / 2, aby = (ay + by) / 2, bcx = (bx + cx) / 2, bcy = (by + cy) / 2, cax = (cx + ax) / 2, cay = (cy + ay) / 2;
283
+ // Rotate the three bits with the corners. New interior sides never carry a boundary bit.
284
+ const turned = (mask >>> turnBy) | ((mask << (3 - turnBy)) & 7);
285
+ const ab = turned & 1, bc = turned & 2, ca = turned & 4;
286
+ if (cuts === 1) {
287
+ stack.push(ax, ay, abx, aby, cx, cy, abx, aby, bx, by, cx, cy);
288
+ boundaries?.push(ab | ca, ab | bc);
289
+ }
290
+ else if (cuts === 2) {
291
+ stack.push(abx, aby, bx, by, bcx, bcy, ax, ay, abx, aby, bcx, bcy, ax, ay, bcx, bcy, cx, cy);
292
+ boundaries?.push(ab | bc, ab, bc | ca);
293
+ }
294
+ else {
295
+ stack.push(ax, ay, abx, aby, cax, cay, abx, aby, bx, by, bcx, bcy, cax, cay, bcx, bcy, cx, cy, abx, aby, bcx, bcy, cax, cay);
296
+ boundaries?.push(ab | ca, ab | bc, bc | ca, 0);
297
+ }
298
+ count += cuts;
299
+ }
300
+ return out;
301
+ }
302
+ //# sourceMappingURL=graphicsFill.js.map