@weasel-js/renderer 0.0.0-stage → 1.9.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/mesh.d.ts ADDED
@@ -0,0 +1,274 @@
1
+ import { GradientUnits, ColorSpace, FillStyle, GradStop } from '@weasel-js/paint';
2
+ import { c as PaintDrawKind } from './paintKinds-B2vEy9A4.js';
3
+ import '@weasel-js/registry';
4
+ import '@weasel-js/geom';
5
+
6
+ /**
7
+ * Coons and tensor-product patch surfaces — PDF's shading types 6 and 7, as
8
+ * pure geometry.
9
+ *
10
+ * A patch is a curved quadrilateral: four cubic Bézier edges and a color at
11
+ * each corner. The interior is what the edges imply — a Coons patch reads it
12
+ * off the boundary alone; a tensor patch adds four interior control points
13
+ * that pull it around. Both evaluate at a `(u, v)` in the unit square, which
14
+ * is what lets the renderer subdivide a patch into as many triangles as it
15
+ * wants without the patch knowing.
16
+ *
17
+ * Nothing here touches GL, a color string, or a scene. `evalPatch` is the
18
+ * whole contract.
19
+ */
20
+ /** A point in the patch's own space. */
21
+ interface MeshPoint {
22
+ x: number;
23
+ y: number;
24
+ }
25
+ /**
26
+ * One patch.
27
+ *
28
+ * `points` walks the boundary: corner 0, the two control points of the edge
29
+ * leaving it, corner 1, its two, and so on — twelve points, ending with the
30
+ * two controls that return to corner 0. A tensor patch appends its four
31
+ * interior points, in the order `(1,1)`, `(1,2)`, `(2,2)`, `(2,1)` of the
32
+ * bicubic control net, which is PDF's own order.
33
+ *
34
+ * Twelve points is a Coons patch and sixteen is a tensor patch; there is no
35
+ * flag, because the count already says which it is.
36
+ *
37
+ * `colors` are the corner colors in the same walk order, as whatever color
38
+ * form the caller resolves — the renderer hands premultiplied RGBA in, the
39
+ * serializer hands CSS strings.
40
+ */
41
+ interface MeshPatch<TColor = string> {
42
+ points: readonly MeshPoint[];
43
+ colors: readonly [TColor, TColor, TColor, TColor];
44
+ }
45
+ /** Corner `i` of the boundary walk. */
46
+ declare function patchCorner(patch: MeshPatch<unknown>, i: number): MeshPoint;
47
+ /** `true` when the patch carries the four interior points a tensor patch adds. */
48
+ declare function isTensorPatch(patch: MeshPatch<unknown>): boolean;
49
+ /** A patch is twelve boundary points, or sixteen with the interior. Anything
50
+ * else cannot be evaluated, and saying so here beats painting nonsense. */
51
+ declare function isValidPatch(patch: MeshPatch<unknown>): boolean;
52
+ /**
53
+ * The patch surface at `(u, v)`, both in `0..1`.
54
+ *
55
+ * `u` runs along the edge leaving corner 0 and `v` along the edge leaving
56
+ * corner 1, so `(0,0)` is corner 0, `(1,0)` corner 1, `(1,1)` corner 2 and
57
+ * `(0,1)` corner 3 — the same walk `colors` is in.
58
+ */
59
+ declare function evalPatch(patch: MeshPatch<unknown>, u: number, v: number): MeshPoint;
60
+ /**
61
+ * The four corner weights at `(u, v)` — bilinear, in the corner walk order.
62
+ *
63
+ * Color is separate from position on purpose: it interpolates in whatever
64
+ * space the caller chose, which is not a thing the surface can do for it.
65
+ */
66
+ declare function cornerWeights(u: number, v: number): [number, number, number, number];
67
+ /** Axis-aligned bounds of a patch, sampled on a grid. Control points alone
68
+ * bound a Bézier but not tightly, and a mesh paint is baked into a texture
69
+ * sized from this — a loose box wastes texels on every side. */
70
+ declare function patchBounds(patch: MeshPatch<unknown>, steps?: number): {
71
+ x: number;
72
+ y: number;
73
+ width: number;
74
+ height: number;
75
+ };
76
+
77
+ /**
78
+ * The `mesh-gradient` paint kind — PDF's shading types 6 and 7 as a registered
79
+ * paint.
80
+ *
81
+ * A mesh gradient is a set of curved quadrilateral patches, each with a color
82
+ * at every corner. It is the paint SVG tried to standardize as
83
+ * `<meshgradient>` and abandoned, and the one PDF has had since 1993; what it
84
+ * buys over the three gradients is a color field that bends, so a highlight
85
+ * can follow a form instead of running straight across it.
86
+ *
87
+ * This is a registered kind rather than a sixth branch in the renderer, and
88
+ * deliberately: it uses only the slots `registerPaintKind` gives a consumer, so
89
+ * whatever it can do, a consumer's own kind can do too. Its one concession is
90
+ * living in this package, because that is where the shader registry is.
91
+ *
92
+ * `docs/proposals/2026-09-17-paint-kinds-beyond-svg.md` is the design.
93
+ */
94
+
95
+ /**
96
+ * A mesh gradient paint.
97
+ *
98
+ * `patches` are in the space `units` names, the same way a gradient's
99
+ * geometry is — under `'bounds'` every point is a fraction of the painted
100
+ * node's box, which is what lets the paint survive a resize.
101
+ *
102
+ * `interpolate` is the space corner colors blend through, matching the three
103
+ * gradients. It is more visible here than there: a mesh blends in two
104
+ * directions at once, so an sRGB blend's muddy middle covers area rather than
105
+ * a line.
106
+ */
107
+ interface MeshGradientFill {
108
+ fill: 'mesh-gradient';
109
+ patches: MeshPatch[];
110
+ units?: GradientUnits;
111
+ interpolate?: ColorSpace;
112
+ opacity?: number;
113
+ }
114
+ /** The kind's id, as `FillStyle['fill']` carries it. */
115
+ declare const MESH_GRADIENT_KIND = "mesh-gradient";
116
+ /** A mesh paint, narrowed. */
117
+ declare function isMeshGradientFill(fill: FillStyle | null | undefined): fill is FillStyle & MeshGradientFill;
118
+ /** A one-patch square covering the unit box, its four corners four readings of
119
+ * `color` — a mesh seeded from a solid has to show *something* curved, or
120
+ * switching kind looks like nothing happened. */
121
+ declare function seedMeshPatch(color: string): MeshGradientFill;
122
+ /**
123
+ * A stop list as a mesh: one full-height band per gap between neighboring
124
+ * stops, colored left to right. The bands blend exactly the way the ramp
125
+ * does, so a horizontal gradient survives the switch unchanged. A zero-width
126
+ * gap is a hard break and gets no band.
127
+ */
128
+ declare function meshFromStops(stops: readonly GradStop[]): MeshGradientFill;
129
+ /**
130
+ * A mesh read as a stop list: every patch's top edge — corner 0 to corner 1 —
131
+ * placed by its `x` across the mesh's horizontal extent. The inverse of
132
+ * `meshFromStops`, and for any other mesh the ramp across its top.
133
+ */
134
+ declare function meshStops(mesh: MeshGradientFill): GradStop[];
135
+ /** The kind's draw half, shader program included. Core adds the editor's
136
+ * slots and registers the whole entry. */
137
+ declare const meshDrawKind: PaintDrawKind;
138
+ /**
139
+ * The `<defs>` entry for a mesh paint, in weasel's own namespace.
140
+ *
141
+ * Not SVG's abandoned `<meshgradient>`: that vocabulary only reaches Coons
142
+ * patches, shares patch edges implicitly — a reader that gets the sharing
143
+ * wrong tears the surface rather than failing outright — and has exactly one
144
+ * implementation in the world. Ours writes every patch's points in full.
145
+ * `@weasel-js/svg` puts the paint fallback color beside the reference, so a
146
+ * foreign renderer paints flat instead of nothing.
147
+ */
148
+ declare function meshGradientXml(id: string, mesh: MeshGradientFill): string;
149
+
150
+ /**
151
+ * A mesh paint baked into one RGBA bitmap.
152
+ *
153
+ * A patch is a surface over `(u, v)`, but a paint has to answer "what color is
154
+ * this fragment", which is the inverse question — and inverting a bicubic is
155
+ * Newton iteration per fragment. Every renderer that draws these instead
156
+ * subdivides the patch into small quads and rasterizes them forward, which is
157
+ * what this does, once per paint, into a texture the shader then samples. The
158
+ * cost is a texture and a resolution; the gain is that the paint composes with
159
+ * every fill the renderer already knows how to draw, clipping included.
160
+ *
161
+ * The bitmap is straight (non-premultiplied) RGBA, matching the ramp atlas;
162
+ * the fragment shader premultiplies.
163
+ */
164
+
165
+ /** Texels on a side of a bake when the caller names no size. The renderer
166
+ * sizes each bake from how large the paint draws — see `meshBakeSize`. */
167
+ declare const MESH_BAKE_SIZE = 256;
168
+ /** The box a bake covers, in the paint's own space. */
169
+ interface MeshBox {
170
+ x: number;
171
+ y: number;
172
+ width: number;
173
+ height: number;
174
+ }
175
+ /** The bitmap `bakeMesh` produces, and the paint-space box it covers. */
176
+ interface BakedMesh {
177
+ /** `size` × `size` texels, straight RGBA. */
178
+ pixels: Uint8ClampedArray;
179
+ /** Texels on a side; `MESH_BAKE_SIZE` unless the caller asked otherwise. */
180
+ size: number;
181
+ /** Where the bitmap sits in paint space, which is what the shader maps a
182
+ * fragment through. */
183
+ box: MeshBox;
184
+ }
185
+ /** The union of every patch's bounds, or `null` when nothing is drawable. */
186
+ declare function meshBounds(patches: readonly MeshPatch[]): MeshBox | null;
187
+ /**
188
+ * Rasterize `patches` into a square bitmap covering their union.
189
+ *
190
+ * Returns `null` when no patch is drawable, which is the caller's cue to paint
191
+ * nothing rather than a blank square.
192
+ */
193
+ declare function bakeMesh(patches: readonly MeshPatch[], space?: ColorSpace, size?: number): BakedMesh | null;
194
+
195
+ /**
196
+ * How large a mesh bake is, and the bounded cache the bakes live in.
197
+ *
198
+ * A gradient's ramp is one-dimensional, so 256 texels cover it at any size. A
199
+ * mesh's bake is two-dimensional and is stretched over whatever the paint
200
+ * covers, so a fixed size shows its texels once the paint draws large. The
201
+ * bake is sized from the device pixels it covers instead, in power-of-two
202
+ * buckets so a zoom inside one bucket reuses the bake it already has.
203
+ */
204
+ /** The smallest bake: below this a texture costs more in bookkeeping than in
205
+ * bytes. */
206
+ declare const MESH_BAKE_MIN = 32;
207
+ /** The largest bake, whatever the context allows: 16 MiB of RGBA, and a bake
208
+ * that already takes a noticeable moment on the CPU. */
209
+ declare const MESH_BAKE_MAX = 2048;
210
+ /**
211
+ * The bake side for a paint drawn `devicePx` device pixels across, on a
212
+ * context accepting `maxTextureSize`: the next power of two at or above it,
213
+ * clamped to `[MESH_BAKE_MIN, MESH_BAKE_MAX]` and to the context. A size that
214
+ * cannot be measured gets `MESH_BAKE_SIZE`.
215
+ */
216
+ declare function meshBakeSize(devicePx: number, maxTextureSize: number): number;
217
+
218
+ /**
219
+ * The draggable points of a mesh paint, as pure geometry — what an on-canvas
220
+ * editor lists, and what moving one of them does to the paint.
221
+ *
222
+ * Nothing here knows a frame: the handles are in whatever space the mesh's
223
+ * points are, so a caller resolves a `'bounds'` mesh onto its box first
224
+ * (`fillInPoseFrame`) and normalizes the result back (`fillToBoundsFrame`).
225
+ */
226
+
227
+ /** What a handle moves: a patch corner, one of the two controls of an edge,
228
+ * or one of a tensor patch's four interior points. */
229
+ type MeshHandleKind = 'corner' | 'control' | 'interior';
230
+ /** One stored point: `patches[patch].points[index]`. */
231
+ interface MeshPointRef {
232
+ patch: number;
233
+ index: number;
234
+ }
235
+ /**
236
+ * One draggable point.
237
+ *
238
+ * Patches store their points in full rather than sharing edges, so two
239
+ * patches that meet each carry a copy of the corner and controls they meet
240
+ * at. Those copies are one handle, with a `ref` per copy — dragging one of
241
+ * them alone would tear the surface open along the seam.
242
+ */
243
+ interface MeshHandle {
244
+ /** Stable while the handle moves: its first ref, as `"patch:index"`. */
245
+ id: string;
246
+ kind: MeshHandleKind;
247
+ at: MeshPoint;
248
+ refs: readonly MeshPointRef[];
249
+ }
250
+ /**
251
+ * Every draggable point of `mesh`, in walk order: each patch's corners and
252
+ * edge controls, then a tensor patch's interior. Coincident points of one
253
+ * kind merge into one handle. A patch that cannot be evaluated has none.
254
+ */
255
+ declare function meshHandles(mesh: MeshGradientFill): MeshHandle[];
256
+ /**
257
+ * `mesh` with `handle` moved to `to`. A corner carries its edge controls
258
+ * and interior neighbor along by the same offset, the way a path editor's
259
+ * anchor carries its handles; a control or interior point moves alone.
260
+ * Returns a new paint; `mesh` is not modified.
261
+ */
262
+ declare function moveMeshHandle(mesh: MeshGradientFill, handle: MeshHandle, to: MeshPoint): MeshGradientFill;
263
+ /** The lines an editor draws under the handles, as control polygons. */
264
+ interface MeshGuides {
265
+ /** Every patch edge as its four cubic control points, from the corner it
266
+ * leaves to the corner it reaches. */
267
+ edges: [MeshPoint, MeshPoint, MeshPoint, MeshPoint][];
268
+ /** Corner to each point it carries — an edge control or interior point. */
269
+ arms: [MeshPoint, MeshPoint][];
270
+ }
271
+ /** The guides for `mesh`'s valid patches. Shared edges appear once per patch. */
272
+ declare function meshGuides(mesh: MeshGradientFill): MeshGuides;
273
+
274
+ export { type BakedMesh, MESH_BAKE_MAX, MESH_BAKE_MIN, MESH_BAKE_SIZE, MESH_GRADIENT_KIND, type MeshBox, type MeshGradientFill, type MeshGuides, type MeshHandle, type MeshHandleKind, type MeshPatch, type MeshPoint, type MeshPointRef, bakeMesh, cornerWeights, evalPatch, isMeshGradientFill, isTensorPatch, isValidPatch, meshBakeSize, meshBounds, meshDrawKind, meshFromStops, meshGradientXml, meshGuides, meshHandles, meshStops, moveMeshHandle, patchBounds, patchCorner, seedMeshPatch };