grafia 0.9.0

Interactive 2D cubic Bezier, spline, glyph, and vector canvas widget for egui with boolean path ops, text stream kerning, and auto-trace.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Attached BCP handle translation, Smooth $G^1$ collinearity enforcement, and Line-to-Cubic conversion (`SPEC-002 §2.1`).

use crate::core::auto_trace_bezier::{reparameterize_newton, solve_schneider_cubic_lsq};
use crate::core::bezier_ops::{align_collinear_opposite, Node, NodeId, NodeType};
use crate::core::coordinates::FontPoint;
use crate::core::curve_manipulation::ContourSegment;
use crate::core::math_consts::{
    COLLINEAR_MIN_LEN_EPSILON, CUBIC_ONE_THIRD, CUBIC_TWO_THIRDS, FIT_SAMPLES_PER_SEGMENT,
    NEWTON_REPARAM_ITERATIONS, PARAM_CLAMP_MAX, PARAM_CLAMP_MIN, SPLIT_RATIO_MIN,
};
use kurbo::{ParamCurve, Point, Vec2};

/// Expands `selected` node IDs to include directly attached `OffCurve` BCP handles of any
/// selected `OnCurve` node (unless `include_attached` is `false`, e.g. when `Alt` is held).
/// Guarantees zero duplicate `NodeId` entries so handles are never double-translated (`TC-1246`).
#[must_use]
pub fn expanded_nodes_with_attached_handles(
    contours: &[Vec<Node>],
    closed_flags: &[bool],
    selected: &[NodeId],
    include_attached: bool,
) -> Vec<NodeId> {
    let mut result: Vec<NodeId> = Vec::new();
    for &id in selected {
        if !result.contains(&id) {
            result.push(id);
        }
    }
    if !include_attached {
        return result;
    }

    for (c_idx, contour) in contours.iter().enumerate() {
        let n = contour.len();
        if n < 2 {
            continue;
        }
        let closed = closed_flags.get(c_idx).copied().unwrap_or(true);
        for (i, node) in contour.iter().enumerate() {
            if node.node_type == NodeType::OffCurve || !selected.contains(&node.id) {
                continue;
            }
            // Preceding neighbor (incoming BCP handle)
            if closed || i > 0 {
                let prev_idx = (i + n - 1) % n;
                if contour[prev_idx].node_type == NodeType::OffCurve {
                    let pid = contour[prev_idx].id;
                    if !result.contains(&pid) {
                        result.push(pid);
                    }
                }
            }
            // Succeeding neighbor (outgoing BCP handle)
            if closed || i + 1 < n {
                let next_idx = (i + 1) % n;
                if contour[next_idx].node_type == NodeType::OffCurve {
                    let nid = contour[next_idx].id;
                    if !result.contains(&nid) {
                        result.push(nid);
                    }
                }
            }
        }
    }
    result
}

/// Enforces $G^1$ collinearity on the opposite `OffCurve` handle across a `NodeType::Smooth`
/// anchor when a single `OffCurve` handle is dragged (`TC-1247`), or breaks the `Smooth`
/// connection to `NodeType::Corner` when `alt_held` is `true` (`TC-1248`).
pub fn enforce_smooth_collinearity_on_handle_drag(
    contour: &mut [Node],
    closed: bool,
    moved_handle_id: NodeId,
    alt_held: bool,
) {
    let n = contour.len();
    if n < 3 {
        return;
    }
    let Some(h_idx) = contour.iter().position(|nd| nd.id == moved_handle_id) else {
        return;
    };
    if contour[h_idx].node_type != NodeType::OffCurve {
        return;
    }

    // Determine whether the adjacent OnCurve anchor is immediately before or after h_idx
    let prev_candidate = if closed || h_idx > 0 {
        Some((h_idx + n - 1) % n)
    } else {
        None
    };
    let next_candidate = if closed || h_idx + 1 < n {
        Some((h_idx + 1) % n)
    } else {
        None
    };

    let anchor_and_opp = if let Some(p_idx) =
        prev_candidate.filter(|&i| contour[i].node_type != NodeType::OffCurve)
    {
        let opp = if closed || p_idx > 0 {
            Some((p_idx + n - 1) % n)
        } else {
            None
        };
        Some((p_idx, opp))
    } else if let Some(n_idx) =
        next_candidate.filter(|&i| contour[i].node_type != NodeType::OffCurve)
    {
        let opp = if closed || n_idx + 1 < n {
            Some((n_idx + 1) % n)
        } else {
            None
        };
        Some((n_idx, opp))
    } else {
        None
    };

    let Some((anchor_idx, opp_opt)) = anchor_and_opp else {
        return;
    };

    if contour[anchor_idx].node_type != NodeType::Smooth {
        return;
    }

    if alt_held {
        contour[anchor_idx].node_type = NodeType::Corner;
        return;
    }

    if let Some(opp_idx) = opp_opt {
        if opp_idx != h_idx && contour[opp_idx].node_type == NodeType::OffCurve {
            let new_opp = align_collinear_opposite(
                contour[anchor_idx].point,
                contour[opp_idx].point,
                contour[h_idx].point,
            );
            contour[opp_idx].point = new_opp;
        }
    }
}

