@altnovatrail/canvas 0.3.34 → 0.3.36

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.
@@ -1,4 +1,5 @@
1
1
  import type { CropNumbers, Frame } from "@altnovatrail/spec/canvas";
2
+ import { type Box } from "./geometry.js";
2
3
  import type { Group } from "./groups.js";
3
4
  import type { Stroke } from "./ink.js";
4
5
  /**
@@ -67,9 +68,10 @@ export declare function medianOf(strokes: readonly Stroke[]): number;
67
68
  /**
68
69
  * The crop of the strokes on a sheet with its digits `digit` px high, and the frames of their
69
70
  * groups (R25): the strokes the sheet shows, and their groups by the sheet's rule (R24). A sheet
70
- * without strokes has no crop: it goes to no check (H3).
71
+ * without strokes has no crop: it goes to no check (H3). On a task from paper the crop takes in
72
+ * `also` too, the tracing paper's rectangle in whole tenths, at the same scale (R87).
71
73
  */
72
- export declare function cropOf(strokes: readonly Stroke[], groups: readonly Group[], digit?: number): Crop | null;
74
+ export declare function cropOf(strokes: readonly Stroke[], groups: readonly Group[], digit?: number, also?: Box): Crop | null;
73
75
  /**
74
76
  * Where a crop with its margins lies on the sheet, its `sheet_box` (R25): [ymin, xmin, ymax, xmax]
75
77
  * in units, each one division of two whole numbers, from the whole numbers of the crop. A
package/lib/core/crop.js CHANGED
@@ -53,9 +53,10 @@ export function medianOf(strokes) {
53
53
  /**
54
54
  * The crop of the strokes on a sheet with its digits `digit` px high, and the frames of their
55
55
  * groups (R25): the strokes the sheet shows, and their groups by the sheet's rule (R24). A sheet
56
- * without strokes has no crop: it goes to no check (H3).
56
+ * without strokes has no crop: it goes to no check (H3). On a task from paper the crop takes in
57
+ * `also` too, the tracing paper's rectangle in whole tenths, at the same scale (R87).
57
58
  */
