@egret/runtime 0.0.0-stage → 7.0.1-rc.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.
- package/LICENSE +201 -0
- package/NOTICE +21 -0
- package/README.md +70 -2
- package/README.zh-CN.md +71 -0
- package/dist/AssetLease.d.ts +23 -0
- package/dist/AssetLease.js +69 -0
- package/dist/AssetManager.d.ts +56 -0
- package/dist/AssetManager.js +451 -0
- package/dist/AssetRef.d.ts +20 -0
- package/dist/AssetRef.js +32 -0
- package/dist/Bitmap.d.ts +40 -0
- package/dist/Bitmap.js +545 -0
- package/dist/ByteReader.d.ts +30 -0
- package/dist/ByteReader.js +177 -0
- package/dist/ByteWriter.d.ts +29 -0
- package/dist/ByteWriter.js +180 -0
- package/dist/CancellationController.d.ts +10 -0
- package/dist/CancellationController.js +66 -0
- package/dist/DisplayObject.d.ts +89 -0
- package/dist/DisplayObject.js +340 -0
- package/dist/DisplayObjectContainer.d.ts +28 -0
- package/dist/DisplayObjectContainer.js +226 -0
- package/dist/EgretError.d.ts +11 -0
- package/dist/EgretError.js +36 -0
- package/dist/Event.d.ts +45 -0
- package/dist/Event.js +84 -0
- package/dist/EventDispatcher.d.ts +24 -0
- package/dist/EventDispatcher.js +178 -0
- package/dist/Graphics.d.ts +106 -0
- package/dist/Graphics.js +504 -0
- package/dist/Mesh2D.d.ts +27 -0
- package/dist/Mesh2D.js +200 -0
- package/dist/NineSliceBitmap.d.ts +24 -0
- package/dist/NineSliceBitmap.js +164 -0
- package/dist/NineSliceGeometry2D.d.ts +18 -0
- package/dist/NineSliceGeometry2D.js +126 -0
- package/dist/Scope.d.ts +18 -0
- package/dist/Scope.js +133 -0
- package/dist/SequencePlayer.d.ts +12 -0
- package/dist/SequencePlayer.js +69 -0
- package/dist/Sprite.d.ts +8 -0
- package/dist/Sprite.js +12 -0
- package/dist/Stage.d.ts +14 -0
- package/dist/Stage.js +31 -0
- package/dist/Texture.d.ts +29 -0
- package/dist/Texture.js +177 -0
- package/dist/assetManagerOwnerState.d.ts +36 -0
- package/dist/assetManagerOwnerState.js +115 -0
- package/dist/assetState.d.ts +44 -0
- package/dist/assetState.js +2 -0
- package/dist/atlasFrameLayout.d.ts +21 -0
- package/dist/atlasFrameLayout.js +48 -0
- package/dist/bitmapDisplayState.d.ts +24 -0
- package/dist/bitmapDisplayState.js +22 -0
- package/dist/coordinateInverse.d.ts +4 -0
- package/dist/coordinateInverse.js +107 -0
- package/dist/diagnostics.d.ts +4 -0
- package/dist/diagnostics.js +13 -0
- package/dist/displayBounds.d.ts +9 -0
- package/dist/displayBounds.js +181 -0
- package/dist/displayCoordinates.d.ts +8 -0
- package/dist/displayCoordinates.js +81 -0
- package/dist/displayHitState.d.ts +18 -0
- package/dist/displayHitState.js +97 -0
- package/dist/displayHitTest.d.ts +8 -0
- package/dist/displayHitTest.js +219 -0
- package/dist/displayInputOperations.d.ts +12 -0
- package/dist/displayInputOperations.js +71 -0
- package/dist/displayTransform.d.ts +5 -0
- package/dist/displayTransform.js +38 -0
- package/dist/displayTreeState.d.ts +90 -0
- package/dist/displayTreeState.js +450 -0
- package/dist/displayVisualState.d.ts +74 -0
- package/dist/displayVisualState.js +140 -0
- package/dist/errorHelp.d.ts +14 -0
- package/dist/errorHelp.js +432 -0
- package/dist/errorHelpZh.d.ts +5 -0
- package/dist/errorHelpZh.js +340 -0
- package/dist/graphicsFill.d.ts +37 -0
- package/dist/graphicsFill.js +302 -0
- package/dist/graphicsShape.d.ts +144 -0
- package/dist/graphicsShape.js +755 -0
- package/dist/index.d.ts +50 -0
- package/dist/index.js +28 -0
- package/dist/input/PointerRouter.d.ts +4 -0
- package/dist/input/PointerRouter.js +453 -0
- package/dist/input/index.d.ts +3 -0
- package/dist/input/index.js +3 -0
- package/dist/input/types.d.ts +58 -0
- package/dist/input/types.js +3 -0
- package/dist/meshCaptureSnapshot.d.ts +5 -0
- package/dist/meshCaptureSnapshot.js +13 -0
- package/dist/meshDisplayState.d.ts +18 -0
- package/dist/meshDisplayState.js +21 -0
- package/dist/ownership.d.ts +75 -0
- package/dist/ownership.js +157 -0
- package/dist/paragraph/Paragraph.d.ts +13 -0
- package/dist/paragraph/Paragraph.js +193 -0
- package/dist/paragraph/ParagraphCache.d.ts +5 -0
- package/dist/paragraph/ParagraphCache.js +354 -0
- package/dist/paragraph/geometry.d.ts +4 -0
- package/dist/paragraph/geometry.js +180 -0
- package/dist/paragraph/index.d.ts +3 -0
- package/dist/paragraph/index.js +3 -0
- package/dist/paragraph/request.d.ts +6 -0
- package/dist/paragraph/request.js +191 -0
- package/dist/paragraph/types.d.ts +183 -0
- package/dist/paragraph/types.js +2 -0
- package/dist/preparedDisplaySubtree.d.ts +7 -0
- package/dist/preparedDisplaySubtree.js +205 -0
- package/dist/renderEffect.d.ts +47 -0
- package/dist/renderEffect.js +87 -0
- package/dist/sequenceClip.d.ts +42 -0
- package/dist/sequenceClip.js +196 -0
- package/dist/sharedFrameCapture.d.ts +24 -0
- package/dist/sharedFrameCapture.js +1069 -0
- package/dist/spriteSheet.d.ts +34 -0
- package/dist/spriteSheet.js +321 -0
- package/dist/srgbBytes.d.ts +3 -0
- package/dist/srgbBytes.js +64 -0
- package/dist/text/TextField.d.ts +13 -0
- package/dist/text/TextField.js +261 -0
- package/dist/text/TextRasterCache.d.ts +5 -0
- package/dist/text/TextRasterCache.js +1191 -0
- package/dist/text/budget.d.ts +51 -0
- package/dist/text/budget.js +116 -0
- package/dist/text/index.d.ts +4 -0
- package/dist/text/index.js +3 -0
- package/dist/text/plan.d.ts +16 -0
- package/dist/text/plan.js +238 -0
- package/dist/text/request.d.ts +44 -0
- package/dist/text/request.js +303 -0
- package/dist/text/types.d.ts +179 -0
- package/dist/text/types.js +2 -0
- package/dist/textDisplayState.d.ts +21 -0
- package/dist/textDisplayState.js +27 -0
- package/dist/ui/displayCommit.d.ts +43 -0
- package/dist/ui/displayCommit.js +376 -0
- package/dist/ui/editing.d.ts +3 -0
- package/dist/ui/editing.js +3 -0
- package/dist/ui/heightIndex.d.ts +35 -0
- package/dist/ui/heightIndex.js +295 -0
- package/dist/ui/index.d.ts +6 -0
- package/dist/ui/index.js +6 -0
- package/dist/ui/layout.d.ts +9 -0
- package/dist/ui/layout.js +349 -0
- package/dist/ui/managedState.d.ts +12 -0
- package/dist/ui/managedState.js +44 -0
- package/dist/ui/textEditState.d.ts +4 -0
- package/dist/ui/textEditState.js +206 -0
- package/dist/ui/textEditTypes.d.ts +48 -0
- package/dist/ui/textEditTypes.js +2 -0
- package/dist/ui/types.d.ts +99 -0
- package/dist/ui/types.js +2 -0
- package/package.json +44 -4
|
@@ -0,0 +1,755 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// The shapes of a Graphics, as triangles. A node whose drawing is more than filled rectangles keeps one list of
|
|
3
|
+
// coloured triangles, in drawing order, and a path still being drawn. Capture reads them as one shared geometry.
|
|
4
|
+
import { createImageData2D, createSharedGeometry2D } from '@egret/contracts';
|
|
5
|
+
import { decodeSRGBByte } from './srgbBytes.js';
|
|
6
|
+
import { fillNonZero, gradientColor, outlineCrosses, refineForGradient } from './graphicsFill.js';
|
|
7
|
+
/** The most one node's shapes may hold: what one shared geometry takes. */
|
|
8
|
+
export const GRAPHICS_SHAPE_LIMITS = Object.freeze({
|
|
9
|
+
maxVertices: 65536,
|
|
10
|
+
maxIndices: 196608,
|
|
11
|
+
maxLogicalBytes: 4980736,
|
|
12
|
+
maxPathPoints: 8192,
|
|
13
|
+
maxLineWidth: 1024,
|
|
14
|
+
maxDashes: 16384,
|
|
15
|
+
maxGradientStops: 16,
|
|
16
|
+
maxFillTriangles: 16384,
|
|
17
|
+
maxBatches: 16,
|
|
18
|
+
maxRepeatPixels: 8192
|
|
19
|
+
});
|
|
20
|
+
/** The one white pixel every shape samples: its colour is the colour of its vertices. */
|
|
21
|
+
export const SHAPE_PAGE = createImageData2D({
|
|
22
|
+
width: 1,
|
|
23
|
+
height: 1,
|
|
24
|
+
pixels: new Uint8Array([255, 255, 255, 255])
|
|
25
|
+
});
|
|
26
|
+
export const SHAPE_REGION = Object.freeze({
|
|
27
|
+
packed: Object.freeze({ x: 0, y: 0, width: 1, height: 1 }),
|
|
28
|
+
logicalWidth: 1,
|
|
29
|
+
logicalHeight: 1,
|
|
30
|
+
trimX: 0,
|
|
31
|
+
trimY: 0,
|
|
32
|
+
quarterTurn: 0
|
|
33
|
+
});
|
|
34
|
+
export function createShapeState() {
|
|
35
|
+
return {
|
|
36
|
+
positions: [],
|
|
37
|
+
colors: [],
|
|
38
|
+
uvs: [],
|
|
39
|
+
indices: [],
|
|
40
|
+
bitmap: undefined,
|
|
41
|
+
done: [],
|
|
42
|
+
path: undefined,
|
|
43
|
+
resolved: undefined
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** A copy that can be drawn on without changing the drawing it was made from. */
|
|
47
|
+
export function copyDrawing(from) {
|
|
48
|
+
return {
|
|
49
|
+
positions: from.positions.slice(),
|
|
50
|
+
colors: from.colors.slice(),
|
|
51
|
+
uvs: from.uvs.slice(),
|
|
52
|
+
indices: from.indices.slice(),
|
|
53
|
+
bitmap: from.bitmap,
|
|
54
|
+
done: from.done.slice()
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** Makes the batch being added to one over this image, or over the white pixel: the one it is, if it is, and otherwise a new one after it. */
|
|
58
|
+
function batchFor(target, bitmap) {
|
|
59
|
+
const now = target.bitmap;
|
|
60
|
+
if (now === bitmap ||
|
|
61
|
+
(now !== undefined && bitmap !== undefined && now.lease === bitmap.lease && now.repeat === bitmap.repeat))
|
|
62
|
+
return;
|
|
63
|
+
if (target.indices.length !== 0) {
|
|
64
|
+
target.done.push({
|
|
65
|
+
positions: target.positions,
|
|
66
|
+
colors: target.colors,
|
|
67
|
+
uvs: target.uvs,
|
|
68
|
+
indices: target.indices,
|
|
69
|
+
bitmap: now
|
|
70
|
+
});
|
|
71
|
+
target.positions = [];
|
|
72
|
+
target.colors = [];
|
|
73
|
+
target.uvs = [];
|
|
74
|
+
target.indices = [];
|
|
75
|
+
}
|
|
76
|
+
else {
|
|
77
|
+
target.positions.length = 0;
|
|
78
|
+
target.colors.length = 0;
|
|
79
|
+
target.uvs.length = 0;
|
|
80
|
+
}
|
|
81
|
+
target.bitmap = bitmap;
|
|
82
|
+
}
|
|
83
|
+
function paint(target, points, color) {
|
|
84
|
+
const first = target.positions.length / 2;
|
|
85
|
+
const r = decodeSRGBByte(color.color >> 16), g = decodeSRGBByte(color.color >> 8), b = decodeSRGBByte(color.color);
|
|
86
|
+
for (let i = 0; i < points.length; i += 2) {
|
|
87
|
+
const x = points[i], y = points[i + 1], bitmap = color.bitmap;
|
|
88
|
+
target.positions.push(x, y);
|
|
89
|
+
// Over an image a vertex is white and says where in the image it is; over the white pixel it is the pixel's middle.
|
|
90
|
+
if (bitmap !== undefined) {
|
|
91
|
+
target.colors.push(1, 1, 1, 1);
|
|
92
|
+
target.uvs.push(bitmap.a * x + bitmap.c * y + bitmap.tx, bitmap.b * x + bitmap.d * y + bitmap.ty);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
target.uvs.push(0.5, 0.5);
|
|
96
|
+
if (color.gradient === undefined)
|
|
97
|
+
target.colors.push(r, g, b, color.alpha);
|
|
98
|
+
else
|
|
99
|
+
gradientColor(color.gradient, points[i], points[i + 1], target.colors, target.colors.length);
|
|
100
|
+
}
|
|
101
|
+
return first;
|
|
102
|
+
}
|
|
103
|
+
/** Twice the signed area of a polygon given as x, y pairs. */
|
|
104
|
+
function area2(points) {
|
|
105
|
+
let sum = 0;
|
|
106
|
+
for (let i = 0, n = points.length; i < n; i += 2) {
|
|
107
|
+
const j = (i + 2) % n;
|
|
108
|
+
sum += points[i] * points[j + 1] - points[j] * points[i + 1];
|
|
109
|
+
}
|
|
110
|
+
return sum;
|
|
111
|
+
}
|
|
112
|
+
/** Drops a point equal to the one before it, and a last point equal to the first. */
|
|
113
|
+
function distinct(points, closed) {
|
|
114
|
+
const out = [];
|
|
115
|
+
for (let i = 0; i < points.length; i += 2) {
|
|
116
|
+
const x = points[i], y = points[i + 1], n = out.length;
|
|
117
|
+
if (n === 0 || out[n - 2] !== x || out[n - 1] !== y)
|
|
118
|
+
out.push(x, y);
|
|
119
|
+
}
|
|
120
|
+
if (closed && out.length >= 4 && out[0] === out[out.length - 2] && out[1] === out[out.length - 1])
|
|
121
|
+
out.length -= 2;
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
/** A convex outline as a fan from its first corner. */
|
|
125
|
+
export function fillConvex(target, outline, color) {
|
|
126
|
+
const points = distinct(outline, true), count = points.length / 2;
|
|
127
|
+
if (count < 3 || area2(points) === 0)
|
|
128
|
+
return;
|
|
129
|
+
const first = paint(target, points, color);
|
|
130
|
+
for (let i = 1; i + 1 < count; i++)
|
|
131
|
+
target.indices.push(first, first + i, first + i + 1);
|
|
132
|
+
}
|
|
133
|
+
/** Any simple outline, by cutting ears. An outline that crosses itself is filled without a promise about where. */
|
|
134
|
+
export function fillPolygon(target, outline, color) {
|
|
135
|
+
const points = distinct(outline, true), count = points.length / 2, area = area2(points);
|
|
136
|
+
if (count < 3 || area === 0)
|
|
137
|
+
return;
|
|
138
|
+
const first = paint(target, points, color), sign = area > 0 ? 1 : -1;
|
|
139
|
+
const ring = [];
|
|
140
|
+
for (let i = 0; i < count; i++)
|
|
141
|
+
ring.push(i);
|
|
142
|
+
const cross = (a, b, c) => (points[2 * b] - points[2 * a]) * (points[2 * c + 1] - points[2 * a + 1]) -
|
|
143
|
+
(points[2 * b + 1] - points[2 * a + 1]) * (points[2 * c] - points[2 * a]);
|
|
144
|
+
const inside = (a, b, c, p) => sign * cross(a, b, p) >= 0 && sign * cross(b, c, p) >= 0 && sign * cross(c, a, p) >= 0;
|
|
145
|
+
let guard = 2 * count * count;
|
|
146
|
+
for (let at = 0; ring.length > 3 && guard-- > 0;) {
|
|
147
|
+
const n = ring.length, a = ring[(at + n - 1) % n], b = ring[at % n], c = ring[(at + 1) % n];
|
|
148
|
+
let ear = sign * cross(a, b, c) > 0;
|
|
149
|
+
for (let k = 0; ear && k < n; k++) {
|
|
150
|
+
const p = ring[k];
|
|
151
|
+
if (p !== a && p !== b && p !== c && inside(a, b, c, p))
|
|
152
|
+
ear = false;
|
|
153
|
+
}
|
|
154
|
+
if (ear) {
|
|
155
|
+
target.indices.push(first + a, first + b, first + c);
|
|
156
|
+
ring.splice(at % n, 1);
|
|
157
|
+
at = at % ring.length;
|
|
158
|
+
}
|
|
159
|
+
else if (sign * cross(a, b, c) === 0) {
|
|
160
|
+
ring.splice(at % n, 1);
|
|
161
|
+
at = at % ring.length;
|
|
162
|
+
}
|
|
163
|
+
else
|
|
164
|
+
at = (at + 1) % n;
|
|
165
|
+
}
|
|
166
|
+
// What no ear could be cut from is closed as a fan, so that a degenerate outline still ends.
|
|
167
|
+
for (let i = 1; i + 1 < ring.length; i++)
|
|
168
|
+
target.indices.push(first + ring[0], first + ring[i], first + ring[i + 1]);
|
|
169
|
+
}
|
|
170
|
+
/** Triangles given as x, y of each corner, added with one vertex for each different corner. */
|
|
171
|
+
function addTriangles(target, triangles, color, boundary) {
|
|
172
|
+
const known = new Map(), one = [0, 0], corner = [0, 0, 0];
|
|
173
|
+
let kept = 0;
|
|
174
|
+
for (let i = 0; i < triangles.length; i += 6) {
|
|
175
|
+
for (let k = 0; k < 3; k++) {
|
|
176
|
+
const x = triangles[i + 2 * k], y = triangles[i + 2 * k + 1], key = x + ',' + y;
|
|
177
|
+
let index = known.get(key);
|
|
178
|
+
if (index === undefined) {
|
|
179
|
+
one[0] = x;
|
|
180
|
+
one[1] = y;
|
|
181
|
+
index = paint(target, one, color);
|
|
182
|
+
known.set(key, index);
|
|
183
|
+
}
|
|
184
|
+
corner[k] = index;
|
|
185
|
+
}
|
|
186
|
+
if (corner[0] !== corner[1] && corner[1] !== corner[2] && corner[2] !== corner[0]) {
|
|
187
|
+
target.indices.push(corner[0], corner[1], corner[2]);
|
|
188
|
+
if (boundary !== undefined)
|
|
189
|
+
boundary[kept++] = boundary[i / 6];
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (boundary !== undefined)
|
|
193
|
+
boundary.length = kept;
|
|
194
|
+
}
|
|
195
|
+
/** Three side bits per original triangle. Coincident vertices share the same edge identity. */
|
|
196
|
+
function originalBoundary(target, firstIndex) {
|
|
197
|
+
const { positions, indices } = target;
|
|
198
|
+
const masks = new Array((indices.length - firstIndex) / 3).fill(0);
|
|
199
|
+
const places = new Map(), sides = new Map();
|
|
200
|
+
const same = (vertex) => {
|
|
201
|
+
const key = positions[2 * vertex] + ',' + positions[2 * vertex + 1];
|
|
202
|
+
const found = places.get(key);
|
|
203
|
+
if (found !== undefined)
|
|
204
|
+
return found;
|
|
205
|
+
places.set(key, vertex);
|
|
206
|
+
return vertex;
|
|
207
|
+
};
|
|
208
|
+
for (let at = firstIndex; at < indices.length; at += 3) {
|
|
209
|
+
const corners = [same(indices[at]), same(indices[at + 1]), same(indices[at + 2])];
|
|
210
|
+
if (corners[0] === corners[1] || corners[1] === corners[2] || corners[2] === corners[0])
|
|
211
|
+
continue;
|
|
212
|
+
for (let side = 0; side < 3; side++) {
|
|
213
|
+
const a = corners[side], b = corners[(side + 1) % 3], key = a < b ? a + ':' + b : b + ':' + a;
|
|
214
|
+
sides.set(key, sides.has(key) ? -1 : at - firstIndex + side);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
for (const side of sides.values())
|
|
218
|
+
if (side >= 0) {
|
|
219
|
+
const triangle = Math.floor(side / 3);
|
|
220
|
+
masks[triangle] = masks[triangle] | (1 << (side % 3));
|
|
221
|
+
}
|
|
222
|
+
return masks;
|
|
223
|
+
}
|
|
224
|
+
/** The triangles added to a target since a mark, taken back out of it as x, y of each corner. */
|
|
225
|
+
function takeTriangles(target, vertices, indices) {
|
|
226
|
+
const out = [];
|
|
227
|
+
for (let i = indices; i < target.indices.length; i++) {
|
|
228
|
+
const v = target.indices[i];
|
|
229
|
+
out.push(target.positions[2 * v], target.positions[2 * v + 1]);
|
|
230
|
+
}
|
|
231
|
+
target.positions.length = 2 * vertices;
|
|
232
|
+
target.colors.length = 4 * vertices;
|
|
233
|
+
target.uvs.length = 2 * vertices;
|
|
234
|
+
target.indices.length = indices;
|
|
235
|
+
return out;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* What closed outlines cover, filled. One outline that does not cross itself is cut into ears; several, or one that
|
|
239
|
+
* crosses itself, are filled by the non-zero rule, so an outline drawn the other way round inside another is a hole.
|
|
240
|
+
* A gradient is given as the colours of the corners, with triangles cut until that is close enough.
|
|
241
|
+
* False when the fill would take more triangles than one fill may.
|
|
242
|
+
*/
|
|
243
|
+
export function fillOutlines(target, outlines, convex, color, edge = true) {
|
|
244
|
+
batchFor(target, color.bitmap);
|
|
245
|
+
const vertices = target.positions.length / 2, indices = target.indices.length, max = GRAPHICS_SHAPE_LIMITS.maxFillTriangles;
|
|
246
|
+
const closed = outlines.map(points => distinct(points, true)).filter(points => points.length >= 6);
|
|
247
|
+
if (closed.length === 0)
|
|
248
|
+
return true;
|
|
249
|
+
if (closed.length === 1 && (convex || !outlineCrosses(closed[0])))
|
|
250
|
+
(convex ? fillConvex : fillPolygon)(target, closed[0], color);
|
|
251
|
+
else {
|
|
252
|
+
const triangles = fillNonZero(closed, max);
|
|
253
|
+
if (triangles === undefined)
|
|
254
|
+
return false;
|
|
255
|
+
addTriangles(target, triangles, color);
|
|
256
|
+
}
|
|
257
|
+
const bitmap = color.bitmap;
|
|
258
|
+
const feather = edge && bitmap === undefined ? (color.feather ?? 0) : 0;
|
|
259
|
+
let boundary;
|
|
260
|
+
if (color.gradient !== undefined) {
|
|
261
|
+
if (feather > 0)
|
|
262
|
+
boundary = originalBoundary(target, indices);
|
|
263
|
+
const refined = refineForGradient(takeTriangles(target, vertices, indices), color.gradient, max, boundary);
|
|
264
|
+
addTriangles(target, refined, color, boundary);
|
|
265
|
+
}
|
|
266
|
+
// An image that repeats is drawn no farther than this many of its pixels from its start: beyond it a frame cannot place it exactly.
|
|
267
|
+
if (bitmap !== undefined)
|
|
268
|
+
for (let i = 2 * vertices; i < target.uvs.length; i += 2)
|
|
269
|
+
if (Math.abs(target.uvs[i] * bitmap.width) > GRAPHICS_SHAPE_LIMITS.maxRepeatPixels ||
|
|
270
|
+
Math.abs(target.uvs[i + 1] * bitmap.height) > GRAPHICS_SHAPE_LIMITS.maxRepeatPixels)
|
|
271
|
+
return false;
|
|
272
|
+
if (feather > 0)
|
|
273
|
+
featherEdges(target, indices, feather, boundary);
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* A band around the triangles from `firstIndex` on, as wide as `width`, that fades from their colour at their edge to
|
|
278
|
+
* nothing: what smooths an edge that would otherwise be a stair of pixels. An edge of theirs is a side that only one
|
|
279
|
+
* of the triangles has; where two edges meet the band turns with them, no farther out than twice its width.
|
|
280
|
+
*/
|
|
281
|
+
function featherEdges(target, firstIndex, width, boundary) {
|
|
282
|
+
const P = target.positions, I = target.indices, last = I.length;
|
|
283
|
+
// Vertices at one place are one vertex here: the first of them.
|
|
284
|
+
const places = new Map(), same = (v) => {
|
|
285
|
+
const key = P[2 * v] + ',' + P[2 * v + 1], found = places.get(key);
|
|
286
|
+
if (found !== undefined)
|
|
287
|
+
return found;
|
|
288
|
+
places.set(key, v);
|
|
289
|
+
return v;
|
|
290
|
+
};
|
|
291
|
+
const sides = new Map();
|
|
292
|
+
for (let i = firstIndex; i + 2 < last; i += 3) {
|
|
293
|
+
const mask = boundary?.[(i - firstIndex) / 3] ?? 7;
|
|
294
|
+
if (mask === 0)
|
|
295
|
+
continue;
|
|
296
|
+
const t = [same(I[i]), same(I[i + 1]), same(I[i + 2])];
|
|
297
|
+
if (t[0] === t[1] || t[1] === t[2] || t[2] === t[0])
|
|
298
|
+
continue;
|
|
299
|
+
for (let k = 0; k < 3; k++) {
|
|
300
|
+
if ((mask & (1 << k)) === 0)
|
|
301
|
+
continue;
|
|
302
|
+
const u = t[k], v = t[(k + 1) % 3], w = t[(k + 2) % 3], key = u < v ? u + ':' + v : v + ':' + u, seen = sides.get(key);
|
|
303
|
+
if (seen !== undefined) {
|
|
304
|
+
seen.times++;
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
const dx = P[2 * v] - P[2 * u], dy = P[2 * v + 1] - P[2 * u + 1], length = Math.hypot(dx, dy);
|
|
308
|
+
// The side's normal, pointing away from the triangle's third corner.
|
|
309
|
+
let nx = dy / length, ny = -dx / length;
|
|
310
|
+
if (nx * (P[2 * w] - P[2 * u]) + ny * (P[2 * w + 1] - P[2 * u + 1]) > 0) {
|
|
311
|
+
nx = -nx;
|
|
312
|
+
ny = -ny;
|
|
313
|
+
}
|
|
314
|
+
sides.set(key, { u, v, nx, ny, times: 1 });
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const sums = new Map();
|
|
318
|
+
for (const side of sides.values()) {
|
|
319
|
+
if (side.times !== 1)
|
|
320
|
+
continue;
|
|
321
|
+
for (const at of [side.u, side.v]) {
|
|
322
|
+
const sum = sums.get(at);
|
|
323
|
+
if (sum === undefined)
|
|
324
|
+
sums.set(at, [side.nx, side.ny, side.nx, side.ny]);
|
|
325
|
+
else {
|
|
326
|
+
sum[0] += side.nx;
|
|
327
|
+
sum[1] += side.ny;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const outer = new Map();
|
|
332
|
+
for (const [at, sum] of sums) {
|
|
333
|
+
let nx = sum[0], ny = sum[1];
|
|
334
|
+
const length = Math.hypot(nx, ny);
|
|
335
|
+
if (length < 1e-6) {
|
|
336
|
+
nx = sum[2];
|
|
337
|
+
ny = sum[3];
|
|
338
|
+
}
|
|
339
|
+
else {
|
|
340
|
+
nx /= length;
|
|
341
|
+
ny /= length;
|
|
342
|
+
}
|
|
343
|
+
const reach = width / Math.max(0.5, nx * sum[2] + ny * sum[3]);
|
|
344
|
+
outer.set(at, P.length / 2);
|
|
345
|
+
P.push(P[2 * at] + nx * reach, P[2 * at + 1] + ny * reach);
|
|
346
|
+
target.colors.push(target.colors[4 * at], target.colors[4 * at + 1], target.colors[4 * at + 2], 0);
|
|
347
|
+
target.uvs.push(0.5, 0.5);
|
|
348
|
+
}
|
|
349
|
+
for (const side of sides.values()) {
|
|
350
|
+
if (side.times !== 1)
|
|
351
|
+
continue;
|
|
352
|
+
const u2 = outer.get(side.u), v2 = outer.get(side.v);
|
|
353
|
+
I.push(side.u, side.v, v2, side.u, v2, u2);
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
/** The pieces of a line that its dashes cover, each a line of its own; undefined when there are too many. */
|
|
357
|
+
function dashPieces(points, loop, dash) {
|
|
358
|
+
const pattern = dash.length % 2 === 0 ? dash : [...dash, ...dash], count = points.length / 2, pieces = [];
|
|
359
|
+
let entry = 0, left = pattern[0], piece = [points[0], points[1]];
|
|
360
|
+
for (let i = 0; i + 1 < count || (loop && i < count); i++) {
|
|
361
|
+
const ax = points[2 * i], ay = points[2 * i + 1], j = (i + 1) % count, bx = points[2 * j], by = points[2 * j + 1], length = Math.hypot(bx - ax, by - ay);
|
|
362
|
+
let done = 0;
|
|
363
|
+
while (length - done > left) {
|
|
364
|
+
done += left;
|
|
365
|
+
const x = ax + ((bx - ax) * done) / length, y = ay + ((by - ay) * done) / length;
|
|
366
|
+
if (piece !== undefined) {
|
|
367
|
+
piece.push(x, y);
|
|
368
|
+
pieces.push(piece);
|
|
369
|
+
piece = undefined;
|
|
370
|
+
if (pieces.length > GRAPHICS_SHAPE_LIMITS.maxDashes)
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
else
|
|
374
|
+
piece = [x, y];
|
|
375
|
+
entry = (entry + 1) % pattern.length;
|
|
376
|
+
left = pattern[entry];
|
|
377
|
+
}
|
|
378
|
+
left -= length - done;
|
|
379
|
+
if (piece !== undefined)
|
|
380
|
+
piece.push(bx, by);
|
|
381
|
+
}
|
|
382
|
+
if (piece !== undefined && piece.length >= 4)
|
|
383
|
+
pieces.push(piece);
|
|
384
|
+
return pieces;
|
|
385
|
+
}
|
|
386
|
+
/**
|
|
387
|
+
* A line of a width along points. Two pieces meet at a corner in one point on its inside, so a translucent line is not
|
|
388
|
+
* darker there; its outside is a mitre, cut to a bevel when longer than `miterLimit` half-widths, a bevel or an arc.
|
|
389
|
+
* An open line ends flat, in a half square or in a half circle. False when its dashes are more than a line may have.
|
|
390
|
+
*/
|
|
391
|
+
export function strokeLine(target, line, closed, style) {
|
|
392
|
+
const points = distinct(line, closed);
|
|
393
|
+
if (points.length < 4 || style.width <= 0)
|
|
394
|
+
return true;
|
|
395
|
+
const loop = closed && points.length >= 6;
|
|
396
|
+
if (style.dash === undefined) {
|
|
397
|
+
strokeSolid(target, points, loop, style);
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
const pieces = dashPieces(points, loop, style.dash);
|
|
401
|
+
if (pieces === undefined)
|
|
402
|
+
return false;
|
|
403
|
+
for (const piece of pieces) {
|
|
404
|
+
const part = distinct(piece, false);
|
|
405
|
+
if (part.length >= 4)
|
|
406
|
+
strokeSolid(target, part, false, style);
|
|
407
|
+
}
|
|
408
|
+
return true;
|
|
409
|
+
}
|
|
410
|
+
function strokeSolid(target, points, loop, style) {
|
|
411
|
+
batchFor(target, undefined);
|
|
412
|
+
const count = points.length / 2, half = style.width / 2, segments = loop ? count : count - 1, arc = curveSegments(half);
|
|
413
|
+
const out = [], triangles = [];
|
|
414
|
+
const add = (x, y) => {
|
|
415
|
+
out.push(x, y);
|
|
416
|
+
return out.length / 2 - 1;
|
|
417
|
+
};
|
|
418
|
+
// Each piece's unit direction and length; its normal is that direction turned a quarter.
|
|
419
|
+
const dx = [], dy = [], lengths = [];
|
|
420
|
+
for (let i = 0; i < segments; i++) {
|
|
421
|
+
const j = (i + 1) % count, x = points[2 * j] - points[2 * i], y = points[2 * j + 1] - points[2 * i + 1], length = Math.hypot(x, y);
|
|
422
|
+
dx.push(x / length);
|
|
423
|
+
dy.push(y / length);
|
|
424
|
+
lengths.push(length);
|
|
425
|
+
}
|
|
426
|
+
/** A fan about a centre from one direction to another, the short way round by `sweep` radians, between two vertices already there. */
|
|
427
|
+
const fan = (hub, cx, cy, fromX, fromY, sweep, first, last) => {
|
|
428
|
+
const steps = Math.max(1, Math.ceil((arc * Math.abs(sweep)) / (2 * Math.PI)));
|
|
429
|
+
let before = first;
|
|
430
|
+
for (let i = 1; i < steps; i++) {
|
|
431
|
+
const angle = (sweep * i) / steps, cos = Math.cos(angle), sin = Math.sin(angle), now = add(cx + fromX * cos - fromY * sin, cy + fromX * sin + fromY * cos);
|
|
432
|
+
triangles.push(hub, before, now);
|
|
433
|
+
before = now;
|
|
434
|
+
}
|
|
435
|
+
triangles.push(hub, before, last);
|
|
436
|
+
};
|
|
437
|
+
// For each piece, the vertices on its normal's side and on the other, at its start and at its end.
|
|
438
|
+
const startPlus = [], startMinus = [], endPlus = [], endMinus = [];
|
|
439
|
+
for (let i = 0; i < count; i++) {
|
|
440
|
+
const before = i > 0 ? i - 1 : loop ? segments - 1 : -1, after = i < segments ? i : loop ? 0 : -1, x = points[2 * i], y = points[2 * i + 1];
|
|
441
|
+
if (before < 0 || after < 0) {
|
|
442
|
+
// An end of an open line.
|
|
443
|
+
const piece = before < 0 ? after : before, away = before < 0 ? -1 : 1, nx = -dy[piece] * half, ny = dx[piece] * half;
|
|
444
|
+
const ex = style.caps === 'square' ? x + away * dx[piece] * half : x, ey = style.caps === 'square' ? y + away * dy[piece] * half : y;
|
|
445
|
+
const plus = add(ex + nx, ey + ny), minus = add(ex - nx, ey - ny);
|
|
446
|
+
if (before < 0) {
|
|
447
|
+
startPlus[piece] = plus;
|
|
448
|
+
startMinus[piece] = minus;
|
|
449
|
+
}
|
|
450
|
+
else {
|
|
451
|
+
endPlus[piece] = plus;
|
|
452
|
+
endMinus[piece] = minus;
|
|
453
|
+
}
|
|
454
|
+
if (style.caps === 'round')
|
|
455
|
+
fan(add(x, y), x, y, nx, ny, away * -Math.PI, plus, minus);
|
|
456
|
+
continue;
|
|
457
|
+
}
|
|
458
|
+
const ax = -dy[before], ay = dx[before], bx = -dy[after], by = dx[after], cross = dx[before] * dy[after] - dy[before] * dx[after], dot = dx[before] * dx[after] + dy[before] * dy[after];
|
|
459
|
+
if (Math.abs(cross) < 1e-9 && dot > 0) {
|
|
460
|
+
// Straight on: the two pieces share both vertices.
|
|
461
|
+
endPlus[before] = startPlus[after] = add(x + ax * half, y + ay * half);
|
|
462
|
+
endMinus[before] = startMinus[after] = add(x - ax * half, y - ay * half);
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
if (Math.abs(cross) < 1e-9) {
|
|
466
|
+
// Straight back: each piece ends square, and a round corner is a half circle about the point.
|
|
467
|
+
const plus = (endPlus[before] = add(x + ax * half, y + ay * half)), minus = (endMinus[before] = add(x - ax * half, y - ay * half));
|
|
468
|
+
startPlus[after] = minus;
|
|
469
|
+
startMinus[after] = plus;
|
|
470
|
+
if (style.joints === 'round')
|
|
471
|
+
fan(add(x, y), x, y, ax * half, ay * half, -Math.PI, plus, minus);
|
|
472
|
+
continue;
|
|
473
|
+
}
|
|
474
|
+
// The inside of the corner is on the normal's side when the line turns that way.
|
|
475
|
+
const side = cross > 0 ? 1 : -1;
|
|
476
|
+
let mx = ax + bx, my = ay + by;
|
|
477
|
+
const length = Math.hypot(mx, my);
|
|
478
|
+
mx /= length;
|
|
479
|
+
my /= length;
|
|
480
|
+
const ratio = 1 / (mx * ax + my * ay);
|
|
481
|
+
// The inside point is kept on the pieces it joins, however sharp the corner.
|
|
482
|
+
const reach = Math.min(ratio * half, Math.hypot(half, Math.min(lengths[before], lengths[after])));
|
|
483
|
+
const inside = add(x + side * mx * reach, y + side * my * reach);
|
|
484
|
+
const outA = add(x - side * ax * half, y - side * ay * half), outB = add(x - side * bx * half, y - side * by * half);
|
|
485
|
+
if (side > 0) {
|
|
486
|
+
endPlus[before] = inside;
|
|
487
|
+
endMinus[before] = outA;
|
|
488
|
+
startPlus[after] = inside;
|
|
489
|
+
startMinus[after] = outB;
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
endMinus[before] = inside;
|
|
493
|
+
endPlus[before] = outA;
|
|
494
|
+
startMinus[after] = inside;
|
|
495
|
+
startPlus[after] = outB;
|
|
496
|
+
}
|
|
497
|
+
if (style.joints === 'round')
|
|
498
|
+
fan(inside, x, y, -side * ax * half, -side * ay * half, Math.atan2(cross, dot), outA, outB);
|
|
499
|
+
else if (style.joints === 'miter' && ratio <= style.miterLimit) {
|
|
500
|
+
const tip = add(x - side * mx * ratio * half, y - side * my * ratio * half);
|
|
501
|
+
triangles.push(inside, outA, tip, inside, tip, outB);
|
|
502
|
+
}
|
|
503
|
+
else
|
|
504
|
+
triangles.push(inside, outA, outB);
|
|
505
|
+
}
|
|
506
|
+
for (let i = 0; i < segments; i++)
|
|
507
|
+
triangles.push(startPlus[i], startMinus[i], endMinus[i], startPlus[i], endMinus[i], endPlus[i]);
|
|
508
|
+
const first = paint(target, out, style), from = target.indices.length;
|
|
509
|
+
for (const index of triangles)
|
|
510
|
+
target.indices.push(first + index);
|
|
511
|
+
if (style.bitmap === undefined && style.feather !== undefined && style.feather > 0)
|
|
512
|
+
featherEdges(target, from, style.feather);
|
|
513
|
+
}
|
|
514
|
+
/** How many straight pieces a curve of this radius is drawn with: each is within a quarter unit of the curve. */
|
|
515
|
+
export function curveSegments(radius) {
|
|
516
|
+
if (!(radius > 0.25))
|
|
517
|
+
return 8;
|
|
518
|
+
// A multiple of four, so that a whole circle has a corner at each of its four extremes.
|
|
519
|
+
return Math.max(8, Math.min(128, 4 * Math.ceil(Math.PI / Math.acos(1 - 0.25 / radius) / 4)));
|
|
520
|
+
}
|
|
521
|
+
/** The cosine and sine of a quarter-turn multiple are whole numbers here, so an extreme of a circle is where it is told. */
|
|
522
|
+
function unitAt(step, count) {
|
|
523
|
+
const quarter = (4 * step) / count;
|
|
524
|
+
if (Number.isInteger(quarter))
|
|
525
|
+
return [[1, 0, -1, 0][((quarter % 4) + 4) % 4], [0, 1, 0, -1][((quarter % 4) + 4) % 4]];
|
|
526
|
+
const angle = (2 * Math.PI * step) / count;
|
|
527
|
+
return [Math.cos(angle), Math.sin(angle)];
|
|
528
|
+
}
|
|
529
|
+
/** An ellipse as an outline, starting at its right and going on through its bottom. */
|
|
530
|
+
export function ellipseOutline(cx, cy, rx, ry) {
|
|
531
|
+
const count = curveSegments(Math.max(rx, ry)), out = [];
|
|
532
|
+
for (let i = 0; i < count; i++) {
|
|
533
|
+
const [cos, sin] = unitAt(i, count);
|
|
534
|
+
out.push(cx + rx * cos, cy + ry * sin);
|
|
535
|
+
}
|
|
536
|
+
return out;
|
|
537
|
+
}
|
|
538
|
+
/** A rectangle whose corners are quarter ellipses of the two radii. */
|
|
539
|
+
export function roundRectOutline(x, y, width, height, rx, ry) {
|
|
540
|
+
if (rx <= 0 || ry <= 0)
|
|
541
|
+
return [x, y, x + width, y, x + width, y + height, x, y + height];
|
|
542
|
+
const count = curveSegments(Math.max(rx, ry)) / 4, out = [];
|
|
543
|
+
// Each corner is a quarter of the ellipse, from the top right round through the bottom to the top left.
|
|
544
|
+
const corners = [
|
|
545
|
+
[x + width - rx, y + ry, 3],
|
|
546
|
+
[x + width - rx, y + height - ry, 0],
|
|
547
|
+
[x + rx, y + height - ry, 1],
|
|
548
|
+
[x + rx, y + ry, 2]
|
|
549
|
+
];
|
|
550
|
+
for (const [cx, cy, quarter] of corners)
|
|
551
|
+
for (let i = 0; i <= count; i++) {
|
|
552
|
+
const [cos, sin] = unitAt(quarter * count + i, 4 * count);
|
|
553
|
+
out.push(cx + rx * cos, cy + ry * sin);
|
|
554
|
+
}
|
|
555
|
+
return out;
|
|
556
|
+
}
|
|
557
|
+
/** The points of an arc after its first, which the caller has already placed. */
|
|
558
|
+
export function arcPoints(cx, cy, radius, start, end, anticlockwise) {
|
|
559
|
+
let sweep = end - start;
|
|
560
|
+
const turn = 2 * Math.PI;
|
|
561
|
+
if (anticlockwise) {
|
|
562
|
+
if (sweep > 0)
|
|
563
|
+
sweep -= turn * Math.ceil(sweep / turn);
|
|
564
|
+
if (sweep === 0 && end !== start)
|
|
565
|
+
sweep = -turn;
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
if (sweep < 0)
|
|
569
|
+
sweep += turn * Math.ceil(-sweep / turn);
|
|
570
|
+
if (sweep === 0 && end !== start)
|
|
571
|
+
sweep = turn;
|
|
572
|
+
}
|
|
573
|
+
if (Math.abs(end - start) >= turn)
|
|
574
|
+
sweep = anticlockwise ? -turn : turn;
|
|
575
|
+
const count = Math.max(1, Math.ceil((curveSegments(radius) * Math.abs(sweep)) / turn)), out = [];
|
|
576
|
+
for (let i = 1; i <= count; i++) {
|
|
577
|
+
const angle = start + (sweep * i) / count;
|
|
578
|
+
out.push(cx + radius * Math.cos(angle), cy + radius * Math.sin(angle));
|
|
579
|
+
}
|
|
580
|
+
return out;
|
|
581
|
+
}
|
|
582
|
+
/** The points of a curve of two or three control points after its first, which is `from`. */
|
|
583
|
+
export function bezierPoints(from, controls, to) {
|
|
584
|
+
const xs = [from[0], ...controls.filter((_, i) => i % 2 === 0), to[0]], ys = [from[1], ...controls.filter((_, i) => i % 2 === 1), to[1]];
|
|
585
|
+
let length = 0;
|
|
586
|
+
for (let i = 1; i < xs.length; i++)
|
|
587
|
+
length += Math.hypot(xs[i] - xs[i - 1], ys[i] - ys[i - 1]);
|
|
588
|
+
const count = Math.max(4, Math.min(96, Math.ceil(length / 6))), out = [];
|
|
589
|
+
for (let step = 1; step <= count; step++) {
|
|
590
|
+
const t = step / count, px = xs.slice(), py = ys.slice();
|
|
591
|
+
for (let level = px.length - 1; level > 0; level--)
|
|
592
|
+
for (let i = 0; i < level; i++) {
|
|
593
|
+
px[i] = px[i] + (px[i + 1] - px[i]) * t;
|
|
594
|
+
py[i] = py[i] + (py[i + 1] - py[i]) * t;
|
|
595
|
+
}
|
|
596
|
+
out.push(px[0], py[0]);
|
|
597
|
+
}
|
|
598
|
+
// The last point is the end itself, not a sum that comes close to it.
|
|
599
|
+
out[out.length - 2] = to[0];
|
|
600
|
+
out[out.length - 1] = to[1];
|
|
601
|
+
return out;
|
|
602
|
+
}
|
|
603
|
+
/** A shape drawn at once: its fill under its line. False when it is more than a fill or a line may be. */
|
|
604
|
+
export function addOutline(target, outline, convex, fill, line) {
|
|
605
|
+
// A fill under a line has its edge under the line, which smooths its own.
|
|
606
|
+
return ((fill === undefined || fillOutlines(target, [outline], convex, fill, line === undefined)) &&
|
|
607
|
+
(line === undefined || strokeLine(target, outline, true, line)));
|
|
608
|
+
}
|
|
609
|
+
/** The triangles of a path: its parts filled together as closed outlines, then each lined, closed only where its ends meet. */
|
|
610
|
+
export function addPath(target, path) {
|
|
611
|
+
if (path.fill !== undefined && !fillOutlines(target, path.subpaths, false, path.fill, path.line === undefined))
|
|
612
|
+
return false;
|
|
613
|
+
if (path.line !== undefined)
|
|
614
|
+
for (const points of path.subpaths) {
|
|
615
|
+
const n = points.length;
|
|
616
|
+
if (!strokeLine(target, points, n >= 6 && points[0] === points[n - 2] && points[1] === points[n - 1], path.line))
|
|
617
|
+
return false;
|
|
618
|
+
}
|
|
619
|
+
return true;
|
|
620
|
+
}
|
|
621
|
+
/** Whether a drawing of this many vertices and indices is one a node may hold. */
|
|
622
|
+
export function shapeFits(vertices, indices) {
|
|
623
|
+
return (vertices <= GRAPHICS_SHAPE_LIMITS.maxVertices &&
|
|
624
|
+
indices <= GRAPHICS_SHAPE_LIMITS.maxIndices &&
|
|
625
|
+
88 * vertices + 4 * indices <= GRAPHICS_SHAPE_LIMITS.maxLogicalBytes);
|
|
626
|
+
}
|
|
627
|
+
// Shapes drawn the same way, on any number of nodes, are one geometry: a frame admits 4,096 of them.
|
|
628
|
+
const shared = new Map(), leases = new WeakMap();
|
|
629
|
+
// What each shared drawing was made from, to tell a true match of its key from a chance one.
|
|
630
|
+
const sources = new WeakMap();
|
|
631
|
+
function sameNumbers(a, b) {
|
|
632
|
+
if (a.length !== b.length)
|
|
633
|
+
return false;
|
|
634
|
+
for (let i = 0; i < a.length; i++)
|
|
635
|
+
if (a[i] !== b[i])
|
|
636
|
+
return false;
|
|
637
|
+
return true;
|
|
638
|
+
}
|
|
639
|
+
function sameDrawing(made, batches) {
|
|
640
|
+
if (made === undefined || made.length !== batches.length)
|
|
641
|
+
return false;
|
|
642
|
+
for (let i = 0; i < made.length; i++) {
|
|
643
|
+
const a = made[i], b = batches[i];
|
|
644
|
+
if (a.bitmap?.lease !== b.bitmap?.lease ||
|
|
645
|
+
a.bitmap?.repeat !== b.bitmap?.repeat ||
|
|
646
|
+
!sameNumbers(a.positions, b.positions) ||
|
|
647
|
+
!sameNumbers(a.colors, b.colors) ||
|
|
648
|
+
!sameNumbers(a.indices, b.indices) ||
|
|
649
|
+
(b.bitmap !== undefined && !sameNumbers(a.uvs, b.uvs)))
|
|
650
|
+
return false;
|
|
651
|
+
}
|
|
652
|
+
return true;
|
|
653
|
+
}
|
|
654
|
+
let nextLease = 1;
|
|
655
|
+
const leaseNumber = (lease) => {
|
|
656
|
+
let n = leases.get(lease);
|
|
657
|
+
if (n === undefined) {
|
|
658
|
+
n = nextLease++;
|
|
659
|
+
leases.set(lease, n);
|
|
660
|
+
}
|
|
661
|
+
return n;
|
|
662
|
+
};
|
|
663
|
+
/** Whether a drawing is one a node may hold: every batch within what one geometry takes, and no more batches than a node has. */
|
|
664
|
+
export function drawingFits(drawing) {
|
|
665
|
+
if (drawing.done.length >= GRAPHICS_SHAPE_LIMITS.maxBatches)
|
|
666
|
+
return false;
|
|
667
|
+
return shapeFits(drawing.positions.length / 2, drawing.indices.length);
|
|
668
|
+
}
|
|
669
|
+
/** The geometries and bounds of a node's shapes, or undefined while they hold no triangle. */
|
|
670
|
+
export function resolveShape(shape) {
|
|
671
|
+
if (shape.resolved !== undefined)
|
|
672
|
+
return shape.resolved;
|
|
673
|
+
let target = shape;
|
|
674
|
+
if (shape.path !== undefined) {
|
|
675
|
+
target = copyDrawing(shape);
|
|
676
|
+
// A path still being drawn that has grown past what a node may hold is not drawn; Graphics refuses it when it is finished.
|
|
677
|
+
if (!addPath(target, shape.path) || !drawingFits(target))
|
|
678
|
+
target = shape;
|
|
679
|
+
}
|
|
680
|
+
const batches = [...target.done, target].filter(batch => batch.indices.length !== 0 && shapeFits(batch.positions.length / 2, batch.indices.length));
|
|
681
|
+
if (batches.length === 0)
|
|
682
|
+
return undefined;
|
|
683
|
+
// Two drawings are the same when their numbers are. A key of all the numbers written out would be as long as the
|
|
684
|
+
// drawing each time it changes; a short key is mixed from them instead, and a match is checked number by number.
|
|
685
|
+
let h1 = 0x811c9dc5, h2 = 0x1b873593, count = 0;
|
|
686
|
+
const mix = (value) => {
|
|
687
|
+
// The value as two whole numbers that tell any two different doubles apart in practice: its integer and scaled fraction parts.
|
|
688
|
+
const whole = Math.trunc(value), part = Math.round((value - whole) * 4294967296);
|
|
689
|
+
h1 = Math.imul(h1 ^ (whole | 0), 0x01000193) ^ (part | 0);
|
|
690
|
+
h2 = Math.imul(h2 + (part | 0), 0x85ebca6b) ^ (whole | 0) ^ (h2 >>> 15);
|
|
691
|
+
count++;
|
|
692
|
+
};
|
|
693
|
+
for (const batch of batches) {
|
|
694
|
+
for (const list of [batch.positions, batch.colors, batch.indices]) {
|
|
695
|
+
mix(list.length);
|
|
696
|
+
for (let i = 0; i < list.length; i++)
|
|
697
|
+
mix(list[i]);
|
|
698
|
+
}
|
|
699
|
+
if (batch.bitmap !== undefined) {
|
|
700
|
+
for (let i = 0; i < batch.uvs.length; i++)
|
|
701
|
+
mix(batch.uvs[i]);
|
|
702
|
+
mix(leaseNumber(batch.bitmap.lease));
|
|
703
|
+
mix(batch.bitmap.repeat ? 1 : 2);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
const key = count + ':' + (h1 >>> 0) + ':' + (h2 >>> 0);
|
|
707
|
+
let resolved = shared.get(key);
|
|
708
|
+
// A key that matches a different drawing is not that drawing: it is made anew and takes the key.
|
|
709
|
+
if (resolved !== undefined && !sameDrawing(sources.get(resolved), batches))
|
|
710
|
+
resolved = undefined;
|
|
711
|
+
if (resolved === undefined) {
|
|
712
|
+
let left = Infinity, top = Infinity, right = -Infinity, bottom = -Infinity;
|
|
713
|
+
const parts = [];
|
|
714
|
+
for (const { positions, colors, uvs, indices, bitmap } of batches) {
|
|
715
|
+
const vertices = positions.length / 2;
|
|
716
|
+
for (let i = 0; i < positions.length; i += 2) {
|
|
717
|
+
const x = positions[i], y = positions[i + 1];
|
|
718
|
+
if (x < left)
|
|
719
|
+
left = x;
|
|
720
|
+
if (x > right)
|
|
721
|
+
right = x;
|
|
722
|
+
if (y < top)
|
|
723
|
+
top = y;
|
|
724
|
+
if (y > bottom)
|
|
725
|
+
bottom = y;
|
|
726
|
+
}
|
|
727
|
+
parts.push(Object.freeze({
|
|
728
|
+
bitmap,
|
|
729
|
+
geometry: createSharedGeometry2D({
|
|
730
|
+
positions: Float64Array.from(positions),
|
|
731
|
+
uvs: Float64Array.from(uvs),
|
|
732
|
+
lightColors: Float64Array.from(colors),
|
|
733
|
+
darkColors: new Float64Array(3 * vertices),
|
|
734
|
+
indices: vertices > 65535 ? Uint32Array.from(indices) : Uint16Array.from(indices)
|
|
735
|
+
})
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
resolved = Object.freeze({
|
|
739
|
+
parts: Object.freeze(parts),
|
|
740
|
+
bounds: Object.freeze({ x: left, y: top, width: right - left, height: bottom - top })
|
|
741
|
+
});
|
|
742
|
+
if (shared.size >= 512)
|
|
743
|
+
shared.clear();
|
|
744
|
+
shared.set(key, resolved);
|
|
745
|
+
sources.set(resolved, batches.map(batch => ({
|
|
746
|
+
positions: batch.positions.slice(),
|
|
747
|
+
colors: batch.colors.slice(),
|
|
748
|
+
uvs: batch.bitmap === undefined ? [] : batch.uvs.slice(),
|
|
749
|
+
indices: batch.indices.slice(),
|
|
750
|
+
bitmap: batch.bitmap
|
|
751
|
+
})));
|
|
752
|
+
}
|
|
753
|
+
return (shape.resolved = resolved);
|
|
754
|
+
}
|
|
755
|
+
//# sourceMappingURL=graphicsShape.js.map
|