@altnovatrail/picture-kit 0.0.0-stage → 0.1.0

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 +27 -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,7 @@
1
+ export type ObjectEntry = {
2
+ code: string;
3
+ word: string;
4
+ words: string;
5
+ };
6
+ export declare const objects: Record<string, ObjectEntry>;
7
+ export declare function isObject(id: unknown): id is string;
package/lib/objects.js ADDED
@@ -0,0 +1,226 @@
1
+ // The objects a scene may name, each by an id of its own, the Noto Emoji it
2
+ // is drawn as, and its word, singular and plural, for the text that reads the
3
+ // picture aloud. A scene names nothing outside this list: a story that needs
4
+ // another object waits until the list has it.
5
+ function o(code, word, words = `${word}s`) {
6
+ return { code, word, words };
7
+ }
8
+ export const objects = {
9
+ // fruit
10
+ "fruit.apple": o("1f34e", "apple"),
11
+ "fruit.green-apple": o("1f34f", "green apple"),
12
+ "fruit.pear": o("1f350", "pear"),
13
+ "fruit.tangerine": o("1f34a", "tangerine"),
14
+ "fruit.lemon": o("1f34b", "lemon"),
15
+ "fruit.banana": o("1f34c", "banana"),
16
+ "fruit.watermelon": o("1f349", "watermelon"),
17
+ "fruit.melon": o("1f348", "melon"),
18
+ "fruit.grapes": o("1f347", "bunch of grapes", "bunches of grapes"),
19
+ "fruit.strawberry": o("1f353", "strawberry", "strawberries"),
20
+ "fruit.cherries": o("1f352", "pair of cherries", "pairs of cherries"),
21
+ "fruit.peach": o("1f351", "peach", "peaches"),
22
+ "fruit.pineapple": o("1f34d", "pineapple"),
23
+ "fruit.kiwi": o("1f95d", "kiwi"),
24
+ "fruit.coconut": o("1f965", "coconut"),
25
+ "fruit.mango": o("1f96d", "mango", "mangoes"),
26
+ "fruit.blueberries": o("1fad0", "handful of blueberries", "handfuls of blueberries"),
27
+ // vegetables
28
+ "veg.carrot": o("1f955", "carrot"),
29
+ "veg.tomato": o("1f345", "tomato", "tomatoes"),
30
+ "veg.corn": o("1f33d", "ear of corn", "ears of corn"),
31
+ "veg.cucumber": o("1f952", "cucumber"),
32
+ "veg.broccoli": o("1f966", "broccoli", "broccoli"),
33
+ "veg.eggplant": o("1f346", "eggplant"),
34
+ "veg.potato": o("1f954", "potato", "potatoes"),
35
+ "veg.mushroom": o("1f344", "mushroom"),
36
+ "veg.pepper": o("1fad1", "pepper"),
37
+ "veg.onion": o("1f9c5", "onion"),
38
+ "veg.garlic": o("1f9c4", "garlic", "garlic"),
39
+ "veg.chestnut": o("1f330", "chestnut"),
40
+ // food
41
+ "food.ice-cream": o("1f366", "ice cream cone"),
42
+ "food.ice-cream-cup": o("1f368", "cup of ice cream", "cups of ice cream"),
43
+ "food.cookie": o("1f36a", "cookie"),
44
+ "food.cake": o("1f370", "piece of cake", "pieces of cake"),
45
+ "food.birthday-cake": o("1f382", "birthday cake"),
46
+ "food.cupcake": o("1f9c1", "cupcake"),
47
+ "food.candy": o("1f36c", "candy", "candies"),
48
+ "food.lollipop": o("1f36d", "lollipop"),
49
+ "food.doughnut": o("1f369", "doughnut"),
50
+ "food.chocolate": o("1f36b", "chocolate bar"),
51
+ "food.bread": o("1f35e", "loaf of bread", "loaves of bread"),
52
+ "food.croissant": o("1f950", "croissant"),
53
+ "food.cheese": o("1f9c0", "piece of cheese", "pieces of cheese"),
54
+ "food.egg": o("1f95a", "egg"),
55
+ "food.pizza": o("1f355", "slice of pizza", "slices of pizza"),
56
+ "food.pie": o("1f967", "pie"),
57
+ "food.honey": o("1f36f", "pot of honey", "pots of honey"),
58
+ "food.milk": o("1f95b", "glass of milk", "glasses of milk"),
59
+ "food.juice": o("1f9c3", "juice box", "juice boxes"),
60
+ "food.pancakes": o("1f95e", "stack of pancakes", "stacks of pancakes"),
61
+ // animals
62
+ "animal.dog": o("1f415", "dog"),
63
+ "animal.cat": o("1f408", "cat"),
64
+ "animal.rabbit": o("1f407", "rabbit"),
65
+ "animal.mouse": o("1f401", "mouse", "mice"),
66
+ "animal.kangaroo": o("1f998", "kangaroo"),
67
+ "animal.elephant": o("1f418", "elephant"),
68
+ "animal.giraffe": o("1f992", "giraffe"),
69
+ "animal.zebra": o("1f993", "zebra"),
70
+ "animal.monkey": o("1f412", "monkey"),
71
+ "animal.horse": o("1f40e", "horse"),
72
+ "animal.cow": o("1f404", "cow"),
73
+ "animal.pig": o("1f416", "pig"),
74
+ "animal.sheep": o("1f411", "sheep", "sheep"),
75
+ "animal.goat": o("1f410", "goat"),
76
+ "animal.hen": o("1f414", "hen"),
77
+ "animal.chick": o("1f424", "chick"),
78
+ "animal.duck": o("1f986", "duck"),
79
+ "animal.owl": o("1f989", "owl"),
80
+ "animal.penguin": o("1f427", "penguin"),
81
+ "animal.turtle": o("1f422", "turtle"),
82
+ "animal.snail": o("1f40c", "snail"),
83
+ "animal.butterfly": o("1f98b", "butterfly", "butterflies"),
84
+ "animal.bee": o("1f41d", "bee"),
85
+ "animal.ladybug": o("1f41e", "ladybug"),
86
+ "animal.ant": o("1f41c", "ant"),
87
+ "animal.fish": o("1f41f", "fish", "fish"),
88
+ "animal.tropical-fish": o("1f420", "tropical fish", "tropical fish"),
89
+ "animal.octopus": o("1f419", "octopus", "octopuses"),
90
+ "animal.crab": o("1f980", "crab"),
91
+ "animal.snake": o("1f40d", "snake"),
92
+ "animal.hedgehog": o("1f994", "hedgehog"),
93
+ "animal.chipmunk": o("1f43f", "chipmunk"),
94
+ "animal.tiger": o("1f405", "tiger"),
95
+ "animal.dragon": o("1f409", "dragon"),
96
+ "animal.bird": o("1f426", "bird"),
97
+ "animal.parrot": o("1f99c", "parrot"),
98
+ "animal.dolphin": o("1f42c", "dolphin"),
99
+ "animal.whale": o("1f40b", "whale"),
100
+ "animal.frog": o("1f438", "frog"),
101
+ "animal.bear": o("1f43b", "bear"),
102
+ "animal.fox": o("1f98a", "fox", "foxes"),
103
+ "animal.lion": o("1f981", "lion"),
104
+ "animal.unicorn": o("1f984", "unicorn"),
105
+ "animal.panda": o("1f43c", "panda"),
106
+ "animal.koala": o("1f428", "koala"),
107
+ "animal.camel": o("1f42b", "camel"),
108
+ "animal.swan": o("1f9a2", "swan"),
109
+ "animal.peacock": o("1f99a", "peacock"),
110
+ "animal.beaver": o("1f9ab", "beaver"),
111
+ // nature
112
+ "nature.tree": o("1f333", "tree"),
113
+ "nature.fir": o("1f332", "fir tree"),
114
+ "nature.palm": o("1f334", "palm tree"),
115
+ "nature.cactus": o("1f335", "cactus", "cacti"),
116
+ "nature.tulip": o("1f337", "tulip"),
117
+ "nature.sunflower": o("1f33b", "sunflower"),
118
+ "nature.daisy": o("1f33c", "flower"),
119
+ "nature.rose": o("1f339", "rose"),
120
+ "nature.hibiscus": o("1f33a", "hibiscus", "hibiscuses"),
121
+ "nature.seedling": o("1f331", "seedling"),
122
+ "nature.clover": o("1f340", "four-leaf clover"),
123
+ "nature.maple-leaf": o("1f341", "maple leaf", "maple leaves"),
124
+ "nature.leaf": o("1f343", "leaf", "leaves"),
125
+ "nature.sun": o("2600", "sun"),
126
+ "nature.star": o("2b50", "star"),
127
+ "nature.moon": o("1f319", "moon"),
128
+ "nature.cloud": o("2601", "cloud"),
129
+ "nature.rainbow": o("1f308", "rainbow"),
130
+ "nature.snowflake": o("2744", "snowflake"),
131
+ "nature.drop": o("1f4a7", "drop"),
132
+ "nature.fire": o("1f525", "fire", "fires"),
133
+ "nature.rock": o("1faa8", "rock"),
134
+ "nature.wood": o("1fab5", "log"),
135
+ "nature.shell": o("1f41a", "shell"),
136
+ "nature.snowman": o("26c4", "snowman", "snowmen"),
137
+ // toys and play
138
+ "toy.soccer-ball": o("26bd", "soccer ball"),
139
+ "toy.basketball": o("1f3c0", "basketball"),
140
+ "toy.tennis-ball": o("1f3be", "tennis ball"),
141
+ "toy.baseball": o("26be", "baseball"),
142
+ "toy.volleyball": o("1f3d0", "volleyball"),
143
+ "toy.balloon": o("1f388", "balloon"),
144
+ "toy.gift": o("1f381", "gift"),
145
+ "toy.teddy": o("1f9f8", "teddy bear"),
146
+ "toy.kite": o("1fa81", "kite"),
147
+ "toy.yo-yo": o("1fa80", "yo-yo"),
148
+ "toy.puzzle": o("1f9e9", "puzzle piece"),
149
+ "toy.die": o("1f3b2", "die", "dice"),
150
+ "toy.blocks": o("1f9f1", "brick"),
151
+ "toy.doll": o("1fa86", "nesting doll"),
152
+ "toy.robot": o("1f916", "robot"),
153
+ "toy.rocket": o("1f680", "rocket"),
154
+ "toy.trophy": o("1f3c6", "trophy", "trophies"),
155
+ "toy.medal": o("1f3c5", "medal"),
156
+ "toy.guitar": o("1f3b8", "guitar"),
157
+ "toy.drum": o("1f941", "drum"),
158
+ "toy.trumpet": o("1f3ba", "trumpet"),
159
+ "toy.bell": o("1f514", "bell"),
160
+ // things
161
+ "thing.pencil": o("270f", "pencil"),
162
+ "thing.crayon": o("1f58d", "crayon"),
163
+ "thing.book": o("1f4d5", "book"),
164
+ "thing.books": o("1f4da", "pile of books", "piles of books"),
165
+ "thing.notebook": o("1f4d2", "notebook"),
166
+ "thing.scissors": o("2702", "pair of scissors", "pairs of scissors"),
167
+ "thing.ruler": o("1f4cf", "ruler"),
168
+ "thing.paperclip": o("1f4ce", "paperclip"),
169
+ "thing.key": o("1f511", "key"),
170
+ "thing.umbrella": o("2602", "umbrella"),
171
+ "thing.hat": o("1f3a9", "hat"),
172
+ "thing.cap": o("1f9e2", "cap"),
173
+ "thing.crown": o("1f451", "crown"),
174
+ "thing.glasses": o("1f453", "pair of glasses", "pairs of glasses"),
175
+ "thing.sock": o("1f9e6", "pair of socks", "pairs of socks"),
176
+ "thing.gloves": o("1f9e4", "pair of gloves", "pairs of gloves"),
177
+ "thing.shoe": o("1f45f", "shoe"),
178
+ "thing.boot": o("1f462", "boot"),
179
+ "thing.t-shirt": o("1f455", "T-shirt"),
180
+ "thing.dress": o("1f457", "dress", "dresses"),
181
+ "thing.scarf": o("1f9e3", "scarf", "scarves"),
182
+ "thing.backpack": o("1f392", "backpack"),
183
+ "thing.bulb": o("1f4a1", "light bulb"),
184
+ "thing.candle": o("1f56f", "candle"),
185
+ "thing.alarm-clock": o("23f0", "alarm clock"),
186
+ "thing.hourglass": o("231b", "hourglass", "hourglasses"),
187
+ "thing.coin": o("1fa99", "coin"),
188
+ "thing.gem": o("1f48e", "gem"),
189
+ "thing.ring": o("1f48d", "ring"),
190
+ "thing.magnet": o("1f9f2", "magnet"),
191
+ "thing.button": o("1f518", "button"),
192
+ "thing.envelope": o("2709", "envelope"),
193
+ "thing.basket": o("1f9fa", "basket"),
194
+ "thing.bucket": o("1faa3", "bucket"),
195
+ "thing.teapot": o("1fad6", "teapot"),
196
+ "thing.cup": o("2615", "cup"),
197
+ "thing.spoon": o("1f944", "spoon"),
198
+ "thing.heart": o("2764", "heart"),
199
+ "thing.flag": o("1f6a9", "flag"),
200
+ "thing.lantern": o("1f3ee", "lantern"),
201
+ // transport and places
202
+ "transport.bicycle": o("1f6b2", "bicycle"),
203
+ "transport.car": o("1f697", "car"),
204
+ "transport.bus": o("1f68c", "bus", "buses"),
205
+ "transport.truck": o("1f69a", "truck"),
206
+ "transport.tractor": o("1f69c", "tractor"),
207
+ "transport.locomotive": o("1f682", "locomotive"),
208
+ "transport.airplane": o("2708", "airplane"),
209
+ "transport.helicopter": o("1f681", "helicopter"),
210
+ "transport.sailboat": o("26f5", "sailboat"),
211
+ "transport.ship": o("1f6a2", "ship"),
212
+ "transport.scooter": o("1f6f4", "scooter"),
213
+ "place.house": o("1f3e0", "house"),
214
+ "place.tent": o("26fa", "tent"),
215
+ "place.castle": o("1f3f0", "castle"),
216
+ "place.island": o("1f3dd", "island"),
217
+ "place.mountain": o("26f0", "mountain"),
218
+ // people
219
+ "person.child": o("1f9d2", "child", "children"),
220
+ "person.boy": o("1f466", "boy"),
221
+ "person.girl": o("1f467", "girl"),
222
+ "person.baby": o("1f476", "baby", "babies"),
223
+ };
224
+ export function isObject(id) {
225
+ return typeof id === "string" && Object.hasOwn(objects, id);
226
+ }
package/lib/pen.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { emojiBox } from "./emoji.ts";
2
+ import { type Attrs } from "./svg.ts";
3
+ /**
4
+ * Pen collects the markup of one picture and the emoji it places. A kind draws
5
+ * with it at the picture's own scale, origin at the top left; a nested picture,
6
+ * such as an option of a choice, draws with a pen of its own, which the outer
7
+ * pen then places, scaled.
8
+ */
9
+ export declare class Pen {
10
+ readonly codes: Set<string>;
11
+ private readonly parts;
12
+ constructor(codes?: Set<string>);
13
+ /** sub is a pen for a nested picture; its emoji count as this picture's. */
14
+ sub(): Pen;
15
+ add(markup: string): void;
16
+ el(tag: string, values: Attrs, children?: string): void;
17
+ /** emoji places an emoji of Noto Emoji, centred, size pixels square. */
18
+ emoji(code: string, cx: number, cy: number, size: number): void;
19
+ /** text writes a label, centred on x unless told otherwise, its middle on y. */
20
+ text(x: number, y: number, words: string, options?: {
21
+ size?: number;
22
+ weight?: number;
23
+ fill?: string;
24
+ anchor?: "start" | "middle" | "end";
25
+ }): void;
26
+ /** place puts a nested pen's picture at x, y, scaled. */
27
+ place(inner: Pen, x: number, y: number, scale?: number): void;
28
+ markup(): string;
29
+ }
30
+ /** textWidth guesses how wide a label is, for laying it out. */
31
+ export declare function textWidth(words: string, size: number): number;
32
+ export { emojiBox };
package/lib/pen.js ADDED
@@ -0,0 +1,60 @@
1
+ import { emojiBox, symbolId } from "./emoji.js";
2
+ import { font, ink } from "./style.js";
3
+ import { el, esc, n } from "./svg.js";
4
+ /**
5
+ * Pen collects the markup of one picture and the emoji it places. A kind draws
6
+ * with it at the picture's own scale, origin at the top left; a nested picture,
7
+ * such as an option of a choice, draws with a pen of its own, which the outer
8
+ * pen then places, scaled.
9
+ */
10
+ export class Pen {
11
+ codes;
12
+ parts = [];
13
+ constructor(codes = new Set()) {
14
+ this.codes = codes;
15
+ }
16
+ /** sub is a pen for a nested picture; its emoji count as this picture's. */
17
+ sub() {
18
+ return new Pen(this.codes);
19
+ }
20
+ add(markup) {
21
+ this.parts.push(markup);
22
+ }
23
+ el(tag, values, children) {
24
+ this.parts.push(el(tag, values, children));
25
+ }
26
+ /** emoji places an emoji of Noto Emoji, centred, size pixels square. */
27
+ emoji(code, cx, cy, size) {
28
+ this.codes.add(code);
29
+ this.parts.push(el("use", {
30
+ href: `#${symbolId(code)}`,
31
+ x: cx - size / 2,
32
+ y: cy - size / 2,
33
+ width: size,
34
+ height: size,
35
+ }));
36
+ }
37
+ /** text writes a label, centred on x unless told otherwise, its middle on y. */
38
+ text(x, y, words, options = {}) {
39
+ const size = options.size ?? 18;
40
+ this.parts.push(`<text x="${n(x)}" y="${n(y + size * 0.36)}" font-size="${n(size)}" font-weight="${options.weight ?? 700}" text-anchor="${options.anchor ?? "middle"}" fill="${options.fill ?? ink}" font-family="${esc(font)}">${esc(words)}</text>`);
41
+ }
42
+ /** place puts a nested pen's picture at x, y, scaled. */
43
+ place(inner, x, y, scale = 1) {
44
+ const transform = scale === 1
45
+ ? `translate(${n(x)} ${n(y)})`
46
+ : `translate(${n(x)} ${n(y)}) scale(${scale.toFixed(4)})`;
47
+ this.parts.push(`<g transform="${transform}">${inner.markup()}</g>`);
48
+ }
49
+ markup() {
50
+ return this.parts.join("");
51
+ }
52
+ }
53
+ /** textWidth guesses how wide a label is, for laying it out. */
54
+ export function textWidth(words, size) {
55
+ let width = 0;
56
+ for (const c of words)
57
+ width += /[0-9]/.test(c) ? 0.6 : /[A-Z?]/.test(c) ? 0.68 : /[ .,:]/.test(c) ? 0.3 : 0.56;
58
+ return width * size;
59
+ }
60
+ export { emojiBox };
package/lib/read.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export type Problems = string[];
2
+ export type Obj = Record<string, unknown>;
3
+ export declare function isObj(value: unknown): value is Obj;
4
+ /** only records a problem for every member that the kind does not know. */
5
+ export declare function only(value: Obj, members: readonly string[], path: string, problems: Problems): void;
6
+ export declare function list(value: unknown, path: string, problems: Problems, least?: number, most?: number): unknown[];
7
+ export declare function int(value: unknown, path: string, problems: Problems, least: number, most: number): number;
8
+ export declare function text(value: unknown, path: string, problems: Problems, most?: number): string;
9
+ export declare function oneOf<T extends string>(value: unknown, options: readonly T[], path: string, problems: Problems): T;
package/lib/read.js ADDED
@@ -0,0 +1,49 @@
1
+ // Reading a scene that came as JSON: each helper checks one member and, when
2
+ // it is wrong, records a problem with the member's path, so that a refused
3
+ // scene says exactly what to fix. The generator gets these words back.
4
+ export function isObj(value) {
5
+ return typeof value === "object" && value !== null && !Array.isArray(value);
6
+ }
7
+ /** only records a problem for every member that the kind does not know. */
8
+ export function only(value, members, path, problems) {
9
+ for (const key of Object.keys(value)) {
10
+ if (!members.includes(key))
11
+ problems.push(`${path}.${key}: not a member of this kind`);
12
+ }
13
+ }
14
+ export function list(value, path, problems, least = 1, most = Number.POSITIVE_INFINITY) {
15
+ if (!Array.isArray(value)) {
16
+ problems.push(`${path}: must be a list`);
17
+ return [];
18
+ }
19
+ if (value.length < least)
20
+ problems.push(`${path}: must hold at least ${least}`);
21
+ if (value.length > most)
22
+ problems.push(`${path}: must hold at most ${most}`);
23
+ return value;
24
+ }
25
+ export function int(value, path, problems, least, most) {
26
+ if (typeof value !== "number" || !Number.isInteger(value)) {
27
+ problems.push(`${path}: must be a whole number`);
28
+ return least;
29
+ }
30
+ if (value < least || value > most)
31
+ problems.push(`${path}: must be from ${least} to ${most}`);
32
+ return value;
33
+ }
34
+ export function text(value, path, problems, most = 12) {
35
+ if (typeof value !== "string" || value === "") {
36
+ problems.push(`${path}: must be a text`);
37
+ return "";
38
+ }
39
+ if ([...value].length > most)
40
+ problems.push(`${path}: at most ${most} characters`);
41
+ return value;
42
+ }
43
+ export function oneOf(value, options, path, problems) {
44
+ if (typeof value !== "string" || !options.includes(value)) {
45
+ problems.push(`${path}: must be one of ${options.join(", ")}`);
46
+ return options[0];
47
+ }
48
+ return value;
49
+ }
@@ -0,0 +1,24 @@
1
+ import { type EmojiSource } from "./emoji.ts";
2
+ import type { Drawn } from "./kind.ts";
3
+ import { Pen } from "./pen.ts";
4
+ export type Rendered = {
5
+ svg: string;
6
+ width: number;
7
+ height: number;
8
+ alt: string;
9
+ };
10
+ export declare class SceneError extends Error {
11
+ readonly problems: readonly string[];
12
+ constructor(problems: readonly string[]);
13
+ }
14
+ /** check lists what is wrong with a scene; an empty list means it can be drawn. */
15
+ export declare function check(scene: unknown): string[];
16
+ /** drawScene draws a checked scene with a pen; a kind that nests scenes uses it. */
17
+ export declare function drawScene(scene: unknown, pen: Pen): Drawn;
18
+ /** describe says a checked scene in words, for a screen reader and the blind solver. */
19
+ export declare function describe(scene: unknown): string;
20
+ /**
21
+ * render checks a scene and draws it: one SVG document, every emoji in it
22
+ * once as a symbol, a white ground, and its words as the title.
23
+ */
24
+ export declare function render(scene: unknown, emoji: EmojiSource): Rendered;
package/lib/render.js ADDED
@@ -0,0 +1,68 @@
1
+ import { symbolOf } from "./emoji.js";
2
+ import { kinds } from "./kinds/index.js";
3
+ import { Pen } from "./pen.js";
4
+ import { isObj } from "./read.js";
5
+ import { ground } from "./style.js";
6
+ import { byCodeUnit, esc, n } from "./svg.js";
7
+ export class SceneError extends Error {
8
+ problems;
9
+ constructor(problems) {
10
+ super(`the scene is refused:\n${problems.join("\n")}`);
11
+ this.problems = problems;
12
+ }
13
+ }
14
+ function kindOf(scene, problems) {
15
+ if (!isObj(scene)) {
16
+ problems.push("scene: must be an object with a kind");
17
+ return undefined;
18
+ }
19
+ const kind = kinds.find((k) => k.kind === scene.kind);
20
+ if (!kind)
21
+ problems.push(`scene.kind: must be one of ${kinds.map((k) => k.kind).join(", ")}`);
22
+ return kind;
23
+ }
24
+ /** check lists what is wrong with a scene; an empty list means it can be drawn. */
25
+ export function check(scene) {
26
+ const problems = [];
27
+ const kind = kindOf(scene, problems);
28
+ if (kind && isObj(scene))
29
+ kind.check(scene, problems);
30
+ return problems;
31
+ }
32
+ /** drawScene draws a checked scene with a pen; a kind that nests scenes uses it. */
33
+ export function drawScene(scene, pen) {
34
+ const kind = kindOf(scene, []);
35
+ if (!kind || !isObj(scene))
36
+ throw new SceneError(["scene: unknown kind"]);
37
+ return kind.draw(scene, pen);
38
+ }
39
+ /** describe says a checked scene in words, for a screen reader and the blind solver. */
40
+ export function describe(scene) {
41
+ const kind = kindOf(scene, []);
42
+ if (!kind || !isObj(scene))
43
+ throw new SceneError(["scene: unknown kind"]);
44
+ return kind.describe(scene);
45
+ }
46
+ /**
47
+ * render checks a scene and draws it: one SVG document, every emoji in it
48
+ * once as a symbol, a white ground, and its words as the title.
49
+ */
50
+ export function render(scene, emoji) {
51
+ const problems = check(scene);
52
+ if (problems.length > 0)
53
+ throw new SceneError(problems);
54
+ const pen = new Pen();
55
+ const { width, height } = drawScene(scene, pen);
56
+ const alt = describe(scene);
57
+ const defs = [...pen.codes]
58
+ .sort(byCodeUnit)
59
+ .map((code) => symbolOf(code, emoji(code)))
60
+ .join("");
61
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 ${n(width)} ${n(height)}" width="${n(width)}" height="${n(height)}" role="img">` +
62
+ `<title>${esc(alt)}</title>` +
63
+ (defs ? `<defs>${defs}</defs>` : "") +
64
+ `<rect width="100%" height="100%" fill="${ground}"/>` +
65
+ pen.markup() +
66
+ "</svg>";
67
+ return { svg, width, height, alt };
68
+ }
@@ -0,0 +1,46 @@
1
+ import type { BalanceScene } from "../kinds/balance.ts";
2
+ import { type Thing } from "../things.ts";
3
+ /**
4
+ * What a picture of balances proves. The solver reads only the scene — the
5
+ * things on the pans and the side each balance tips to — and tries every
6
+ * assignment of whole weights to the things (or every order of the weights
7
+ * the statement gives). An answer is proved when it holds in every
8
+ * assignment that agrees with the picture; otherwise the picture does not
9
+ * decide, which is itself a fact a question may ask about ("it could be A or B").
10
+ * Without given weights the search runs over whole weights up to 12 (fewer
11
+ * for more things): enough for the comparisons of grade 1–2, and a proof
12
+ * only within that range, so it stands beside the blind solver, not alone.
13
+ */
14
+ export type Question = {
15
+ ask: "heaviest";
16
+ } | {
17
+ ask: "lightest";
18
+ } | {
19
+ ask: "weighs";
20
+ weight: number;
21
+ } | {
22
+ ask: "how-many";
23
+ of: Thing;
24
+ balance: Thing[];
25
+ } | {
26
+ ask: "heavier";
27
+ a: Thing;
28
+ b: Thing;
29
+ };
30
+ export type Answer = {
31
+ proved: true;
32
+ answer: Thing | number | "yes" | "no";
33
+ } | {
34
+ proved: false;
35
+ candidates: (Thing | number | string)[];
36
+ };
37
+ export type Givens = {
38
+ /** The weights the statement gives, one to each thing, in some order. */
39
+ weights?: number[];
40
+ };
41
+ /** things lists the different things the scene's balances hold, in order. */
42
+ export declare function thingsOf(scene: BalanceScene): Thing[];
43
+ /** worlds are the weight assignments that agree with every balance. */
44
+ export declare function worlds(scene: BalanceScene, givens?: Givens): Map<Thing, number>[];
45
+ /** solve answers a question from the picture alone, and the givens of the statement. */
46
+ export declare function solve(scene: BalanceScene, question: Question, givens?: Givens): Answer;
@@ -0,0 +1,118 @@
1
+ import { tiltOf } from "../kinds/balance.js";
2
+ import { sameThing, wordOf } from "../things.js";
3
+ const most = 7;
4
+ /** things lists the different things the scene's balances hold, in order. */
5
+ export function thingsOf(scene) {
6
+ const out = [];
7
+ for (const scale of scene.scales)
8
+ for (const t of [...scale.left, ...scale.right])
9
+ if (!out.some((o) => sameThing(o, t)))
10
+ out.push(t);
11
+ return out;
12
+ }
13
+ function* assignments(k, givens) {
14
+ if (givens.weights) {
15
+ const pool = givens.weights;
16
+ const used = pool.map(() => false);
17
+ const pick = [];
18
+ function* permute() {
19
+ if (pick.length === k) {
20
+ yield [...pick];
21
+ return;
22
+ }
23
+ for (let i = 0; i < pool.length; i++) {
24
+ if (used[i])
25
+ continue;
26
+ used[i] = true;
27
+ pick.push(pool[i]);
28
+ yield* permute();
29
+ pick.pop();
30
+ used[i] = false;
31
+ }
32
+ }
33
+ yield* permute();
34
+ return;
35
+ }
36
+ const top = k <= 4 ? 12 : k <= 5 ? 9 : 7;
37
+ const w = new Array(k).fill(1);
38
+ while (true) {
39
+ yield [...w];
40
+ let i = 0;
41
+ while (i < k && w[i] === top)
42
+ w[i++] = 1;
43
+ if (i === k)
44
+ return;
45
+ w[i] = w[i] + 1;
46
+ }
47
+ }
48
+ /** worlds are the weight assignments that agree with every balance. */
49
+ export function worlds(scene, givens = {}) {
50
+ const things = thingsOf(scene);
51
+ if (things.length > most)
52
+ throw new Error(`at most ${most} different things on the balances`);
53
+ const out = [];
54
+ for (const w of assignments(things.length, givens)) {
55
+ const weight = (t) => w[things.findIndex((o) => sameThing(o, t))];
56
+ if (scene.scales.every((s) => tiltOf(s, weight) === s.tilt)) {
57
+ out.push(new Map(things.map((t, i) => [t, w[i]])));
58
+ }
59
+ }
60
+ return out;
61
+ }
62
+ function weightIn(world, t) {
63
+ for (const [k, v] of world)
64
+ if (sameThing(k, t))
65
+ return v;
66
+ throw new Error(`${wordOf(t)} is on no balance`);
67
+ }
68
+ function same(values, key) {
69
+ const seen = new Map();
70
+ for (const v of values)
71
+ seen.set(key(v), v);
72
+ if (seen.size === 1)
73
+ return { proved: true, answer: [...seen.values()][0] };
74
+ return { proved: false, candidates: [...seen.values()] };
75
+ }
76
+ /** solve answers a question from the picture alone, and the givens of the statement. */
77
+ export function solve(scene, question, givens = {}) {
78
+ const all = worlds(scene, givens);
79
+ if (all.length === 0)
80
+ return { proved: false, candidates: ["the balances contradict each other"] };
81
+ const things = thingsOf(scene);
82
+ const key = (t) => JSON.stringify(t);
83
+ switch (question.ask) {
84
+ case "heaviest":
85
+ case "lightest": {
86
+ const pick = (world) => {
87
+ const ws = things.map((t) => weightIn(world, t));
88
+ const best = question.ask === "heaviest" ? Math.max(...ws) : Math.min(...ws);
89
+ const at = things.filter((_, i) => ws[i] === best);
90
+ return at.length === 1 ? at[0] : "a tie";
91
+ };
92
+ const picks = all.map(pick);
93
+ if (picks.includes("a tie"))
94
+ return { proved: false, candidates: ["a tie is possible"] };
95
+ return same(picks, key);
96
+ }
97
+ case "weighs": {
98
+ const picks = all.map((world) => things.find((t) => weightIn(world, t) === question.weight));
99
+ if (picks.some((p) => p === undefined))
100
+ return { proved: false, candidates: ["nothing weighs that"] };
101
+ return same(picks, key);
102
+ }
103
+ case "how-many": {
104
+ const ratios = all.map((world) => {
105
+ const total = question.balance.reduce((sum, t) => sum + weightIn(world, t), 0);
106
+ return total / weightIn(world, question.of);
107
+ });
108
+ const answer = same(ratios, String);
109
+ if (answer.proved && !Number.isInteger(answer.answer))
110
+ return { proved: false, candidates: [answer.answer] };
111
+ return answer;
112
+ }
113
+ case "heavier": {
114
+ const picks = all.map((world) => weightIn(world, question.a) > weightIn(world, question.b) ? "yes" : "no");
115
+ return same(picks, String);
116
+ }
117
+ }
118
+ }