@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,550 @@
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 { colorNames, colors, ink, isColor, outline, textOn } from "../style.js";
5
+ import { n } from "../svg.js";
6
+ import { checkThing, drawThing, wordOf } from "../things.js";
7
+ import { article, mix } from "./cubes.js";
8
+ /**
9
+ * Coins: money and goods. Groups of coins and banknotes, each in a holder the
10
+ * picture draws itself — a purse, a piggy bank, an open hand — or in none;
11
+ * and goods on a shop shelf, each with its price tag on the shelf's edge. A
12
+ * coin is a round disc with its value, a note an oblong with its value: both
13
+ * generic, the design of no real currency. Every coin and note is drawn in a
14
+ * place of its own, whole, so a group shows exactly the money the scene says.
15
+ */
16
+ export const holders = ["none", "purse", "piggy", "hand"];
17
+ const coinR = 23;
18
+ const coinPitch = 52;
19
+ const noteW = 88;
20
+ const noteH = 46;
21
+ const notePitchX = 96;
22
+ const notePitchY = 54;
23
+ const room = 640 - 2 * margin;
24
+ const groupGap = 22;
25
+ const goodSize = 52;
26
+ const copySize = 40;
27
+ /** How many pieces a holder takes, how many of them notes, and how many go in a row. */
28
+ const holds = {
29
+ none: { most: 20, notes: 6, coinRow: 6, noteRow: 3 },
30
+ purse: { most: 20, notes: 6, coinRow: 5, noteRow: 2 },
31
+ piggy: { most: 12, notes: 2, coinRow: 4, noteRow: 2 },
32
+ hand: { most: 6, notes: 2, coinRow: 3, noteRow: 2 },
33
+ };
34
+ const defaultColor = {
35
+ none: "gray",
36
+ purse: "red",
37
+ piggy: "pink",
38
+ hand: "orange",
39
+ };
40
+ const holderWord = {
41
+ none: "group",
42
+ purse: "purse",
43
+ piggy: "piggy bank",
44
+ hand: "hand",
45
+ };
46
+ const placeWord = {
47
+ none: "money",
48
+ purse: "in a purse",
49
+ piggy: "in a piggy bank",
50
+ hand: "on an open hand",
51
+ };
52
+ function piecesOf(group) {
53
+ const out = [];
54
+ for (const item of group.items) {
55
+ const form = item.note !== undefined ? "note" : "coin";
56
+ const value = (form === "note" ? item.note : item.coin);
57
+ const color = item.color ?? (form === "note" ? "green" : "yellow");
58
+ for (let i = 0; i < (item.count ?? 1); i++)
59
+ out.push({ form, value, color });
60
+ }
61
+ return out;
62
+ }
63
+ /** rows splits k pieces into rows of at most per, as even as can be, the fuller rows first. */
64
+ function rows(k, per) {
65
+ if (k === 0)
66
+ return [];
67
+ const r = Math.ceil(k / per);
68
+ return Array.from({ length: r }, (_, i) => Math.floor(k / r) + (i < k % r ? 1 : 0));
69
+ }
70
+ /** lay places the pieces of a group in rows, notes above coins, centred on x = 0, from y = 0. */
71
+ function lay(group) {
72
+ const hold = holds[group.holder ?? "none"];
73
+ const all = piecesOf(group);
74
+ const notes = all.filter((p) => p.form === "note");
75
+ const coins = all.filter((p) => p.form === "coin");
76
+ const laid = [];
77
+ let y = 0;
78
+ let w = 0;
79
+ let i = 0;
80
+ for (const inRow of rows(notes.length, hold.noteRow)) {
81
+ w = Math.max(w, inRow * notePitchX - (notePitchX - noteW));
82
+ for (let j = 0; j < inRow; j++)
83
+ laid.push({ piece: notes[i++], x: (j - (inRow - 1) / 2) * notePitchX, y: y + noteH / 2 });
84
+ y += notePitchY;
85
+ }
86
+ i = 0;
87
+ for (const inRow of rows(coins.length, hold.coinRow)) {
88
+ w = Math.max(w, inRow * coinPitch - (coinPitch - 2 * coinR));
89
+ for (let j = 0; j < inRow; j++)
90
+ laid.push({ piece: coins[i++], x: (j - (inRow - 1) / 2) * coinPitch, y: y + coinR });
91
+ y += coinPitch;
92
+ }
93
+ return { laid, w, h: y - 6 };
94
+ }
95
+ /**
96
+ * reach is the half width of the smallest ellipse, its height k of its width,
97
+ * centred on the middle of the laid pieces, that holds every piece with pad to spare.
98
+ */
99
+ function reach(laid, h, k, pad) {
100
+ let most = 0;
101
+ for (const p of laid) {
102
+ const y = p.y - h / 2;
103
+ const points = p.piece.form === "coin"
104
+ ? Array.from({ length: 16 }, (_, i) => {
105
+ const a = (i * Math.PI) / 8;
106
+ return { x: p.x + (coinR + pad) * Math.cos(a), y: y + (coinR + pad) * Math.sin(a) };
107
+ })
108
+ : [-1, 1].flatMap((sx) => [-1, 1].map((sy) => ({
109
+ x: p.x + sx * (noteW / 2 + pad),
110
+ y: y + sy * (noteH / 2 + pad),
111
+ })));
112
+ for (const q of points)
113
+ most = Math.max(most, Math.hypot(q.x, q.y / k));
114
+ }
115
+ return most;
116
+ }
117
+ function valueSize(value, form) {
118
+ const digits = String(value).length;
119
+ if (form === "note")
120
+ return digits > 3 ? 18 : 20;
121
+ return digits > 2 ? 15 : 18;
122
+ }
123
+ function drawPiece(pen, piece, cx, cy) {
124
+ const c = colors[piece.color];
125
+ const ask = piece.value === "?";
126
+ if (piece.form === "coin") {
127
+ pen.el("circle", {
128
+ cx,
129
+ cy,
130
+ r: coinR,
131
+ fill: ask ? "#ffffff" : c.fill,
132
+ stroke: ink,
133
+ "stroke-width": outline,
134
+ "stroke-dasharray": ask ? "5 4" : undefined,
135
+ class: "coin",
136
+ });
137
+ if (!ask)
138
+ pen.el("circle", { cx, cy, r: coinR - 5, fill: "none", stroke: c.dark, "stroke-width": 2 });
139
+ pen.text(cx, cy + 1, String(piece.value), {
140
+ size: ask ? 22 : valueSize(piece.value, "coin"),
141
+ fill: ask ? ink : textOn(piece.color),
142
+ });
143
+ return;
144
+ }
145
+ pen.el("rect", {
146
+ x: cx - noteW / 2,
147
+ y: cy - noteH / 2,
148
+ width: noteW,
149
+ height: noteH,
150
+ rx: 6,
151
+ fill: ask ? "#ffffff" : c.fill,
152
+ stroke: ink,
153
+ "stroke-width": outline,
154
+ "stroke-dasharray": ask ? "6 4" : undefined,
155
+ class: "note",
156
+ });
157
+ if (!ask) {
158
+ pen.el("rect", {
159
+ x: cx - noteW / 2 + 5,
160
+ y: cy - noteH / 2 + 5,
161
+ width: noteW - 10,
162
+ height: noteH - 10,
163
+ rx: 3,
164
+ fill: "none",
165
+ stroke: c.dark,
166
+ "stroke-width": 1.5,
167
+ });
168
+ pen.el("ellipse", { cx, cy, rx: 28, ry: 15, fill: mix(c.fill, "#ffffff", 0.6) });
169
+ }
170
+ pen.text(cx, cy + 1, String(piece.value), {
171
+ size: ask ? 24 : valueSize(piece.value, "note"),
172
+ });
173
+ }
174
+ const line = { stroke: ink, "stroke-width": outline, "stroke-linejoin": "round" };
175
+ /** drawPurse draws a purse with a clasp round a block of money w × h, its top left at 0, 0. */
176
+ function drawPurse(pen, c, w, h) {
177
+ const bw = Math.max(150, w + 44);
178
+ const bh = h + 46;
179
+ const top = 26;
180
+ const l = 4;
181
+ const r = l + bw;
182
+ const b = top + bh;
183
+ for (const dx of [-9, 9])
184
+ pen.el("circle", { cx: l + bw / 2 + dx, cy: top - 9, r: 9, fill: colors.gray.fill, ...line });
185
+ pen.el("path", {
186
+ d: `M${n(l)} ${n(top + 14)}Q${n(l)} ${n(top)} ${n(l + 14)} ${n(top)}H${n(r - 14)}Q${n(r)} ${n(top)} ${n(r)} ${n(top + 14)}V${n(b - 40)}Q${n(r)} ${n(b)} ${n(r - 40)} ${n(b)}H${n(l + 40)}Q${n(l)} ${n(b)} ${n(l)} ${n(b - 40)}Z`,
187
+ fill: c.fill,
188
+ ...line,
189
+ });
190
+ // The metal frame along the top, a dark rim round a light bar.
191
+ for (const [stroke, width] of [
192
+ [colors.gray.dark, 8],
193
+ [colors.gray.fill, 3.5],
194
+ ])
195
+ pen.el("path", {
196
+ d: `M${n(l + 9)} ${n(top + 4)}H${n(r - 9)}`,
197
+ stroke,
198
+ "stroke-width": width,
199
+ "stroke-linecap": "round",
200
+ });
201
+ return { width: bw + 8, height: b + 4 };
202
+ }
203
+ /** drawPiggy draws a piggy bank whose window holds the money; it gives the window's middle. */
204
+ function drawPiggy(pen, c, a, b) {
205
+ const rx = a + 14;
206
+ const ry = b + 13;
207
+ const cx = rx + 22;
208
+ const cy = ry + 18;
209
+ const body = { fill: c.fill, ...line };
210
+ for (const dx of [-0.58, -0.26, 0.26, 0.58])
211
+ pen.el("rect", {
212
+ x: cx + rx * dx - 10,
213
+ y: cy + ry * 0.72,
214
+ width: 20,
215
+ height: ry * 0.28 + 18,
216
+ rx: 7,
217
+ ...body,
218
+ });
219
+ // The ear stands behind the head, on the right.
220
+ pen.el("path", {
221
+ d: `M${n(cx + rx * 0.38)} ${n(cy - ry * 0.86)}Q${n(cx + rx * 0.5)} ${n(cy - ry - 26)} ${n(cx + rx * 0.66)} ${n(cy - ry - 14)}Q${n(cx + rx * 0.7)} ${n(cy - ry * 0.72)} ${n(cx + rx * 0.7)} ${n(cy - ry * 0.62)}Z`,
222
+ ...body,
223
+ });
224
+ pen.el("path", {
225
+ d: `M${n(cx - rx + 3)} ${n(cy - ry * 0.2)}c-10 -2 -18 -10 -12 -18c6 -7 14 1 6 6c-5 3 -11 0 -14 -4`,
226
+ fill: "none",
227
+ stroke: ink,
228
+ "stroke-width": outline,
229
+ "stroke-linecap": "round",
230
+ });
231
+ pen.el("ellipse", { cx, cy, rx, ry, ...body });
232
+ pen.el("rect", { x: cx - 24, y: cy - ry + 7, width: 48, height: 8, rx: 4, fill: ink });
233
+ pen.el("ellipse", { cx: cx + rx - 2, cy: cy + 4, rx: 14, ry: 19, fill: c.dark, ...line });
234
+ for (const dy of [-6, 8])
235
+ pen.el("circle", { cx: cx + rx - 2, cy: cy + 4 + dy, r: 3, fill: ink });
236
+ pen.el("circle", { cx: cx + rx - 16, cy: cy - 26, r: 4.5, fill: ink });
237
+ pen.el("ellipse", {
238
+ cx: cx - 8,
239
+ cy: cy + 2,
240
+ rx: a,
241
+ ry: b,
242
+ fill: mix(c.fill, "#ffffff", 0.6),
243
+ stroke: c.dark,
244
+ "stroke-width": 2,
245
+ });
246
+ return { width: cx + rx + 18, height: cy + ry + 22, cx: cx - 8, cy: cy + 2 };
247
+ }
248
+ /** drawHand draws an open hand, palm up, its palm w × h; it gives the palm's top left. */
249
+ function drawHand(pen, c, w, h) {
250
+ const pw = Math.max(128, w + 34);
251
+ const ph = h + 34;
252
+ const fingers = 74;
253
+ const left = 40;
254
+ const top = fingers;
255
+ const skin = mix(c.fill, "#ffffff", 0.6);
256
+ const fw = (pw - 10) / 4;
257
+ [0.62, 0.78, 0.72, 0.52].forEach((k, i) => {
258
+ pen.el("rect", {
259
+ x: left + 8 + i * fw,
260
+ y: top - fingers * k,
261
+ width: fw - 6,
262
+ height: fingers * k + 26,
263
+ rx: (fw - 6) / 2,
264
+ fill: skin,
265
+ ...line,
266
+ });
267
+ });
268
+ const ty = top + ph * 0.42;
269
+ pen.el("rect", {
270
+ x: left - 38,
271
+ y: ty - 14,
272
+ width: 64,
273
+ height: 28,
274
+ rx: 14,
275
+ fill: skin,
276
+ transform: `rotate(-38 ${n(left + 12)} ${n(ty)})`,
277
+ ...line,
278
+ });
279
+ pen.el("rect", { x: left, y: top, width: pw, height: ph, rx: 24, fill: skin, ...line });
280
+ return { width: left + pw + 6, height: top + ph + 4, left, top };
281
+ }
282
+ /** drawGroup draws a holder and its money, its top left at 0, 0. */
283
+ function drawGroup(pen, group) {
284
+ const holder = group.holder ?? "none";
285
+ const { laid, w, h } = lay(group);
286
+ const c = colors[group.color ?? defaultColor[holder]];
287
+ const put = (cx, top) => {
288
+ for (const p of laid)
289
+ drawPiece(pen, p.piece, cx + p.x, top + p.y);
290
+ };
291
+ switch (holder) {
292
+ case "none": {
293
+ put(w / 2 + 4, 4);
294
+ return { width: w + 8, height: h + 8 };
295
+ }
296
+ case "purse": {
297
+ const box = drawPurse(pen, c, w, h);
298
+ put(box.width / 2, 26 + 25);
299
+ return box;
300
+ }
301
+ case "piggy": {
302
+ // The money lies in a window of the body: the smallest ellipse round it, a pig's shape.
303
+ const k = 0.7;
304
+ const a = Math.max(64, reach(laid, h, k, 3));
305
+ const box = drawPiggy(pen, c, a, a * k);
306
+ put(box.cx, box.cy - h / 2);
307
+ return box;
308
+ }
309
+ case "hand": {
310
+ const box = drawHand(pen, c, w, h);
311
+ put(box.left + Math.max(128, w + 34) / 2, box.top + 17);
312
+ return box;
313
+ }
314
+ }
315
+ }
316
+ // --- goods on a shelf ----------------------------------------------------------
317
+ function tagWidth(price) {
318
+ return Math.max(46, textWidth(price, 20) + 22);
319
+ }
320
+ function slotWidth(good) {
321
+ const k = good.count ?? 1;
322
+ const things = k === 1 ? goodSize + 18 : k * (copySize + 4) + 14;
323
+ return Math.max(84, things, tagWidth(good.price) + 18);
324
+ }
325
+ function goodsWidth(goods) {
326
+ return goods.reduce((sum, g) => sum + slotWidth(g), 0) + 16;
327
+ }
328
+ /** drawShelf draws the goods on a shelf, tags on its edge, its top left at 0, 0. */
329
+ function drawShelf(pen, goods) {
330
+ const labelled = goods.some((g) => g.label);
331
+ const top = labelled ? 26 : 2;
332
+ const plank = top + goodSize + 4;
333
+ const width = goodsWidth(goods);
334
+ pen.el("rect", {
335
+ x: 0,
336
+ y: plank,
337
+ width,
338
+ height: 14,
339
+ rx: 4,
340
+ fill: colors.brown.fill,
341
+ stroke: ink,
342
+ "stroke-width": outline,
343
+ });
344
+ let x = 8;
345
+ for (const good of goods) {
346
+ const w = slotWidth(good);
347
+ const cx = x + w / 2;
348
+ const k = good.count ?? 1;
349
+ if (k === 1)
350
+ drawThing(pen, good.thing, cx, top + goodSize / 2, goodSize);
351
+ else
352
+ for (let i = 0; i < k; i++)
353
+ drawThing(pen, good.thing, cx + (i - (k - 1) / 2) * (copySize + 4), top + goodSize - copySize / 2, copySize);
354
+ if (good.label)
355
+ pen.text(cx, 12, good.label, { size: 16 });
356
+ const tw = tagWidth(good.price);
357
+ const ask = good.price === "?";
358
+ pen.el("path", {
359
+ d: `M${n(cx)} ${n(plank + 14)}V${n(plank + 20)}`,
360
+ stroke: ink,
361
+ "stroke-width": 2,
362
+ });
363
+ pen.el("rect", {
364
+ x: cx - tw / 2,
365
+ y: plank + 20,
366
+ width: tw,
367
+ height: 32,
368
+ rx: 6,
369
+ fill: ask ? "#ffffff" : colors.yellow.fill,
370
+ stroke: ink,
371
+ "stroke-width": ask ? 2 : outline,
372
+ "stroke-dasharray": ask ? "5 4" : undefined,
373
+ class: "tag",
374
+ });
375
+ pen.text(cx, plank + 36, good.price, { size: 20 });
376
+ x += w;
377
+ }
378
+ return { width, height: plank + 56 };
379
+ }
380
+ // --- the check -------------------------------------------------------------------
381
+ function checkMoney(item, path, problems) {
382
+ if (!isObj(item)) {
383
+ problems.push(`${path}: must be a coin or a note, with a count`);
384
+ return 0;
385
+ }
386
+ only(item, ["coin", "note", "count", "color"], path, problems);
387
+ const forms = ["coin", "note"].filter((f) => item[f] !== undefined);
388
+ if (forms.length !== 1) {
389
+ problems.push(`${path}: must give either coin or note, its value`);
390
+ return 0;
391
+ }
392
+ const form = forms[0];
393
+ const value = item[form];
394
+ if (value !== "?") {
395
+ const most = form === "coin" ? 999 : 9999;
396
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > most)
397
+ problems.push(`${path}.${form}: must be a whole number from 1 to ${most}, or "?"`);
398
+ }
399
+ if (item.color !== undefined && !isColor(item.color))
400
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
401
+ return item.count === undefined ? 1 : int(item.count, `${path}.count`, problems, 1, 20);
402
+ }
403
+ function checkGroup(g, path, problems) {
404
+ if (!isObj(g)) {
405
+ problems.push(`${path}: must be a group of money`);
406
+ return;
407
+ }
408
+ only(g, ["holder", "items", "label", "color"], path, problems);
409
+ const holder = g.holder === undefined ? "none" : oneOf(g.holder, holders, `${path}.holder`, problems);
410
+ if (g.label !== undefined)
411
+ text(g.label, `${path}.label`, problems, 8);
412
+ if (g.color !== undefined && !isColor(g.color))
413
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
414
+ let total = 0;
415
+ let notes = 0;
416
+ list(g.items, `${path}.items`, problems, 1, 6).forEach((item, j) => {
417
+ const k = checkMoney(item, `${path}.items[${j}]`, problems);
418
+ total += k;
419
+ if (isObj(item) && item.note !== undefined)
420
+ notes += k;
421
+ });
422
+ const hold = holds[holder];
423
+ const where = holder === "none" ? "a group without a holder" : article(holderWord[holder]);
424
+ if (total > hold.most)
425
+ problems.push(`${path}.items: at most ${hold.most} coins and notes in ${where}`);
426
+ if (notes > hold.notes)
427
+ problems.push(`${path}.items: at most ${hold.notes} notes in ${where}`);
428
+ }
429
+ function checkGood(g, path, problems) {
430
+ if (!isObj(g)) {
431
+ problems.push(`${path}: must be a thing with a price`);
432
+ return;
433
+ }
434
+ only(g, ["thing", "count", "price", "label"], path, problems);
435
+ checkThing(g.thing, `${path}.thing`, problems);
436
+ if (g.count !== undefined)
437
+ int(g.count, `${path}.count`, problems, 1, 4);
438
+ text(g.price, `${path}.price`, problems, 6);
439
+ if (g.label !== undefined)
440
+ text(g.label, `${path}.label`, problems, 3);
441
+ }
442
+ // --- words -----------------------------------------------------------------------
443
+ const cap = (words) => `${words[0].toUpperCase()}${words.slice(1)}`;
444
+ function sayMoney(group) {
445
+ const parts = group.items.map((item) => {
446
+ const form = item.note !== undefined ? "note" : "coin";
447
+ const value = form === "note" ? item.note : item.coin;
448
+ const k = item.count ?? 1;
449
+ const color = colors[item.color ?? (form === "note" ? "green" : "yellow")].word;
450
+ const what = `${k} ${color} ${form}${k === 1 ? "" : "s"}`;
451
+ return value === "?"
452
+ ? `${what} with a question mark instead of the value`
453
+ : `${what} of ${value}`;
454
+ });
455
+ return parts.length === 1 ? parts[0] : `${parts.slice(0, -1).join(", ")} and ${parts.at(-1)}`;
456
+ }
457
+ function sayGroup(group, i, many) {
458
+ const holder = group.holder ?? "none";
459
+ let name;
460
+ if (group.label)
461
+ name = holder === "none" ? cap(group.label) : `${cap(group.label)}, ${placeWord[holder]}`;
462
+ else if (many)
463
+ name = holder === "none" ? `Group ${i + 1}` : `${cap(holderWord[holder])} ${i + 1}`;
464
+ else
465
+ name = cap(placeWord[holder]);
466
+ return `${name}: ${sayMoney(group)}.`;
467
+ }
468
+ function sayGood(good) {
469
+ const k = good.count ?? 1;
470
+ const what = k === 1 ? article(wordOf(good.thing)) : `${k} ${wordOf(good.thing, k)} together`;
471
+ const price = good.price === "?" ? "a question mark" : good.price;
472
+ return `${good.label ? `${good.label}, ` : ""}${what}: price ${price}`;
473
+ }
474
+ export const coins = {
475
+ kind: "coins",
476
+ members: ["kind", "goods", "groups"],
477
+ check(scene, problems) {
478
+ only(scene, this.members, "scene", problems);
479
+ if (scene.goods === undefined && scene.groups === undefined) {
480
+ problems.push("scene: must give goods, groups of money, or both");
481
+ return;
482
+ }
483
+ if (scene.groups !== undefined)
484
+ list(scene.groups, "groups", problems, 1, 4).forEach((g, i) => {
485
+ checkGroup(g, `groups[${i}]`, problems);
486
+ });
487
+ if (scene.goods !== undefined) {
488
+ const before = problems.length;
489
+ const goods = list(scene.goods, "goods", problems, 1, 6);
490
+ goods.forEach((g, i) => {
491
+ checkGood(g, `goods[${i}]`, problems);
492
+ });
493
+ if (problems.length === before && goodsWidth(goods) > room)
494
+ problems.push("goods: too wide for one shelf; use fewer goods, fewer copies, or shorter prices");
495
+ }
496
+ },
497
+ draw(scene, pen) {
498
+ const s = scene;
499
+ const parts = (s.groups ?? []).map((g) => {
500
+ const sub = pen.sub();
501
+ return { sub, box: drawGroup(sub, g), label: g.label };
502
+ });
503
+ // The groups flow in rows as wide as the picture, their bottoms on one line.
504
+ const lines = [];
505
+ for (const p of parts) {
506
+ const last = lines.at(-1);
507
+ const used = last ? last.reduce((sum, q) => sum + q.box.width + groupGap, 0) : 0;
508
+ if (last && used + p.box.width <= room)
509
+ last.push(p);
510
+ else
511
+ lines.push([p]);
512
+ }
513
+ const widthOf = (row) => row.reduce((sum, q) => sum + q.box.width, 0) + groupGap * (row.length - 1);
514
+ let shelf;
515
+ if (s.goods) {
516
+ const sub = pen.sub();
517
+ shelf = { sub, box: drawShelf(sub, s.goods) };
518
+ }
519
+ const width = Math.max(shelf?.box.width ?? 0, ...lines.map(widthOf)) + 2 * margin;
520
+ let y = margin;
521
+ if (shelf) {
522
+ pen.place(shelf.sub, (width - shelf.box.width) / 2, y);
523
+ y += shelf.box.height + (lines.length > 0 ? 24 : 0);
524
+ }
525
+ lines.forEach((row, r) => {
526
+ const high = Math.max(...row.map((q) => q.box.height));
527
+ const labelled = row.some((q) => q.label);
528
+ let x = (width - widthOf(row)) / 2;
529
+ for (const q of row) {
530
+ pen.place(q.sub, x, y + high - q.box.height);
531
+ if (q.label)
532
+ pen.text(x + q.box.width / 2, y + high + 18, q.label, { size: 18 });
533
+ x += q.box.width + groupGap;
534
+ }
535
+ y += high + (labelled ? 34 : 0) + (r < lines.length - 1 ? 18 : 0);
536
+ });
537
+ return { width, height: y + margin };
538
+ },
539
+ describe(scene) {
540
+ const s = scene;
541
+ const out = [];
542
+ if (s.goods)
543
+ out.push(`Goods on a shelf, each with a price tag: ${s.goods.map(sayGood).join("; ")}.`);
544
+ const groups = s.groups ?? [];
545
+ groups.forEach((g, i) => {
546
+ out.push(sayGroup(g, i, groups.length > 1));
547
+ });
548
+ return out.join(" ");
549
+ },
550
+ };
@@ -0,0 +1,37 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type Thing } from "../things.ts";
3
+ /**
4
+ * Column: a sum, a difference or a product written in columns, the units
5
+ * lined up on the right, some digits hidden. A row is a text of cells (or a
6
+ * whole number): a digit; a letter, drawn as itself or, when masks names it,
7
+ * as the thing masks gives it; "_" an empty box; "*" a star; "?" the digit
8
+ * the question asks. operands are the numbers above the line, the top one
9
+ * first: 2 to 4 for "+", 2 for "-" and "×"; result is the number under it.
10
+ * An operand holds at most 8 cells, the result and a partial at most 9, and
11
+ * all the rows together take at most 9 columns. A product by a number of two
12
+ * or more digits may show partials: one for each digit of the second operand
13
+ * from its last, each one column further left, added under a line of their
14
+ * own. carries writes small carried digits above the columns of a sum,
15
+ * right-aligned, "." for none. facts may give every row in digits: the check
16
+ * then proves that the shown digits agree, that a letter is one digit
17
+ * wherever it stands and different letters are different digits, that the
18
+ * arithmetic holds, and that the carries are the sum's.
19
+ */
20
+ export declare const operators: readonly ["+", "-", "×"];
21
+ export type Operator = (typeof operators)[number];
22
+ export type Row = string | number;
23
+ export type ColumnScene = {
24
+ kind: "column";
25
+ operator: Operator;
26
+ operands: Row[];
27
+ result: Row;
28
+ partials?: Row[];
29
+ masks?: Record<string, Thing>;
30
+ carries?: string;
31
+ facts?: {
32
+ operands: Row[];
33
+ result: Row;
34
+ partials?: Row[];
35
+ };
36
+ };
37
+ export declare const column: Kind;