@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,473 @@
1
+ import { margin } from "../kind.js";
2
+ import { textWidth } from "../pen.js";
3
+ import { isObj, list, oneOf, only, text } from "../read.js";
4
+ import { colorNames, colors, ground, ink, isColor, outline, textOn, } from "../style.js";
5
+ import { n } from "../svg.js";
6
+ import { checkThing, drawThing, sameThing, wordOf } from "../things.js";
7
+ import { smallText } from "./commands-parts.js";
8
+ export const grows = ["down", "up", "right", "left"];
9
+ export const lineStyles = ["straight", "elbow"];
10
+ const mostNodes = 31;
11
+ const mostLevels = 6;
12
+ const mostChildren = 5;
13
+ const wide = 616;
14
+ const tall = 600;
15
+ /** Two looks: the usual one, and a tighter one for a tree that does not fit it; under is the size of a label under a thing. */
16
+ const looks = [
17
+ { thing: 44, font: 16, under: 15, sibling: 12 },
18
+ { thing: 34, font: 14, under: 14, sibling: 8 },
19
+ ];
20
+ const pillFont = 14;
21
+ const pillH = 22;
22
+ function nodeSize(node, look) {
23
+ if (node.ask)
24
+ return { w: look.thing, h: look.thing };
25
+ if (node.thing !== undefined && node.label !== undefined)
26
+ return {
27
+ w: Math.max(look.thing + 2, textWidth(node.label, look.under) + 8),
28
+ h: look.thing + look.font + 6,
29
+ };
30
+ if (node.thing !== undefined)
31
+ return { w: look.thing + 2, h: look.thing + 2 };
32
+ return {
33
+ w: Math.max(look.thing, textWidth(node.label ?? "", look.font) + 20),
34
+ h: look.font * 2 + 2,
35
+ };
36
+ }
37
+ function pillSize(label) {
38
+ return { w: Math.max(26, textWidth(label, pillFont) + 12), h: pillH };
39
+ }
40
+ /** layout places every node: levels along the way the tree grows, each subtree in a band of its own across it. */
41
+ function layout(s, look) {
42
+ const grow = s.grow ?? "down";
43
+ const across = grow === "right" || grow === "left";
44
+ const elbow = (s.lines ?? (s.bracket ? "elbow" : "straight")) === "elbow";
45
+ const placed = [];
46
+ const build = (node, path, depth, parent) => {
47
+ const size = nodeSize(node, look);
48
+ const p = {
49
+ node,
50
+ path,
51
+ depth,
52
+ children: [],
53
+ box: { x: 0, y: 0, w: size.w, h: size.h },
54
+ out: { x: 0, y: 0 },
55
+ in: { x: 0, y: 0 },
56
+ line: [],
57
+ ...(parent ? { parent } : {}),
58
+ };
59
+ placed.push(p);
60
+ p.children = (node.children ?? []).map((c, i) => build(c, `${path}.children[${i}]`, depth + 1, p));
61
+ return p;
62
+ };
63
+ const root = build(s.root, "root", 0);
64
+ const main = (p) => (across ? p.box.w : p.box.h);
65
+ const cross = (p) => (across ? p.box.h : p.box.w);
66
+ /** room is the breadth a node keeps for itself: its own, or more for the label on its line, which stands partway along a straight line and on the last leg of an elbow. */
67
+ const room = (p) => {
68
+ if (p.node.branch === undefined)
69
+ return cross(p);
70
+ const size = pillSize(p.node.branch);
71
+ const pill = across ? size.h : size.w;
72
+ return Math.max(cross(p), elbow ? pill + 6 : 2 * pill + 6);
73
+ };
74
+ const levels = Math.max(...placed.map((p) => p.depth)) + 1;
75
+ const levelMain = Array.from({ length: levels }, (_, d) => Math.max(...placed.filter((p) => p.depth === d).map(main)));
76
+ const gapAfter = Array.from({ length: levels - 1 }, (_, d) => {
77
+ const pills = placed
78
+ .filter((p) => p.depth === d + 1 && p.node.branch !== undefined)
79
+ .map((p) => pillSize(p.node.branch ?? ""));
80
+ if (pills.length === 0)
81
+ return across ? 46 : 40;
82
+ return across ? Math.max(...pills.map((q) => q.w)) + 34 : pillH + 40;
83
+ });
84
+ const levelAt = [];
85
+ let at = 0;
86
+ for (let d = 0; d < levels; d++) {
87
+ levelAt.push(at);
88
+ at += levelMain[d] + (gapAfter[d] ?? 0);
89
+ }
90
+ const length = at;
91
+ const extent = new Map();
92
+ const measure = (p) => {
93
+ const kids = p.children.map(measure);
94
+ const sum = kids.reduce((a, b) => a + b, 0) + look.sibling * Math.max(0, kids.length - 1);
95
+ const e = Math.max(room(p), sum);
96
+ extent.set(p, e);
97
+ return e;
98
+ };
99
+ const breadth = measure(root);
100
+ const centre = new Map();
101
+ const place = (p, from) => {
102
+ const e = extent.get(p);
103
+ const kids = p.children;
104
+ const sum = kids.reduce((a, k) => a + extent.get(k), 0) + look.sibling * Math.max(0, kids.length - 1);
105
+ let x = from + (e - sum) / 2;
106
+ for (const k of kids) {
107
+ place(k, x);
108
+ x += extent.get(k) + look.sibling;
109
+ }
110
+ const half = cross(p) / 2;
111
+ const c = kids.length > 0
112
+ ? (centre.get(kids[0]) + centre.get(kids[kids.length - 1])) / 2
113
+ : from + e / 2;
114
+ centre.set(p, Math.min(from + e - half, Math.max(from + half, c)));
115
+ };
116
+ place(root, 0);
117
+ // From (main, cross) to the picture's x, y, margins included.
118
+ const width = across ? length : breadth;
119
+ const height = across ? breadth : length;
120
+ const to = (m, c) => {
121
+ if (grow === "down")
122
+ return { x: margin + c, y: margin + m };
123
+ if (grow === "up")
124
+ return { x: margin + c, y: margin + length - m };
125
+ if (grow === "right")
126
+ return { x: margin + m, y: margin + c };
127
+ return { x: margin + length - m, y: margin + c };
128
+ };
129
+ for (const p of placed) {
130
+ const mid = levelAt[p.depth] + levelMain[p.depth] / 2;
131
+ const c = to(mid, centre.get(p));
132
+ p.box.x = c.x - p.box.w / 2;
133
+ p.box.y = c.y - p.box.h / 2;
134
+ const half = main(p) / 2;
135
+ p.out = to(mid + half, centre.get(p));
136
+ p.in = to(mid - half, centre.get(p));
137
+ }
138
+ for (const p of placed) {
139
+ const parent = p.parent;
140
+ if (!parent)
141
+ continue;
142
+ if (elbow) {
143
+ const bar = levelAt[parent.depth] + levelMain[parent.depth] + gapAfter[parent.depth] / 2;
144
+ const a = to(bar, centre.get(parent));
145
+ const b = to(bar, centre.get(p));
146
+ p.line = [parent.out, a, b, p.in];
147
+ }
148
+ else
149
+ p.line = [parent.out, p.in];
150
+ if (p.node.branch !== undefined) {
151
+ const size = pillSize(p.node.branch);
152
+ // On a straight line the label stands a little nearer the child, whose branch it names.
153
+ const [u, v] = elbow ? [p.line[2], p.line[3]] : [p.line[0], p.line[1]];
154
+ const t = elbow ? 0.5 : 0.58;
155
+ const mx = u.x + (v.x - u.x) * t;
156
+ const my = u.y + (v.y - u.y) * t;
157
+ p.pill = { x: mx - size.w / 2, y: my - size.h / 2, w: size.w, h: size.h };
158
+ }
159
+ }
160
+ return { placed, width: width + 2 * margin, height: height + 2 * margin };
161
+ }
162
+ function overlaps(a, b, room = 0) {
163
+ return (a.x < b.x + b.w + room &&
164
+ b.x < a.x + a.w + room &&
165
+ a.y < b.y + b.h + room &&
166
+ b.y < a.y + a.h + room);
167
+ }
168
+ /** crosses tells whether a line through the points runs through a box. */
169
+ function crosses(points, box) {
170
+ for (let i = 0; i + 1 < points.length; i++) {
171
+ const a = points[i];
172
+ const b = points[i + 1];
173
+ for (let t = 0; t <= 1; t += 1 / 40) {
174
+ const x = a.x + (b.x - a.x) * t;
175
+ const y = a.y + (b.y - a.y) * t;
176
+ if (x > box.x + 1 && x < box.x + box.w - 1 && y > box.y + 1 && y < box.y + box.h - 1)
177
+ return true;
178
+ }
179
+ }
180
+ return false;
181
+ }
182
+ /** thingSize is the size a node's thing is drawn at in a look. */
183
+ function thingSize(look) {
184
+ return look.thing - 2;
185
+ }
186
+ /** troubles lists what a tree drawn in a look would get wrong: too big, text too small, or labels on lines that cover something. */
187
+ function troubles(s, tree, look) {
188
+ const out = [];
189
+ for (const p of tree.placed) {
190
+ if (p.node.thing === undefined || p.node.ask)
191
+ continue;
192
+ const small = smallText(p.node.thing, thingSize(look), `${p.path}.thing`);
193
+ if (small)
194
+ out.push(`${small}; write it as the node's label instead`);
195
+ }
196
+ const w = tree.width - 2 * margin;
197
+ const h = tree.height - 2 * margin;
198
+ if (w > wide)
199
+ out.push(`root: the tree is ${Math.round(w)} px wide, more than ${wide}; use fewer nodes across, shorter labels${s.grow === "right" || s.grow === "left" ? "" : ', or "grow": "right"'}`);
200
+ if (h > tall)
201
+ out.push(`root: the tree is ${Math.round(h)} px tall, more than ${tall}; use fewer levels or fewer nodes${s.grow === "right" || s.grow === "left" ? ' across, or "grow": "down"' : ""}`);
202
+ const pills = tree.placed.filter((p) => p.pill);
203
+ for (const p of pills) {
204
+ const box = p.pill;
205
+ const covered = pills.some((q) => q !== p && tree.placed.indexOf(q) < tree.placed.indexOf(p) && overlaps(q.pill, box, 2)) ||
206
+ tree.placed.some((q) => overlaps(q.box, box, 2)) ||
207
+ tree.placed.some((q) => q !== p && q.line.length > 0 && crosses(q.line, box));
208
+ if (covered)
209
+ out.push(`${p.path}.branch: the label "${p.node.branch}" would cover another label, line or node; shorten it, or the labels beside it`);
210
+ }
211
+ return out;
212
+ }
213
+ function pickLook(s) {
214
+ let last;
215
+ for (const look of looks) {
216
+ const tree = layout(s, look);
217
+ const problems = troubles(s, tree, look);
218
+ last = { tree, look, problems };
219
+ if (problems.length === 0)
220
+ break;
221
+ }
222
+ return last;
223
+ }
224
+ function checkNode(value, path, depth, bracket, problems, counter) {
225
+ if (!isObj(value)) {
226
+ problems.push(`${path}: must be a node: an object with a label, a thing or ask`);
227
+ return;
228
+ }
229
+ counter.nodes++;
230
+ only(value, ["label", "thing", "color", "ask", "branch", "children"], path, problems);
231
+ if (value.ask !== undefined) {
232
+ if (value.ask !== true)
233
+ problems.push(`${path}.ask: must be true`);
234
+ else if (value.label !== undefined || value.thing !== undefined || value.color !== undefined)
235
+ problems.push(`${path}: a node that asks shows "?" alone, with no label, thing or color`);
236
+ }
237
+ else if (value.label === undefined && value.thing === undefined)
238
+ problems.push(`${path}: needs a label, a thing or "ask": true`);
239
+ if (value.label !== undefined) {
240
+ const label = text(value.label, `${path}.label`, problems, 16);
241
+ if (label === "?")
242
+ problems.push(`${path}.label: write "ask": true for the node the question asks`);
243
+ }
244
+ if (value.thing !== undefined) {
245
+ if (isObj(value.thing) && (value.thing.ask === true || value.thing.empty === true))
246
+ problems.push(`${path}.thing: write "ask": true on the node for "?"; a node is never empty`);
247
+ else
248
+ checkThing(value.thing, `${path}.thing`, problems);
249
+ }
250
+ if (value.color !== undefined) {
251
+ if (!isColor(value.color))
252
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
253
+ else if (value.thing !== undefined || value.label === undefined)
254
+ problems.push(`${path}.color: only a box with a label and no thing has a colour`);
255
+ }
256
+ if (value.branch !== undefined) {
257
+ if (depth === 0)
258
+ problems.push("root.branch: the root has no line above it to label");
259
+ else
260
+ text(value.branch, `${path}.branch`, problems, 8);
261
+ }
262
+ if (value.children === undefined)
263
+ return;
264
+ if (depth + 1 >= mostLevels) {
265
+ problems.push(`${path}.children: at most ${mostLevels} levels, the root's included`);
266
+ return;
267
+ }
268
+ const kids = list(value.children, `${path}.children`, problems, 1, mostChildren);
269
+ if (bracket && kids.length !== 2)
270
+ problems.push(`${path}.children: a match in a bracket has two players, not ${kids.length}`);
271
+ kids.forEach((k, i) => {
272
+ checkNode(k, `${path}.children[${i}]`, depth + 1, bracket, problems, counter);
273
+ });
274
+ }
275
+ function nodesOf(root) {
276
+ const out = [];
277
+ const queue = [{ node: root, path: "root", depth: 0 }];
278
+ while (queue.length > 0) {
279
+ const next = queue.shift();
280
+ out.push(next);
281
+ (next.node.children ?? []).forEach((c, i) => {
282
+ queue.push({ node: c, path: `${next.path}.children[${i}]`, depth: next.depth + 1 });
283
+ });
284
+ }
285
+ return out;
286
+ }
287
+ function same(a, b) {
288
+ return (a.label === b.label &&
289
+ ((a.thing === undefined && b.thing === undefined) ||
290
+ (a.thing !== undefined && b.thing !== undefined && sameThing(a.thing, b.thing))));
291
+ }
292
+ function leavesOf(node) {
293
+ return node.children ? node.children.flatMap(leavesOf) : [node];
294
+ }
295
+ /** name says a node: its label in quotes, its thing, both, or "?". */
296
+ function name(node) {
297
+ if (node.ask)
298
+ return "?";
299
+ const thing = node.thing === undefined ? undefined : wordOf(node.thing);
300
+ if (node.label !== undefined)
301
+ return `"${node.label}"${thing ? ` (${thing})` : node.color ? ` (${colors[node.color].word})` : ""}`;
302
+ return thing ?? "";
303
+ }
304
+ const ordinals = ["first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth"];
305
+ export const branches = {
306
+ kind: "branches",
307
+ members: ["kind", "root", "grow", "lines", "bracket"],
308
+ check(scene, problems) {
309
+ only(scene, this.members, "scene", problems);
310
+ if (scene.grow !== undefined)
311
+ oneOf(scene.grow, grows, "grow", problems);
312
+ if (scene.lines !== undefined)
313
+ oneOf(scene.lines, lineStyles, "lines", problems);
314
+ if (scene.bracket !== undefined && typeof scene.bracket !== "boolean")
315
+ problems.push("bracket: must be true or false");
316
+ const bracket = scene.bracket === true;
317
+ const counter = { nodes: 0 };
318
+ if (scene.root === undefined)
319
+ problems.push("root: must be the top node of the tree");
320
+ else
321
+ checkNode(scene.root, "root", 0, bracket, problems, counter);
322
+ if (counter.nodes > mostNodes)
323
+ problems.push(`root: at most ${mostNodes} nodes in a tree; there are ${counter.nodes}`);
324
+ if (problems.length > 0)
325
+ return;
326
+ const s = scene;
327
+ if (bracket) {
328
+ if (!s.root.children)
329
+ problems.push("root.children: a bracket needs at least one match");
330
+ const leaves = leavesOf(s.root).filter((l) => !l.ask);
331
+ leaves.forEach((l, i) => {
332
+ if (leaves.findIndex((m) => same(m, l)) < i)
333
+ problems.push(`root: two players are both ${name(l)}; give each player a label or thing of its own`);
334
+ });
335
+ for (const { node, path } of nodesOf(s.root)) {
336
+ const kids = node.children;
337
+ if (!kids || node.ask)
338
+ continue;
339
+ if (kids.every((k) => !k.ask) && !kids.some((k) => same(k, node)))
340
+ problems.push(`${path}: the winner of a match is one of its two players: ${kids.map(name).join(" or ")}`);
341
+ }
342
+ }
343
+ if (problems.length > 0)
344
+ return;
345
+ problems.push(...pickLook(s).problems);
346
+ },
347
+ draw(scene, pen) {
348
+ const s = scene;
349
+ const { tree, look } = pickLook(s);
350
+ for (const p of tree.placed) {
351
+ if (p.line.length === 0)
352
+ continue;
353
+ pen.el("path", {
354
+ d: p.line.map((q, i) => `${i === 0 ? "M" : "L"}${n(q.x)} ${n(q.y)}`).join(""),
355
+ fill: "none",
356
+ stroke: ink,
357
+ "stroke-width": outline,
358
+ "stroke-linejoin": "round",
359
+ "stroke-linecap": "round",
360
+ class: "edge",
361
+ });
362
+ }
363
+ for (const p of tree.placed) {
364
+ if (!p.pill)
365
+ continue;
366
+ const b = p.pill;
367
+ pen.el("rect", {
368
+ x: b.x,
369
+ y: b.y,
370
+ width: b.w,
371
+ height: b.h,
372
+ rx: b.h / 2,
373
+ fill: ground,
374
+ stroke: ink,
375
+ "stroke-width": 1.6,
376
+ class: "branch",
377
+ });
378
+ pen.text(b.x + b.w / 2, b.y + b.h / 2, p.node.branch ?? "", { size: pillFont });
379
+ }
380
+ for (const p of tree.placed) {
381
+ const node = p.node;
382
+ const b = p.box;
383
+ const cx = b.x + b.w / 2;
384
+ const cy = b.y + b.h / 2;
385
+ pen.add('<g class="node">');
386
+ if (node.ask)
387
+ drawThing(pen, { ask: true }, cx, cy, look.thing * 1.05);
388
+ else if (node.thing !== undefined && node.label !== undefined) {
389
+ drawThing(pen, node.thing, cx, b.y + look.thing / 2, thingSize(look));
390
+ pen.text(cx, b.y + b.h - look.under / 2 - 2, node.label, { size: look.under });
391
+ }
392
+ else if (node.thing !== undefined)
393
+ drawThing(pen, node.thing, cx, cy, thingSize(look));
394
+ else {
395
+ const color = node.color ?? "white";
396
+ pen.el("rect", {
397
+ x: b.x,
398
+ y: b.y,
399
+ width: b.w,
400
+ height: b.h,
401
+ rx: 9,
402
+ fill: colors[color].fill,
403
+ stroke: ink,
404
+ "stroke-width": outline,
405
+ });
406
+ pen.text(cx, cy, node.label ?? "", { size: look.font, fill: textOn(color) });
407
+ }
408
+ pen.add("</g>");
409
+ }
410
+ return { width: tree.width, height: tree.height };
411
+ },
412
+ describe(scene) {
413
+ const s = scene;
414
+ const grow = s.grow ?? "down";
415
+ const across = grow === "right" || grow === "left";
416
+ const all = nodesOf(s.root);
417
+ const line = across ? "column" : "row";
418
+ const along = across ? "from the top" : "from the left";
419
+ /** ref names a node so that no other node shares the words. */
420
+ const ref = (node, depth) => {
421
+ const word = name(node);
422
+ if (all.filter((o) => name(o.node) === word).length === 1)
423
+ return word;
424
+ const level = all.filter((o) => o.depth === depth);
425
+ const alike = level.filter((o) => name(o.node) === word);
426
+ const where = `in ${line} ${depth + 1}`;
427
+ if (alike.length === 1)
428
+ return `${word} ${where}`;
429
+ const k = alike.findIndex((o) => o.node === node);
430
+ return `the ${ordinals[k] ?? `${k + 1}th`} ${word} ${along} ${where}`;
431
+ };
432
+ const side = { down: "below", up: "above", right: "to the right of", left: "to the left of" }[grow];
433
+ const toward = { down: "above", up: "below", right: "to the left of", left: "to the right of" }[grow];
434
+ const rootAt = { down: "top", up: "bottom", right: "left", left: "right" }[grow];
435
+ if (s.bracket) {
436
+ const leaves = leavesOf(s.root);
437
+ const round = new Map();
438
+ const roundOf = (node) => {
439
+ if (!node.children)
440
+ return 0;
441
+ const r = 1 + Math.max(...node.children.map(roundOf));
442
+ round.set(node, r);
443
+ return r;
444
+ };
445
+ const last = roundOf(s.root);
446
+ const out = [
447
+ `A knockout bracket of ${leaves.length} players, ${along}: ${leaves.map(name).join(", ")}. Each match joins two players, and the winner stands ${toward} them, the champion at the ${rootAt}.`,
448
+ ];
449
+ for (let r = 1; r <= last; r++) {
450
+ const matches = all
451
+ .filter((o) => round.get(o.node) === r)
452
+ .map((o) => `${o.node.children.map(name).join(" against ")}, won by ${o.node.ask ? "?" : name(o.node)}`);
453
+ out.push(`${r === last ? "The final" : `Round ${r}`}: ${matches.join("; ")}.`);
454
+ }
455
+ return out.join(" ");
456
+ }
457
+ const elbow = (s.lines ?? "straight") === "elbow";
458
+ const out = [
459
+ `A tree diagram growing ${grow === "down" || grow === "up" ? grow : `to the ${grow}`} from ${name(s.root)} at the ${rootAt}; ${elbow ? "elbow lines" : "lines"} join each node to the nodes ${side} it.`,
460
+ ];
461
+ for (const o of all) {
462
+ const kids = o.node.children;
463
+ if (!kids)
464
+ continue;
465
+ const words = kids
466
+ .map((k) => `${name(k)}${k.branch !== undefined ? ` on the branch "${k.branch}"` : ""}`)
467
+ .join(", ");
468
+ const where = side[0].toUpperCase() + side.slice(1);
469
+ out.push(`${where} ${ref(o.node, o.depth)}: ${words}.`);
470
+ }
471
+ return out.join(" ");
472
+ },
473
+ };
@@ -0,0 +1,55 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type ColorName } from "../style.ts";
3
+ import { type Thing } from "../things.ts";
4
+ /**
5
+ * A calendar: a month page, its dates in rows of weeks under the names of
6
+ * the days, the 1st under the weekday the scene names; or a strip of days
7
+ * in a row, one after another, past the end of a month into the next; or
8
+ * torn-off sheets, each with a month, a date and a weekday. A date may be
9
+ * coloured or carry a thing, a "?" may stand in place of a date, and an ink
10
+ * blot may cover one. Dates are named by their number, weekdays by their
11
+ * first three letters: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun".
12
+ */
13
+ export declare const weekdays: readonly ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
14
+ export type Weekday = (typeof weekdays)[number];
15
+ export declare const calendarStyles: readonly ["month", "strip", "sheets"];
16
+ export type CalendarStyle = (typeof calendarStyles)[number];
17
+ export declare const sheetAsks: readonly ["day", "weekday", "month"];
18
+ export type Mark = {
19
+ day: number;
20
+ color?: ColorName;
21
+ thing?: Thing;
22
+ };
23
+ export type Sheet = {
24
+ month?: string;
25
+ day?: number;
26
+ weekday?: Weekday;
27
+ ask?: (typeof sheetAsks)[number];
28
+ };
29
+ export type CalendarScene = {
30
+ kind: "calendar";
31
+ style?: CalendarStyle;
32
+ month?: string;
33
+ first?: Weekday;
34
+ days?: number;
35
+ weekStart?: "Mon" | "Sun";
36
+ start?: number;
37
+ count?: number;
38
+ marks?: Mark[];
39
+ ask?: number[];
40
+ blots?: number[];
41
+ sheets?: Sheet[];
42
+ color?: ColorName;
43
+ };
44
+ /** ordinal writes a date as words read it: 1st, 2nd, 3rd, 4th, 11th, 22nd. */
45
+ export declare function ordinal(d: number): string;
46
+ /** columnsOf is the order of the weekdays across a month page. */
47
+ export declare function columnsOf(c: CalendarScene): Weekday[];
48
+ /** cellOf is the row and the column, from 0, of a date on a month page. */
49
+ export declare function cellOf(c: CalendarScene, day: number): {
50
+ row: number;
51
+ col: number;
52
+ };
53
+ /** datesOf lists the dates a month page or a strip shows, in order. */
54
+ export declare function datesOf(c: CalendarScene): number[];
55
+ export declare const calendar: Kind;