1use sequoia_core::Size;
2use sequoia_text::{
3 ElideMode, FontStyle, FontWeight, LetterSpacing, RichAlign, TextSpan, TextSystem, WrapMode,
4};
5
6pub struct LayoutCtx<'a> {
7 text: &'a mut TextSystem,
8}
9
10impl<'a> LayoutCtx<'a> {
11 pub fn new(text: &'a mut TextSystem) -> Self {
12 Self { text }
13 }
14
15 pub fn text_width(&mut self, text: &str, size: f32) -> f32 {
16 self.text.text_width(text, size)
17 }
18
19 pub fn text_width_weighted(&mut self, text: &str, size: f32, weight: FontWeight) -> f32 {
20 self.text.text_width_weighted(text, size, weight)
21 }
22
23 pub fn text_width_spaced(&mut self, text: &str, size: f32, spacing: LetterSpacing) -> f32 {
24 self.text.text_width_spaced(text, size, spacing)
25 }
26
27 pub fn text_width_weighted_spaced(
28 &mut self,
29 text: &str,
30 size: f32,
31 weight: FontWeight,
32 spacing: LetterSpacing,
33 ) -> f32 {
34 self.text
35 .text_width_weighted_spaced(text, size, weight, spacing)
36 }
37
38 pub fn text_width_styled_spaced(
39 &mut self,
40 text: &str,
41 size: f32,
42 weight: FontWeight,
43 spacing: LetterSpacing,
44 family: Option<&str>,
45 style: FontStyle,
46 ) -> f32 {
47 self.text
48 .text_width_styled_spaced(text, size, weight, spacing, family, style)
49 }
50
51 pub fn caret_positions(&mut self, text: &str, size: f32, weight: FontWeight) -> Vec<f32> {
52 self.text.caret_positions(text, size, weight)
53 }
54
55 pub fn caret_positions_spaced(
56 &mut self,
57 text: &str,
58 size: f32,
59 weight: FontWeight,
60 spacing: LetterSpacing,
61 ) -> Vec<f32> {
62 self.text
63 .caret_positions_spaced(text, size, weight, spacing)
64 }
65
66 pub fn text_wrapped_size(
67 &mut self,
68 text: &str,
69 size: f32,
70 weight: FontWeight,
71 width: f32,
72 mode: WrapMode,
73 ) -> Size {
74 self.text_wrapped_size_spaced(text, size, weight, width, mode, 0.0)
75 }
76
77 pub fn text_wrapped_size_spaced(
78 &mut self,
79 text: &str,
80 size: f32,
81 weight: FontWeight,
82 width: f32,
83 mode: WrapMode,
84 spacing: LetterSpacing,
85 ) -> Size {
86 let shaped = self
87 .text
88 .shape_wrapped_spaced(text, size, weight, width, mode, spacing);
89 Size::new(shaped.width(), shaped.height())
90 }
91
92 pub fn text_wrapped_size_styled_spaced(
93 &mut self,
94 text: &str,
95 size: f32,
96 weight: FontWeight,
97 width: f32,
98 mode: WrapMode,
99 spacing: LetterSpacing,
100 family: Option<&str>,
101 style: FontStyle,
102 ) -> Size {
103 let shaped = self
104 .text
105 .shape_wrapped_styled_spaced(text, size, weight, width, mode, spacing, family, style);
106 Size::new(shaped.width(), shaped.height())
107 }
108
109 pub fn text_elided_size(
110 &mut self,
111 text: &str,
112 size: f32,
113 weight: FontWeight,
114 max_width: f32,
115 mode: ElideMode,
116 ) -> Size {
117 self.text_elided_size_spaced(text, size, weight, max_width, mode, 0.0)
118 }
119
120 pub fn text_elided_size_spaced(
121 &mut self,
122 text: &str,
123 size: f32,
124 weight: FontWeight,
125 max_width: f32,
126 mode: ElideMode,
127 spacing: LetterSpacing,
128 ) -> Size {
129 let shown = self
130 .text
131 .elide_spaced(text, size, weight, max_width, mode, spacing);
132 let w = self
133 .text
134 .text_width_weighted_spaced(&shown, size, weight, spacing)
135 .min(max_width.max(0.0));
136 Size::new(w, Self::text_line_height(size))
137 }
138
139 pub fn text_elided_size_styled_spaced(
140 &mut self,
141 text: &str,
142 size: f32,
143 weight: FontWeight,
144 max_width: f32,
145 mode: ElideMode,
146 spacing: LetterSpacing,
147 family: Option<&str>,
148 style: FontStyle,
149 ) -> Size {
150 let shown = self
151 .text
152 .elide_styled_spaced(text, size, weight, max_width, mode, spacing, family, style);
153 let w = self
154 .text
155 .text_width_styled_spaced(&shown, size, weight, spacing, family, style)
156 .min(max_width.max(0.0));
157 Size::new(w, Self::text_line_height(size))
158 }
159
160 pub fn text_line_height(size: f32) -> f32 {
161 size * 1.35
162 }
163
164 pub fn rich_size(
165 &mut self,
166 spans: &[TextSpan],
167 size: f32,
168 width: f32,
169 mode: WrapMode,
170 spacing: LetterSpacing,
171 align: RichAlign,
172 ) -> Size {
173 self.text
174 .shape_rich_full(spans, size, width, mode, spacing, align)
175 .size()
176 }
177}