rofd-editor 0.1.4

Annotation command/transaction layer for the rofd OFD editor.
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
use rofd_dom::{AnnotationPayload, Color, PathCommand, Point, Rect, ShapeKind};

/// Shift an annotation's geometry by (dx, dy).
pub fn move_payload(p: &mut AnnotationPayload, dx: f64, dy: f64) {
    match p {
        AnnotationPayload::Markup { quad_points, .. } => {
            for pt in quad_points {
                pt.x += dx;
                pt.y += dy;
            }
        }
        AnnotationPayload::Freehand { path, .. } => {
            for cmd in &mut path.commands {
                shift_cmd(cmd, dx, dy);
            }
        }
        AnnotationPayload::Shape { rect, points, .. } => {
            rect.x += dx;
            rect.y += dy;
            // Line/Arrow/Polygon/PolyLine endpoints must move with the bbox,
            // else the geometry stays put while the bbox slides away.
            for pt in points {
                pt.x += dx;
                pt.y += dy;
            }
        }
        AnnotationPayload::Note { rect, .. }
        | AnnotationPayload::TextBox { rect, .. }
        | AnnotationPayload::Stamp { rect, .. }
        | AnnotationPayload::Watermark { rect, .. } => {
            rect.x += dx;
            rect.y += dy;
        }
    }
}

/// Set the rect (for resize). No-op for Markup/Freehand (no single rect).
///
/// For `Shape`, the endpoint `points` are rescaled from the old rect to the
/// new rect (proportional, per-axis), preserving each point's relative
/// position within the bbox. This keeps a Line/Arrow's direction and a
/// Polygon/PolyLine's shape intact across resize. A degenerate old rect
/// (zero size on an axis) degrades to translating points by the origin delta.
pub fn resize_payload(p: &mut AnnotationPayload, new_rect: Rect) {
    match p {
        AnnotationPayload::Shape { rect, points, .. } => {
            let old = *rect;
            if !points.is_empty() {
                let sx = if old.w != 0.0 {
                    new_rect.w / old.w
                } else {
                    1.0
                };
                let sy = if old.h != 0.0 {
                    new_rect.h / old.h
                } else {
                    1.0
                };
                for pt in points {
                    pt.x = new_rect.x + (pt.x - old.x) * sx;
                    pt.y = new_rect.y + (pt.y - old.y) * sy;
                }
            }
            *rect = new_rect;
        }
        AnnotationPayload::Note { rect, .. }
        | AnnotationPayload::TextBox { rect, .. }
        | AnnotationPayload::Stamp { rect, .. }
        | AnnotationPayload::Watermark { rect, .. } => {
            *rect = new_rect;
        }
        AnnotationPayload::Markup { .. } | AnnotationPayload::Freehand { .. } => { /* no-op v1 */ }
    }
}

