@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.
- package/LICENSE +201 -0
- package/NOTICE +7 -0
- package/README.md +27 -2
- package/lib/emoji.d.ts +16 -0
- package/lib/emoji.js +64 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +10 -0
- package/lib/kind.d.ts +22 -0
- package/lib/kind.js +2 -0
- package/lib/kinds/balance.d.ts +34 -0
- package/lib/kinds/balance.js +217 -0
- package/lib/kinds/branches.d.ts +33 -0
- package/lib/kinds/branches.js +473 -0
- package/lib/kinds/calendar.d.ts +55 -0
- package/lib/kinds/calendar.js +532 -0
- package/lib/kinds/chart.d.ts +48 -0
- package/lib/kinds/chart.js +645 -0
- package/lib/kinds/choices.d.ts +14 -0
- package/lib/kinds/choices.js +53 -0
- package/lib/kinds/clock.d.ts +36 -0
- package/lib/kinds/clock.js +219 -0
- package/lib/kinds/coins.d.ts +37 -0
- package/lib/kinds/coins.js +550 -0
- package/lib/kinds/column.d.ts +37 -0
- package/lib/kinds/column.js +410 -0
- package/lib/kinds/commands-parts.d.ts +12 -0
- package/lib/kinds/commands-parts.js +24 -0
- package/lib/kinds/commands.d.ts +66 -0
- package/lib/kinds/commands.js +789 -0
- package/lib/kinds/containers.d.ts +52 -0
- package/lib/kinds/containers.js +758 -0
- package/lib/kinds/countfig.d.ts +41 -0
- package/lib/kinds/countfig.js +372 -0
- package/lib/kinds/cubes.d.ts +70 -0
- package/lib/kinds/cubes.js +371 -0
- package/lib/kinds/die.d.ts +32 -0
- package/lib/kinds/die.js +301 -0
- package/lib/kinds/domino.d.ts +37 -0
- package/lib/kinds/domino.js +450 -0
- package/lib/kinds/dotgrid.d.ts +38 -0
- package/lib/kinds/dotgrid.js +411 -0
- package/lib/kinds/figure.d.ts +62 -0
- package/lib/kinds/figure.js +687 -0
- package/lib/kinds/fold-parts.d.ts +73 -0
- package/lib/kinds/fold-parts.js +315 -0
- package/lib/kinds/fold.d.ts +52 -0
- package/lib/kinds/fold.js +650 -0
- package/lib/kinds/fraction.d.ts +50 -0
- package/lib/kinds/fraction.js +568 -0
- package/lib/kinds/gauges.d.ts +53 -0
- package/lib/kinds/gauges.js +880 -0
- package/lib/kinds/gears.d.ts +40 -0
- package/lib/kinds/gears.js +787 -0
- package/lib/kinds/grid.d.ts +45 -0
- package/lib/kinds/grid.js +591 -0
- package/lib/kinds/groups.d.ts +23 -0
- package/lib/kinds/groups.js +335 -0
- package/lib/kinds/index.d.ts +3 -0
- package/lib/kinds/index.js +87 -0
- package/lib/kinds/lanes.d.ts +36 -0
- package/lib/kinds/lanes.js +220 -0
- package/lib/kinds/layers.d.ts +31 -0
- package/lib/kinds/layers.js +419 -0
- package/lib/kinds/line.d.ts +38 -0
- package/lib/kinds/line.js +739 -0
- package/lib/kinds/magic-parts.d.ts +72 -0
- package/lib/kinds/magic-parts.js +186 -0
- package/lib/kinds/magic.d.ts +66 -0
- package/lib/kinds/magic.js +664 -0
- package/lib/kinds/map.d.ts +32 -0
- package/lib/kinds/map.js +370 -0
- package/lib/kinds/matchsticks.d.ts +26 -0
- package/lib/kinds/matchsticks.js +434 -0
- package/lib/kinds/necklace.d.ts +27 -0
- package/lib/kinds/necklace.js +342 -0
- package/lib/kinds/net.d.ts +47 -0
- package/lib/kinds/net.js +241 -0
- package/lib/kinds/overlay.d.ts +50 -0
- package/lib/kinds/overlay.js +519 -0
- package/lib/kinds/pieces.d.ts +41 -0
- package/lib/kinds/pieces.js +420 -0
- package/lib/kinds/pipes.d.ts +46 -0
- package/lib/kinds/pipes.js +657 -0
- package/lib/kinds/plane-parts.d.ts +105 -0
- package/lib/kinds/plane-parts.js +372 -0
- package/lib/kinds/plane.d.ts +35 -0
- package/lib/kinds/plane.js +755 -0
- package/lib/kinds/pyramid.d.ts +26 -0
- package/lib/kinds/pyramid.js +228 -0
- package/lib/kinds/ropes.d.ts +30 -0
- package/lib/kinds/ropes.js +571 -0
- package/lib/kinds/shapes.d.ts +59 -0
- package/lib/kinds/shapes.js +490 -0
- package/lib/kinds/solids.d.ts +78 -0
- package/lib/kinds/solids.js +1058 -0
- package/lib/kinds/tangram.d.ts +52 -0
- package/lib/kinds/tangram.js +713 -0
- package/lib/kinds/target.d.ts +39 -0
- package/lib/kinds/target.js +761 -0
- package/lib/kinds/tiles.d.ts +55 -0
- package/lib/kinds/tiles.js +606 -0
- package/lib/kinds/venn.d.ts +38 -0
- package/lib/kinds/venn.js +710 -0
- package/lib/noto.d.ts +2 -0
- package/lib/noto.js +212 -0
- package/lib/objects.d.ts +7 -0
- package/lib/objects.js +226 -0
- package/lib/pen.d.ts +32 -0
- package/lib/pen.js +60 -0
- package/lib/read.d.ts +9 -0
- package/lib/read.js +49 -0
- package/lib/render.d.ts +24 -0
- package/lib/render.js +68 -0
- package/lib/solve/balance.d.ts +46 -0
- package/lib/solve/balance.js +118 -0
- package/lib/style.d.ts +70 -0
- package/lib/style.js +39 -0
- package/lib/svg.d.ts +22 -0
- package/lib/svg.js +48 -0
- package/lib/things.d.ts +40 -0
- package/lib/things.js +269 -0
- package/package.json +28 -4
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import { margin } from "../kind.js";
|
|
2
|
+
import { int, isObj, list, oneOf, only, text } from "../read.js";
|
|
3
|
+
import { colorNames, colors, ink, isColor, outline, soft, textOn, } from "../style.js";
|
|
4
|
+
import { n } from "../svg.js";
|
|
5
|
+
import { checkThing, drawThing, wordOf } from "../things.js";
|
|
6
|
+
/**
|
|
7
|
+
* A calendar: a month page, its dates in rows of weeks under the names of
|
|
8
|
+
* the days, the 1st under the weekday the scene names; or a strip of days
|
|
9
|
+
* in a row, one after another, past the end of a month into the next; or
|
|
10
|
+
* torn-off sheets, each with a month, a date and a weekday. A date may be
|
|
11
|
+
* coloured or carry a thing, a "?" may stand in place of a date, and an ink
|
|
12
|
+
* blot may cover one. Dates are named by their number, weekdays by their
|
|
13
|
+
* first three letters: "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun".
|
|
14
|
+
*/
|
|
15
|
+
export const weekdays = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
|
|
16
|
+
export const calendarStyles = ["month", "strip", "sheets"];
|
|
17
|
+
export const sheetAsks = ["day", "weekday", "month"];
|
|
18
|
+
const dayWords = {
|
|
19
|
+
Mon: "Monday",
|
|
20
|
+
Tue: "Tuesday",
|
|
21
|
+
Wed: "Wednesday",
|
|
22
|
+
Thu: "Thursday",
|
|
23
|
+
Fri: "Friday",
|
|
24
|
+
Sat: "Saturday",
|
|
25
|
+
Sun: "Sunday",
|
|
26
|
+
};
|
|
27
|
+
/** The members each style takes besides kind and style. */
|
|
28
|
+
const styleMembers = {
|
|
29
|
+
month: ["month", "first", "days", "weekStart", "marks", "ask", "blots", "color"],
|
|
30
|
+
strip: ["month", "first", "days", "start", "count", "marks", "ask", "blots", "color"],
|
|
31
|
+
sheets: ["sheets", "color"],
|
|
32
|
+
};
|
|
33
|
+
const monthMost = 12;
|
|
34
|
+
const stripMost = 14;
|
|
35
|
+
const sheetsMost = 5;
|
|
36
|
+
const askMost = 10;
|
|
37
|
+
const cellW = 64;
|
|
38
|
+
const cellH = 52;
|
|
39
|
+
const dateSize = 21;
|
|
40
|
+
function styleOf(c) {
|
|
41
|
+
return c.style ?? "month";
|
|
42
|
+
}
|
|
43
|
+
/** ordinal writes a date as words read it: 1st, 2nd, 3rd, 4th, 11th, 22nd. */
|
|
44
|
+
export function ordinal(d) {
|
|
45
|
+
const teen = d % 100 >= 11 && d % 100 <= 13;
|
|
46
|
+
const end = teen ? "th" : d % 10 === 1 ? "st" : d % 10 === 2 ? "nd" : d % 10 === 3 ? "rd" : "th";
|
|
47
|
+
return `${d}${end}`;
|
|
48
|
+
}
|
|
49
|
+
/** columnsOf is the order of the weekdays across a month page. */
|
|
50
|
+
export function columnsOf(c) {
|
|
51
|
+
return c.weekStart === "Sun" ? ["Sun", ...weekdays.slice(0, 6)] : [...weekdays];
|
|
52
|
+
}
|
|
53
|
+
/** cellOf is the row and the column, from 0, of a date on a month page. */
|
|
54
|
+
export function cellOf(c, day) {
|
|
55
|
+
const offset = columnsOf(c).indexOf(c.first);
|
|
56
|
+
const i = offset + day - 1;
|
|
57
|
+
return { row: Math.floor(i / 7), col: i % 7 };
|
|
58
|
+
}
|
|
59
|
+
function rowsOf(c) {
|
|
60
|
+
return cellOf(c, c.days).row + 1;
|
|
61
|
+
}
|
|
62
|
+
/** datesOf lists the dates a month page or a strip shows, in order. */
|
|
63
|
+
export function datesOf(c) {
|
|
64
|
+
if (styleOf(c) === "strip") {
|
|
65
|
+
const days = c.days ?? 31;
|
|
66
|
+
return Array.from({ length: c.count }, (_, i) => ((c.start - 1 + i) % days) + 1);
|
|
67
|
+
}
|
|
68
|
+
return Array.from({ length: c.days }, (_, i) => i + 1);
|
|
69
|
+
}
|
|
70
|
+
/** weekdayOf is the weekday of the i-th cell of a strip, from 0. */
|
|
71
|
+
function weekdayOf(first, i) {
|
|
72
|
+
return weekdays[(weekdays.indexOf(first) + i) % 7];
|
|
73
|
+
}
|
|
74
|
+
// --- checking ---------------------------------------------------------------
|
|
75
|
+
function checkDays(value, path, shown, most, problems) {
|
|
76
|
+
const out = [];
|
|
77
|
+
list(value, path, problems, 1, most).forEach((d, i) => {
|
|
78
|
+
const before = problems.length;
|
|
79
|
+
const day = int(d, `${path}[${i}]`, problems, 1, 31);
|
|
80
|
+
if (problems.length > before)
|
|
81
|
+
return;
|
|
82
|
+
if (!shown.includes(day))
|
|
83
|
+
problems.push(`${path}[${i}]: the calendar shows no date ${day}`);
|
|
84
|
+
else if (out.includes(day))
|
|
85
|
+
problems.push(`${path}[${i}]: names the ${ordinal(day)} twice`);
|
|
86
|
+
out.push(day);
|
|
87
|
+
});
|
|
88
|
+
return out;
|
|
89
|
+
}
|
|
90
|
+
function checkPage(scene, style, problems) {
|
|
91
|
+
const before = problems.length;
|
|
92
|
+
if (scene.month !== undefined)
|
|
93
|
+
text(scene.month, "month", problems, monthMost);
|
|
94
|
+
if (style === "month") {
|
|
95
|
+
if (scene.first === undefined)
|
|
96
|
+
problems.push(`first: give the weekday of the 1st: ${weekdays.join(", ")}`);
|
|
97
|
+
else
|
|
98
|
+
oneOf(scene.first, weekdays, "first", problems);
|
|
99
|
+
int(scene.days, "days", problems, 28, 31);
|
|
100
|
+
if (scene.weekStart !== undefined)
|
|
101
|
+
oneOf(scene.weekStart, ["Mon", "Sun"], "weekStart", problems);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
if (scene.first !== undefined)
|
|
105
|
+
oneOf(scene.first, weekdays, "first", problems);
|
|
106
|
+
const days = scene.days === undefined ? 31 : int(scene.days, "days", problems, 28, 31);
|
|
107
|
+
const start = int(scene.start, "start", problems, 1, 31);
|
|
108
|
+
int(scene.count, "count", problems, 2, stripMost);
|
|
109
|
+
if (start > days)
|
|
110
|
+
problems.push(`start: a month of ${days} days has no date ${start}`);
|
|
111
|
+
}
|
|
112
|
+
if (problems.length > before)
|
|
113
|
+
return;
|
|
114
|
+
const shown = datesOf(scene);
|
|
115
|
+
const asked = scene.ask === undefined ? [] : checkDays(scene.ask, "ask", shown, askMost, problems);
|
|
116
|
+
const blots = scene.blots === undefined ? [] : checkDays(scene.blots, "blots", shown, 31, problems);
|
|
117
|
+
blots.forEach((d, i) => {
|
|
118
|
+
if (asked.includes(d))
|
|
119
|
+
problems.push(`blots[${i}]: the ${ordinal(d)} is asked; a blot covers a date not asked`);
|
|
120
|
+
});
|
|
121
|
+
if (blots.length > 0 && blots.length === shown.length)
|
|
122
|
+
problems.push("blots: at least one date must show");
|
|
123
|
+
if (scene.marks === undefined)
|
|
124
|
+
return;
|
|
125
|
+
const marked = [];
|
|
126
|
+
list(scene.marks, "marks", problems, 1, 31).forEach((m, i) => {
|
|
127
|
+
const path = `marks[${i}]`;
|
|
128
|
+
if (!isObj(m)) {
|
|
129
|
+
problems.push(`${path}: must be a mark: a day and a colour, a thing or both`);
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
only(m, ["day", "color", "thing"], path, problems);
|
|
133
|
+
const b = problems.length;
|
|
134
|
+
const day = int(m.day, `${path}.day`, problems, 1, 31);
|
|
135
|
+
if (problems.length === b) {
|
|
136
|
+
if (!shown.includes(day))
|
|
137
|
+
problems.push(`${path}.day: the calendar shows no date ${day}`);
|
|
138
|
+
else if (marked.includes(day))
|
|
139
|
+
problems.push(`${path}.day: the ${ordinal(day)} is marked twice`);
|
|
140
|
+
else if (blots.includes(day))
|
|
141
|
+
problems.push(`${path}.day: the ${ordinal(day)} is under a blot; mark a date that shows`);
|
|
142
|
+
marked.push(day);
|
|
143
|
+
}
|
|
144
|
+
if (m.color === undefined && m.thing === undefined)
|
|
145
|
+
problems.push(`${path}: give a colour, a thing or both`);
|
|
146
|
+
if (m.color !== undefined && !isColor(m.color))
|
|
147
|
+
problems.push(`${path}.color: must be a colour: ${colorNames.join(", ")}`);
|
|
148
|
+
if (m.thing !== undefined) {
|
|
149
|
+
checkThing(m.thing, `${path}.thing`, problems);
|
|
150
|
+
if (isObj(m.thing) && (m.thing.ask === true || m.thing.empty === true))
|
|
151
|
+
problems.push(`${path}.thing: must be an object or a drawn thing; a "?" goes in ask`);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
function checkSheets(scene, problems) {
|
|
156
|
+
list(scene.sheets, "sheets", problems, 1, sheetsMost).forEach((s, i) => {
|
|
157
|
+
const path = `sheets[${i}]`;
|
|
158
|
+
if (!isObj(s)) {
|
|
159
|
+
problems.push(`${path}: must be a sheet: a month, a day, a weekday`);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
only(s, ["month", "day", "weekday", "ask"], path, problems);
|
|
163
|
+
const asked = s.ask === undefined ? undefined : oneOf(s.ask, sheetAsks, `${path}.ask`, problems);
|
|
164
|
+
if (s.month !== undefined)
|
|
165
|
+
text(s.month, `${path}.month`, problems, 9);
|
|
166
|
+
if (s.day !== undefined)
|
|
167
|
+
int(s.day, `${path}.day`, problems, 1, 31);
|
|
168
|
+
if (s.weekday !== undefined)
|
|
169
|
+
oneOf(s.weekday, weekdays, `${path}.weekday`, problems);
|
|
170
|
+
if (asked !== undefined && s[asked] !== undefined)
|
|
171
|
+
problems.push(`${path}.${asked}: the sheet asks its ${asked}, so it shows none; leave ${asked} out`);
|
|
172
|
+
if (s.day === undefined && asked !== "day")
|
|
173
|
+
problems.push(`${path}.day: give the date, or "ask": "day"`);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
// --- drawing ----------------------------------------------------------------
|
|
177
|
+
/** blot draws an ink blot, its shape fixed by the date it covers. */
|
|
178
|
+
function blot(pen, cx, cy, r, seed) {
|
|
179
|
+
const k = 11;
|
|
180
|
+
const pts = [];
|
|
181
|
+
for (let j = 0; j < k; j++) {
|
|
182
|
+
const a = (j * 2 * Math.PI) / k;
|
|
183
|
+
const rr = r * (1 + 0.16 * Math.sin(3 * a + seed) + 0.09 * Math.cos(5 * a + 2 * seed));
|
|
184
|
+
pts.push({ x: cx + rr * Math.cos(a), y: cy + rr * 0.86 * Math.sin(a) });
|
|
185
|
+
}
|
|
186
|
+
const mid = (a, b) => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 });
|
|
187
|
+
const start = mid(pts[k - 1], pts[0]);
|
|
188
|
+
let d = `M${n(start.x)} ${n(start.y)}`;
|
|
189
|
+
for (let j = 0; j < k; j++) {
|
|
190
|
+
const m = mid(pts[j], pts[(j + 1) % k]);
|
|
191
|
+
d += `Q${n(pts[j].x)} ${n(pts[j].y)} ${n(m.x)} ${n(m.y)}`;
|
|
192
|
+
}
|
|
193
|
+
pen.el("path", {
|
|
194
|
+
d: `${d}Z`,
|
|
195
|
+
fill: colors.black.fill,
|
|
196
|
+
stroke: ink,
|
|
197
|
+
"stroke-width": 1.5,
|
|
198
|
+
"stroke-linejoin": "round",
|
|
199
|
+
class: "blot",
|
|
200
|
+
});
|
|
201
|
+
const drop = (seed * 1.7) % (2 * Math.PI);
|
|
202
|
+
pen.el("circle", {
|
|
203
|
+
cx: cx + r * 1.22 * Math.cos(drop),
|
|
204
|
+
cy: cy + r * 0.95 * Math.sin(drop),
|
|
205
|
+
r: r * 0.12,
|
|
206
|
+
fill: colors.black.fill,
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/** date draws what a cell shows: its number, a "?" or a blot, its colour and its thing. */
|
|
210
|
+
function date(pen, day, look, x, y, w, h) {
|
|
211
|
+
const cx = x + w / 2;
|
|
212
|
+
const cy = y + h / 2;
|
|
213
|
+
const fill = look.mark?.color;
|
|
214
|
+
if (fill)
|
|
215
|
+
pen.el("rect", {
|
|
216
|
+
x: x + 4,
|
|
217
|
+
y: y + 4,
|
|
218
|
+
width: w - 8,
|
|
219
|
+
height: h - 8,
|
|
220
|
+
rx: 8,
|
|
221
|
+
fill: colors[fill].fill,
|
|
222
|
+
stroke: ink,
|
|
223
|
+
"stroke-width": 1.5,
|
|
224
|
+
class: "marked",
|
|
225
|
+
});
|
|
226
|
+
if (look.blot) {
|
|
227
|
+
blot(pen, cx, cy, Math.min(w, h) * 0.33, day);
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
const thing = look.mark?.thing;
|
|
231
|
+
// A thing stands in the upper right corner of a wide cell, the date moved
|
|
232
|
+
// down and left; in a narrow cell it stands under the date.
|
|
233
|
+
const wide = w >= 56;
|
|
234
|
+
const side = wide ? 22 : 20;
|
|
235
|
+
const tx = thing === undefined || !wide ? cx : x + w * 0.36;
|
|
236
|
+
const ty = thing === undefined ? cy : wide ? y + h * 0.64 : y + h * 0.33;
|
|
237
|
+
if (look.ask)
|
|
238
|
+
drawThing(pen, { ask: true }, tx, ty, Math.min(40, thing === undefined ? h - 10 : 28));
|
|
239
|
+
else
|
|
240
|
+
pen.text(tx, ty, String(day), { size: dateSize, fill: fill ? textOn(fill) : ink });
|
|
241
|
+
if (thing === undefined)
|
|
242
|
+
return;
|
|
243
|
+
if (wide)
|
|
244
|
+
drawThing(pen, thing, x + w - side / 2 - 6, y + side / 2 + 6, side);
|
|
245
|
+
else
|
|
246
|
+
drawThing(pen, thing, cx, y + h * 0.75, side);
|
|
247
|
+
}
|
|
248
|
+
function lookOf(c, day) {
|
|
249
|
+
return {
|
|
250
|
+
...(c.marks?.find((m) => m.day === day) ? { mark: c.marks.find((m) => m.day === day) } : {}),
|
|
251
|
+
ask: c.ask?.includes(day) ?? false,
|
|
252
|
+
blot: c.blots?.includes(day) ?? false,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
/** band draws the coloured top of a page with the month's name, round at its top corners. */
|
|
256
|
+
function band(pen, c, width, height) {
|
|
257
|
+
const r = 10;
|
|
258
|
+
const colour = c.color ?? "red";
|
|
259
|
+
pen.el("path", {
|
|
260
|
+
d: `M0 ${n(height)}V${r}A${r} ${r} 0 0 1 ${r} 0H${n(width - r)}A${r} ${r} 0 0 1 ${n(width)} ${r}V${n(height)}Z`,
|
|
261
|
+
fill: colors[colour].fill,
|
|
262
|
+
});
|
|
263
|
+
if (c.month)
|
|
264
|
+
pen.text(width / 2, height / 2, c.month, { size: 22, fill: textOn(colour) });
|
|
265
|
+
}
|
|
266
|
+
function monthPage(pen, c) {
|
|
267
|
+
const rows = rowsOf(c);
|
|
268
|
+
const width = 7 * cellW;
|
|
269
|
+
const top = c.month ? 44 : 20;
|
|
270
|
+
const head = 32;
|
|
271
|
+
const grid = top + head;
|
|
272
|
+
const height = grid + rows * cellH;
|
|
273
|
+
pen.el("rect", { x: 0, y: 0, width, height, rx: 10, fill: "#ffffff" });
|
|
274
|
+
band(pen, c, width, top);
|
|
275
|
+
pen.el("rect", { x: 0, y: top, width, height: head, fill: soft });
|
|
276
|
+
columnsOf(c).forEach((w, i) => {
|
|
277
|
+
pen.text((i + 0.5) * cellW, top + head / 2, w, { size: 15, weight: 700 });
|
|
278
|
+
});
|
|
279
|
+
for (let day = 1; day <= c.days; day++) {
|
|
280
|
+
const { row, col } = cellOf(c, day);
|
|
281
|
+
date(pen, day, lookOf(c, day), col * cellW, grid + row * cellH, cellW, cellH);
|
|
282
|
+
}
|
|
283
|
+
let lines = "";
|
|
284
|
+
for (let i = 1; i < 7; i++)
|
|
285
|
+
lines += `M${i * cellW} ${n(top)}V${n(height)}`;
|
|
286
|
+
for (let r = 0; r < rows; r++)
|
|
287
|
+
lines += `M0 ${n(grid + r * cellH)}H${width}`;
|
|
288
|
+
pen.el("path", { d: lines, stroke: ink, "stroke-width": 1.5, fill: "none", class: "grid" });
|
|
289
|
+
pen.el("path", { d: `M0 ${n(top)}H${width}`, stroke: ink, "stroke-width": outline });
|
|
290
|
+
pen.el("rect", {
|
|
291
|
+
x: 0,
|
|
292
|
+
y: 0,
|
|
293
|
+
width,
|
|
294
|
+
height,
|
|
295
|
+
rx: 10,
|
|
296
|
+
fill: "none",
|
|
297
|
+
stroke: ink,
|
|
298
|
+
"stroke-width": outline,
|
|
299
|
+
});
|
|
300
|
+
return { width, height };
|
|
301
|
+
}
|
|
302
|
+
function stripPage(pen, c) {
|
|
303
|
+
const k = c.count;
|
|
304
|
+
const w = Math.min(72, Math.floor((640 - 2 * margin) / k));
|
|
305
|
+
const width = k * w;
|
|
306
|
+
const top = c.month ? 40 : 0;
|
|
307
|
+
const head = c.first ? 30 : 0;
|
|
308
|
+
const h = 56;
|
|
309
|
+
const height = top + head + h;
|
|
310
|
+
pen.el("rect", { x: 0, y: 0, width, height, rx: 10, fill: "#ffffff" });
|
|
311
|
+
if (c.month)
|
|
312
|
+
band(pen, c, width, top);
|
|
313
|
+
if (c.first) {
|
|
314
|
+
pen.el("path", {
|
|
315
|
+
d: top > 0
|
|
316
|
+
? `M0 ${top}H${width}V${top + head}H0Z`
|
|
317
|
+
: `M0 ${head}V10A10 10 0 0 1 10 0H${width - 10}A10 10 0 0 1 ${width} 10V${head}Z`,
|
|
318
|
+
fill: soft,
|
|
319
|
+
});
|
|
320
|
+
for (let i = 0; i < k; i++)
|
|
321
|
+
pen.text((i + 0.5) * w, top + head / 2, weekdayOf(c.first, i), { size: 15 });
|
|
322
|
+
}
|
|
323
|
+
datesOf(c).forEach((day, i) => {
|
|
324
|
+
date(pen, day, lookOf(c, day), i * w, top + head, w, h);
|
|
325
|
+
});
|
|
326
|
+
let lines = "";
|
|
327
|
+
for (let i = 1; i < k; i++)
|
|
328
|
+
lines += `M${i * w} ${top}V${height}`;
|
|
329
|
+
if (c.first)
|
|
330
|
+
lines += `M0 ${top + head}H${width}`;
|
|
331
|
+
pen.el("path", { d: lines, stroke: ink, "stroke-width": 1.5, fill: "none", class: "grid" });
|
|
332
|
+
if (c.month)
|
|
333
|
+
pen.el("path", { d: `M0 ${top}H${width}`, stroke: ink, "stroke-width": outline });
|
|
334
|
+
pen.el("rect", {
|
|
335
|
+
x: 0,
|
|
336
|
+
y: 0,
|
|
337
|
+
width,
|
|
338
|
+
height,
|
|
339
|
+
rx: 10,
|
|
340
|
+
fill: "none",
|
|
341
|
+
stroke: ink,
|
|
342
|
+
"stroke-width": outline,
|
|
343
|
+
});
|
|
344
|
+
return { width, height };
|
|
345
|
+
}
|
|
346
|
+
function sheetsPage(pen, c) {
|
|
347
|
+
const sheets = c.sheets;
|
|
348
|
+
const k = sheets.length;
|
|
349
|
+
const gap = 18;
|
|
350
|
+
const w = Math.min(140, Math.floor((640 - 2 * margin - (k - 1) * gap) / k));
|
|
351
|
+
const h = 176;
|
|
352
|
+
const colour = c.color ?? "red";
|
|
353
|
+
sheets.forEach((s, i) => {
|
|
354
|
+
const x = i * (w + gap);
|
|
355
|
+
const y = 10;
|
|
356
|
+
pen.el("rect", {
|
|
357
|
+
x,
|
|
358
|
+
y,
|
|
359
|
+
width: w,
|
|
360
|
+
height: h,
|
|
361
|
+
rx: 8,
|
|
362
|
+
fill: "#ffffff",
|
|
363
|
+
stroke: ink,
|
|
364
|
+
"stroke-width": outline,
|
|
365
|
+
class: "sheet",
|
|
366
|
+
});
|
|
367
|
+
// The band with the month's name, square below, round at the top.
|
|
368
|
+
pen.el("path", {
|
|
369
|
+
d: `M${x} ${y + 40}V${y + 8}A8 8 0 0 1 ${x + 8} ${y}H${x + w - 8}A8 8 0 0 1 ${x + w} ${y + 8}V${y + 40}Z`,
|
|
370
|
+
fill: colors[colour].fill,
|
|
371
|
+
stroke: ink,
|
|
372
|
+
"stroke-width": outline,
|
|
373
|
+
"stroke-linejoin": "round",
|
|
374
|
+
});
|
|
375
|
+
// Two rings that hold the sheet to the pad.
|
|
376
|
+
for (const rx of [x + w * 0.3, x + w * 0.7]) {
|
|
377
|
+
pen.el("rect", {
|
|
378
|
+
x: rx - 4,
|
|
379
|
+
y: y - 10,
|
|
380
|
+
width: 8,
|
|
381
|
+
height: 20,
|
|
382
|
+
rx: 4,
|
|
383
|
+
fill: colors.gray.fill,
|
|
384
|
+
stroke: ink,
|
|
385
|
+
"stroke-width": 2,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
const cx = x + w / 2;
|
|
389
|
+
if (s.ask === "month")
|
|
390
|
+
drawThing(pen, { ask: true }, cx, y + 23, 30);
|
|
391
|
+
else if (s.month)
|
|
392
|
+
pen.text(cx, y + 23, s.month, { size: 18, fill: textOn(colour) });
|
|
393
|
+
if (s.ask === "day")
|
|
394
|
+
drawThing(pen, { ask: true }, cx, y + 92, 66);
|
|
395
|
+
else if (s.day !== undefined)
|
|
396
|
+
pen.text(cx, y + 92, String(s.day), { size: 58 });
|
|
397
|
+
if (s.ask === "weekday")
|
|
398
|
+
drawThing(pen, { ask: true }, cx, y + 148, 34);
|
|
399
|
+
else if (s.weekday)
|
|
400
|
+
pen.text(cx, y + 148, dayWords[s.weekday], { size: w < 120 ? 15 : 18 });
|
|
401
|
+
});
|
|
402
|
+
return { width: k * w + (k - 1) * gap, height: h + 10 };
|
|
403
|
+
}
|
|
404
|
+
// --- words ------------------------------------------------------------------
|
|
405
|
+
function an(words) {
|
|
406
|
+
return `${/^[aeiou]/.test(words) ? "an" : "a"} ${words}`;
|
|
407
|
+
}
|
|
408
|
+
function sayMarks(c, place) {
|
|
409
|
+
return (c.marks ?? []).map((m) => {
|
|
410
|
+
const what = [];
|
|
411
|
+
if (m.color)
|
|
412
|
+
what.push(`is coloured ${colors[m.color].word}`);
|
|
413
|
+
if (m.thing !== undefined)
|
|
414
|
+
what.push(`has ${an(wordOf(m.thing))} drawn on it`);
|
|
415
|
+
return `${place(m.day)} ${what.join(" and ")}.`;
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
function sayCell(c, day) {
|
|
419
|
+
if (c.ask?.includes(day))
|
|
420
|
+
return "?";
|
|
421
|
+
if (c.blots?.includes(day))
|
|
422
|
+
return "a blot";
|
|
423
|
+
return String(day);
|
|
424
|
+
}
|
|
425
|
+
export const calendar = {
|
|
426
|
+
kind: "calendar",
|
|
427
|
+
members: [
|
|
428
|
+
"kind",
|
|
429
|
+
"style",
|
|
430
|
+
"month",
|
|
431
|
+
"first",
|
|
432
|
+
"days",
|
|
433
|
+
"weekStart",
|
|
434
|
+
"start",
|
|
435
|
+
"count",
|
|
436
|
+
"marks",
|
|
437
|
+
"ask",
|
|
438
|
+
"blots",
|
|
439
|
+
"sheets",
|
|
440
|
+
"color",
|
|
441
|
+
],
|
|
442
|
+
check(scene, problems) {
|
|
443
|
+
only(scene, this.members, "scene", problems);
|
|
444
|
+
const before = problems.length;
|
|
445
|
+
const style = scene.style === undefined ? "month" : oneOf(scene.style, calendarStyles, "style", problems);
|
|
446
|
+
if (problems.length > before)
|
|
447
|
+
return;
|
|
448
|
+
for (const m of this.members) {
|
|
449
|
+
if (m === "kind" || m === "style" || scene[m] === undefined)
|
|
450
|
+
continue;
|
|
451
|
+
if (!styleMembers[style].includes(m))
|
|
452
|
+
problems.push(`${m}: a calendar of style "${style}" has no ${m}; it has ${styleMembers[style].join(", ")}`);
|
|
453
|
+
}
|
|
454
|
+
if (scene.color !== undefined && !isColor(scene.color))
|
|
455
|
+
problems.push(`color: must be a colour: ${colorNames.join(", ")}`);
|
|
456
|
+
if (problems.length > before)
|
|
457
|
+
return;
|
|
458
|
+
if (style === "sheets")
|
|
459
|
+
checkSheets(scene, problems);
|
|
460
|
+
else
|
|
461
|
+
checkPage(scene, style, problems);
|
|
462
|
+
},
|
|
463
|
+
draw(scene, pen) {
|
|
464
|
+
const c = scene;
|
|
465
|
+
const sub = pen.sub();
|
|
466
|
+
const style = styleOf(c);
|
|
467
|
+
const drawn = style === "month"
|
|
468
|
+
? monthPage(sub, c)
|
|
469
|
+
: style === "strip"
|
|
470
|
+
? stripPage(sub, c)
|
|
471
|
+
: sheetsPage(sub, c);
|
|
472
|
+
pen.place(sub, margin, margin);
|
|
473
|
+
return { width: drawn.width + 2 * margin, height: drawn.height + 2 * margin };
|
|
474
|
+
},
|
|
475
|
+
describe(scene) {
|
|
476
|
+
const c = scene;
|
|
477
|
+
const style = styleOf(c);
|
|
478
|
+
if (style === "sheets") {
|
|
479
|
+
const sheets = c.sheets;
|
|
480
|
+
const one = (s) => [
|
|
481
|
+
s.ask === "month" ? "?" : s.month,
|
|
482
|
+
s.ask === "day" ? "?" : s.day,
|
|
483
|
+
s.ask === "weekday" ? "?" : s.weekday ? dayWords[s.weekday] : undefined,
|
|
484
|
+
]
|
|
485
|
+
.filter((x) => x !== undefined)
|
|
486
|
+
.join(", ");
|
|
487
|
+
if (sheets.length === 1)
|
|
488
|
+
return `A torn-off calendar sheet: ${one(sheets[0])}.`;
|
|
489
|
+
return `${sheets.length} torn-off calendar sheets in a row. ${sheets
|
|
490
|
+
.map((s, i) => `Sheet ${i + 1}: ${one(s)}.`)
|
|
491
|
+
.join(" ")}`;
|
|
492
|
+
}
|
|
493
|
+
if (style === "strip") {
|
|
494
|
+
const dates = datesOf(c);
|
|
495
|
+
const cells = dates.map((d, i) => `${c.first ? `${dayWords[weekdayOf(c.first, i)]} ` : ""}${sayCell(c, d)}`);
|
|
496
|
+
const where = (day) => {
|
|
497
|
+
const i = dates.indexOf(day);
|
|
498
|
+
return c.ask?.includes(day)
|
|
499
|
+
? `The ${ordinal(i + 1)} cell, with the question mark,`
|
|
500
|
+
: `The ${ordinal(day)}`;
|
|
501
|
+
};
|
|
502
|
+
return [
|
|
503
|
+
`A strip of ${dates.length} days in a row${c.month ? `, headed ${c.month}` : ""}, from the left: ${cells.join(", ")}.`,
|
|
504
|
+
...sayMarks(c, where),
|
|
505
|
+
].join(" ");
|
|
506
|
+
}
|
|
507
|
+
const cols = columnsOf(c);
|
|
508
|
+
const rows = rowsOf(c);
|
|
509
|
+
const head = `A calendar page${c.month ? ` for ${c.month}` : ""}: ${c.days} days in ${rows} rows of weeks, the columns from ${dayWords[cols[0]]} to ${dayWords[cols[6]]}.`;
|
|
510
|
+
const hidden = (c.ask?.length ?? 0) + (c.blots?.length ?? 0) > 0;
|
|
511
|
+
const out = [head];
|
|
512
|
+
if (!hidden)
|
|
513
|
+
out.push(`The 1st is in the first row under ${dayWords[c.first]}, and the dates follow in order, a row for each week, up to the ${ordinal(c.days)}.`);
|
|
514
|
+
else
|
|
515
|
+
for (let r = 0; r < rows; r++) {
|
|
516
|
+
const cells = [];
|
|
517
|
+
for (let col = 0; col < 7; col++) {
|
|
518
|
+
const day = r * 7 + col - cols.indexOf(c.first) + 1;
|
|
519
|
+
cells.push(day >= 1 && day <= c.days ? sayCell(c, day) : "empty");
|
|
520
|
+
}
|
|
521
|
+
out.push(`Row ${r + 1}: ${cells.join(", ")}.`);
|
|
522
|
+
}
|
|
523
|
+
const where = (day) => {
|
|
524
|
+
if (!c.ask?.includes(day))
|
|
525
|
+
return `The ${ordinal(day)}`;
|
|
526
|
+
const { row, col } = cellOf(c, day);
|
|
527
|
+
return `The question mark in row ${row + 1} under ${dayWords[cols[col]]}`;
|
|
528
|
+
};
|
|
529
|
+
out.push(...sayMarks(c, where));
|
|
530
|
+
return out.join(" ");
|
|
531
|
+
},
|
|
532
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Kind } from "../kind.ts";
|
|
2
|
+
import { type ColorName } from "../style.ts";
|
|
3
|
+
import { type Thing } from "../things.ts";
|
|
4
|
+
/**
|
|
5
|
+
* A chart: a bar chart, its bars standing on a line side by side or lying
|
|
6
|
+
* one under another, read against a scale with a line at every step; or a
|
|
7
|
+
* picture chart, where each bar is a column or a row of icons, one icon
|
|
8
|
+
* standing for a unit and half an icon for half of it, with a key that says
|
|
9
|
+
* so. A bar is named by a word, an object or both, and a missing bar is a
|
|
10
|
+
* "?" in its place. A bar ends on a line of the scale or halfway between two,
|
|
11
|
+
* so that its value can be read exactly.
|
|
12
|
+
*/
|
|
13
|
+
export declare const chartStyles: readonly ["bars", "pictures"];
|
|
14
|
+
export type ChartStyle = (typeof chartStyles)[number];
|
|
15
|
+
export declare const keyModes: readonly ["show", "ask", "none"];
|
|
16
|
+
export type KeyMode = (typeof keyModes)[number];
|
|
17
|
+
export type Bar = {
|
|
18
|
+
label?: string;
|
|
19
|
+
thing?: Thing;
|
|
20
|
+
value?: number;
|
|
21
|
+
ask?: true;
|
|
22
|
+
color?: ColorName;
|
|
23
|
+
};
|
|
24
|
+
export type ChartScene = {
|
|
25
|
+
kind: "chart";
|
|
26
|
+
style?: ChartStyle;
|
|
27
|
+
horizontal?: boolean;
|
|
28
|
+
bars: Bar[];
|
|
29
|
+
step?: number;
|
|
30
|
+
max?: number;
|
|
31
|
+
color?: ColorName;
|
|
32
|
+
icon?: Thing;
|
|
33
|
+
unit?: number;
|
|
34
|
+
key?: KeyMode;
|
|
35
|
+
title?: string;
|
|
36
|
+
};
|
|
37
|
+
/** scaleOf is the scale of a bar chart: its step, its top and how many lines it has. */
|
|
38
|
+
export declare function scaleOf(c: ChartScene): {
|
|
39
|
+
step: number;
|
|
40
|
+
max: number;
|
|
41
|
+
lines: number;
|
|
42
|
+
};
|
|
43
|
+
/** iconsOf is how many whole icons a value makes, and whether half an icon more. */
|
|
44
|
+
export declare function iconsOf(value: number, unit: number): {
|
|
45
|
+
whole: number;
|
|
46
|
+
half: boolean;
|
|
47
|
+
};
|
|
48
|
+
export declare const chart: Kind;
|