/// Converts a straight `ContourSegment::Line` into a `ContourSegment::Cubic` by inserting
/// two `NodeType::OffCurve` BCP handles at $1/3$ and $2/3$ along the line (`TC-1249`).
pub fn convert_line_segment_to_cubic(
    contour: &mut Vec<Node>,
    segment: &ContourSegment,
    next_node_id: &mut u64,
) -> Option<(NodeId, NodeId)> {
    convert_line_segment_to_cubic_with_grid(contour, segment, next_node_id, 0.0)
}

/// Converts a straight `ContourSegment::Line` into a `ContourSegment::Cubic` at $1/3$ and $2/3$
/// (`convertLineToCurve:pathIndex:nodeIndex:theEvent:` @ `CanvasEngine spec_ref` / `spec_ref`),
/// immediately rounding both inserted `OffCurve` handles to `grid_step_upm` when `grid_step_upm > 0.0`.
pub fn convert_line_segment_to_cubic_with_grid(
    contour: &mut Vec<Node>,
    segment: &ContourSegment,
    next_node_id: &mut u64,
    grid_step_upm: f64,
) -> Option<(NodeId, NodeId)> {
    let ContourSegment::Line {
        start_idx,
        end_idx,
        line,
    } = *segment
    else {
        return None;
    };
    let max_existing = contour.iter().map(|n| n.id.0).max().unwrap_or(0);
    if *next_node_id <= max_existing {
        *next_node_id = max_existing + 1;
    }
    let p1 = FontPoint::new(line.eval(CUBIC_ONE_THIRD).x, line.eval(CUBIC_ONE_THIRD).y)
        .round_to_grid_step(grid_step_upm);
    let p2 = FontPoint::new(line.eval(CUBIC_TWO_THIRDS).x, line.eval(CUBIC_TWO_THIRDS).y)
        .round_to_grid_step(grid_step_upm);
    let h1_id = NodeId(*next_node_id);
    *next_node_id += 1;
    let h2_id = NodeId(*next_node_id);
    *next_node_id += 1;
    let h1 = Node::new(h1_id, p1.x.0, p1.y.0, NodeType::OffCurve);
    let h2 = Node::new(h2_id, p2.x.0, p2.y.0, NodeType::OffCurve);
    let insert_pos = if end_idx > start_idx {
        end_idx
    } else {
        contour.len()
    };
    contour.insert(insert_pos, h1);
    contour.insert(insert_pos + 1, h2);
    Some((h1_id, h2_id))
}

/// Cycles selection forward (`reverse == false`) or backward (`reverse == true`) along the contour (`TC-1250`).
#[must_use]
pub fn cycle_selection_tab(
    contours: &[Vec<Node>],
    selected: &[NodeId],
    reverse: bool,
) -> Option<NodeId> {
    let target = *selected.first()?;
    for contour in contours {
        let n = contour.len();
        if n == 0 {
            continue;
        }
        if let Some(idx) = contour.iter().position(|nd| nd.id == target) {
            let next_idx = if reverse {
                (idx + n - 1) % n
            } else {
                (idx + 1) % n
            };
            return Some(contour[next_idx].id);
        }
    }
    None
}

