spine-rigc 1.5.2 → 1.6.1

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/src/keys.ts CHANGED
@@ -219,8 +219,12 @@ export function dottedPath(path: ReadonlyArray<string | number>): string {
219
219
  * kind of container is the refusal of the reader that walks it (a rig spec's
220
220
  * `"bones"` that is not an array is *a rig spec needs a non-empty "bones"
221
221
  * array*);
222
- * - `enum` — a closed set of names, refused by name where it is read, with the
223
- * names that exist listed (a slot's `blend`, a constraint's `type`);
222
+ * - `enum` — a closed set of names. Every `enum` row has an entry in the
223
+ * shape's enum table (`RIG_ENUMS`, `MOTION_ENUMS`, `MANIFEST_ENUMS`, typed by
224
+ * `SpecEnumTable`), which either states the set — and
225
+ * `refuseValuesOutsideTheirSet` refuses a value outside it — or names the
226
+ * reader that refuses it where it reads it, with the names that exist listed
227
+ * (a slot's `blend`, a constraint's `type`);
224
228
  * - `mixed` — a union of kinds whose owner decides by the value (`MotionKey.v`
225
229
  * is numbers on `rotate`, a name on `attachment`, a member map on a group).
226
230
  */
@@ -410,3 +414,73 @@ export function refuseValuesOfTheWrongType(
410
414
  }
411
415
  }
412
416
  }
417
+
418
+ /**
419
+ * Who refuses a value outside an `enum` row's closed set (issue #900).
420
+ *
421
+ * - `set` — the names the row accepts, refused from one place by
422
+ * `refuseValuesOutsideTheirSet`; `readAs` is what the readers did with any
423
+ * other value before the set was stated, which the refusal says, because
424
+ * "one of" alone does not tell an author that the build they had was wrong;
425
+ * - `owner` — the function that reads the value and refuses one outside its
426
+ * set by name there, listing the names that exist. It keeps its own sentence,
427
+ * which often says more than a set can (a first letter whose case is free, a
428
+ * row chosen by the value itself).
429
+ */
430
+ export type SpecEnumRule = { readonly set: readonly string[]; readonly readAs: string } | { readonly owner: string };
431
+
432
+ /** The keys of a type row whose type is `enum`. */
433
+ type EnumKeysOf<R> = { [K in keyof R]: R[K] extends 'enum' ? K : never }[keyof R];
434
+
435
+ /**
436
+ * A type table's enum table: one entry per `enum` row, and nothing else.
437
+ *
438
+ * 🔒 `satisfies SpecEnumTable<typeof X_TYPES>` is what makes a fourth unowned
439
+ * enum a type error rather than a row nobody holds: a shape with an `enum` row
440
+ * must appear, with exactly its `enum` keys, and a key of another type cannot.
441
+ * A selftest control holds the same claim at runtime and reads each `owner`'s
442
+ * body for the key it is said to own.
443
+ */
444
+ export type SpecEnumTable<T extends Readonly<Record<string, SpecTypeRow>>> = {
445
+ readonly [S in keyof T as [EnumKeysOf<T[S]>] extends [never] ? never : S]: { readonly [K in EnumKeysOf<T[S]>]: SpecEnumRule };
446
+ };
447
+
448
+ /**
449
+ * Refuse the first value of an `enum` row whose enum table states a set and
450
+ * that is not in it (issue #900).
451
+ *
452
+ * 🚨 The defect this closes was a green build again, one step past #890's:
453
+ * the type walk does not check `enum` (a name's type says nothing about which
454
+ * names exist), and three rows had nobody holding the name — `boneIndexing`,
455
+ * a bone's `from.rotation` and the cut manifest's `mesh.kind`. `"foo"` and `5`
456
+ * were each read as something the spec never said, in silence.
457
+ *
458
+ * It runs after the type walk and over the same visits, so a key the scan
459
+ * refused is never judged here, and before every reader. A row whose table
460
+ * entry names an `owner` is passed over: that reader refuses it with its own
461
+ * sentence. The order is the type walk's — visits in the scan's order, keys in
462
+ * the document's — so the refusal names the same value on every run.
463
+ */
464
+ export function refuseValuesOutsideTheirSet(
465
+ visits: readonly ShapeVisit[],
466
+ types: Readonly<Record<string, SpecTypeRow>>,
467
+ enums: Readonly<Record<string, Readonly<Record<string, SpecEnumRule>>>>,
468
+ where: string,
469
+ ): void {
470
+ for (const visit of visits) {
471
+ const row = types[visit.shape];
472
+ if (row === undefined) throw new Error(`no type row for shape ${visit.shape} (a key scan visited a shape the type table does not have)`);
473
+ for (const key of Object.keys(visit.node)) {
474
+ if (row[key] !== 'enum') continue;
475
+ const rule = enums[visit.shape]?.[key];
476
+ if (rule === undefined) throw new Error(`${visit.shape}.${key} is an enum row with no entry in its enum table`);
477
+ if (!('set' in rule)) continue;
478
+ const value = visit.node[key];
479
+ if (typeof value === 'string' && rule.set.includes(value)) continue;
480
+ throw new CompileError(
481
+ `${where}: ${visit.name([key])} is ${JSON.stringify(value) ?? String(value)}; one of ` +
482
+ `${rule.set.map((name) => JSON.stringify(name)).join(', ')} — ${rule.readAs}`,
483
+ );
484
+ }
485
+ }
486
+ }
package/src/mesh.ts CHANGED
@@ -42,6 +42,7 @@
42
42
  * Everything here is pure and integer-stable: same manifest in, same floats out
