@altnovatrail/canvas 0.3.37 → 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.
- package/lib/core/geometry.js +2 -2
- package/lib/core/grid.d.ts +11 -4
- package/lib/core/grid.js +33 -8
- package/lib/core/hands.d.ts +14 -7
- package/lib/core/hands.js +58 -7
- package/lib/core/history.js +3 -3
- package/lib/core/marks.d.ts +2 -0
- package/lib/core/marks.js +11 -6
- package/lib/core/notation.d.ts +29 -0
- package/lib/core/notation.js +180 -0
- package/lib/core/screen.d.ts +10 -11
- package/lib/core/screen.js +13 -14
- package/lib/core/sheet.d.ts +46 -3
- package/lib/core/sheet.js +46 -3
- package/lib/core/submission.js +5 -1
- package/lib/core/task.d.ts +9 -4
- package/lib/core/task.js +7 -2
- package/lib/core/tracing.d.ts +49 -13
- package/lib/core/tracing.js +110 -31
- package/lib/core/zoom.d.ts +19 -8
- package/lib/core/zoom.js +36 -15
- package/lib/element/icons.d.ts +90 -5
- package/lib/element/icons.js +94 -5
- package/lib/element/index.d.ts +32 -13
- package/lib/element/index.js +712 -240
- package/lib/element/labels.d.ts +30 -5
- package/lib/element/labels.js +32 -5
- package/lib/element/notation.d.ts +2 -0
- package/lib/element/notation.js +94 -0
- package/lib/element/session.js +4 -4
- package/lib/element/tools.d.ts +31 -16
- package/lib/element/tools.js +87 -54
- package/lib/element/tracing.d.ts +49 -5
- package/lib/element/tracing.js +446 -10
- package/package.json +3 -3
package/lib/core/geometry.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { REACH } from "./sheet.js";
|
|
2
2
|
/**
|
|
3
3
|
* A length or a place in whole tenths of a unit, as a sample file keeps them (R09), rounded half
|
|
4
4
|
* up: 100.05 units, 1000.5 tenths, are 1001. The groups and the crop count in them (R24, R25).
|
|
@@ -19,7 +19,7 @@ export function extentOf({ id, points }) {
|
|
|
19
19
|
let maxX = Number.NEGATIVE_INFINITY;
|
|
20
20
|
let maxY = Number.NEGATIVE_INFINITY;
|
|
21
21
|
for (const { x, y } of points) {
|
|
22
|
-
if (!onSheet(x,
|
|
22
|
+
if (!onSheet(x, REACH.width) || !onSheet(y, REACH.height)) {
|
|
23
23
|
throw new RangeError(`the stroke ${id} has a point (${x}, ${y}) off the sheet`);
|
|
24
24
|
}
|
|
25
25
|
minX = Math.min(minX, tenths(x));
|
package/lib/core/grid.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type Size } from "./sheet.js";
|
|
1
2
|
/** How a task rules the sheet: in squares, in lines or in dots. */
|
|
2
3
|
export type GridKind = "squares" | "lines" | "dots";
|
|
3
4
|
/** The grid of a sheet: its kind and its step, in sheet units. */
|
|
@@ -20,8 +21,14 @@ export declare const GRID_STEP_MAX = 500;
|
|
|
20
21
|
*/
|
|
21
22
|
export declare const DEFAULT_GRID: Grid;
|
|
22
23
|
/**
|
|
23
|
-
* A grid's path in SVG, in sheet units: a line along every ruling, strictly inside the sheet
|
|
24
|
-
*
|
|
25
|
-
* dot.
|
|
24
|
+
* A grid's path in SVG, in sheet units: a line along every ruling, strictly inside the sheet as
|
|
25
|
+
* large as `size` — the page, unless the sheet is laid out larger —, or, for dots, a dot where two
|
|
26
|
+
* rulings cross — a subpath of no length, which a round cap draws as a dot.
|
|
26
27
|
*/
|
|
27
|
-
export declare function gridPath(grid: Grid): string;
|
|
28
|
+
export declare function gridPath(grid: Grid, size?: Size): string;
|
|
29
|
+
/**
|
|
30
|
+
* A grid's path on a band of the sheet across its whole width, from `top` to `bottom` in units,
|
|
31
|
+
* either of them past the sheet: its rulings at the step's multiples within the band, both ends
|
|
32
|
+
* taken, so that they go on from the sheet's own above or below it (R73).
|
|
33
|
+
*/
|
|
34
|
+
export declare function gridBand(grid: Grid, width: number, top: number, bottom: number): string;
|
package/lib/core/grid.js
CHANGED
|
@@ -14,15 +14,15 @@ export const GRID_STEP_MAX = 500;
|
|
|
14
14
|
*/
|
|
15
15
|
export const DEFAULT_GRID = Object.freeze({ kind: "squares", step: GRID_STEP });
|
|
16
16
|
/**
|
|
17
|
-
* A grid's path in SVG, in sheet units: a line along every ruling, strictly inside the sheet
|
|
18
|
-
*
|
|
19
|
-
* dot.
|
|
17
|
+
* A grid's path in SVG, in sheet units: a line along every ruling, strictly inside the sheet as
|
|
18
|
+
* large as `size` — the page, unless the sheet is laid out larger —, or, for dots, a dot where two
|
|
19
|
+
* rulings cross — a subpath of no length, which a round cap draws as a dot.
|
|
20
20
|
*/
|
|
21
|
-
export function gridPath(grid) {
|
|
22
|
-
const xs = rulings(grid.step,
|
|
23
|
-
const ys = rulings(grid.step,
|
|
24
|
-
const down = xs.map((x) => `M${x} 0V${
|
|
25
|
-
const across = ys.map((y) => `M0 ${y}H${
|
|
21
|
+
export function gridPath(grid, size = SHEET) {
|
|
22
|
+
const xs = rulings(grid.step, size.width);
|
|
23
|
+
const ys = rulings(grid.step, size.height);
|
|
24
|
+
const down = xs.map((x) => `M${x} 0V${size.height}`);
|
|
25
|
+
const across = ys.map((y) => `M0 ${y}H${size.width}`);
|
|
26
26
|
switch (grid.kind) {
|
|
27
27
|
case "squares":
|
|
28
28
|
return [...down, ...across].join("");
|
|
@@ -45,3 +45,28 @@ function rulings(step, length) {
|
|
|
45
45
|
places.push(place);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* A grid's path on a band of the sheet across its whole width, from `top` to `bottom` in units,
|
|
50
|
+
* either of them past the sheet: its rulings at the step's multiples within the band, both ends
|
|
51
|
+
* taken, so that they go on from the sheet's own above or below it (R73).
|
|
52
|
+
*/
|
|
53
|
+
export function gridBand(grid, width, top, bottom) {
|
|
54
|
+
const xs = rulings(grid.step, width);
|
|
55
|
+
const ys = [];
|
|
56
|
+
for (let i = Math.ceil(top / grid.step - 1e-9);; i++) {
|
|
57
|
+
const place = Math.round(i * grid.step * 1000) / 1000;
|
|
58
|
+
if (place > bottom)
|
|
59
|
+
break;
|
|
60
|
+
ys.push(place);
|
|
61
|
+
}
|
|
62
|
+
const down = xs.map((x) => `M${x} ${top}V${bottom}`);
|
|
63
|
+
const across = ys.map((y) => `M0 ${y}H${width}`);
|
|
64
|
+
switch (grid.kind) {
|
|
65
|
+
case "squares":
|
|
66
|
+
return [...down, ...across].join("");
|
|
67
|
+
case "lines":
|
|
68
|
+
return across.join("");
|
|
69
|
+
case "dots":
|
|
70
|
+
return ys.flatMap((y) => xs.map((x) => `M${x} ${y}h0`)).join("");
|
|
71
|
+
}
|
|
72
|
+
}
|
package/lib/core/hands.d.ts
CHANGED
|
@@ -41,17 +41,17 @@ export declare const SLOP_PX = 8;
|
|
|
41
41
|
/**
|
|
42
42
|
* Two fingers that have gone this many times further along one axis than along the other, when
|
|
43
43
|
* they pass the slop, scroll along that axis alone for the rest of the gesture (R73): a scroll
|
|
44
|
-
* down a
|
|
44
|
+
* down a sheet does not wander sideways, nor one across it up and down. Between, they go
|
|
45
45
|
* both ways.
|
|
46
46
|
*/
|
|
47
47
|
export declare const RAIL = 2;
|
|
48
48
|
/**
|
|
49
|
-
* The pen mode (R29): on from a pen's touch on a sheet,
|
|
50
|
-
*
|
|
49
|
+
* The pen mode (R29): on from a pen's touch on a sheet, till the page goes, with no button that
|
|
50
|
+
* turns it off: while it is on, a finger neither writes nor erases, and moves the sheet (R84). One is shared by every sheet of a page, and
|
|
51
51
|
* it lives in memory alone: nothing is stored or sent, and a new page starts without it.
|
|
52
52
|
*
|
|
53
|
-
* It knows, too, whether any sheet of the page is written on
|
|
54
|
-
*
|
|
53
|
+
* It knows, too, whether any sheet of the page is written on, and tells its sheets when the last
|
|
54
|
+
* stroke ends: each bar shows the mode as it is then.
|
|
55
55
|
*/
|
|
56
56
|
export declare class PenMode {
|
|
57
57
|
#private;
|
|
@@ -82,8 +82,13 @@ export interface HandsSheet {
|
|
|
82
82
|
*/
|
|
83
83
|
scroll(dx: number, dy: number, fresh: boolean): void;
|
|
84
84
|
/**
|
|
85
|
-
*
|
|
86
|
-
*
|
|
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;
|
|
89
|
+
/**
|
|
90
|
+
* Whether the sheet scrolls across at all now: one wider than its container does (R73). A
|
|
91
|
+
* gesture on a sheet that does not goes by its travel down alone.
|
|
87
92
|
*/
|
|
88
93
|
across(): boolean;
|
|
89
94
|
}
|
|
@@ -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
|
@@ -18,17 +18,17 @@ export const SLOP_PX = 8;
|
|
|
18
18
|
/**
|
|
19
19
|
* Two fingers that have gone this many times further along one axis than along the other, when
|
|
20
20
|
* they pass the slop, scroll along that axis alone for the rest of the gesture (R73): a scroll
|
|
21
|
-
* down a
|
|
21
|
+
* down a sheet does not wander sideways, nor one across it up and down. Between, they go
|
|
22
22
|
* both ways.
|
|
23
23
|
*/
|
|
24
24
|
export const RAIL = 2;
|
|
25
25
|
/**
|
|
26
|
-
* The pen mode (R29): on from a pen's touch on a sheet,
|
|
27
|
-
*
|
|
26
|
+
* The pen mode (R29): on from a pen's touch on a sheet, till the page goes, with no button that
|
|
27
|
+
* turns it off: while it is on, a finger neither writes nor erases, and moves the sheet (R84). One is shared by every sheet of a page, and
|
|
28
28
|
* it lives in memory alone: nothing is stored or sent, and a new page starts without it.
|
|
29
29
|
*
|
|
30
|
-
* It knows, too, whether any sheet of the page is written on
|
|
31
|
-
*
|
|
30
|
+
* It knows, too, whether any sheet of the page is written on, and tells its sheets when the last
|
|
31
|
+
* stroke ends: each bar shows the mode as it is then.
|
|
32
32
|
*/
|
|
33
33
|
export class PenMode {
|
|
34
34
|
#on = false;
|
|
@@ -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) {
|
package/lib/core/history.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { record, shown } from "./fields.js";
|
|
2
|
-
import {
|
|
2
|
+
import { REACH } from "./sheet.js";
|
|
3
3
|
/**
|
|
4
4
|
* The strokes a history leaves on the sheet, in the order they were written: what the sheet shows
|
|
5
5
|
* after it (R09).
|
|
@@ -194,8 +194,8 @@ function strokeOf(value, path) {
|
|
|
194
194
|
const points = Array.from(fields.points, (item, n) => {
|
|
195
195
|
const at = `${path}.points[${n}]`;
|
|
196
196
|
const point = record(item, at);
|
|
197
|
-
const x = within(point.x, `${at}.x`, 0,
|
|
198
|
-
const y = within(point.y, `${at}.y`, 0,
|
|
197
|
+
const x = within(point.x, `${at}.x`, 0, REACH.width);
|
|
198
|
+
const y = within(point.y, `${at}.y`, 0, REACH.height);
|
|
199
199
|
const t = timeOf(point.t, `${at}.t`);
|
|
200
200
|
if (t < latest) {
|
|
201
201
|
throw new TypeError(`${at}.t: ${t}, earlier than the point before it, at ${latest}`);
|
package/lib/core/marks.d.ts
CHANGED
|
@@ -56,6 +56,8 @@ export type { Anchor, CellPlace, CropNumbers, Mark, Marks, PaperSubmission, Shee
|
|
|
56
56
|
*/
|
|
57
57
|
export interface Paper {
|
|
58
58
|
readonly at: readonly [number, number];
|
|
59
|
+
/** The tracing paper's scale (R89). */
|
|
60
|
+
readonly scale: number;
|
|
59
61
|
readonly onTracing: (id: number) => boolean;
|
|
60
62
|
}
|
|
61
63
|
/**
|
package/lib/core/marks.js
CHANGED
|
@@ -2,7 +2,7 @@ import { CROP, sheetBoxOf } from "./crop.js";
|
|
|
2
2
|
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
|
-
import {
|
|
5
|
+
import { REACH } from "./sheet.js";
|
|
6
6
|
import { tracingFieldsOf } from "./tracing.js";
|
|
7
7
|
/**
|
|
8
8
|
* The rule the sheet snaps by (R26): a quarter of a digit — 6 px at 24, a quarter of H on the
|
|
@@ -112,6 +112,9 @@ export function placesOf(marks, known, cells = null, rule = SNAP_RULE, paper) {
|
|
|
112
112
|
if (traced.at[0] !== paper.at[0] || traced.at[1] !== paper.at[1]) {
|
|
113
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
114
|
}
|
|
115
|
+
if ((traced.scale ?? 1) !== paper.scale) {
|
|
116
|
+
throw new TypeError(`marks.submission.tracing.scale: ${shown(traced.scale ?? 1)}, and the tracing paper's scale is ${shown(paper.scale)}: the submission's strokes lie elsewhere`);
|
|
117
|
+
}
|
|
115
118
|
}
|
|
116
119
|
const { crop, groups, digest } = submission;
|
|
117
120
|
const sent = [];
|
|
@@ -245,7 +248,9 @@ export function digestOf(strokes, crop, groups, paper) {
|
|
|
245
248
|
.map(({ id }) => id)
|
|
246
249
|
.filter((id) => paper.onTracing(id))
|
|
247
250
|
.sort(byNumber);
|
|
248
|
-
|
|
251
|
+
// A scale of 1 adds nothing, so a digest made before scales came stays the same (R89).
|
|
252
|
+
const scale = paper.scale === 1 ? "" : ` ${paper.scale}`;
|
|
253
|
+
lines.push(`tracing ${tenths(paper.at[0])} ${tenths(paper.at[1])} ${traced.join(",")}${scale}`);
|
|
249
254
|
}
|
|
250
255
|
return sumOf(lines);
|
|
251
256
|
}
|
|
@@ -461,10 +466,10 @@ function numbersOf(value, path, groups) {
|
|
|
461
466
|
const exact = record(crop.exact, `${path}.exact`);
|
|
462
467
|
const median = whole(exact.median, `${path}.exact.median`, CROP.flat * 10, CROP.ceiling * 10);
|
|
463
468
|
const points = record(exact.box, `${path}.exact.box`);
|
|
464
|
-
const minX = whole(points.minX, `${path}.exact.box.minX`, 0,
|
|
465
|
-
const minY = whole(points.minY, `${path}.exact.box.minY`, 0,
|
|
466
|
-
const maxX = whole(points.maxX, `${path}.exact.box.maxX`, minX,
|
|
467
|
-
const maxY = whole(points.maxY, `${path}.exact.box.maxY`, minY,
|
|
469
|
+
const minX = whole(points.minX, `${path}.exact.box.minX`, 0, REACH.width * 10);
|
|
470
|
+
const minY = whole(points.minY, `${path}.exact.box.minY`, 0, REACH.height * 10);
|
|
471
|
+
const maxX = whole(points.maxX, `${path}.exact.box.maxX`, minX, REACH.width * 10);
|
|
472
|
+
const maxY = whole(points.maxY, `${path}.exact.box.maxY`, minY, REACH.height * 10);
|
|
468
473
|
const numbers = {
|
|
469
474
|
drawn,
|
|
470
475
|
width,
|
|
@@ -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
|
+
}
|
package/lib/core/screen.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Where the sheet is on the screen, one view for every layer (R06): `k` CSS pixels to a logical
|
|
3
|
-
* unit, for the
|
|
4
|
-
*
|
|
3
|
+
* unit, for the page fits the width of its area, and `vx` and `vy`, the left and the top of the
|
|
4
|
+
* sheet's visible part, in units.
|
|
5
5
|
*/
|
|
6
6
|
export interface View {
|
|
7
7
|
readonly k: number;
|
|
@@ -14,19 +14,18 @@ export interface Point {
|
|
|
14
14
|
readonly y: number;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* The most CSS pixels to a unit
|
|
18
|
-
*
|
|
19
|
-
* The page's zoom takes it beyond that, to twice the fit (R73).
|
|
17
|
+
* The most CSS pixels to a unit: on a wide screen the page is 1000 CSS pixels wide, a pixel to a
|
|
18
|
+
* unit as on the picture of the work, at the sheet's left, and the sheet goes on past it (R23).
|
|
20
19
|
*/
|
|
21
20
|
export declare const K_MAX = 1;
|
|
22
|
-
/** `k` of a sheet `width` CSS pixels wide as it is on the screen: k = A / W. */
|
|
23
|
-
export declare function scaleFor(width: number): number;
|
|
21
|
+
/** `k` of a sheet `width` CSS pixels wide as it is on the screen, `across` units wide: k = A / W. */
|
|
22
|
+
export declare function scaleFor(width: number, across?: number): number;
|
|
24
23
|
/**
|
|
25
|
-
* `k` for a sheet laid out in an area `width` CSS pixels wide:
|
|
26
|
-
*
|
|
27
|
-
*
|
|
24
|
+
* `k` for a sheet laid out in an area `width` CSS pixels wide: the page fits the width, up to
|
|
25
|
+
* `K_MAX`. This is the layout's `k`, before any transform the page puts on the element; input takes
|
|
26
|
+
* `k` from the sheet's rectangle on the screen, by `scaleFor`.
|
|
28
27
|
*/
|
|
29
|
-
export declare function fitScale(width: number
|
|
28
|
+
export declare function fitScale(width: number): number;
|
|
30
29
|
/**
|
|
31
30
|
* The point of the sheet under a point of the screen, given in CSS pixels from the top left corner
|
|
32
31
|
* of the sheet's visible part. A point beyond the sheet is clamped to its edge: a pointer captured
|
package/lib/core/screen.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
import { SHEET } from "./sheet.js";
|
|
1
|
+
import { REACH, SHEET } from "./sheet.js";
|
|
2
2
|
/**
|
|
3
|
-
* The most CSS pixels to a unit
|
|
4
|
-
*
|
|
5
|
-
* The page's zoom takes it beyond that, to twice the fit (R73).
|
|
3
|
+
* The most CSS pixels to a unit: on a wide screen the page is 1000 CSS pixels wide, a pixel to a
|
|
4
|
+
* unit as on the picture of the work, at the sheet's left, and the sheet goes on past it (R23).
|
|
6
5
|
*/
|
|
7
6
|
export const K_MAX = 1;
|
|
8
|
-
/** `k` of a sheet `width` CSS pixels wide as it is on the screen: k = A / W. */
|
|
9
|
-
export function scaleFor(width) {
|
|
10
|
-
return width /
|
|
7
|
+
/** `k` of a sheet `width` CSS pixels wide as it is on the screen, `across` units wide: k = A / W. */
|
|
8
|
+
export function scaleFor(width, across = SHEET.width) {
|
|
9
|
+
return width / across;
|
|
11
10
|
}
|
|
12
11
|
/**
|
|
13
|
-
* `k` for a sheet laid out in an area `width` CSS pixels wide:
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* `k` for a sheet laid out in an area `width` CSS pixels wide: the page fits the width, up to
|
|
13
|
+
* `K_MAX`. This is the layout's `k`, before any transform the page puts on the element; input takes
|
|
14
|
+
* `k` from the sheet's rectangle on the screen, by `scaleFor`.
|
|
16
15
|
*/
|
|
17
|
-
export function fitScale(width
|
|
18
|
-
return Math.min(width / SHEET.width, K_MAX)
|
|
16
|
+
export function fitScale(width) {
|
|
17
|
+
return Math.min(width / SHEET.width, K_MAX);
|
|
19
18
|
}
|
|
20
19
|
/**
|
|
21
20
|
* The point of the sheet under a point of the screen, given in CSS pixels from the top left corner
|
|
@@ -24,8 +23,8 @@ export function fitScale(width, zoom = 1) {
|
|
|
24
23
|
*/
|
|
25
24
|
export function toSheet(view, x, y) {
|
|
26
25
|
return {
|
|
27
|
-
x: clamp(x / view.k + view.vx, 0,
|
|
28
|
-
y: clamp(y / view.k + view.vy, 0,
|
|
26
|
+
x: clamp(x / view.k + view.vx, 0, REACH.width),
|
|
27
|
+
y: clamp(y / view.k + view.vy, 0, REACH.height),
|
|
29
28
|
};
|
|
30
29
|
}
|
|
31
30
|
/** The point of the screen over a point of the sheet, from the top left corner of its visible part. */
|