grafia 0.7.0

Interactive 2D cubic Bezier contour, glyph, and vector canvas engine for egui with variable-font master compatibility and stylus brush support.
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! `SPEC-008` (`grafia v0.7.0`) adapter operations:
//! Background Layer aliases, 2D `CanvasAnchor` & `GhostMarkPath`, 2D `CanvasComponent` & Decompose (`Cmd+Shift+D`),
//! 8-Step Fit Curve (`Ctrl+Opt+1..8`), and Live Contour Stroke & Expand Outline.

use crate::adapters::state::GrafiaCanvasState;
use crate::core::{
    apply_fit_curve_delta_to_contours, apply_fit_curve_ratio_to_contours,
    compute_mark_cloud_contours, expand_single_contour_stroke, expand_stroked_contours_preview,
    hit_test_component_upm, normalize_cubic_handles_in_contours, CanvasAnchor, CanvasComponent,
    CanvasHostRequest, ContourId, ContourStrokeStyle, FontPoint, GhostMarkPath, GlyphContour,
    NodeId,
};
use crate::snapping::SnapEngine;
use crate::tokens::{ANCHOR_HIT_RADIUS_DP, DECOMPOSE_NODE_ID_OFFSET, MIN_VIEWPORT_ZOOM};
use std::collections::HashSet;

impl GrafiaCanvasState {
    /// Alias for `swap_foreground_and_background` (`Cmd+K`, `SPEC-008 §2.1`).
    pub fn swap_foreground_with_background(&mut self) {
        self.swap_foreground_and_background();
    }

    /// Alias for `toggle_editing_background_layer` (`Cmd+B`, `SPEC-008 §2.1`).
    pub fn toggle_background_editing(&mut self) {
        self.toggle_editing_background_layer();
    }

    /// Translates a single node `node_id` in the active layer by `delta` in UPM space (`SPEC-008 §2.1`).
    pub fn translate_node_upm(&mut self, node_id: NodeId, delta: FontPoint) -> bool {
        let before = self.snapshot();
        let mut found = false;
        for contour in &mut self.contours {
            for node in contour {
                if node.id == node_id {
                    node.point.x.0 += delta.x.0;
                    node.point.y.0 += delta.y.0;
                    found = true;
                }
            }
        }
        if found {
            self.sync_paths_from_contours();
            self.record_mutation("Translate Node", before);
            self.assert_invariants();
        }
        found
    }

    /// Replaces the active glyph's `CanvasAnchor` list (`SPEC-008 §2.2`).
    pub fn set_anchors(&mut self, anchors: Vec<CanvasAnchor>) {
        for a in &anchors {
            if a.id.0 >= self.next_anchor_id {
                self.next_anchor_id = a.id.0 + 1;
            }
        }
        self.anchors = anchors;
        self.glyph.anchors = self.anchors.clone();
        self.bump_content_generation();
    }

    /// Sets the `GhostMarkPath` mark preview cloud templates (`SPEC-008 §2.2`).
    pub fn set_ghost_mark_paths(&mut self, paths: Vec<GhostMarkPath>) {
        self.ghost_paths = paths;
        self.bump_content_generation();
    }

    /// Hit-tests and selects a `CanvasAnchor` at `pt` in UPM coordinates (`SPEC-008 §2.2.2`).
    pub fn select_anchor_at_upm(&mut self, pt: FontPoint, extend: bool) -> bool {
        let zoom = self.viewport.zoom.max(MIN_VIEWPORT_ZOOM);
        let hit_radius_upm = f64::from(ANCHOR_HIT_RADIUS_DP / zoom);
        let mut best: Option<(usize, f64)> = None;
        for (idx, a) in self.anchors.iter().enumerate() {
            let d = a.position.distance_to(pt);
            if d <= hit_radius_upm && best.is_none_or(|(_, bd)| d < bd) {
                best = Some((idx, d));
            }
        }
        if let Some((hit_idx, _)) = best {
            self.selected_nodes.clear();
            self.selected_guide = None;
            if !extend {
                for a in &mut self.anchors {
                    a.selected = false;
                }
            }
            self.anchors[hit_idx].selected = true;
            self.glyph.anchors = self.anchors.clone();
            true
        } else {
            if !extend {
                for a in &mut self.anchors {
                    a.selected = false;
                }
                self.glyph.anchors = self.anchors.clone();
            }
            false
        }
    }

