@altnovatrail/picture-kit 0.0.0-stage → 0.1.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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +27 -2
  4. package/lib/emoji.d.ts +16 -0
  5. package/lib/emoji.js +64 -0
  6. package/lib/index.d.ts +9 -0
  7. package/lib/index.js +10 -0
  8. package/lib/kind.d.ts +22 -0
  9. package/lib/kind.js +2 -0
  10. package/lib/kinds/balance.d.ts +34 -0
  11. package/lib/kinds/balance.js +217 -0
  12. package/lib/kinds/branches.d.ts +33 -0
  13. package/lib/kinds/branches.js +473 -0
  14. package/lib/kinds/calendar.d.ts +55 -0
  15. package/lib/kinds/calendar.js +532 -0
  16. package/lib/kinds/chart.d.ts +48 -0
  17. package/lib/kinds/chart.js +645 -0
  18. package/lib/kinds/choices.d.ts +14 -0
  19. package/lib/kinds/choices.js +53 -0
  20. package/lib/kinds/clock.d.ts +36 -0
  21. package/lib/kinds/clock.js +219 -0
  22. package/lib/kinds/coins.d.ts +37 -0
  23. package/lib/kinds/coins.js +550 -0
  24. package/lib/kinds/column.d.ts +37 -0
  25. package/lib/kinds/column.js +410 -0
  26. package/lib/kinds/commands-parts.d.ts +12 -0
  27. package/lib/kinds/commands-parts.js +24 -0
  28. package/lib/kinds/commands.d.ts +66 -0
  29. package/lib/kinds/commands.js +789 -0
  30. package/lib/kinds/containers.d.ts +52 -0
  31. package/lib/kinds/containers.js +758 -0
  32. package/lib/kinds/countfig.d.ts +41 -0
  33. package/lib/kinds/countfig.js +372 -0
  34. package/lib/kinds/cubes.d.ts +70 -0
  35. package/lib/kinds/cubes.js +371 -0
  36. package/lib/kinds/die.d.ts +32 -0
  37. package/lib/kinds/die.js +301 -0
  38. package/lib/kinds/domino.d.ts +37 -0
  39. package/lib/kinds/domino.js +450 -0
  40. package/lib/kinds/dotgrid.d.ts +38 -0
  41. package/lib/kinds/dotgrid.js +411 -0
  42. package/lib/kinds/figure.d.ts +62 -0
  43. package/lib/kinds/figure.js +687 -0
  44. package/lib/kinds/fold-parts.d.ts +73 -0
  45. package/lib/kinds/fold-parts.js +315 -0
  46. package/lib/kinds/fold.d.ts +52 -0
  47. package/lib/kinds/fold.js +650 -0
  48. package/lib/kinds/fraction.d.ts +50 -0
  49. package/lib/kinds/fraction.js +568 -0
  50. package/lib/kinds/gauges.d.ts +53 -0
  51. package/lib/kinds/gauges.js +880 -0
  52. package/lib/kinds/gears.d.ts +40 -0
  53. package/lib/kinds/gears.js +787 -0
  54. package/lib/kinds/grid.d.ts +45 -0
  55. package/lib/kinds/grid.js +591 -0
  56. package/lib/kinds/groups.d.ts +23 -0
  57. package/lib/kinds/groups.js +335 -0
  58. package/lib/kinds/index.d.ts +3 -0
  59. package/lib/kinds/index.js +87 -0
  60. package/lib/kinds/lanes.d.ts +36 -0
  61. package/lib/kinds/lanes.js +220 -0
  62. package/lib/kinds/layers.d.ts +31 -0
  63. package/lib/kinds/layers.js +419 -0
  64. package/lib/kinds/line.d.ts +38 -0
  65. package/lib/kinds/line.js +739 -0
  66. package/lib/kinds/magic-parts.d.ts +72 -0
  67. package/lib/kinds/magic-parts.js +186 -0
  68. package/lib/kinds/magic.d.ts +66 -0
  69. package/lib/kinds/magic.js +664 -0
  70. package/lib/kinds/map.d.ts +32 -0
  71. package/lib/kinds/map.js +370 -0
  72. package/lib/kinds/matchsticks.d.ts +26 -0
  73. package/lib/kinds/matchsticks.js +434 -0
  74. package/lib/kinds/necklace.d.ts +27 -0
  75. package/lib/kinds/necklace.js +342 -0
  76. package/lib/kinds/net.d.ts +47 -0
  77. package/lib/kinds/net.js +241 -0
  78. package/lib/kinds/overlay.d.ts +50 -0
  79. package/lib/kinds/overlay.js +519 -0
  80. package/lib/kinds/pieces.d.ts +41 -0
  81. package/lib/kinds/pieces.js +420 -0
  82. package/lib/kinds/pipes.d.ts +46 -0
  83. package/lib/kinds/pipes.js +657 -0
  84. package/lib/kinds/plane-parts.d.ts +105 -0
  85. package/lib/kinds/plane-parts.js +372 -0
  86. package/lib/kinds/plane.d.ts +35 -0
  87. package/lib/kinds/plane.js +755 -0
  88. package/lib/kinds/pyramid.d.ts +26 -0
  89. package/lib/kinds/pyramid.js +228 -0
  90. package/lib/kinds/ropes.d.ts +30 -0
  91. package/lib/kinds/ropes.js +571 -0
  92. package/lib/kinds/shapes.d.ts +59 -0
  93. package/lib/kinds/shapes.js +490 -0
  94. package/lib/kinds/solids.d.ts +78 -0
  95. package/lib/kinds/solids.js +1058 -0
  96. package/lib/kinds/tangram.d.ts +52 -0
  97. package/lib/kinds/tangram.js +713 -0
  98. package/lib/kinds/target.d.ts +39 -0
  99. package/lib/kinds/target.js +761 -0
  100. package/lib/kinds/tiles.d.ts +55 -0
  101. package/lib/kinds/tiles.js +606 -0
  102. package/lib/kinds/venn.d.ts +38 -0
  103. package/lib/kinds/venn.js +710 -0
  104. package/lib/noto.d.ts +2 -0
  105. package/lib/noto.js +212 -0
  106. package/lib/objects.d.ts +7 -0
  107. package/lib/objects.js +226 -0
  108. package/lib/pen.d.ts +32 -0
  109. package/lib/pen.js +60 -0
  110. package/lib/read.d.ts +9 -0
  111. package/lib/read.js +49 -0
  112. package/lib/render.d.ts +24 -0
  113. package/lib/render.js +68 -0
  114. package/lib/solve/balance.d.ts +46 -0
  115. package/lib/solve/balance.js +118 -0
  116. package/lib/style.d.ts +70 -0
  117. package/lib/style.js +39 -0
  118. package/lib/svg.d.ts +22 -0
  119. package/lib/svg.js +48 -0
  120. package/lib/things.d.ts +40 -0
  121. package/lib/things.js +269 -0
  122. package/package.json +28 -4
