1
2
3 use gpui::text_layout::*;
4 use gpui::fonts::{Properties, Weight};
5
6 #[gpui::test]
7 fn test_wrap_line(cx: &mut gpui::AppContext) {
8 let font_cache = cx.font_cache().clone();
9 let font_system = cx.platform().fonts();
10 let family = font_cache
11 .load_family(&["Courier"], &Default::default())
12 .unwrap();
13 let font_id = font_cache.select_font(family, &Default::default()).unwrap();
14
15 let mut wrapper = LineWrapper::new(font_id, 16., font_system);
16 assert_eq!(
17 wrapper
18 .wrap_line("aa bbb cccc ddddd eeee", 72.0)
19 .collect::<Vec<_>>(),
20 &[
21 Boundary::new(7, 0),
22 Boundary::new(12, 0),
23 Boundary::new(18, 0)
24 ],
25 );
26 assert_eq!(
27 wrapper
28 .wrap_line("aaa aaaaaaaaaaaaaaaaaa", 72.0)
29 .collect::<Vec<_>>(),
30 &[
31 Boundary::new(4, 0),
32 Boundary::new(11, 0),
33 Boundary::new(18, 0)
34 ],
35 );
36 assert_eq!(
37 wrapper.wrap_line(" aaaaaaa", 72.).collect::<Vec<_>>(),
38 &[
39 Boundary::new(7, 5),
40 Boundary::new(9, 5),
41 Boundary::new(11, 5),
42 ]
43 );
44 assert_eq!(
45 wrapper
46 .wrap_line(" ", 72.)
47 .collect::<Vec<_>>(),
48 &[
49 Boundary::new(7, 0),
50 Boundary::new(14, 0),
51 Boundary::new(21, 0)
52 ]
53 );
54 assert_eq!(
55 wrapper
56 .wrap_line(" aaaaaaaaaaaaaa", 72.)
57 .collect::<Vec<_>>(),
58 &[
59 Boundary::new(7, 0),
60 Boundary::new(14, 3),
61 Boundary::new(18, 3),
62 Boundary::new(22, 3),
63 ]
64 );
65 }
66
67 #[gpui::test(retries = 5)]
68 fn test_wrap_shaped_line(cx: &mut gpui::AppContext) {
69 // This is failing intermittently on CI and we don't have time to figure it out
70 let font_cache = cx.font_cache().clone();
71 let font_system = cx.platform().fonts();
72 let text_layout_cache = TextLayoutCache::new(font_system.clone());
73
74 let family = font_cache
75 .load_family(&["Helvetica"], &Default::default())
76 .unwrap();
77 let font_id = font_cache.select_font(family, &Default::default()).unwrap();
78 let normal = RunStyle {
79 font_id,
80 color: Default::default(),
81 underline: Default::default(),
82 };
83 let bold = RunStyle {
84 font_id: font_cache
85 .select_font(
86 family,
87 &Properties {
88 weight: Weight::BOLD,
89 ..Default::default()
90 },
91 )
92 .unwrap(),
93 color: Default::default(),
94 underline: Default::default(),
95 };
96
97 let text = "aa bbb cccc ddddd eeee";
98 let line = text_layout_cache.layout_str(
99 text,
100 16.0,
101 &[(4, normal), (5, bold), (6, normal), (1, bold), (7, normal)],
102 );
103
104 let mut wrapper = LineWrapper::new(font_id, 16., font_system);
105 assert_eq!(
106 wrapper
107 .wrap_shaped_line(text, &line, 72.0)
108 .collect::<Vec<_>>(),
109 &[
110 ShapedBoundary {
111 run_ix: 1,
112 glyph_ix: 3
113 },
114 ShapedBoundary {
115 run_ix: 2,
116 glyph_ix: 3
117 },
118 ShapedBoundary {
119 run_ix: 4,
120 glyph_ix: 2
121 }
122 ],
123 );
124
125}