@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,880 @@
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, ink, isColor, outline, soft } from "../style.js";
5
+ import { n, poly } from "../svg.js";
6
+ import { checkThing, count, drawThing } from "../things.js";
7
+ import { article, mix } from "./cubes.js";
8
+ /**
9
+ * Gauges: measuring instruments, one to five of one sort side by side — a
10
+ * ruler, whole or a broken piece of one, with things laid along it; a
11
+ * thermometer; a kitchen scale with a dial and things on its pan; a
12
+ * measuring jug. An instrument's scale runs from one number to another: a
13
+ * number every step and a mark every tick. The level, the pointer or the end
14
+ * of a thing stands exactly on a mark, so the picture can be read as exactly
15
+ * as the scene says; the reading itself is never written.
16
+ */
17
+ export const instruments = ["ruler", "thermometer", "scale", "jug"];
18
+ export const objectNames = ["pencil", "crayon", "ribbon", "stick", "nail", "caterpillar"];
19
+ export const breaks = ["left", "right", "both"];
20
+ const room = 640 - 2 * margin;
21
+ const common = ["from", "to", "step", "tick", "unit", "label", "color"];
22
+ const membersOf = {
23
+ ruler: [...common, "objects", "broken"],
24
+ thermometer: [...common, "value"],
25
+ scale: [...common, "value", "things"],
26
+ jug: [...common, "value"],
27
+ };
28
+ /** How many of an instrument a picture holds, how many numbers each, the room a tick needs. */
29
+ const limits = {
30
+ ruler: { most: 3, numbers: 21, tick: 4 },
31
+ thermometer: { most: 5, numbers: 13, tick: 4.5 },
32
+ scale: { most: 3, numbers: 11, tick: 4 },
33
+ jug: { most: 4, numbers: 11, tick: 5 },
34
+ };
35
+ const defaultColor = {
36
+ ruler: "yellow",
37
+ thermometer: "red",
38
+ scale: "red",
39
+ jug: "blue",
40
+ };
41
+ const objectColor = {
42
+ pencil: "red",
43
+ crayon: "green",
44
+ ribbon: "pink",
45
+ stick: "brown",
46
+ nail: "gray",
47
+ caterpillar: "green",
48
+ };
49
+ const glass = "#e7f4fd";
50
+ const guide = colors.gray.dark;
51
+ // --- numbers on a scale -----------------------------------------------------------
52
+ const whole = (x) => Math.abs(x - Math.round(x)) < 1e-6;
53
+ const clean = (v) => Math.round(v * 1e6) / 1e6;
54
+ /** fmt writes a number as the picture shows it, a true minus sign before a negative one. */
55
+ export function fmt(v) {
56
+ const s = String(clean(v));
57
+ return s.startsWith("-") ? `−${s.slice(1)}` : s;
58
+ }
59
+ /** say writes a number for the words. */
60
+ function say(v) {
61
+ const s = String(clean(v));
62
+ return s.startsWith("-") ? `minus ${s.slice(1)}` : s;
63
+ }
64
+ const stepOf = (g) => g.step ?? 1;
65
+ const tickOf = (g) => g.tick ?? stepOf(g);
66
+ /** marks lists the marks of a scale from its first number up: big where a number stands. */
67
+ export function marks(g) {
68
+ const per = Math.round(stepOf(g) / tickOf(g));
69
+ const k = Math.round((g.to - g.from) / tickOf(g));
70
+ return Array.from({ length: k + 1 }, (_, i) => ({
71
+ v: clean(g.from + i * tickOf(g)),
72
+ big: i % per === 0,
73
+ mid: per % 2 === 0 && i % per === per / 2,
74
+ }));
75
+ }
76
+ /** ordinal writes 1st, 2nd, 3rd, 4th…: a step has at most nine small marks. */
77
+ function ordinal(k) {
78
+ return `${k}${k === 1 ? "st" : k === 2 ? "nd" : k === 3 ? "rd" : "th"}`;
79
+ }
80
+ /** where says where a reading stands, by the number below it and the small marks after it. */
81
+ function where(g, v, beyond) {
82
+ const k = Math.round((v - g.from) / tickOf(g));
83
+ const per = Math.round(stepOf(g) / tickOf(g));
84
+ const base = clean(g.from + Math.floor(k / per) * stepOf(g));
85
+ const rest = k % per;
86
+ return rest === 0
87
+ ? `the mark ${say(base)}`
88
+ : `the ${ordinal(rest)} small mark ${beyond} ${say(base)}`;
89
+ }
90
+ function sayScale(g, along) {
91
+ const per = Math.round(stepOf(g) / tickOf(g));
92
+ const small = per === 1
93
+ ? "no small marks between them"
94
+ : `${per - 1} small mark${per - 1 === 1 ? "" : "s"} between each two numbers`;
95
+ return `${g.unit ? `in ${g.unit}, ` : ""}its numbers from ${say(g.from)} to ${say(g.to)} in steps of ${say(stepOf(g))}${along}, with ${small}`;
96
+ }
97
+ // --- shared drawing helpers --------------------------------------------------------
98
+ const edge = { stroke: ink, "stroke-width": outline, "stroke-linejoin": "round" };
99
+ const labelSize = 15;
100
+ function widest(g) {
101
+ return Math.max(...marks(g)
102
+ .filter((m) => m.big)
103
+ .map((m) => textWidth(fmt(m.v), labelSize)));
104
+ }
105
+ // --- ruler ------------------------------------------------------------------------------
106
+ const rulerH = 64;
107
+ const laneH = 34;
108
+ function rulerPad(g) {
109
+ const labels = [g.label ?? "", ...(g.objects ?? []).map((o) => o.label ?? "")];
110
+ const w = Math.max(0, ...labels.map((l) => (l ? textWidth(l, 18) + 14 : 0)));
111
+ return w;
112
+ }
113
+ /** ends are how far a ruler reaches past its first number and its last: further where broken. */
114
+ function ends(g) {
115
+ return [
116
+ g.broken === "left" || g.broken === "both" ? 28 : 18,
117
+ g.broken === "right" || g.broken === "both" ? 28 : 18,
118
+ ];
119
+ }
120
+ /** rulerLength is the length of a ruler's scale, from its first number to its last. */
121
+ function rulerLength(g) {
122
+ const steps = (g.to - g.from) / stepOf(g);
123
+ const [l, r] = ends(g);
124
+ return Math.min(room - l - r - rulerPad(g), steps * 56);
125
+ }
126
+ function drawObject(pen, o, x1, x2, cy) {
127
+ const c = colors[o.color ?? objectColor[o.object]];
128
+ const body = { fill: c.fill, ...edge, class: "object" };
129
+ switch (o.object) {
130
+ case "pencil": {
131
+ const t = 20;
132
+ pen.el("rect", {
133
+ x: x1,
134
+ y: cy - t / 2,
135
+ width: 14,
136
+ height: t,
137
+ rx: 5,
138
+ fill: colors.pink.fill,
139
+ ...edge,
140
+ });
141
+ pen.el("path", {
142
+ d: poly([
143
+ { x: x2 - 30, y: cy - t / 2 },
144
+ { x: x2, y: cy },
145
+ { x: x2 - 30, y: cy + t / 2 },
146
+ ]),
147
+ fill: mix(colors.yellow.fill, "#ffffff", 0.55),
148
+ ...edge,
149
+ });
150
+ pen.el("path", {
151
+ d: poly([
152
+ { x: x2 - 10, y: cy - t / 6 },
153
+ { x: x2, y: cy },
154
+ { x: x2 - 10, y: cy + t / 6 },
155
+ ]),
156
+ fill: c.dark,
157
+ ...edge,
158
+ "stroke-width": 1.5,
159
+ });
160
+ pen.el("rect", { x: x1 + 22, y: cy - t / 2, width: x2 - 30 - x1 - 22, height: t, ...body });
161
+ pen.el("rect", {
162
+ x: x1 + 22,
163
+ y: cy - t / 6,
164
+ width: x2 - 30 - x1 - 22,
165
+ height: t / 3,
166
+ fill: mix(c.fill, "#ffffff", 0.35),
167
+ });
168
+ pen.el("rect", {
169
+ x: x1 + 12,
170
+ y: cy - t / 2,
171
+ width: 10,
172
+ height: t,
173
+ fill: colors.gray.fill,
174
+ ...edge,
175
+ });
176
+ return;
177
+ }
178
+ case "crayon": {
179
+ const t = 20;
180
+ pen.el("path", {
181
+ d: poly([
182
+ { x: x2 - 18, y: cy - t / 2 },
183
+ { x: x2, y: cy - t / 6 },
184
+ { x: x2, y: cy + t / 6 },
185
+ { x: x2 - 18, y: cy + t / 2 },
186
+ ]),
187
+ fill: c.fill,
188
+ ...edge,
189
+ });
190
+ pen.el("rect", {
191
+ x: x1,
192
+ y: cy - t / 2,
193
+ width: x2 - 18 - x1,
194
+ height: t,
195
+ rx: 4,
196
+ ...body,
197
+ });
198
+ const w = x2 - 18 - x1;
199
+ for (const f of [0.14, 0.2, 0.8, 0.86])
200
+ pen.el("path", {
201
+ d: `M${n(x1 + w * f)} ${n(cy - t / 2 + 1)}V${n(cy + t / 2 - 1)}`,
202
+ stroke: c.dark,
203
+ "stroke-width": 2.5,
204
+ });
205
+ return;
206
+ }
207
+ case "ribbon": {
208
+ const t = 18;
209
+ pen.el("path", {
210
+ d: poly([
211
+ { x: x1, y: cy - t / 2 },
212
+ { x: x2, y: cy - t / 2 },
213
+ { x: x2 - 7, y: cy },
214
+ { x: x2, y: cy + t / 2 },
215
+ { x: x1, y: cy + t / 2 },
216
+ { x: x1 + 7, y: cy },
217
+ ]),
218
+ ...body,
219
+ });
220
+ pen.el("path", {
221
+ d: `M${n(x1 + 10)} ${n(cy - t / 2 + 4)}H${n(x2 - 10)}M${n(x1 + 10)} ${n(cy + t / 2 - 4)}H${n(x2 - 10)}`,
222
+ stroke: mix(c.fill, "#ffffff", 0.45),
223
+ "stroke-width": 2,
224
+ });
225
+ return;
226
+ }
227
+ case "stick": {
228
+ const t = 14;
229
+ pen.el("rect", { x: x1, y: cy - t / 2, width: x2 - x1, height: t, rx: t / 2, ...body });
230
+ for (const f of [0.3, 0.68])
231
+ pen.el("ellipse", {
232
+ cx: x1 + (x2 - x1) * f,
233
+ cy: cy + 1,
234
+ rx: 5,
235
+ ry: 3,
236
+ fill: c.dark,
237
+ });
238
+ return;
239
+ }
240
+ case "nail": {
241
+ pen.el("path", {
242
+ d: poly([
243
+ { x: x1 + 5, y: cy - 4 },
244
+ { x: x2 - 12, y: cy - 4 },
245
+ { x: x2, y: cy },
246
+ { x: x2 - 12, y: cy + 4 },
247
+ { x: x1 + 5, y: cy + 4 },
248
+ ]),
249
+ ...body,
250
+ });
251
+ pen.el("rect", {
252
+ x: x1,
253
+ y: cy - 12,
254
+ width: 7,
255
+ height: 24,
256
+ rx: 2,
257
+ fill: c.dark,
258
+ ...edge,
259
+ });
260
+ return;
261
+ }
262
+ case "caterpillar": {
263
+ const r = 12;
264
+ const head = 14;
265
+ const k = Math.max(3, Math.round((x2 - x1 - head - r) / (r * 1.7)) + 1);
266
+ const step = (x2 - head - (x1 + r)) / (k - 1);
267
+ for (let i = 0; i < k - 1; i++) {
268
+ const x = x1 + r + i * step;
269
+ pen.el("path", {
270
+ d: `M${n(x - 4)} ${n(cy + r - 2)}l-2 7M${n(x + 4)} ${n(cy + r - 2)}l2 7`,
271
+ stroke: ink,
272
+ "stroke-width": 2,
273
+ "stroke-linecap": "round",
274
+ });
275
+ pen.el("circle", {
276
+ cx: x,
277
+ cy,
278
+ r,
279
+ fill: i % 2 ? c.fill : mix(c.fill, "#ffffff", 0.3),
280
+ ...edge,
281
+ class: i === 0 ? "object" : undefined,
282
+ });
283
+ }
284
+ const hx = x2 - head;
285
+ pen.el("path", {
286
+ d: `M${n(hx - 4)} ${n(cy - head + 2)}l-4 -9M${n(hx + 5)} ${n(cy - head + 2)}l3 -9`,
287
+ stroke: ink,
288
+ "stroke-width": 2,
289
+ "stroke-linecap": "round",
290
+ });
291
+ pen.el("circle", { cx: hx, cy: cy - 1, r: head, fill: c.dark, ...edge });
292
+ pen.el("circle", { cx: hx + 4, cy: cy - 4, r: 2.5, fill: "#ffffff" });
293
+ pen.el("circle", { cx: hx + 4, cy: cy - 4, r: 1.2, fill: ink });
294
+ return;
295
+ }
296
+ }
297
+ }
298
+ /** rulerEdge is one end of a ruler, from top to bottom: straight, or the jagged edge of a break. */
299
+ function rulerEdge(x, top, out, broken) {
300
+ if (!broken)
301
+ return [];
302
+ const teeth = 5;
303
+ return Array.from({ length: teeth * 2 - 1 }, (_, i) => ({
304
+ x: x + (i % 2 === 0 ? 0 : -out * 7),
305
+ y: top + ((i + 1) * rulerH) / (teeth * 2),
306
+ }));
307
+ }
308
+ function drawRuler(pen, g, width) {
309
+ const objects = g.objects ?? [];
310
+ const pad = rulerPad(g);
311
+ const len = rulerLength(g);
312
+ const [endL, endR] = ends(g);
313
+ const left = (width - pad - endL - len - endR) / 2 + pad + endL;
314
+ const u = len / (g.to - g.from);
315
+ const at = (v) => left + (v - g.from) * u;
316
+ const top = objects.length * laneH + (objects.length > 0 ? 6 : 0);
317
+ const c = colors[g.color ?? "yellow"];
318
+ const brokenL = g.broken === "left" || g.broken === "both";
319
+ const brokenR = g.broken === "right" || g.broken === "both";
320
+ const a = at(g.from) - endL;
321
+ const b = at(g.to) + endR;
322
+ const leftEnd = brokenL
323
+ ? [{ x: a, y: top + rulerH }, ...rulerEdge(a, top, -1, true).reverse(), { x: a, y: top }]
324
+ : [];
325
+ const rightEnd = brokenR ? rulerEdge(b, top, 1, true) : [];
326
+ if (brokenL || brokenR)
327
+ pen.el("path", {
328
+ d: poly([
329
+ { x: a, y: top },
330
+ { x: b, y: top },
331
+ ...rightEnd,
332
+ { x: b, y: top + rulerH },
333
+ { x: a, y: top + rulerH },
334
+ ...leftEnd.slice(1, -1),
335
+ ]),
336
+ fill: c.fill,
337
+ ...edge,
338
+ class: "ruler",
339
+ });
340
+ else
341
+ pen.el("rect", {
342
+ x: a,
343
+ y: top,
344
+ width: b - a,
345
+ height: rulerH,
346
+ rx: 6,
347
+ fill: c.fill,
348
+ ...edge,
349
+ class: "ruler",
350
+ });
351
+ for (const m of marks(g)) {
352
+ const x = at(m.v);
353
+ const len_ = m.big ? 20 : m.mid ? 15 : 10;
354
+ pen.el("path", {
355
+ d: `M${n(x)} ${n(top + 1)}V${n(top + len_)}`,
356
+ stroke: ink,
357
+ "stroke-width": m.big ? 2.2 : 1.6,
358
+ class: "mark",
359
+ });
360
+ if (m.big)
361
+ pen.text(x, top + 33, fmt(m.v), { size: 16 });
362
+ }
363
+ if (g.unit)
364
+ pen.text(at(g.from) - 2, top + 52, g.unit, { size: 14, anchor: "start", weight: 600 });
365
+ if (g.label)
366
+ pen.text(a - 12, top + rulerH / 2, g.label, { size: 18, anchor: "end" });
367
+ objects.forEach((o, i) => {
368
+ const cy = top - 6 - laneH / 2 - i * laneH;
369
+ const x1 = at(o.from);
370
+ const x2 = at(o.to);
371
+ for (const x of [x1, x2])
372
+ pen.el("path", {
373
+ d: `M${n(x)} ${n(cy + 8)}V${n(top - 1)}`,
374
+ stroke: guide,
375
+ "stroke-width": 1.5,
376
+ "stroke-dasharray": "4 3",
377
+ });
378
+ drawObject(pen, o, x1, x2, cy);
379
+ if (o.label)
380
+ pen.text(x1 - 10, cy, o.label, { size: 18, anchor: "end" });
381
+ });
382
+ return { width, height: top + rulerH };
383
+ }
384
+ // --- thermometer -------------------------------------------------------------------------
385
+ const thermoH = 220;
386
+ function thermoWidth(g) {
387
+ return Math.max(96, 58 + widest(g) + 14, g.unit ? textWidth(g.unit, 16) + 30 : 0);
388
+ }
389
+ function drawThermometer(pen, g) {
390
+ const bw = thermoWidth(g);
391
+ const tx = 30;
392
+ const yT = 44;
393
+ const yB = yT + thermoH;
394
+ const y = (v) => yB - ((v - g.from) / (g.to - g.from)) * thermoH;
395
+ const c = colors[g.color ?? "red"];
396
+ const bulbY = yB + 32;
397
+ pen.el("rect", { x: 0, y: 0, width: bw, height: bulbY + 30, rx: 16, fill: soft, ...edge });
398
+ if (g.unit)
399
+ pen.text(bw / 2, 20, g.unit, { size: 16 });
400
+ pen.el("circle", { cx: tx, cy: bulbY, r: 16, fill: c.fill, ...edge });
401
+ pen.el("rect", { x: tx - 8, y: yT - 16, width: 16, height: bulbY - yT, fill: "#ffffff" });
402
+ pen.el("path", {
403
+ d: `M${n(tx - 8)} ${n(bulbY - 13)}V${n(yT - 8)}a8 8 0 0 1 16 0V${n(bulbY - 13)}`,
404
+ fill: "none",
405
+ ...edge,
406
+ });
407
+ if (g.value !== undefined)
408
+ pen.el("rect", {
409
+ x: tx - 4,
410
+ y: y(g.value),
411
+ width: 8,
412
+ height: bulbY - y(g.value),
413
+ fill: c.fill,
414
+ class: "level",
415
+ });
416
+ for (const m of marks(g)) {
417
+ const yy = y(m.v);
418
+ pen.el("path", {
419
+ d: `M${n(tx + 12)} ${n(yy)}H${n(tx + 12 + (m.big ? 15 : m.mid ? 11 : 7))}`,
420
+ stroke: ink,
421
+ "stroke-width": m.big ? 2.2 : 1.6,
422
+ class: "mark",
423
+ });
424
+ if (m.big)
425
+ pen.text(tx + 32, yy, fmt(m.v), { size: labelSize, anchor: "start" });
426
+ }
427
+ return { width: bw, height: bulbY + 30 };
428
+ }
429
+ // --- kitchen scale ---------------------------------------------------------------------
430
+ const sweep = 270;
431
+ function dialRadius(k) {
432
+ return k >= 3 ? 72 : 82;
433
+ }
434
+ /** turn is the angle of a value on the dial, in degrees clockwise from the top. */
435
+ export function turn(g, v) {
436
+ return -sweep / 2 + (sweep * (v - g.from)) / (g.to - g.from);
437
+ }
438
+ function drawScale(pen, g, R) {
439
+ const things = g.things ?? [];
440
+ const w = 2 * R + 34;
441
+ const cx = w / 2;
442
+ const thingSize = things.length > 3 ? 40 : 46;
443
+ const panY = things.length > 0 ? thingSize + 4 : 6;
444
+ const bodyTop = panY + 22;
445
+ const cy = bodyTop + R + 16;
446
+ const bottom = cy + R + 20;
447
+ const c = colors[g.color ?? "red"];
448
+ // the pan on its stem, the things standing on it
449
+ pen.el("rect", {
450
+ x: cx - 10,
451
+ y: panY + 6,
452
+ width: 20,
453
+ height: bodyTop - panY,
454
+ fill: colors.gray.fill,
455
+ ...edge,
456
+ });
457
+ pen.el("path", {
458
+ d: poly([
459
+ { x: cx - R - 6, y: panY },
460
+ { x: cx + R + 6, y: panY },
461
+ { x: cx + R - 16, y: panY + 12 },
462
+ { x: cx - R + 16, y: panY + 12 },
463
+ ]),
464
+ fill: colors.gray.fill,
465
+ ...edge,
466
+ });
467
+ things.forEach((t, i) => {
468
+ drawThing(pen, t, cx + (i - (things.length - 1) / 2) * (thingSize + 2), panY - thingSize / 2 + 2, thingSize);
469
+ });
470
+ for (const dx of [-R * 0.6, R * 0.6])
471
+ pen.el("rect", { x: cx + dx - 14, y: bottom - 6, width: 28, height: 12, rx: 4, fill: ink });
472
+ pen.el("rect", {
473
+ x: 0,
474
+ y: bodyTop,
475
+ width: w,
476
+ height: bottom - bodyTop,
477
+ rx: 22,
478
+ fill: c.fill,
479
+ ...edge,
480
+ });
481
+ pen.el("circle", { cx, cy, r: R, fill: "#ffffff", ...edge });
482
+ const polar = (deg, r) => ({
483
+ x: cx + r * Math.sin((deg * Math.PI) / 180),
484
+ y: cy - r * Math.cos((deg * Math.PI) / 180),
485
+ });
486
+ for (const m of marks(g)) {
487
+ const a = turn(g, m.v);
488
+ const p = polar(a, R - 5);
489
+ const q = polar(a, R - 5 - (m.big ? 13 : m.mid ? 9 : 6));
490
+ pen.el("path", {
491
+ d: `M${n(p.x)} ${n(p.y)}L${n(q.x)} ${n(q.y)}`,
492
+ stroke: ink,
493
+ "stroke-width": m.big ? 2.2 : 1.6,
494
+ "stroke-linecap": "round",
495
+ class: "mark",
496
+ });
497
+ if (m.big) {
498
+ const t = polar(a, R - 30);
499
+ pen.text(t.x, t.y, fmt(m.v), { size: labelSize });
500
+ }
501
+ }
502
+ if (g.unit)
503
+ pen.text(cx, cy + R * 0.55, g.unit, { size: labelSize });
504
+ if (g.value !== undefined) {
505
+ const a = turn(g, g.value);
506
+ const tip = polar(a, R - 9);
507
+ const tail = polar(a + 180, 14);
508
+ const side = (s) => polar(a + 90 * s, 2);
509
+ pen.el("path", {
510
+ d: poly([tail, side(-1), tip, side(1)]),
511
+ fill: colors.red.fill,
512
+ ...edge,
513
+ "stroke-width": 1.2,
514
+ class: "pointer",
515
+ });
516
+ }
517
+ pen.el("circle", { cx, cy, r: 6, fill: ink });
518
+ return { width: w, height: bottom + 6 };
519
+ }
520
+ // --- measuring jug ------------------------------------------------------------------------
521
+ const jugH = 200;
522
+ const jugW = 104;
523
+ function drawJug(pen, g) {
524
+ const x0 = 16;
525
+ const x1 = x0 + jugW;
526
+ const yb = jugH + 40;
527
+ const yt = yb - jugH - 36;
528
+ const y = (v) => yb - (v / g.to) * jugH;
529
+ const c = colors[g.color ?? "blue"];
530
+ // the handle, a hollow loop on the right
531
+ const loop = `M${n(x1 - 2)} ${n(yt + 26)}C${n(x1 + 40)} ${n(yt + 22)} ${n(x1 + 34)} ${n(yt + 120)} ${n(x1 - 2)} ${n(yt + 132)}`;
532
+ pen.el("path", {
533
+ d: loop,
534
+ fill: "none",
535
+ stroke: ink,
536
+ "stroke-width": 12,
537
+ "stroke-linecap": "round",
538
+ });
539
+ pen.el("path", {
540
+ d: loop,
541
+ fill: "none",
542
+ stroke: glass,
543
+ "stroke-width": 6.5,
544
+ "stroke-linecap": "round",
545
+ });
546
+ const r = 14;
547
+ pen.el("path", {
548
+ d: `M${n(x0 - 14)} ${n(yt - 6)}Q${n(x0 + 2)} ${n(yt)} ${n(x0 + 22)} ${n(yt)}H${n(x1)}V${n(yb - r)}Q${n(x1)} ${n(yb)} ${n(x1 - r)} ${n(yb)}H${n(x0 + r)}Q${n(x0)} ${n(yb)} ${n(x0)} ${n(yb - r)}V${n(yt + 16)}Q${n(x0)} ${n(yt + 4)} ${n(x0 - 14)} ${n(yt - 6)}Z`,
549
+ fill: glass,
550
+ ...edge,
551
+ });
552
+ if (g.value !== undefined && g.value > 0) {
553
+ const top = y(g.value);
554
+ const i = outline / 2;
555
+ const rr = Math.min(r - i, yb - i - top);
556
+ pen.el("path", {
557
+ d: `M${n(x0 + i)} ${n(top)}H${n(x1 - i)}V${n(yb - i - rr)}Q${n(x1 - i)} ${n(yb - i)} ${n(x1 - i - rr)} ${n(yb - i)}H${n(x0 + i + rr)}Q${n(x0 + i)} ${n(yb - i)} ${n(x0 + i)} ${n(yb - i - rr)}Z`,
558
+ fill: mix(c.fill, "#ffffff", 0.45),
559
+ class: "level",
560
+ });
561
+ pen.el("path", {
562
+ d: `M${n(x0 + i)} ${n(top)}H${n(x1 - i)}`,
563
+ stroke: c.dark,
564
+ "stroke-width": 2.5,
565
+ });
566
+ }
567
+ for (const m of marks(g)) {
568
+ const yy = y(m.v);
569
+ pen.el("path", {
570
+ d: `M${n(x0 + 1)} ${n(yy)}H${n(x0 + (m.big ? 19 : m.mid ? 14 : 9))}`,
571
+ stroke: ink,
572
+ "stroke-width": m.big ? 2.2 : 1.6,
573
+ class: "mark",
574
+ });
575
+ if (m.big)
576
+ pen.text(x0 + 25, yy, fmt(m.v), { size: labelSize, anchor: "start" });
577
+ }
578
+ if (g.unit)
579
+ pen.text(x0 + 25, y(g.to) - 21, g.unit, { size: 14, anchor: "start", weight: 600 });
580
+ return { width: x1 + 30, height: yb + 4 };
581
+ }
582
+ // --- the check ----------------------------------------------------------------------------
583
+ function isNumber(v, places = 2) {
584
+ return typeof v === "number" && Number.isFinite(v) && whole(v * 10 ** places);
585
+ }
586
+ function checkNumber(v, path, problems, positive = false) {
587
+ if (!isNumber(v) || (positive && v <= 0)) {
588
+ problems.push(`${path}: must be a ${positive ? "positive " : ""}number with at most two decimals`);
589
+ return false;
590
+ }
591
+ return true;
592
+ }
593
+ /** onMark records a problem unless v lies on a mark of the scale. */
594
+ function onMark(g, v, path, problems, what) {
595
+ if (!checkNumber(v, path, problems))
596
+ return;
597
+ if (v < g.from || v > g.to)
598
+ problems.push(`${path}: must lie on the scale, from ${fmt(g.from)} to ${fmt(g.to)}`);
599
+ else if (!whole((v - g.from) / tickOf(g)))
600
+ problems.push(`${path}: ${what} must stand on a mark: ${fmt(g.from)} and every ${fmt(tickOf(g))} after it`);
601
+ }
602
+ function checkScale(g, path, instrument, problems) {
603
+ const before = problems.length;
604
+ checkNumber(g.from, `${path}.from`, problems);
605
+ checkNumber(g.to, `${path}.to`, problems);
606
+ if (g.step !== undefined)
607
+ checkNumber(g.step, `${path}.step`, problems, true);
608
+ if (g.tick !== undefined)
609
+ checkNumber(g.tick, `${path}.tick`, problems, true);
610
+ if (problems.length > before)
611
+ return false;
612
+ const s = g;
613
+ if (s.to <= s.from) {
614
+ problems.push(`${path}.to: must be greater than from`);
615
+ return false;
616
+ }
617
+ if (instrument === "jug" && s.from < 0) {
618
+ problems.push(`${path}.from: a jug's scale starts at 0 or above`);
619
+ return false;
620
+ }
621
+ const per = stepOf(s) / tickOf(s);
622
+ if (!whole(per) || per > 10 || per < 1) {
623
+ problems.push(`${path}.tick: must divide the step into 1 to 10 equal parts`);
624
+ return false;
625
+ }
626
+ if (!whole((s.to - s.from) / stepOf(s))) {
627
+ problems.push(`${path}.to: must be from plus a whole number of steps`);
628
+ return false;
629
+ }
630
+ const numbers = Math.round((s.to - s.from) / stepOf(s)) + 1;
631
+ const most = limits[instrument].numbers;
632
+ if (numbers > most) {
633
+ problems.push(`${path}.step: gives ${numbers} numbers; at most ${most} fit, use a bigger step`);
634
+ return false;
635
+ }
636
+ return true;
637
+ }
638
+ /** crowding records a problem when the numbers or the marks of a scale stand too close. */
639
+ function crowding(g, path, instrument, k, problems) {
640
+ const steps = (g.to - g.from) / stepOf(g);
641
+ const ticks = (g.to - g.from) / tickOf(g);
642
+ const label = widest(g);
643
+ let gap;
644
+ let tick;
645
+ let need = 20;
646
+ if (instrument === "ruler") {
647
+ const len = rulerLength(g);
648
+ gap = len / steps;
649
+ tick = len / ticks;
650
+ need = textWidth(fmt(g.to), 16) + 6;
651
+ }
652
+ else if (instrument === "thermometer") {
653
+ gap = thermoH / steps;
654
+ tick = thermoH / ticks;
655
+ }
656
+ else if (instrument === "jug") {
657
+ gap = (jugH * stepOf(g)) / g.to;
658
+ tick = (jugH * tickOf(g)) / g.to;
659
+ }
660
+ else {
661
+ const R = dialRadius(k);
662
+ const angle = ((sweep / steps) * Math.PI) / 180;
663
+ gap = 2 * (R - 30) * Math.sin(angle / 2);
664
+ need = label + 4;
665
+ tick = ((R - 5) * sweep * Math.PI) / 180 / ticks;
666
+ }
667
+ if (gap < need)
668
+ problems.push(`${path}.step: the numbers stand too close to read; use a bigger step`);
669
+ else if (tick < limits[instrument].tick)
670
+ problems.push(`${path}.tick: the marks stand too close to read; use a bigger tick`);
671
+ }
672
+ function checkGauge(g, path, instrument, k, problems) {
673
+ if (!isObj(g)) {
674
+ problems.push(`${path}: must be ${article(instrument)} with its scale`);
675
+ return;
676
+ }
677
+ const members = membersOf[instrument];
678
+ for (const key of Object.keys(g))
679
+ if (!members.includes(key))
680
+ problems.push(`${path}.${key}: not a member of ${article(instrument)}; it takes ${members.join(", ")}`);
681
+ if (g.unit !== undefined)
682
+ text(g.unit, `${path}.unit`, problems, 4);
683
+ if (g.label !== undefined)
684
+ text(g.label, `${path}.label`, problems, 8);
685
+ if (g.color !== undefined && !isColor(g.color))
686
+ problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
687
+ const ok = checkScale(g, `${path}`, instrument, problems);
688
+ if (!ok)
689
+ return;
690
+ const s = g;
691
+ crowding(s, path, instrument, k, problems);
692
+ if (g.value !== undefined)
693
+ onMark(s, g.value, `${path}.value`, problems, "the reading");
694
+ if (g.things !== undefined)
695
+ list(g.things, `${path}.things`, problems, 1, 4).forEach((t, i) => {
696
+ checkThing(t, `${path}.things[${i}]`, problems);
697
+ });
698
+ if (g.broken !== undefined)
699
+ oneOf(g.broken, breaks, `${path}.broken`, problems);
700
+ if (g.objects !== undefined) {
701
+ const len = rulerLength(s);
702
+ list(g.objects, `${path}.objects`, problems, 1, 3).forEach((o, i) => {
703
+ const p = `${path}.objects[${i}]`;
704
+ if (!isObj(o)) {
705
+ problems.push(`${p}: must be an object laid from a mark to a mark`);
706
+ return;
707
+ }
708
+ only(o, ["object", "from", "to", "color", "label"], p, problems);
709
+ oneOf(o.object, objectNames, `${p}.object`, problems);
710
+ if (o.color !== undefined && !isColor(o.color))
711
+ problems.push(`${p}.color: must be a colour: ${colorNames.join(", ")}`);
712
+ if (o.label !== undefined)
713
+ text(o.label, `${p}.label`, problems, 3);
714
+ const before = problems.length;
715
+ onMark(s, o.from, `${p}.from`, problems, "an end");
716
+ onMark(s, o.to, `${p}.to`, problems, "an end");
717
+ if (problems.length > before)
718
+ return;
719
+ const from = o.from;
720
+ const to = o.to;
721
+ if (to <= from)
722
+ problems.push(`${p}.to: must be greater than from`);
723
+ else if (((to - from) / (s.to - s.from)) * len < 56)
724
+ problems.push(`${p}: too short to draw; it must span at least 56 pixels of the ruler`);
725
+ });
726
+ }
727
+ }
728
+ // --- words ----------------------------------------------------------------------------------
729
+ const nameOf = {
730
+ ruler: "ruler",
731
+ thermometer: "thermometer",
732
+ scale: "kitchen scale",
733
+ jug: "measuring jug",
734
+ };
735
+ function sameScale(gs) {
736
+ const key = (g) => [g.from, g.to, stepOf(g), tickOf(g), g.unit ?? ""].join(" ");
737
+ return gs.every((g) => key(g) === key(gs[0]));
738
+ }
739
+ function sayObjects(g) {
740
+ const objects = g.objects ?? [];
741
+ if (objects.length === 0)
742
+ return "";
743
+ const one = (o) => `${o.label ? `${o.label}, ` : ""}${article(`${colors[o.color ?? objectColor[o.object]].word} ${o.object}`)} from ${where(g, o.from, "after")} to ${where(g, o.to, "after")}`;
744
+ if (objects.length === 1)
745
+ return ` Along it lies ${one(objects[0])}.`;
746
+ return ` Along it, one above another from the ruler up: ${objects.map(one).join("; ")}.`;
747
+ }
748
+ function sayReading(instrument, g) {
749
+ const color = colors[g.color ?? defaultColor[instrument]].word;
750
+ if (instrument === "ruler")
751
+ return sayObjects(g).trim();
752
+ if (instrument === "thermometer")
753
+ return g.value === undefined
754
+ ? `its ${color} column is not drawn`
755
+ : `its ${color} column reaches ${where(g, g.value, "above")}`;
756
+ if (instrument === "jug")
757
+ return g.value === undefined
758
+ ? "it is empty"
759
+ : g.value === 0
760
+ ? "it is empty"
761
+ : `the ${color} liquid in it reaches ${where(g, g.value, "above")}`;
762
+ const things = g.things ?? [];
763
+ const on = things.length ? `on its pan ${groupWords(things)}; ` : "its pan is empty; ";
764
+ return `${on}${g.value === undefined ? "the pointer is not drawn" : `the pointer points at ${where(g, g.value, "after")}`}`;
765
+ }
766
+ function groupWords(things) {
767
+ const out = [];
768
+ for (const t of things) {
769
+ const seen = out.find((o) => JSON.stringify(o.thing) === JSON.stringify(t));
770
+ if (seen)
771
+ seen.k++;
772
+ else
773
+ out.push({ thing: t, k: 1 });
774
+ }
775
+ const parts = out.map((o) => count(o.thing, o.k));
776
+ return parts.length === 1 ? parts[0] : `${parts.slice(0, -1).join(", ")} and ${parts.at(-1)}`;
777
+ }
778
+ function alongOf(instrument) {
779
+ return instrument === "scale"
780
+ ? " round its dial"
781
+ : instrument === "ruler"
782
+ ? " along it"
783
+ : " up its side";
784
+ }
785
+ export const gauges = {
786
+ kind: "gauges",
787
+ members: ["kind", "instrument", "gauges"],
788
+ check(scene, problems) {
789
+ only(scene, this.members, "scene", problems);
790
+ const before = problems.length;
791
+ const instrument = oneOf(scene.instrument, instruments, "instrument", problems);
792
+ if (problems.length > before)
793
+ return;
794
+ const most = limits[instrument].most;
795
+ const gs = list(scene.gauges, "gauges", problems, 1, most);
796
+ gs.forEach((g, i) => {
797
+ checkGauge(g, `gauges[${i}]`, instrument, gs.length, problems);
798
+ });
799
+ if (problems.length > before)
800
+ return;
801
+ const typed = gs;
802
+ if (instrument === "thermometer") {
803
+ const w = typed.reduce((sum, g) => sum + thermoWidth(g) + 18, -18);
804
+ if (w > room)
805
+ problems.push("gauges: too wide side by side; use fewer thermometers");
806
+ }
807
+ if (instrument === "jug" && typed.length * (jugW + 46) + (typed.length - 1) * 14 > room)
808
+ problems.push("gauges: too wide side by side; use fewer jugs");
809
+ },
810
+ draw(scene, pen) {
811
+ const s = scene;
812
+ const gs = s.gauges;
813
+ const labelled = gs.some((g) => g.label);
814
+ if (s.instrument === "ruler") {
815
+ let y = margin;
816
+ for (const g of gs) {
817
+ const sub = pen.sub();
818
+ const box = drawRuler(sub, g, room);
819
+ pen.place(sub, margin, y);
820
+ y += box.height + 22;
821
+ }
822
+ return { width: 640, height: y - 22 + margin };
823
+ }
824
+ const R = dialRadius(gs.length);
825
+ const boxes = gs.map((g) => {
826
+ const sub = pen.sub();
827
+ const box = s.instrument === "thermometer"
828
+ ? drawThermometer(sub, g)
829
+ : s.instrument === "scale"
830
+ ? drawScale(sub, g, R)
831
+ : drawJug(sub, g);
832
+ return { sub, box, g };
833
+ });
834
+ const gap = s.instrument === "thermometer" ? 18 : 14;
835
+ const high = Math.max(...boxes.map((b) => b.box.height));
836
+ let x = margin;
837
+ for (const b of boxes) {
838
+ pen.place(b.sub, x, margin + high - b.box.height);
839
+ if (b.g.label)
840
+ pen.text(x + (s.instrument === "jug" ? 68 : b.box.width / 2), margin + high + 20, b.g.label, {
841
+ size: 18,
842
+ });
843
+ x += b.box.width + gap;
844
+ }
845
+ return { width: x - gap + margin, height: margin + high + (labelled ? 36 : 0) + margin };
846
+ },
847
+ describe(scene) {
848
+ const s = scene;
849
+ const gs = s.gauges;
850
+ const name = (g) => s.instrument === "ruler" && g.broken
851
+ ? `broken piece of a ruler, broken off at ${g.broken === "both" ? "both ends" : `the ${g.broken} end`}`
852
+ : nameOf[s.instrument];
853
+ if (gs.length === 1) {
854
+ const g = gs[0];
855
+ const label = g.label ? ` labelled ${g.label}` : "";
856
+ if (s.instrument === "ruler")
857
+ return `${cap(article(name(g)))}${label}, ${sayScale(g, alongOf("ruler"))}.${sayObjects(g)}`;
858
+ return `${cap(article(name(g)))}${label}, ${sayScale(g, alongOf(s.instrument))}; ${sayReading(s.instrument, g)}.`;
859
+ }
860
+ const plural = s.instrument === "scale" ? "kitchen scales" : `${nameOf[s.instrument]}s`;
861
+ const where_ = s.instrument === "ruler" ? "one below another" : "side by side";
862
+ const shared = s.instrument !== "ruler" && sameScale(gs);
863
+ const head = shared
864
+ ? `${gs.length} ${plural} ${where_}, each ${sayScale(gs[0], alongOf(s.instrument))}.`
865
+ : `${gs.length} ${plural} ${where_}.`;
866
+ const each = gs.map((g, i) => {
867
+ const id = g.label ?? String(i + 1);
868
+ const title = s.instrument === "ruler" ? cap(name(g)) : cap(nameOf[s.instrument]);
869
+ if (s.instrument === "ruler")
870
+ return `${title} ${id}, ${sayScale(g, alongOf("ruler"))}.${sayObjects(g)}`;
871
+ return shared
872
+ ? `${title} ${id}: ${sayReading(s.instrument, g)}.`
873
+ : `${title} ${id}, ${sayScale(g, alongOf(s.instrument))}; ${sayReading(s.instrument, g)}.`;
874
+ });
875
+ return `${head} ${each.join(" ")}`;
876
+ },
877
+ };
878
+ function cap(words) {
879
+ return `${words[0].toUpperCase()}${words.slice(1)}`;
880
+ }