@@ -0,0 +1,490 @@
1
+ import { margin } from "../kind.js";
2
+ import { textWidth } from "../pen.js";
3
+ import { int, isObj, list, oneOf, only, text } from "../read.js";
4
+ import { colors, faint, isColor, soft } from "../style.js";
5
+ import { drawThing, shapeNames } from "../things.js";
6
+ /**
7
+ * Shapes: geometric shapes of the palette in rows — side by side, standing
8
+ * one on another in a tower, lying one over another in a pile with a clear
9
+ * order, or in equations with operators and totals ("▲ + ▲ = 10") — or in a
10
+ * table whose rows and columns carry totals. A scene may carry its facts, the
11
+ * value of each shape, which the picture does not show: then the check proves
12
+ * that every equation and every total holds.
13
+ */
14
+ export const sizes = ["small", "medium", "large"];
15
+ export const ops = ["+", "-", "×", "=", "<", ">"];
16
+ const base = 60;
17
+ const least = 0.65;
18
+ const room = 640 - 2 * margin;
19
+ const scaleOf = { small: 0.55, medium: 0.78, large: 1 };
20
+ const opWords = {
21
+ "+": "plus",
22
+ "-": "minus",
23
+ "×": "times",
24
+ "=": "equals",
25
+ "<": "is less than",
26
+ ">": "is greater than",
27
+ };
28
+ const opGlyph = { "+": "+", "-": "−", "×": "×", "=": "=", "<": "<", ">": ">" };
29
+ /** How far each shape reaches above and below its centre, and to each side, per half its size. */
30
+ const extent = {
31
+ circle: { top: 1, bottom: 1, side: 1 },
32
+ oval: { top: 0.66, bottom: 0.66, side: 1 },
33
+ square: { top: 0.86, bottom: 0.86, side: 0.86 },
34
+ triangle: { top: 0.87, bottom: 0.705, side: 0.91 },
35
+ star: { top: 0.98, bottom: 0.9, side: 0.99 },
36
+ heart: { top: 0.675, bottom: 0.9, side: 0.83 },
37
+ diamond: { top: 1, bottom: 1, side: 1 },
38
+ hexagon: { top: 0.866, bottom: 0.866, side: 1 },
39
+ pentagon: { top: 0.94, bottom: 0.87, side: 0.95 },
40
+ cross: { top: 1, bottom: 1, side: 1 },
41
+ moon: { top: 1, bottom: 1, side: 1 },
42
+ };
43
+ /** drawThing draws a shape's outline 0.9 of the size it is given, so half of it is r. */
44
+ const halfOf = (size) => size * 0.45;
45
+ function checkPiece(p, path, problems, members) {
46
+ if (!isObj(p)) {
47
+ problems.push(`${path}: must be a shape and a colour`);
48
+ return;
49
+ }
50
+ only(p, members, path, problems);
51
+ oneOf(p.shape, shapeNames, `${path}.shape`, problems);
52
+ if (!isColor(p.color))
53
+ problems.push(`${path}.color: must be a colour`);
54
+ if (p.label !== undefined)
55
+ text(p.label, `${path}.label`, problems, 2);
56
+ if (p.size !== undefined)
57
+ oneOf(p.size, sizes, `${path}.size`, problems);
58
+ }
59
+ function isPiece(item) {
60
+ return "shape" in item;
61
+ }
62
+ const pileStep = 0.3;
63
+ const pileSize = 1.5;
64
+ function pileAt(p, i, k) {
65
+ if (p.at)
66
+ return p.at;
67
+ return [Math.round((i - (k - 1) / 2) * 1.4 * 10) / 10, i % 2 === 0 ? 0.5 : -0.5];
68
+ }
69
+ /** box is the size an item takes in a row, at scale 1. */
70
+ function box(item) {
71
+ if (isPiece(item)) {
72
+ const s = base * scaleOf[item.size ?? "large"];
73
+ return { w: Math.max(s, base * 0.55) + 8, h: s };
74
+ }
75
+ if ("op" in item)
76
+ return { w: 40, h: base };
77
+ if ("number" in item)
78
+ return { w: textWidth(item.number, 34) + 20, h: base };
79
+ if ("ask" in item)
80
+ return { w: base + 8, h: base };
81
+ if ("tower" in item) {
82
+ let h = 0;
83
+ let w = 0;
84
+ for (const p of item.tower) {
85
+ const r = halfOf(base * scaleOf[p.size ?? "large"]);
86
+ h += r * (extent[p.shape].top + extent[p.shape].bottom);
87
+ w = Math.max(w, 2 * r * extent[p.shape].side);
88
+ }
89
+ return { w: w + 24, h: h + 6 };
90
+ }
91
+ const at = item.pile.map((p, i) => pileAt(p, i, item.pile.length));
92
+ const xs = at.map((a) => a[0]);
93
+ const ys = at.map((a) => a[1]);
94
+ return {
95
+ w: base * pileSize + (Math.max(...xs) - Math.min(...xs)) * pileStep * base + 16,
96
+ h: base * pileSize + (Math.max(...ys) - Math.min(...ys)) * pileStep * base,
97
+ };
98
+ }
99
+ function rowWidth(row) {
100
+ return (row.label ? 40 : 0) + row.items.reduce((sum, item) => sum + box(item).w, 0);
101
+ }
102
+ function sayPiece(p) {
103
+ const size = "size" in p && p.size && p.size !== "large" ? `${p.size} ` : "";
104
+ const label = "label" in p && p.label ? ` marked ${p.label}` : "";
105
+ return `${size}${colors[p.color].word} ${p.shape}${label}`;
106
+ }
107
+ function a(words) {
108
+ return `${/^[aeiou]/.test(words) ? "an" : "a"} ${words}`;
109
+ }
110
+ function sayItem(item) {
111
+ if (isPiece(item))
112
+ return a(sayPiece(item));
113
+ if ("op" in item)
114
+ return opWords[item.op];
115
+ if ("number" in item)
116
+ return item.number === "?" ? "a question mark" : item.number;
117
+ if ("ask" in item)
118
+ return "a question mark";
119
+ if ("tower" in item)
120
+ return `a tower of ${item.tower.length} shapes standing one on another, from the bottom up: ${item.tower.map((p) => a(sayPiece(p))).join(", ")}`;
121
+ return `a pile of ${item.pile.length} shapes lying one over another, from the bottom one to the top one: ${item.pile.map((p) => a(sayPiece(p))).join(", ")}`;
122
+ }
123
+ function worth(values, p) {
124
+ return values.find((v) => v.shape === p.shape && v.color === p.color)?.value;
125
+ }
126
+ /** sideValue works a side of an equation out, × before + and −; undefined when it cannot. */
127
+ function sideValue(items, values, missing) {
128
+ const terms = [];
129
+ for (const item of items) {
130
+ if (isPiece(item)) {
131
+ const v = worth(values, item);
132
+ if (v === undefined) {
133
+ missing(item);
134
+ return undefined;
135
+ }
136
+ terms.push(v);
137
+ }
138
+ else if ("op" in item)
139
+ terms.push(item.op);
140
+ else if ("number" in item && /^\d+$/.test(item.number))
141
+ terms.push(Number(item.number));
142
+ else
143
+ return undefined;
144
+ }
145
+ // A side alternates numbers and + − ×, starting and ending with a number.
146
+ if (terms.length % 2 === 0)
147
+ return undefined;
148
+ for (let i = 0; i < terms.length; i++)
149
+ if ((i % 2 === 0) !== (typeof terms[i] === "number"))
150
+ return undefined;
151
+ const sums = [terms[0]];
152
+ const signs = [1];
153
+ for (let i = 1; i < terms.length; i += 2) {
154
+ const op = terms[i];
155
+ const v = terms[i + 1];
156
+ if (op === "×")
157
+ sums[sums.length - 1] *= v;
158
+ else if (op === "+" || op === "-") {
159
+ sums.push(v);
160
+ signs.push(op === "+" ? 1 : -1);
161
+ }
162
+ else
163
+ return undefined;
164
+ }
165
+ return sums.reduce((sum, v, i) => sum + v * signs[i], 0);
166
+ }
167
+ function checkFacts(typed, problems) {
168
+ const values = typed.facts?.values ?? [];
169
+ const missing = (where) => (p) => problems.push(`facts.values: no value for the ${colors[p.color].word} ${p.shape} of ${where}`);
170
+ (typed.rows ?? []).forEach((row, i) => {
171
+ const marks = row.items.filter((item) => "op" in item && ["=", "<", ">"].includes(item.op));
172
+ if (marks.length !== 1)
173
+ return;
174
+ const mark = marks[0];
175
+ const at = row.items.indexOf(mark);
176
+ const left = sideValue(row.items.slice(0, at), values, missing(`rows[${i}]`));
177
+ const right = sideValue(row.items.slice(at + 1), values, missing(`rows[${i}]`));
178
+ if (left === undefined || right === undefined)
179
+ return;
180
+ const holds = mark.op === "=" ? left === right : mark.op === "<" ? left < right : left > right;
181
+ if (!holds)
182
+ problems.push(`rows[${i}]: the values make the left side ${left} and the right side ${right}, so "${mark.op}" does not hold`);
183
+ });
184
+ const table = typed.table;
185
+ if (!table)
186
+ return;
187
+ const cell = (p, where) => {
188
+ const v = worth(values, p);
189
+ if (v === undefined)
190
+ missing(where)(p);
191
+ return v ?? 0;
192
+ };
193
+ table.right?.forEach((total, r) => {
194
+ const sum = table.cells[r].reduce((s, p, c) => s + cell(p, `table.cells[${r}][${c}]`), 0);
195
+ if (/^\d+$/.test(total) && Number(total) !== sum)
196
+ problems.push(`table.right[${r}]: the values make the row ${sum}, not ${total}`);
197
+ });
198
+ table.below?.forEach((total, c) => {
199
+ const sum = table.cells.reduce((s, row, r) => s + cell(row[c], `table.cells[${r}][${c}]`), 0);
200
+ if (/^\d+$/.test(total) && Number(total) !== sum)
201
+ problems.push(`table.below[${c}]: the values make the column ${sum}, not ${total}`);
202
+ });
203
+ }
204
+ function drawRow(pen, row, x0, mid, scale) {
205
+ let x = x0;
206
+ if (row.label) {
207
+ pen.text(x + 14 * scale, mid, row.label, { size: 22 });
208
+ x += 40 * scale;
209
+ }
210
+ for (const item of row.items) {
211
+ const b = box(item);
212
+ const cx = x + (b.w * scale) / 2;
213
+ if (isPiece(item))
214
+ drawThing(pen, item, cx, mid, base * scaleOf[item.size ?? "large"] * scale);
215
+ else if ("op" in item)
216
+ pen.text(cx, mid - 2, opGlyph[item.op], { size: 34 * scale });
217
+ else if ("number" in item)
218
+ pen.text(cx, mid, item.number, { size: 34 * scale });
219
+ else if ("ask" in item)
220
+ drawThing(pen, { ask: true }, cx, mid, base * scale);
221
+ else if ("tower" in item) {
222
+ // Stand each shape on the one below it, the first at the bottom.
223
+ let y = mid + (b.h * scale) / 2 - 3 * scale;
224
+ for (const p of item.tower) {
225
+ const size = base * scaleOf[p.size ?? "large"] * scale;
226
+ const r = halfOf(size);
227
+ const cy = y - r * extent[p.shape].bottom;
228
+ drawThing(pen, p, cx, cy, size);
229
+ y = cy - r * extent[p.shape].top;
230
+ }
231
+ }
232
+ else {
233
+ // Each later shape lies over the ones before it: drawn later, outline whole.
234
+ const k = item.pile.length;
235
+ const at = item.pile.map((p, i) => pileAt(p, i, k));
236
+ const mx = (Math.max(...at.map((q) => q[0])) + Math.min(...at.map((q) => q[0]))) / 2;
237
+ const my = (Math.max(...at.map((q) => q[1])) + Math.min(...at.map((q) => q[1]))) / 2;
238
+ item.pile.forEach((p, i) => {
239
+ const [ax, ay] = at[i];
240
+ drawThing(pen, p, cx + (ax - mx) * pileStep * base * scale, mid + (ay - my) * pileStep * base * scale, base * pileSize * scale);
241
+ });
242
+ }
243
+ x += b.w * scale;
244
+ }
245
+ }
246
+ function tableSize(table) {
247
+ const cols = table.cells[0].length;
248
+ const rows = table.cells.length;
249
+ const cell = 68;
250
+ const w = cols * cell + (table.right ? 72 : 0);
251
+ const h = rows * cell + (table.below ? 56 : 0);
252
+ return { cell, w, h };
253
+ }
254
+ function drawTable(pen, table, x0, y0) {
255
+ const { cell } = tableSize(table);
256
+ const rows = table.cells.length;
257
+ const cols = table.cells[0].length;
258
+ pen.el("rect", {
259
+ x: x0,
260
+ y: y0,
261
+ width: cols * cell,
262
+ height: rows * cell,
263
+ rx: 8,
264
+ fill: soft,
265
+ stroke: faint,
266
+ "stroke-width": 2,
267
+ });
268
+ for (let c = 1; c < cols; c++)
269
+ pen.el("path", {
270
+ d: `M${x0 + c * cell} ${y0 + 4}V${y0 + rows * cell - 4}`,
271
+ stroke: faint,
272
+ "stroke-width": 2,
273
+ });
274
+ for (let r = 1; r < rows; r++)
275
+ pen.el("path", {
276
+ d: `M${x0 + 4} ${y0 + r * cell}H${x0 + cols * cell - 4}`,
277
+ stroke: faint,
278
+ "stroke-width": 2,
279
+ });
280
+ table.cells.forEach((row, r) => {
281
+ row.forEach((p, c) => {
282
+ drawThing(pen, p, x0 + (c + 0.5) * cell, y0 + (r + 0.5) * cell, cell * 0.8 * scaleOf[p.size ?? "large"]);
283
+ });
284
+ });
285
+ table.right?.forEach((total, r) => {
286
+ pen.text(x0 + cols * cell + 40, y0 + (r + 0.5) * cell, total, { size: 30 });
287
+ });
288
+ table.below?.forEach((total, c) => {
289
+ pen.text(x0 + (c + 0.5) * cell, y0 + rows * cell + 30, total, { size: 30 });
290
+ });
291
+ }
292
+ export const shapes = {
293
+ kind: "shapes",
294
+ members: ["kind", "rows", "table", "facts"],
295
+ check(scene, problems) {
296
+ only(scene, this.members, "scene", problems);
297
+ if ((scene.rows === undefined) === (scene.table === undefined)) {
298
+ problems.push("scene: must hold rows or a table, one of the two");
299
+ return;
300
+ }
301
+ const pieceMembers = ["shape", "color", "label", "size"];
302
+ if (scene.rows !== undefined) {
303
+ list(scene.rows, "rows", problems, 1, 5).forEach((row, i) => {
304
+ const path = `rows[${i}]`;
305
+ if (!isObj(row)) {
306
+ problems.push(`${path}: must be a row of items`);
307
+ return;
308
+ }
309
+ only(row, ["items", "label"], path, problems);
310
+ if (row.label !== undefined)
311
+ text(row.label, `${path}.label`, problems, 2);
312
+ const items = list(row.items, `${path}.items`, problems, 1, 10);
313
+ const before = problems.length;
314
+ items.forEach((item, j) => {
315
+ const at = `${path}.items[${j}]`;
316
+ if (!isObj(item)) {
317
+ problems.push(`${at}: must be a shape, an op, a number, ask, a tower or a pile`);
318
+ }
319
+ else if ("shape" in item)
320
+ checkPiece(item, at, problems, pieceMembers);
321
+ else if ("op" in item) {
322
+ only(item, ["op"], at, problems);
323
+ oneOf(item.op, ops, `${at}.op`, problems);
324
+ }
325
+ else if ("number" in item) {
326
+ only(item, ["number"], at, problems);
327
+ text(item.number, `${at}.number`, problems, 4);
328
+ }
329
+ else if ("ask" in item) {
330
+ only(item, ["ask"], at, problems);
331
+ if (item.ask !== true)
332
+ problems.push(`${at}.ask: must be true`);
333
+ }
334
+ else if ("tower" in item) {
335
+ only(item, ["tower"], at, problems);
336
+ list(item.tower, `${at}.tower`, problems, 2, 4).forEach((p, k) => {
337
+ checkPiece(p, `${at}.tower[${k}]`, problems, pieceMembers);
338
+ });
339
+ }
340
+ else if ("pile" in item) {
341
+ only(item, ["pile"], at, problems);
342
+ const seen = new Set();
343
+ list(item.pile, `${at}.pile`, problems, 2, 5).forEach((p, k) => {
344
+ const pp = `${at}.pile[${k}]`;
345
+ checkPiece(p, pp, problems, ["shape", "color", "at"]);
346
+ if (!isObj(p) || p.at === undefined)
347
+ return;
348
+ const xy = list(p.at, `${pp}.at`, problems, 2, 2);
349
+ if (xy.length !== 2)
350
+ return;
351
+ int(xy[0], `${pp}.at[0]`, problems, -3, 3);
352
+ int(xy[1], `${pp}.at[1]`, problems, -2, 2);
353
+ const key = JSON.stringify(xy);
354
+ if (seen.has(key))
355
+ problems.push(`${pp}.at: two shapes of a pile lie in one place; one would hide the other`);
356
+ seen.add(key);
357
+ });
358
+ if (isObj(item) && Array.isArray(item.pile)) {
359
+ const placed = item.pile.filter((p) => isObj(p) && p.at !== undefined).length;
360
+ if (placed !== 0 && placed !== item.pile.length)
361
+ problems.push(`${at}.pile: give every shape its place, or none`);
362
+ }
363
+ }
364
+ else
365
+ problems.push(`${at}: must be a shape, an op, a number, ask, a tower or a pile`);
366
+ });
367
+ if (problems.length === before) {
368
+ const w = rowWidth(row);
369
+ if (w * least > room)
370
+ problems.push(`${path}.items: too wide to draw; use fewer or smaller items`);
371
+ }
372
+ });
373
+ }
374
+ if (scene.table !== undefined) {
375
+ const t = scene.table;
376
+ if (!isObj(t)) {
377
+ problems.push("table: must hold cells");
378
+ return;
379
+ }
380
+ only(t, ["cells", "right", "below"], "table", problems);
381
+ const rows = list(t.cells, "table.cells", problems, 1, 5);
382
+ let cols = 0;
383
+ rows.forEach((row, r) => {
384
+ const cells = list(row, `table.cells[${r}]`, problems, 1, 5);
385
+ if (r === 0)
386
+ cols = cells.length;
387
+ else if (cells.length !== cols)
388
+ problems.push(`table.cells[${r}]: every row holds as many cells as the first, ${cols}`);
389
+ cells.forEach((p, c) => {
390
+ checkPiece(p, `table.cells[${r}][${c}]`, problems, ["shape", "color", "size"]);
391
+ });
392
+ });
393
+ for (const [side, want, what] of [
394
+ ["right", rows.length, "a total for every row"],
395
+ ["below", cols, "a total for every column"],
396
+ ]) {
397
+ if (t[side] === undefined)
398
+ continue;
399
+ const totals = list(t[side], `table.${side}`, problems, want, want);
400
+ if (totals.length !== want)
401
+ problems.push(`table.${side}: must hold ${what}, ${want}`);
402
+ totals.forEach((v, i) => {
403
+ text(v, `table.${side}[${i}]`, problems, 4);
404
+ });
405
+ }
406
+ }
407
+ if (scene.facts === undefined || problems.length > 0)
408
+ return;
409
+ if (!isObj(scene.facts)) {
410
+ problems.push("facts: must hold values");
411
+ return;
412
+ }
413
+ only(scene.facts, ["values"], "facts", problems);
414
+ const seen = new Set();
415
+ list(scene.facts.values, "facts.values", problems, 1, 12).forEach((v, i) => {
416
+ const at = `facts.values[${i}]`;
417
+ if (!isObj(v)) {
418
+ problems.push(`${at}: must be a shape, a colour and a value`);
419
+ return;
420
+ }
421
+ only(v, ["shape", "color", "value"], at, problems);
422
+ oneOf(v.shape, shapeNames, `${at}.shape`, problems);
423
+ if (!isColor(v.color))
424
+ problems.push(`${at}.color: must be a colour`);
425
+ int(v.value, `${at}.value`, problems, 0, 1000);
426
+ const key = `${String(v.color)} ${String(v.shape)}`;
427
+ if (seen.has(key))
428
+ problems.push(`${at}: the ${key} has a value already`);
429
+ seen.add(key);
430
+ });
431
+ if (problems.length > 0)
432
+ return;
433
+ checkFacts(scene, problems);
434
+ },
435
+ draw(scene, pen) {
436
+ const typed = scene;
437
+ if (typed.table) {
438
+ const { w, h } = tableSize(typed.table);
439
+ drawTable(pen, typed.table, margin, margin);
440
+ return { width: w + 2 * margin, height: h + 2 * margin };
441
+ }
442
+ const rows = typed.rows ?? [];
443
+ const widest = Math.max(...rows.map(rowWidth));
444
+ const scale = Math.min(1, room / widest);
445
+ const labelled = rows.some((r) => r.label);
446
+ let y = margin;
447
+ for (const row of rows) {
448
+ const h = Math.max(...row.items.map((item) => box(item).h)) * scale;
449
+ const x0 = margin + (labelled ? 0 : (widest - rowWidth(row)) * scale * 0.5);
450
+ drawRow(pen, row, x0, y + h / 2, scale);
451
+ y += h + 14;
452
+ }
453
+ return { width: widest * scale + 2 * margin, height: y - 14 + margin };
454
+ },
455
+ describe(scene) {
456
+ const typed = scene;
457
+ if (typed.table) {
458
+ const t = typed.table;
459
+ const rows = t.cells.length;
460
+ const cols = t.cells[0].length;
461
+ const lines = t.cells.map((row, r) => {
462
+ const total = t.right ? `; the total of the row is ${t.right[r]}` : "";
463
+ return `Row ${r + 1}: ${row.map((p) => a(sayPiece(p))).join(", ")}${total}.`;
464
+ });
465
+ const below = t.below
466
+ ? ` The totals of the columns, from left to right: ${t.below.join(", ")}.`
467
+ : "";
468
+ return `A table of shapes, ${rows} row${rows === 1 ? "" : "s"} of ${cols}. ${lines.join(" ")}${below}`.replace(/\?/g, "a question mark");
469
+ }
470
+ const rows = typed.rows ?? [];
471
+ const say = (row) => row.items
472
+ .map((item, i) => {
473
+ const prev = row.items[i - 1];
474
+ const glue = i === 0
475
+ ? ""
476
+ : "op" in item || (prev && "op" in prev)
477
+ ? " "
478
+ : row.items.some((x) => "tower" in x || "pile" in x)
479
+ ? "; "
480
+ : ", ";
481
+ return glue + sayItem(item);
482
+ })
483
+ .join("");
484
+ if (rows.length === 1 && !rows[0].label) {
485
+ const words = say(rows[0]);
486
+ return `${words[0].toUpperCase()}${words.slice(1)}.`;
487
+ }
488
+ return rows.map((row, i) => `Row ${row.label ?? i + 1}: ${say(row)}.`).join(" ");
489
+ },
490
+ };
@@ -0,0 +1,78 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ /**
4
+ * Solids: one to five solids on a table — cubes, cuboids, cylinders, cones,
5
+ * pyramids, prisms and spheres — in oblique projection: the front true to
6
+ * size, depth running up and to the right at half scale. A solid stands on
7
+ * the table, to the right of the one before, or, by `on`, in the middle of the
8
+ * top of a cube, a cuboid or an upright cylinder, so solids stand in towers.
9
+ * Under them, a row of panels may show the solids from the front, back, left,
10
+ * right or top, computed from the solids, or a flat outline given by name;
11
+ * drawn as views, coloured with their edges, or as shadows, flat and gray.
12
+ */
13
+ export declare const solidShapes: readonly ["cube", "cuboid", "cylinder", "cone", "pyramid", "prism", "sphere"];
14
+ export type SolidShape = (typeof solidShapes)[number];
15
+ export declare const directions: readonly ["front", "back", "left", "right", "top"];
16
+ export type Direction = (typeof directions)[number];
17
+ export declare const outlines: readonly ["circle", "square", "rectangle", "triangle", "semicircle", "oval", "ring", "rhombus", "trapezoid", "pentagon", "hexagon"];
18
+ export type Outline = (typeof outlines)[number];
19
+ export type Solid = {
20
+ shape: SolidShape;
21
+ size?: number | number[];
22
+ sides?: number;
23
+ lying?: boolean;
24
+ on?: number;
25
+ color?: ColorName;
26
+ label?: string;
27
+ };
28
+ export type Panel = {
29
+ from?: Direction;
30
+ shape?: Outline;
31
+ label?: string;
32
+ ask?: boolean;
33
+ };
34
+ export type SolidsScene = {
35
+ kind: "solids";
36
+ solids: Solid[];
37
+ hidden?: boolean;
38
+ views?: Panel[];
39
+ shadows?: boolean;
40
+ };
41
+ /** A point of a flat drawing: a to the right, b up. */
42
+ export type Pt = {
43
+ a: number;
44
+ b: number;
45
+ };
46
+ /**
47
+ * ngon is a regular polygon of n sides, w wide, a side flat at the bottom,
48
+ * counterclockwise from its bottom left corner, b running from 0 up; middle is
49
+ * the height of its centre.
50
+ */
51
+ export declare function ngon(sides: number, w: number): {
52
+ pts: Pt[];
53
+ middle: number;
54
+ };
55
+ /** The measures of a solid: the box it fills, w wide, d deep, h tall; its radius; its polygon. */
56
+ export type Measure = {
57
+ w: number;
58
+ d: number;
59
+ h: number;
60
+ r: number;
61
+ pts: Pt[];
62
+ middle: number;
63
+ sides: number;
64
+ lying: boolean;
65
+ };
66
+ /** A solid in its place: cx the middle of its width, y0 its front, z0 its bottom. */
67
+ export type Placed = Measure & {
68
+ i: number;
69
+ tower: number;
70
+ shape: SolidShape;
71
+ color: ColorName;
72
+ cx: number;
73
+ y0: number;
74
+ z0: number;
75
+ };
76
+ /** place stands each solid on the table, right of the tower before, or on the solid it names. */
77
+ export declare function place(s: SolidsScene): Placed[];
78
+ export declare const solids: Kind;