bevy_text 0.20.0

Provides text functionality for Bevy Engine
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! `EditableText` scrolling logic
//!
//! - [`TextViewport`] is a rectangle aligned to the text layout's axis representing
//!   the user's view of the text layout.
//! - Coordinates are in text layout space, increasing right and downwards.
//! - If the text layout is smaller than the viewport on an axis, the viewport is
//!   given an offset of zero on that the axis.
//! - An origin-size representation is used because the size is generally fixed.
//!   This avoids floating point error accumulation that might happen with min-max coords.
//!
//! # Scrolling rules
//!
//! - The text viewport is controlled exclusively through `TextEdit`s.
//! - Displacement scrolling is continuous and unquantized.
//! - Scrolling to a point moves each axis by the minimum amount needed to make
//!   that point visible.
//! - Scrolling by lines moves by the distance between visual-line starts.
//!   Fractional amounts interpolate across the next line interval in the scroll
//!   direction. This supports wrapped and variable-height lines without
//!   snapping the viewport to line bounds.
//!
//! Text and keyboard edits reveal the caret, including edits that do not change
//! the editor state. Pointer-driven edits and explicit scroll edits do not.
//! Caret reveal follows these rules:
//!
//! - Normalized horizontal and vertical margins inset a caret reveal region from every
//!   viewport edge. Caret movement within this region leaves the viewport unchanged.
//! - If the caret leaves the caret reveal region horizontally, the viewport scrolls
//!   sideways by the smallest amount that brings it back inside, keeping one
//!   `0`-advance of space visible from the caret position onward to leave room for the
//!   next typed character.
//! - Vertically, the viewport scrolls to reveal Parley's caret rectangle,
//!   which spans the whole visual line. The margin-aware offset is clamped
//!   toward the next visual-line start in the scroll direction, so a caret
//!   that moved up or down leaves the viewport aligned to a line in that direction.
//! - If the caret or its visual line is too large to fit inside the caret
//!   reveal region on an axis, then the viewport is centered on it on that axis instead.
//! - The viewport never scrolls outside the layout bounds (and when the
//!   viewport is larger than the layout on an axis, it does not scroll on
//!   that axis at all), even when that leaves the caret closer to the
//!   viewport edge than the margin requests.

use bevy_math::Rect;
use bevy_math::Vec2;
use bevy_reflect::Reflect;

use crate::LineBreak;

/// The region of the editable text layout visible to the user.
///
/// Scrolling changes the offset, size depends on the layout.
#[derive(Debug, Clone, Copy, Default, PartialEq, Reflect)]
pub struct TextViewport {
    /// The top-left corner of the text viewport in text-layout coordinates.
    pub offset: Vec2,
    /// The size of the viewport in text-layout coordinates.
    pub size: Vec2,
}

impl TextViewport {
    /// Returns the viewport as a `Rect`.
    pub fn rect(&self) -> Rect {
        Rect {
            min: self.offset,
            max: self.offset + self.size,
        }
    }

    /// Clamp the scroll offset to fit inside `max`.
    pub fn clamp_inside(&mut self, max: Vec2) {
        self.offset = self
            .offset
            .clamp(Vec2::ZERO, (max - self.size).max(Vec2::ZERO));
    }

    /// Scroll by a displacement
    pub fn scroll_by(&mut self, displacement: Vec2, max: Vec2) {
        self.offset += displacement;
        self.clamp_inside(max);
    }

    /// Scroll to a position
    pub fn scroll_to(&mut self, point: Vec2, max: Vec2) {
        self.offset = Vec2::new(
            scroll_to_axis(self.offset.x, self.size.x, point.x),
            scroll_to_axis(self.offset.y, self.size.y, point.y),
        );
        self.clamp_inside(max);
    }