/// Toggles selected `OnCurve` nodes between `NodeType::Corner` and `NodeType::Smooth` (`TC-1250`).
pub fn toggle_selected_on_curve_smooth(
    contours: &mut [Vec<Node>],
    closed_flags: &[bool],
    selected: &[NodeId],
) -> bool {
    let mut changed = false;
    for (c_idx, contour) in contours.iter_mut().enumerate() {
        let n = contour.len();
        let closed = closed_flags.get(c_idx).copied().unwrap_or(true);
        for i in 0..n {
            if !selected.contains(&contour[i].id) || contour[i].node_type == NodeType::OffCurve {
                continue;
            }
            contour[i].node_type = match contour[i].node_type {
                NodeType::Corner => NodeType::Smooth,
                NodeType::Smooth | NodeType::HobbyStar { .. } | NodeType::EulerSpiro { .. } => {
                    NodeType::Corner
                }
                NodeType::OffCurve => NodeType::OffCurve,
            };
            changed = true;
            if contour[i].node_type == NodeType::Smooth && n >= 3 {
                let prev_idx = (i + n - 1) % n;
                let next_idx = (i + 1) % n;
                if (closed || (i > 0 && i + 1 < n))
                    && contour[prev_idx].node_type == NodeType::OffCurve
                    && contour[next_idx].node_type == NodeType::OffCurve
                {
                    let aligned = align_collinear_opposite(
                        contour[i].point,
                        contour[prev_idx].point,
                        contour[next_idx].point,
                    );
                    contour[prev_idx].point = aligned;
                }
            }
        }
    }
    changed
}

fn seg_endpoints_and_tangents(seg: &ContourSegment) -> (Point, Point, Vec2, Vec2) {
    match *seg {
        ContourSegment::Line { line, .. } => {
            let v = line.p1 - line.p0;
            (line.p0, line.p1, v, -v)
        }
        ContourSegment::Cubic { cubic, .. } => {
            let mut v1 = cubic.p1 - cubic.p0;
            if v1.hypot() <= COLLINEAR_MIN_LEN_EPSILON {
                v1 = cubic.p3 - cubic.p0;
            }
            let mut v2 = cubic.p2 - cubic.p3;
            if v2.hypot() <= COLLINEAR_MIN_LEN_EPSILON {
                v2 = cubic.p0 - cubic.p3;
            }
            (cubic.p0, cubic.p3, v1, v2)
        }
    }
}

fn sample_segment_chain_for_fit(segments: &[ContourSegment]) -> (Vec<Point>, Vec<f64>) {
    let mut weights = vec![1.0_f64; segments.len()];
    for idx in 0..segments.len().saturating_sub(1) {
        let d_in = match segments[idx] {
            ContourSegment::Cubic { cubic, .. } => (cubic.p3 - cubic.p2).hypot(),
            ContourSegment::Line { line, .. } => (line.p1 - line.p0).hypot() * CUBIC_ONE_THIRD,
        };
        let d_out = match segments[idx + 1] {
            ContourSegment::Cubic { cubic, .. } => (cubic.p1 - cubic.p0).hypot(),
            ContourSegment::Line { line, .. } => (line.p1 - line.p0).hypot() * CUBIC_ONE_THIRD,
        };
        let ratio = if d_in > COLLINEAR_MIN_LEN_EPSILON && d_out > COLLINEAR_MIN_LEN_EPSILON {
            (d_out / d_in).clamp(SPLIT_RATIO_MIN, 1.0 / SPLIT_RATIO_MIN)
        } else {
            1.0
        };
        weights[idx + 1] = weights[idx] * ratio;
    }

    let weight_sum: f64 = weights.iter().sum::<f64>().max(COLLINEAR_MIN_LEN_EPSILON);
    let mut pts: Vec<Point> = Vec::new();
    let mut params: Vec<f64> = Vec::new();
    let mut u_acc = 0.0;
    for (seg, &w) in segments.iter().zip(&weights) {
        let span = w / weight_sum;
        for k in 0..FIT_SAMPLES_PER_SEGMENT {
            if u_acc == 0.0 && k == 0 {
                continue;
            }
            let local_t = (k as f64) / (FIT_SAMPLES_PER_SEGMENT as f64);
            let u = (u_acc + span * local_t).clamp(PARAM_CLAMP_MIN, PARAM_CLAMP_MAX);
            let pt = match *seg {
                ContourSegment::Line { line, .. } => line.eval(local_t),
                ContourSegment::Cubic { cubic, .. } => cubic.eval(local_t),
            };
            pts.push(pt);
            params.push(u);
        }
        if (u_acc..=u_acc + span).contains(&0.5) {
            let lt = ((0.5 - u_acc) / span).clamp(0.0, 1.0);
            let pt = match *seg {
                ContourSegment::Line { line, .. } => line.eval(lt),
                ContourSegment::Cubic { cubic, .. } => cubic.eval(lt),
            };
            pts.push(pt);
            params.push(0.5);
        }
        u_acc += span;
    }
    (pts, params)
}

