@cogniplay/logic-games 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 (89) hide show
  1. package/README.md +172 -2
  2. package/board.d.ts +102 -0
  3. package/board.d.ts.map +1 -0
  4. package/board.js +201 -0
  5. package/board.js.map +1 -0
  6. package/clues.d.ts +16 -0
  7. package/clues.d.ts.map +1 -0
  8. package/clues.js +64 -0
  9. package/clues.js.map +1 -0
  10. package/digits/generated/digits.d.ts +10 -0
  11. package/digits/generated/digits.d.ts.map +1 -0
  12. package/digits/generated/digits.js +397 -0
  13. package/digits/generated/digits.js.map +1 -0
  14. package/digits.d.ts +7 -0
  15. package/digits.d.ts.map +1 -0
  16. package/digits.js +38 -0
  17. package/digits.js.map +1 -0
  18. package/fair-shares.d.ts +20 -0
  19. package/fair-shares.d.ts.map +1 -0
  20. package/fair-shares.js +164 -0
  21. package/fair-shares.js.map +1 -0
  22. package/fills.d.ts +31 -0
  23. package/fills.d.ts.map +1 -0
  24. package/fills.js +142 -0
  25. package/fills.js.map +1 -0
  26. package/geometry.d.ts +14 -0
  27. package/geometry.d.ts.map +1 -0
  28. package/geometry.js +55 -0
  29. package/geometry.js.map +1 -0
  30. package/index.d.ts +12 -0
  31. package/index.d.ts.map +1 -0
  32. package/index.js +9 -0
  33. package/index.js.map +1 -0
  34. package/line-counts.d.ts +25 -0
  35. package/line-counts.d.ts.map +1 -0
  36. package/line-counts.js +136 -0
  37. package/line-counts.js.map +1 -0
  38. package/marks.d.ts +17 -0
  39. package/marks.d.ts.map +1 -0
  40. package/marks.js +50 -0
  41. package/marks.js.map +1 -0
  42. package/outline.d.ts +31 -0
  43. package/outline.d.ts.map +1 -0
  44. package/outline.js +150 -0
  45. package/outline.js.map +1 -0
  46. package/package.json +21 -4
  47. package/positions.d.ts +7 -0
  48. package/positions.d.ts.map +1 -0
  49. package/positions.js +28 -0
  50. package/positions.js.map +1 -0
  51. package/regions.d.ts +20 -0
  52. package/regions.d.ts.map +1 -0
  53. package/regions.js +84 -0
  54. package/regions.js.map +1 -0
  55. package/solution.d.ts +15 -0
  56. package/solution.d.ts.map +1 -0
  57. package/solution.js +50 -0
  58. package/solution.js.map +1 -0
  59. package/src/board.ts +377 -0
  60. package/src/clues.ts +91 -0
  61. package/src/digits/OFL.txt +93 -0
  62. package/src/digits/README.md +5 -0
  63. package/src/digits/generated/digits.ts +404 -0
  64. package/src/digits.ts +58 -0
  65. package/src/fair-shares.ts +257 -0
  66. package/src/fills.ts +176 -0
  67. package/src/geometry.ts +66 -0
  68. package/src/index.ts +31 -0
  69. package/src/line-counts.ts +211 -0
  70. package/src/marks.ts +68 -0
  71. package/src/outline.ts +196 -0
  72. package/src/positions.ts +35 -0
  73. package/src/regions.ts +108 -0
  74. package/src/solution.ts +75 -0
  75. package/src/svg.ts +46 -0
  76. package/src/tray.ts +114 -0
  77. package/src/types.ts +106 -0
  78. package/svg.d.ts +7 -0
  79. package/svg.d.ts.map +1 -0
  80. package/svg.js +29 -0
  81. package/svg.js.map +1 -0
  82. package/tray.d.ts +32 -0
  83. package/tray.d.ts.map +1 -0
  84. package/tray.js +58 -0
  85. package/tray.js.map +1 -0
  86. package/types.d.ts +95 -0
  87. package/types.d.ts.map +1 -0
  88. package/types.js +2 -0
  89. package/types.js.map +1 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"solution.d.ts","sourceRoot":"","sources":["src/solution.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAInB,MAAM,YAAY,CAAC;AASpB,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAE3C,2DAA2D;AAC3D,eAAO,MAAM,GAAG,IAAI,CAAC;AAIrB;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,QAAQ,EAClB,OAAO,GAAE,aAAa,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAO,GAChD,MAAM,CA2CR"}
