@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,26 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Entry } from "./magic-parts.ts";
4
+ /**
5
+ * Pyramid: a number pyramid, a wall of bricks. rows go from the top down,
6
+ * each one brick longer than the row above it, so that every brick rests on
7
+ * two bricks of the row below; the top row may hold more than one brick. A
8
+ * brick holds an entry: a number, a short text, nothing, a thing, or the "?"
9
+ * of the question. rule names how a brick comes from the two under it, sum
10
+ * or product; showRule draws a key with it at the top left. facts may give
11
+ * every brick's value: the check then proves that the shown numbers agree and
12
+ * that every brick above the bottom row follows the rule.
13
+ */
14
+ export declare const rules: readonly ["sum", "product"];
15
+ export type Rule = (typeof rules)[number];
16
+ export type PyramidScene = {
17
+ kind: "pyramid";
18
+ rows: Entry[][];
19
+ rule?: Rule;
20
+ showRule?: boolean;
21
+ color?: ColorName;
22
+ facts?: {
23
+ values: number[][];
24
+ };
25
+ };
26
+ export declare const pyramid: Kind;
@@ -0,0 +1,228 @@
1
+ import { margin } from "../kind.js";
2
+ import { int, isObj, list, oneOf, only } from "../read.js";
3
+ import { colorNames, colors, ink, isColor, outline, soft } from "../style.js";
4
+ import { checkEntry, contentOf, drawContent, numberOf, ordinal, sayEntry, } from "./magic-parts.js";
5
+ /**
6
+ * Pyramid: a number pyramid, a wall of bricks. rows go from the top down,
7
+ * each one brick longer than the row above it, so that every brick rests on
8
+ * two bricks of the row below; the top row may hold more than one brick. A
9
+ * brick holds an entry: a number, a short text, nothing, a thing, or the "?"
10
+ * of the question. rule names how a brick comes from the two under it, sum
11
+ * or product; showRule draws a key with it at the top left. facts may give
12
+ * every brick's value: the check then proves that the shown numbers agree and
13
+ * that every brick above the bottom row follows the rule.
14
+ */
15
+ export const rules = ["sum", "product"];
16
+ const limits = { biggest: 9999, chars: 4 };
17
+ const most = 8;
18
+ const brickHeight = 46;
19
+ const room = 640 - 2 * margin;
20
+ // The key: a small pyramid of three bricks in a soft frame.
21
+ const keyBrick = { w: 48, h: 28 };
22
+ const keyPad = 8;
23
+ const keyWidth = 2 * keyBrick.w + 2 * keyPad;
24
+ const keyHeight = 2 * keyBrick.h + 2 * keyPad;
25
+ const sign = { sum: "+", product: "×" };
26
+ function apply(rule, a, b) {
27
+ return rule === "sum" ? a + b : a * b;
28
+ }
29
+ /** layout finds the widest bricks that fit, and how far the key pushes the wall right. */
30
+ function layout(s) {
31
+ const rows = s.rows.length;
32
+ const bottom = s.rows[rows - 1].length;
33
+ const top = s.rows[0].length;
34
+ for (let w = 80;; w -= 2) {
35
+ let shift = 0;
36
+ if (s.showRule)
37
+ for (let i = 0; i < rows && i * brickHeight < keyHeight + 12; i++)
38
+ shift = Math.max(shift, keyWidth + 14 - ((bottom - top - i) * w) / 2);
39
+ const width = Math.max(shift + bottom * w, s.showRule ? keyWidth : 0);
40
+ if (width <= room || w <= 50)
41
+ return { w, shift, width, height: rows * brickHeight };
42
+ }
43
+ }
44
+ /** textFor is the size of a brick's text: as big as the brick allows, at most 24. */
45
+ function textFor(text, w) {
46
+ return Math.min(24, (w - 12) / (0.62 * Math.max(2, [...text].length)));
47
+ }
48
+ function drawKey(pen, rule) {
49
+ pen.el("rect", {
50
+ x: 0,
51
+ y: 0,
52
+ width: keyWidth,
53
+ height: keyHeight,
54
+ rx: 10,
55
+ fill: soft,
56
+ });
57
+ const { w, h } = keyBrick;
58
+ const bricks = [
59
+ [keyPad + w / 2, keyPad, `a ${sign[rule]} b`],
60
+ [keyPad, keyPad + h, "a"],
61
+ [keyPad + w, keyPad + h, "b"],
62
+ ];
63
+ for (const [x, y, words] of bricks) {
64
+ pen.el("rect", {
65
+ x,
66
+ y,
67
+ width: w,
68
+ height: h,
69
+ rx: 4,
70
+ fill: colors.white.fill,
71
+ stroke: ink,
72
+ "stroke-width": 2,
73
+ });
74
+ pen.text(x + w / 2, y + h / 2, words, { size: 15, weight: 600 });
75
+ }
76
+ }
77
+ export const pyramid = {
78
+ kind: "pyramid",
79
+ members: ["kind", "rows", "rule", "showRule", "color", "facts"],
80
+ check(scene, problems) {
81
+ only(scene, this.members, "scene", problems);
82
+ const before = problems.length;
83
+ const rows = list(scene.rows, "rows", problems, 2, most);
84
+ let top = 0;
85
+ rows.forEach((row, i) => {
86
+ const path = `rows[${i}]`;
87
+ if (!Array.isArray(row)) {
88
+ problems.push(`${path}: must be a list of bricks`);
89
+ return;
90
+ }
91
+ if (i === 0) {
92
+ top = row.length;
93
+ if (row.length < 1)
94
+ problems.push(`${path}: must hold at least 1 brick`);
95
+ }
96
+ else if (row.length !== top + i)
97
+ problems.push(`${path}: must hold ${top + i} bricks, one more than rows[${i - 1}], so that each brick rests on two`);
98
+ row.forEach((e, j) => {
99
+ checkEntry(e, `${path}[${j}]`, problems, limits);
100
+ });
101
+ });
102
+ if (rows.length > 0 && top + rows.length - 1 > most && problems.length === before)
103
+ problems.push(`rows: the bottom row may hold at most ${most} bricks`);
104
+ let rule;
105
+ if (scene.rule !== undefined)
106
+ rule = oneOf(scene.rule, rules, "rule", problems);
107
+ if (scene.showRule !== undefined && typeof scene.showRule !== "boolean")
108
+ problems.push("showRule: must be true or false");
109
+ if (scene.showRule === true && scene.rule === undefined)
110
+ problems.push("showRule: needs a rule to show, sum or product");
111
+ if (scene.color !== undefined && !isColor(scene.color))
112
+ problems.push(`color: must be a colour: ${colorNames.join(", ")}`);
113
+ if (problems.length > before || rule === undefined) {
114
+ if (scene.facts !== undefined && scene.rule === undefined)
115
+ problems.push("facts: needs the rule the bricks follow, sum or product");
116
+ return;
117
+ }
118
+ const typed = scene;
119
+ const k = typed.rows.length;
120
+ // Whatever the picture shows already must follow the rule.
121
+ for (let i = 0; i < k - 1; i++)
122
+ typed.rows[i].forEach((e, j) => {
123
+ const v = numberOf(e);
124
+ const a = numberOf(typed.rows[i + 1][j]);
125
+ const b = numberOf(typed.rows[i + 1][j + 1]);
126
+ if (v === undefined || a === undefined || b === undefined)
127
+ return;
128
+ if (apply(rule, a, b) !== v)
129
+ problems.push(`rows[${i}][${j}]: ${v} is not the ${rule} of ${a} and ${b} under it`);
130
+ });
131
+ if (scene.facts === undefined || problems.length > before)
132
+ return;
133
+ if (!isObj(scene.facts)) {
134
+ problems.push("facts: must hold values, row by row as rows");
135
+ return;
136
+ }
137
+ only(scene.facts, ["values"], "facts", problems);
138
+ const values = list(scene.facts.values, "facts.values", problems, k, k);
139
+ if (values.length !== k)
140
+ return;
141
+ values.forEach((row, i) => {
142
+ const width = typed.rows[i]?.length ?? 0;
143
+ list(row, `facts.values[${i}]`, problems, width, width).forEach((v, j) => {
144
+ int(v, `facts.values[${i}][${j}]`, problems, 0, 999999);
145
+ });
146
+ });
147
+ if (problems.length > before)
148
+ return;
149
+ const known = values;
150
+ typed.rows.forEach((row, i) => {
151
+ row.forEach((e, j) => {
152
+ const shown = numberOf(e);
153
+ const v = known[i][j];
154
+ if (shown !== undefined && shown !== v)
155
+ problems.push(`facts.values[${i}][${j}]: rows[${i}][${j}] shows ${shown}, not ${v}`);
156
+ if (i === k - 1)
157
+ return;
158
+ const a = known[i + 1][j];
159
+ const b = known[i + 1][j + 1];
160
+ if (apply(rule, a, b) !== v)
161
+ problems.push(`facts.values[${i}][${j}]: ${v} is not the ${rule} of ${a} and ${b} under it`);
162
+ });
163
+ });
164
+ },
165
+ draw(scene, pen) {
166
+ const s = scene;
167
+ const { w, shift, width, height } = layout(s);
168
+ const bottom = s.rows[s.rows.length - 1].length;
169
+ const base = s.color ?? "white";
170
+ if (s.showRule && s.rule) {
171
+ const key = pen.sub();
172
+ drawKey(key, s.rule);
173
+ pen.place(key, margin, margin);
174
+ }
175
+ s.rows.forEach((row, i) => {
176
+ const y = margin + i * brickHeight;
177
+ const left = margin + shift + ((bottom - row.length) * w) / 2;
178
+ row.forEach((e, j) => {
179
+ const x = left + j * w;
180
+ const c = contentOf(e);
181
+ const fill = c.fill ?? base;
182
+ pen.el("rect", {
183
+ class: "brick",
184
+ x,
185
+ y,
186
+ width: w,
187
+ height: brickHeight,
188
+ rx: 5,
189
+ fill: colors[fill].fill,
190
+ stroke: ink,
191
+ "stroke-width": outline,
192
+ "stroke-linejoin": "round",
193
+ });
194
+ drawContent(pen, e, x + w / 2, y + brickHeight / 2, brickHeight, {
195
+ base,
196
+ ...(c.text !== undefined ? { text: textFor(c.text, w) } : {}),
197
+ });
198
+ });
199
+ });
200
+ return { width: width + 2 * margin, height: height + 2 * margin };
201
+ },
202
+ describe(scene) {
203
+ const s = scene;
204
+ const k = s.rows.length;
205
+ const top = s.rows[0].length;
206
+ const bottom = s.rows[k - 1].length;
207
+ const total = s.rows.reduce((sum, row) => sum + row.length, 0);
208
+ const out = [
209
+ `A wall of ${total} bricks in ${k} rows, from ${bottom} at the bottom to ${top} at the top; each brick rests on two bricks of the row below.`,
210
+ ];
211
+ if (s.color && s.color !== "white")
212
+ out.push(`The bricks are ${colors[s.color].word}.`);
213
+ for (let i = k - 1; i >= 0; i--) {
214
+ const words = s.rows[i].map(sayEntry).join(", ");
215
+ const name = i === k - 1
216
+ ? "The bottom row, from left to right"
217
+ : i === 0
218
+ ? top === 1
219
+ ? "The top brick"
220
+ : "The top row"
221
+ : `The ${ordinal(k - i)} row from the bottom`;
222
+ out.push(`${name}: ${words}.`);
223
+ }
224
+ if (s.showRule && s.rule)
225
+ out.push(`A key at the top left shows the rule: the brick on two bricks a and b holds a ${sign[s.rule]} b.`);
226
+ return out.join(" ");
227
+ },
228
+ };
@@ -0,0 +1,30 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * Ropes: ropes and strings laid on a table, each a smooth line through points
6
+ * of a grid, open with two ends or closed in a loop, and pegs standing on the
7
+ * table. Ropes cross only at a point that two passes share, and there the
8
+ * scene says which strand goes under; the picture draws the strand on top
9
+ * whole and breaks the one below, so every crossing reads one way. An end
10
+ * may carry a thing — a ball of a colour, a child, a kite.
11
+ */
12
+ export type Point = [number, number] | [number, number, "under"];
13
+ export type Rope = {
14
+ path: Point[];
15
+ color?: ColorName;
16
+ closed?: boolean;
17
+ start?: Thing;
18
+ end?: Thing;
19
+ label?: string;
20
+ };
21
+ export type Peg = {
22
+ at: [number, number];
23
+ label?: string;
24
+ };
25
+ export type RopesScene = {
26
+ kind: "ropes";
27
+ ropes: Rope[];
28
+ pegs?: Peg[];
29
+ };
30
+ export declare const ropes: Kind;