@weasel-js/core 1.3.0 → 1.4.0-pre.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +303 -0
- package/dist/{DrawCommand-BnZyQaDc.d.ts → DrawCommand-b-z1bCT-.d.ts} +1 -1
- package/dist/{chunk-3FB44DBC.js → chunk-BLJNRMKB.js} +3 -3
- package/dist/{chunk-3FB44DBC.js.map → chunk-BLJNRMKB.js.map} +1 -1
- package/dist/{chunk-3ZV53ZW2.js → chunk-H6E6KPTU.js} +19 -2
- package/dist/chunk-H6E6KPTU.js.map +1 -0
- package/dist/{chunk-N3T7D6AS.js → chunk-IMFXEINV.js} +455 -172
- package/dist/chunk-IMFXEINV.js.map +1 -0
- package/dist/{chunk-F74NVEHT.js → chunk-SBFC6J3C.js} +14 -3
- package/dist/chunk-SBFC6J3C.js.map +1 -0
- package/dist/{chunk-AQS4JGRA.js → chunk-Y5KS66G7.js} +3 -3
- package/dist/{chunk-AQS4JGRA.js.map → chunk-Y5KS66G7.js.map} +1 -1
- package/dist/index.d.ts +215 -165
- package/dist/index.js +5 -5
- package/dist/insert.js +1 -1
- package/dist/move.js +2 -2
- package/dist/{registry-BU1_3L_C.d.ts → registry-sBLZYWI1.d.ts} +167 -13
- package/dist/renderer.d.ts +6 -413
- package/dist/renderer.js +5 -5
- package/dist/resize.js +2 -2
- package/dist/routing.d.ts +2 -2
- package/dist/routing.js +1 -1
- package/dist/stroke-B_BxktoE.d.ts +626 -0
- package/package.json +8 -8
- package/dist/chunk-3ZV53ZW2.js.map +0 -1
- package/dist/chunk-F74NVEHT.js.map +0 -1
- package/dist/chunk-N3T7D6AS.js.map +0 -1
- package/dist/stroke-5pBwOm9s.d.ts +0 -190
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
import { GradStop, Stroke } from '@weasel-js/paint';
|
|
2
|
+
import { M as Mat3, a as ShaderProgramHandle, D as DrawCommand } from './DrawCommand-b-z1bCT-.js';
|
|
3
|
+
import { P as Path } from './path-JEV2c5If.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Minimal compile/link/lookup wrapper for a GL program. Throws
|
|
7
|
+
* `ShaderCompileError` on compile or link failure with the GL info log
|
|
8
|
+
* embedded in the error message — never returns a half-initialized program.
|
|
9
|
+
*
|
|
10
|
+
* Designed so test code can stub `getShaderParameter` / `getProgramParameter`
|
|
11
|
+
* via the recorder Proxy without further special-casing.
|
|
12
|
+
*/
|
|
13
|
+
type Stage = 'vertex' | 'fragment' | 'link';
|
|
14
|
+
/** Thrown when a shader fails to compile or link, carrying which stage failed
|
|
15
|
+
* and the driver's log. */
|
|
16
|
+
declare class ShaderCompileError extends Error {
|
|
17
|
+
readonly stage: Stage;
|
|
18
|
+
readonly log: string;
|
|
19
|
+
constructor(stage: Stage, log: string);
|
|
20
|
+
}
|
|
21
|
+
declare class ShaderProgram {
|
|
22
|
+
private readonly gl;
|
|
23
|
+
readonly handle: WebGLProgram;
|
|
24
|
+
private readonly uniforms;
|
|
25
|
+
private readonly attributes;
|
|
26
|
+
constructor(gl: WebGL2RenderingContext, vertSrc: string, fragSrc: string);
|
|
27
|
+
private compile;
|
|
28
|
+
lookupUniforms(names: readonly string[]): void;
|
|
29
|
+
lookupAttributes(names: readonly string[]): void;
|
|
30
|
+
uniform(name: string): WebGLUniformLocation | undefined;
|
|
31
|
+
attribute(name: string): number | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A tessellated representation of a Path, ready to upload to GL.
|
|
36
|
+
*
|
|
37
|
+
* - `vertices` is interleaved x,y in path-local coordinates (`Float32Array`
|
|
38
|
+
* of length `2 * vertexCount`).
|
|
39
|
+
* - `indices` are triangle indices into `vertices` (`Uint32Array`, length
|
|
40
|
+
* `3 * triangleCount`).
|
|
41
|
+
* - `requiresStencil` is set for paths whose fillRule is `'evenodd'` and
|
|
42
|
+
* whose triangulation is a *naive* per-contour fan rather than a clean
|
|
43
|
+
* inside/outside triangulation. The renderer must use a stencil
|
|
44
|
+
* two-pass when this flag is true. Single-contour paths and `'nonzero'`
|
|
45
|
+
* multi-contour paths leave it false.
|
|
46
|
+
* - `anchorA` / `anchorB` / `anchorT` parameterize each mesh vertex by
|
|
47
|
+
* the two consecutive path anchors it lies between and the arc-length
|
|
48
|
+
* fraction along that segment (0 = at A, 1 = at B). Vertices that fall
|
|
49
|
+
* exactly on an anchor set A === B and t = 0. Used at draw time when
|
|
50
|
+
* the DrawCommand supplies per-anchor colors so the renderer can lerp
|
|
51
|
+
* per mesh vertex. Optional — emitted by the path tessellators; absent
|
|
52
|
+
* on meshes built by other paths (e.g. text glyphs).
|
|
53
|
+
*/
|
|
54
|
+
interface Mesh {
|
|
55
|
+
readonly vertices: Float32Array;
|
|
56
|
+
readonly indices: Uint32Array;
|
|
57
|
+
readonly requiresStencil?: boolean;
|
|
58
|
+
readonly anchorA?: Uint32Array;
|
|
59
|
+
readonly anchorB?: Uint32Array;
|
|
60
|
+
readonly anchorT?: Float32Array;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
interface GLMeshHandle {
|
|
64
|
+
readonly vao: WebGLVertexArrayObject;
|
|
65
|
+
readonly indexCount: number;
|
|
66
|
+
readonly requiresStencil: boolean;
|
|
67
|
+
readonly anchorA?: Uint32Array;
|
|
68
|
+
readonly anchorB?: Uint32Array;
|
|
69
|
+
readonly anchorT?: Float32Array;
|
|
70
|
+
}
|
|
71
|
+
/** Resources to release when a Mesh is reclaimed by GC. */
|
|
72
|
+
interface MeshResources {
|
|
73
|
+
vao: WebGLVertexArrayObject;
|
|
74
|
+
vbo: WebGLBuffer;
|
|
75
|
+
ibo: WebGLBuffer;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Caches GL-side buffers + VAO per `Mesh` identity. Upload happens lazily
|
|
79
|
+
* on first `handleFor(mesh)` call.
|
|
80
|
+
*
|
|
81
|
+
* **GC-aware cleanup (deferred-delete queue):** when a Mesh becomes
|
|
82
|
+
* unreachable, the WeakMap entry is dropped automatically — but the
|
|
83
|
+
* underlying GL resources (VAO, VBO, IBO) would leak forever without an
|
|
84
|
+
* explicit `gl.delete*` call. We register each Mesh with a
|
|
85
|
+
* `FinalizationRegistry`; when the Mesh is reclaimed, the finalizer pushes
|
|
86
|
+
* the resources onto an internal queue. The renderer drains this queue at
|
|
87
|
+
* the start of each `render()` call (via `drainPendingDeletes()`), when GL
|
|
88
|
+
* is in a known state — no VAO bound, no draw mid-flight. Deleting from the
|
|
89
|
+
* finalizer directly was racy (it could fire between two `gl.bindVertexArray`
|
|
90
|
+
* calls inside `dispatch()`, sometimes corrupting the next draw); deferring
|
|
91
|
+
* the actual delete to a known-safe point keeps the use-after-free risk to
|
|
92
|
+
* zero.
|
|
93
|
+
*
|
|
94
|
+
* Caveats:
|
|
95
|
+
* - Finalizer timing is non-deterministic; resources may live for a frame
|
|
96
|
+
* or two beyond their Mesh, but they will be reclaimed.
|
|
97
|
+
* - If the GL context is lost between finalizer and drain, the drain
|
|
98
|
+
* no-ops safely (`gl.isContextLost()` makes deletes no-ops, and
|
|
99
|
+
* `gl.deleteBuffer(null)` is allowed).
|
|
100
|
+
*
|
|
101
|
+
* The cache is GL-context-bound; if the context is lost and re-created, the
|
|
102
|
+
* renderer should construct a new GLMeshCache. (Context loss handling lives
|
|
103
|
+
* in `WeaselRenderer`.)
|
|
104
|
+
*/
|
|
105
|
+
declare class GLMeshCache {
|
|
106
|
+
private readonly gl;
|
|
107
|
+
private readonly aPositionLoc;
|
|
108
|
+
private readonly map;
|
|
109
|
+
/** Meshes this context has drawn at least once; see `uploadRecurring`. */
|
|
110
|
+
private readonly seen;
|
|
111
|
+
private readonly finalizer;
|
|
112
|
+
private readonly pendingDeletes;
|
|
113
|
+
/** Transient resources allocated this frame; freed at end of render(). */
|
|
114
|
+
private readonly transientThisFrame;
|
|
115
|
+
constructor(gl: WebGL2RenderingContext, aPositionLoc: number);
|
|
116
|
+
handleFor(mesh: Mesh): GLMeshHandle;
|
|
117
|
+
/**
|
|
118
|
+
* Upload a Mesh that the caller knows is single-use (e.g. the per-frame
|
|
119
|
+
* stroke ribbon from `tessellateStroke`). Returns a handle backed by fresh
|
|
120
|
+
* GL resources that the renderer will free deterministically at the end
|
|
121
|
+
* of the current frame via `freeTransient()`. Bypasses the WeakMap cache
|
|
122
|
+
* and the FinalizationRegistry, so transient meshes never wait on GC.
|
|
123
|
+
*/
|
|
124
|
+
uploadTransient(mesh: Mesh): GLMeshHandle;
|
|
125
|
+
/**
|
|
126
|
+
* Upload a Mesh that may or may not recur across frames. Its first sight in
|
|
127
|
+
* *this* context takes `uploadTransient`; every later one takes the
|
|
128
|
+
* persistent handle. One transient upload to find out is cheaper than
|
|
129
|
+
* stranding a persistent VAO on a mesh that never returns, whose release
|
|
130
|
+
* would wait on GC. The transient upload does not populate the persistent
|
|
131
|
+
* map, so steady-state reuse begins on the third frame.
|
|
132
|
+
*/
|
|
133
|
+
uploadRecurring(mesh: Mesh): GLMeshHandle;
|
|
134
|
+
/**
|
|
135
|
+
* Free all transient resources allocated since the last call. Called by
|
|
136
|
+
* the renderer at the end of each `render()`. Safe under context loss.
|
|
137
|
+
*/
|
|
138
|
+
freeTransient(): void;
|
|
139
|
+
/** @internal — for tests asserting the transient-list size. */
|
|
140
|
+
_transientCount(): number;
|
|
141
|
+
/**
|
|
142
|
+
* Free GL resources whose Mesh has been GC'd since the last drain.
|
|
143
|
+
* Called by the renderer at the top of each `render()`, before any draws.
|
|
144
|
+
*/
|
|
145
|
+
drainPendingDeletes(): void;
|
|
146
|
+
/** @internal — for tests asserting the queue size. */
|
|
147
|
+
_pendingDeleteCount(): number;
|
|
148
|
+
/** @internal — for tests that need to simulate the finalizer firing. */
|
|
149
|
+
_enqueueDeleteForTest(resources: MeshResources): void;
|
|
150
|
+
private upload;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* GL texture upload + cache for MSDF font atlases (and future images).
|
|
155
|
+
*
|
|
156
|
+
* Textures are keyed by a string id (the font family name or image id).
|
|
157
|
+
* Pixels are uploaded once per id; a later upload of the same id reuses the
|
|
158
|
+
* texture and only re-applies its wrap mode when that changed.
|
|
159
|
+
* The cache is GL-context-bound; discard and create a new one on context loss.
|
|
160
|
+
*
|
|
161
|
+
* Atlas format: RGBA UNSIGNED_BYTE, linear filtering, no mipmaps.
|
|
162
|
+
* Mipmap generation is deliberately skipped — MSDF works correctly with
|
|
163
|
+
* linear filtering, and mipmap resampling corrupts the multi-channel SDF signal.
|
|
164
|
+
*/
|
|
165
|
+
type TexSource$1 = HTMLImageElement | ImageBitmap | ImageData | HTMLCanvasElement;
|
|
166
|
+
/** How a texture samples outside `0..1`. Pattern tiles need `'repeat'`;
|
|
167
|
+
* everything else clamps. WebGL2 allows `REPEAT` on NPOT textures, so a
|
|
168
|
+
* tile of any size tiles correctly. */
|
|
169
|
+
type TextureWrap = 'clamp' | 'repeat';
|
|
170
|
+
declare class GLTextureCache {
|
|
171
|
+
private readonly gl;
|
|
172
|
+
private readonly map;
|
|
173
|
+
private readonly wraps;
|
|
174
|
+
constructor(gl: WebGL2RenderingContext);
|
|
175
|
+
has(id: string): boolean;
|
|
176
|
+
/** Uploads the pixels once per id. `wrap` is re-applied whenever it differs
|
|
177
|
+
* from what the id was last uploaded under: the same registered image can
|
|
178
|
+
* be a clamped shader texture in one draw and a repeating pattern tile in
|
|
179
|
+
* the next, and first-upload-wins gave the second one the first one's mode. */
|
|
180
|
+
upload(id: string, source: TexSource$1, wrap?: TextureWrap): string;
|
|
181
|
+
private applyWrap;
|
|
182
|
+
/** Create a single-channel R8 texture from raw bytes (full upload).
|
|
183
|
+
* No-op if `id` already exists. Same LINEAR/CLAMP params as `upload`;
|
|
184
|
+
* UNPACK_ALIGNMENT dropped to 1 for non-4-aligned row widths. */
|
|
185
|
+
uploadR8(id: string, width: number, height: number, data: Uint8Array): void;
|
|
186
|
+
/** Patch a rect of an existing R8 texture with tightly-packed w×h bytes. */
|
|
187
|
+
subImageR8(id: string, x: number, y: number, w: number, h: number, data: Uint8Array): void;
|
|
188
|
+
bind(id: string, unit: number): void;
|
|
189
|
+
/**
|
|
190
|
+
* Delete every uploaded GL texture and clear the map. Called by
|
|
191
|
+
* `WeaselRenderer.dispose()`. The cache is unusable but refillable
|
|
192
|
+
* afterward — a subsequent `upload()` for a previously-seen id re-creates
|
|
193
|
+
* the texture rather than restoring the deleted one.
|
|
194
|
+
*/
|
|
195
|
+
free(): void;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* GL texture upload cache for ImageBitmap objects.
|
|
200
|
+
*
|
|
201
|
+
* Key: ImageBitmap (or pattern source object) identity (WeakMap) — lets GC
|
|
202
|
+
* reclaim unreferenced bitmaps. The GL textures are NOT freed when the source
|
|
203
|
+
* is gc'd; deferred to v2.
|
|
204
|
+
*
|
|
205
|
+
* Wrapping is set once at upload time per the `repetition` parameter:
|
|
206
|
+
* - undefined / 'no-repeat' → CLAMP_TO_EDGE
|
|
207
|
+
* - 'repeat' → REPEAT (both axes)
|
|
208
|
+
* - 'repeat-x' → REPEAT on S, CLAMP on T
|
|
209
|
+
* - 'repeat-y' → CLAMP on S, REPEAT on T
|
|
210
|
+
*
|
|
211
|
+
* Convention §2: texels stored straight; shader premultiplies.
|
|
212
|
+
*/
|
|
213
|
+
type PatternRepetition = 'repeat' | 'repeat-x' | 'repeat-y' | 'no-repeat';
|
|
214
|
+
/** MIN_FILTER strategy for uploaded textures. */
|
|
215
|
+
type ImageMinification = 'linear' | 'mipmap';
|
|
216
|
+
type TexSource = ImageBitmap | ImageData | HTMLCanvasElement | HTMLImageElement;
|
|
217
|
+
declare class GLImageCache {
|
|
218
|
+
private readonly gl;
|
|
219
|
+
private readonly minification;
|
|
220
|
+
private readonly map;
|
|
221
|
+
/** `minification` selects the MIN_FILTER strategy for uploaded textures.
|
|
222
|
+
* `'linear'` (default) is the screen path's existing behavior. `'mipmap'`
|
|
223
|
+
* generates mipmaps and filters LINEAR_MIPMAP_LINEAR — required for
|
|
224
|
+
* quality minification when a large source bitmap is drawn small (the
|
|
225
|
+
* headless print/export path); bilinear-only minification undersamples
|
|
226
|
+
* and produces moiré. */
|
|
227
|
+
constructor(gl: WebGL2RenderingContext, minification?: ImageMinification);
|
|
228
|
+
upload(key: object, source: TexSource, repetition?: PatternRepetition): WebGLTexture;
|
|
229
|
+
bind(key: object, unit: number): void;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* CPU gradient-ramp builder + GL 1×256 RGBA texture cache.
|
|
234
|
+
*
|
|
235
|
+
* Each unique stop list is uploaded once. Key = JSON.stringify(stops).
|
|
236
|
+
* The cache is GL-context-bound; discard and recreate on context loss.
|
|
237
|
+
*
|
|
238
|
+
* Output convention §2: texels are stored as straight RGBA. The fragment
|
|
239
|
+
* shader applies premultiplication before writing outColor.
|
|
240
|
+
*/
|
|
241
|
+
|
|
242
|
+
/** Bake gradient stops into a 256-entry RGBA lookup strip, which the shader
|
|
243
|
+
* samples instead of evaluating stops per fragment. */
|
|
244
|
+
declare function buildGradientRamp(stops: GradStop[]): Uint8ClampedArray;
|
|
245
|
+
declare class GradientRampCache {
|
|
246
|
+
private readonly gl;
|
|
247
|
+
private readonly map;
|
|
248
|
+
private totalQueries;
|
|
249
|
+
private cacheHits;
|
|
250
|
+
constructor(gl: WebGL2RenderingContext);
|
|
251
|
+
upload(stops: GradStop[]): string;
|
|
252
|
+
bind(key: string, unit: number): void;
|
|
253
|
+
hitRate(): number;
|
|
254
|
+
resetStats(): void;
|
|
255
|
+
/**
|
|
256
|
+
* Delete every uploaded GL ramp texture and clear the map. Called by
|
|
257
|
+
* `WeaselRenderer.dispose()`. Only the GL textures are owned resources —
|
|
258
|
+
* `buildGradientRamp`'s CPU-side `Uint8ClampedArray` is transient per
|
|
259
|
+
* `upload()` call and already gone by the time a ramp is cached. The
|
|
260
|
+
* cache is unusable but refillable afterward (stats are left as-is; call
|
|
261
|
+
* `resetStats()` separately if desired).
|
|
262
|
+
*/
|
|
263
|
+
free(): void;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** Row-major 4×5 color matrix identity. */
|
|
267
|
+
declare const IDENTITY_COLOR_MATRIX: Float32Array<ArrayBuffer>;
|
|
268
|
+
interface GroupFrame {
|
|
269
|
+
transform?: Mat3;
|
|
270
|
+
alpha?: number;
|
|
271
|
+
/** Row-major 4×5 color matrix (20 floats). Absent leaves the stack unchanged. */
|
|
272
|
+
colorMatrix?: Float32Array | number[];
|
|
273
|
+
}
|
|
274
|
+
declare class GroupState {
|
|
275
|
+
private transformStack;
|
|
276
|
+
private alphaStack;
|
|
277
|
+
private colorMatrixStack;
|
|
278
|
+
get transform(): Mat3;
|
|
279
|
+
get alpha(): number;
|
|
280
|
+
get colorMatrix(): Float32Array;
|
|
281
|
+
push(frame: GroupFrame): void;
|
|
282
|
+
/** Drop every pushed frame, leaving the root. A frame that throws part-way
|
|
283
|
+
* down the tree never pops, and the next frame would draw under leftovers. */
|
|
284
|
+
reset(): void;
|
|
285
|
+
pop(): void;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Growable vertex staging for consecutive solid-fill geometry.
|
|
290
|
+
*
|
|
291
|
+
* Geometry only: `draw.ts` owns when a run starts, what breaks it, and the
|
|
292
|
+
* uniforms the flush draws under. Colors ride the vertices (the batch program
|
|
293
|
+
* is `pathFillVColor` with `u_color` left at white) because shapes in a run
|
|
294
|
+
* differ in color and a merged draw has one set of uniforms — and so does the
|
|
295
|
+
* model transform, applied here rather than uploaded, so that shapes under
|
|
296
|
+
* different transforms still share a draw.
|
|
297
|
+
*/
|
|
298
|
+
|
|
299
|
+
declare class SolidBatch {
|
|
300
|
+
private readonly gl;
|
|
301
|
+
private readonly aPos;
|
|
302
|
+
private readonly aColor;
|
|
303
|
+
/** Cycled per flush, and created on first use so a renderer that flushes
|
|
304
|
+
* rarely allocates as few slots as it flushes. */
|
|
305
|
+
private readonly ring;
|
|
306
|
+
private next;
|
|
307
|
+
/** The same, for flushes past a slot's capacity; these sets grow to fit. */
|
|
308
|
+
private readonly largeRing;
|
|
309
|
+
private nextLarge;
|
|
310
|
+
private verts;
|
|
311
|
+
private idx;
|
|
312
|
+
private nVerts;
|
|
313
|
+
private nIdx;
|
|
314
|
+
/** Whether the staged run is rects alone, so its indices are the canonical
|
|
315
|
+
* quad pattern and a slot already holding that pattern needs no upload. */
|
|
316
|
+
private pureRects;
|
|
317
|
+
constructor(gl: WebGL2RenderingContext, prog: ShaderProgram);
|
|
318
|
+
get length(): number;
|
|
319
|
+
/** Whether staging `vertices` more would put the run past the per-flush cap. */
|
|
320
|
+
wouldOverflow(vertices: number): boolean;
|
|
321
|
+
/**
|
|
322
|
+
* Append one rect's four corners through `m`, all carrying `rgba` (straight
|
|
323
|
+
* alpha). An affine maps a rect to a parallelogram, so two triangles still
|
|
324
|
+
* cover it and the batch draws at `u_model` identity.
|
|
325
|
+
*/
|
|
326
|
+
pushRect(x: number, y: number, w: number, h: number, m: Mat3, r: number, g: number, b: number, a: number): void;
|
|
327
|
+
/**
|
|
328
|
+
* Append a tessellated mesh through `m`, all vertices carrying `rgba`. The
|
|
329
|
+
* mesh's own indices are rebased onto the staged vertices, which is why the
|
|
330
|
+
* index buffer is uploaded per flush rather than written once.
|
|
331
|
+
*/
|
|
332
|
+
pushMesh(mesh: Mesh, m: Mat3, r: number, g: number, b: number, a: number): void;
|
|
333
|
+
/** Upload the staged geometry into the next set of buffers and bind its VAO.
|
|
334
|
+
* Returns the index count for the caller's `drawElements`. */
|
|
335
|
+
uploadAndBind(): number;
|
|
336
|
+
reset(): void;
|
|
337
|
+
dispose(): void;
|
|
338
|
+
/** Grow the CPU arrays so `vertices` / `indices` more fit. */
|
|
339
|
+
private reserve;
|
|
340
|
+
private nextRingSlot;
|
|
341
|
+
private nextLargeSlot;
|
|
342
|
+
private createSet;
|
|
343
|
+
private deleteSet;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** How to construct a `WeaselRenderer`: the GL context or canvas to draw
|
|
347
|
+
* into, the output size, and the quality knobs that separate screen
|
|
348
|
+
* rendering from print. */
|
|
349
|
+
interface WeaselRendererOptions {
|
|
350
|
+
gl?: WebGL2RenderingContext;
|
|
351
|
+
canvas?: HTMLCanvasElement;
|
|
352
|
+
width: number;
|
|
353
|
+
height: number;
|
|
354
|
+
dpr: number;
|
|
355
|
+
/** MIN_FILTER strategy for image/pattern textures (`GLImageCache`).
|
|
356
|
+
* Default `'linear'` — the existing screen behavior. The headless
|
|
357
|
+
* `renderSceneToPixels` path passes `'mipmap'` for print-quality
|
|
358
|
+
* minification. Explicitly passing `'linear'` is always valid. */
|
|
359
|
+
imageMinification?: ImageMinification;
|
|
360
|
+
/** Flatness tolerance for curve tessellation, in WORLD units (see
|
|
361
|
+
* `TessellateOptions.flattenTolerance`). When set, path fills are
|
|
362
|
+
* tessellated fresh at this tolerance per frame (transient pool) instead
|
|
363
|
+
* of served from the Path-identity mesh cache — the cache key does not
|
|
364
|
+
* include tolerance. Default: unset — the existing cached behavior at
|
|
365
|
+
* `DEFAULT_FLATTEN_TOLERANCE`. The headless path derives this from the
|
|
366
|
+
* requested output scale; screen callers normally leave it unset. */
|
|
367
|
+
flattenTolerance?: number;
|
|
368
|
+
/** Per-render synchronous bake budget for dynamic canvas-SDF glyphs.
|
|
369
|
+
* Default DEFAULT_BAKE_BUDGET (16). The headless renderSceneToPixels
|
|
370
|
+
* path passes Infinity so print never defers a glyph. */
|
|
371
|
+
bakeBudget?: number;
|
|
372
|
+
/** On-screen glyph size, in CSS pixels, at or above which text renders from
|
|
373
|
+
* tessellated font outlines instead of a distance field — see
|
|
374
|
+
* `OUTLINE_MIN_SCREEN_PX`. Only faces registered with
|
|
375
|
+
* `registerFontOutlines` are affected; everything else keeps its SDF tier
|
|
376
|
+
* whatever this says. Pass `Infinity` to disable the tier outright, or 0
|
|
377
|
+
* to use outlines wherever they exist (what the headless path does, since
|
|
378
|
+
* print has no reason to sample a field it could evaluate exactly). */
|
|
379
|
+
textOutlineMinScreenSize?: number;
|
|
380
|
+
}
|
|
381
|
+
/** Where a renderer draws inside a buffer it does not own.
|
|
382
|
+
*
|
|
383
|
+
* The rect's SIZE is the renderer's own `width`/`height` — `resize()` owns
|
|
384
|
+
* that, and a second copy here could disagree with the one `DrawContext`
|
|
385
|
+
* reports to screen-space layers. */
|
|
386
|
+
interface RenderTarget {
|
|
387
|
+
/** Top-left of this renderer's output within the drawing buffer, in CSS
|
|
388
|
+
* pixels, with the origin at the buffer's top-left. GL's bottom-left origin
|
|
389
|
+
* is handled internally. */
|
|
390
|
+
origin: {
|
|
391
|
+
x: number;
|
|
392
|
+
y: number;
|
|
393
|
+
};
|
|
394
|
+
/** Clear colour and stencil within the rect before drawing. Default true.
|
|
395
|
+
* Pass false only when the caller clears the whole buffer itself on behalf
|
|
396
|
+
* of every co-tenant — a frame that clears neither inherits its neighbour's
|
|
397
|
+
* stencil bits, and even-odd fills then fill their holes. */
|
|
398
|
+
clear?: boolean;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* The WebGL2 renderer: takes a list of draw commands and paints them.
|
|
402
|
+
*
|
|
403
|
+
* It knows nothing about the scene — commands are the whole interface, which
|
|
404
|
+
* is what lets layers, HUD widgets and overlays all draw through the same
|
|
405
|
+
* pipeline. GPU resources (meshes, textures, gradient ramps) are cached across
|
|
406
|
+
* frames and keyed by identity, so re-issuing the same command is cheap.
|
|
407
|
+
*/
|
|
408
|
+
declare class WeaselRenderer {
|
|
409
|
+
private readonly gl;
|
|
410
|
+
private pathFill;
|
|
411
|
+
private pathFillVColor;
|
|
412
|
+
private textSdf;
|
|
413
|
+
private textSdfR8;
|
|
414
|
+
private imageFill;
|
|
415
|
+
private gradFill;
|
|
416
|
+
private patternFill;
|
|
417
|
+
private meshCache;
|
|
418
|
+
private textureCache;
|
|
419
|
+
private imageCache;
|
|
420
|
+
private gradRampCache;
|
|
421
|
+
private programRegistry;
|
|
422
|
+
private quadVbo;
|
|
423
|
+
private quadIbo;
|
|
424
|
+
private solidBatch;
|
|
425
|
+
private readonly groupState;
|
|
426
|
+
private widthCss;
|
|
427
|
+
private heightCss;
|
|
428
|
+
private dpr;
|
|
429
|
+
private canvas;
|
|
430
|
+
private target;
|
|
431
|
+
private readonly imageMinification;
|
|
432
|
+
private readonly flattenTolerance?;
|
|
433
|
+
private readonly bakeBudget;
|
|
434
|
+
private readonly textOutlineMinScreenSize;
|
|
435
|
+
private contextLost;
|
|
436
|
+
private boundOnLost;
|
|
437
|
+
private boundOnRestored;
|
|
438
|
+
/** True after `dispose()`. A disposed renderer ignores further `render()`
|
|
439
|
+
* calls and `registerProgram()` calls. */
|
|
440
|
+
private disposed;
|
|
441
|
+
constructor(opts: WeaselRendererOptions);
|
|
442
|
+
private uploadQuadGeometry;
|
|
443
|
+
/**
|
|
444
|
+
* Compile a consumer-registered shader program against this renderer's GL context.
|
|
445
|
+
*
|
|
446
|
+
* Call once per renderer after the module-level `registerProgram()`.
|
|
447
|
+
* Throws `ShaderCompileError` if compilation fails.
|
|
448
|
+
*
|
|
449
|
+
* In dev mode, calling again with the same handle replaces the compiled program.
|
|
450
|
+
*
|
|
451
|
+
* @experimental
|
|
452
|
+
*/
|
|
453
|
+
registerProgram(handle: ShaderProgramHandle): void;
|
|
454
|
+
/**
|
|
455
|
+
* The compiled program for `id`, compiling it against this context on first
|
|
456
|
+
* use. Unlike {@link registerProgram} this neither throws on a missing
|
|
457
|
+
* source nor on a compile failure: a paint kind asking for a program it
|
|
458
|
+
* never registered must decline the paint, not take down the frame.
|
|
459
|
+
*/
|
|
460
|
+
private ensureProgram;
|
|
461
|
+
/** The GL state every frame assumes. Applied per `render()` rather than once
|
|
462
|
+
* at construction because a co-tenant sharing this context moves all of it
|
|
463
|
+
* between our frames. */
|
|
464
|
+
private applyGlState;
|
|
465
|
+
/** Viewport and scissor for this frame. Re-applied inside `render()` because
|
|
466
|
+
* a co-tenant on the same context moves both between our frames. */
|
|
467
|
+
private applyTarget;
|
|
468
|
+
/** Confine this renderer to a rect of its drawing buffer, or pass null to
|
|
469
|
+
* give it the whole buffer back. Takes effect from the next `render()`. */
|
|
470
|
+
setTarget(target: RenderTarget | null): void;
|
|
471
|
+
getTarget(): RenderTarget | null;
|
|
472
|
+
isContextLost(): boolean;
|
|
473
|
+
private onContextLost;
|
|
474
|
+
private onContextRestored;
|
|
475
|
+
/** Free the GL resources this renderer itself owns and detach context-loss
|
|
476
|
+
* listeners when a canvas was supplied. Idempotent.
|
|
477
|
+
*
|
|
478
|
+
* Scope: built-in shader programs, any consumer-registered programs, the
|
|
479
|
+
* shared quad/rect geometry, any in-flight transient meshes, and the
|
|
480
|
+
* enumerable Map-keyed caches (`GLTextureCache` atlas/image textures,
|
|
481
|
+
* `GradientRampCache` ramp textures) ARE freed.
|
|
482
|
+
*
|
|
483
|
+
* NOT freed: `GLImageCache` (bitmap/pattern textures) and `GLMeshCache`'s
|
|
484
|
+
* persistent per-Path mesh cache are keyed by `WeakMap`, not enumerable,
|
|
485
|
+
* and are only reclaimed when the GL context itself goes away (cf.
|
|
486
|
+
* `GLImageCache`'s own "deferred to v2" note). On a caller-owned
|
|
487
|
+
* long-lived context — the headless render-to-pixels path hands the same
|
|
488
|
+
* `gl` to many short-lived renderers — the image/pattern textures and
|
|
489
|
+
* persistent Path meshes each renderer uploads DO accumulate across
|
|
490
|
+
* renderer instances until the caller recycles the context.
|
|
491
|
+
*
|
|
492
|
+
* Also: a Mesh that gets GC'd after `dispose()` still lands in
|
|
493
|
+
* `GLMeshCache`'s `pendingDeletes` queue via its `FinalizationRegistry`,
|
|
494
|
+
* but nothing drains that queue post-dispose (only `render()` does) —
|
|
495
|
+
* those GL resources leak until the context goes away too.
|
|
496
|
+
*
|
|
497
|
+
* A disposed renderer ignores further `render()` and `registerProgram()`
|
|
498
|
+
* calls. */
|
|
499
|
+
dispose(): void;
|
|
500
|
+
/**
|
|
501
|
+
* Draw one frame.
|
|
502
|
+
*
|
|
503
|
+
* `viewMatrix` is the frame's world→screen transform. It is only consulted
|
|
504
|
+
* by `units: 'world'` gradients, which fall back to screen space without
|
|
505
|
+
* it — every other command carries its own transform in the stream, so
|
|
506
|
+
* callers with no view concept can keep calling `render(commands)`.
|
|
507
|
+
*/
|
|
508
|
+
render(commands: DrawCommand[], viewMatrix?: Mat3): void;
|
|
509
|
+
resize(dims: {
|
|
510
|
+
width: number;
|
|
511
|
+
height: number;
|
|
512
|
+
dpr: number;
|
|
513
|
+
}): void;
|
|
514
|
+
/** @internal */ _gl(): WebGL2RenderingContext;
|
|
515
|
+
/** @internal */ _pathFill(): ShaderProgram;
|
|
516
|
+
/** @internal */ _pathFillVColor(): ShaderProgram;
|
|
517
|
+
/** @internal */ _solidBatch(): SolidBatch;
|
|
518
|
+
/** @internal */ _textSdf(): ShaderProgram;
|
|
519
|
+
/** @internal */ _textSdfR8(): ShaderProgram;
|
|
520
|
+
/** @internal */ _imageFill(): ShaderProgram;
|
|
521
|
+
/** @internal */ _gradFill(): ShaderProgram;
|
|
522
|
+
/** @internal */ _patternFill(): ShaderProgram;
|
|
523
|
+
/** @internal */ _meshCache(): GLMeshCache;
|
|
524
|
+
/** @internal */ _textureCache(): GLTextureCache;
|
|
525
|
+
/** @internal */ _imageCache(): GLImageCache;
|
|
526
|
+
/** @internal */ _gradRampCache(): GradientRampCache;
|
|
527
|
+
/** @internal */ _groupState(): GroupState;
|
|
528
|
+
/** @internal */ _widthCss(): number;
|
|
529
|
+
/** @internal */ _heightCss(): number;
|
|
530
|
+
/** @internal */ _dpr(): number;
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Uniform-grid sprite sheet layout — frame index to the source rect an
|
|
535
|
+
* `ImageDrawCommand` samples. Pure arithmetic over the grid description; it
|
|
536
|
+
* never touches the bitmap.
|
|
537
|
+
*/
|
|
538
|
+
/** A sprite sheet's grid. `margin` and `spacing` follow the Tiled / Aseprite
|
|
539
|
+
* tileset convention, so a sheet exported from either describes itself here. */
|
|
540
|
+
interface SpriteSheet {
|
|
541
|
+
frameWidth: number;
|
|
542
|
+
frameHeight: number;
|
|
543
|
+
columns: number;
|
|
544
|
+
/** Empty pixels around the whole grid. Default 0. */
|
|
545
|
+
margin?: number;
|
|
546
|
+
/** Empty pixels between adjacent cells — the gutter that keeps linear
|
|
547
|
+
* sampling off the neighboring frame. Default 0. */
|
|
548
|
+
spacing?: number;
|
|
549
|
+
}
|
|
550
|
+
/**
|
|
551
|
+
* Row-major source rect for frame `index`, counting from 0 at the top-left.
|
|
552
|
+
*
|
|
553
|
+
* `index` is not range-checked and does not wrap: past the last cell this
|
|
554
|
+
* returns a rect below the sheet, which draws as an edge smear. Wrapping is
|
|
555
|
+
* the animation's business (`frameRect(sheet, tick % count)`) — a sheet does
|
|
556
|
+
* not know how many of its cells are filled.
|
|
557
|
+
*/
|
|
558
|
+
declare function frameRect(sheet: SpriteSheet, index: number): {
|
|
559
|
+
x: number;
|
|
560
|
+
y: number;
|
|
561
|
+
w: number;
|
|
562
|
+
h: number;
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* View → Mat3 helper for layer `draw` implementations on world-space layers.
|
|
567
|
+
*
|
|
568
|
+
* The kit's main package ships a `View` type at
|
|
569
|
+
* `src/core/viewport/view.ts`; we re-declare a structurally compatible
|
|
570
|
+
* shape here to avoid a runtime cross-package import. Exported as
|
|
571
|
+
* `ViewLike` from the package barrel.
|
|
572
|
+
*/
|
|
573
|
+
|
|
574
|
+
/** A weasel View — `{x, y, scale: {x, y}}`. Local type to avoid a cross-package import. */
|
|
575
|
+
interface View {
|
|
576
|
+
x: number;
|
|
577
|
+
y: number;
|
|
578
|
+
scale: {
|
|
579
|
+
x: number;
|
|
580
|
+
y: number;
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
/**
|
|
584
|
+
* Build the world→screen transform matrix for a `View`.
|
|
585
|
+
* Use as the `transform` field of a `kind: 'group'` DrawCommand to wrap
|
|
586
|
+
* world-space content emitted from a layer `draw` implementation.
|
|
587
|
+
*
|
|
588
|
+
* Mapping: `screen = (world − {view.x, view.y}) × {view.scale.x, view.scale.y}`.
|
|
589
|
+
*
|
|
590
|
+
* Column-major layout (matches `mat3.identity()`):
|
|
591
|
+
* `[scale.x, 0, 0, 0, scale.y, 0, -view.x*scale.x, -view.y*scale.y, 1]`.
|
|
592
|
+
*/
|
|
593
|
+
declare function viewToMat3(view: View): Mat3;
|
|
594
|
+
|
|
595
|
+
/** Options for stroke tessellation. */
|
|
596
|
+
interface StrokeOptions {
|
|
597
|
+
flattenTolerance?: number;
|
|
598
|
+
/**
|
|
599
|
+
* Shorten each open subpath by this much at its start / end, in the same
|
|
600
|
+
* world units as the path. Stroke markers use this so a filled head is not
|
|
601
|
+
* speared by its own line; the caller resolves the distance, because this
|
|
602
|
+
* layer knows nothing about the marker registry.
|
|
603
|
+
*/
|
|
604
|
+
startInset?: number;
|
|
605
|
+
endInset?: number;
|
|
606
|
+
}
|
|
607
|
+
/** Resolve a stroke width to world units. A number is already world units;
|
|
608
|
+
* `{ px }` is screen pixels divided by the accumulated scale, so it holds its
|
|
609
|
+
* on-screen thickness as the view zooms. */
|
|
610
|
+
declare function resolveStrokeWidth(width: number | {
|
|
611
|
+
px: number;
|
|
612
|
+
}, scale: number): number;
|
|
613
|
+
/**
|
|
614
|
+
* Build a triangle-mesh ribbon from a stroked Path.
|
|
615
|
+
*
|
|
616
|
+
* Supports:
|
|
617
|
+
* - cap: 'butt' | 'round' | 'square'
|
|
618
|
+
* - join: 'miter' | 'round' | 'bevel'
|
|
619
|
+
* - center alignment directly; RectPath inner/outer alignment via a
|
|
620
|
+
* pre-shifted rect; PolygonPath inner/outer alignment via stencil at the
|
|
621
|
+
* renderer level
|
|
622
|
+
* - dash splitting
|
|
623
|
+
*/
|
|
624
|
+
declare function tessellateStroke(path: Path, stroke: Stroke, opts?: StrokeOptions): Mesh;
|
|
625
|
+
|
|
626
|
+
export { IDENTITY_COLOR_MATRIX as I, type Mesh as M, type RenderTarget as R, ShaderCompileError as S, type View as V, WeaselRenderer as W, type ImageMinification as a, type SpriteSheet as b, type StrokeOptions as c, type WeaselRendererOptions as d, buildGradientRamp as e, frameRect as f, ShaderProgram as g, resolveStrokeWidth as r, tessellateStroke as t, viewToMat3 as v };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weasel-js/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0-pre.0",
|
|
4
4
|
"description": "Domain-agnostic 2D scene graph primitives for React: viewport math, drag/resize/insert/clone interactions, layered canvas rendering.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "orochi235",
|
|
@@ -72,15 +72,15 @@
|
|
|
72
72
|
},
|
|
73
73
|
"peerDependencies": {
|
|
74
74
|
"react": ">=18",
|
|
75
|
-
"@weasel-js/font": "1.
|
|
75
|
+
"@weasel-js/font": "1.4.0-pre.0"
|
|
76
76
|
},
|
|
77
77
|
"dependencies": {
|
|
78
|
-
"@weasel-js/geom": "1.
|
|
79
|
-
"@weasel-js/gestures": "1.
|
|
80
|
-
"@weasel-js/history": "1.
|
|
81
|
-
"@weasel-js/modes": "1.
|
|
82
|
-
"@weasel-js/paint": "1.
|
|
83
|
-
"@weasel-js/text": "1.
|
|
78
|
+
"@weasel-js/geom": "1.4.0-pre.0",
|
|
79
|
+
"@weasel-js/gestures": "1.4.0-pre.0",
|
|
80
|
+
"@weasel-js/history": "1.4.0-pre.0",
|
|
81
|
+
"@weasel-js/modes": "1.4.0-pre.0",
|
|
82
|
+
"@weasel-js/paint": "1.4.0-pre.0",
|
|
83
|
+
"@weasel-js/text": "1.4.0-pre.0",
|
|
84
84
|
"earcut": "2.2.4",
|
|
85
85
|
"polygon-clipping": "^0.15.7"
|
|
86
86
|
},
|