//! THE MARKS, read by rule (owner, 3 Nov 2026: "the model is the letter//! reader"): a dot or a tap's slide is nothing. A stroke whose ends are//! nearly its whole length apart is a line, and so is a thin one that arcs a//! little (a thumb's swipe): any line but an upright one is handed back as a//! line (a level one is the deck's page swipe, a short level one edits the//! letters, `letters`). An upright line is a letter's stroke (an I, an l)//! and the model reads it — unless it stands alone and tall beside the page//! (`vertical_short`), most likely a letter's first stem, which is a line. A//! scratch-out — back and forth across at least three times — clears; a flat//! one that turns back only twice is a scratch-out begun, and nothing — each//! counted along the stroke's own axis, so a slanted or tight zigzag clears//! too (`scratch`). A check — a short leg down to a corner and a longer one//! up and on, at any size past a tap — is a mark (`check`): it acts on the//! card it is drawn over by what the card's table binds to it, and is never//! a letter (owner, 3 Nov 2026: "marking a todo as done is just having a//! check mark"). LETTERS search, MARKS act.usesuper::{Knobs, Point, Reading, Rect, CHECK};/// The travel along `points`.pubfn length(points: &[Point]) -> f64 { points.windows(2).map(|pair| (pair[1].x - pair[0].x).hypot(pair[1].y - pair[0].y)).sum()}/// The box `points` stand in; a zero box for none.pubfn bounds(points: &[Point]) -> Rect {letSome(first) = points.first() else { returnRect::default() };let (mut min_x, mut max_x, mut min_y, mut max_y) = (first.x, first.x, first.y, first.y);for point in points { min_x = min_x.min(point.x); max_x = max_x.max(point.x); min_y = min_y.min(point.y); max_y = max_y.max(point.y); }Rect { x: min_x, y: min_y, width: max_x - min_x, height: max_y - min_y }}/// A dot (an i's, a j's) or a tap's slide: smaller than `dot` every way. A/// short bar is no dot: it may be a small A's.pubfn dot(points: &[Point], knobs: &Knobs) -> bool {if points.len() <= 1 {returntrue; }let held = bounds(points); held.width.max(held.height) < knobs.dot}/// A line: its ends nearly its whole travel apart, or thin and a little/// arced (a thumb's swipe).pubfn straight(points: &[Point], knobs: &Knobs) -> bool {let (Some(first), Some(last)) = (points.first(), points.last()) else { returnfalse };let travel = length(points);if travel <= 0.0 {returnfalse; }let held = bounds(points);let chord = (last.x - first.x).hypot(last.y - first.y);let thin = held.width.min(held.height) <= knobs.thin * held.width.max(held.height); chord / travel >= knobs.straightness || (thin && chord / travel >= knobs.thin_straightness)}/// The stroke as a mark — nothing, a line, a clear or a check — or None/// when it is a letter's, for the model. An upright line is a letter's/// stroke when no `height` is given or it is shorter than `vertical_short`/// of it. A check is read at any size past a tap and a dot.pubfn mark(points: &[Point], height: Option<f64>, knobs: &Knobs) -> Option<Reading> {let (Some(first), Some(last)) = (points.first(), points.last()) else { returnSome(Reading::Nothing) };if points.len() <= 1 {returnSome(Reading::Nothing); }if check(points, knobs) {returnSome(Reading::Mark(CHECK.to_string())); }let held = bounds(points);if length(points) < knobs.shortest || held.width.max(held.height) < knobs.smallest {returnSome(Reading::Nothing); }if straight(points, knobs) {let lean = (last.x - first.x).atan2(last.y - first.y).abs() * 180.0 / std::f64::consts::PI;if lean.min(180.0 - lean) > knobs.vertical_slope {returnSome(Reading::Line { from: *first, to: *last }); }ifletSome(height) = height {if height > 0.0 && held.height >= knobs.vertical_short * height {returnSome(Reading::Line { from: *first, to: *last }); } }returnNone; } scratch(points, knobs)}/// A quick flick, written in `seconds`: a swipe however it curls or hooks/// (a thumb's flick across a deck), read as the line from its first point to/// its last and never handed to the model, which read such a hook as an f./// Fast (`flick_speed` points a second on average), its ends `flick_chord`/// of its travel apart, rising no more than `flick_level` of its run. A/// finger writing a letter goes nowhere near that pace.pubfn flick(points: &[Point], seconds: f64, knobs: &Knobs) -> Option<Reading> {let (Some(first), Some(last)) = (points.first(), points.last()) else { returnNone };let travel = length(points);if travel < knobs.shortest || seconds <= 0.0 || travel / seconds < knobs.flick_speed {returnNone; }let (run, rise) = ((last.x - first.x).abs(), (last.y - first.y).abs());if run <= 0.0 || rise > knobs.flick_level * run || run.hypot(rise) < knobs.flick_chord * travel {returnNone; }Some(Reading::Line { from: *first, to: *last })}/// A scratch-out, read along the stroke's own axis (`turns`), so a thumb's/// slanted or tight zigzag clears as a level one does (owner, 3 Nov 2026:/// "worked once and didn't work other times … confused into B or N"): one/// turning back across `scratch_reversals` times, going back and forth along/// an axis within `scratch_tilt` of level for at least `scratch_travel`/// times its length, clears; one turning back `scratch_never` times clears/// at any angle and is never the model's. One turning back a time fewer, as/// level, travelling as far a sweep and no thicker than `scratch_flat` of/// its length, is a scratch-out begun: nothing. Any other is the model's: a/// W or an N turns back along its height.pubfn scratch(points: &[Point], knobs: &Knobs) -> Option<Reading> {let turned = turns(points);if turned.reversals >= knobs.scratch_never {returnSome(Reading::Clear); }if turned.tilt > knobs.scratch_tilt {returnNone; }if turned.reversals >= knobs.scratch_reversals && turned.travel >= knobs.scratch_travel {returnSome(Reading::Clear); }// A time fewer is a sweep fewer: as much travel for each sweep.let begun = knobs.scratch_travel * knobs.scratch_reversals as f64 / (knobs.scratch_reversals + 1) as f64;if turned.reversals == knobs.scratch_reversals - 1 && turned.travel >= begun && turned.thickness <= knobs.scratch_flat {returnSome(Reading::Nothing); }None}/// A check: one stroke travelling at least `tap_travel`, no dot (an i's tick/// is the i's) and no line, that turns once at a corner — the point farthest/// from the line between its ends — into a leg at least `check_ratio` times/// the first, each leg's ends `check_straight` of its travel apart (a u's/// curl, a J's hook, a y's tail are not), the corner `check_angle` degrees/// wide, opening up within `angle_range` degrees either way, the long leg to/// the right of the short one. A v's legs are alike, so it is no check.pubfn check(points: &[Point], knobs: &Knobs) -> bool {if points.len() <= 2 || length(points) < knobs.tap_travel || dot(points, knobs) || straight(points, knobs) {returnfalse; }let (first, last) = (points[0], points[points.len() - 1]);let (dx, dy) = (last.x - first.x, last.y - first.y);let chord = dx.hypot(dy);if chord <= 0.0 {returnfalse; }let (mut corner, mut farthest) = (0usize, 0.0f64);for at in1..(points.len() - 1) {let off = ((points[at].x - first.x) * dy - (points[at].y - first.y) * dx).abs() / chord;if off > farthest { farthest = off; corner = at; } }if corner == 0 {returnfalse; }let turn = points[corner];let short = (turn.x - first.x).hypot(turn.y - first.y);let long = (last.x - turn.x).hypot(last.y - turn.y);if short <= 0.0 || long < knobs.check_ratio * short || short / length(&points[..=corner]) < knobs.check_straight || long / length(&points[corner..]) < knobs.check_straight {returnfalse; }// The legs from the corner, as unit lengths: the angle between them, and// the way the corner opens (their sum) from straight up.let (ux, uy) = ((first.x - turn.x) / short, (first.y - turn.y) / short);let (vx, vy) = ((last.x - turn.x) / long, (last.y - turn.y) / long);let wide = (ux * vx + uy * vy).clamp(-1.0, 1.0).acos() * 180.0 / std::f64::consts::PI;if knobs.check_angle.len() != 2 || wide < knobs.check_angle[0] || wide > knobs.check_angle[1] {returnfalse; }let (ox, oy) = (ux + vx, uy + vy);if ox.hypot(oy) <= 0.0 || ox.atan2(-oy).abs() * 180.0 / std::f64::consts::PI > knobs.angle_range {returnfalse; }// Across the opening, left to right: the long leg ends right of where the// short one began. dx * -oy + dy * ox > 0.0}/// How a stroke turns back on itself, along its own axis.#[derive(Debug, Clone, Copy, PartialEq)]pubstructTurns {/// How many times it turns back along the axis, each turn counted once/// it has come back a fifth of its length.pub reversals: i64,/// Its travel along the axis, in its lengths along it.pub travel: f64,/// The axis's angle from level, 0–90°.pub tilt: f64,/// Its width across the axis, as a share of its length along it.pub thickness: f64,}/// `points` along their principal axis (the line their travel lies along,/// length-weighted): how often they turn back along it, how far they travel/// along it, its tilt, and how thick the stroke stands across it.pubfn turns(points: &[Point]) -> Turns {let (mut weight, mut cx, mut cy) = (0.0, 0.0, 0.0);for pair in points.windows(2) {let (a, b) = (pair[0], pair[1]);let l = (b.x - a.x).hypot(b.y - a.y); weight += l; cx += l * (a.x + b.x) / 2.0; cy += l * (a.y + b.y) / 2.0; }if weight <= 0.0 {returnTurns { reversals: 0, travel: 0.0, tilt: 0.0, thickness: 1.0 }; } cx /= weight; cy /= weight;let (mut sxx, mut syy, mut sxy) = (0.0, 0.0, 0.0);for pair in points.windows(2) {let (a, b) = (pair[0], pair[1]);let l = (b.x - a.x).hypot(b.y - a.y);let (mx, my) = ((a.x + b.x) / 2.0 - cx, (a.y + b.y) / 2.0 - cy); sxx += l * mx * mx; syy += l * my * my; sxy += l * mx * my; }let angle = (2.0 * sxy).atan2(sxx - syy) / 2.0;let (ux, uy) = (angle.cos(), angle.sin());let along: Vec<f64> = points.iter().map(|p| p.x * ux + p.y * uy).collect();let across: Vec<f64> = points.iter().map(|p| p.y * ux - p.x * uy).collect();let spread = |values: &[f64]| {let max = values.iter().cloned().fold(f64::NEG_INFINITY, f64::max);let min = values.iter().cloned().fold(f64::INFINITY, f64::min);if values.is_empty() { 0.0 } else { max - min } };let (length, width) = (spread(&along), spread(&across));let hysteresis = length / 5.0;let (mut travel, mut heading, mut extreme, mut reversals) = (0.0, 0i32, along[0], 0i64);for pair in along.windows(2) {let (a, b) = (pair[0], pair[1]); travel += (b - a).abs();match heading {0 => {if b - extreme > hysteresis { heading = 1; extreme = b; } elseif extreme - b > hysteresis { heading = -1; extreme = b; } }1 => {if b > extreme { extreme = b; } elseif extreme - b > hysteresis { reversals += 1; heading = -1; extreme = b; } } _ => {if b < extreme { extreme = b; } elseif b - extreme > hysteresis { reversals += 1; heading = 1; extreme = b; } } } }let tilt = angle.abs() * 180.0 / std::f64::consts::PI;Turns { reversals, travel: if length > 0.0 { travel / length } else { 0.0 }, tilt: tilt.min(180.0 - tilt), thickness: if length > 0.0 { width / length } else { 1.0 }, }}/// Whether `next` and `held` overlap across by at least `group_overlap` of/// the narrower, as one letter's strokes do (an H's bar and its stems, a t's/// cross), or nearly: a stroke that stops short of another by no more than/// `group_touch` of the taller still touches it (an h's hump, a k's arms/// beside the stem). The next letter, written beside, does not (owner,/// 3 Nov 2026: "cl" read as d).pubfn across(held: &Rect, next: &Rect, knobs: &Knobs) -> bool {let overlap = held.max_x().min(next.max_x()) - held.min_x().max(next.min_x()); overlap + knobs.group_touch * held.height.max(next.height) >= knobs.group_overlap * held.width.min(next.width)}/// Whether the box `next` stands near `held`: overlapping it, or within/// `group_near` of the longer side of either.pubfn near(held: &Rect, next: &Rect, knobs: &Knobs) -> bool {let slack = knobs.group_near * held.width.max(held.height).max(next.width).max(next.height); held.outset(slack).intersects(&next.outset(0.5))}#[cfg(test)]#[path = "marks_tests.rs"]mod marks_tests;