@altnovatrail/picture-kit 0.0.0-stage → 0.1.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.
Files changed (122) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +7 -0
  3. package/README.md +28 -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,410 @@
1
+ import { margin } from "../kind.js";
2
+ import { isObj, list, oneOf, only } from "../read.js";
3
+ import { colors, ink, outline } from "../style.js";
4
+ import { n } from "../svg.js";
5
+ import { checkThing, drawThing, sameThing, wordOf } from "../things.js";
6
+ import { ordinal, plural, sayList } from "./magic-parts.js";
7
+ /**
8
+ * Column: a sum, a difference or a product written in columns, the units
9
+ * lined up on the right, some digits hidden. A row is a text of cells (or a
10
+ * whole number): a digit; a letter, drawn as itself or, when masks names it,
11
+ * as the thing masks gives it; "_" an empty box; "*" a star; "?" the digit
12
+ * the question asks. operands are the numbers above the line, the top one
13
+ * first: 2 to 4 for "+", 2 for "-" and "×"; result is the number under it.
14
+ * An operand holds at most 8 cells, the result and a partial at most 9, and
15
+ * all the rows together take at most 9 columns. A product by a number of two
16
+ * or more digits may show partials: one for each digit of the second operand
17
+ * from its last, each one column further left, added under a line of their
18
+ * own. carries writes small carried digits above the columns of a sum,
19
+ * right-aligned, "." for none. facts may give every row in digits: the check
20
+ * then proves that the shown digits agree, that a letter is one digit
21
+ * wherever it stands and different letters are different digits, that the
22
+ * arithmetic holds, and that the carries are the sum's.
23
+ */
24
+ export const operators = ["+", "-", "×"];
25
+ const widest = 8;
26
+ const mostColumns = 9;
27
+ const col = 46;
28
+ const rowHeight = 52;
29
+ const carryHeight = 26;
30
+ const digitSize = 30;
31
+ const signs = { "+": "+", "-": "−", "×": "×" };
32
+ const names = {
33
+ "+": { what: "An addition", sign: "a plus sign", result: "the sum" },
34
+ "-": { what: "A subtraction", sign: "a minus sign", result: "the difference" },
35
+ "×": { what: "A multiplication", sign: "a times sign", result: "the product" },
36
+ };
37
+ const places = [
38
+ "units",
39
+ "tens",
40
+ "hundreds",
41
+ "thousands",
42
+ "ten thousands",
43
+ "hundred thousands",
44
+ "millions",
45
+ "ten millions",
46
+ "hundred millions",
47
+ ];
48
+ const textOf = (row) => String(row);
49
+ /** cellsOf splits a row into its cells. */
50
+ const cellsOf = (row) => [...textOf(row)];
51
+ const isLetter = (c) => /^[A-Za-z]$/.test(c);
52
+ function checkRow(value, path, problems, most = widest) {
53
+ if (typeof value === "number") {
54
+ if (!Number.isInteger(value) || value < 0)
55
+ problems.push(`${path}: must be a whole number from 0, or a text of cells`);
56
+ else if (String(value).length > most)
57
+ problems.push(`${path}: at most ${most} digits`);
58
+ return;
59
+ }
60
+ if (typeof value !== "string" || value === "") {
61
+ problems.push(`${path}: must be a text of cells: digits, letters, "_" for an empty box, "*" for a star, "?" for the digit asked`);
62
+ return;
63
+ }
64
+ const bad = [...value].filter((c) => !/^[0-9A-Za-z_*?]$/.test(c));
65
+ if (bad.length > 0)
66
+ problems.push(`${path}: "${bad[0]}" is not a cell; a row holds digits, letters, "_" for an empty box, "*" for a star and "?" for the digit asked`);
67
+ if ([...value].length > most)
68
+ problems.push(`${path}: at most ${most} cells`);
69
+ }
70
+ /** columnsOf is how many columns the rows take, the partials shifted. */
71
+ function columnsOf(s) {
72
+ return Math.max(...s.operands.map((r) => cellsOf(r).length), cellsOf(s.result).length, ...(s.partials ?? []).map((r, i) => cellsOf(r).length + i));
73
+ }
74
+ /** The rows of a scene, each with the path a problem names it by. */
75
+ function rowsOf(s) {
76
+ return [
77
+ ...s.operands.map((row, i) => ({ row, path: `operands[${i}]` })),
78
+ ...(s.partials ?? []).map((row, i) => ({ row, path: `partials[${i}]` })),
79
+ { row: s.result, path: "result" },
80
+ ];
81
+ }
82
+ function digitsOf(value) {
83
+ if (typeof value === "number" && Number.isInteger(value) && value >= 0)
84
+ return String(value);
85
+ if (typeof value === "string" && /^[0-9]+$/.test(value))
86
+ return value;
87
+ return undefined;
88
+ }
89
+ /** carriesOf works out the carries of a sum of digit rows: carries[j] goes into column j from the right. */
90
+ function carriesOf(rows, columns) {
91
+ const out = [0];
92
+ let carry = 0;
93
+ for (let j = 0; j < columns - 1; j++) {
94
+ let sum = carry;
95
+ for (const r of rows)
96
+ sum += Number(r[r.length - 1 - j] ?? "0");
97
+ carry = Math.floor(sum / 10);
98
+ out.push(carry);
99
+ }
100
+ return out;
101
+ }
102
+ function checkFacts(s, facts, problems) {
103
+ if (!isObj(facts)) {
104
+ problems.push("facts: must hold operands and result, in digits");
105
+ return;
106
+ }
107
+ only(facts, ["operands", "result", "partials"], "facts", problems);
108
+ const before = problems.length;
109
+ const given = [];
110
+ const take = (value, shown, path) => {
111
+ const digits = digitsOf(value);
112
+ const want = cellsOf(shown).length;
113
+ if (digits === undefined || digits.length !== want) {
114
+ problems.push(`${path}: must be the ${plural(want, "digit")} of the row it fills`);
115
+ return;
116
+ }
117
+ if (digits.length > 1 && digits[0] === "0")
118
+ problems.push(`${path}: a number of two or more digits does not start with 0`);
119
+ given.push({ shown, digits, path });
120
+ };
121
+ const operands = list(facts.operands, "facts.operands", problems, s.operands.length, s.operands.length);
122
+ operands.forEach((v, i) => {
123
+ take(v, s.operands[i], `facts.operands[${i}]`);
124
+ });
125
+ if (s.partials) {
126
+ const partials = list(facts.partials, "facts.partials", problems, s.partials.length, s.partials.length);
127
+ partials.forEach((v, i) => {
128
+ take(v, s.partials[i], `facts.partials[${i}]`);
129
+ });
130
+ }
131
+ else if (facts.partials !== undefined)
132
+ problems.push("facts.partials: the scene shows no partials");
133
+ take(facts.result, s.result, "facts.result");
134
+ if (problems.length > before)
135
+ return;
136
+ // Every shown digit agrees, and every letter is one digit.
137
+ const letters = new Map();
138
+ for (const { shown, digits, path } of given)
139
+ cellsOf(shown).forEach((c, k) => {
140
+ const d = digits[k];
141
+ if (/[0-9]/.test(c) && c !== d)
142
+ problems.push(`${path}: has ${d} in its ${ordinal(k + 1)} cell, where the picture shows ${c}`);
143
+ if (!isLetter(c))
144
+ return;
145
+ const seen = letters.get(c);
146
+ if (!seen)
147
+ letters.set(c, { digit: d, path });
148
+ else if (seen.digit !== d)
149
+ problems.push(`${path}: makes ${c} ${d}, but ${seen.path} makes it ${seen.digit}`);
150
+ });
151
+ const byDigit = new Map();
152
+ for (const [letter, { digit }] of [...letters].sort(([a], [b]) => (a < b ? -1 : 1))) {
153
+ const other = byDigit.get(digit);
154
+ if (other)
155
+ problems.push(`facts: ${other} and ${letter} are both ${digit}; different letters stand for different digits`);
156
+ else
157
+ byDigit.set(digit, letter);
158
+ }
159
+ if (problems.length > before)
160
+ return;
161
+ const value = (path) => BigInt(given.find((g) => g.path === path).digits);
162
+ const result = value("facts.result");
163
+ const ops = s.operands.map((_, i) => value(`facts.operands[${i}]`));
164
+ const shownOp = signs[s.operator];
165
+ const want = s.operator === "+"
166
+ ? ops.reduce((a, b) => a + b, 0n)
167
+ : s.operator === "-"
168
+ ? ops[0] - ops[1]
169
+ : ops[0] * ops[1];
170
+ if (want !== result)
171
+ problems.push(`facts: ${ops.join(` ${shownOp} `)} is ${want}, not ${result}`);
172
+ if (s.partials) {
173
+ const multiplier = given.find((g) => g.path === "facts.operands[1]").digits;
174
+ s.partials.forEach((_, i) => {
175
+ const d = BigInt(multiplier[multiplier.length - 1 - i]);
176
+ const p = value(`facts.partials[${i}]`);
177
+ if (ops[0] * d !== p)
178
+ problems.push(`facts.partials[${i}]: ${ops[0]} × ${d} is ${ops[0] * d}, not ${p}`);
179
+ });
180
+ }
181
+ if (s.operator === "+" && s.carries !== undefined) {
182
+ const columns = columnsOf(s);
183
+ const real = carriesOf(given.filter((g) => g.path.startsWith("facts.operands")).map((g) => g.digits), columns);
184
+ const shown = [...s.carries].reverse();
185
+ for (let j = 1; j < columns; j++) {
186
+ const c = shown[j] === undefined || shown[j] === "." ? 0 : Number(shown[j]);
187
+ if (c !== real[j])
188
+ problems.push(`carries: the sum carries ${real[j]} into the ${places[j]}, the picture shows ${c === 0 ? "none" : c}`);
189
+ }
190
+ }
191
+ }
192
+ /** a puts "a" or "an" before a word. */
193
+ const a = (word) => `${/^[aeiou]/i.test(word) ? "an" : "a"} ${word}`;
194
+ function sayCell(c, masks) {
195
+ if (/[0-9]/.test(c))
196
+ return c;
197
+ if (c === "_")
198
+ return "an empty box";
199
+ if (c === "*")
200
+ return "a star";
201
+ if (c === "?")
202
+ return "a question mark";
203
+ const thing = masks?.[c];
204
+ return thing === undefined ? `the letter ${c}` : a(wordOf(thing));
205
+ }
206
+ /** drawCell draws one cell of a row, centred at x, y. */
207
+ function drawCell(pen, c, x, y, masks) {
208
+ if (c === "_") {
209
+ pen.el("rect", {
210
+ class: "box",
211
+ x: x - 16,
212
+ y: y - 20,
213
+ width: 32,
214
+ height: 40,
215
+ rx: 6,
216
+ fill: colors.white.fill,
217
+ stroke: ink,
218
+ "stroke-width": outline,
219
+ });
220
+ return;
221
+ }
222
+ if (c === "*") {
223
+ drawThing(pen, { shape: "star", color: "yellow" }, x, y, 40);
224
+ return;
225
+ }
226
+ if (c === "?") {
227
+ drawThing(pen, { ask: true }, x, y, 44);
228
+ return;
229
+ }
230
+ const thing = masks?.[c];
231
+ if (thing !== undefined)
232
+ drawThing(pen, thing, x, y, 40);
233
+ else
234
+ pen.text(x, y, c, { size: digitSize });
235
+ }
236
+ export const column = {
237
+ kind: "column",
238
+ members: ["kind", "operator", "operands", "result", "partials", "masks", "carries", "facts"],
239
+ check(scene, problems) {
240
+ only(scene, this.members, "scene", problems);
241
+ const before = problems.length;
242
+ const operator = oneOf(scene.operator, operators, "operator", problems);
243
+ const operands = list(scene.operands, "operands", problems, 2, 4);
244
+ if (operator !== "+" && operands.length > 2)
245
+ problems.push(`operands: ${names[operator].result.replace("the ", "a ")} has 2 numbers`);
246
+ operands.forEach((r, i) => {
247
+ checkRow(r, `operands[${i}]`, problems);
248
+ });
249
+ checkRow(scene.result, "result", problems, mostColumns);
250
+ if (scene.partials !== undefined) {
251
+ if (operator !== "×")
252
+ problems.push("partials: only a product has partials");
253
+ else {
254
+ const second = operands[1];
255
+ const k = typeof second === "string" || typeof second === "number" ? cellsOf(second).length : 0;
256
+ if (k < 2)
257
+ problems.push("partials: a product by a one-digit number has none; its result is its only line");
258
+ else {
259
+ const partials = Array.isArray(scene.partials) ? scene.partials : [];
260
+ if (partials.length !== k)
261
+ problems.push(`partials: must be a list of ${k} rows, one for each digit of operands[1]`);
262
+ partials.forEach((r, i) => {
263
+ checkRow(r, `partials[${i}]`, problems, mostColumns);
264
+ });
265
+ }
266
+ }
267
+ }
268
+ if (problems.length > before)
269
+ return;
270
+ const typed = scene;
271
+ const columns = columnsOf(typed);
272
+ if (columns > mostColumns)
273
+ problems.push(`partials: shifted one column further left each, they take ${columns} columns; at most ${mostColumns}`);
274
+ const used = new Set(rowsOf(typed).flatMap(({ row }) => cellsOf(row).filter(isLetter)));
275
+ if (scene.masks !== undefined) {
276
+ if (!isObj(scene.masks))
277
+ problems.push("masks: must map letters to things");
278
+ else {
279
+ const seen = [];
280
+ for (const [letter, thing] of Object.entries(scene.masks)) {
281
+ const path = `masks.${letter}`;
282
+ if (!isLetter(letter)) {
283
+ problems.push(`${path}: a mask is named by one letter of a row`);
284
+ continue;
285
+ }
286
+ if (!used.has(letter))
287
+ problems.push(`${path}: no row holds the letter ${letter}`);
288
+ if (typeof thing !== "string" &&
289
+ !(isObj(thing) && ("ball" in thing || "shape" in thing))) {
290
+ problems.push(`${path}: must be an object id, a ball or a shape`);
291
+ continue;
292
+ }
293
+ const before_ = problems.length;
294
+ checkThing(thing, path, problems);
295
+ if (problems.length > before_)
296
+ continue;
297
+ const twin = seen.find((one) => sameThing(one.thing, thing));
298
+ if (twin)
299
+ problems.push(`${path}: looks the same as masks.${twin.letter}; different letters need different things`);
300
+ seen.push({ letter, thing: thing });
301
+ }
302
+ }
303
+ }
304
+ if (scene.carries !== undefined) {
305
+ const c = scene.carries;
306
+ if (operator !== "+")
307
+ problems.push("carries: only a sum shows carries");
308
+ else if (typeof c !== "string" || !/^[1-9.]+$/.test(c))
309
+ problems.push('carries: must be a text of carried digits 1 to 9, "." for none');
310
+ else if (c.length > columns)
311
+ problems.push(`carries: at most ${columns} places, one for each column`);
312
+ else if (!c.endsWith("."))
313
+ problems.push('carries: nothing is carried into the units column; end with "."');
314
+ }
315
+ if (scene.facts !== undefined && problems.length === before)
316
+ checkFacts(typed, scene.facts, problems);
317
+ },
318
+ draw(scene, pen) {
319
+ const s = scene;
320
+ const columns = columnsOf(s);
321
+ const right = margin + (columns + 1) * col;
322
+ const xOf = (j) => right - col / 2 - j * col;
323
+ let y = margin;
324
+ if (s.carries) {
325
+ [...s.carries].reverse().forEach((c, j) => {
326
+ if (c !== ".")
327
+ pen.text(xOf(j), y + carryHeight / 2 + 2, c, { size: 17 });
328
+ });
329
+ y += carryHeight;
330
+ }
331
+ const row = (r, shift) => {
332
+ const cells = cellsOf(r);
333
+ cells.forEach((c, k) => {
334
+ drawCell(pen, c, xOf(cells.length - 1 - k + shift), y + rowHeight / 2, s.masks);
335
+ });
336
+ y += rowHeight;
337
+ };
338
+ const rule = () => {
339
+ pen.el("path", {
340
+ d: `M${n(margin + 4)} ${n(y + 5)}H${n(right - 4)}`,
341
+ stroke: ink,
342
+ "stroke-width": 3,
343
+ "stroke-linecap": "round",
344
+ });
345
+ y += 12;
346
+ };
347
+ // A sign is drawn in strokes, not as text: fonts draw the minus heavier than the plus.
348
+ const signAt = (sign, from, to) => {
349
+ const x = margin + col / 2;
350
+ const y0 = (from + to) / 2;
351
+ const d = sign === "×"
352
+ ? `M${n(x - 7.5)} ${n(y0 - 7.5)}L${n(x + 7.5)} ${n(y0 + 7.5)}M${n(x + 7.5)} ${n(y0 - 7.5)}L${n(x - 7.5)} ${n(y0 + 7.5)}`
353
+ : sign === "+"
354
+ ? `M${n(x - 10)} ${n(y0)}H${n(x + 10)}M${n(x)} ${n(y0 - 10)}V${n(y0 + 10)}`
355
+ : `M${n(x - 10)} ${n(y0)}H${n(x + 10)}`;
356
+ pen.el("path", {
357
+ class: "sign",
358
+ d,
359
+ fill: "none",
360
+ stroke: ink,
361
+ "stroke-width": outline,
362
+ "stroke-linecap": "round",
363
+ });
364
+ };
365
+ const top = y;
366
+ for (const r of s.operands)
367
+ row(r, 0);
368
+ signAt(s.operator, top + rowHeight / 2, y - rowHeight / 2);
369
+ rule();
370
+ if (s.partials) {
371
+ const first = y;
372
+ s.partials.forEach((r, i) => {
373
+ row(r, i);
374
+ });
375
+ signAt("+", first + rowHeight / 2, y - rowHeight / 2);
376
+ rule();
377
+ }
378
+ row(s.result, 0);
379
+ return { width: right + margin, height: y + margin };
380
+ },
381
+ describe(scene) {
382
+ const s = scene;
383
+ const name = names[s.operator];
384
+ const say = (r) => cellsOf(r)
385
+ .map((c) => sayCell(c, s.masks))
386
+ .join(", ");
387
+ const k = s.operands.length;
388
+ const out = [
389
+ `${name.what} in columns, the units lined up on the right: ${plural(k, "number")} one under another with ${name.sign} at the left, a line, and ${name.result} under it.`,
390
+ ];
391
+ if (s.operator === "-")
392
+ out.push(`The number on top: ${say(s.operands[0])}.`, `The number taken away: ${say(s.operands[1])}.`);
393
+ else
394
+ s.operands.forEach((r, i) => {
395
+ out.push(`The ${ordinal(i + 1)} number: ${say(r)}.`);
396
+ });
397
+ if (s.partials)
398
+ out.push(`Under the line, the partial products, each one column further left, with a plus sign at the left: ${s.partials
399
+ .map((r, i) => `the ${ordinal(i + 1)}: ${say(r)}`)
400
+ .join("; ")}; then another line.`);
401
+ out.push(`${name.result[0].toUpperCase()}${name.result.slice(1)}: ${say(s.result)}.`);
402
+ if (s.carries) {
403
+ const marks = [...s.carries]
404
+ .reverse()
405
+ .flatMap((c, j) => (c === "." ? [] : [`${c} above the ${places[j]}`]));
406
+ out.push(`Small carried digits stand above the columns: ${sayList(marks)}.`);
407
+ }
408
+ return out.join(" ");
409
+ },
410
+ };
@@ -0,0 +1,12 @@
1
+ import { type Thing } from "../things.ts";
2
+ /** The smallest text a picture may hold, in pixels. */
3
+ export declare const smallestFont = 14;
4
+ /**
5
+ * smallText tells whether a thing drawn size pixels across would hold text
6
+ * smaller than a picture may: it gives the start of a problem, naming the
7
+ * member that holds the text and the size it would come out at, or undefined.
8
+ * It draws the thing on a pen of its own and reads the sizes back, so that it
9
+ * follows whatever things.ts draws. The kinds commands, branches, lanes and
10
+ * necklace share it.
11
+ */
12
+ export declare function smallText(thing: Thing, size: number, path: string): string | undefined;
@@ -0,0 +1,24 @@
1
+ import { Pen } from "../pen.js";
2
+ import { drawThing } from "../things.js";
3
+ /** The smallest text a picture may hold, in pixels. */
4
+ export const smallestFont = 14;
5
+ /**
6
+ * smallText tells whether a thing drawn size pixels across would hold text
7
+ * smaller than a picture may: it gives the start of a problem, naming the
8
+ * member that holds the text and the size it would come out at, or undefined.
9
+ * It draws the thing on a pen of its own and reads the sizes back, so that it
10
+ * follows whatever things.ts draws. The kinds commands, branches, lanes and
11
+ * necklace share it.
12
+ */
13
+ export function smallText(thing, size, path) {
14
+ const pen = new Pen();
15
+ drawThing(pen, thing, 0, 0, size);
16
+ const sizes = [...pen.markup().matchAll(/font-size="([\d.]+)"/g)].map((m) => Number(m[1]));
17
+ const px = Math.min(...sizes);
18
+ if (px >= smallestFont)
19
+ return undefined;
20
+ const member = typeof thing === "object"
21
+ ? ["label", "card", "weight"].find((k) => k in thing)
22
+ : undefined;
23
+ return `${path}${member ? `.${member}` : ""}: its text would be ${Math.floor(px * 10) / 10} px, less than ${smallestFont}`;
24
+ }
@@ -0,0 +1,66 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type Thing } from "../things.ts";
3
+ /**
4
+ * Commands: a program, a strip of command tiles read from the left — arrows
5
+ * that move one cell up, down, left or right whatever way the walker faces,
6
+ * or blocks that step forward, jump, turn left or turn right from the way it
7
+ * faces —, with a repeat drawn as a frame round the commands it repeats and
8
+ * an if-block as a frame with a thing to check, a row for yes (✓) and a row
9
+ * for no (✗). Beside or under the strip may stand a board: the walker (the
10
+ * robot or an animal) on its start, a pointer on the side it faces, a
11
+ * target, obstacles, other things, cells marked with letters and a drawn
12
+ * path. A place is [row, column], counted from 1 at the top left.
13
+ * {"ask": true} stands for a command, or a repeat's count, that the question
14
+ * asks.
15
+ */
16
+ export type At = [number, number];
17
+ export declare const facings: readonly ["up", "right", "down", "left"];
18
+ export type Facing = (typeof facings)[number];
19
+ /** arrows move one cell the way they point, whatever way the walker faces. */
20
+ export declare const arrows: readonly ["up", "down", "left", "right"];
21
+ /** blocks act from the way the walker faces. */
22
+ export declare const blocks: readonly ["step", "jump", "turn-left", "turn-right"];
23
+ export type Move = (typeof arrows)[number] | (typeof blocks)[number];
24
+ export type Ask = {
25
+ ask: true;
26
+ };
27
+ export type Repeat = {
28
+ repeat: number | Ask;
29
+ do: Command[];
30
+ };
31
+ export type Branch = {
32
+ if: Thing;
33
+ yes: Command[];
34
+ no?: Command[];
35
+ };
36
+ export type Command = Move | Ask | Repeat | Branch;
37
+ export type Spot = {
38
+ at: At;
39
+ thing: Thing;
40
+ };
41
+ export type Board = {
42
+ rows: number;
43
+ cols: number;
44
+ target?: Spot;
45
+ obstacles?: {
46
+ at: At;
47
+ thing?: Thing;
48
+ }[];
49
+ things?: Spot[];
50
+ marks?: {
51
+ at: At;
52
+ label: string;
53
+ }[];
54
+ path?: At[];
55
+ };
56
+ export type CommandsScene = {
57
+ kind: "commands";
58
+ walker?: string;
59
+ start?: {
60
+ at?: At;
61
+ facing?: Facing;
62
+ };
63
+ board?: Board;
64
+ commands?: Command[];
65
+ };
66
+ export declare const commands: Kind;