    /// Cycles selection to the next (`reverse == false`) or previous (`reverse == true`) `CanvasAnchor` (`Tab` / `Shift+Tab`).
    pub fn cycle_selected_anchor(&mut self, reverse: bool) -> bool {
        if self.anchors.is_empty() {
            return false;
        }
        let len = self.anchors.len();
        let cur = self.anchors.iter().position(|a| a.selected);
        let next_idx = match cur {
            Some(i) if reverse => (i + len - 1) % len,
            Some(i) => (i + 1) % len,
            None if reverse => len - 1,
            None => 0,
        };
        for (i, a) in self.anchors.iter_mut().enumerate() {
            a.selected = i == next_idx;
        }
        self.glyph.anchors = self.anchors.clone();
        true
    }

    /// Drags all selected `CanvasAnchor`s so the first selected anchor moves to `pt` (with optional guide/metric snapping).
    pub fn drag_selected_anchors_to_upm(&mut self, pt: FontPoint, bypass_snap: bool) {
        let Some(lead_pos) = self.anchors.iter().find(|a| a.selected).map(|a| a.position) else {
            return;
        };
        let target_pt = if bypass_snap {
            pt
        } else {
            let res = SnapEngine::snap_point(
                pt,
                &[],
                &[],
                &self.metrics,
                self.glyph.width,
                self.italic_angle_deg,
                self.show_guides,
                &self.guides,
                &self.global_guides,
                self.viewport.zoom,
                false,
            );
            self.active_snappers = res.active_snappers;
            res.snapped_point
        };
        let dx = target_pt.x.0 - lead_pos.x.0;
        let dy = target_pt.y.0 - lead_pos.y.0;
        let before = self.snapshot();
        for a in self.anchors.iter_mut().filter(|a| a.selected) {
            a.position.x.0 += dx;
            a.position.y.0 += dy;
        }
        self.glyph.anchors = self.anchors.clone();
        self.record_mutation("Move Anchor", before);
    }

    /// Deletes all currently selected `CanvasAnchor`s with Undo support (`Delete` / `Backspace`).
    pub fn delete_selected_anchors(&mut self) -> bool {
        if !self.anchors.iter().any(|a| a.selected) {
            return false;
        }
        let before = self.snapshot();
        self.anchors.retain(|a| !a.selected);
        self.glyph.anchors = self.anchors.clone();
        self.record_mutation("Delete Anchors", before);
        true
    }

    /// Computes the active `GhostMarkPath` mark preview cloud contours for all selected anchors (`SPEC-008 §2.2.2`).
    #[must_use]
    pub fn compute_active_mark_cloud_contours(&self) -> Vec<GlyphContour> {
        compute_mark_cloud_contours(&self.anchors, &self.ghost_paths, self.show_mark_cloud)
    }

    /// Replaces the 2D pre-resolved `CanvasComponent` list (`SPEC-008 §2.3`).
    pub fn set_components(&mut self, components: Vec<CanvasComponent>) {
        self.components = components;
        self.bump_content_generation();
    }

    /// Hit-tests and selects an unlocked `CanvasComponent` at `pt` in UPM coordinates (`SPEC-008 §2.3.2`).
    pub fn select_component_at_upm(&mut self, pt: FontPoint, extend: bool) -> bool {
        let hit_idx = self
            .components
            .iter()
            .enumerate()
            .rev()
            .find(|(_, c)| !c.locked && hit_test_component_upm(c, pt))
            .map(|(i, _)| i);
        if let Some(idx) = hit_idx {
            if !extend {
                for c in &mut self.components {
                    c.selected = false;
                }
            }
            self.components[idx].selected = true;
            true
        } else {
            if !extend {
                for c in &mut self.components {
                    c.selected = false;
                }
            }
            false
        }
    }