    /// Moves the viewport by the minimum amount needed to reveal the caret.
    pub fn reveal_caret(
        &mut self,
        caret: Rect,
        max: Vec2,
        caret_margin: Vec2,
        lines: impl IntoIterator<Item = TextLineYBounds>,
    ) {
        let mut line_bounds = lines.into_iter().peekable();
        let caret_max = max.max(caret.max.max(Vec2::ZERO));

        self.clamp_inside(caret_max);
        let view = self.rect();
        let margin = caret_margin.clamp(Vec2::ZERO, Vec2::splat(0.5)) * self.size;
        let caret_reveal_region = Rect {
            min: view.min + margin,
            max: view.max - margin,
        };
        if caret_reveal_region.min.cmple(caret.min).all()
            && caret_reveal_region.max.cmpge(caret.max).all()
        {
            return;
        }

        self.offset.x = min_scroll_axis(
            caret_reveal_region.min.x,
            caret_reveal_region.max.x,
            caret.min.x,
            caret.max.x,
        ) - margin.x;
        let vertical_offset = min_scroll_axis(
            caret_reveal_region.min.y,
            caret_reveal_region.max.y,
            caret.min.y,
            caret.max.y,
        ) - margin.y;
        if line_bounds.peek().is_none() {
            self.offset.y = vertical_offset;
        } else if vertical_offset < self.offset.y {
            self.offset.y = line_bounds
                .filter(|line| line.min <= vertical_offset)
                .last()
                .map_or(0.0, |line| line.min);
        } else if self.offset.y < vertical_offset {
            self.offset.y = line_bounds
                .find(|line| vertical_offset <= line.min)
                .map_or((caret_max - self.size).max(Vec2::ZERO).y, |line| line.min);
        }

        self.clamp_inside(caret_max);
    }

    /// Scroll vertically by a number of lines.
    pub fn scroll_by_lines<I>(&mut self, scroll_lines: f32, content_size: Vec2, line_bounds: I)
    where
        I: IntoIterator<Item = TextLineYBounds>,
        I::IntoIter: Clone + DoubleEndedIterator + ExactSizeIterator,
    {
        let line_bounds = line_bounds.into_iter();

        if line_bounds.clone().next().is_none() {
            return;
        }

        let content_size = Vec2::new(content_size.x, line_bounds.clone().next_back().unwrap().max);

        self.clamp_inside(content_size);
        if scroll_lines == 0.0 {
            return;
        }

        let current_line = line_bounds
            .clone()
            .rposition(|line| line.min <= self.offset.y)
            .unwrap_or(0);
        let line_delta = scroll_lines.abs();
        let whole_lines = line_delta.floor() as usize;
        let fraction = line_delta.fract();
        let current_line_bounds = line_bounds.clone().nth(current_line).unwrap();

        if scroll_lines.is_sign_positive() {
            if line_bounds.len() - 1 - current_line < whole_lines {
                self.offset.y = (content_size - self.size).max(Vec2::ZERO).y;
                return;
            }

            let target_line = current_line + whole_lines;
            let target_line_bounds = line_bounds.clone().nth(target_line).unwrap();
            self.offset.y += target_line_bounds.min - current_line_bounds.min
                + fraction
                    * (line_bounds
                        .clone()
                        .nth(target_line + 1)
                        .map_or(target_line_bounds.max, |line| line.min)
                        - target_line_bounds.min);
        } else {
            if current_line < whole_lines {
                self.offset.y = 0.0;
                return;
            }

            let target_line = current_line - whole_lines;
            let target_line_bounds = line_bounds.clone().nth(target_line).unwrap();
            self.offset.y += target_line_bounds.min
                - current_line_bounds.min
                - fraction
                    * (if target_line == 0 {
                        target_line_bounds.max - target_line_bounds.min
                    } else {
                        target_line_bounds.min
                            - line_bounds.clone().nth(target_line - 1).unwrap().min
                    });
        }
        self.clamp_inside(content_size);
    }
}

/// The vertical bounds of one visual line in text-layout coordinates.
#[derive(Clone, Copy, Debug, PartialEq, Reflect)]
pub struct TextLineYBounds {
    /// Top edge of the visual line.
    pub min: f32,
    /// Bottom edge of the visual line.
    pub max: f32,
}

impl TextLineYBounds {
    /// Creates line bounds from a top and bottom edge.
    pub const fn new(min: f32, max: f32) -> Self {
        Self { min, max }
    }

    /// Creates line bounds from a Parley `Line`.
    pub fn from_line<'a, B: parley::Brush>(line: &parley::Line<'a, B>) -> Self {
        Self {
            min: line.metrics().block_min_coord,
            max: line.metrics().block_max_coord,
        }
    }
}

/// The horizontal extent an editable text viewport may scroll across.
pub fn scrollable_text_layout_width(
    linebreak: LineBreak,
    layout_width: f32,
    viewport_width: f32,
    caret: Option<Rect>,
) -> f32 {
    let scrollable_width = match linebreak {
        LineBreak::NoWrap | LineBreak::WordBoundary => layout_width.max(viewport_width),
        LineBreak::AnyCharacter | LineBreak::WordOrCharacter => viewport_width,
    };

    if linebreak == LineBreak::NoWrap || viewport_width < scrollable_width {
        caret.map_or(scrollable_width, |caret| scrollable_width.max(caret.max.x))
    } else {
        scrollable_width
    }
}