/// Fits a single cubic Bézier `(h1, h2)` along the endpoint tangents of a chain of segments
/// using inverse De Casteljau handle-ratio initialization and Schneider's Newton-Raphson
/// least-squares solver (`SPEC-002 §3.1`, Reference `removeNodeCheckKeepShape:` parity).
#[must_use]
pub fn fit_cubic_to_segment_chain(segments: &[ContourSegment]) -> (FontPoint, FontPoint) {
    debug_assert!(!segments.is_empty());
    let (p0, _, v1_raw, _) = seg_endpoints_and_tangents(&segments[0]);
    let (_, p3, _, v2_raw) = seg_endpoints_and_tangents(&segments[segments.len() - 1]);
    let chord = (p3 - p0).hypot().max(1.0);
    let t1 = if v1_raw.hypot() > COLLINEAR_MIN_LEN_EPSILON {
        v1_raw / v1_raw.hypot()
    } else {
        (p3 - p0) / chord
    };
    let t2 = if v2_raw.hypot() > COLLINEAR_MIN_LEN_EPSILON {
        v2_raw / v2_raw.hypot()
    } else {
        (p0 - p3) / chord
    };

    let (pts, mut params) = sample_segment_chain_for_fit(segments);

    if segments.len() >= 2 && t1.cross(t2).abs() <= COLLINEAR_MIN_LEN_EPSILON && t1.dot(t2) > 0.0 {
        let mid_chord = p0 + (p3 - p0) * 0.5;
        let arch_h = pts
            .iter()
            .map(|&pt| (pt - mid_chord).dot(t1))
            .fold(0.0_f64, f64::max);
        if arch_h > COLLINEAR_MIN_LEN_EPSILON {
            let alpha_arch = 2.0 * CUBIC_TWO_THIRDS * arch_h;
            let h1_u = p0 + t1 * alpha_arch;
            let h2_u = p3 + t2 * alpha_arch;
            return (
                FontPoint::new(h1_u.x, h1_u.y),
                FontPoint::new(h2_u.x, h2_u.y),
            );
        }
    }

    let mut alpha1 = chord * CUBIC_ONE_THIRD;
    let mut alpha2 = chord * CUBIC_ONE_THIRD;
    let tan_l = (t1.x, t1.y);
    let tan_r = (t2.x, t2.y);

    for _ in 0..=NEWTON_REPARAM_ITERATIONS {
        let (a1, a2, cand) = solve_schneider_cubic_lsq(
            (p0, p3),
            &pts,
            &params,
            tan_l,
            tan_r,
            COLLINEAR_MIN_LEN_EPSILON,
            (alpha1, alpha2),
        );
        alpha1 = a1;
        alpha2 = a2;
        reparameterize_newton(
            cand,
            &pts,
            &mut params,
            false,
            (PARAM_CLAMP_MIN, PARAM_CLAMP_MAX),
        );
    }

    let h1_final = p0 + t1 * alpha1;
    let h2_final = p3 + t2 * alpha2;
    (
        FontPoint::new(h1_final.x, h1_final.y),
        FontPoint::new(h2_final.x, h2_final.y),
    )
}