    /// Translates all selected unlocked components that do not have `automatic_alignment == true` by `delta` (`SPEC-008 §2.3.2`).
    pub fn translate_selected_components(&mut self, delta: FontPoint) -> bool {
        if !self
            .components
            .iter()
            .any(|c| c.selected && !c.locked && !c.automatic_alignment)
        {
            return false;
        }
        let before = self.snapshot();
        for c in self
            .components
            .iter_mut()
            .filter(|c| c.selected && !c.locked && !c.automatic_alignment)
        {
            c.transform.tx += delta.x.0;
            c.transform.ty += delta.y.0;
        }
        self.record_mutation("Translate Components", before);
        true
    }

    /// Emits `CanvasHostRequest::OpenComponentSource` when a component is double-clicked at `pt` (`SPEC-008 §2.3.2`).
    pub fn double_click_component_at_upm(&mut self, pt: FontPoint) -> bool {
        if let Some(comp) = self
            .components
            .iter()
            .rev()
            .find(|c| hit_test_component_upm(c, pt))
        {
            self.host_requests
                .push(CanvasHostRequest::OpenComponentSource {
                    id: comp.id,
                    component_name: comp.component_name.clone(),
                });
            true
        } else {
            false
        }
    }

    /// Decomposes all selected unlocked `CanvasComponent`s into editable `GlyphContour`s (`Cmd+Shift+D`, `SPEC-008 §2.3.2`).
    pub fn decompose_selected_components(&mut self) -> bool {
        if !self.components.iter().any(|c| c.selected && !c.locked) {
            return false;
        }
        let before = self.snapshot();
        let mut next_nid = self.next_node_id.max(DECOMPOSE_NODE_ID_OFFSET);
        let mut next_cid = self.glyph.contours.len();
        let mut remaining = Vec::with_capacity(self.components.len());
        let mut decomposed_node_ids = Vec::new();
        for comp in std::mem::take(&mut self.components) {
            if comp.selected && !comp.locked {
                for c in &comp.contours {
                    let tc =
                        comp.transform
                            .transform_contour(c, ContourId(next_cid), &mut next_nid);
                    next_cid += 1;
                    decomposed_node_ids.extend(tc.nodes.iter().map(|n| n.id));
                    self.contours.push(tc.nodes.clone());
                    self.glyph.contours.push(tc);
                }
            } else {
                remaining.push(comp);
            }
        }
        self.components = remaining;
        self.next_node_id = next_nid;
        self.selected_nodes = decomposed_node_ids;
        self.active_contour_closed = self.glyph.contours.last().is_none_or(|c| c.closed);
        self.sync_paths_from_contours();
        self.record_mutation("Decompose Components", before);
        self.assert_invariants();
        true
    }

    /// Drains all pending `CanvasHostRequest` events (`SPEC-008 §2.3.2`).
    pub fn drain_host_requests(&mut self) -> Vec<CanvasHostRequest> {
        std::mem::take(&mut self.host_requests)
    }

    /// Applies column `col` (`0..=7`) of the 8-step Fit Curve ladder (`Ctrl+Opt+1..8`, `SPEC-008 §2.4.2`).
    pub fn apply_fit_curve_column(&mut self, col: usize) -> bool {
        let ratio = self.fit_curve_config.ratio_for_column(col);
        self.fit_selected_curves(ratio)
    }

    /// Applies a uniform Fit Curve ratio `ratio` to all selected cubic segments (`SPEC-008 §2.4.2`).
    pub fn fit_selected_curves(&mut self, ratio: f64) -> bool {
        let before = self.snapshot();
        if apply_fit_curve_ratio_to_contours(&mut self.glyph.contours, &self.selected_nodes, ratio)
        {
            self.contours = self
                .glyph
                .contours
                .iter()
                .map(|c| c.nodes.clone())
                .collect();
            self.sync_paths_from_contours();
            self.record_mutation("Fit Curve", before);
            self.assert_invariants();
            true
        } else {
            self.sync_paths_from_contours();
            false
        }
    }

