@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,72 @@
1
+ import type { Pen } from "../pen.ts";
2
+ import { type Problems } from "../read.ts";
3
+ import { type ColorName } from "../style.ts";
4
+ import { type Thing } from "../things.ts";
5
+ /** A plain entry: a number, or a short text such as a letter. */
6
+ export type Plain = number | string;
7
+ /** An entry with a fill: one of a value, a thing and ask, or none for an empty place. */
8
+ export type EntryObj = {
9
+ value?: Plain;
10
+ thing?: Thing;
11
+ ask?: true;
12
+ fill?: ColorName;
13
+ };
14
+ /**
15
+ * An entry is what stands in a place of a figure: a number, a short text,
16
+ * null (or "") for an empty place, {"ask": true} for the place the question
17
+ * asks about, or an object with a fill of the palette and at most one of
18
+ * value, thing and ask.
19
+ */
20
+ export type Entry = Plain | null | EntryObj;
21
+ export type EntryLimits = {
22
+ biggest: number;
23
+ chars: number;
24
+ };
25
+ /** Content is an entry as the picture draws it. */
26
+ export type Content = {
27
+ text?: string;
28
+ thing?: Thing;
29
+ ask?: true;
30
+ fill?: ColorName;
31
+ };
32
+ /** checkEntry records what is wrong with an entry at a path. */
33
+ export declare function checkEntry(value: unknown, path: string, problems: Problems, limits: EntryLimits): void;
34
+ /** contentOf is what a checked entry draws. */
35
+ export declare function contentOf(entry: Entry): Content;
36
+ /** numberOf is the number an entry shows, if it shows one. */
37
+ export declare function numberOf(entry: Entry): number | undefined;
38
+ /** sayText says a text: a number as it is, anything else in quotes. */
39
+ export declare function sayText(text: string): string;
40
+ /** sayEntry says an entry for the words of a picture: "5", "\"A\"", "?", "empty", "pear on yellow". */
41
+ export declare function sayEntry(entry: Entry): string;
42
+ /** fillOf is the fill of an entry's place, white unless it names one. */
43
+ export declare function fillOf(entry: Entry, base?: ColorName): ColorName;
44
+ /** textSize is the size of a label of so many characters in a place size pixels across. */
45
+ export declare function textSize(chars: number, size: number): number;
46
+ /**
47
+ * drawContent draws what stands in a place, centred at cx, cy, the place
48
+ * size pixels across; its outline is the caller's. In a dashed place, a
49
+ * question is a bare "?"; elsewhere it is the dashed circle with "?". A text
50
+ * takes the size options.text gives, or one that fits the place.
51
+ */
52
+ export declare function drawContent(pen: Pen, entry: Entry, cx: number, cy: number, size: number, options?: {
53
+ base?: ColorName;
54
+ dashed?: boolean;
55
+ text?: number;
56
+ }): void;
57
+ export declare const tagHeight = 32;
58
+ /** tagWidth is how wide the tag of a text is. */
59
+ export declare function tagWidth(text: string): number;
60
+ /** drawTag draws a number beside a line: a soft pill with the number, centred at cx, cy. */
61
+ export declare function drawTag(pen: Pen, cx: number, cy: number, text: string): void;
62
+ /** A card of the numbers to place is so many pixels across and high. */
63
+ export declare const cardWidth = 40;
64
+ export declare const cardHeight = 48;
65
+ /** drawCard draws a card with a number or a short text, centred at cx, cy. */
66
+ export declare function drawCard(pen: Pen, cx: number, cy: number, text: string): void;
67
+ /** ordinal writes 1st, 2nd, 3rd, 4th … */
68
+ export declare function ordinal(k: number): string;
69
+ /** sayList joins words with commas and a final "and". */
70
+ export declare function sayList(words: readonly string[]): string;
71
+ /** plural writes "1 circle", "3 circles". */
72
+ export declare function plural(k: number, word: string, words?: string): string;
@@ -0,0 +1,186 @@
1
+ import { textWidth } from "../pen.js";
2
+ import { int, isObj, only } from "../read.js";
3
+ import { colorNames, colors, ink, isColor, outline, soft, textOn, } from "../style.js";
4
+ import { checkThing, drawThing, wordOf } from "../things.js";
5
+ function expected(limits) {
6
+ return `a whole number from 0 to ${limits.biggest}, a text of at most ${limits.chars} characters, null for an empty place, {"ask": true}, or an object with a fill and one of value, thing and ask`;
7
+ }
8
+ function checkPlain(value, path, problems, limits) {
9
+ if (typeof value === "number") {
10
+ int(value, path, problems, 0, limits.biggest);
11
+ return;
12
+ }
13
+ if (typeof value !== "string") {
14
+ problems.push(`${path}: must be ${expected(limits)}`);
15
+ return;
16
+ }
17
+ if (value.trim() === "?") {
18
+ problems.push(`${path}: write {"ask": true} for the place the question asks`);
19
+ return;
20
+ }
21
+ if (value !== value.trim())
22
+ problems.push(`${path}: no spaces before or after the text`);
23
+ if ([...value].length > limits.chars)
24
+ problems.push(`${path}: at most ${limits.chars} characters`);
25
+ }
26
+ /** checkEntry records what is wrong with an entry at a path. */
27
+ export function checkEntry(value, path, problems, limits) {
28
+ if (value === null || value === "")
29
+ return;
30
+ if (typeof value === "number" || typeof value === "string") {
31
+ checkPlain(value, path, problems, limits);
32
+ return;
33
+ }
34
+ if (!isObj(value)) {
35
+ problems.push(`${path}: must be ${expected(limits)}`);
36
+ return;
37
+ }
38
+ only(value, ["value", "thing", "ask", "fill"], path, problems);
39
+ const contents = ["value", "thing", "ask"].filter((m) => value[m] !== undefined);
40
+ if (contents.length > 1)
41
+ problems.push(`${path}: holds one of value, thing and ask, not ${contents.join(" and ")}`);
42
+ if (value.value !== undefined && value.value !== null && value.value !== "")
43
+ checkPlain(value.value, `${path}.value`, problems, limits);
44
+ if (value.thing !== undefined) {
45
+ const t = value.thing;
46
+ if (typeof t === "string" || (isObj(t) && ("ball" in t || "shape" in t)))
47
+ checkThing(t, `${path}.thing`, problems);
48
+ else
49
+ problems.push(`${path}.thing: must be an object id, a ball or a shape`);
50
+ }
51
+ if (value.ask !== undefined && value.ask !== true)
52
+ problems.push(`${path}.ask: must be true`);
53
+ if (value.fill !== undefined && !isColor(value.fill))
54
+ problems.push(`${path}.fill: must be a colour: ${colorNames.join(", ")}`);
55
+ }
56
+ /** contentOf is what a checked entry draws. */
57
+ export function contentOf(entry) {
58
+ if (entry === null || entry === "")
59
+ return {};
60
+ if (typeof entry === "number")
61
+ return { text: String(entry) };
62
+ if (typeof entry === "string")
63
+ return { text: entry };
64
+ const out = {};
65
+ if (entry.value !== undefined && entry.value !== null && entry.value !== "")
66
+ out.text = String(entry.value);
67
+ if (entry.thing !== undefined)
68
+ out.thing = entry.thing;
69
+ if (entry.ask === true)
70
+ out.ask = true;
71
+ if (entry.fill !== undefined)
72
+ out.fill = entry.fill;
73
+ return out;
74
+ }
75
+ /** numberOf is the number an entry shows, if it shows one. */
76
+ export function numberOf(entry) {
77
+ const text = contentOf(entry).text;
78
+ return text !== undefined && /^\d+$/.test(text) ? Number(text) : undefined;
79
+ }
80
+ /** sayText says a text: a number as it is, anything else in quotes. */
81
+ export function sayText(text) {
82
+ return /^\d+$/.test(text) ? text : `"${text}"`;
83
+ }
84
+ /** sayEntry says an entry for the words of a picture: "5", "\"A\"", "?", "empty", "pear on yellow". */
85
+ export function sayEntry(entry) {
86
+ const c = contentOf(entry);
87
+ const what = c.ask
88
+ ? "?"
89
+ : c.text !== undefined
90
+ ? sayText(c.text)
91
+ : c.thing !== undefined
92
+ ? wordOf(c.thing)
93
+ : undefined;
94
+ if (c.fill)
95
+ return what ? `${what} on ${colors[c.fill].word}` : `empty ${colors[c.fill].word}`;
96
+ return what ?? "empty";
97
+ }
98
+ /** fillOf is the fill of an entry's place, white unless it names one. */
99
+ export function fillOf(entry, base = "white") {
100
+ return contentOf(entry).fill ?? base;
101
+ }
102
+ /** textSize is the size of a label of so many characters in a place size pixels across. */
103
+ export function textSize(chars, size) {
104
+ return size * (chars <= 2 ? 0.5 : chars === 3 ? 0.4 : 0.33);
105
+ }
106
+ /**
107
+ * drawContent draws what stands in a place, centred at cx, cy, the place
108
+ * size pixels across; its outline is the caller's. In a dashed place, a
109
+ * question is a bare "?"; elsewhere it is the dashed circle with "?". A text
110
+ * takes the size options.text gives, or one that fits the place.
111
+ */
112
+ export function drawContent(pen, entry, cx, cy, size, options = {}) {
113
+ const c = contentOf(entry);
114
+ const fill = c.fill ?? options.base ?? "white";
115
+ if (c.ask) {
116
+ if (options.dashed)
117
+ pen.text(cx, cy, "?", { size: size * 0.56, fill: textOn(fill) });
118
+ else
119
+ drawThing(pen, { ask: true }, cx, cy, size * 0.8);
120
+ return;
121
+ }
122
+ if (c.thing !== undefined) {
123
+ drawThing(pen, c.thing, cx, cy, size * 0.8);
124
+ return;
125
+ }
126
+ if (c.text !== undefined)
127
+ pen.text(cx, cy, c.text, {
128
+ size: options.text ?? textSize([...c.text].length, size),
129
+ fill: textOn(fill),
130
+ });
131
+ }
132
+ export const tagHeight = 32;
133
+ const tagText = 19;
134
+ /** tagWidth is how wide the tag of a text is. */
135
+ export function tagWidth(text) {
136
+ return Math.max(42, textWidth(text, tagText) + 22);
137
+ }
138
+ /** drawTag draws a number beside a line: a soft pill with the number, centred at cx, cy. */
139
+ export function drawTag(pen, cx, cy, text) {
140
+ const w = tagWidth(text);
141
+ pen.el("rect", {
142
+ class: "tag",
143
+ x: cx - w / 2,
144
+ y: cy - tagHeight / 2,
145
+ width: w,
146
+ height: tagHeight,
147
+ rx: tagHeight / 2,
148
+ fill: soft,
149
+ stroke: ink,
150
+ "stroke-width": 2,
151
+ });
152
+ pen.text(cx, cy, text, { size: tagText });
153
+ }
154
+ /** A card of the numbers to place is so many pixels across and high. */
155
+ export const cardWidth = 40;
156
+ export const cardHeight = 48;
157
+ /** drawCard draws a card with a number or a short text, centred at cx, cy. */
158
+ export function drawCard(pen, cx, cy, text) {
159
+ pen.el("rect", {
160
+ class: "card",
161
+ x: cx - cardWidth / 2,
162
+ y: cy - cardHeight / 2,
163
+ width: cardWidth,
164
+ height: cardHeight,
165
+ rx: 6,
166
+ fill: soft,
167
+ stroke: ink,
168
+ "stroke-width": outline,
169
+ });
170
+ pen.text(cx, cy, text, { size: textSize([...text].length, 46) });
171
+ }
172
+ /** ordinal writes 1st, 2nd, 3rd, 4th … */
173
+ export function ordinal(k) {
174
+ const tail = k % 100 >= 11 && k % 100 <= 13 ? "th" : (["th", "st", "nd", "rd"][k % 10] ?? "th");
175
+ return `${k}${tail}`;
176
+ }
177
+ /** sayList joins words with commas and a final "and". */
178
+ export function sayList(words) {
179
+ if (words.length <= 1)
180
+ return words.join("");
181
+ return `${words.slice(0, -1).join(", ")} and ${words[words.length - 1]}`;
182
+ }
183
+ /** plural writes "1 circle", "3 circles". */
184
+ export function plural(k, word, words = `${word}s`) {
185
+ return `${k} ${k === 1 ? word : words}`;
186
+ }
@@ -0,0 +1,66 @@
1
+ import type { Kind } from "../kind.ts";
2
+ import { type Entry } from "./magic-parts.ts";
3
+ /**
4
+ * Magic: a figure of numbers. Its places are slots — circles or boxes along
5
+ * the lines of a triangle, a square, a star or a wheel — or the cells of a
6
+ * cross or a grid; each holds an entry: a number, a short text, nothing, or
7
+ * the "?" of the question. The lines whose sums matter are the figure's own,
8
+ * and the picture draws them:
9
+ *
10
+ * - triangle: perSide (3–5) slots on each side, corners included, slots
11
+ * clockwise from the top corner; with centre (perSide odd) one more slot in
12
+ * the middle, on the three lines from each corner to the middle of the side
13
+ * across. Lines: the right, bottom and left sides, then the three lines
14
+ * from the top, bottom-right and bottom-left corners.
15
+ * - square: perSide (3–5) slots on each side, clockwise from the top-left
16
+ * corner. Lines: the top, right, bottom and left sides.
17
+ * - star: points 5 or 6, a slot at each point and at each crossing of its
18
+ * lines, clockwise round its outline from the top point, a point and a
19
+ * crossing in turn. Lines: each straight line, from each point clockwise.
20
+ * - wheel: a middle slot and lines (2–4) straight lines through it, perArm
21
+ * (1–2) slots on each side of the middle; the middle first, then each arm
22
+ * clockwise from the top, from the middle outwards. Lines: from each end
23
+ * clockwise from the top, through the middle.
24
+ * - cross: a row and a column of cells sharing the middle one, arm (1–3)
25
+ * cells on each side of it; cells row by row from the top. Lines: the row,
26
+ * the column.
27
+ * - grid: rows by columns (2–5) cells, row by row from the top. Lines: the
28
+ * rows, the columns, and with diagonals (a square grid) both diagonals,
29
+ * from the top-left corner and from the top-right one.
30
+ *
31
+ * sums, one for each line in that order, puts a number (or "?") in a tag at
32
+ * the line's end; null shows none. pool shows number cards below the figure.
33
+ * facts may give every slot's value: the check then proves that the shown
34
+ * numbers agree, that the lines add up to their tags — or, with no sums, all
35
+ * to one sum — and that the open slots take cards of the pool.
36
+ */
37
+ export declare const magicShapes: readonly ["triangle", "square", "star", "wheel", "cross", "grid"];
38
+ export type MagicShape = (typeof magicShapes)[number];
39
+ export declare const slotLooks: readonly ["circle", "box"];
40
+ export type SlotLook = (typeof slotLooks)[number];
41
+ export type Sum = number | null | {
42
+ ask: true;
43
+ };
44
+ export type MagicScene = {
45
+ kind: "magic";
46
+ shape: MagicShape;
47
+ perSide?: number;
48
+ centre?: boolean;
49
+ points?: number;
50
+ lines?: number;
51
+ perArm?: number;
52
+ arm?: number;
53
+ rows?: number;
54
+ columns?: number;
55
+ diagonals?: boolean;
56
+ slot?: SlotLook;
57
+ slots: Entry[];
58
+ sums?: Sum[];
59
+ pool?: (number | string)[];
60
+ facts?: {
61
+ values: number[];
62
+ };
63
+ };
64
+ /** magicLines lists the lines of a checked scene, each as the indices of its slots. */
65
+ export declare function magicLines(scene: MagicScene): number[][];
66
+ export declare const magic: Kind;