@altnovatrail/canvas 0.3.38 → 0.3.39

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.
@@ -81,6 +81,11 @@ export interface HandsSheet {
81
81
  * for negative ones: the sheet goes with them. `fresh` marks a gesture's first step.
82
82
  */
83
83
  scroll(dx: number, dy: number, fresh: boolean): void;
84
+ /**
85
+ * Two fingers drew apart, or together, by `ratio` of the span between them: the sheet's zoom
86
+ * goes by it, the point of the sheet at `x` and `y` on the screen, their middle, standing (R73).
87
+ */
88
+ zoom(ratio: number, x: number, y: number): void;
84
89
  /**
85
90
  * Whether the sheet scrolls across at all now: one wider than its container does (R73). A
86
91
  * gesture on a sheet that does not goes by its travel down alone.
@@ -115,6 +120,8 @@ export type Role = "writes" | "waits" | "scrolls" | "palm" | "idle";
115
120
  * - Two fingers scroll by the travel of their middle on the screen, and one by its own, past its
116
121
  * first `SLOP_PX`: along one axis alone if it went `RAIL` times further along it than along the
117
122
  * other, or else both.
123
+ * - Two fingers whose span has changed by more than `SLOP_PX` pinch too: the zoom goes by the
124
+ * span's ratio from there, about their middle (R73).
118
125
  * - Any other pointer, a mouse or one of no type, writes when nothing does, whatever the mode.
119
126
  */
120
127
  export declare class Hands {
package/lib/core/hands.js CHANGED
@@ -96,6 +96,8 @@ export class PenMode {
96
96
  * - Two fingers scroll by the travel of their middle on the screen, and one by its own, past its
97
97
  * first `SLOP_PX`: along one axis alone if it went `RAIL` times further along it than along the
98
98
  * other, or else both.
99
+ * - Two fingers whose span has changed by more than `SLOP_PX` pinch too: the zoom goes by the
100
+ * span's ratio from there, about their middle (R73).
99
101
  * - Any other pointer, a mouse or one of no type, writes when nothing does, whatever the mode.
100
102
  */
101
103
  export class Hands {
@@ -163,10 +165,13 @@ export class Hands {
163
165
  last: undefined,
164
166
  across: true,
165
167
  down: true,
168
+ span0: 0,
169
+ span: undefined,
170
+ unpaired: undefined,
166
171
  };
167
172
  }
168
173
  if (hand.role === "scrolls")
169
- this.#step();
174
+ this.#step(contact.pointerId);
170
175
  return hand.role === "writes" && this.#sheet.writer() === contact.pointerId;
171
176
  }
172
177
  /** `pointerup`, `pointercancel` or a lost capture: the pointer has gone, and its gesture too. */
@@ -265,6 +270,9 @@ export class Hands {
265
270
  last: undefined,
266
271
  across: true,
267
272
  down: true,
273
+ span0: spanOf(one, two),
274
+ span: undefined,
275
+ unpaired: undefined,
268
276
  };
269
277
  }
270
278
  /** The gesture ends: its fingers do nothing until they lift. */
@@ -283,7 +291,7 @@ export class Hands {
283
291
  * The two fingers' middle has moved, or the one finger: past the slop, the sheet goes with it,
284
292
  * along the axes the gesture chose as it passed it.
285
293
  */
286
- #step() {
294
+ #step(pointerId) {
287
295
  const gesture = this.#gesture;
288
296
  if (gesture === undefined)
289
297
  return;
@@ -291,7 +299,17 @@ export class Hands {
291
299
  const two = gesture.fingers.length === 2 ? this.#hands.get(gesture.fingers[1]) : one;
292
300
  if (one === undefined || two === undefined)
293
301
  return;
302
+ // Two fingers are read once both have moved: between their moves the middle stands half a
303
+ // step aside and the span is off, which would scroll a pinch off the point under it.
304
+ if (one !== two && !paired(gesture, pointerId))
305
+ return;
294
306
  const middle = middleOf(one, two);
307
+ this.#move(gesture, middle);
308
+ if (one !== two)
309
+ this.#pinch(gesture, spanOf(one, two), middle);
310
+ }
311
+ /** The sheet goes with the fingers' middle, past the slop, along the axes the gesture chose. */
312
+ #move(gesture, middle) {
295
313
  if (gesture.last === undefined) {
296
314
  // A sheet that does not scroll across counts the travel down alone.
297
315
  const dx = this.#sheet.across() ? middle.x - gesture.from.x : 0;
@@ -314,6 +332,39 @@ export class Hands {
314
332
  if (dx !== 0 || dy !== 0)
315
333
  this.#sheet.scroll(dx, dy, false);
316
334
  }
335
+ /**
336
+ * The two fingers' span has changed: past the slop, the zoom goes by its ratio to the last step's,
337
+ * from the slop on, without a jump, about their middle (R73).
338
+ */
339
+ #pinch(gesture, span, middle) {
340
+ if (gesture.span === undefined) {
341
+ const change = span - gesture.span0;
342
+ if (Math.abs(change) <= SLOP_PX)
343
+ return;
344
+ gesture.span = gesture.span0 + Math.sign(change) * SLOP_PX;
345
+ }
346
+ if (span > 0 && gesture.span > 0 && span !== gesture.span) {
347
+ this.#sheet.zoom(span / gesture.span, middle.x, middle.y);
348
+ }
349
+ gesture.span = span;
350
+ }
351
+ }
352
+ /**
353
+ * Whether two fingers are read at this finger's move — their middle and their span: once both have
354
+ * moved, or this one twice while the other stood still.
355
+ */
356
+ function paired(gesture, pointerId) {
357
+ if (gesture.unpaired === undefined) {
358
+ gesture.unpaired = pointerId;
359
+ return false;
360
+ }
361
+ if (gesture.unpaired !== pointerId)
362
+ gesture.unpaired = undefined;
363
+ return true;
364
+ }
365
+ /** The span between two fingers on the screen. */
366
+ function spanOf(one, two) {
367
+ return Math.hypot(one.x - two.x, one.y - two.y);
317
368
  }
318
369
  /** The middle of two fingers on the screen. */
319
370
  function middleOf(one, two) {
@@ -0,0 +1,29 @@
1
+ /**
2
+ * A statement read from a task from paper, as the element draws it (R89): the text as printed,
3
+ * "□" for each box an answer goes in, and what one line of text cannot hold written LaTeX's way —
4
+ * a fraction with a bar as `\frac{3}{17}`, nested as printed, and a raised power as `^{2}` (R169
5
+ * spec). Nothing else of LaTeX is the reading's; a model that strays all the same gets its few
6
+ * common signs drawn as signs, `\left` and `\right` dropped, and any other command shown as
7
+ * written, so a reading is never lost to its notation.
8
+ */
9
+ export type Piece = {
10
+ readonly kind: "text";
11
+ readonly text: string;
12
+ } | {
13
+ readonly kind: "box";
14
+ } | {
15
+ readonly kind: "fraction";
16
+ readonly over: readonly Piece[];
17
+ readonly under: readonly Piece[];
18
+ } | {
19
+ readonly kind: "power";
20
+ readonly base: readonly Piece[];
21
+ readonly exponent: readonly Piece[];
22
+ };
23
+ /** The pieces of a statement read, in order. */
24
+ export declare function piecesOf(statement: string): readonly Piece[];
25
+ /**
26
+ * Whether a statement is a formula to the eye, its lone letters its unknowns: it holds no word of
27
+ * two letters or more, outside LaTeX's commands. "3x + 5 = 20" is one, "Ann has 3 apples" is not.
28
+ */
29
+ export declare function formulaic(statement: string): boolean;
@@ -0,0 +1,180 @@
1
+ /** The signs a model may write as LaTeX's commands, drawn as the page prints them. */
2
+ const SIGNS = Object.freeze({
3
+ cdot: "·",
4
+ times: "×",
5
+ div: "÷",
6
+ pm: "±",
7
+ le: "≤",
8
+ leq: "≤",
9
+ ge: "≥",
10
+ geq: "≥",
11
+ ne: "≠",
12
+ neq: "≠",
13
+ approx: "≈",
14
+ square: "□",
15
+ Box: "□",
16
+ });
17
+ /** The commands that only size a bracket, dropped with the space after them. */
18
+ const DROPPED = new Set(["left", "right"]);
19
+ /** The pieces of a statement read, in order. */
20
+ export function piecesOf(statement) {
21
+ return new Reader(statement).pieces(false);
22
+ }
23
+ /**
24
+ * Whether a statement is a formula to the eye, its lone letters its unknowns: it holds no word of
25
+ * two letters or more, outside LaTeX's commands. "3x + 5 = 20" is one, "Ann has 3 apples" is not.
26
+ */
27
+ export function formulaic(statement) {
28
+ return !/\p{L}{2,}/u.test(statement.replace(/\\[A-Za-z]+/g, " "));
29
+ }
30
+ /** Whether a character is an ASCII digit. */
31
+ const digit = (c) => c !== undefined && c >= "0" && c <= "9";
32
+ /**
33
+ * The atom a power raises: the number, with one decimal mark, the letter or the bracket at the
34
+ * text's end. Read back from the end: a pattern anchored at the end alone tries every start, and
35
+ * is quadratic on a long run of digits.
36
+ */
37
+ function atomAtEnd(text) {
38
+ let start = text.length;
39
+ while (digit(text[start - 1]))
40
+ start--;
41
+ if (start === text.length) {
42
+ const last = [...text.slice(-2)].at(-1);
43
+ return last !== undefined && /^(?:\p{L}|[)\]])$/u.test(last) ? last : undefined;
44
+ }
45
+ const mark = text[start - 1];
46
+ if ((mark === "." || mark === ",") && digit(text[start - 2])) {
47
+ start--;
48
+ while (digit(text[start - 1]))
49
+ start--;
50
+ }
51
+ return text.slice(start);
52
+ }
53
+ class Reader {
54
+ #at = 0;
55
+ #text;
56
+ constructor(text) {
57
+ this.#text = text;
58
+ }
59
+ /** The pieces up to the end, or to the brace that closes a group when `inGroup`. */
60
+ pieces(inGroup) {
61
+ const out = [];
62
+ const text = this.#text;
63
+ while (this.#at < text.length) {
64
+ const c = text[this.#at];
65
+ if (c === "}" && inGroup) {
66
+ this.#at++;
67
+ return out;
68
+ }
69
+ if (c === "□") {
70
+ this.#at++;
71
+ out.push({ kind: "box" });
72
+ }
73
+ else if (c === "\\") {
74
+ this.#command(out);
75
+ }
76
+ else if (c === "^") {
77
+ this.#at++;
78
+ out.push({ kind: "power", base: baseOf(out), exponent: this.#argument() });
79
+ }
80
+ else if (c === "{") {
81
+ // A group of its own only groups: its pieces stand in the line.
82
+ this.#at++;
83
+ for (const piece of this.pieces(true)) {
84
+ if (piece.kind === "text")
85
+ push(out, piece.text);
86
+ else
87
+ out.push(piece);
88
+ }
89
+ }
90
+ else if (c === "}") {
91
+ // A brace that closes nothing is no part of the print.
92
+ this.#at++;
93
+ }
94
+ else {
95
+ this.#at++;
96
+ push(out, c);
97
+ }
98
+ }
99
+ return out;
100
+ }
101
+ /** A command after its backslash: a fraction, a sign, a bracket's size, or the text as written. */
102
+ #command(out) {
103
+ const text = this.#text;
104
+ const name = /^\\([A-Za-z]+|.)/.exec(text.slice(this.#at))?.[1] ?? "";
105
+ this.#at += 1 + name.length;
106
+ if (name === "frac" || name === "dfrac" || name === "tfrac") {
107
+ const over = this.#argument();
108
+ const under = this.#argument();
109
+ out.push({ kind: "fraction", over, under });
110
+ }
111
+ else if (name in SIGNS) {
112
+ const sign = SIGNS[name];
113
+ if (sign === "□")
114
+ out.push({ kind: "box" });
115
+ else
116
+ push(out, sign);
117
+ }
118
+ else if (DROPPED.has(name)) {
119
+ if (text[this.#at] === " ")
120
+ this.#at++;
121
+ }
122
+ else if (name === "{" || name === "}" || name === " " || name === ",") {
123
+ push(out, name === "," ? " " : name);
124
+ }
125
+ else {
126
+ push(out, `\\${name}`);
127
+ }
128
+ }
129
+ /** A command's argument: a group in braces, or the one character after it, spaces passed over. */
130
+ #argument() {
131
+ const text = this.#text;
132
+ while (text[this.#at] === " ")
133
+ this.#at++;
134
+ if (text[this.#at] === "{") {
135
+ this.#at++;
136
+ return this.pieces(true);
137
+ }
138
+ const out = [];
139
+ const c = text[this.#at];
140
+ if (c === undefined)
141
+ return out;
142
+ if (c === "\\") {
143
+ this.#command(out);
144
+ return out;
145
+ }
146
+ this.#at++;
147
+ if (c === "□")
148
+ out.push({ kind: "box" });
149
+ else
150
+ push(out, c);
151
+ return out;
152
+ }
153
+ }
154
+ /** Text added to the pieces: joined to the text before it. */
155
+ function push(out, text) {
156
+ const last = out.at(-1);
157
+ if (last?.kind === "text")
158
+ out[out.length - 1] = { kind: "text", text: last.text + text };
159
+ else
160
+ out.push({ kind: "text", text });
161
+ }
162
+ /** What a power raises, taken from the pieces before it: their last atom, or none. */
163
+ function baseOf(out) {
164
+ const last = out.at(-1);
165
+ if (last === undefined)
166
+ return [];
167
+ if (last.kind !== "text") {
168
+ out.pop();
169
+ return [last];
170
+ }
171
+ const atom = atomAtEnd(last.text);
172
+ if (atom === undefined)
173
+ return [];
174
+ const rest = last.text.slice(0, -atom.length);
175
+ if (rest === "")
176
+ out.pop();
177
+ else
178
+ out[out.length - 1] = { kind: "text", text: rest };
179
+ return [{ kind: "text", text: atom }];
180
+ }
@@ -0,0 +1,34 @@
1
+ /** The smallest and the largest zoom of the sheet: half its fit, and twice it (R73). */
2
+ export declare const ZOOM_MIN = 0.5;
3
+ export declare const ZOOM_MAX = 2;
4
+ /**
5
+ * The step of Zoom in and Zoom out, a quarter of the fit (R73): from any zoom a pinch left, a
6
+ * press goes to the next quarter, so 50, 75, … 200 % are where the buttons stop.
7
+ */
8
+ export declare const ZOOM_STEP = 0.25;
9
+ /** The zoom `value` kept within its bounds; anything that is not a number is the fit. */
10
+ export declare function clampZoom(value: number): number;
11
+ /**
12
+ * The next quarter from `value`, larger or smaller, within the bounds. A zoom within a hair of a
13
+ * quarter counts as that quarter, so that the arithmetic of a pinch never makes a press a no-op.
14
+ */
15
+ export declare function stepZoom(value: number, larger: boolean): number;
16
+ /**
17
+ * The page's zoom (R73): one for every sheet of the page, from 100 % when the page is loaded, as
18
+ * the pen mode is (R29). The next task is a new element (R20), and the zoom stays: a learner who
19
+ * needs it larger or smaller asks once. It lives in memory alone: nothing is stored or sent.
20
+ */
21
+ export declare class PageZoom {
22
+ #private;
23
+ /** The zoom now: 1 for the fit, 2 for twice it, 0.5 for half. */
24
+ get value(): number;
25
+ /** Whether there is a larger zoom to go to, for Zoom in, or a smaller one, for Zoom out. */
26
+ get larger(): boolean;
27
+ get smaller(): boolean;
28
+ /** A step to the next quarter, larger or smaller, if there is one (R73). */
29
+ step(larger: boolean): boolean;
30
+ /** The zoom set to `value` within its bounds — a pinch's —: every watcher hears of a change. */
31
+ set(value: number): boolean;
32
+ /** `watcher` hears of every change from now on, until the function this gives is called. */
33
+ watch(watcher: () => void): () => void;
34
+ }
@@ -0,0 +1,64 @@
1
+ /** The smallest and the largest zoom of the sheet: half its fit, and twice it (R73). */
2
+ export const ZOOM_MIN = 0.5;
3
+ export const ZOOM_MAX = 2;
4
+ /**
5
+ * The step of Zoom in and Zoom out, a quarter of the fit (R73): from any zoom a pinch left, a
6
+ * press goes to the next quarter, so 50, 75, … 200 % are where the buttons stop.
7
+ */
8
+ export const ZOOM_STEP = 0.25;
9
+ /** The zoom `value` kept within its bounds; anything that is not a number is the fit. */
10
+ export function clampZoom(value) {
11
+ if (!Number.isFinite(value))
12
+ return 1;
13
+ return Math.min(Math.max(value, ZOOM_MIN), ZOOM_MAX);
14
+ }
15
+ /**
16
+ * The next quarter from `value`, larger or smaller, within the bounds. A zoom within a hair of a
17
+ * quarter counts as that quarter, so that the arithmetic of a pinch never makes a press a no-op.
18
+ */
19
+ export function stepZoom(value, larger) {
20
+ const quarters = value / ZOOM_STEP;
21
+ const next = larger ? Math.floor(quarters + 1e-6) + 1 : Math.ceil(quarters - 1e-6) - 1;
22
+ return clampZoom(next * ZOOM_STEP);
23
+ }
24
+ /**
25
+ * The page's zoom (R73): one for every sheet of the page, from 100 % when the page is loaded, as
26
+ * the pen mode is (R29). The next task is a new element (R20), and the zoom stays: a learner who
27
+ * needs it larger or smaller asks once. It lives in memory alone: nothing is stored or sent.
28
+ */
29
+ export class PageZoom {
30
+ #value = 1;
31
+ #watchers = new Set();
32
+ /** The zoom now: 1 for the fit, 2 for twice it, 0.5 for half. */
33
+ get value() {
34
+ return this.#value;
35
+ }
36
+ /** Whether there is a larger zoom to go to, for Zoom in, or a smaller one, for Zoom out. */
37
+ get larger() {
38
+ return this.#value < ZOOM_MAX;
39
+ }
40
+ get smaller() {
41
+ return this.#value > ZOOM_MIN;
42
+ }
43
+ /** A step to the next quarter, larger or smaller, if there is one (R73). */
44
+ step(larger) {
45
+ return this.set(stepZoom(this.#value, larger));
46
+ }
47
+ /** The zoom set to `value` within its bounds — a pinch's —: every watcher hears of a change. */
48
+ set(value) {
49
+ const next = clampZoom(value);
50
+ if (next === this.#value)
51
+ return false;
52
+ this.#value = next;
53
+ for (const watcher of [...this.#watchers])
54
+ watcher();
55
+ return true;
56
+ }
57
+ /** `watcher` hears of every change from now on, until the function this gives is called. */
58
+ watch(watcher) {
59
+ this.#watchers.add(watcher);
60
+ return () => {
61
+ this.#watchers.delete(watcher);
62
+ };
63
+ }
64
+ }
@@ -68,6 +68,14 @@ export declare const ICONS: {
68
68
  readonly viewBox: "0 0 24 24";
69
69
  readonly paths: readonly ["M5 21q-.825 0-1.412-.587T3 19V5q0-.825.588-1.412T5 3h14q.825 0 1.413.588T21 5v14q0 .825-.587 1.413T19 21zm0-2h3.325v-3.325H5zm5.325 0h3.35v-3.325h-3.35zm5.35 0H19v-3.325h-3.325zM5 13.675h3.325v-3.35H5zm5.325 0h3.35v-3.35h-3.35zm5.35 0H19v-3.35h-3.325zM5 8.325h3.325V5H5zm5.325 0h3.35V5h-3.35zm5.35 0H19V5h-3.325z"];
70
70
  };
71
+ readonly "zoom-in-outline-rounded": {
72
+ readonly viewBox: "0 0 24 24";
73
+ readonly paths: readonly ["M8.5 10.5h-1q-.425 0-.712-.288T6.5 9.5t.288-.712T7.5 8.5h1v-1q0-.425.288-.712T9.5 6.5t.713.288t.287.712v1h1q.425 0 .713.288t.287.712t-.288.713t-.712.287h-1v1q0 .425-.288.713T9.5 12.5t-.712-.288T8.5 11.5zm1 5.5q-2.725 0-4.612-1.888T3 9.5t1.888-4.612T9.5 3t4.613 1.888T16 9.5q0 1.1-.35 2.075T14.7 13.3l5.6 5.6q.275.275.275.7t-.275.7t-.7.275t-.7-.275l-5.6-5.6q-.75.6-1.725.95T9.5 16m0-2q1.875 0 3.188-1.312T14 9.5t-1.312-3.187T9.5 5T6.313 6.313T5 9.5t1.313 3.188T9.5 14"];
74
+ };
75
+ readonly "zoom-out-outline-rounded": {
76
+ readonly viewBox: "0 0 24 24";
77
+ readonly paths: readonly ["M8 10.5q-.425 0-.712-.288T7 9.5t.288-.712T8 8.5h3q.425 0 .713.288T12 9.5t-.288.713T11 10.5zM9.5 16q-2.725 0-4.612-1.888T3 9.5t1.888-4.612T9.5 3t4.613 1.888T16 9.5q0 1.1-.35 2.075T14.7 13.3l5.6 5.6q.275.275.275.7t-.275.7t-.7.275t-.7-.275l-5.6-5.6q-.75.6-1.725.95T9.5 16m0-2q1.875 0 3.188-1.312T14 9.5t-1.312-3.187T9.5 5T6.313 6.313T5 9.5t1.313 3.188T9.5 14"];
78
+ };
71
79
  readonly "remove-outline-rounded": {
72
80
  readonly viewBox: "0 0 24 24";
73
81
  readonly paths: readonly ["M6 13q-.425 0-.712-.288T5 12t.288-.712T6 11h12q.425 0 .713.288T19 12t-.288.713T18 13z"];
@@ -162,12 +170,22 @@ export declare const ROW: {
162
170
  readonly filled: "grid-on-outline-rounded";
163
171
  readonly mirrored: false;
164
172
  };
173
+ readonly zoomIn: {
174
+ readonly outline: "zoom-in-outline-rounded";
175
+ readonly filled: "zoom-in-outline-rounded";
176
+ readonly mirrored: false;
177
+ };
165
178
  readonly zoomOut: {
179
+ readonly outline: "zoom-out-outline-rounded";
180
+ readonly filled: "zoom-out-outline-rounded";
181
+ readonly mirrored: false;
182
+ };
183
+ readonly smaller: {
166
184
  readonly outline: "remove-outline-rounded";
167
185
  readonly filled: "remove-outline-rounded";
168
186
  readonly mirrored: false;
169
187
  };
170
- readonly zoomIn: {
188
+ readonly larger: {
171
189
  readonly outline: "add-outline-rounded";
172
190
  readonly filled: "add-outline-rounded";
173
191
  readonly mirrored: false;
@@ -106,6 +106,18 @@ export const ICONS = {
106
106
  "M5 21q-.825 0-1.412-.587T3 19V5q0-.825.588-1.412T5 3h14q.825 0 1.413.588T21 5v14q0 .825-.587 1.413T19 21zm0-2h3.325v-3.325H5zm5.325 0h3.35v-3.325h-3.35zm5.35 0H19v-3.325h-3.325zM5 13.675h3.325v-3.35H5zm5.325 0h3.35v-3.35h-3.35zm5.35 0H19v-3.35h-3.325zM5 8.325h3.325V5H5zm5.325 0h3.35V5h-3.35zm5.35 0H19V5h-3.325z",
107
107
  ],
108
108
  },
109
+ "zoom-in-outline-rounded": {
110
+ viewBox: "0 0 24 24",
111
+ paths: [
112
+ "M8.5 10.5h-1q-.425 0-.712-.288T6.5 9.5t.288-.712T7.5 8.5h1v-1q0-.425.288-.712T9.5 6.5t.713.288t.287.712v1h1q.425 0 .713.288t.287.712t-.288.713t-.712.287h-1v1q0 .425-.288.713T9.5 12.5t-.712-.288T8.5 11.5zm1 5.5q-2.725 0-4.612-1.888T3 9.5t1.888-4.612T9.5 3t4.613 1.888T16 9.5q0 1.1-.35 2.075T14.7 13.3l5.6 5.6q.275.275.275.7t-.275.7t-.7.275t-.7-.275l-5.6-5.6q-.75.6-1.725.95T9.5 16m0-2q1.875 0 3.188-1.312T14 9.5t-1.312-3.187T9.5 5T6.313 6.313T5 9.5t1.313 3.188T9.5 14",
113
+ ],
114
+ },
115
+ "zoom-out-outline-rounded": {
116
+ viewBox: "0 0 24 24",
117
+ paths: [
118
+ "M8 10.5q-.425 0-.712-.288T7 9.5t.288-.712T8 8.5h3q.425 0 .713.288T12 9.5t-.288.713T11 10.5zM9.5 16q-2.725 0-4.612-1.888T3 9.5t1.888-4.612T9.5 3t4.613 1.888T16 9.5q0 1.1-.35 2.075T14.7 13.3l5.6 5.6q.275.275.275.7t-.275.7t-.7.275t-.7-.275l-5.6-5.6q-.75.6-1.725.95T9.5 16m0-2q1.875 0 3.188-1.312T14 9.5t-1.312-3.187T9.5 5T6.313 6.313T5 9.5t1.313 3.188T9.5 14",
119
+ ],
120
+ },
109
121
  "remove-outline-rounded": {
110
122
  viewBox: "0 0 24 24",
111
123
  paths: [
@@ -188,8 +200,18 @@ export const ROW = {
188
200
  filled: "grid-on-outline-rounded",
189
201
  mirrored: false,
190
202
  },
191
- zoomOut: { outline: "remove-outline-rounded", filled: "remove-outline-rounded", mirrored: false },
192
- zoomIn: { outline: "add-outline-rounded", filled: "add-outline-rounded", mirrored: false },
203
+ zoomIn: {
204
+ outline: "zoom-in-outline-rounded",
205
+ filled: "zoom-in-outline-rounded",
206
+ mirrored: false,
207
+ },
208
+ zoomOut: {
209
+ outline: "zoom-out-outline-rounded",
210
+ filled: "zoom-out-outline-rounded",
211
+ mirrored: false,
212
+ },
213
+ smaller: { outline: "remove-outline-rounded", filled: "remove-outline-rounded", mirrored: false },
214
+ larger: { outline: "add-outline-rounded", filled: "add-outline-rounded", mirrored: false },
193
215
  fit: {
194
216
  outline: "fit-screen-outline-rounded",
195
217
  filled: "fit-screen-outline-rounded",
@@ -70,12 +70,13 @@ import { cellsForCheck, paperWorkForCheck, workForCheck } from "../core/submissi
70
70
  import { HAND, isTool, isWorkbook, NO_TASK, sameLayer, sameStatement, taskOf, templateOf, wholeSheet, } from "../core/task.js";
71
71
  import { boxOnSheet, isPaperGrid, keptOn, layOver, nextGrid, onSheet, onTracing, PAPER_GRIDS, readingOf, rulingsOf, sameTracing, scaled, scaleIn, TRACING, TRACING_START, toTracing, rectOf as tracingRect, scaleOf as tracingScaleOf, tracingStateOf, whollyOff, } from "../core/tracing.js";
72
72
  import { nextIdOf, resumeOf, Work } from "../core/work.js";
73
+ import { ZOOM_MIN, ZOOM_STEP } from "../core/zoom.js";
73
74
  import { placeOver, SVG, write } from "./attributes.js";
74
75
  import { knows, labelsFor, say, sayAround } from "./labels.js";
75
76
  import { Parts } from "./parts.js";
76
77
  import { Pictures } from "./pictures.js";
77
78
  import { attachProbe } from "./probe.js";
78
- import { PEN_MODE } from "./session.js";
79
+ import { PAGE_ZOOM, PEN_MODE } from "./session.js";
79
80
  import { Statement } from "./statement.js";
80
81
  import { Tools } from "./tools.js";
81
82
  import { TracingPaper } from "./tracing.js";
@@ -201,7 +202,9 @@ const ERASER_RADIUS_PX = 10;
201
202
  // element inherits — spacing, weight, transforms, the direction — moves them: `all: initial` takes
202
203
  // it off, and what the print needs comes back (R36). A box's frame is one CSS pixel at any scale,
203
204
  // on whole device pixels: an antialiased line of one pixel would lose its contrast. A filled box's
204
- // is three, in the same ink, and the system's highlight with forced colours (R37).
205
+ // is three, in the same ink, and the system's highlight with forced colours (R37). A dot of the grid
206
+ // is three pixels across at the fit and above it, and smaller by the zoom below it, so that a sheet
207
+ // zoomed out is no field of dots (R73).
205
208
  const STYLE = `
206
209
  :host { display: flex; flex-direction: column; isolation: isolate; writing-mode: horizontal-tb; }
207
210
  :host([hidden]) { display: none; }
@@ -296,11 +299,13 @@ const STYLE = `
296
299
  display: block; outline: 2px solid var(--novatrail-canvas-ink, ${INK}); outline-offset: -2px;
297
300
  }
298
301
  .column {
299
- position: relative; width: calc(min(100%, ${K_MAX * SHEET.width}px) * var(--across, 1));
302
+ position: relative;
303
+ width: calc(min(100%, ${K_MAX * SHEET.width}px) * var(--zoom, 1) * var(--across, 1));
300
304
  padding-block: var(--novatrail-canvas-pad-top, 0px) var(--novatrail-canvas-pad-bottom, 0px);
301
305
  }
302
306
  .statement, .parts {
303
- position: sticky; left: 0; box-sizing: border-box; width: calc(100% / var(--across, 1));
307
+ position: sticky; left: 0; box-sizing: border-box;
308
+ width: calc(100% / (var(--zoom, 1) * var(--across, 1)));
304
309
  direction: var(--direction, ltr);
305
310
  }
306
311
  .statement { padding: 0 0 12px; overflow-wrap: break-word; }
@@ -349,7 +354,7 @@ const STYLE = `
349
354
  fill: none; stroke: var(--novatrail-canvas-grid, #d3dbe5); stroke-width: 1px;
350
355
  vector-effect: non-scaling-stroke;
351
356
  }
352
- .grid.dots { stroke-width: 3px; stroke-linecap: round; }
357
+ .grid.dots { stroke-width: calc(3px * min(1, var(--zoom, 1))); stroke-linecap: round; }
353
358
  .print {
354
359
  all: initial; font-family: inherit; visibility: inherit; pointer-events: none; direction: ltr;
355
360
  font-variant-numeric: tabular-nums; font-size: ${LAYOUT.size}px;
@@ -399,10 +404,14 @@ canvas {
399
404
  color: var(--novatrail-canvas-ink, ${INK});
400
405
  }
401
406
  .statements .slot {
402
- display: inline-block; box-sizing: border-box; width: 1.08em; height: 1.08em;
403
- margin: 0 0.18em; vertical-align: -0.22em; border-radius: 0.2em;
407
+ box-sizing: border-box; border-radius: 0.2em;
404
408
  border: max(2px, 0.04em) dashed var(--novatrail-canvas-accent, ${ACCENT});
405
409
  }
410
+ .statements span.slot {
411
+ display: inline-block; width: 1.08em; height: 1.08em; margin: 0 0.18em; vertical-align: -0.22em;
412
+ }
413
+ .statements mspace.slot { margin: 0 0.1em; }
414
+ .statements math { font-family: inherit; font-size: inherit; color: inherit; }
406
415
  .scan {
407
416
  position: absolute; inset: 0; overflow: hidden;
408
417
  background: color-mix(in srgb, var(--novatrail-canvas-accent, ${ACCENT}) 4%, transparent);
@@ -411,9 +420,13 @@ canvas {
411
420
  position: absolute; top: 0; bottom: 0; left: -30%; width: 30%;
412
421
  background: linear-gradient(90deg, transparent,
413
422
  color-mix(in srgb, var(--novatrail-canvas-accent, ${ACCENT}) 18%, transparent), transparent);
414
- border-right: 2px solid color-mix(in srgb, var(--novatrail-canvas-accent, ${ACCENT}) 60%, transparent);
415
423
  left: 35%;
416
424
  }
425
+ /* The line runs through the middle of its glow, so the glow is the same on both sides of it. */
426
+ .scan i::after {
427
+ content: ""; position: absolute; top: 0; bottom: 0; left: calc(50% - 1px); width: 2px;
428
+ background: color-mix(in srgb, var(--novatrail-canvas-accent, ${ACCENT}) 60%, transparent);
429
+ }
417
430
  @keyframes novatrail-scan { from { left: -30%; } to { left: 100%; } }
418
431
  @keyframes novatrail-spin { to { transform: rotate(360deg); } }
419
432
  .tracing-frame {
@@ -504,7 +517,8 @@ canvas {
504
517
  .statements { color: CanvasText; }
505
518
  .statements .slot { border-color: CanvasText; }
506
519
  .scan { background: transparent; }
507
- .scan i { background: transparent; border-right-color: Highlight; }
520
+ .scan i { background: transparent; }
521
+ .scan i::after { background: Highlight; }
508
522
  .stage > .tracing-sign, .stage > .tracing-sign[data-shows] {
509
523
  background: Canvas; color: CanvasText; border: 1px solid CanvasText;
510
524
  }
@@ -697,6 +711,7 @@ export class NovatrailCanvas extends HTMLElement {
697
711
  choose: (tool) => this.#choose(tool),
698
712
  undo: () => this.undo(),
699
713
  clear: () => this.clear(),
714
+ zoom: (larger) => this.#zoomBy(larger),
700
715
  turnGrid: () => this.#turnGrid(),
701
716
  next: () => this.#openNext(),
702
717
  toggle: () => this.#toggleDemo(),
@@ -750,6 +765,7 @@ export class NovatrailCanvas extends HTMLElement {
750
765
  writer: () => this.#writer(),
751
766
  drop: (pointerId) => this.#drop(pointerId),
752
767
  scroll: (dx, dy, fresh) => this.#scroll(dx, dy, fresh),
768
+ zoom: (ratio, x, y) => this.#pinch(ratio, x, y),
753
769
  across: () => overflowOf(this.#viewport, "x") > EDGE,
754
770
  }, PEN_MODE);
755
771
  /**
@@ -771,6 +787,18 @@ export class NovatrailCanvas extends HTMLElement {
771
787
  #port = null;
772
788
  #ported = new ResizeObserver(() => this.#fit());
773
789
  #unwatchPen;
790
+ #unwatchZoom;
791
+ /** The zoom the sheet is laid out by: the page's, and a worked example's 1 (R73). */
792
+ #zoom = 1;
793
+ /** Where on the screen a pinch zooms about, while it sets the page's zoom (R73). */
794
+ #anchor;
795
+ /**
796
+ * The point of the sheet a press zoomed about, where on the screen it stood, and where the press
797
+ * left the container: while nothing has scrolled or moved it since, the next press zooms about the
798
+ * same point, so that a zoom out at the sheet's edge, where the scroll stops short, and back in
799
+ * comes back where it was (R73).
800
+ */
801
+ #held;
774
802
  #tool = "pen";
775
803
  /** The pointer the hand moves the sheet by, and where it was on the screen last (R88). */
776
804
  #pan;
@@ -1400,6 +1428,8 @@ export class NovatrailCanvas extends HTMLElement {
1400
1428
  unwatch();
1401
1429
  unenlist();
1402
1430
  };
1431
+ // The page's zoom, on every sheet of it (R73): it may have changed while the element was away.
1432
+ this.#unwatchZoom = PAGE_ZOOM.watch(() => this.#zoomed());
1403
1433
  // A page that grows or shrinks scrolls nothing, and neither does a window of another size.
1404
1434
  this.#page.observe(document.documentElement);
1405
1435
  window.addEventListener("resize", this.#resized);
@@ -1603,6 +1633,8 @@ export class NovatrailCanvas extends HTMLElement {
1603
1633
  this.#latchAcross = undefined;
1604
1634
  this.#unwatchPen?.();
1605
1635
  this.#unwatchPen = undefined;
1636
+ this.#unwatchZoom?.();
1637
+ this.#unwatchZoom = undefined;
1606
1638
  this.#endPan();
1607
1639
  this.#page.disconnect();
1608
1640
  this.#ported.disconnect();
@@ -2241,9 +2273,9 @@ export class NovatrailCanvas extends HTMLElement {
2241
2273
  const area = this.#area;
2242
2274
  if (area === undefined || area.width === 0)
2243
2275
  return;
2244
- // The page is as wide as the container up to the cap, the column as wide as the sheet goes, by
2245
- // the style, and the stage as tall and as wide as the visible part of the sheet.
2246
- const k = fitScale(area.width);
2276
+ // The page is as wide as the container up to the cap, times the zoom, the column as wide as the
2277
+ // sheet goes, by the style, and the stage as tall and as wide as the visible part of the sheet.
2278
+ const k = fitScale(area.width) * this.#zoom;
2247
2279
  const extent = this.#extent;
2248
2280
  this.#stage.style.width = `${Math.min(area.width, extent.width * k)}px`;
2249
2281
  this.#stage.style.height = `${Math.min(area.height, extent.height * k)}px`;
@@ -2278,16 +2310,19 @@ export class NovatrailCanvas extends HTMLElement {
2278
2310
  }
2279
2311
  /**
2280
2312
  * How far the sheet goes (R73): past the page, the ink and the tracing paper where it lies, by
2281
- * one view of the window more, at the window's own fit, within the reach. A worked example's
2282
- * sheet is its page alone. The window's view and not the container's: the sheet is sized by its
2283
- * style, and a size that the container's observer gave would resize the container again while
2284
- * the observer reports it, which the browser tells the window as a loop.
2313
+ * one view of the window more across and two down, at the window's own fit times the zoom,
2314
+ * within the reach. A worked example's sheet is its page alone. The window's view and not the
2315
+ * container's: the sheet is sized by its style, and a size that the container's observer gave
2316
+ * would resize the container again while the observer reports it, which the browser tells the
2317
+ * window as a loop. A zoom below the fit counts by its quarter below, so that a pinch lays the
2318
+ * sheet out anew only as it crosses one, and above the fit as the fit: the view is smaller then.
2285
2319
  */
2286
2320
  #reach() {
2287
2321
  if (this.#example !== undefined)
2288
2322
  return SHEET;
2289
- const k = fitScale(innerWidth);
2290
- return extentOf(this.#inkReach(), { width: innerWidth / k, height: innerHeight / k });
2323
+ const quarter = Math.max(ZOOM_MIN, Math.floor(this.#zoom / ZOOM_STEP + 1e-6) * ZOOM_STEP);
2324
+ const k = fitScale(innerWidth) * Math.min(1, quarter);
2325
+ return extentOf(this.#inkReach(), { width: innerWidth / k, height: (2 * innerHeight) / k });
2291
2326
  }
2292
2327
  /** The right and the bottom of the sheet's ink and of the tracing paper, which counts whole. */
2293
2328
  #inkReach() {
@@ -2318,15 +2353,77 @@ export class NovatrailCanvas extends HTMLElement {
2318
2353
  * page, and the sheet in the extent's proportions —, and laid out (R73).
2319
2354
  */
2320
2355
  #regrow() {
2356
+ const zoom = this.#example === undefined ? PAGE_ZOOM.value : 1;
2357
+ const zoomed = zoom !== this.#zoom;
2358
+ if (zoomed) {
2359
+ this.#zoom = zoom;
2360
+ this.#column.style.setProperty("--zoom", String(zoom));
2361
+ }
2321
2362
  const extent = this.#reach();
2322
- if (extent.width === this.#extent.width && extent.height === this.#extent.height)
2363
+ const grown = extent.width !== this.#extent.width || extent.height !== this.#extent.height;
2364
+ if (!zoomed && !grown)
2323
2365
  return;
2324
- this.#extent = extent;
2325
- this.#column.style.setProperty("--across", String(extent.width / SHEET.width));
2326
- this.#sheet.style.aspectRatio = `${extent.width} / ${extent.height}`;
2327
- this.#drawGrid();
2366
+ if (grown) {
2367
+ this.#extent = extent;
2368
+ this.#column.style.setProperty("--across", String(extent.width / SHEET.width));
2369
+ this.#sheet.style.aspectRatio = `${extent.width} / ${extent.height}`;
2370
+ this.#drawGrid();
2371
+ }
2328
2372
  this.#layout();
2329
2373
  }
2374
+ /**
2375
+ * Zoom in or Zoom out was pressed (R73): the page's zoom goes a quarter larger or smaller, and
2376
+ * every sheet of the page is laid out by it — but not while a pointer writes or erases on one of
2377
+ * them, for nothing moves under it.
2378
+ */
2379
+ #zoomBy(larger) {
2380
+ if (!PEN_MODE.writing)
2381
+ PAGE_ZOOM.step(larger);
2382
+ }
2383
+ /** Two fingers pinched by `ratio` about `x` and `y` on the screen: the page's zoom goes by it. */
2384
+ #pinch(ratio, x, y) {
2385
+ if (this.#example !== undefined)
2386
+ return;
2387
+ this.#anchor = { x, y };
2388
+ try {
2389
+ PAGE_ZOOM.set(PAGE_ZOOM.value * ratio);
2390
+ }
2391
+ finally {
2392
+ this.#anchor = undefined;
2393
+ }
2394
+ }
2395
+ /**
2396
+ * The page's zoom has changed (R73): the sheet is laid out by it at once, the point of the sheet
2397
+ * under a pinch's middle, or else the one the last press held, or else under the middle of the
2398
+ * part in sight, standing where it was on the screen, and the bar shows what the zoom can do now.
2399
+ */
2400
+ #zoomed() {
2401
+ const before = this.isConnected ? this.#view() : undefined;
2402
+ const k = this.#k;
2403
+ this.#regrow();
2404
+ if (before !== undefined && this.#k !== k && this.#k > 0) {
2405
+ const { shown } = before;
2406
+ const held = this.#anchor === undefined && this.#held?.box === this.#heldBox() ? this.#held : undefined;
2407
+ const x = this.#anchor?.x ?? held?.x ?? (shown.left + shown.right) / 2;
2408
+ const y = this.#anchor?.y ?? held?.y ?? (shown.top + shown.bottom) / 2;
2409
+ const at = held?.at ?? {
2410
+ x: (x - before.left) / before.k + before.vx,
2411
+ y: (y - before.top) / before.k + before.vy,
2412
+ };
2413
+ const port = clientBoxOf(this.#viewport);
2414
+ this.#viewport.scrollLeft = this.#sheetAtX + at.x * this.#k - (x - port.left) / port.across;
2415
+ this.#viewport.scrollTop = this.#sheetAt + at.y * this.#k - (y - port.top) / port.down;
2416
+ this.#scrolled();
2417
+ this.#held = this.#anchor === undefined ? { at, x, y, box: this.#heldBox() } : undefined;
2418
+ }
2419
+ this.#refresh();
2420
+ }
2421
+ /** Where the container stands on the screen and how far it is scrolled, as a press leaves it. */
2422
+ #heldBox() {
2423
+ const { left, top, right, bottom } = this.#viewport.getBoundingClientRect();
2424
+ const { scrollLeft, scrollTop } = this.#viewport;
2425
+ return [left, top, right, bottom, scrollLeft, scrollTop].join(" ");
2426
+ }
2330
2427
  /**
2331
2428
  * The template's grid over the whole sheet, as far as it goes (R73): the task's, or a task from
2332
2429
  * paper's that the learner turns, none on a blank one (R88); a workbook has its lines instead.
@@ -2447,7 +2544,7 @@ export class NovatrailCanvas extends HTMLElement {
2447
2544
  const state = this.#tracing;
2448
2545
  if (this.#paper === undefined || state === undefined)
2449
2546
  return;
2450
- this.#tracing = Object.freeze({ at: state.at, hidden: !state.hidden });
2547
+ this.#tracing = Object.freeze({ ...state, hidden: !state.hidden });
2451
2548
  this.#refresh();
2452
2549
  this.#request();
2453
2550
  this.#tracingChanged();
@@ -2744,6 +2841,8 @@ export class NovatrailCanvas extends HTMLElement {
2744
2841
  tool: this.#tool,
2745
2842
  undoable: this.#work.undoable,
2746
2843
  clearable: this.#work.strokes.length > 0,
2844
+ larger: PAGE_ZOOM.larger,
2845
+ smaller: PAGE_ZOOM.smaller,
2747
2846
  opens: this.#nextPart() !== undefined,
2748
2847
  expanded: demo?.hidden !== true,
2749
2848
  traced: this.#tracing?.hidden !== true,
@@ -25,6 +25,9 @@ export interface Labels {
25
25
  readonly clear: string;
26
26
  /** The tool that moves the sheet and writes nothing (R88). */
27
27
  readonly hand: string;
28
+ /** The buttons of the page's zoom of the sheet, a step larger and a step smaller (R73). */
29
+ readonly zoomIn: string;
30
+ readonly zoomOut: string;
28
31
  /**
29
32
  * The button of a task from paper's grid, by the grid the sheet shows now: a press turns it to
30
33
  * the next (R88).
@@ -42,8 +45,8 @@ export interface Labels {
42
45
  * {percent}, which a press sets back to its own size, and fit to the window.
43
46
  */
44
47
  readonly scaleButtons: string;
45
- readonly zoomOut: string;
46
- readonly zoomIn: string;
48
+ readonly smaller: string;
49
+ readonly larger: string;
47
50
  readonly scaleReset: string;
48
51
  readonly fit: string;
49
52
  /**
@@ -29,6 +29,8 @@ const ENGLISH = Object.freeze({
29
29
  undo: "Undo",
30
30
  clear: "Clear",
31
31
  hand: "Hand",
32
+ zoomIn: "Zoom in",
33
+ zoomOut: "Zoom out",
32
34
  gridDots: "Grid: dots",
33
35
  gridLines: "Grid: lines",
34
36
  gridBlank: "Grid: blank",
@@ -37,8 +39,8 @@ const ENGLISH = Object.freeze({
37
39
  hideTask: "Hide the task",
38
40
  showTask: "Show the task",
39
41
  scaleButtons: "Task scale",
40
- zoomOut: "Smaller",
41
- zoomIn: "Larger",
42
+ smaller: "Smaller",
43
+ larger: "Larger",
42
44
  scaleReset: "{percent}: back to 100%",
43
45
  fit: "Fit",
44
46
  recognise: "Recognise",
@@ -77,6 +79,8 @@ const LABELS = new Map([
77
79
  undo: "Отменить",
78
80
  clear: "Очистить",
79
81
  hand: "Рука",
82
+ zoomIn: "Крупнее",
83
+ zoomOut: "Мельче",
80
84
  gridDots: "Сетка: точки",
81
85
  gridLines: "Сетка: строчки",
82
86
  gridBlank: "Сетка: пусто",
@@ -85,8 +89,8 @@ const LABELS = new Map([
85
89
  hideTask: "Спрятать задачу",
86
90
  showTask: "Показать задачу",
87
91
  scaleButtons: "Масштаб задачи",
88
- zoomOut: "Уменьшить",
89
- zoomIn: "Увеличить",
92
+ smaller: "Уменьшить",
93
+ larger: "Увеличить",
90
94
  scaleReset: "{percent}: вернуть 100 %",
91
95
  fit: "Вписать",
92
96
  recognise: "Распознать",
@@ -0,0 +1,2 @@
1
+ /** The nodes of one statement's line. */
2
+ export declare function lineOf(statement: string): Node[];
@@ -0,0 +1,94 @@
1
+ import { formulaic, piecesOf } from "../core/notation.js";
2
+ // A statement read, drawn as the page prints it (R89): its text as text, a box an answer goes in
3
+ // as a dashed box, and a fraction or a power as MathML, which the browser lays out itself, a
4
+ // fraction full size on the line and those within it smaller, as a book sets them. In a statement
5
+ // with no word its lone letters are its unknowns, in italics as MathML sets a letter.
6
+ const MATHML = "http://www.w3.org/1998/Math/MathML";
7
+ /** The nodes of one statement's line. */
8
+ export function lineOf(statement) {
9
+ const letters = formulaic(statement);
10
+ const out = [];
11
+ for (const piece of piecesOf(statement)) {
12
+ if (piece.kind === "text")
13
+ out.push(...textOf(piece.text, letters));
14
+ else if (piece.kind === "box")
15
+ out.push(slot());
16
+ else {
17
+ const math = mathml("math");
18
+ math.setAttribute("displaystyle", "true");
19
+ math.append(mathOf(piece));
20
+ out.push(math);
21
+ }
22
+ }
23
+ return out;
24
+ }
25
+ /** Text of the line: in a formula, each lone letter in italics. */
26
+ function textOf(text, letters) {
27
+ if (!letters)
28
+ return [document.createTextNode(text)];
29
+ return text
30
+ .split(/(\p{L})/u)
31
+ .filter((part) => part !== "")
32
+ .map((part) => {
33
+ if (!/^\p{L}$/u.test(part))
34
+ return document.createTextNode(part);
35
+ const letter = document.createElement("i");
36
+ letter.textContent = part;
37
+ return letter;
38
+ });
39
+ }
40
+ /** A dashed box an answer goes in. */
41
+ function slot() {
42
+ const box = document.createElement("span");
43
+ box.className = "slot";
44
+ return box;
45
+ }
46
+ /** A piece as MathML. */
47
+ function mathOf(piece) {
48
+ switch (piece.kind) {
49
+ case "text":
50
+ return row(tokensOf(piece.text));
51
+ case "box": {
52
+ const box = mathml("mspace");
53
+ box.setAttribute("class", "slot");
54
+ // The box of the line's own, 1.08em square, its foot as far under the baseline.
55
+ box.setAttribute("width", "1.08em");
56
+ box.setAttribute("height", "0.86em");
57
+ box.setAttribute("depth", "0.22em");
58
+ return box;
59
+ }
60
+ case "fraction": {
61
+ const fraction = mathml("mfrac");
62
+ fraction.append(rowOf(piece.over), rowOf(piece.under));
63
+ return fraction;
64
+ }
65
+ case "power": {
66
+ const power = mathml("msup");
67
+ power.append(rowOf(piece.base), rowOf(piece.exponent));
68
+ return power;
69
+ }
70
+ }
71
+ }
72
+ /** Pieces as one row of MathML. */
73
+ function rowOf(pieces) {
74
+ return row(pieces.map(mathOf));
75
+ }
76
+ function row(children) {
77
+ const out = mathml("mrow");
78
+ out.append(...children);
79
+ return out;
80
+ }
81
+ /** Text as MathML's tokens: a number, a letter, and each other sign an operator; spaces dropped. */
82
+ function tokensOf(text) {
83
+ const out = [];
84
+ for (const [token] of text.matchAll(/\d+(?:[.,]\d+)?|\p{L}|\S/gu)) {
85
+ const kind = /^\d/.test(token) ? "mn" : /^\p{L}$/u.test(token) ? "mi" : "mo";
86
+ const element = mathml(kind);
87
+ element.textContent = token;
88
+ out.push(element);
89
+ }
90
+ return out;
91
+ }
92
+ function mathml(name) {
93
+ return document.createElementNS(MATHML, name);
94
+ }
@@ -1,3 +1,6 @@
1
1
  import { PenMode } from "../core/hands.js";
2
+ import { PageZoom } from "../core/zoom.js";
2
3
  /** The pen mode of every sheet of this page. */
3
4
  export declare const PEN_MODE: PenMode;
5
+ /** The zoom of every sheet of this page. */
6
+ export declare const PAGE_ZOOM: PageZoom;
@@ -1,7 +1,11 @@
1
- // The page's pen mode (R29): one for every sheet of the page, from a pen's first touch on any of
2
- // them till the page is loaded again. The next task is a new element (R20), and it stays. It is a
3
- // state of the interface in memory, not data: nothing is stored or sent. This module is no entry
4
- // point of the package.
1
+ // The page's pen mode (R29) and its zoom (R73): one of each for every sheet of the page, the mode
2
+ // from a pen's first touch on any of them, the zoom as the learner last set it, till the page is
3
+ // loaded again. The next task is a new element (R20), and both stay. They are states of the
4
+ // interface in memory, not data: nothing is stored or sent. This module is no entry point of the
5
+ // package.
5
6
  import { PenMode } from "../core/hands.js";
7
+ import { PageZoom } from "../core/zoom.js";
6
8
  /** The pen mode of every sheet of this page. */
7
9
  export const PEN_MODE = new PenMode();
10
+ /** The zoom of every sheet of this page. */
11
+ export const PAGE_ZOOM = new PageZoom();
@@ -5,13 +5,14 @@ import { ROW } from "./icons.js";
5
5
  export type Value = keyof typeof ROW;
6
6
  /**
7
7
  * What the bar asks of the element: a tool the learner chose — perhaps the one in use — an undo, a
8
- * clearing, and on a worked example the next part of its demo, or the whole demo hidden or shown
8
+ * clearing, a step of the zoom, larger or smaller (R73), and on a worked example the next part of its demo, or the whole demo hidden or shown
9
9
  * (R42).
10
10
  */
11
11
  export interface ToolActions {
12
12
  readonly choose: (tool: Tool) => void;
13
13
  readonly undo: () => void;
14
14
  readonly clear: () => void;
15
+ readonly zoom: (larger: boolean) => void;
15
16
  /** A task from paper's grid turned to the next (R88). */
16
17
  readonly turnGrid: () => void;
17
18
  readonly next: () => void;
@@ -33,8 +34,9 @@ export interface DemoControls {
33
34
  export type RowKind = "sheet" | "workbook" | "paper" | "example";
34
35
  /**
35
36
  * What the bar shows: what it is of, the tools the task allows — the hand besides, always —, the
36
- * one in use, whether undo and clearing have anything to do, and on a worked example whether Next
37
- * has a part to open and whether the demo is shown (R42).
37
+ * one in use, whether undo and clearing have anything to do, whether the zoom has a larger step
38
+ * and a smaller one (R73), and on a worked example whether Next has a part to open and whether the
39
+ * demo is shown (R42).
38
40
  */
39
41
  export interface ToolState {
40
42
  readonly kind: RowKind;
@@ -42,6 +44,8 @@ export interface ToolState {
42
44
  readonly tool: Tool;
43
45
  readonly undoable: boolean;
44
46
  readonly clearable: boolean;
47
+ readonly larger: boolean;
48
+ readonly smaller: boolean;
45
49
  readonly opens: boolean;
46
50
  readonly expanded: boolean;
47
51
  /** On a task from paper, whether its tracing paper is shown (R87), and its sheet's grid (R88). */
@@ -4,7 +4,9 @@
4
4
  // Authoring Practices have one: one stop of Tab, the arrow keys up and down from button to button,
5
5
  // and the tools a group of radio buttons that the arrows go through without choosing one. Undo and
6
6
  // Clear with nothing to take back or to clear stay where the focus can reach them, marked
7
- // unavailable: a last undo from the keyboard leaves the focus where it was. The sheet has no
7
+ // unavailable: a last undo from the keyboard leaves the focus where it was. After a line, Zoom in
8
+ // and Zoom out step the page's zoom of the sheet by quarters from half its fit to twice it, each
9
+ // unavailable at its end, as a pinch of two fingers sets it between (R73). The sheet has no
8
10
  // buttons that scroll it: the hand, two fingers or a finger in pen mode drag it, the wheel and the
9
11
  // keys scroll it (R84; R88; R90 spec). The buttons of the bar show an icon alone, of the set every
10
12
  // NovaTrail package of the interface draws from (R46; R78 spec): each is named by its label, in the
@@ -66,6 +68,10 @@ export class Tools {
66
68
  #hand = radioButton("hand");
67
69
  #undo = plainButton("undo");
68
70
  #clear = plainButton("clear");
71
+ /** The page's zoom of the sheet, after a line (R73). */
72
+ #beforeZoom = separator();
73
+ #zoomIn = plainButton("zoomIn");
74
+ #zoomOut = plainButton("zoomOut");
69
75
  /** A task from paper's grid, after a line (R88). */
70
76
  #beforeGrid = separator();
71
77
  #grid = plainButton("gridDots");
@@ -82,6 +88,8 @@ export class Tools {
82
88
  [this.#hand, "hand"],
83
89
  [this.#undo, "undo"],
84
90
  [this.#clear, "clear"],
91
+ [this.#zoomIn, "zoomIn"],
92
+ [this.#zoomOut, "zoomOut"],
85
93
  [this.#grid, "gridDots"],
86
94
  ];
87
95
  /**
@@ -152,7 +160,7 @@ export class Tools {
152
160
  this.#group.className = "choice";
153
161
  this.#group.setAttribute("role", "radiogroup");
154
162
  this.#group.append(this.#pen, this.#eraser, this.#hand);
155
- this.element.append(this.#task, this.#afterTask, this.#group, this.#undo, this.#clear, this.#beforeGrid, this.#grid);
163
+ this.element.append(this.#task, this.#afterTask, this.#group, this.#undo, this.#clear, this.#beforeZoom, this.#zoomIn, this.#zoomOut, this.#beforeGrid, this.#grid);
156
164
  this.rail.append(this.element, this.row, this.tip);
157
165
  for (const button of this.#buttons)
158
166
  button.classList.add("icon");
@@ -183,6 +191,8 @@ export class Tools {
183
191
  };
184
192
  doing(this.#undo, actions.undo);
185
193
  doing(this.#clear, actions.clear);
194
+ doing(this.#zoomIn, () => actions.zoom(true));
195
+ doing(this.#zoomOut, () => actions.zoom(false));
186
196
  doing(this.#next, actions.next);
187
197
  this.#demo.addEventListener("click", () => actions.toggle());
188
198
  this.#grid.addEventListener("click", () => actions.turnGrid());
@@ -231,7 +241,7 @@ export class Tools {
231
241
  * and fills its icon while the paper is shown (R87), and its grid's button shows the grid (R88).
232
242
  */
233
243
  show(state) {
234
- const { kind, tools, tool, undoable, clearable, opens, expanded, traced, grid } = state;
244
+ const { kind, tools, tool, undoable, clearable, larger, smaller, opens, expanded, traced, grid, } = state;
235
245
  if (kind !== this.#kind) {
236
246
  this.#kind = kind;
237
247
  this.#name();
@@ -251,6 +261,9 @@ export class Tools {
251
261
  [this.#hand, writes],
252
262
  [this.#undo, writes],
253
263
  [this.#clear, writes],
264
+ [this.#beforeZoom, writes],
265
+ [this.#zoomIn, writes],
266
+ [this.#zoomOut, writes],
254
267
  [this.#beforeGrid, kind === "paper"],
255
268
  [this.#grid, kind === "paper"],
256
269
  ]);
@@ -259,6 +272,8 @@ export class Tools {
259
272
  write(this.#hand, "aria-checked", String(tool === "hand"));
260
273
  write(this.#undo, "aria-disabled", undoable ? null : "true");
261
274
  write(this.#clear, "aria-disabled", clearable ? null : "true");
275
+ write(this.#zoomIn, "aria-disabled", larger ? null : "true");
276
+ write(this.#zoomOut, "aria-disabled", smaller ? null : "true");
262
277
  write(this.#next, "aria-disabled", opens ? null : "true");
263
278
  write(this.#demo, "aria-expanded", String(expanded));
264
279
  draw(this.#pen, "pen", tool === "pen");
@@ -20,6 +20,7 @@ import { grayOf, LARGEST, SCALE, scaleOf, sizeOf, TRACING, } from "../core/traci
20
20
  import { SVG, write } from "./attributes.js";
21
21
  import { ICONS, ROW } from "./icons.js";
22
22
  import { say } from "./labels.js";
23
+ import { lineOf } from "./notation.js";
23
24
  import { draw, plainButton } from "./tools.js";
24
25
  /** The grip's size, CSS pixels: a target of 68 × 44 (WCAG 2.5.5), the bar above the paper's edge. */
25
26
  const GRIP = Object.freeze({ width: 68, height: 44, over: 29 });
@@ -55,9 +56,9 @@ export class TracingPaper {
55
56
  #signWords = document.createElement("span");
56
57
  /** Over the ink: the bar of scale under the paper chosen. */
57
58
  bar = document.createElement("div");
58
- #smaller = plainButton("zoomOut");
59
+ #smaller = plainButton("smaller");
59
60
  #percent = document.createElement("button");
60
- #larger = plainButton("zoomIn");
61
+ #larger = plainButton("larger");
61
62
  #fit = plainButton("fit");
62
63
  #mode = plainButton("recognise");
63
64
  #moves;
@@ -71,6 +72,13 @@ export class TracingPaper {
71
72
  #asked = false;
72
73
  /** The statements drawn, and the size they were fitted to: drawn again only when either changes. */
73
74
  #drawn = "";
75
+ /** The letters' size the paper's height gives the statements, before they are fitted. */
76
+ #full = 0;
77
+ /**
78
+ * The lines' sizes, which change when they are first laid out — the paper hidden when they were
79
+ * drawn, or the font not yet loaded — and when they are fitted: each time they are fitted again.
80
+ */
81
+ #lines = new ResizeObserver(() => this.#fitText());
74
82
  /** The paper on the stage, CSS pixels, as last placed. */
75
83
  #box = { width: 0, height: 0 };
76
84
  /** A corner's drag under way: its pointer, where it began, the paper then and the corner. */
@@ -291,8 +299,8 @@ export class TracingPaper {
291
299
  write(part, "lang", lang);
292
300
  write(this.bar, "aria-label", labels.scaleButtons);
293
301
  for (const [button, words] of [
294
- [this.#smaller, labels.zoomOut],
295
- [this.#larger, labels.zoomIn],
302
+ [this.#smaller, labels.smaller],
303
+ [this.#larger, labels.larger],
296
304
  [this.#fit, labels.fit],
297
305
  ]) {
298
306
  const label = button.querySelector(".label");
@@ -375,6 +383,8 @@ export class TracingPaper {
375
383
  /** The element left the page: the crop's address goes, and a drag or a tooltip with it. */
376
384
  disconnect() {
377
385
  this.#connected = false;
386
+ this.#lines.disconnect();
387
+ this.#drawn = "";
378
388
  this.#release();
379
389
  this.#drag = undefined;
380
390
  this.grip.classList.remove("dragging");
@@ -613,8 +623,9 @@ export class TracingPaper {
613
623
  this.#signWords.textContent = sign;
614
624
  }
615
625
  /**
616
- * The statements in place of the crop, a line each, "□" a dashed box: their letters as large as
617
- * the paper's height shares out among them, and smaller where the longest line would not fit.
626
+ * The statements in place of the crop, a line each, "□" a dashed box and a fraction built up
627
+ * (./notation.ts): their letters as large as the paper's height shares out among them, and
628
+ * smaller where the longest line, or the lines with their fractions, would not fit.
618
629
  */
619
630
  #drawText() {
620
631
  const { width, height } = this.#box;
@@ -627,25 +638,41 @@ export class TracingPaper {
627
638
  this.#text.replaceChildren(...statements.map((statement) => {
628
639
  const line = document.createElement("div");
629
640
  line.className = "line";
630
- for (const [n, piece] of statement.split("□").entries()) {
631
- if (n > 0) {
632
- const box = document.createElement("span");
633
- box.className = "slot";
634
- line.append(box);
635
- }
636
- if (piece !== "")
637
- line.append(piece);
638
- }
641
+ line.append(...lineOf(statement));
639
642
  return line;
640
643
  }));
641
- const lines = Math.max(1, statements.length);
642
- let size = Math.min(height / (lines * 1.5), height * 0.55);
643
- this.#text.style.fontSize = `${size}px`;
644
- const wide = this.#text.scrollWidth;
645
- if (wide > width && wide > 0) {
646
- size *= (width * 0.92) / wide;
647
- this.#text.style.fontSize = `${size}px`;
648
- }
644
+ const count = Math.max(1, statements.length);
645
+ this.#full = Math.min(height / (count * 1.5), height * 0.55);
646
+ this.#text.style.fontSize = `${this.#full}px`;
647
+ this.#lines.disconnect();
648
+ for (const line of this.#text.children)
649
+ this.#lines.observe(line);
650
+ this.#fitText();
651
+ }
652
+ /**
653
+ * The statements' letters at the paper's full size, or smaller by as much as the widest line, or
654
+ * the lines together, exceed the paper. Measured at the size they have, so that once fitted they
655
+ * fit and their size stays: the observer of the lines, which calls this again, then stops.
656
+ */
657
+ #fitText() {
658
+ const { width, height } = this.#box;
659
+ // The lines as laid out, not the box's scroll: centred, they spill over both its edges.
660
+ const lines = [...this.#text.children];
661
+ const wide = Math.max(0, ...lines.map((line) => line.offsetWidth));
662
+ const first = lines[0];
663
+ const last = lines.at(-1);
664
+ const tall = first === undefined || last === undefined
665
+ ? 0
666
+ : last.offsetTop + last.offsetHeight - first.offsetTop;
667
+ // Not laid out: hidden, or out of the page. The observer calls again once they are.
668
+ if (wide === 0 || tall === 0)
669
+ return;
670
+ const size = Number.parseFloat(this.#text.style.fontSize) || this.#full;
671
+ const k = Math.min((width * 0.92) / wide, (height * 0.92) / tall);
672
+ const fitted = Math.min(this.#full, size * k);
673
+ // Within half a percent it stays: a size that moves by a rounding feeds the observer again.
674
+ if (Math.abs(fitted - size) > size * 0.005)
675
+ this.#text.style.fontSize = `${fitted}px`;
649
676
  }
650
677
  /** The tooltip on hover or the keyboard's focus, unless Escape or a drag dismissed it. */
651
678
  #showTip() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@altnovatrail/canvas",
3
- "version": "0.3.38",
3
+ "version": "0.3.39",
4
4
  "description": "NovaTrail's canvases: the workplace of a task, where the learner writes, draws and types.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -26,12 +26,12 @@
26
26
  "./lib/element/index.js"
27
27
  ],
28
28
  "dependencies": {
29
- "@altnovatrail/spec": "0.19.8",
29
+ "@altnovatrail/spec": "0.19.9",
30
30
  "perfect-freehand": "1.2.3",
31
31
  "rbush": "4.0.1"
32
32
  },
33
33
  "devDependencies": {
34
- "@altnovatrail/contracts": "0.19.8",
34
+ "@altnovatrail/contracts": "0.19.9",
35
35
  "@biomejs/biome": "2.6.6",
36
36
  "@bufbuild/protobuf": "2.17.0",
37
37
  "@iconify-json/material-symbols": "1.2.99",