@altnovatrail/canvas 0.3.42 → 0.3.43

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/png.js CHANGED
@@ -1,13 +1,14 @@
1
1
  /**
2
- * The PNG of a grey image, a byte to a pixel, row by row from the top: own code, so that the
3
- * same pixels give the same bytes in every browser and in Node, as no browser's canvas promises.
2
+ * The PNG of an image row by row from the top, grey, a byte to a pixel, or RGB, three bytes to a
3
+ * pixel, as `channels` says: own code, so that the same pixels give the same bytes in every browser
4
+ * and in Node, as no browser's canvas promises.
4
5
  * The file is the least PNG holds: its signature, the header, one chunk of data and the end,
5
6
  * no ancillary chunk, for a decoder may ignore them (PNG Third Edition, 11.3). The data is a zlib
6
7
  * stream of one deflate block with the fixed Huffman codes (RFC 1950, RFC 1951): every row
7
8
  * unfiltered, and a repeat looked for in two places only — the byte before, which takes the runs of
8
9
  * white, and the row above, which takes the rows a stroke runs straight down through.
9
10
  */
10
- export function encodePNG(gray, width, height) {
11
+ export function encodePNG(pixels, width, height, channels = 1) {
11
12
  for (const [name, side] of [
12
13
  ["width", width],
13
14
  ["height", height],
@@ -16,22 +17,26 @@ export function encodePNG(gray, width, height) {
16
17
  throw new RangeError(`the image's ${name} is ${side}, not a whole number from 1 to ${MOST}`);
17
18
  }
18
19
  }
19
- if (gray.length !== width * height) {
20
- throw new RangeError(`${gray.length} pixels are no image ${width} × ${height}`);
20
+ if (channels !== 1 && channels !== 3) {
21
+ throw new RangeError(`an image of ${channels} channels: grey has 1, RGB 3`);
22
+ }
23
+ const across = width * channels;
24
+ if (pixels.length !== across * height) {
25
+ throw new RangeError(`${pixels.length} bytes are no image ${width} × ${height} of ${channels} channels`);
21
26
  }
22
27
  // Each row after its filter, 0: none.
23
- const stride = width + 1;
28
+ const stride = across + 1;
24
29
  const rows = new Uint8Array(stride * height);
25
30
  for (let row = 0; row < height; row++) {
26
- rows.set(gray.subarray(row * width, (row + 1) * width), row * stride + 1);
31
+ rows.set(pixels.subarray(row * across, (row + 1) * across), row * stride + 1);
27
32
  }
28
33
  const header = new Uint8Array(13);
29
34
  const view = new DataView(header.buffer);
30
35
  view.setUint32(0, width);
31
36
  view.setUint32(4, height);
32
- // A depth of 8 bits, the colour type of grey, and the one method of compression, of filtering
33
- // and of no interlacing there is.
34
- header.set([8, 0, 0, 0, 0], 8);
37
+ // A depth of 8 bits, the colour type of grey, 0, or of RGB, 2, and the one method of compression,
38
+ // of filtering and of no interlacing there is.
39
+ header.set([8, channels === 1 ? 0 : 2, 0, 0, 0], 8);
35
40
  const stream = deflate(rows, stride);
36
41
  const zlib = new Uint8Array(2 + stream.length + 4);
37
42
  // The deflate method with a window of 32 KiB, and the fastest algorithm; FCHECK makes 0x7801 a
@@ -1,14 +1,25 @@
1
- import type { Crop } from "./crop.js";
1
+ import { type Crop } from "./crop.js";
2
2
  import type { Stroke } from "./ink.js";
3
3
  /**
4
- * The pixels of a crop: grey, a byte each, row by row from the top, white 255 and the ink 0,
5
- * of the strokes the crop was made of. Every segment of a stroke is a line as wide all along, with
6
- * round ends, and a stroke of one point a dot as wide as the line: a pixel takes the share of it
7
- * that the line covers, along a ramp a pixel wide at the line's edge, and the darkest of what
8
- * covers it. The places are the crop's own whole numbers, from the points in tenths of a unit, as
9
- * a sample keeps them. Only adding, subtracting, multiplying, dividing and Math.sqrt go into
10
- * a pixel, which ECMAScript rounds alike in every engine, so the bytes are the same in each.
4
+ * How many bytes a pixel of a crop of these strokes takes: 1, grey, when none of them has a
5
+ * style, and 3, RGB, when one has.
6
+ */
7
+ export declare function channelsOf(strokes: readonly Stroke[]): 1 | 3;
8
+ /**
9
+ * The pixels of a crop, row by row from the top, of the strokes the crop was made of: grey, a
10
+ * byte each, white 255 and the ink 0, when no stroke has a style, and RGB, three bytes each, when
11
+ * one has (`channelsOf`). Every segment of a stroke is a line as wide all along, with round ends,
12
+ * and a stroke of one point a dot as wide as the line: a pixel takes the share of it that the line
13
+ * covers, along a ramp a pixel wide at the line's edge. In grey a pixel is the darkest of what
14
+ * covers it. In RGB the strokes are laid on white by their IDs ascending, each as one layer — what
15
+ * its segments cover of a pixel is the most any of them does, so where it crosses itself it is no
16
+ * darker —, its line its width at the crop's scale and 1 px at least: a stroke without a style
17
+ * black and opaque; of the kind pen or liner its colour at its opacity over what lies under it;
18
+ * of the kind marker what lies under it multiplied by its colour, at its opacity. The places are
19
+ * the crop's own whole numbers, from the points in tenths of a unit, as a sample keeps them. Only
20
+ * adding, subtracting, multiplying, dividing, Math.sqrt and Math.round go into a pixel, which
21
+ * ECMAScript rounds alike in every engine, so the bytes are the same in each.
11
22
  */
12
23
  export declare function rasterOf(strokes: readonly Stroke[], crop: Crop): Uint8Array<ArrayBuffer>;
13
- /** The PNG of a crop: its pixels, encoded. */
24
+ /** The PNG of a crop: its pixels, encoded, grey or RGB as they are. */
14
25
  export declare function pictureOf(strokes: readonly Stroke[], crop: Crop): Uint8Array<ArrayBuffer>;
@@ -1,47 +1,137 @@
1
+ import { inkOf, widthOf } from "./crop.js";
1
2
  import { tenths } from "./geometry.js";
2
3
  import { encodePNG } from "./png.js";
3
4
  /**
4
- * The pixels of a crop: grey, a byte each, row by row from the top, white 255 and the ink 0,
5
- * of the strokes the crop was made of. Every segment of a stroke is a line as wide all along, with
6
- * round ends, and a stroke of one point a dot as wide as the line: a pixel takes the share of it
7
- * that the line covers, along a ramp a pixel wide at the line's edge, and the darkest of what
8
- * covers it. The places are the crop's own whole numbers, from the points in tenths of a unit, as
9
- * a sample keeps them. Only adding, subtracting, multiplying, dividing and Math.sqrt go into
10
- * a pixel, which ECMAScript rounds alike in every engine, so the bytes are the same in each.
5
+ * How many bytes a pixel of a crop of these strokes takes: 1, grey, when none of them has a
6
+ * style, and 3, RGB, when one has.
7
+ */
8
+ export function channelsOf(strokes) {
9
+ return strokes.some((stroke) => stroke.style !== undefined) ? 3 : 1;
10
+ }
11
+ /**
12
+ * The pixels of a crop, row by row from the top, of the strokes the crop was made of: grey, a
13
+ * byte each, white 255 and the ink 0, when no stroke has a style, and RGB, three bytes each, when
14
+ * one has (`channelsOf`). Every segment of a stroke is a line as wide all along, with round ends,
15
+ * and a stroke of one point a dot as wide as the line: a pixel takes the share of it that the line
16
+ * covers, along a ramp a pixel wide at the line's edge. In grey a pixel is the darkest of what
17
+ * covers it. In RGB the strokes are laid on white by their IDs ascending, each as one layer — what
18
+ * its segments cover of a pixel is the most any of them does, so where it crosses itself it is no
19
+ * darker —, its line its width at the crop's scale and 1 px at least: a stroke without a style
20
+ * black and opaque; of the kind pen or liner its colour at its opacity over what lies under it;
21
+ * of the kind marker what lies under it multiplied by its colour, at its opacity. The places are
22
+ * the crop's own whole numbers, from the points in tenths of a unit, as a sample keeps them. Only
23
+ * adding, subtracting, multiplying, dividing, Math.sqrt and Math.round go into a pixel, which
24
+ * ECMAScript rounds alike in every engine, so the bytes are the same in each.
11
25
  */
12
26
  export function rasterOf(strokes, crop) {
13
- const { width, height, drawn } = crop;
27
+ return channelsOf(strokes) === 1 ? grayOf(strokes, crop) : colourOf(strokes, crop);
28
+ }
29
+ /** The PNG of a crop: its pixels, encoded, grey or RGB as they are. */
30
+ export function pictureOf(strokes, crop) {
31
+ return encodePNG(rasterOf(strokes, crop), crop.width, crop.height, channelsOf(strokes));
32
+ }
33
+ /** A point's place on the crop: a whole number over the median, one division, as the crop's own. */
34
+ function spotsOf(stroke, crop) {
14
35
  const [top, left] = crop.margins;
15
36
  const { box, median: unit } = crop.exact;
16
- const gray = new Uint8Array(width * height).fill(255);
17
- const reach = Math.max(drawn, 12) / 24;
18
- // A whole number over the median: one division, as the crop's own numbers are.
19
- const spot = ({ x, y }) => ({
37
+ const { drawn } = crop;
38
+ return stroke.points.map(({ x, y }) => ({
20
39
  x: ((tenths(x) - box.minX) * drawn + left * unit) / unit,
21
40
  y: ((tenths(y) - box.minY) * drawn + top * unit) / unit,
22
- });
23
- for (const { points } of strokes) {
24
- const spots = points.map(spot);
25
- const [first] = spots;
26
- if (spots.length === 1 && first !== undefined)
27
- paint(gray, width, height, first, first, reach);
28
- for (let k = 1; k < spots.length; k++) {
29
- const [a, b] = [spots[k - 1], spots[k]];
30
- if (a !== undefined && b !== undefined)
31
- paint(gray, width, height, a, b, reach);
32
- }
41
+ }));
42
+ }
43
+ /** Each segment of a stroke on the crop, by its ends: a stroke of one point is one of no length. */
44
+ function eachSegment(spots, each) {
45
+ const [first] = spots;
46
+ if (spots.length === 1 && first !== undefined)
47
+ each(first, first);
48
+ for (let k = 1; k < spots.length; k++) {
49
+ const [a, b] = [spots[k - 1], spots[k]];
50
+ if (a !== undefined && b !== undefined)
51
+ each(a, b);
52
+ }
53
+ }
54
+ function grayOf(strokes, crop) {
55
+ const { width, height, drawn } = crop;
56
+ const gray = new Uint8Array(width * height).fill(255);
57
+ const reach = Math.max(drawn, 12) / 24;
58
+ const darken = (at, cover) => {
59
+ const value = Math.round(255 * (1 - Math.min(1, cover)));
60
+ if (value < (gray[at] ?? 255))
61
+ gray[at] = value;
62
+ };
63
+ for (const stroke of strokes) {
64
+ eachSegment(spotsOf(stroke, crop), (a, b) => paint(width, height, a, b, reach, darken));
33
65
  }
34
66
  return gray;
35
67
  }
36
- /** The PNG of a crop: its pixels, encoded. */
37
- export function pictureOf(strokes, crop) {
38
- return encodePNG(rasterOf(strokes, crop), crop.width, crop.height);
68
+ function colourOf(strokes, crop) {
69
+ const { width, height, drawn } = crop;
70
+ const unit = crop.exact.median;
71
+ const rgb = new Uint8Array(3 * width * height).fill(255);
72
+ // What the stroke being laid covers of each pixel, cleared after it within its own box.
73
+ const covered = new Float64Array(width * height);
74
+ const cover = (at, share) => {
75
+ const own = Math.min(1, share);
76
+ if (own > (covered[at] ?? 0))
77
+ covered[at] = own;
78
+ };
79
+ for (const stroke of [...strokes].sort((a, b) => a.id - b.id)) {
80
+ const reach = inkOf(widthOf(stroke), drawn, unit) / (24 * unit);
81
+ const spots = spotsOf(stroke, crop);
82
+ eachSegment(spots, (a, b) => paint(width, height, a, b, reach, cover));
83
+ const { style } = stroke;
84
+ const opacity = style === undefined ? 1 : style.opacity / 100;
85
+ const [red, green, blue] = style === undefined ? [0, 0, 0] : coloursOf(style.color);
86
+ const multiply = style?.pen === "marker";
87
+ const lay = (under, colour, share) => Math.round(under * (1 - share) + (multiply ? (under * colour) / 255 : colour) * share);
88
+ const box = boxOf(spots, reach, width, height);
89
+ for (let row = box.top; row <= box.bottom; row++) {
90
+ for (let column = box.left; column <= box.right; column++) {
91
+ const at = row * width + column;
92
+ const share = covered[at] ?? 0;
93
+ if (share === 0)
94
+ continue;
95
+ covered[at] = 0;
96
+ const a = opacity * share;
97
+ rgb[3 * at] = lay(rgb[3 * at] ?? 255, red, a);
98
+ rgb[3 * at + 1] = lay(rgb[3 * at + 1] ?? 255, green, a);
99
+ rgb[3 * at + 2] = lay(rgb[3 * at + 2] ?? 255, blue, a);
100
+ }
101
+ }
102
+ }
103
+ return rgb;
104
+ }
105
+ /** A colour `#rrggbb` as its three channels. */
106
+ function coloursOf(colour) {
107
+ const channel = (at) => Number.parseInt(colour.slice(at, at + 2), 16);
108
+ return [channel(1), channel(3), channel(5)];
109
+ }
110
+ /** The pixels a stroke's line may shade, within the crop: as far as `paint` looks. */
111
+ function boxOf(spots, reach, width, height) {
112
+ let left = Number.POSITIVE_INFINITY;
113
+ let top = Number.POSITIVE_INFINITY;
114
+ let right = Number.NEGATIVE_INFINITY;
115
+ let bottom = Number.NEGATIVE_INFINITY;
116
+ for (const { x, y } of spots) {
117
+ left = Math.min(left, x);
118
+ top = Math.min(top, y);
119
+ right = Math.max(right, x);
120
+ bottom = Math.max(bottom, y);
121
+ }
122
+ return {
123
+ left: Math.max(0, Math.floor(left - reach - 1)),
124
+ top: Math.max(0, Math.floor(top - reach - 1)),
125
+ right: Math.min(width - 1, Math.ceil(right + reach + 1)),
126
+ bottom: Math.min(height - 1, Math.ceil(bottom + reach + 1)),
127
+ };
39
128
  }
40
129
  /**
41
- * One segment's ink on the pixels near it. Its ends go in one order whichever way it was written,
42
- * so a stroke drawn back to front gives the same bytes.
130
+ * One segment's ink on the pixels near it: `take` gets each pixel it covers, by its place, with
131
+ * how much of it the line covers, above 0 and 1 or more within the line. Its ends go in one order
132
+ * whichever way it was written, so a stroke drawn back to front gives the same bytes.
43
133
  */
44
- function paint(gray, width, height, from, to, reach) {
134
+ function paint(width, height, from, to, reach, take) {
45
135
  const forward = from.x < to.x || (from.x === to.x && from.y <= to.y);
46
136
  const a = forward ? from : to;
47
137
  const b = forward ? to : from;
@@ -61,12 +151,8 @@ function paint(gray, width, height, from, to, reach) {
61
151
  const ex = cx - (a.x + along * dx);
62
152
  const ey = cy - (a.y + along * dy);
63
153
  const cover = reach + 0.5 - Math.sqrt(ex * ex + ey * ey);
64
- if (cover <= 0)
65
- continue;
66
- const value = Math.round(255 * (1 - Math.min(1, cover)));
67
- const at = row * width + column;
68
- if (value < (gray[at] ?? 255))
69
- gray[at] = value;
154
+ if (cover > 0)
155
+ take(row * width + column, cover);
70
156
  }
71
157
  }
72
158
  }
@@ -1,5 +1,6 @@
1
1
  import { Clock } from "./clock.js";
2
2
  import type { Stroke } from "./ink.js";
3
+ import type { StrokeStyle } from "./style.js";
3
4
  /**
4
5
  * What the recorder takes of a pointer event, and of each event coalesced into it. The DOM-free
5
6
  * part sees no DOM, so the element hands it this much, the place already in sheet units.
@@ -45,8 +46,11 @@ export declare class Recorder {
45
46
  get live(): Stroke | undefined;
46
47
  /** The `pointerId` of the pointer writing, or nothing between strokes. */
47
48
  get pointer(): number | undefined;
48
- /** `pointerdown`: a stroke begins here, if none is under way and the primary button went down. */
49
- down(sample: PointerSample): boolean;
49
+ /**
50
+ * `pointerdown`: a stroke begins here, if none is under way and the primary button went down,
51
+ * in the style given, which it keeps; without one it has none.
52
+ */
53
+ down(sample: PointerSample, style?: StrokeStyle): boolean;
50
54
  /**
51
55
  * `pointermove`, once for each coalesced event: the stroke goes on here. A move without the
52
56
  * primary button held ends the stroke where it was, its `pointerup` lost.
@@ -70,4 +74,11 @@ export declare class Recorder {
70
74
  * `pointercancel` — its ID and its times are the last sheet's — and the next stroke is `nextId`.
71
75
  */
72
76
  restart(nextId: number): void;
77
+ /**
78
+ * `count` IDs in a row for strokes the recorder does not write — the pieces of a cut, numbered
79
+ * when the eraser lifts —, the first of them given: the next stroke takes the ID after them, so
80
+ * the sheet's IDs have one source and none is used twice. A stroke under way keeps the ID it
81
+ * took when it began.
82
+ */
83
+ reserve(count: number): number;
73
84
  }
@@ -33,18 +33,22 @@ export class Recorder {
33
33
  get pointer() {
34
34
  return this.#live?.pointerId;
35
35
  }
36
- /** `pointerdown`: a stroke begins here, if none is under way and the primary button went down. */
37
- down(sample) {
36
+ /**
37
+ * `pointerdown`: a stroke begins here, if none is under way and the primary button went down,
38
+ * in the style given, which it keeps; without one it has none.
39
+ */
40
+ down(sample, style) {
38
41
  if (this.#live !== undefined || sample.button !== PRIMARY)
39
42
  return false;
40
43
  const point = this.#point(sample, sample.pressure);
41
- this.#live = {
44
+ const live = {
42
45
  id: this.#nextId++,
43
46
  kind: sample.kind,
44
47
  points: [point],
45
48
  pointerId: sample.pointerId,
46
49
  latest: point,
47
50
  };
51
+ this.#live = style === undefined ? live : { ...live, style };
48
52
  return true;
49
53
  }
50
54
  /**
@@ -104,6 +108,17 @@ export class Recorder {
104
108
  this.#live = undefined;
105
109
  this.#nextId = nextId;
106
110
  }
111
+ /**
112
+ * `count` IDs in a row for strokes the recorder does not write — the pieces of a cut, numbered
113
+ * when the eraser lifts —, the first of them given: the next stroke takes the ID after them, so
114
+ * the sheet's IDs have one source and none is used twice. A stroke under way keeps the ID it
115
+ * took when it began.
116
+ */
117
+ reserve(count) {
118
+ const first = this.#nextId;
119
+ this.#nextId += count;
120
+ return first;
121
+ }
107
122
  #finish() {
108
123
  const live = this.#live;
109
124
  if (live === undefined)
@@ -112,7 +127,8 @@ export class Recorder {
112
127
  live.points.push(live.latest);
113
128
  // The stroke is no longer under way by the time the handler has it.
114
129
  this.#live = undefined;
115
- this.#finished({ id: live.id, kind: live.kind, points: live.points });
130
+ const { id, kind, style, points } = live;
131
+ this.#finished(style === undefined ? { id, kind, points } : { id, kind, style, points });
116
132
  return true;
117
133
  }
118
134
  /**
@@ -40,12 +40,13 @@ export declare const PICTURE_BYTES: number;
40
40
  /**
41
41
  * The picture of a work: the page, and the ink past it, right and down, with no room past the
42
42
  * ink, at a pixel a unit while its rows hold no more than `PICTURE_BYTES`, and scaled down to hold
43
- * them otherwise. `scale` is pixels a unit.
43
+ * them otherwise. `bytes` is what a pixel takes, 1 in grey and 3 in RGB, and each row takes one
44
+ * byte more, its filter's. `scale` is pixels a unit.
44
45
  */
45
46
  export declare function pictureOf(ink: {
46
47
  readonly maxX: number;
47
48
  readonly maxY: number;
48
- } | undefined): {
49
+ } | undefined, bytes?: 1 | 3): {
49
50
  readonly width: number;
50
51
  readonly height: number;
51
52
  readonly scale: number;
package/lib/core/sheet.js CHANGED
@@ -33,12 +33,14 @@ export const PICTURE_BYTES = (SHEET.width + 1) * SHEET.height;
33
33
  /**
34
34
  * The picture of a work: the page, and the ink past it, right and down, with no room past the
35
35
  * ink, at a pixel a unit while its rows hold no more than `PICTURE_BYTES`, and scaled down to hold
36
- * them otherwise. `scale` is pixels a unit.
36
+ * them otherwise. `bytes` is what a pixel takes, 1 in grey and 3 in RGB, and each row takes one
37
+ * byte more, its filter's. `scale` is pixels a unit.
37
38
  */
38
- export function pictureOf(ink) {
39
+ export function pictureOf(ink, bytes = 1) {
39
40
  const { width, height } = extentOf(ink, { width: 0, height: 0 });
40
- // The scale s whose rows, (width s + 1) × height s, are PICTURE_BYTES: the root of a quadratic.
41
- const area = width * height;
41
+ // The scale s whose rows, (bytes × width s + 1) × height s, are PICTURE_BYTES: the root of a
42
+ // quadratic.
43
+ const area = bytes * width * height;
42
44
  const fit = (Math.sqrt(height * height + 4 * area * PICTURE_BYTES) - height) / (2 * area);
43
45
  const scale = Math.min(1, fit);
44
46
  return {
@@ -0,0 +1,54 @@
1
+ import type { EraserMode, PenKind, PenSettings, StrokeStyle } from "@altnovatrail/spec/canvas";
2
+ import type { Stroke } from "./ink.js";
3
+ /**
4
+ * How a stroke looks, the learner's settings of the pen and the eraser, the kinds of pen and the
5
+ * modes of the eraser: spec's types, of the contract of canvas and app.
6
+ */
7
+ export type { EraserMode, PenKind, PenSettings, StrokeStyle };
8
+ /** The kinds of pen, in the order the pen's popover offers them. */
9
+ export declare const PEN_KINDS: readonly PenKind[];
10
+ /** The eraser's modes: a stroke it touches whole, or only what it passes over. */
11
+ export declare const ERASER_MODES: readonly EraserMode[];
12
+ /** The five widths of the pen and the liner, in sheet units, the thinnest first. */
13
+ export declare const PEN_WIDTHS: readonly number[];
14
+ /**
15
+ * The five widths of the marker, in sheet units: each four times the pen's step, and its style's
16
+ * width is the very width it draws.
17
+ */
18
+ export declare const MARKER_WIDTHS: readonly number[];
19
+ /** The eraser's five sizes, its diameters in sheet units, the smallest first. */
20
+ export declare const ERASER_SIZES: readonly number[];
21
+ /** The palette: black, blue, green, yellow and red. */
22
+ export declare const PALETTE: readonly string[];
23
+ /** The other colours, offered past the palette. */
24
+ export declare const OTHER_COLOURS: readonly string[];
25
+ /** The bounds of a style, in sheet units and in percent, and of the eraser's size, bounds in. */
26
+ export declare const STYLE_BOUNDS: Readonly<{
27
+ width: readonly [0.1, 50];
28
+ opacity: readonly [10, 100];
29
+ eraserSize: readonly [1, 200];
30
+ }>;
31
+ /** The settings an element starts with, frozen through. */
32
+ export declare const PEN_SETTINGS: PenSettings;
33
+ /**
34
+ * Settings of the pen and the eraser given by a consumer, checked field by field and copied into
35
+ * frozen objects of the element's own, with only the fields they have: others are passed over.
36
+ * Anything not of `PenSettings` is a TypeError that names the field — a style whose `pen` is not
37
+ * its kind's, a colour, a width, an opacity or a size out of its bounds.
38
+ */
39
+ export declare function penSettingsOf(value: unknown, path?: string): PenSettings;
40
+ /**
41
+ * A stroke's style, checked field by field and copied, frozen: a kind of pen, a colour of sRGB as
42
+ * `#rrggbb` in lowercase digits, a width in sheet units to a tenth from 0.1 to 50 and an opacity
43
+ * in whole percent from 10 to 100. Anything else is a TypeError at `path`.
44
+ */
45
+ export declare function styleOf(value: unknown, path: string): StrokeStyle;
46
+ /** Whether two styles say the same, field by field; none is the same as none alone. */
47
+ export declare function sameStyle(a: StrokeStyle | undefined, b: StrokeStyle | undefined): boolean;
48
+ /**
49
+ * How far a stroke's ink reaches from its points on the screen at most, in its own units: a stroke
50
+ * without a style as the pen without styles drew it, `INK_REACH`; one of the kind "pen" half its
51
+ * width at the widest a pen's pressure or its speed makes it; a liner's and a marker's half their
52
+ * width.
53
+ */
54
+ export declare function reachOf(stroke: Stroke): number;
@@ -0,0 +1,146 @@
1
+ import { record, shown } from "./fields.js";
2
+ import { INK_REACH, PEN_WIDEST } from "./outline.js";
3
+ /** The kinds of pen, in the order the pen's popover offers them. */
4
+ export const PEN_KINDS = Object.freeze(["pen", "liner", "marker"]);
5
+ /** The eraser's modes: a stroke it touches whole, or only what it passes over. */
6
+ export const ERASER_MODES = Object.freeze(["stroke", "part"]);
7
+ /** The five widths of the pen and the liner, in sheet units, the thinnest first. */
8
+ export const PEN_WIDTHS = Object.freeze([1.2, 2, 3, 4.5, 7]);
9
+ /**
10
+ * The five widths of the marker, in sheet units: each four times the pen's step, and its style's
11
+ * width is the very width it draws.
12
+ */
13
+ export const MARKER_WIDTHS = Object.freeze([4.8, 8, 12, 18, 28]);
14
+ /** The eraser's five sizes, its diameters in sheet units, the smallest first. */
15
+ export const ERASER_SIZES = Object.freeze([8, 16, 28, 44, 64]);
16
+ /** The palette: black, blue, green, yellow and red. */
17
+ export const PALETTE = Object.freeze([
18
+ "#1c1c1e",
19
+ "#0a63d6",
20
+ "#1e9e4a",
21
+ "#ffcc00",
22
+ "#e5322d",
23
+ ]);
24
+ /** The other colours, offered past the palette. */
25
+ export const OTHER_COLOURS = Object.freeze([
26
+ "#8e8e93",
27
+ "#7a4fd0",
28
+ "#f08a00",
29
+ "#00a6a6",
30
+ "#ff5fa2",
31
+ "#8b5a2b",
32
+ "#5ac8fa",
33
+ "#34c759",
34
+ "#ff9f0a",
35
+ "#bf5af2",
36
+ "#ff3b30",
37
+ "#ffffff",
38
+ ]);
39
+ /** The bounds of a style, in sheet units and in percent, and of the eraser's size, bounds in. */
40
+ export const STYLE_BOUNDS = Object.freeze({
41
+ width: Object.freeze([0.1, 50]),
42
+ opacity: Object.freeze([10, 100]),
43
+ eraserSize: Object.freeze([1, 200]),
44
+ });
45
+ const COLOUR = /^#[0-9a-f]{6}$/;
46
+ /** The settings an element starts with, frozen through. */
47
+ export const PEN_SETTINGS = penSettingsOf({
48
+ pen: "pen",
49
+ styles: {
50
+ pen: { pen: "pen", color: "#1c1c1e", width: 3, opacity: 100 },
51
+ liner: { pen: "liner", color: "#0a63d6", width: 2, opacity: 100 },
52
+ marker: { pen: "marker", color: "#ffcc00", width: 18, opacity: 40 },
53
+ },
54
+ eraser: "stroke",
55
+ eraserSize: 16,
56
+ });
57
+ /**
58
+ * Settings of the pen and the eraser given by a consumer, checked field by field and copied into
59
+ * frozen objects of the element's own, with only the fields they have: others are passed over.
60
+ * Anything not of `PenSettings` is a TypeError that names the field — a style whose `pen` is not
61
+ * its kind's, a colour, a width, an opacity or a size out of its bounds.
62
+ */
63
+ export function penSettingsOf(value, path = "pens") {
64
+ const fields = record(value, path);
65
+ const pen = kindOf(fields.pen, `${path}.pen`);
66
+ const given = record(fields.styles, `${path}.styles`);
67
+ const style = (kind) => {
68
+ const at = `${path}.styles.${kind}`;
69
+ const one = styleOf(given[kind], at);
70
+ if (one.pen !== kind)
71
+ throw new TypeError(`${at}.pen: "${kind}", its kind's, not "${one.pen}"`);
72
+ return one;
73
+ };
74
+ const styles = Object.freeze({
75
+ pen: style("pen"),
76
+ liner: style("liner"),
77
+ marker: style("marker"),
78
+ });
79
+ if (!ERASER_MODES.includes(fields.eraser)) {
80
+ throw new TypeError(`${path}.eraser: "stroke" or "part", not ${shown(fields.eraser)}`);
81
+ }
82
+ const eraser = fields.eraser;
83
+ const [least, most] = STYLE_BOUNDS.eraserSize;
84
+ const eraserSize = fields.eraserSize;
85
+ if (typeof eraserSize !== "number" || !(eraserSize >= least && eraserSize <= most)) {
86
+ throw new TypeError(`${path}.eraserSize: the eraser's diameter in sheet units, from ${least} to ${most}, not ${shown(eraserSize)}`);
87
+ }
88
+ return Object.freeze({ pen, styles, eraser, eraserSize });
89
+ }
90
+ /**
91
+ * A stroke's style, checked field by field and copied, frozen: a kind of pen, a colour of sRGB as
92
+ * `#rrggbb` in lowercase digits, a width in sheet units to a tenth from 0.1 to 50 and an opacity
93
+ * in whole percent from 10 to 100. Anything else is a TypeError at `path`.
94
+ */
95
+ export function styleOf(value, path) {
96
+ const fields = record(value, path);
97
+ const pen = kindOf(fields.pen, `${path}.pen`);
98
+ const color = fields.color;
99
+ if (typeof color !== "string" || !COLOUR.test(color)) {
100
+ throw new TypeError(`${path}.color: a colour, # and six lowercase hexadecimal digits, not ${shown(color)}`);
101
+ }
102
+ const width = fields.width;
103
+ const [thinnest, widest] = STYLE_BOUNDS.width;
104
+ if (typeof width !== "number" ||
105
+ !Number.isFinite(width) ||
106
+ Math.round(width * 10) / 10 !== width ||
107
+ width < thinnest ||
108
+ width > widest) {
109
+ throw new TypeError(`${path}.width: sheet units to a tenth, from ${thinnest} to ${widest}, not ${shown(width)}`);
110
+ }
111
+ const opacity = fields.opacity;
112
+ const [faintest, fullest] = STYLE_BOUNDS.opacity;
113
+ if (typeof opacity !== "number" ||
114
+ !Number.isInteger(opacity) ||
115
+ opacity < faintest ||
116
+ opacity > fullest) {
117
+ throw new TypeError(`${path}.opacity: whole percent from ${faintest} to ${fullest}, not ${shown(opacity)}`);
118
+ }
119
+ return Object.freeze({ pen, color, width, opacity });
120
+ }
121
+ /** Whether two styles say the same, field by field; none is the same as none alone. */
122
+ export function sameStyle(a, b) {
123
+ if (a === b)
124
+ return true;
125
+ if (a === undefined || b === undefined)
126
+ return false;
127
+ return a.pen === b.pen && a.color === b.color && a.width === b.width && a.opacity === b.opacity;
128
+ }
129
+ /**
130
+ * How far a stroke's ink reaches from its points on the screen at most, in its own units: a stroke
131
+ * without a style as the pen without styles drew it, `INK_REACH`; one of the kind "pen" half its
132
+ * width at the widest a pen's pressure or its speed makes it; a liner's and a marker's half their
133
+ * width.
134
+ */
135
+ export function reachOf(stroke) {
136
+ const { style } = stroke;
137
+ if (style === undefined)
138
+ return INK_REACH;
139
+ return style.pen === "pen" ? (style.width * PEN_WIDEST) / 2 : style.width / 2;
140
+ }
141
+ function kindOf(value, path) {
142
+ if (!PEN_KINDS.includes(value)) {
143
+ throw new TypeError(`${path}: "pen", "liner" or "marker", not ${shown(value)}`);
144
+ }
145
+ return value;
146
+ }
@@ -4,7 +4,7 @@ import { tenths } from "./geometry.js";
4
4
  import { groupsOf } from "./groups.js";
5
5
  import { digestOf, workbookDigestOf, } from "./marks.js";
6
6
  import { encodePNG } from "./png.js";
7
- import { pictureOf, rasterOf } from "./raster.js";
7
+ import { channelsOf, pictureOf, rasterOf } from "./raster.js";
8
8
  import { layOver, tenthsOf } from "./tracing.js";
9
9
  /**
10
10
  * The work of a sheet for checking, as `exportWork()` gives it: of the strokes the sheet
@@ -67,8 +67,9 @@ export function paperWorkForCheck(strokes, paper, tracing, rect, picture, digit)
67
67
  if (crop === null)
68
68
  return null;
69
69
  const ink = rasterOf(strokes, crop);
70
+ const channels = channelsOf(strokes);
70
71
  if (picture !== null)
71
- layOver(ink, crop.width, picture, pixelsOf(rect, crop));
72
+ layOver(ink, crop.width, picture, pixelsOf(rect, crop), channels);
72
73
  const { drawn, width, height, margins, sheetBox, frames, exact } = crop;
73
74
  const numbers = { drawn, width, height, margins, sheetBox, frames, exact };
74
75
  const submission = {
@@ -82,7 +83,7 @@ export function paperWorkForCheck(strokes, paper, tracing, rect, picture, digit)
82
83
  digest: digestOf(strokes, numbers, groups, paper),
83
84
  };
84
85
  return Object.freeze({
85
- png: encodePNG(ink, crop.width, crop.height),
86
+ png: encodePNG(ink, crop.width, crop.height, channels),
86
87
  submission: frozen(submission),
87
88
  });
88
89
  }