/// Move vertex `index` of a point-based Shape payload to `new_point` and
/// recompute `rect` as the points' bounding box (hit-test / selection frame /
/// resize all read `rect`). Returns false -- payload untouched -- for
/// rect-based Shape kinds (Rect/Ellipse), non-Shape payloads, or an
/// out-of-range index.
///
/// Line/Arrow with NO points first seeds the endpoints from the rect's
/// TL->BR diagonal (mirroring render's `line_endpoints` fallback) so external
/// OFD that carries only a boundary is still editable. A 1-point Line/Arrow
/// is degenerate and is a clean no-op (returns false, payload untouched) -
/// seeding it would corrupt the geometry to 3 points while rendering reads
/// `points[0..2]`. Seeding only happens when `index` is reachable (< 2), so
/// a false return never leaves a partially seeded payload behind.
pub fn move_vertex_payload(p: &mut AnnotationPayload, index: usize, new_point: (f64, f64)) -> bool {
    let AnnotationPayload::Shape {
        kind, rect, points, ..
    } = p
    else {
        return false;
    };
    if !matches!(
        kind,
        ShapeKind::Line | ShapeKind::Arrow | ShapeKind::Polygon | ShapeKind::PolyLine
    ) {
        return false;
    }
    if matches!(kind, ShapeKind::Line | ShapeKind::Arrow) {
        if points.is_empty() && index < 2 {
            points.push(Point {
                x: rect.x,
                y: rect.y,
            });
            points.push(Point {
                x: rect.x + rect.w,
                y: rect.y + rect.h,
            });
        }
        if points.len() < 2 {
            // Degenerate 1-point Line/Arrow: no meaningful vertex geometry to
            // edit. Nothing was seeded above, so return a clean no-op.
            return false;
        }
    }
    let Some(pt) = points.get_mut(index) else {
        return false;
    };
    pt.x = new_point.0;
    pt.y = new_point.1;
    let (mut minx, mut miny, mut maxx, mut maxy) = (
        f64::INFINITY,
        f64::INFINITY,
        f64::NEG_INFINITY,
        f64::NEG_INFINITY,
    );
    for p in points.iter() {
        minx = minx.min(p.x);
        miny = miny.min(p.y);
        maxx = maxx.max(p.x);
        maxy = maxy.max(p.y);
    }
    *rect = Rect {
        x: minx,
        y: miny,
        w: maxx - minx,
        h: maxy - miny,
    };
    true
}

fn shift_cmd(cmd: &mut PathCommand, dx: f64, dy: f64) {
    match cmd {
        PathCommand::M(x, y) | PathCommand::L(x, y) => {
            *x += dx;
            *y += dy;
        }
        PathCommand::C(x1, y1, x2, y2, x, y) => {
            *x1 += dx;
            *y1 += dy;
            *x2 += dx;
            *y2 += dy;
            *x += dx;
            *y += dy;
        }
        PathCommand::Q(x1, y1, x, y) => {
            *x1 += dx;
            *y1 += dy;
            *x += dx;
            *y += dy;
        }
        PathCommand::A(_a, _b, _c, _d, x, y) => {
            *x += dx;
            *y += dy;
        }
        PathCommand::Z => {}
    }
}

/// Set the primary color of an annotation (no-op for Stamp which has no color).
pub fn set_color(p: &mut AnnotationPayload, color: Color) {
    match p {
        AnnotationPayload::Markup { color: c, .. } => *c = color,
        AnnotationPayload::Freehand { color: c, .. } => *c = color,
        AnnotationPayload::Shape { stroke: c, .. } => *c = color,
        AnnotationPayload::Note { color: c, .. } => *c = color,
        AnnotationPayload::TextBox { color: c, .. } => *c = color,
        AnnotationPayload::Watermark { color: c, .. } => *c = color,
        AnnotationPayload::Stamp { .. } => { /* no color */ }
    }
}