    /// Steps selected cubic curve handles longer (`longer == true`) or shorter (`longer == false`) by `12.5%` of the Fit Curve range.
    pub fn fit_curve_step_delta(&mut self, longer: bool) -> bool {
        let cfg = self.fit_curve_config.sanitized();
        let delta = cfg.step_delta(longer);
        let before = self.snapshot();
        if apply_fit_curve_delta_to_contours(
            &mut self.glyph.contours,
            &self.selected_nodes,
            delta,
            cfg.min_ratio,
            cfg.max_ratio,
        ) {
            self.contours = self
                .glyph
                .contours
                .iter()
                .map(|c| c.nodes.clone())
                .collect();
            self.sync_paths_from_contours();
            self.record_mutation("Step Fit Curve", before);
            self.assert_invariants();
            true
        } else {
            self.sync_paths_from_contours();
            false
        }
    }

    /// Normalizes each selected cubic segment's handle ratios to their arithmetic mean $\bar{t} = 0.5(t_1 + t_2)$ (`SPEC-008 §2.4.3`).
    pub fn normalize_selected_cubic_handles(&mut self) -> bool {
        let before = self.snapshot();
        if normalize_cubic_handles_in_contours(&mut self.glyph.contours, &self.selected_nodes) {
            self.contours = self
                .glyph
                .contours
                .iter()
                .map(|c| c.nodes.clone())
                .collect();
            self.sync_paths_from_contours();
            self.record_mutation("Normalize Cubic Handles", before);
            self.assert_invariants();
            true
        } else {
            self.sync_paths_from_contours();
            false
        }
    }

    /// Attaches or updates a non-destructive `ContourStrokeStyle` on `contour_id` (`SPEC-008 §2.5.2`).
    pub fn set_contour_stroke(&mut self, contour_id: ContourId, style: ContourStrokeStyle) {
        let before = self.snapshot();
        self.contour_strokes.insert(contour_id, style);
        self.record_mutation("Set Contour Stroke", before);
    }

    /// Computes non-destructive expanded stroke preview contours for all stroked contours (`SPEC-008 §2.5.2`).
    #[must_use]
    pub fn compute_expanded_stroke_contours(&self) -> Vec<GlyphContour> {
        expand_stroked_contours_preview(&self.glyph.contours, &self.contour_strokes)
    }

    /// Destructively expands stroked contours into closed outlines and removes their stroke attributes (`SPEC-008 §2.5.3`).
    pub fn expand_selected_strokes(&mut self) -> bool {
        if self.contour_strokes.is_empty() {
            return false;
        }
        let before = self.snapshot();
        let sel_set: HashSet<NodeId> = self.selected_nodes.iter().copied().collect();
        let mut target_ids: HashSet<ContourId> = self
            .glyph
            .contours
            .iter()
            .filter(|gc| {
                self.contour_strokes
                    .get(&gc.id)
                    .is_some_and(|st| st.width > 0.0 && st.height > 0.0)
                    && (sel_set.is_empty() || gc.nodes.iter().any(|n| sel_set.contains(&n.id)))
            })
            .map(|gc| gc.id)
            .collect();
        if target_ids.is_empty() {
            target_ids = self
                .contour_strokes
                .iter()
                .filter(|(_, st)| st.width > 0.0 && st.height > 0.0)
                .map(|(&cid, _)| cid)
                .collect();
        }
        if target_ids.is_empty() {
            return false;
        }
        let mut next_cid = 0usize;
        let mut next_nid = self.next_node_id.max(DECOMPOSE_NODE_ID_OFFSET);
        let mut new_g_contours = Vec::new();
        for gc in &self.glyph.contours {
            if target_ids.contains(&gc.id) {
                if let Some(style) = self.contour_strokes.remove(&gc.id) {
                    let expanded =
                        expand_single_contour_stroke(gc, style, &mut next_cid, &mut next_nid);
                    new_g_contours.extend(expanded);
                    continue;
                }
            }
            let mut copy = gc.clone();
            copy.id = ContourId(next_cid);
            next_cid += 1;
            new_g_contours.push(copy);
        }
        self.next_node_id = next_nid;
        self.contours = new_g_contours.iter().map(|c| c.nodes.clone()).collect();
        self.active_contour_closed = new_g_contours.last().is_none_or(|c| c.closed);
        self.glyph.contours = new_g_contours;
        self.sync_paths_from_contours();
        self.record_mutation("Expand Stroke Outline", before);
        self.assert_invariants();
        true
    }
}