package/solution.js ADDED
@@ -0,0 +1,50 @@
1
+ import { bounds, drawPosition, requireSquare, } from "./board.js";
2
+ import { isFairShares, renderFairShares } from "./fair-shares.js";
3
+ import { isLineCounts, positionCount, renderLineCounts, } from "./line-counts.js";
4
+ import { positions } from "./positions.js";
5
+ import { element, fmt, root } from "./svg.js";
6
+ /** Millimetres between positions in a solution drawing. */
7
+ export const gap = 8;
8
+ const defaultPerRow = 4;
9
+ /**
10
+ * Amoeba's solution: every position in order, left to right, wrapping after
11
+ * `perRow` (default 4), as one SVG document. A Line Counts or a Fair Shares
12
+ * board's solution is the solved board alone. The order is the numbering; the moved piece and the
13
+ * hint of where it came from show each move. Every position is drawn within
14
+ * the same bounds, so the boards line up.
15
+ */
16
+ export function renderSolution(instance, options = {}) {
17
+ if (isLineCounts(instance)) {
18
+ // The order of a Line Counts solution carries no meaning, so there is no
19
+ // row of positions: the solution is the board with every piece placed.
20
+ return renderLineCounts(instance, {
21
+ ...options,
22
+ position: positionCount(instance) - 1,
23
+ });
24
+ }
25
+ if (isFairShares(instance)) {
26
+ // Likewise: the answer is the split, and one drawing shows it.
27
+ return renderFairShares(instance, {
28
+ ...options,
29
+ position: positionCount(instance) - 1,
30
+ });
31
+ }
32
+ requireSquare(instance);
33
+ const all = positions(instance);
34
+ const perRow = Math.max(1, Math.floor(options.perRow ?? defaultPerRow));
35
+ const within = bounds(instance, all);
36
+ const drawings = all.map((position, i) => drawPosition(instance, position, all[i - 1], options, within));
37
+ const width = Math.max(...drawings.map((d) => d.width));
38
+ const height = Math.max(...drawings.map((d) => d.height));
39
+ const columns = Math.min(perRow, drawings.length);
40
+ const rows = Math.ceil(drawings.length / perRow);
41
+ const body = drawings
42
+ .map((drawing, i) => {
43
+ const x = (i % perRow) * (width + gap);
44
+ const y = Math.floor(i / perRow) * (height + gap);
45
+ return element("g", { class: "position", transform: `translate(${fmt(x)} ${fmt(y)})` }, drawing.body);
46
+ })
47
+ .join("");
48
+ return root(columns * width + (columns - 1) * gap, rows * height + (rows - 1) * gap, body);
49
+ }
50
+ //# sourceMappingURL=solution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"solution.js","sourceRoot":"","sources":["src/solution.ts"],"names":[],"mappings":"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"}
package/src/board.ts ADDED
@@ -0,0 +1,377 @@
1
+ import { isFairShares, renderFairShares } from "./fair-shares.js";
2
+ import {
3
+ type Fill,
4
+ flatFill,
5
+ ghostFill,
6
+ ghostGrey,
7
+ pieceFill,
8
+ } from "./fills.js";
9
+ import { place } from "./geometry.js";
10
+ import { isLineCounts, renderLineCounts } from "./line-counts.js";
11
+ import { type MarkDrawer, placeholder, standardMarks } from "./marks.js";
12
+ import { insetLoops, loopsPath } from "./outline.js";
13
+ import { positions } from "./positions.js";
14
+ import { element, root } from "./svg.js";
15
+ import type { Cell, GameData, Instance, Piece, Position } from "./types.js";
16
+
17
+ export interface RenderOptions {
18
+ /** The side of a cell in millimetres. Default 16. */
19
+ cellSizeMm?: number;
20
+ /** Drawings for mark kinds, by kind. Overrides the standard ones. */
21
+ marks?: Record<string, MarkDrawer>;
22
+ /**
23
+ * A flat colour per piece id, such as the kit's. A piece without one gets
24
+ * the greyscale fills, which print well.
25
+ */
26
+ pieceColours?: Record<string, string>;
27
+ /**
28
+ * How pieces are drawn. A `tile` is filled and heavily outlined, as a piece
29
+ * on the board looks. A `ghost` is a thin outline with light stripes, for
30
+ * the child's sheet, where the real piece covers the drawing: it marks the
31
+ * place without competing with the blockers. Default `tile`.
32
+ */
33
+ pieceStyle?: "tile" | "ghost";
34
+ /**
35
+ * The cell size of the tray, where a Line Counts board's pieces to place
36
+ * and a Fair Shares board's pieces to share are drawn, as a share of the
37
+ * board's. Default 1: the board's scale.
38
+ */
39
+ trayScale?: number;
40
+ /**
41
+ * Where the tray's row of pieces wraps, in millimetres. Default: the
42
+ * board's width with its clues, or the grids' width together.
43
+ */
44
+ trayWidthMm?: number;
45
+ /**
46
+ * Where the tray lies: above the board or under it. Default `below` for
47
+ * Line Counts and `above` for Fair Shares, whose pieces are what the child
48
+ * reads first.
49
+ */
50
+ trayPosition?: "above" | "below";
51
+ /**
52
+ * How many of a Fair Shares board's grids lie side by side before the next
53
+ * row starts. Default: as near a square of grids as there is, so two for
54
+ * two shares, and two above one for three.
55
+ */
56
+ gridsPerRow?: number;
57
+ /**
58
+ * Keeps the tray's room at every position: the drawing is as large as at
59
+ * the start, and the board and any piece still to place stay where they
60
+ * were, so a view that steps through the positions doesn't move. On a Fair
61
+ * Shares board whose solution leaves a piece out, the start then has the
62
+ * room of the box drawn around that piece. Default false: the drawing is
63
+ * as large as what it shows.
64
+ */
65
+ keepTrayRoom?: boolean;
66
+ }
67
+
68
+ /** SVG content without a root, and its size in millimetres. */
69
+ export interface Drawing {
70
+ body: string;
71
+ width: number;
72
+ height: number;
73
+ }
74
+
75
+ /** The cells a drawing must hold: the board, and anything placed on or off it. */
76
+ export interface Bounds {
77
+ minX: number;
78
+ minY: number;
79
+ columns: number;
80
+ rows: number;
81
+ }
82
+
83
+ /** Millimetres around the board, so the heavy outline isn't clipped. */
84
+ export const padding = 1;
85
+
86
+ export const defaultCellSize = 16;
87
+
88
+ const gridStroke = { stroke: "#c4c4c4", "stroke-width": 0.2 };
89
+ const heavyStroke = {
90
+ stroke: "#000",
91
+ "stroke-width": 0.8,
92
+ "stroke-linejoin": "round",
93
+ fill: "none",
94
+ };
95
+ const movedStroke = { ...heavyStroke, "stroke-width": 1.4 };
96
+ const ghostStroke = (colour: string) => ({
97
+ stroke: colour,
98
+ "stroke-width": 0.4,
99
+ "stroke-linejoin": "round",
100
+ fill: "none",
101
+ });
102
+
103
+ /** A piece's fill and outline, by the options and its order. */
104
+ export interface Look {
105
+ fill: Fill;
106
+ outline: Record<string, string | number>;
107
+ }
108
+
109
+ export function lookOf(
110
+ pieceId: string,
111
+ index: number,
112
+ options: RenderOptions
113
+ ): Look {
114
+ const colour = options.pieceColours?.[pieceId];
115
+ if (options.pieceStyle === "ghost") {
116
+ return {
117
+ fill: ghostFill(colour),
118
+ outline: ghostStroke(colour ?? ghostGrey),
119
+ };
120
+ }
121
+ return {
122
+ fill: colour === undefined ? pieceFill(index) : flatFill(colour),
123
+ outline: heavyStroke,
124
+ };
125
+ }
126
+ const movedFromStroke = {
127
+ stroke: "#777",
128
+ "stroke-width": 0.5,
129
+ "stroke-dasharray": "1.5 1",
130
+ "stroke-linejoin": "round",
131
+ fill: "none",
132
+ };
133
+ const blockerStyle = { fill: "#8c8c8c", stroke: "#666", "stroke-width": 0.3 };
134
+
135
+ /** Pieces and blockers are tiles: a little smaller than their cells, with rounded corners, so neighbours show a seam. */
136
+ const insetOf = (size: number) => size * 0.04;
137
+ const radiusOf = (size: number) => size * 0.06;
138
+
139
+ export function requireSquare(instance: { geometry: string }): void {
140
+ if (instance.geometry !== "square") {
141
+ throw new Error(
142
+ `Only square geometry is drawn, not "${instance.geometry}"`
143
+ );
144
+ }
145
+ }
146
+
147
+ /**
148
+ * The bounds of the board together with every piece and mark in the given
149
+ * positions, so nothing placed off the board is clipped away silently. For a
150
+ * valid instance that is the board alone.
151
+ */
152
+ export function bounds(instance: Instance, included: Position[]): Bounds {
153
+ if (instance.board.length === 0) {
154
+ throw new Error("The board has no cells");
155
+ }
156
+ const cells: Cell[] = [
157
+ ...instance.board,
158
+ ...(instance.marks ?? []).map((mark) => mark.cell),
159
+ ...included.flatMap((position) =>
160
+ position.pieces.flatMap((piece) => place(piece.shape, piece))
161
+ ),
162
+ ];
163
+ const xs = cells.map(([x]) => x);
164
+ const ys = cells.map(([, y]) => y);
165
+ const minX = Math.min(...xs);
166
+ const minY = Math.min(...ys);
167
+ return {
168
+ minX,
169
+ minY,
170
+ columns: Math.max(...xs) - minX + 1,
171
+ rows: Math.max(...ys) - minY + 1,
172
+ };
173
+ }
174
+
175
+ /**
176
+ * One position's board as SVG content, without a root. `previous` is the
177
+ * position before it, for the moved-from hint. The bounds default to the
178
+ * board and this position's pieces.
179
+ */
180
+ export function drawPosition(
181
+ instance: Instance,
182
+ position: Position,
183
+ previous: Position | undefined,
184
+ options: RenderOptions = {},
185
+ within: Bounds = bounds(instance, [position])
186
+ ): Drawing {
187
+ requireSquare(instance);
188
+ const size = options.cellSizeMm ?? defaultCellSize;
189
+ const { minX, minY, columns, rows } = within;
190
+ const toMm = ([x, y]: Cell): [number, number] => [
191
+ padding + (x - minX) * size,
192
+ padding + (y - minY) * size,
193
+ ];
194
+ const board = drawCells(instance.board, toMm, size);
195
+
196
+ const inset = insetOf(size);
197
+ const blockers = element(
198
+ "g",
199
+ { class: "blockers" },
200
+ (instance.blockers ?? [])
201
+ .map((cell) => {
202
+ const [x, y] = toMm(cell);
203
+ return element("rect", {
204
+ class: "blocker",
205
+ x: x + inset,
206
+ y: y + inset,
207
+ width: size - 2 * inset,
208
+ height: size - 2 * inset,
209
+ rx: radiusOf(size),
210
+ ...blockerStyle,
211
+ });
212
+ })
213
+ .join("")
214
+ );
215
+
216
+ const movedFrom = (() => {
217
+ if (position.moved === undefined || previous === undefined) return "";
218
+ const before = previous.pieces.find((p) => p.pieceId === position.moved);
219
+ if (before === undefined) return "";
220
+ return element("path", {
221
+ class: "moved-from",
222
+ d: tilePath(place(before.shape, before), toMm, size),
223
+ ...movedFromStroke,
224
+ });
225
+ })();
226
+
227
+ const pieces = element(
228
+ "g",
229
+ { class: "pieces" },
230
+ position.pieces
231
+ .map((piece, index) =>
232
+ drawPiece(
233
+ piece,
234
+ lookOf(piece.pieceId, index, options),
235
+ position.moved,
236
+ toMm,
237
+ size
238
+ )
239
+ )
240
+ .join("")
241
+ );
242
+
243
+ const drawers = { ...standardMarks, ...options.marks };
244
+ const marks = element(
245
+ "g",
246
+ { class: "marks" },
247
+ (instance.marks ?? [])
248
+ .map((mark) => {
249
+ const [x, y] = toMm(mark.cell);
250
+ const draw = drawers[mark.kind] ?? placeholder;
251
+ return element(
252
+ "g",
253
+ { class: "mark", "data-kind": mark.kind },
254
+ draw({ x, y, size })
255
+ );
256
+ })
257
+ .join("")
258
+ );
259
+
260
+ return {
261
+ body: board + blockers + movedFrom + pieces + marks,
262
+ width: padding * 2 + columns * size,
263
+ height: padding * 2 + rows * size,
264
+ };
265
+ }
266
+
267
+ /** The tile a set of cells makes: its outline, inset and rounded, as one path. */
268
+ function tilePath(
269
+ cells: readonly Cell[],
270
+ toMm: (cell: Cell) => [number, number],
271
+ size: number
272
+ ): string {
273
+ return loopsPath(
274
+ insetLoops(cells, toMm, size, insetOf(size)),
275
+ radiusOf(size)
276
+ );
277
+ }
278
+
279
+ function drawPiece(
280
+ piece: Piece,
281
+ look: Look,
282
+ moved: string | undefined,
283
+ toMm: (cell: Cell) => [number, number],
284
+ size: number
285
+ ): string {
286
+ const isMoved = piece.pieceId === moved;
287
+ return drawTile(
288
+ piece.pieceId,
289
+ place(piece.shape, piece),
290
+ look,
291
+ isMoved ? "piece moved" : "piece",
292
+ isMoved,
293
+ toMm,
294
+ size
295
+ );
296
+ }
297
+
298
+ /** The board's cells and its heavy outline, as a group. */
299
+ export function drawCells(
300
+ cells: readonly Cell[],
301
+ toMm: (cell: Cell) => [number, number],
302
+ size: number
303
+ ): string {
304
+ return element(
305
+ "g",
306
+ { class: "board" },
307
+ cells
308
+ .map((cell) => {
309
+ const [x, y] = toMm(cell);
310
+ return element("rect", {
311
+ x,
312
+ y,
313
+ width: size,
314
+ height: size,
315
+ class: "cell",
316
+ fill: "#fff",
317
+ ...gridStroke,
318
+ });
319
+ })
320
+ .join("") +
321
+ element("path", {
322
+ class: "board-outline",
323
+ d: loopsPath(insetLoops(cells, toMm, size, 0), radiusOf(size)),
324
+ ...heavyStroke,
325
+ })
326
+ );
327
+ }
328
+
329
+ /**
330
+ * One piece as a tile over the cells it covers: its fill, its pattern and its
331
+ * outline, which is the heavier one when `heavy`.
332
+ */
333
+ export function drawTile(
334
+ pieceId: string,
335
+ cells: readonly Cell[],
336
+ { fill, outline }: Look,
337
+ classes: string,
338
+ heavy: boolean,
339
+ toMm: (cell: Cell) => [number, number],
340
+ size: number
341
+ ): string {
342
+ const loops = insetLoops(cells, toMm, size, insetOf(size));
343
+ const d = loopsPath(loops, radiusOf(size));
344
+ return element(
345
+ "g",
346
+ { class: classes, "data-piece": pieceId },
347
+ element("path", { class: "piece-fill", d, fill: fill.colour }) +
348
+ fill.pattern({ cells: cells.map(toMm), size, loops }) +
349
+ element("path", { d, ...(heavy ? movedStroke : outline) })
350
+ );
351
+ }
352
+
353
+ /** The board at one position, the start by default, as a complete SVG document. */
354
+ export function renderBoard(
355
+ instance: GameData,
356
+ options: RenderOptions & { position?: number } = {}
357
+ ): string {
358
+ if (isLineCounts(instance)) return renderLineCounts(instance, options);
359
+ if (isFairShares(instance)) return renderFairShares(instance, options);
360
+ requireSquare(instance);
361
+ const index = options.position ?? 0;
362
+ const count = (instance.solution ?? []).length + 1;
363
+ if (!Number.isInteger(index) || index < 0 || index >= count) {
364
+ throw new RangeError(
365
+ `No position ${index}; there are ${count}, numbered 0 to ${count - 1}`
366
+ );
367
+ }
368
+ // Replay only up to the position asked for, so a bad later move doesn't
369
+ // stop earlier positions from drawing.
370
+ const all = positions({
371
+ ...instance,
372
+ solution: (instance.solution ?? []).slice(0, index),
373
+ });
374
+ const position = all[index] as Position;
375
+ const drawing = drawPosition(instance, position, all[index - 1], options);
376
+ return root(drawing.width, drawing.height, drawing.body);
377
+ }
package/src/clues.ts ADDED
@@ -0,0 +1,91 @@
1
+ import { numberPath } from "./digits.js";
2
+ import { element } from "./svg.js";
3
+ import type { Cell, Clue } from "./types.js";
4
+
5
+ /** The clue's square, the number's height and its widest, as shares of a cell. */
6
+ const square = 0.62;
7
+ const corner = 0.1;
8
+ const outline = 0.05;
9
+ const digitHeight = 0.36;
10
+ const digitWidth = 0.44;
11
+
12
+ const ink = "#222";
13
+
14
+ /** The bands the clues need beside the board, in cells: one on the left for rows' clues, one on top for columns'. */
15
+ export function clueBands(clues: readonly Clue[]): {
16
+ left: number;
17
+ top: number;
18
+ } {
19
+ return {
20
+ left: clues.some((clue) => clue.direction === "row") ? 1 : 0,
21
+ top: clues.some((clue) => clue.direction === "column") ? 1 : 0,
22
+ };
23
+ }
24
+
25
+ /** The cell of the band a clue sits in: left of its row, above its column. */
26
+ export function clueCell(clue: Clue, minX: number, minY: number): Cell {
27
+ return clue.direction === "row"
28
+ ? [minX - 1, clue.index]
29
+ : [clue.index, minY - 1];
30
+ }
31
+
32
+ /**
33
+ * The clues as a group; empty when there are none. A count of covered cells
34
+ * is a white number in a solid square; a count of empty cells is a dark
35
+ * number in an outlined one. The square is smaller than a cell and stands off
36
+ * the board, so it reads as neither a cell nor a blocker.
37
+ */
38
+ export function drawClues(
39
+ clues: readonly Clue[],
40
+ minX: number,
41
+ minY: number,
42
+ toMm: (cell: Cell) => [number, number],
43
+ size: number
44
+ ): string {
45
+ if (clues.length === 0) return "";
46
+ const side = size * square;
47
+ const margin = (size - side) / 2;
48
+ return element(
49
+ "g",
50
+ { class: "clues" },
51
+ clues
52
+ .map((clue) => {
53
+ const [x, y] = toMm(clueCell(clue, minX, minY));
54
+ const covered = clue.kind === "covered";
55
+ // The outline is drawn inside the square, so both kinds are one size.
56
+ const stroke = covered ? 0 : size * outline;
57
+ return element(
58
+ "g",
59
+ {
60
+ class: "clue",
61
+ "data-direction": clue.direction,
62
+ "data-index": clue.index,
63
+ "data-kind": clue.kind,
64
+ "data-value": clue.value,
65
+ },
66
+ element("rect", {
67
+ class: "clue-square",
68
+ x: x + margin + stroke / 2,
69
+ y: y + margin + stroke / 2,
70
+ width: side - stroke,
71
+ height: side - stroke,
72
+ rx: size * corner,
73
+ fill: covered ? ink : "#fff",
74
+ ...(covered ? {} : { stroke: ink, "stroke-width": stroke }),
75
+ }) +
76
+ element("path", {
77
+ class: "clue-number",
78
+ d: numberPath(
79
+ clue.value,
80
+ x + size / 2,
81
+ y + size / 2,
82
+ size * digitHeight,
83
+ size * digitWidth
84
+ ),
85
+ fill: covered ? "#fff" : ink,
86
+ })
87
+ );
88
+ })
89
+ .join("")
90
+ );
91
+ }
@@ -0,0 +1,93 @@
1
+ Copyright 2011 The Roboto Project Authors (https://github.com/googlefonts/roboto-classic)
2
+
3
+ This Font Software is licensed under the SIL Open Font License, Version 1.1.
4
+ This license is copied below, and is also available with a FAQ at:
5
+ https://openfontlicense.org
6
+
7
+
8
+ -----------------------------------------------------------
9
+ SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
10
+ -----------------------------------------------------------
11
+
12
+ PREAMBLE
13
+ The goals of the Open Font License (OFL) are to stimulate worldwide
14
+ development of collaborative font projects, to support the font creation
15
+ efforts of academic and linguistic communities, and to provide a free and
16
+ open framework in which fonts may be shared and improved in partnership
17
+ with others.
18
+
19
+ The OFL allows the licensed fonts to be used, studied, modified and
20
+ redistributed freely as long as they are not sold by themselves. The
21
+ fonts, including any derivative works, can be bundled, embedded,
22
+ redistributed and/or sold with any software provided that any reserved
23
+ names are not used by derivative works. The fonts and derivatives,
24
+ however, cannot be released under any other type of license. The
25
+ requirement for fonts to remain under this license does not apply
26
+ to any document created using the fonts or their derivatives.
27
+
28
+ DEFINITIONS
29
+ "Font Software" refers to the set of files released by the Copyright
30
+ Holder(s) under this license and clearly marked as such. This may
31
+ include source files, build scripts and documentation.
32
+
33
+ "Reserved Font Name" refers to any names specified as such after the
34
+ copyright statement(s).
35
+
36
+ "Original Version" refers to the collection of Font Software components as
37
+ distributed by the Copyright Holder(s).
38
+
39
+ "Modified Version" refers to any derivative made by adding to, deleting,
40
+ or substituting -- in part or in whole -- any of the components of the
41
+ Original Version, by changing formats or by porting the Font Software to a
42
+ new environment.
43
+
44
+ "Author" refers to any designer, engineer, programmer, technical
45
+ writer or other person who contributed to the Font Software.
46
+
47
+ PERMISSION & CONDITIONS
48
+ Permission is hereby granted, free of charge, to any person obtaining
49
+ a copy of the Font Software, to use, study, copy, merge, embed, modify,
50
+ redistribute, and sell modified and unmodified copies of the Font
51
+ Software, subject to the following conditions:
52
+
53
+ 1) Neither the Font Software nor any of its individual components,
54
+ in Original or Modified Versions, may be sold by itself.
55
+
56
+ 2) Original or Modified Versions of the Font Software may be bundled,
57
+ redistributed and/or sold with any software, provided that each copy
58
+ contains the above copyright notice and this license. These can be
59
+ included either as stand-alone text files, human-readable headers or
60
+ in the appropriate machine-readable metadata fields within text or
61
+ binary files as long as those fields can be easily viewed by the user.
62
+
63
+ 3) No Modified Version of the Font Software may use the Reserved Font
64
+ Name(s) unless explicit written permission is granted by the corresponding
65
+ Copyright Holder. This restriction only applies to the primary font name as
66
+ presented to the users.
67
+
68
+ 4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
69
+ Software shall not be used to promote, endorse or advertise any
70
+ Modified Version, except to acknowledge the contribution(s) of the
71
+ Copyright Holder(s) and the Author(s) or with their explicit written
72
+ permission.
73
+
74
+ 5) The Font Software, modified or unmodified, in part or in whole,
75
+ must be distributed entirely under this license, and must not be
76
+ distributed under any other license. The requirement for fonts to
77
+ remain under this license does not apply to any document created
78
+ using the Font Software.
79
+
80
+ TERMINATION
81
+ This license becomes null and void if any of the above conditions are
82
+ not met.
83
+
84
+ DISCLAIMER
85
+ THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
86
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
87
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
88
+ OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
89
+ COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
90
+ INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
91
+ DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
92
+ FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
93
+ OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,5 @@
1
+ # The digits
2
+
3
+ A clue's number is drawn as a path, so the renderer's output has no text and needs no font. The outlines are the digits of Roboto Regular, version 3.014 (2025), copyright 2011 The Roboto Project Authors (https://github.com/googlefonts/roboto-classic), licensed under the SIL Open Font License, Version 1.1, in `OFL.txt` beside this file. It is the font the sheet embeds, so a clue matches the sheet's text.
4
+
5
+ `generated/digits.ts` holds each digit's outline and advance in font units. Regenerate it with `node scripts/digits.mjs` from the package's directory after a pdfmake upgrade: the script reads the file pdfmake ships, `build/fonts/Roboto/Roboto-Regular.ttf`.