/// Set the stroke width (Freehand/Shape only; no-op otherwise).
pub fn set_width(p: &mut AnnotationPayload, width: f64) {
    match p {
        AnnotationPayload::Freehand { width: w, .. } => *w = width,
        AnnotationPayload::Shape { width: w, .. } => *w = width,
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rofd_dom::{Color, PathData, Point, ShapeKind};

    #[test]
    fn move_note_shifts_rect() {
        let mut p = AnnotationPayload::Note {
            rect: Rect {
                x: 10.0,
                y: 10.0,
                w: 5.0,
                h: 5.0,
            },
            color: Color::Rgb(0, 0, 0),
            content: "".into(),
            icon: rofd_dom::NoteIcon::Note,
        };
        move_payload(&mut p, 3.0, 4.0);
        assert!(matches!(
            p,
            AnnotationPayload::Note {
                rect: Rect {
                    x: 13.0,
                    y: 14.0,
                    ..
                },
                ..
            }
        ));
    }

    #[test]
    fn resize_shape_sets_rect() {
        let mut p = AnnotationPayload::Shape {
            kind: rofd_dom::ShapeKind::Rect,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 1.0,
                h: 1.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 1.0,
            points: vec![],
        };
        resize_payload(
            &mut p,
            Rect {
                x: 5.0,
                y: 5.0,
                w: 2.0,
                h: 2.0,
            },
        );
        assert!(matches!(
            p,
            AnnotationPayload::Shape {
                rect: Rect {
                    x: 5.0,
                    y: 5.0,
                    w: 2.0,
                    h: 2.0
                },
                ..
            }
        ));
    }

    #[test]
    fn move_freehand_shifts_path_points() {
        let mut p = AnnotationPayload::Freehand {
            path: PathData {
                commands: vec![PathCommand::M(1.0, 2.0), PathCommand::L(3.0, 4.0)],
            },
            color: Color::Rgb(0, 0, 0),
            width: 1.0,
        };
        move_payload(&mut p, 10.0, 20.0);
        if let AnnotationPayload::Freehand { path, .. } = &p {
            assert!(matches!(path.commands[0], PathCommand::M(11.0, 22.0)));
            assert!(matches!(path.commands[1], PathCommand::L(13.0, 24.0)));
        } else {
            panic!("expected Freehand");
        }
    }

    #[test]
    fn move_shape_translates_points_with_rect() {
        // A Line's `points` must move with `rect` (else the line stays put
        // while its bbox moves -> arrow renders at the wrong place).
        let mut p = AnnotationPayload::Shape {
            kind: rofd_dom::ShapeKind::Line,
            rect: Rect {
                x: 10.0,
                y: 10.0,
                w: 40.0,
                h: 50.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 2.0,
            points: vec![
                rofd_dom::Point { x: 10.0, y: 10.0 },
                rofd_dom::Point { x: 50.0, y: 60.0 },
            ],
        };
        move_payload(&mut p, 3.0, 4.0);
        match p {
            AnnotationPayload::Shape { rect, points, .. } => {
                assert_eq!(rect.x, 13.0);
                assert_eq!(rect.y, 14.0);
                assert_eq!(
                    points,
                    vec![
                        rofd_dom::Point { x: 13.0, y: 14.0 },
                        rofd_dom::Point { x: 53.0, y: 64.0 },
                    ],
                    "points must translate with rect"
                );
            }
            _ => panic!("expected Shape"),
        }
    }

    #[test]
    fn move_polygon_translates_points() {
        // Polygon `points` must move with `rect` (latent bug: previously only
        // rect moved, leaving the polygon's vertices behind).
        let mut p = AnnotationPayload::Shape {
            kind: rofd_dom::ShapeKind::Polygon,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 10.0,
                h: 10.0,
            },
            stroke: Color::Rgb(255, 0, 0),
            fill: None,
            width: 1.0,
            points: vec![
                rofd_dom::Point { x: 0.0, y: 0.0 },
                rofd_dom::Point { x: 5.0, y: 10.0 },
                rofd_dom::Point { x: 10.0, y: 0.0 },
            ],
        };
        move_payload(&mut p, 1.0, 2.0);
        if let AnnotationPayload::Shape { points, rect, .. } = &p {
            assert_eq!(rect.x, 1.0);
            assert_eq!(rect.y, 2.0);
            assert_eq!(
                points,
                &vec![
                    rofd_dom::Point { x: 1.0, y: 2.0 },
                    rofd_dom::Point { x: 6.0, y: 12.0 },
                    rofd_dom::Point { x: 11.0, y: 2.0 },
                ]
            );
        }
    }

    #[test]
    fn resize_shape_scales_points_proportionally() {
        // Resizing the bbox must rescale the endpoints proportionally so the
        // line keeps its relative position/direction within the bbox. A Line
        // from rect TL->BR (0,0,100,50) resized to (10,20,200,100) maps the
        // endpoints (0,0)->(10,20) and (100,50)->(210,120).
        let mut p = AnnotationPayload::Shape {
            kind: rofd_dom::ShapeKind::Line,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 100.0,
                h: 50.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 2.0,
            points: vec![
                rofd_dom::Point { x: 0.0, y: 0.0 },
                rofd_dom::Point { x: 100.0, y: 50.0 },
            ],
        };
        resize_payload(
            &mut p,
            Rect {
                x: 10.0,
                y: 20.0,
                w: 200.0,
                h: 100.0,
            },
        );
        if let AnnotationPayload::Shape { rect, points, .. } = &p {
            assert_eq!(
                *rect,
                Rect {
                    x: 10.0,
                    y: 20.0,
                    w: 200.0,
                    h: 100.0
                }
            );
            assert_eq!(
                points,
                &vec![
                    rofd_dom::Point { x: 10.0, y: 20.0 },
                    rofd_dom::Point { x: 210.0, y: 120.0 },
                ],
                "points must rescale from old rect to new rect"
            );
        }
    }

    #[test]
    fn resize_shape_preserves_reverse_direction() {
        // A reverse Line (BR->TL) must keep its direction after resize: the
        // endpoint that was at BR maps to the new BR, the TL endpoint to new TL.
        let mut p = AnnotationPayload::Shape {
            kind: rofd_dom::ShapeKind::Arrow,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 100.0,
                h: 50.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 2.0,
            points: vec![
                rofd_dom::Point { x: 100.0, y: 50.0 },
                rofd_dom::Point { x: 0.0, y: 0.0 },
            ],
        };
        resize_payload(
            &mut p,
            Rect {
                x: 10.0,
                y: 20.0,
                w: 200.0,
                h: 100.0,
            },
        );
        if let AnnotationPayload::Shape { points, .. } = &p {
            assert_eq!(
                points,
                &vec![
                    rofd_dom::Point { x: 210.0, y: 120.0 },
                    rofd_dom::Point { x: 10.0, y: 20.0 },
                ],
                "BR->TL direction preserved after resize"
            );
        }
    }

    #[test]
    fn move_vertex_line_updates_points_and_rect() {
        let mut p = AnnotationPayload::Shape {
            kind: ShapeKind::Line,
            rect: Rect {
                x: 10.0,
                y: 10.0,
                w: 90.0,
                h: 50.0,
            },
            stroke: Color::Rgb(255, 0, 0),
            fill: None,
            width: 2.0,
            points: vec![Point { x: 10.0, y: 10.0 }, Point { x: 100.0, y: 60.0 }],
        };
        assert!(move_vertex_payload(&mut p, 1, (30.0, 20.0)));
        match &p {
            AnnotationPayload::Shape { rect, points, .. } => {
                assert_eq!(points[1], Point { x: 30.0, y: 20.0 });
                // rect 重算为 points 的 bbox:(10,10)-(30,20)
                assert_eq!(
                    *rect,
                    Rect {
                        x: 10.0,
                        y: 10.0,
                        w: 20.0,
                        h: 10.0
                    }
                );
            }
            _ => panic!("expected Shape"),
        }
    }

    #[test]
    fn move_vertex_polygon_updates_only_that_vertex() {
        let mut p = AnnotationPayload::Shape {
            kind: ShapeKind::Polygon,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 10.0,
                h: 10.0,
            },
            stroke: Color::Rgb(0, 0, 255),
            fill: None,
            width: 1.0,
            points: vec![
                Point { x: 0.0, y: 0.0 },
                Point { x: 5.0, y: 10.0 },
                Point { x: 10.0, y: 0.0 },
            ],
        };
        assert!(move_vertex_payload(&mut p, 1, (5.0, 4.0)));
        match &p {
            AnnotationPayload::Shape { rect, points, .. } => {
                assert_eq!(points[0], Point { x: 0.0, y: 0.0 });
                assert_eq!(points[2], Point { x: 10.0, y: 0.0 });
                assert_eq!(
                    *rect,
                    Rect {
                        x: 0.0,
                        y: 0.0,
                        w: 10.0,
                        h: 4.0
                    }
                );
            }
            _ => panic!("expected Shape"),
        }
    }

    #[test]
    fn move_vertex_line_with_empty_points_seeds_diagonal() {
        // 外部 OFD 只带边界无顶点:先播种 rect 对角线(镜像渲染的
        // line_endpoints 回退),再把 Vertex(1) 挪到新位置。
        let mut p = AnnotationPayload::Shape {
            kind: ShapeKind::Arrow,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 40.0,
                h: 30.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 2.0,
            points: vec![],
        };
        assert!(move_vertex_payload(&mut p, 1, (50.0, 10.0)));
        match &p {
            AnnotationPayload::Shape { points, .. } => {
                assert_eq!(points[0], Point { x: 0.0, y: 0.0 }, "seeded from rect TL");
                assert_eq!(points[1], Point { x: 50.0, y: 10.0 });
            }
            _ => panic!("expected Shape"),
        }
    }

    #[test]
    fn move_vertex_rect_kind_is_noop() {
        let mut p = AnnotationPayload::Shape {
            kind: ShapeKind::Rect,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 10.0,
                h: 10.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 1.0,
            points: vec![],
        };
        assert!(
            !move_vertex_payload(&mut p, 0, (5.0, 5.0)),
            "Rect has no vertices"
        );
    }

    #[test]
    fn move_vertex_out_of_range_is_noop() {
        let mut p = AnnotationPayload::Shape {
            kind: ShapeKind::PolyLine,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 10.0,
                h: 10.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 1.0,
            points: vec![Point { x: 0.0, y: 0.0 }, Point { x: 10.0, y: 10.0 }],
        };
        let before = p.clone();
        assert!(!move_vertex_payload(&mut p, 2, (5.0, 5.0)));
        assert_eq!(p, before, "payload untouched on bad index");
    }

    #[test]
    fn move_vertex_freehand_is_noop() {
        let mut p = AnnotationPayload::Freehand {
            path: PathData {
                commands: vec![PathCommand::M(0.0, 0.0), PathCommand::L(5.0, 5.0)],
            },
            color: Color::Rgb(0, 0, 255),
            width: 1.5,
        };
        assert!(!move_vertex_payload(&mut p, 0, (1.0, 1.0)));
    }

    #[test]
    fn move_vertex_one_point_line_is_noop() {
        // A 1-point Line is degenerate: Vertex(0)/Vertex(1) drags must return
        // false AND leave the payload untouched. (Previously the <2 seeding
        // guard pushed TL+BR onto the single point, yielding 3 points while
        // rendering reads points[0..2] -> dragging Vertex(0) visibly moved the
        // wrong endpoint.)
        for index in [0usize, 1] {
            let mut p = AnnotationPayload::Shape {
                kind: ShapeKind::Line,
                rect: Rect {
                    x: 0.0,
                    y: 0.0,
                    w: 40.0,
                    h: 30.0,
                },
                stroke: Color::Rgb(0, 0, 0),
                fill: None,
                width: 2.0,
                points: vec![Point { x: 5.0, y: 5.0 }],
            };
            let before = p.clone();
            assert!(
                !move_vertex_payload(&mut p, index, (50.0, 10.0)),
                "Vertex({index}) on a 1-point Line must be a clean no-op"
            );
            assert_eq!(p, before, "payload untouched for Vertex({index})");
        }
    }

    #[test]
    fn move_vertex_line_unreachable_index_does_not_seed() {
        // Line with empty points + index >= 2: seeding must NOT run (the
        // diagonal seed only applies when the index is reachable), so a
        // false return leaves the payload completely untouched.
        let mut p = AnnotationPayload::Shape {
            kind: ShapeKind::Line,
            rect: Rect {
                x: 0.0,
                y: 0.0,
                w: 40.0,
                h: 30.0,
            },
            stroke: Color::Rgb(0, 0, 0),
            fill: None,
            width: 2.0,
            points: vec![],
        };
        let before = p.clone();
        assert!(!move_vertex_payload(&mut p, 2, (50.0, 10.0)));
        assert_eq!(p, before, "unreachable index must not seed the diagonal");
        if let AnnotationPayload::Shape { points, .. } = &p {
            assert!(points.is_empty(), "points still empty after no-op");
        }
    }
}