43
43
  * (assertion A18 recompiles and compares bytes).
44
44
  */
45
+ import type { ModelBinding, ModelVertices } from './model.ts';
45
46
 
46
47
  export interface MeshSpecInput {
47
48
  /** Polygon in part-local pixels, y down, in manifest order. */
@@ -1567,44 +1568,52 @@ export function buildContourMesh(input: ContourSpecInput): MeshGeometry {
1567
1568
  };
1568
1569
  }
1569
1570
 
1570
- /** One bone a weighted vertex can bind to: its index, and its world inverse. */
1571
+ /**
1572
+ * One bone a weighted vertex can bind to: its NAME, and its world inverse.
1573
+ *
1574
+ * It carried the bone's index into the emitted bone array until issue #917; the
1575
+ * index is the Spine emitter's now (`emitVertices` in `src/emit_spine.ts`), so a
1576
+ * binding made here names its bone and survives a bone inserted ahead of it.
1577
+ */
1571
1578
  export interface MeshBoneRef {
1572
- index: number;
1579
+ name: string;
1573
1580
  /** Spine world point -> this bone's local space, at the setup pose. */
1574
1581
  toBind: (worldX: number, worldY: number) => [number, number];
1575
1582
  }
1576
1583
 
1577
1584
  /**
1578
- * Weighted-mesh `vertices` encoding: per vertex, boneCount then
1579
- * (boneIndex, bindX, bindY, weight) repeated.
1585
+ * Bind a generated mesh's vertices to their bones by name: per vertex, one
1586
+ * `{ bone, x, y, weight }` per influence, `x, y` the vertex in that bone's LOCAL
1587
+ * setup space and `weight` on the generator's grid (`r6`).
1580
1588
  *
1581
1589
  * Bind coordinates are in each bone's LOCAL space, so a rotated bone needs a real
1582
1590
  * inverse transform — see `src/transform.ts` for why the old "world minus origin"
1583
1591
  * shortcut had to go and what it would have failed like.
1584
1592
  *
1585
- * The encoding is chosen by a length comparison alone, so
1586
- * there is no field that says "weighted" — get the run lengths wrong and the
1587
- * loader reads weights as coordinates without a word.
1593
+ * Returned as the model's `ModelVertices`, weighted, and not as Spine's run: the
1594
+ * run's bone INDEX is written by the Spine emitter at emission (issue #917), and
1595
+ * the encoding it chooses by a length comparison alone is said here outright.
1596
+ * The caller puts the numbers on the float32 grid, as it did the run's.
1588
1597
  */
1589
- export function encodeWeightedVertices(
1598
+ export function bindWeightedVertices(
1590
1599
  geometry: MeshGeometry,
1591
1600
  /** Part-local pixel -> Spine world. */
1592
1601
  toWorld: (x: number, y: number) => [number, number],
1593
1602
  bones: { anchor: MeshBoneRef; controls: MeshBoneRef[] },
1594
- ): number[] {
1595
- const out: number[] = [];
1603
+ ): Extract<ModelVertices, { weighted: true }> {
1604
+ const bindings: ModelBinding[][] = [];
1596
1605
  geometry.points.forEach(([px, py], i) => {
1597
1606
  const [wx, wy] = toWorld(px, py);
1598
- const vw = geometry.weights[i];
1599
- out.push(vw.length);
1600
- for (const { bone, control, weight } of vw) {
1607
+ const vertex: ModelBinding[] = [];
1608
+ for (const { bone, control, weight } of geometry.weights[i]) {
1601
1609
  const ref = bone === 'anchor' ? bones.anchor : bones.controls[control ?? 0];
1602
1610
  if (!ref) throw new MeshError(`vertex ${i} binds to control bone ${control ?? 0}, which the rig does not have`);
1603
1611
  const [bx, by] = ref.toBind(wx, wy);
1604
- out.push(ref.index, bx, by, r6(weight));
1612
+ vertex.push({ bone: ref.name, x: bx, y: by, weight: r6(weight) });
1605
1613
  }
1614
+ bindings.push(vertex);
1606
1615
  });
1607
- return out;
1616
+ return { weighted: true, bindings };
1608
1617
  }
1609
1618
 
1610
1619
  // ---------------------------------------------------------------------------