fn scroll_to_axis(view_min: f32, view_size: f32, point: f32) -> f32 {
    if point < view_min {
        point
    } else if view_min + view_size < point {
        point - view_size
    } else {
        view_min
    }
}

fn min_scroll_axis(view_min: f32, view_max: f32, target_min: f32, target_max: f32) -> f32 {
    let view_size = view_max - view_min;
    let target_size = target_max - target_min;

    if view_size < target_size {
        target_min + (target_size - view_size) / 2.
    } else if target_min < view_min {
        target_min
    } else if view_max < target_max {
        target_max - view_size
    } else {
        view_min
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use bevy_utils::default;

    const VARIABLE_LINE_BOUNDS: [TextLineYBounds; 5] = [
        TextLineYBounds::new(0.0, 10.0),
        TextLineYBounds::new(10.0, 25.0),
        TextLineYBounds::new(25.0, 45.0),
        TextLineYBounds::new(45.0, 70.0),
        TextLineYBounds::new(70.0, 100.0),
    ];

    fn make_lines(line_count: usize, line_height: f32) -> Vec<TextLineYBounds> {
        (0..line_count)
            .map(move |index| {
                let min = index as f32 * line_height;
                TextLineYBounds::new(min, min + line_height)
            })
            .collect()
    }

    #[test]
    fn scroll_by_lines() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 60.0),
            ..default()
        };
        let lines = make_lines(10, 20.);
        view.scroll_by_lines(2.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        assert_eq!(view.offset, Vec2::new(0.0, 40.0));

        view.scroll_by_lines(3.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        view.scroll_by_lines(-2.0, Vec2::new(100.0, 200.0), lines);
        assert_eq!(view.offset, Vec2::new(0.0, 60.0));
    }

    #[test]
    fn scroll_by_lines_preserves_partial_line_offset() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 55.0),
            ..default()
        };
        let lines = make_lines(10, 20.);

        view.scroll_by(Vec2::new(0.0, 5.0), Vec2::new(100.0, 200.0));

        view.scroll_by_lines(1.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        assert_eq!(view.offset, Vec2::new(0.0, 25.0));

        view.scroll_by_lines(1.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        assert_eq!(view.offset, Vec2::new(0.0, 45.0));

        view.scroll_to(Vec2::new(0.0, 45.0), Vec2::new(100.0, 200.0));

        view.scroll_by_lines(-1.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        assert_eq!(view.offset, Vec2::new(0.0, 25.0));

        view.scroll_by_lines(-1.0, Vec2::new(100.0, 200.0), lines);
        assert_eq!(view.offset, Vec2::new(0.0, 5.0));
    }

    #[test]
    fn scroll_by_fractional_lines() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };
        let lines = make_lines(10, 20.);
        view.scroll_by(Vec2::new(0.0, 45.0), Vec2::new(100.0, 200.0));

        view.scroll_by_lines(0.5, Vec2::new(100.0, 200.0), lines.iter().cloned());
        assert_eq!(view.offset, Vec2::new(0.0, 55.0));

        view.scroll_by_lines(-0.25, Vec2::new(100.0, 200.0), lines);
        assert_eq!(view.offset, Vec2::new(0.0, 50.0));
    }

    #[test]
    fn text_view_zero_line_scroll_clamp() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 60.0),
            ..default()
        };
        let lines = make_lines(10, 20.);
        view.scroll_by_lines(10.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        view.size = Vec2::new(100.0, 180.0);
        view.scroll_by_lines(0.0, Vec2::new(100.0, 200.0), lines);

        assert_eq!(view.offset, Vec2::new(0.0, 20.0));
    }

    #[test]
    fn text_view_scroll_lines_clamps_large_deltas() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 60.0),
            ..default()
        };
        let lines = make_lines(10, 20.);
        view.scroll_by_lines(1000.0, Vec2::new(100.0, 200.0), lines.iter().cloned());
        assert_eq!(view.offset, Vec2::new(0.0, 140.0));

        view.scroll_by_lines(-1000.0, Vec2::new(100.0, 200.0), lines);
        assert_eq!(view.offset, Vec2::ZERO);
    }

    #[test]
    fn text_view_scroll_lines_keeps_offset_zero_when_content_fits() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 200.0),
            ..default()
        };
        view.scroll_by_lines(5.0, Vec2::new(100.0, 80.0), make_lines(4, 20.0));

        assert_eq!(view.offset, Vec2::ZERO);
    }

    #[test]
    fn text_view_scrolls_by_variable_lines() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 40.0),
            ..default()
        };
        view.scroll_by(Vec2::new(0.0, 5.0), Vec2::new(100.0, 100.0));
        view.scroll_by_lines(3.0, Vec2::new(100.0, 100.0), VARIABLE_LINE_BOUNDS);

        assert_eq!(view.offset, Vec2::new(0.0, 50.0));

        view.scroll_by_lines(-1.0, Vec2::new(100.0, 100.0), VARIABLE_LINE_BOUNDS);
        assert_eq!(view.offset, Vec2::new(0.0, 30.0));
    }

    #[test]
    fn text_view_scrolls_by_fractional_variable_lines() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 20.0),
            ..default()
        };
        view.scroll_by(Vec2::new(0.0, 5.0), Vec2::new(100.0, 100.0));

        view.scroll_by_lines(1.5, Vec2::new(100.0, 100.0), VARIABLE_LINE_BOUNDS);
        assert_eq!(view.offset, Vec2::new(0.0, 22.5));

        view.scroll_by_lines(-0.5, Vec2::new(100.0, 100.0), VARIABLE_LINE_BOUNDS);
        assert_eq!(view.offset, Vec2::new(0.0, 17.5));
    }

    #[test]
    fn text_view_fractional_line_scroll_clamps_at_start() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };
        let content_size = Vec2::new(100.0, 200.0);

        view.scroll_by(Vec2::new(0.0, 5.0), content_size);
        view.scroll_by_lines(-0.5, content_size, make_lines(10, 20.));
        assert_eq!(view.offset, Vec2::ZERO);
    }

    #[test]
    fn text_view_scroll_by_clamping() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };

        view.scroll_by(Vec2::new(20.0, 30.0), Vec2::new(80.0, 40.0));
        assert_eq!(view.offset, Vec2::ZERO);

        view.scroll_by(Vec2::new(30.0, 40.0), Vec2::new(250.0, 180.0));
        assert_eq!(view.offset, Vec2::new(30.0, 40.0));

        view.scroll_by(Vec2::new(1000.0, 1000.0), Vec2::new(180.0, 90.0));
        assert_eq!(view.offset, Vec2::new(80.0, 40.0));

        view.scroll_by(Vec2::new(-1000.0, -1000.0), Vec2::new(180.0, 90.0));
        assert_eq!(view.offset, Vec2::ZERO);
    }

    #[test]
    fn text_view_scroll_to_moves_min_distance() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };
        let content_size = Vec2::new(250.0, 180.0);
        view.scroll_by(Vec2::new(30.0, 40.0), content_size);

        view.scroll_to(Vec2::new(80.0, 60.0), content_size);
        assert_eq!(view.offset, Vec2::new(30.0, 40.0));

        view.scroll_by(Vec2::new(20.0, 10.0), content_size);

        view.scroll_to(Vec2::new(40.0, 40.0), content_size);
        assert_eq!(view.offset, Vec2::new(40.0, 40.0));

        view.scroll_to(Vec2::new(160.0, 100.0), content_size);
        assert_eq!(view.offset, Vec2::new(60.0, 50.0));

        view.scroll_to(Vec2::new(60.0, 110.0), content_size);
        assert_eq!(view.offset, Vec2::new(60.0, 60.0));
    }

    #[test]
    fn text_view_scroll_to_clamps_to_content_bounds() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };
        let content_size = Vec2::new(250.0, 180.0);

        view.scroll_to(Vec2::new(1000.0, 1000.0), content_size);
        assert_eq!(view.offset, Vec2::new(150.0, 130.0));

        view.scroll_to(Vec2::new(-1000.0, -1000.0), content_size);
        assert_eq!(view.offset, Vec2::ZERO);
    }

    #[test]
    fn text_view_reveal_caret_keeps_visible_caret_in_view() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 55.0),
            ..default()
        };
        let content_size = Vec2::new(100.0, 200.0);
        view.scroll_by(Vec2::new(0.0, 25.0), content_size);

        view.reveal_caret(
            Rect::new(20.0, 40.0, 22.0, 60.0),
            content_size,
            Vec2::ZERO,
            make_lines(10, 20.0),
        );

        assert_eq!(view.offset, Vec2::new(0.0, 25.0));
    }

    #[test]
    fn text_view_reveal_caret_quantizes_vertical_scroll_to_lines() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 55.0),
            ..default()
        };
        let lines = make_lines(10, 20.);
        let content_size = Vec2::new(100.0, 200.0);

        view.reveal_caret(
            Rect::new(20.0, 60.0, 22.0, 80.0),
            content_size,
            Vec2::ZERO,
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(0.0, 40.0));

        view.scroll_by(Vec2::new(0.0, 100.0), content_size);
        view.reveal_caret(
            Rect::new(20.0, 20.0, 22.0, 40.0),
            content_size,
            Vec2::ZERO,
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(0.0, 20.0));

        view.reveal_caret(
            Rect::new(20.0, 180.0, 22.0, 200.0),
            content_size,
            Vec2::ZERO,
            lines,
        );
        assert_eq!(view.offset, Vec2::new(0.0, 145.0));
    }

    #[test]
    fn text_view_reveal_caret_rounds_margin_scroll_in_direction() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 90.0),
            ..default()
        };
        let lines = make_lines(15, 20.);
        let content_size = Vec2::new(100.0, 300.0);
        view.scroll_by(Vec2::new(0.0, 50.0), content_size);

        view.reveal_caret(
            Rect::new(50.0, 45.0, 52.0, 55.0),
            content_size,
            Vec2::splat(0.2),
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(0.0, 20.0));

        view.scroll_by(Vec2::new(0.0, 30.0), content_size);
        view.reveal_caret(
            Rect::new(50.0, 145.0, 52.0, 155.0),
            content_size,
            Vec2::splat(0.2),
            lines,
        );
        assert_eq!(view.offset, Vec2::new(0.0, 100.0));
    }

    #[test]
    fn text_view_reveal_caret_rounds_to_variable_line_starts() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };
        let content_size = Vec2::new(100.0, 135.0);
        let lines = [
            TextLineYBounds::new(0.0, 10.0),
            TextLineYBounds::new(10.0, 25.0),
            TextLineYBounds::new(25.0, 45.0),
            TextLineYBounds::new(45.0, 70.0),
            TextLineYBounds::new(70.0, 100.0),
            TextLineYBounds::new(100.0, 135.0),
        ];
        view.scroll_by(Vec2::new(0.0, 45.0), content_size);

        view.reveal_caret(
            Rect::new(50.0, 25.0, 52.0, 45.0),
            content_size,
            Vec2::splat(0.2),
            lines,
        );
        assert_eq!(view.offset, Vec2::new(0.0, 10.0));

        view.scroll_by(Vec2::new(0.0, 35.0), content_size);
        view.reveal_caret(
            Rect::new(50.0, 70.0, 52.0, 100.0),
            content_size,
            Vec2::splat(0.2),
            lines,
        );
        assert_eq!(view.offset, Vec2::new(0.0, 70.0));
    }

    #[test]
    fn text_view_reveal_caret_without_line_bounds() {
        let mut view = TextViewport {
            size: Vec2::new(100.0, 50.0),
            ..default()
        };

        view.reveal_caret(
            Rect::new(50.0, 75.0, 52.0, 85.0),
            Vec2::new(100.0, 200.0),
            Vec2::splat(0.2),
            [],
        );

        assert_eq!(view.offset, Vec2::new(0.0, 45.0));
    }

    #[test]
    fn text_view_reveal_caret_scrolls_horizontally_and_clamps() {
        let mut view = TextViewport {
            size: Vec2::new(50.0, 20.0),
            ..default()
        };

        view.reveal_caret(
            Rect::new(95.0, 0.0, 110.0, 20.0),
            Vec2::new(100.0, 20.0),
            Vec2::ZERO,
            make_lines(1, 20.0),
        );

        assert_eq!(view.offset, Vec2::new(60.0, 0.0));
    }

    #[test]
    fn text_view_reveal_caret_keeps_target_inside_margin_safe_region() {
        let mut view = TextViewport {
            size: Vec2::splat(100.0),
            ..default()
        };
        let content_size = Vec2::splat(300.0);
        view.scroll_by(Vec2::splat(50.0), content_size);

        view.reveal_caret(
            Rect::new(80.0, 80.0, 82.0, 100.0),
            content_size,
            Vec2::splat(0.2),
            make_lines(15, 20.0),
        );

        assert_eq!(view.offset, Vec2::splat(50.0));
    }

    #[test]
    fn text_view_reveal_caret_scrolls_minimally_at_each_margin_edge() {
        let mut view = TextViewport {
            size: Vec2::splat(100.0),
            ..default()
        };
        let lines = make_lines(15, 20.);
        let content_size = Vec2::splat(300.0);

        view.scroll_by(Vec2::splat(50.0), content_size);
        view.reveal_caret(
            Rect::new(60.0, 80.0, 62.0, 100.0),
            content_size,
            Vec2::splat(0.2),
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(40.0, 50.0));

        view.scroll_by(Vec2::new(10.0, 0.0), content_size);
        view.reveal_caret(
            Rect::new(140.0, 80.0, 142.0, 100.0),
            content_size,
            Vec2::splat(0.2),
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(62.0, 50.0));

        view.reveal_caret(
            Rect::new(100.0, 45.0, 102.0, 55.0),
            content_size,
            Vec2::splat(0.2),
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(62.0, 20.0));

        view.scroll_by(Vec2::new(0.0, 30.0), content_size);
        view.reveal_caret(
            Rect::new(100.0, 145.0, 102.0, 155.0),
            content_size,
            Vec2::splat(0.2),
            lines,
        );
        assert_eq!(view.offset, Vec2::new(62.0, 80.0));
    }

    #[test]
    fn text_view_reveal_caret_independent_margins() {
        let mut view = TextViewport {
            size: Vec2::splat(100.0),
            ..default()
        };
        let lines = make_lines(15, 20.);
        let content_size = Vec2::splat(300.0);
        view.scroll_by(Vec2::splat(50.0), content_size);

        view.reveal_caret(
            Rect::new(140.0, 75.0, 142.0, 95.0),
            content_size,
            Vec2::new(0.2, 0.0),
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::new(62.0, 50.0));

        view.reveal_caret(
            Rect::new(80.0, 120.0, 82.0, 140.0),
            content_size,
            Vec2::new(0.0, 0.2),
            lines,
        );
        assert_eq!(view.offset, Vec2::new(62.0, 60.0));
    }

    #[test]
    fn text_view_reveal_caret_margin_clamped_at_layout_edges() {
        let mut view = TextViewport {
            size: Vec2::splat(100.0),
            ..default()
        };
        let lines = make_lines(10, 20.);
        let content_size = Vec2::splat(200.0);

        view.reveal_caret(
            Rect::new(0.0, 0.0, 2.0, 20.0),
            content_size,
            Vec2::splat(0.2),
            lines.iter().cloned(),
        );
        assert_eq!(view.offset, Vec2::ZERO);

        view.scroll_by(Vec2::splat(100.0), content_size);
        view.reveal_caret(
            Rect::new(198.0, 180.0, 200.0, 200.0),
            content_size,
            Vec2::splat(0.2),
            lines,
        );
        assert_eq!(view.offset, Vec2::splat(100.0));
    }

    #[test]
    fn character_wrapped_text_returns_viewport_width() {
        for linebreak in [LineBreak::AnyCharacter, LineBreak::WordOrCharacter] {
            assert_eq!(
                scrollable_text_layout_width(
                    linebreak,
                    102.0,
                    100.0,
                    Some(Rect::new(95.0, 0.0, 110.0, 20.0))
                ),
                100.0
            );
        }
    }

    #[test]
    fn word_boundary_wrapping_preserves_overflow() {
        assert_eq!(
            scrollable_text_layout_width(LineBreak::WordBoundary, 150.0, 100.0, None,),
            150.0
        );
    }

    #[test]
    fn overflowing_wrapped_text_includes_trailing_caret() {
        assert_eq!(
            scrollable_text_layout_width(
                LineBreak::WordBoundary,
                102.0,
                100.0,
                Some(Rect::new(95.0, 0.0, 110.0, 20.0)),
            ),
            110.0
        );
    }

    #[test]
    fn no_wrap_text_includes_trailing_caret() {
        assert_eq!(
            scrollable_text_layout_width(
                LineBreak::NoWrap,
                102.0,
                100.0,
                Some(Rect::new(95.0, 0.0, 110.0, 20.0)),
            ),
            110.0
        );
    }

    #[test]
    fn scrollable_text_word_boundary() {
        assert_eq!(
            scrollable_text_layout_width(
                LineBreak::WordBoundary,
                150.,
                100.,
                Some(Rect::new(150., 0., 160.0, 10.0)),
            ),
            160.
        );
    }
}