@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,758 @@
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, faint, ink, isColor, outline } from "../style.js";
5
+ import { n, poly } from "../svg.js";
6
+ import { article, mix } from "./cubes.js";
7
+ /**
8
+ * Containers: vessels that hold a liquid — jugs, glasses, bottles, buckets,
9
+ * tanks — each with its capacity, in rows of steps. The first row is the
10
+ * start; each next row shows the arrows of its pours, from a vessel, or from
11
+ * the tap, into another vessel, or out into the sink, and the levels after
12
+ * them. A level is drawn exactly: a vessel neither empty nor full shows its
13
+ * marks, a line every so many units, or its amount in words, and the check
14
+ * proves that every row's levels follow from the row above and its pours.
15
+ */
16
+ export const vesselTypes = ["jug", "glass", "bottle", "bucket", "tank"];
17
+ /** markStep is how many units lie between two marks; 0 where a scene has none. */
18
+ function markStep(s) {
19
+ return s.marks === true ? 1 : typeof s.marks === "number" ? s.marks : 0;
20
+ }
21
+ const room = 640 - 2 * margin;
22
+ const columnGap = 24;
23
+ const stationW = 64;
24
+ /**
25
+ * The look of each vessel: the height of its body to its width, its widest
26
+ * body, how far its wall goes above the capacity line (brim), what stands
27
+ * above the brim (crown) and below the bottom (foot), and what it carries on
28
+ * each side.
29
+ */
30
+ const shapes = {
31
+ glass: { aspect: 1.25, most: 66, brim: 7, crown: 0, foot: 8, left: 0, right: 0, color: "blue" },
32
+ bottle: { aspect: 2, most: 58, brim: 0, crown: 50, foot: 0, left: 0, right: 0, color: "red" },
33
+ jug: { aspect: 1.2, most: 92, brim: 10, crown: 5, foot: 0, left: 10, right: 26, color: "blue" },
34
+ bucket: {
35
+ aspect: 0.95,
36
+ most: 104,
37
+ brim: 6,
38
+ crown: 26,
39
+ foot: 0,
40
+ left: 0,
41
+ right: 0,
42
+ color: "orange",
43
+ },
44
+ tank: { aspect: 1.15, most: 140, brim: 8, crown: 3, foot: 18, left: 0, right: 18, color: "gray" },
45
+ };
46
+ const glassTint = "#e7f4fd";
47
+ const line = { stroke: ink, "stroke-width": outline, "stroke-linejoin": "round" };
48
+ /** tallest is the height of the tallest body in a picture of so many rows. */
49
+ function tallest(rows) {
50
+ return [180, 180, 140, 112, 92, 80, 72][Math.min(rows, 6)];
51
+ }
52
+ /**
53
+ * sizes gives each vessel a body whose area is its capacity, in its own
54
+ * shape: the largest as tall as the rows allow and no wider than its kind.
55
+ */
56
+ export function sizes(vessels, rows) {
57
+ const raw = vessels.map((v) => {
58
+ const a = shapes[v.type].aspect;
59
+ const w = Math.sqrt(v.capacity / a);
60
+ return { w, h: a * w };
61
+ });
62
+ const f = Math.min(tallest(rows) / Math.max(...raw.map((r) => r.h)), ...raw.map((r, i) => shapes[vessels[i].type].most / r.w));
63
+ return raw.map((r) => ({ w: Math.max(34, r.w * f), h: Math.max(30, r.h * f) }));
64
+ }
65
+ /** walls are a body's left and right x at a height up it, a glass and a bucket wider at the top. */
66
+ function walls(type, { w, h }) {
67
+ const taper = type === "glass" ? 0.09 : type === "bucket" ? 0.1 : 0;
68
+ const top = h + shapes[type].brim;
69
+ return (up) => {
70
+ const inset = taper * w * (1 - up / top);
71
+ return [inset, w - inset];
72
+ };
73
+ }
74
+ function columnWidth(v, size, label) {
75
+ const s = shapes[v.type];
76
+ return Math.max(s.left + size.w + s.right, textWidth(label, 15) + 6);
77
+ }
78
+ function capWords(v, unit) {
79
+ return `${v.ask ? "?" : v.capacity}${unit ? ` ${unit}` : ""}`;
80
+ }
81
+ function labelOf(v, unit) {
82
+ return v.label ? `${v.label}: ${capWords(v, unit)}` : capWords(v, unit);
83
+ }
84
+ /** rise gives how far above its bottom a vessel's highest point and its mouth stand. */
85
+ function rise(v, size) {
86
+ const s = shapes[v.type];
87
+ const mouth = v.type === "bottle" ? size.h + 50 : size.h + s.brim + (v.type === "tank" ? 3 : 0);
88
+ return { top: size.h + s.brim + s.crown, mouth };
89
+ }
90
+ /** drawVessel draws a vessel with its level, its body's bottom left at x, y. */
91
+ function drawVessel(pen, v, size, level, x, y, scene) {
92
+ const s = shapes[v.type];
93
+ const { w, h } = size;
94
+ const c = colors[v.color ?? s.color];
95
+ const wall = walls(v.type, size);
96
+ const at = (up, side) => x + wall(up)[side];
97
+ const liquid = colors[scene.liquid ?? "blue"];
98
+ const top = y - h - s.brim;
99
+ const cx = x + w / 2;
100
+ const r = v.type === "glass" || v.type === "bucket" ? 5 : 9;
101
+ /** body runs up the left wall from upTo, round the bottom, and up the right wall. */
102
+ const body = (inset, upTo) => {
103
+ const rr = Math.max(0, Math.min(r - inset, upTo / 2));
104
+ const yb = y - inset;
105
+ return `M${n(at(upTo, 0) + inset)} ${n(y - upTo)}L${n(at(0, 0) + inset)} ${n(yb - rr)}Q${n(at(0, 0) + inset)} ${n(yb)} ${n(at(0, 0) + inset + rr)} ${n(yb)}H${n(at(0, 1) - inset - rr)}Q${n(at(0, 1) - inset)} ${n(yb)} ${n(at(0, 1) - inset)} ${n(yb - rr)}L${n(at(upTo, 1) - inset)} ${n(y - upTo)}`;
106
+ };
107
+ // what stands behind the body
108
+ if (v.type === "jug") {
109
+ const reach = Math.min(h * 0.8, 84);
110
+ const loop = `M${n(x + w - 2)} ${n(top + 10)}C${n(x + w + 34)} ${n(top + 4)} ${n(x + w + 32)} ${n(top + reach + 4)} ${n(x + w - 2)} ${n(top + reach)}`;
111
+ pen.el("path", {
112
+ d: loop,
113
+ fill: "none",
114
+ stroke: ink,
115
+ "stroke-width": 11,
116
+ "stroke-linecap": "round",
117
+ });
118
+ pen.el("path", {
119
+ d: loop,
120
+ fill: "none",
121
+ stroke: c.fill,
122
+ "stroke-width": 5.5,
123
+ "stroke-linecap": "round",
124
+ });
125
+ }
126
+ if (v.type === "bucket") {
127
+ const arc = `M${n(x + 4)} ${n(top + 2)}C${n(x + 6)} ${n(top - 34)} ${n(x + w - 6)} ${n(top - 34)} ${n(x + w - 4)} ${n(top + 2)}`;
128
+ pen.el("path", {
129
+ d: arc,
130
+ fill: "none",
131
+ stroke: ink,
132
+ "stroke-width": 6,
133
+ "stroke-linecap": "round",
134
+ });
135
+ pen.el("path", {
136
+ d: arc,
137
+ fill: "none",
138
+ stroke: colors.gray.fill,
139
+ "stroke-width": 2.5,
140
+ "stroke-linecap": "round",
141
+ });
142
+ }
143
+ if (v.type === "tank") {
144
+ for (const dx of [w * 0.14, w * 0.86 - 12])
145
+ pen.el("rect", {
146
+ x: x + dx,
147
+ y: y + 2,
148
+ width: 12,
149
+ height: s.foot - 2,
150
+ rx: 3,
151
+ fill: c.dark,
152
+ ...line,
153
+ });
154
+ for (const [stroke, width] of [
155
+ [ink, 9],
156
+ [colors.gray.fill, 4],
157
+ ])
158
+ pen.el("path", {
159
+ d: `M${n(x + w)} ${n(y - 14)}h12v11`,
160
+ fill: "none",
161
+ stroke,
162
+ "stroke-width": width,
163
+ "stroke-linecap": "round",
164
+ "stroke-linejoin": "round",
165
+ });
166
+ }
167
+ // the body, glass to see the liquid through
168
+ let shell = body(0, h + s.brim);
169
+ if (v.type === "jug")
170
+ shell = `M${n(x - 10)} ${n(top - 5)}Q${n(x)} ${n(top + 1)} ${n(x)} ${n(top + 12)}L${n(x)} ${n(y - r)}Q${n(x)} ${n(y)} ${n(x + r)} ${n(y)}H${n(x + w - r)}Q${n(x + w)} ${n(y)} ${n(x + w)} ${n(y - r)}V${n(top)}H${n(x + 14)}Q${n(x + 2)} ${n(top)} ${n(x - 10)} ${n(top - 5)}Z`;
171
+ if (v.type === "bottle") {
172
+ const nw = Math.max(16, w * 0.38);
173
+ shell = `${body(0, h)}Q${n(x + w)} ${n(y - h - 18)} ${n(cx + nw / 2)} ${n(y - h - 20)}V${n(y - h - 38)}H${n(cx - nw / 2)}V${n(y - h - 20)}Q${n(x)} ${n(y - h - 18)} ${n(x)} ${n(y - h)}Z`;
174
+ }
175
+ pen.el("path", { d: shell, fill: glassTint });
176
+ if (v.type === "glass")
177
+ pen.el("path", {
178
+ d: `M${n(at(0, 0))} ${n(y)}H${n(at(0, 1))}V${n(y + s.foot - 5)}Q${n(at(0, 1))} ${n(y + s.foot)} ${n(at(0, 1) - 5)} ${n(y + s.foot)}H${n(at(0, 0) + 5)}Q${n(at(0, 0))} ${n(y + s.foot)} ${n(at(0, 0))} ${n(y + s.foot - 5)}Z`,
179
+ fill: mix(glassTint, ink, 0.14),
180
+ });
181
+ if (typeof level === "number" && level > 0) {
182
+ const up = (h * level) / v.capacity;
183
+ pen.el("path", {
184
+ d: `${body(outline / 2, up)}Z`,
185
+ fill: mix(liquid.fill, "#ffffff", 0.45),
186
+ class: "liquid",
187
+ });
188
+ pen.el("path", {
189
+ d: `M${n(at(up, 0) + outline / 2)} ${n(y - up)}H${n(at(up, 1) - outline / 2)}`,
190
+ stroke: liquid.dark,
191
+ "stroke-width": 2.5,
192
+ });
193
+ }
194
+ const every = markStep(scene);
195
+ if (every > 0)
196
+ for (let k = every; k <= v.capacity; k += every) {
197
+ const up = (h * k) / v.capacity;
198
+ const l = at(up, 0);
199
+ pen.el("path", {
200
+ d: `M${n(l + 1)} ${n(y - up)}H${n(l + ((k / every) % 5 === 0 ? 14 : 9))}`,
201
+ stroke: ink,
202
+ "stroke-width": 2,
203
+ class: "mark",
204
+ });
205
+ }
206
+ if (v.type === "glass")
207
+ pen.el("path", {
208
+ d: `M${n(at(h + s.brim, 0))} ${n(top)}L${n(at(0, 0))} ${n(y + s.foot - 5)}Q${n(at(0, 0))} ${n(y + s.foot)} ${n(at(0, 0) + 5)} ${n(y + s.foot)}H${n(at(0, 1) - 5)}Q${n(at(0, 1))} ${n(y + s.foot)} ${n(at(0, 1))} ${n(y + s.foot - 5)}L${n(at(h + s.brim, 1))} ${n(top)}`,
209
+ fill: "none",
210
+ ...line,
211
+ class: "vessel",
212
+ });
213
+ else
214
+ pen.el("path", { d: shell, fill: "none", ...line, class: "vessel" });
215
+ // what stands in front: a bucket's rim, a tank's lid, a bottle's cap
216
+ if (v.type === "bucket")
217
+ pen.el("path", {
218
+ d: poly([
219
+ { x: at(h + s.brim, 0) - 2, y: top },
220
+ { x: at(h + s.brim, 1) + 2, y: top },
221
+ { x: at(h + s.brim - 9, 1) + 1, y: top + 9 },
222
+ { x: at(h + s.brim - 9, 0) - 1, y: top + 9 },
223
+ ]),
224
+ fill: c.fill,
225
+ ...line,
226
+ });
227
+ if (v.type === "tank")
228
+ pen.el("rect", { x: x - 3, y: top - 3, width: w + 6, height: 8, rx: 3, fill: c.fill, ...line });
229
+ if (v.type === "bottle") {
230
+ const nw = Math.max(16, w * 0.38);
231
+ pen.el("rect", {
232
+ x: cx - nw / 2 - 3,
233
+ y: y - h - 50,
234
+ width: nw + 6,
235
+ height: 13,
236
+ rx: 3,
237
+ fill: c.fill,
238
+ ...line,
239
+ });
240
+ }
241
+ // the level in words
242
+ if (level === "?") {
243
+ const rr = Math.min(16, h / 2 - 3, w / 2 - 6);
244
+ pen.el("circle", {
245
+ cx,
246
+ cy: y - h / 2,
247
+ r: rr,
248
+ fill: "#ffffff",
249
+ stroke: ink,
250
+ "stroke-width": 2,
251
+ "stroke-dasharray": "4 3",
252
+ });
253
+ pen.text(cx, y - h / 2, "?", { size: Math.max(14, rr * 1.2) });
254
+ }
255
+ else if (scene.amounts) {
256
+ const up = (h * level) / v.capacity;
257
+ const words = `${level}${scene.unit ? ` ${scene.unit}` : ""}`;
258
+ const yy = up >= 24 ? y - up / 2 : level === 0 ? y - 14 : y - up - 13;
259
+ const ww = textWidth(words, 15) + 8;
260
+ const dx = every > 0 ? 5 : 0;
261
+ pen.el("rect", {
262
+ x: cx - ww / 2 + dx,
263
+ y: yy - 10,
264
+ width: ww,
265
+ height: 20,
266
+ rx: 6,
267
+ fill: "#ffffff",
268
+ });
269
+ pen.text(cx + dx, yy, words, { size: 15 });
270
+ }
271
+ }
272
+ /** drawTap draws a tap whose spout opens at x, y. */
273
+ function drawTap(pen, x, y) {
274
+ const pipe = `M${n(x - 30)} ${n(y - 16)}H${n(x)}V${n(y - 2)}`;
275
+ for (const [stroke, width] of [
276
+ [ink, 13],
277
+ [colors.gray.fill, 7.5],
278
+ ])
279
+ pen.el("path", {
280
+ d: pipe,
281
+ fill: "none",
282
+ stroke,
283
+ "stroke-width": width,
284
+ "stroke-linejoin": "round",
285
+ });
286
+ pen.el("path", { d: `M${n(x - 12)} ${n(y - 22)}V${n(y - 32)}`, stroke: ink, "stroke-width": 3 });
287
+ pen.el("rect", {
288
+ x: x - 22,
289
+ y: y - 38,
290
+ width: 20,
291
+ height: 7,
292
+ rx: 3,
293
+ fill: colors.red.fill,
294
+ ...line,
295
+ "stroke-width": 2,
296
+ class: "tap",
297
+ });
298
+ pen.el("rect", {
299
+ x: x - 6,
300
+ y: y - 3,
301
+ width: 12,
302
+ height: 5,
303
+ rx: 2,
304
+ fill: colors.gray.dark,
305
+ ...line,
306
+ "stroke-width": 2,
307
+ });
308
+ }
309
+ /** drawSink draws a sink whose basin opens at x, y. */
310
+ function drawSink(pen, x, y) {
311
+ pen.el("path", {
312
+ d: poly([
313
+ { x: x - 28, y },
314
+ { x: x + 28, y },
315
+ { x: x + 20, y: y + 22 },
316
+ { x: x - 20, y: y + 22 },
317
+ ]),
318
+ fill: colors.gray.fill,
319
+ ...line,
320
+ class: "sink",
321
+ });
322
+ pen.el("ellipse", { cx: x, cy: y + 14, rx: 6, ry: 3, fill: ink });
323
+ }
324
+ /** bend gives the height of the controls that take an arc from a to b through apex halfway. */
325
+ function bend(a, b, apex) {
326
+ return (apex - 0.125 * (a.y + b.y)) / 0.75;
327
+ }
328
+ /** along gives points of the arc from a to b whose controls stand at height c. */
329
+ function along(a, b, c) {
330
+ const c1 = a.x + (b.x - a.x) * 0.08;
331
+ const c2 = b.x - (b.x - a.x) * 0.08;
332
+ return Array.from({ length: 41 }, (_, i) => {
333
+ const t = i / 40;
334
+ const u = 1 - t;
335
+ return {
336
+ x: u * u * u * a.x + 3 * u * u * t * c1 + 3 * u * t * t * c2 + t * t * t * b.x,
337
+ y: u * u * u * a.y + 3 * u * t * c + t * t * t * b.y,
338
+ };
339
+ });
340
+ }
341
+ function arrowHead(pen, x, y, dx, dy) {
342
+ const l = Math.hypot(dx, dy) || 1;
343
+ const ux = dx / l;
344
+ const uy = dy / l;
345
+ const s = 11;
346
+ pen.el("path", {
347
+ d: poly([
348
+ { x, y },
349
+ { x: x - ux * s - uy * s * 0.55, y: y - uy * s + ux * s * 0.55 },
350
+ { x: x - ux * s + uy * s * 0.55, y: y - uy * s - ux * s * 0.55 },
351
+ ]),
352
+ fill: ink,
353
+ ...line,
354
+ "stroke-width": 1.5,
355
+ });
356
+ }
357
+ /** tag writes a label on a white ground; "?" stands in a dashed box. */
358
+ function tag(pen, x, y, label) {
359
+ const ask = label === "?";
360
+ const w = Math.max(ask ? 26 : 0, textWidth(label, 15) + 10);
361
+ pen.el("rect", {
362
+ x: x - w / 2,
363
+ y: y - 12,
364
+ width: w,
365
+ height: 24,
366
+ rx: 7,
367
+ fill: "#ffffff",
368
+ stroke: ask ? ink : "none",
369
+ "stroke-width": ask ? 2 : undefined,
370
+ "stroke-dasharray": ask ? "4 3" : undefined,
371
+ });
372
+ pen.text(x, y, label, { size: 15 });
373
+ }
374
+ function usesTap(s) {
375
+ return s.steps.some((st) => (st.pours ?? []).some((p) => p.from === "tap"));
376
+ }
377
+ function usesSink(s) {
378
+ return s.steps.some((st) => (st.pours ?? []).some((p) => p.to === "out"));
379
+ }
380
+ /** layout places the columns of a row: the tap, each vessel's body, the sink. */
381
+ function layout(s) {
382
+ const all = sizes(s.vessels, s.steps.length);
383
+ const pad = Math.max(0, ...s.steps.map((st) => (st.label ? textWidth(st.label, 16) + 14 : 0)));
384
+ let x = pad;
385
+ let tapX;
386
+ if (usesTap(s)) {
387
+ tapX = x + 38;
388
+ x += stationW;
389
+ }
390
+ const columns = s.vessels.map((v, i) => {
391
+ const size = all[i];
392
+ const w = columnWidth(v, size, labelOf(v, s.unit));
393
+ const sh = shapes[v.type];
394
+ const at = x + (w - (sh.left + size.w + sh.right)) / 2 + sh.left;
395
+ x += w + columnGap;
396
+ return at;
397
+ });
398
+ x -= columnGap;
399
+ let sinkX;
400
+ if (usesSink(s)) {
401
+ sinkX = x + 46;
402
+ x += stationW + 12;
403
+ }
404
+ return {
405
+ sizes: all,
406
+ columns,
407
+ ...(tapX === undefined ? {} : { tapX }),
408
+ ...(sinkX === undefined ? {} : { sinkX }),
409
+ left: pad,
410
+ width: x,
411
+ };
412
+ }
413
+ // --- the check -----------------------------------------------------------------------
414
+ const nameOf = (s, i) => s.vessels[i]?.label ? `vessel ${s.vessels[i].label}` : `vessel ${i + 1}`;
415
+ function checkPour(p, path, k, problems) {
416
+ if (!isObj(p)) {
417
+ problems.push(`${path}: must be a pour, from and to`);
418
+ return;
419
+ }
420
+ only(p, ["from", "to", "amount", "label"], path, problems);
421
+ const end = (value, word, p_) => {
422
+ if (value === word)
423
+ return;
424
+ if (typeof value !== "number" || !Number.isInteger(value) || value < 1 || value > k)
425
+ problems.push(`${p_}: must be a vessel's number, 1 to ${k}, or "${word}"`);
426
+ };
427
+ end(p.from, "tap", `${path}.from`);
428
+ end(p.to, "out", `${path}.to`);
429
+ if (p.from === p.to)
430
+ problems.push(`${path}.to: must be another vessel than from`);
431
+ if (p.from === "tap" && p.to === "out")
432
+ problems.push(`${path}: the tap pours into a vessel, not out`);
433
+ if (p.amount !== undefined)
434
+ int(p.amount, `${path}.amount`, problems, 1, 100);
435
+ if (p.label !== undefined)
436
+ text(p.label, `${path}.label`, problems, 6);
437
+ }
438
+ /** simulate runs the pours of every step and records where a row's levels disagree. */
439
+ function simulate(s, problems) {
440
+ const caps = s.vessels.map((v) => v.capacity);
441
+ let state = s.steps[0].levels.map((l) => (l === "?" ? undefined : l));
442
+ s.steps.forEach((step, k) => {
443
+ if (k === 0)
444
+ return;
445
+ const now = [...state];
446
+ (step.pours ?? []).forEach((p, j) => {
447
+ const path = `steps[${k}].pours[${j}]`;
448
+ const from = p.from === "tap" ? undefined : p.from - 1;
449
+ const to = p.to === "out" ? undefined : p.to - 1;
450
+ const has = from === undefined ? Number.POSITIVE_INFINITY : now[from];
451
+ const room_ = to === undefined
452
+ ? Number.POSITIVE_INFINITY
453
+ : now[to] === undefined
454
+ ? undefined
455
+ : caps[to] - now[to];
456
+ let moved;
457
+ if (p.amount !== undefined) {
458
+ moved = p.amount;
459
+ // A pour that cannot be leaves the vessels unknown, so the rows below raise nothing more.
460
+ if (has !== undefined && p.amount > has) {
461
+ problems.push(`${path}.amount: ${nameOf(s, from)} holds only ${has}`);
462
+ moved = undefined;
463
+ }
464
+ if (room_ !== undefined && p.amount > room_) {
465
+ problems.push(`${path}.amount: only ${room_} more fits in ${nameOf(s, to)}`);
466
+ moved = undefined;
467
+ }
468
+ }
469
+ else if (has !== undefined && room_ !== undefined)
470
+ moved = Math.min(has, room_);
471
+ if (moved === 0)
472
+ problems.push(`${path}: moves nothing, ${has === 0 ? `${nameOf(s, from)} is empty` : `${nameOf(s, to)} is full`}`);
473
+ if (from !== undefined)
474
+ now[from] = moved === undefined || now[from] === undefined ? undefined : now[from] - moved;
475
+ if (to !== undefined)
476
+ now[to] = moved === undefined || now[to] === undefined ? undefined : now[to] + moved;
477
+ });
478
+ step.levels.forEach((l, i) => {
479
+ if (l === "?")
480
+ return;
481
+ if (now[i] !== undefined && now[i] !== l)
482
+ problems.push(`steps[${k}].levels[${i}]: the pours leave ${now[i]} in ${nameOf(s, i)}, not ${l}`);
483
+ now[i] = l;
484
+ });
485
+ state = now;
486
+ });
487
+ }
488
+ // --- words -------------------------------------------------------------------------------
489
+ /** callOf names a vessel in words: by its label, or by its place among several. */
490
+ function callOf(s, i) {
491
+ return s.vessels[i].label ?? (s.vessels.length === 1 ? "the vessel" : `vessel ${i + 1}`);
492
+ }
493
+ function sayLevels(s, levels) {
494
+ const u = s.unit ? ` ${s.unit}` : "";
495
+ const said = levels.map((l, i) => {
496
+ const v = s.vessels[i];
497
+ const name = callOf(s, i);
498
+ if (l === "?")
499
+ return `${name} shows a question mark instead of its level`;
500
+ if (l === 0)
501
+ return `${name} is empty`;
502
+ // A full vessel whose capacity is asked tells its amount only where the picture writes it.
503
+ if (l === v.capacity)
504
+ return v.ask && !s.amounts ? `${name} is full` : `${name} is full with ${l}${u}`;
505
+ return `${name} holds ${l}${u}`;
506
+ });
507
+ return said.length === 1 ? said[0] : `${said.slice(0, -1).join(", ")} and ${said.at(-1)}`;
508
+ }
509
+ function sayPour(s, p) {
510
+ const who = (end) => end === "tap" ? "the tap" : end === "out" ? "the sink" : callOf(s, end - 1);
511
+ const marked = p.label !== undefined
512
+ ? p.label === "?"
513
+ ? ", marked with a question mark"
514
+ : `, marked ${p.label}`
515
+ : p.amount !== undefined
516
+ ? `, marked ${p.amount}${s.unit ? ` ${s.unit}` : ""}`
517
+ : "";
518
+ return `from ${who(p.from)} into ${who(p.to)}${marked}`;
519
+ }
520
+ export const containers = {
521
+ kind: "containers",
522
+ members: ["kind", "vessels", "steps", "unit", "liquid", "marks", "amounts"],
523
+ check(scene, problems) {
524
+ only(scene, this.members, "scene", problems);
525
+ if (scene.unit !== undefined)
526
+ text(scene.unit, "unit", problems, 5);
527
+ if (scene.liquid !== undefined && !isColor(scene.liquid))
528
+ problems.push(`liquid: must be a colour: ${colorNames.join(", ")}`);
529
+ if (scene.amounts !== undefined && typeof scene.amounts !== "boolean")
530
+ problems.push("amounts: must be true or false");
531
+ if (scene.marks !== undefined &&
532
+ typeof scene.marks !== "boolean" &&
533
+ !(Number.isInteger(scene.marks) &&
534
+ scene.marks >= 1 &&
535
+ scene.marks <= 50))
536
+ problems.push("marks: must be true (a mark for every unit), false, or a whole number of units from 1 to 50 between marks");
537
+ const every = markStep(scene);
538
+ const vessels = list(scene.vessels, "vessels", problems, 1, 5);
539
+ const caps = [];
540
+ vessels.forEach((v, i) => {
541
+ const path = `vessels[${i}]`;
542
+ if (!isObj(v)) {
543
+ problems.push(`${path}: must be a vessel with a type and a capacity`);
544
+ caps.push(1);
545
+ return;
546
+ }
547
+ only(v, ["type", "capacity", "label", "ask", "color"], path, problems);
548
+ oneOf(v.type, vesselTypes, `${path}.type`, problems);
549
+ caps.push(int(v.capacity, `${path}.capacity`, problems, 1, 100));
550
+ if (v.label !== undefined)
551
+ text(v.label, `${path}.label`, problems, 3);
552
+ if (v.ask !== undefined && v.ask !== true)
553
+ problems.push(`${path}.ask: must be true, or left out`);
554
+ if (v.color !== undefined && !isColor(v.color))
555
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
556
+ });
557
+ const before = problems.length;
558
+ const steps = list(scene.steps, "steps", problems, 1, 6);
559
+ steps.forEach((st, k) => {
560
+ const path = `steps[${k}]`;
561
+ if (!isObj(st)) {
562
+ problems.push(`${path}: must be a step with the levels of the vessels`);
563
+ return;
564
+ }
565
+ only(st, ["levels", "pours", "label"], path, problems);
566
+ if (st.label !== undefined)
567
+ text(st.label, `${path}.label`, problems, 6);
568
+ const levels = list(st.levels, `${path}.levels`, problems, 0);
569
+ if (Array.isArray(st.levels) && levels.length !== vessels.length)
570
+ problems.push(`${path}.levels: must hold one level for each of the ${vessels.length} vessels`);
571
+ levels.forEach((l, i) => {
572
+ if (l === "?" || i >= caps.length)
573
+ return;
574
+ const cap = caps[i];
575
+ if (typeof l !== "number" || !Number.isInteger(l) || l < 0 || l > cap) {
576
+ problems.push(`${path}.levels[${i}]: must be a whole number from 0 to ${cap}, or "?"`);
577
+ return;
578
+ }
579
+ if (l > 0 && l < cap && scene.amounts !== true) {
580
+ if (every === 0)
581
+ problems.push(`${path}.levels[${i}]: a vessel neither empty nor full cannot be read; set marks or amounts`);
582
+ else if (l % every !== 0)
583
+ problems.push(`${path}.levels[${i}]: stands between two marks, one every ${every}; set amounts, or a level on a mark`);
584
+ }
585
+ });
586
+ if (st.pours !== undefined) {
587
+ if (k === 0)
588
+ problems.push(`${path}.pours: the first step is the start; it has no pours`);
589
+ list(st.pours, `${path}.pours`, problems, 1, 3).forEach((p, j) => {
590
+ checkPour(p, `${path}.pours[${j}]`, vessels.length, problems);
591
+ });
592
+ }
593
+ });
594
+ if (problems.length > 0)
595
+ return;
596
+ const s = scene;
597
+ if (every > 0) {
598
+ const all = sizes(s.vessels, s.steps.length);
599
+ s.vessels.forEach((v, i) => {
600
+ if ((all[i].h * every) / v.capacity < 6)
601
+ problems.push(`vessels[${i}].capacity: too many marks for its size; leave marks out, or use smaller capacities`);
602
+ });
603
+ }
604
+ if (layout(s).width > room)
605
+ problems.push("vessels: too wide side by side; use fewer or narrower vessels, or shorter labels");
606
+ if (problems.length > before)
607
+ return;
608
+ simulate(s, problems);
609
+ },
610
+ draw(scene, pen) {
611
+ const s = scene;
612
+ const lay = layout(s);
613
+ const width = lay.width + 2 * margin;
614
+ const x0 = margin;
615
+ const reach = Math.max(...s.vessels.map((v, i) => lay.sizes[i].h + shapes[v.type].brim + shapes[v.type].crown));
616
+ const low = Math.max(...s.vessels.map((v) => shapes[v.type].foot));
617
+ // Many rows show the vessels' names and capacities under the first row only.
618
+ const named = (k) => k === 0 || s.steps.length < 3;
619
+ const middle = (i) => x0 + lay.columns[i] + lay.sizes[i].w / 2;
620
+ let y = margin;
621
+ s.steps.forEach((step, k) => {
622
+ const pours = step.pours ?? [];
623
+ // The row is laid out on a base line at 0 first, then moved down to its place.
624
+ const ups = s.vessels.map((v, i) => rise(v, lay.sizes[i]));
625
+ const extent = s.vessels.map((v, i) => {
626
+ const sh = shapes[v.type];
627
+ const l = x0 + lay.columns[i] - sh.left;
628
+ return { l, r: l + sh.left + lay.sizes[i].w + sh.right };
629
+ });
630
+ const tapAt = { x: x0 + (lay.tapX ?? 0), y: 10 - reach };
631
+ const sinkAt = { x: x0 + (lay.sinkX ?? 0), y: low - 22 };
632
+ const arcs = pours.map((p, j) => {
633
+ const start = p.from === "tap"
634
+ ? { x: tapAt.x, y: tapAt.y + 8 }
635
+ : { x: middle(p.from - 1), y: -ups[p.from - 1].mouth };
636
+ const end = p.to === "out"
637
+ ? { x: sinkAt.x, y: sinkAt.y + 2 }
638
+ : { x: middle(p.to - 1), y: -ups[p.to - 1].mouth };
639
+ const dir = end.x > start.x ? 1 : -1;
640
+ const a = { x: start.x + dir * 8, y: start.y - 5 };
641
+ const b = { x: end.x - dir * 8, y: end.y - 5 };
642
+ const words = p.label ?? (p.amount !== undefined ? `${p.amount}${s.unit ? ` ${s.unit}` : ""}` : "");
643
+ const ends = [p.from, p.to].filter((e) => typeof e === "number").map((e) => e - 1);
644
+ return { j, a, b, lo: Math.min(a.x, b.x), hi: Math.max(a.x, b.x), ends, words };
645
+ });
646
+ // Each arc stands over the vessels it passes, a shorter one lower, one that spans another above it.
647
+ const placed = [];
648
+ const drawn = new Map();
649
+ for (const arc of [...arcs].sort((u, v) => u.hi - u.lo - (v.hi - v.lo) || u.j - v.j)) {
650
+ const under = extent.flatMap((e, i) => (e.l <= arc.hi && e.r >= arc.lo ? [i] : []));
651
+ let apex = Math.min(arc.a.y, arc.b.y, ...under.map((i) => -ups[i].top)) - 28;
652
+ for (const o of placed)
653
+ if (o.lo < arc.hi && arc.lo < o.hi)
654
+ apex = Math.min(apex, o.apex - 28);
655
+ const between = under.filter((i) => !arc.ends.includes(i));
656
+ let c = bend(arc.a, arc.b, apex);
657
+ let path = along(arc.a, arc.b, c);
658
+ const clears = () => between.every((i) => path.every((q) => q.x < extent[i].l - 4 || q.x > extent[i].r + 4 || q.y <= -ups[i].top - 10));
659
+ for (let tries = 0; tries < 40 && !clears(); tries++) {
660
+ apex -= 6;
661
+ c = bend(arc.a, arc.b, apex);
662
+ path = along(arc.a, arc.b, c);
663
+ }
664
+ placed.push({ lo: arc.lo, hi: arc.hi, apex });
665
+ drawn.set(arc.j, { apex, c, path });
666
+ }
667
+ const tops = [
668
+ ...ups.map((u) => -u.top),
669
+ ...(pours.some((p) => p.from === "tap") ? [tapAt.y - 40] : []),
670
+ ...arcs.flatMap((arc) => {
671
+ const d = drawn.get(arc.j);
672
+ return [...d.path.map((q) => q.y - 2), ...(arc.words ? [d.apex - 13] : [])];
673
+ }),
674
+ ];
675
+ const base = y - Math.min(...tops);
676
+ const sub = pen.sub();
677
+ s.vessels.forEach((v, i) => {
678
+ drawVessel(sub, v, lay.sizes[i], step.levels[i], x0 + lay.columns[i], base, s);
679
+ });
680
+ pen.add(sub.markup());
681
+ pen.el("path", {
682
+ d: `M${n(x0 + lay.left - 4)} ${n(base + low + 2)}H${n(x0 + lay.width + 4)}`,
683
+ stroke: faint,
684
+ "stroke-width": 3,
685
+ "stroke-linecap": "round",
686
+ });
687
+ if (named(k))
688
+ s.vessels.forEach((v, i) => {
689
+ pen.text(middle(i), base + low + 20, labelOf(v, s.unit), { size: 15 });
690
+ });
691
+ if (step.label)
692
+ pen.text(x0, base - lay.sizes.reduce((m, z) => Math.max(m, z.h), 0) / 2, step.label, {
693
+ size: 16,
694
+ anchor: "start",
695
+ });
696
+ if (pours.some((p) => p.from === "tap"))
697
+ drawTap(pen, tapAt.x, base + tapAt.y);
698
+ if (pours.some((p) => p.to === "out"))
699
+ drawSink(pen, sinkAt.x, base + sinkAt.y);
700
+ for (const arc of arcs) {
701
+ const { apex, c } = drawn.get(arc.j);
702
+ const { a, b } = arc;
703
+ const c1x = a.x + (b.x - a.x) * 0.08;
704
+ const c2x = b.x - (b.x - a.x) * 0.08;
705
+ pen.el("path", {
706
+ d: `M${n(a.x)} ${n(base + a.y)}C${n(c1x)} ${n(base + c)} ${n(c2x)} ${n(base + c)} ${n(b.x)} ${n(base + b.y)}`,
707
+ fill: "none",
708
+ stroke: ink,
709
+ "stroke-width": 2.5,
710
+ "stroke-linecap": "round",
711
+ class: "pour",
712
+ });
713
+ arrowHead(pen, b.x, base + b.y, b.x - c2x, b.y - c);
714
+ if (arc.words)
715
+ tag(pen, (a.x + b.x) / 2, base + apex, arc.words);
716
+ }
717
+ y = base + low + (named(k) ? 32 : 8) + (k < s.steps.length - 1 ? 14 : 0);
718
+ });
719
+ return { width, height: y + margin };
720
+ },
721
+ describe(scene) {
722
+ const s = scene;
723
+ const u = s.unit ? ` ${s.unit}` : "";
724
+ const one = s.vessels.length === 1;
725
+ const liquid = `${colors[s.liquid ?? "blue"].word} liquid`;
726
+ const vessels = s.vessels.map((v, i) => {
727
+ const what = article(`${v.color ? `${colors[v.color].word} ` : ""}${v.type}`);
728
+ const cap = v.ask
729
+ ? "with a question mark instead of its capacity"
730
+ : `that holds ${v.capacity}${u}`;
731
+ if (one)
732
+ return `${what[0].toUpperCase()}${what.slice(1)} ${cap}${v.label ? `, named ${v.label}` : ""}, with ${liquid}`;
733
+ return `${callOf(s, i)}, ${what} ${cap}`;
734
+ });
735
+ const every = markStep(s);
736
+ const fifth = every > 0 && s.vessels.some((v) => v.capacity >= 5 * every);
737
+ const how = [
738
+ every > 0
739
+ ? `${one ? "it has" : "each vessel has"} a mark every ${every}${u || (every === 1 ? " unit" : " units")}${fifth ? ", every fifth mark longer" : ""}`
740
+ : "",
741
+ s.amounts ? `the amount in ${one ? "it" : "each"} is written on it` : "",
742
+ ].filter(Boolean);
743
+ const head = `${one ? vessels[0] : `${s.vessels.length} vessels side by side with ${liquid}: ${vessels.join("; ")}`}${how.length ? `; ${how.join(", and ")}` : ""}.`;
744
+ const rows = s.steps.map((step, k) => {
745
+ const levels = sayLevels(s, step.levels);
746
+ if (step.label && k === 0)
747
+ return `Row ${step.label}: ${levels}.`;
748
+ if (k === 0)
749
+ return s.steps.length > 1 ? `At the start, ${levels}.` : `Now ${levels}.`;
750
+ const when = step.label ? `Row ${step.label}` : `Step ${k}`;
751
+ const pours = (step.pours ?? []).map((p) => sayPour(s, p));
752
+ if (pours.length === 0)
753
+ return `${when}: ${levels}.`;
754
+ return `${when}: an arrow ${pours.join(", then an arrow ")}; after ${pours.length > 1 ? "them" : "it"}, ${levels}.`;
755
+ });
756
+ return `${head} ${rows.join(" ")}`;
757
+ },
758
+ };