58
- export function cropOf(strokes, groups, digit = CROP.digit) {
59
+ export function cropOf(strokes, groups, digit = CROP.digit, also) {
59
60
  if (!Number.isInteger(digit) || digit < 1 || digit > CROP.tallest) {
60
61
  throw new RangeError(`a digit ${digit} px high: a crop takes whole px from 1 to ${CROP.tallest}`);
61
62
  }
@@ -63,7 +64,7 @@ export function cropOf(strokes, groups, digit = CROP.digit) {
63
64
  const members = membersOf(groups, extents);
64
65
  if (extents.length === 0)
65
66
  return null;
66
- const box = around(extents);
67
+ const box = around(also === undefined ? extents : [...extents, also]);
67
68
  const median = medianTenths(extents);
68
69
  const unit = Math.min(median, CROP.ceiling * 10);
69
70
  let size = sizeOf(box, unit, digit);
@@ -20,11 +20,13 @@ export declare function replay(history: readonly HistoryEvent[]): Stroke[];
20
20
  * the four, at a time from 0 that never goes back along the history; a stroke must have a new ID, a
21
21
  * whole number from 1, and one point at least, each on the sheet, with a pressure from 0 to 1, its
22
22
  * times never going back and none after the time it landed. Times are checked for their order
23
- * alone: strokes may overlap in time, as several pointers at once would write them. Whether the
24
- * actions keep the rules of the stack, the work on the sheet checks as it replays them
25
- * (`Work.restore`).
23
+ * alone: strokes may overlap in time, as several pointers at once would write them. On a task from
24
+ * paper, `paper`, a stroke written on the tracing paper says so, `on: "tracing"`, its points from
25
+ * the paper's top left corner within the sheet's size; anywhere else `on` is a TypeError
26
+ * (R87). Whether the actions keep the rules of the stack, the work on the sheet checks as it
27
+ * replays them (`Work.restore`).
26
28
  */
27
- export declare function historyOf(value: unknown): HistoryEvent[];
29
+ export declare function historyOf(value: unknown, paper?: boolean): HistoryEvent[];
28
30
  /**
29
31
  * An event, frozen through: its stroke with its points, or its lists of IDs (R27). Whoever holds
30
32
  * an event the sheet handed out cannot change the sheet by it.
@@ -35,11 +35,13 @@ export function replay(history) {
35
35
  * the four, at a time from 0 that never goes back along the history; a stroke must have a new ID, a
36
36
  * whole number from 1, and one point at least, each on the sheet, with a pressure from 0 to 1, its
37
37
  * times never going back and none after the time it landed. Times are checked for their order
38
- * alone: strokes may overlap in time, as several pointers at once would write them. Whether the
39
- * actions keep the rules of the stack, the work on the sheet checks as it replays them
40
- * (`Work.restore`).
38
+ * alone: strokes may overlap in time, as several pointers at once would write them. On a task from
39
+ * paper, `paper`, a stroke written on the tracing paper says so, `on: "tracing"`, its points from
40
+ * the paper's top left corner within the sheet's size; anywhere else `on` is a TypeError
41
+ * (R87). Whether the actions keep the rules of the stack, the work on the sheet checks as it
42
+ * replays them (`Work.restore`).
41
43
  */
42
- export function historyOf(value) {
44
+ export function historyOf(value, paper = false) {
43
45
  if (!Array.isArray(value))
44
46
  throw new TypeError(`history: a list of events, not ${shown(value)}`);
45
47
  const ids = new Set();
@@ -64,7 +66,12 @@ export function historyOf(value) {
64
66
  if (t < end) {
65
67
  throw new TypeError(`${path}.t: ${t}, earlier than the stroke's last point, at ${end}`);
66
68
  }
67
- return frozen({ kind: "write", t, stroke });
69
+ if (fields.on === undefined)
70
+ return frozen({ kind: "write", t, stroke });
71
+ if (fields.on !== "tracing" || !paper) {
72
+ throw new TypeError(`${path}.on: ${paper ? '"tracing", or none' : "none, on a sheet with no task from paper"}, not ${shown(fields.on)}`);
73
+ }
74
+ return frozen({ kind: "write", t, stroke, on: "tracing" });
68
75
  }
69
76
  case "erase":
70
77
  return frozen({ kind: "erase", t, ids: idsOf(fields.ids, `${path}.ids`) });
@@ -138,7 +145,7 @@ export function sameEvent(value, event) {
138
145
  return false;
139
146
  switch (event.kind) {
140
147
  case "write":
141
- return sameStroke(given.stroke, event.stroke);
148
+ return given.on === event.on && sameStroke(given.stroke, event.stroke);
142
149
  case "erase":
143
150
  case "clear":
144
151
  return sameIds(given.ids, event.ids);
@@ -7,6 +7,7 @@ export type { Group } from "./groups.js";
7
7
  export { type HistoryEvent, replay } from "./history.js";
8
8
  export type { InkPoint, Stroke } from "./ink.js";
9
9
  export type { Manifest } from "./manifest.js";
10
- export type { Anchor, CellPlace, CropNumbers, Mark, Marks, SheetSubmission, Submission, WorkbookSubmission, } from "./marks.js";
10
+ export type { Anchor, CellPlace, CropNumbers, Mark, Marks, PaperSubmission, SheetSubmission, Submission, WorkbookSubmission, } from "./marks.js";
11
11
  export { SHEET } from "./sheet.js";
12
12
  export type { Canvas, Task } from "./task.js";
13
+ export type { TracingState } from "./tracing.js";
package/lib/core/index.js CHANGED
@@ -3,10 +3,10 @@
3
3
  // too (R05, R07). Importing it registers no element: the element is the other entry point. It
4
4
  // gives the sheet's size, the types of the data the element takes and gives — the strokes and the
5
5
  // history of writing, the task, the worked example and the manifest, how far the learner has gone
6
- // through the example's demo (R42) and the context of a submission, of a sheet or of a workbook,
7
- // all of spec's types (R73 spec, R74 spec), and the tutor's marks, spec's types too (R83 spec) —
8
- // and the fold of a history into the strokes it leaves, so that a consumer
9
- // reads a history it keeps without the DOM (R27). The code runs without the DOM, but spec's
6
+ // through the example's demo (R42) and the context of a submission, of a sheet, a workbook or a
7
+ // task from paper with where its tracing paper lay (R87), all of spec's types (R73 spec, R74
8
+ // spec), and the tutor's marks, spec's types too (R83 spec) — and the fold of a history into the
9
+ // strokes it leaves, so that a consumer reads a history it keeps without the DOM (R27). The code runs without the DOM, but spec's
10
10
  // declarations of the element's contract, which its types come from, name the DOM's types too: a
11
11
  // consumer without the DOM's types skips them with skipLibCheck (R27). The rules of the groups, the
12
12
  // crop and the snapping stay inside: the eval protocol has taken them as they are, and spec's test
@@ -1,4 +1,4 @@
1
- import type { Anchor, CellPlace, CropNumbers, Filling, Mark, Marks, SheetSubmission, Submission, WorkbookSubmission } from "@altnovatrail/spec/canvas";
1
+ import type { Anchor, CellPlace, CropNumbers, Filling, Mark, Marks, PaperSubmission, SheetSubmission, Submission, WorkbookSubmission } from "@altnovatrail/spec/canvas";
2
2
  import type { Cell } from "./boxes.js";
3
3
  import { type Box } from "./geometry.js";
4
4
  import type { Group } from "./groups.js";
@@ -36,7 +36,9 @@ export declare const SNAP_RULE: SnapRule;
36
36
  * numbers its strokes from 1, so a sheet of other strokes with the same IDs tells it by the digest.
37
37
  * A workbook's holds the table «box → stroke IDs», each filled box by its place, in the task's
38
38
  * order, and the digest of its strokes and of every box of the page, `workbookDigestOf` it (R34,
39
- * R38): no numbers of the crops, for no mark on a workbook is snapped. An answer cell has no ID:
39
+ * R38): no numbers of the crops, for no mark on a workbook is snapped. A task from paper's holds
40
+ * what a sheet's does, its strokes where they lie on the sheet, and where the tracing paper lay,
41
+ * which its digest holds with the strokes written on it (R87). An answer cell has no ID:
40
42
  * its place names it, its item and its place among the item's cells, each from 1 (R51 spec).
41
43
  *
42
44
  * The tutor's marks of one submission, with the submission's context, are spec's types too, of the
@@ -44,20 +46,35 @@ export declare const SNAP_RULE: SnapRule;
44
46
  * reader gives it, in the consumer's words — the element words nothing. Where it is: a group of
45
47
  * strokes by its label, an answer cell by its place, or a box on the crop the model saw,
46
48
  * [ymin, xmin, ymax, xmax] in whole thousandths of its height and width, min < max (R53 spec). On a
47
- * workbook a mark stands on a cell alone (R34).
49
+ * workbook a mark stands on a cell alone (R34). On a task from paper a mark may be a verdict too, a
50
+ * tick or a cross at a box alone: the box of an answer its reading gave (R162 spec).
48
51
  */
49
- export type { Anchor, CellPlace, CropNumbers, Mark, Marks, SheetSubmission, Submission, WorkbookSubmission, };
52
+ export type { Anchor, CellPlace, CropNumbers, Mark, Marks, PaperSubmission, SheetSubmission, Submission, WorkbookSubmission, };
53
+ /**
54
+ * What a task from paper's sheet tells `placesOf` and `digestOf` (R87): where its tracing paper
55
+ * lies, and which strokes were written on it.
56
+ */
57
+ export interface Paper {
58
+ readonly at: readonly [number, number];
59
+ readonly onTracing: (id: number) => boolean;
60
+ }
50
61
  /**
51
62
  * A mark on the sheet, `{stroke_ids, snapped_box}` (C2): the strokes it holds on to, by ID in
52
63
  * ascending order, and the rectangle of their points, in units, as the model saw them. A mark of a
53
64
  * workbook's cell has the box of its cell too, which its frame stands round with the ink (R39). A
54
65
  * mark of a box the submission found empty holds no strokes, and its `box` is then the cell's own:
55
- * `shownOf` frames it alone.
66
+ * `shownOf` frames it alone. A verdict stands by the group of strokes `verdictsOf` gives it, its
67
+ * `box` theirs, or, with none, by the answer's box laid on the sheet by the crop's `sheetBox`: the
68
+ * answer may be ink on the photo of the page. `correct` says a tick or a cross (R162 spec). A wavy
69
+ * line holds on to the strokes its box takes, as a frame does, and with none stands under the box
70
+ * laid on the sheet, the place of the mistake; `wavy` says it is one (R87).
56
71
  */
57
72
  export interface Placed {
58
73
  readonly ids: readonly number[];
59
74
  readonly box: Box;
60
75
  readonly cell?: Box;
76
+ readonly correct?: boolean;
77
+ readonly wavy?: true;
61
78
  }
62
79
  /**
63
80
  * The tutor's marks a value gives, checked (R26). A value that is not the marks of a submission is
@@ -88,7 +105,7 @@ export declare function snapOf(box: readonly [number, number, number, number], c
88
105
  * `cells` are the boxes of a workbook's page, in the task's order, and null on a sheet: there a
89
106
  * cell's mark holds the strokes of its row of the table, with its box; one of a box not in the
90
107
  * table, an empty one, holds its box alone, and one of a box not on the page holds none (R39;
91
- * R74 spec).
108
+ * R74 spec). A verdict of a task from paper always has its place, its box on the sheet (R162 spec).
92
109
  *
93
110
  * A submission of other strokes is a TypeError: a stroke of its groups the sheet does not know, or
94
111
  * a digest the sheet's strokes of those IDs do not give with the submission's numbers (R27). A
@@ -97,12 +114,15 @@ export declare function snapOf(box: readonly [number, number, number, number], c
97
114
  * submission always passes, the strokes erased since too, and so does the same history given to
98
115
  * another sheet. A workbook's digest holds every box of its page too, so a page of another layout
99
116
  * is not its submission's (R38), and a context of the other kind of canvas is a TypeError.
117
+ * On a task from paper, `paper`, `known` are the strokes where they lie on the sheet now, and a
118
+ * context of the tracing paper at another place is a TypeError: its strokes lie elsewhere (R87).
100
119
  */
101
- export declare function placesOf(marks: Marks, known: readonly Stroke[], cells?: readonly Cell[] | null, rule?: SnapRule): (Placed | null)[];
120
+ export declare function placesOf(marks: Marks, known: readonly Stroke[], cells?: readonly Cell[] | null, rule?: SnapRule, paper?: Paper): (Placed | null)[];
102
121
  /**
103
122
  * The rectangle a mark shows now (R26): around the points of those of its strokes the sheet shows,
104
123
  * in units, or null while it shows none of them. A mark of a box that was empty shows the box
105
- * itself, always (R39). `shown` gives the box of a stroke's points by its ID while the stroke is
124
+ * itself, always (R39), a verdict its box, and a wavy line of no strokes its box (R162 spec).
125
+ * `shown` gives the box of a stroke's points by its ID while the stroke is
106
126
  * on the screen — not erased, nor under the eraser under way (R22). New strokes are none of a
107
127
  * mark's.
108
128
  */
@@ -116,9 +136,10 @@ export declare function shownOf(placed: Placed, shown: (id: number) => Box | und
116
136
  * within its slack. The same strokes and numbers give the same digest in every engine, through any
117
137
  * store that keeps them as JSON does. The digest tells an accidental difference, not a forged one:
118
138
  * CRC-32 tells every change within 32 bits running, and any other change keeps both sums with a
119
- * chance of about one in 2^32 at most.
139
+ * chance of about one in 2^32 at most. A task from paper's, `paper`, holds a last line: where the
140
+ * tracing paper lies, in tenths, and the IDs of the strokes written on it, ascending (R87).
120
141
  */
121
- export declare function digestOf(strokes: readonly Stroke[], crop: Pick<CropNumbers, "drawn" | "width" | "height" | "margins" | "frames" | "exact">, groups: readonly Group[]): string;
142
+ export declare function digestOf(strokes: readonly Stroke[], crop: Pick<CropNumbers, "drawn" | "width" | "height" | "margins" | "frames" | "exact">, groups: readonly Group[], paper?: Paper): string;
122
143
  /**
123
144
  * The digest of a workbook's submission (R38), as `digestOf` makes a sheet's: of a text that holds
124
145
  * the strokes of its table, by ID ascending, as a sheet's digest has them, then every box of the
package/lib/core/marks.js CHANGED
@@ -3,6 +3,7 @@ import { record, shown, whole } from "./fields.js";
3
3
  import { around, boxOf, tenths } from "./geometry.js";
4
4
  import { adler32, crc32 } from "./png.js";
5
5
  import { SHEET } from "./sheet.js";
6
+ import { tracingFieldsOf } from "./tracing.js";
6
7
  /**
7
8
  * The rule the sheet snaps by (R26): a quarter of a digit — 6 px at 24, a quarter of H on the
8
9
  * sheet, H the height the crop scales by — and two thirds of the points. A sweep over the
@@ -38,9 +39,10 @@ export function marksOf(value) {
38
39
  }
39
40
  // What a mark may stand on: a group of a sheet's, or on a workbook a cell alone.
40
41
  const labels = "groups" in submission ? new Set(submission.groups.map(({ label }) => label)) : null;
42
+ const paper = "tracing" in submission && submission.tracing !== undefined;
41
43
  return {
42
44
  submission,
43
- marks: fields.marks.map((mark, n) => markOf(mark, `marks.marks[${n}]`, labels)),
45
+ marks: fields.marks.map((mark, n) => markOf(mark, `marks.marks[${n}]`, labels, paper)),
44
46
  };
45
47
  }
46
48
  /**
@@ -76,7 +78,7 @@ export function snapOf(box, crop, strokes, rule = SNAP_RULE) {
76
78
  * `cells` are the boxes of a workbook's page, in the task's order, and null on a sheet: there a
77
79
  * cell's mark holds the strokes of its row of the table, with its box; one of a box not in the
78
80
  * table, an empty one, holds its box alone, and one of a box not on the page holds none (R39;
79
- * R74 spec).
81
+ * R74 spec). A verdict of a task from paper always has its place, its box on the sheet (R162 spec).
80
82
  *
81
83
  * A submission of other strokes is a TypeError: a stroke of its groups the sheet does not know, or
82
84
  * a digest the sheet's strokes of those IDs do not give with the submission's numbers (R27). A
@@ -85,8 +87,10 @@ export function snapOf(box, crop, strokes, rule = SNAP_RULE) {
85
87
  * submission always passes, the strokes erased since too, and so does the same history given to
86
88
  * another sheet. A workbook's digest holds every box of its page too, so a page of another layout
87
89
  * is not its submission's (R38), and a context of the other kind of canvas is a TypeError.
90
+ * On a task from paper, `paper`, `known` are the strokes where they lie on the sheet now, and a
91
+ * context of the tracing paper at another place is a TypeError: its strokes lie elsewhere (R87).
88
92
  */
89
- export function placesOf(marks, known, cells = null, rule = SNAP_RULE) {
93
+ export function placesOf(marks, known, cells = null, rule = SNAP_RULE, paper) {
90
94
  checkRule(rule);
91
95
  const { submission } = marks;
92
96
  const byId = new Map(known.map((stroke) => [stroke.id, stroke]));
@@ -97,6 +101,18 @@ export function placesOf(marks, known, cells = null, rule = SNAP_RULE) {
97
101
  }
98
102
  if (cells !== null)
99
103
  throw new TypeError(`marks.submission: ${otherKind("a sheet's", "a workbook")}`);
104
+ const traced = "tracing" in submission ? submission.tracing : undefined;
105
+ if (traced !== undefined && paper === undefined) {
106
+ throw new TypeError(`marks.submission: ${otherKind("a task from paper's", "a sheet")}`);
107
+ }
108
+ if (traced === undefined && paper !== undefined) {
109
+ throw new TypeError(`marks.submission: ${otherKind("a sheet's", "a task from paper")}`);
110
+ }
111
+ if (traced !== undefined && paper !== undefined) {
112
+ if (traced.at[0] !== paper.at[0] || traced.at[1] !== paper.at[1]) {
113
+ throw new TypeError(`marks.submission.tracing.at: ${shown(traced.at)}, and the tracing paper lies at ${shown(paper.at)}: the submission's strokes lie elsewhere`);
114
+ }
115
+ }
100
116
  const { crop, groups, digest } = submission;
101
117
  const sent = [];
102
118
  for (const [n, { label, ids }] of groups.entries()) {
@@ -106,12 +122,23 @@ export function placesOf(marks, known, cells = null, rule = SNAP_RULE) {
106
122
  }
107
123
  sent.push(...knownOf(ids, byId, `the group ${label}`, "sheet"));
108
124
  }
109
- if (digestOf(sent, crop, groups) !== digest) {
125
+ if (digestOf(sent, crop, groups, paper) !== digest) {
110
126
  throw new TypeError("marks.submission.digest: the sheet's strokes of its IDs give another with its numbers: " +
111
127
  "the submission is not this sheet's, or it was changed");
112
128
  }
113
129
  const tables = new Map(groups.map(({ label, ids }) => [label, ids]));
114
- return marks.marks.map(({ at }) => {
130
+ const verdicts = verdictsOf(marks.marks, crop, groups, byId);
131
+ return marks.marks.map((mark, n) => {
132
+ const { at } = mark;
133
+ if (mark.kind === "verdict")
134
+ return verdicts.get(n) ?? null;
135
+ if (mark.kind === "wavy_line") {
136
+ const ids = snapOf(mark.at.box, crop, sent, rule);
137
+ const box = ids.length === 0
138
+ ? onSheetOf(mark.at.box, crop)
139
+ : boxOf(ids.flatMap((id) => byId.get(id)?.points ?? []));
140
+ return { ids, box, wavy: true };
141
+ }
115
142
  let ids = [];
116
143
  if ("group" in at)
117
144
  ids = [...(tables.get(at.group) ?? [])].sort(byNumber);
@@ -125,11 +152,14 @@ export function placesOf(marks, known, cells = null, rule = SNAP_RULE) {
125
152
  /**
126
153
  * The rectangle a mark shows now (R26): around the points of those of its strokes the sheet shows,
127
154
  * in units, or null while it shows none of them. A mark of a box that was empty shows the box
128
- * itself, always (R39). `shown` gives the box of a stroke's points by its ID while the stroke is
155
+ * itself, always (R39), a verdict its box, and a wavy line of no strokes its box (R162 spec).
156
+ * `shown` gives the box of a stroke's points by its ID while the stroke is
129
157
  * on the screen — not erased, nor under the eraser under way (R22). New strokes are none of a
130
158
  * mark's.
131
159
  */
132
160
  export function shownOf(placed, shown) {
161
+ if (placed.correct !== undefined || (placed.wavy && placed.ids.length === 0))
162
+ return placed.box;
133
163
  // A mark of a box that was empty stands round the box alone, whatever is written in it since.
134
164
  if (placed.cell !== undefined && placed.ids.length === 0)
135
165
  return placed.cell;
@@ -199,9 +229,10 @@ function cellsPlacesOf(marks, { boxes, digest }, byId, cells) {
199
229
  * within its slack. The same strokes and numbers give the same digest in every engine, through any
200
230
  * store that keeps them as JSON does. The digest tells an accidental difference, not a forged one:
201
231
  * CRC-32 tells every change within 32 bits running, and any other change keeps both sums with a
202
- * chance of about one in 2^32 at most.
232
+ * chance of about one in 2^32 at most. A task from paper's, `paper`, holds a last line: where the
233
+ * tracing paper lies, in tenths, and the IDs of the strokes written on it, ascending (R87).
203
234
  */
204
- export function digestOf(strokes, crop, groups) {
235
+ export function digestOf(strokes, crop, groups, paper) {
205
236
  const lines = strokeLines(strokes);
206
237
  const { drawn, width, height, margins: [top, left], exact: { box, median }, } = crop;
207
238
  lines.push(`crop ${drawn} ${width} ${height} ${top} ${left} ${box.minX} ${box.minY} ${box.maxX} ${box.maxY} ${median}`);
@@ -209,6 +240,13 @@ export function digestOf(strokes, crop, groups) {
209
240
  lines.push(`group ${label} ${ids.join(",")}`);
210
241
  for (const { label, box: frame } of crop.frames)
211
242
  lines.push(`frame ${label} ${frame.join(",")}`);
243
+ if (paper !== undefined) {
244
+ const traced = strokes
245
+ .map(({ id }) => id)
246
+ .filter((id) => paper.onTracing(id))
247
+ .sort(byNumber);
248
+ lines.push(`tracing ${tenths(paper.at[0])} ${tenths(paper.at[1])} ${traced.join(",")}`);
249
+ }
212
250
  return sumOf(lines);
213
251
  }
214
252
  /**
@@ -238,6 +276,65 @@ function sumOf(lines) {
238
276
  const bytes = utf8(lines.join("\n"));
239
277
  return hex(crc32(bytes)) + hex(adler32(bytes));
240
278
  }
279
+ /**
280
+ * Where each verdict of the marks stands, by its index: by a group of the submission's strokes, one
281
+ * group a verdict, or by its box alone. A model's box of where it read an answer may be off by half
282
+ * a line, and the learner's strokes are where the answer is: the pairs of a verdict and a group
283
+ * whose boxes on the sheet are nearest take each other first, the gap between the boxes before the
284
+ * distance of their centres, and a group farther than the taller of the two boxes is none of the
285
+ * verdict's.
286
+ */
287
+ function verdictsOf(marks, crop, groups, byId) {
288
+ const boxes = new Map();
289
+ for (const [n, mark] of marks.entries()) {
290
+ if (mark.kind === "verdict")
291
+ boxes.set(n, onSheetOf(mark.at.box, crop));
292
+ }
293
+ const inked = groups.map(({ ids }) => {
294
+ const own = [...ids].sort(byNumber);
295
+ return { ids: own, box: boxOf(own.flatMap((id) => byId.get(id)?.points ?? [])) };
296
+ });
297
+ const pairs = [];
298
+ for (const [n, box] of boxes) {
299
+ for (const [g, group] of inked.entries()) {
300
+ const gap = Math.hypot(Math.max(0, box.minX - group.box.maxX, group.box.minX - box.maxX), Math.max(0, box.minY - group.box.maxY, group.box.minY - box.maxY));
301
+ const reach = Math.max(box.maxY - box.minY, group.box.maxY - group.box.minY);
302
+ if (gap > reach)
303
+ continue;
304
+ const apart = Math.hypot((box.minX + box.maxX - group.box.minX - group.box.maxX) / 2, (box.minY + box.maxY - group.box.minY - group.box.maxY) / 2);
305
+ pairs.push({ n, g, box: group.box, gap, apart });
306
+ }
307
+ }
308
+ pairs.sort((a, b) => a.gap - b.gap || a.apart - b.apart);
309
+ const placed = new Map();
310
+ const taken = new Set();
311
+ for (const { n, g, box } of pairs) {
312
+ if (placed.has(n) || taken.has(g))
313
+ continue;
314
+ taken.add(g);
315
+ placed.set(n, { ids: [], box, correct: verdictOf(marks[n]) });
316
+ }
317
+ for (const [n, box] of boxes) {
318
+ if (!placed.has(n))
319
+ placed.set(n, { ids: [], box, correct: verdictOf(marks[n]) });
320
+ }
321
+ return placed;
322
+ }
323
+ /** Whether a verdict says right. */
324
+ function verdictOf(mark) {
325
+ return mark?.kind === "verdict" && mark.correct;
326
+ }
327
+ /**
328
+ * A box on the crop, in thousandths of its height and width, laid on the sheet by the crop's
329
+ * `sheetBox`, in units: where the model saw it.
330
+ */
331
+ function onSheetOf(box, crop) {
332
+ const [top, left, bottom, right] = crop.sheetBox;
333
+ const [ymin, xmin, ymax, xmax] = box;
334
+ const x = (n) => left + ((right - left) * n) / 1000;
335
+ const y = (n) => top + ((bottom - top) * n) / 1000;
336
+ return { minX: x(xmin), minY: y(ymin), maxX: x(xmax), maxY: y(ymax) };
337
+ }
241
338
  /**
242
339
  * Whether a point lies in a box of the crop widened by `margin` thousandths of D′, its edges too:
243
340
  * in whole numbers, a place on the crop times 1000 × H (R25). For a stroke of the submission every
@@ -272,13 +369,16 @@ function checkRule({ margin, share }) {
272
369
  throw new RangeError(`the snapping rule's share: a fraction of whole numbers no more than 1, below from 1 to 1000, not ${shown(share)}`);
273
370
  }
274
371
  }
275
- /** A submission's context, a sheet's or a workbook's by its keys (R38). */
372
+ /** A submission's context, a sheet's, a workbook's or a task from paper's by its keys (R38, R87). */
276
373
  function submissionOf(value, path) {
277
374
  const fields = record(value, path);
278
375
  let parts;
279
376
  if (absent(fields.boxes)) {
280
377
  const groups = groupTableOf(fields.groups, `${path}.groups`);
281
- parts = { crop: numbersOf(fields.crop, `${path}.crop`, groups), groups };
378
+ const crop = numbersOf(fields.crop, `${path}.crop`, groups);
379
+ parts = absent(fields.tracing)
380
+ ? { crop, groups }
381
+ : { crop, groups, tracing: tracingFieldsOf(fields.tracing, `${path}.tracing`) };
282
382
  }
283
383
  else if (absent(fields.crop) && absent(fields.groups)) {
284
384
  parts = { boxes: rowsOf(fields.boxes, `${path}.boxes`) };
@@ -396,15 +496,32 @@ function numbersOf(value, path, groups) {
396
496
  }),
397
497
  };
398
498
  }
399
- function markOf(value, path, labels) {
499
+ /**
500
+ * A mark checked: a frame, or on a task from paper, `paper`, a verdict too, at a box alone and with
501
+ * whether the answer is right, and a wavy line, at a box alone (R162 spec).
502
+ */
503
+ function markOf(value, path, labels, paper) {
400
504
  const mark = record(value, path);
401
- if (mark.kind !== "frame") {
402
- throw new TypeError(`${path}.kind: the sheet draws "frame" marks, not ${shown(mark.kind)}`);
505
+ const kinds = paper ? '"frame", "verdict" and "wavy_line" marks' : '"frame" marks';
506
+ if (mark.kind !== "frame" && !(paper && (mark.kind === "verdict" || mark.kind === "wavy_line"))) {
507
+ throw new TypeError(`${path}.kind: the sheet draws ${kinds}, not ${shown(mark.kind)}`);
403
508
  }
404
509
  if (typeof mark.name !== "string" || mark.name.trim() === "") {
405
510
  throw new TypeError(`${path}.name: what a screen reader names the mark, not ${shown(mark.name)}`);
406
511
  }
407
- return { kind: "frame", at: anchorOf(mark.at, `${path}.at`, labels), name: mark.name };
512
+ const at = anchorOf(mark.at, `${path}.at`, labels);
513
+ if (mark.kind === "frame")
514
+ return { kind: "frame", at, name: mark.name };
515
+ if (!("box" in at)) {
516
+ const which = mark.kind === "verdict" ? "a verdict stands" : "a wavy line stands";
517
+ throw new TypeError(`${path}.at: ${which} at a box alone, not ${shown(mark.at)}`);
518
+ }
519
+ if (mark.kind === "wavy_line")
520
+ return { kind: "wavy_line", at, name: mark.name };
521
+ if (typeof mark.correct !== "boolean") {
522
+ throw new TypeError(`${path}.correct: whether the answer is right, true or false, not ${shown(mark.correct)}`);
523
+ }
524
+ return { kind: "verdict", at, correct: mark.correct, name: mark.name };
408
525
  }
409
526
  /** Where a mark is: on a sheet, `labels` its groups'; on a workbook, null, a cell alone (R39). */
410
527
  function anchorOf(value, path, labels) {
@@ -1,11 +1,21 @@
1
1
  import { type Cell } from "./boxes.js";
2
+ import { type Box } from "./geometry.js";
2
3
  import type { Stroke } from "./ink.js";
3
- import { type SheetSubmission, type WorkbookSubmission } from "./marks.js";
4
+ import { type Paper, type PaperSubmission, type SheetSubmission, type WorkbookSubmission } from "./marks.js";
5
+ import { type Gray, type TracingState } from "./tracing.js";
4
6
  /** The work of a sheet for checking (R27): the PNG of its crop, and the context of the submission. */
5
7
  export interface WorkForCheck {
6
8
  readonly png: Uint8Array<ArrayBuffer>;
7
9
  readonly submission: SheetSubmission;
8
10
  }
11
+ /**
12
+ * The work of a task from paper for checking (R87): the PNG of the crop of its work, and the
13
+ * context of the submission.
14
+ */
15
+ export interface PaperForCheck {
16
+ readonly png: Uint8Array<ArrayBuffer>;
17
+ readonly submission: PaperSubmission;
18
+ }
9
19
  /** The crop of one filled box of a workbook (R38): its item and its slot, from 1, and its PNG. */
10
20
  export interface CellForCheck {
11
21
  readonly item: number;
@@ -39,3 +49,15 @@ export declare function workForCheck(strokes: readonly Stroke[], digit: number):
39
49
  * `croppedOf` of the table's strokes with the table's rows for groups (R38).
40
50
  */
41
51
  export declare function cellsForCheck(strokes: readonly Stroke[], cells: readonly Cell[], digit: number): CellsForCheck | null;
52
+ /**
53
+ * The work of a task from paper for checking, as `exportWork()` gives it with the task's crop
54
+ * (R87): of the strokes the sheet and the tracing paper hold, where they lie on the sheet, their
55
+ * groups by the sheet's rule (R24), and their crop by R25 with its digits `digit` px high, taking
56
+ * in the tracing paper's rectangle `rect`, in units, at the same scale — the crop's picture,
57
+ * `picture`, laid over it there as the screen lays it — and the context: the crop's numbers, the
58
+ * groups, where the tracing paper lay, `tracing`, and the digest of it all, which holds the
59
+ * strokes written on the tracing paper, `paper`. Hidden or shown, the tracing paper is in the
60
+ * crop. No stroke on either is no work to check: null (H3). The picture not read yet, or not at
61
+ * all, `null`, leaves the paper white.
62
+ */
63
+ export declare function paperWorkForCheck(strokes: readonly Stroke[], paper: Paper, tracing: TracingState, rect: Box, picture: Gray | null, digit: number): PaperForCheck | null;
@@ -1,8 +1,11 @@
1
1
  import { tableOf } from "./boxes.js";
2
2
  import { cropOf } from "./crop.js";
3
+ import { tenths } from "./geometry.js";
3
4
  import { groupsOf } from "./groups.js";
4
5
  import { digestOf, workbookDigestOf, } from "./marks.js";
5
- import { pictureOf } from "./raster.js";
6
+ import { encodePNG } from "./png.js";
7
+ import { pictureOf, rasterOf } from "./raster.js";
8
+ import { layOver, tenthsOf } from "./tracing.js";
6
9
  /**
7
10
  * The work of a sheet for checking, as `exportWork()` gives it (R07, R27): of the strokes the sheet
8
11
  * shows, their groups by the sheet's rule (R24), their crop with its digits `digit` px high and its
@@ -47,6 +50,51 @@ export function cellsForCheck(strokes, cells, digit) {
47
50
  const submission = { boxes, digest: workbookDigestOf(sent, boxes, cells) };
48
51
  return Object.freeze({ cells: Object.freeze(crops), submission: frozen(submission) });
49
52
  }
53
+ /**
54
+ * The work of a task from paper for checking, as `exportWork()` gives it with the task's crop
55
+ * (R87): of the strokes the sheet and the tracing paper hold, where they lie on the sheet, their
56
+ * groups by the sheet's rule (R24), and their crop by R25 with its digits `digit` px high, taking
57
+ * in the tracing paper's rectangle `rect`, in units, at the same scale — the crop's picture,
58
+ * `picture`, laid over it there as the screen lays it — and the context: the crop's numbers, the
59
+ * groups, where the tracing paper lay, `tracing`, and the digest of it all, which holds the
60
+ * strokes written on the tracing paper, `paper`. Hidden or shown, the tracing paper is in the
61
+ * crop. No stroke on either is no work to check: null (H3). The picture not read yet, or not at
62
+ * all, `null`, leaves the paper white.
63
+ */
64
+ export function paperWorkForCheck(strokes, paper, tracing, rect, picture, digit) {
65
+ const groups = groupsOf(strokes);
66
+ const crop = cropOf(strokes, groups, digit, tenthsOf(rect));
67
+ if (crop === null)
68
+ return null;
69
+ const ink = rasterOf(strokes, crop);
70
+ if (picture !== null)
71
+ layOver(ink, crop.width, picture, pixelsOf(rect, crop));
72
+ const { drawn, width, height, margins, sheetBox, frames, exact } = crop;
73
+ const numbers = { drawn, width, height, margins, sheetBox, frames, exact };
74
+ const submission = {
75
+ crop: numbers,
76
+ groups,
77
+ tracing: { at: [tracing.at[0], tracing.at[1]], hidden: tracing.hidden },
78
+ digest: digestOf(strokes, numbers, groups, paper),
79
+ };
80
+ return Object.freeze({
81
+ png: encodePNG(ink, crop.width, crop.height),
82
+ submission: frozen(submission),
83
+ });
84
+ }
85
+ /** Where a rectangle of the sheet, in units, lies on a crop, in px, as its raster places points. */
86
+ function pixelsOf(rect, crop) {
87
+ const [top, left] = crop.margins;
88
+ const { box, median: unit } = crop.exact;
89
+ const across = (x) => ((tenths(x) - box.minX) * crop.drawn + left * unit) / unit;
90
+ const down = (y) => ((tenths(y) - box.minY) * crop.drawn + top * unit) / unit;
91
+ return {
92
+ left: across(rect.minX),
93
+ top: down(rect.minY),
94
+ right: across(rect.maxX),
95
+ bottom: down(rect.maxY),
96
+ };
97
+ }
50
98
  /** The crop of strokes in their groups, its digits `digit` px high, and its PNG (R25). */
51
99
  function croppedOf(strokes, groups, digit) {
52
100
  const crop = cropOf(strokes, groups, digit);