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
use std::{cell::OnceCell, rc::Rc, sync::Arc};
use cranpose_core::PackedU64;
use cranpose_text::TextLayout;
use cranpose_ui_graphics::{Size, TextLayoutHandle};
use super::{
AnnotatedString, RenderString, TextAlign,
measure::{TextFrame, TextMetrics},
style::TextStyle,
};
use crate::text_layout_result::TextLayoutResult;
/// A text laid out between two widths: its glyphs and lines, which every
/// query, measurement and renderer reads, and the size it measures.
#[derive(Clone, Debug)]
pub struct PreparedTextLayout {
/// The text that was laid out, with its spans, shared with the text it
/// came from.
pub text: Arc<RenderString>,
/// The style the text draws in, shared with every render node drawn
/// from this layout.
pub visual_style: Arc<TextStyle>,
/// The lines and glyphs.
pub layout: TextLayout,
// The widest the text could be, and the fewest lines its height covers,
// which its metrics follow from with its layout.
max_width: f32,
min_lines: u32,
pub(crate) visual_style_hash: OnceCell<PackedU64>,
// The layout as a draw scope carries it, made on first use.
layout_handle: OnceCell<TextLayoutHandle>,
}
impl PreparedTextLayout {
// The prepared layout of `text` in `style` laid out as `layout`, `max_width`
// the widest it could be.
pub(super) fn new(
text: &AnnotatedString,
style: Arc<TextStyle>,
layout: TextLayout,
(max_width, min_lines): (f32, usize),
) -> Self {
Self {
max_width,
min_lines: u32::try_from(min_lines).unwrap_or(u32::MAX),
text: Arc::clone(text.render_string()),
visual_style: style,
layout,
visual_style_hash: OnceCell::new(),
layout_handle: OnceCell::new(),
}
}
// Takes `text` in `style`, which its layout holds laid out already, in
// place of what it held.
pub(super) fn retext(
&mut self,
text: &AnnotatedString,
style: Arc<TextStyle>,
frame: TextFrame,
) {
let min_lines = frame.options.normalized().min_lines;
self.max_width = frame.max_width;
self.min_lines = u32::try_from(min_lines).unwrap_or(u32::MAX);
self.text = Arc::clone(text.render_string());
self.visual_style = style;
self.visual_style_hash = OnceCell::new();
self.layout_handle = OnceCell::new();
}
/// The paragraph's width, within the widths it was laid out between, and
/// the height of all its lines together.
pub fn size(&self) -> Size {
Size::new(
self.layout.width().min(self.max_width),
self.layout.height(),
)
}
// The max widths at which the text laid out from `min_width` gives this
// layout again: its lines, its width and its size the same.
pub(crate) fn max_widths_kept(&self, min_width: f32) -> cranpose_ui_layout::BoundRange {
let width = self.layout.width();
// A paragraph wider than its max measures as the max, which moves
// with it.
if self.max_width < width {
return cranpose_ui_layout::BoundRange::exactly(self.max_width);
}
let (low, high) = self.layout.max_widths_kept(min_width);
cranpose_ui_layout::BoundRange {
low: low.max(width),
high,
}
}
/// Size and line count.
pub fn metrics(&self) -> TextMetrics {
let layout = &self.layout;
let size = self.size();
TextMetrics {
width: size.width,
height: size.height,
line_height: layout.line_advance(),
line_count: layout
.line_count()
.max(usize::try_from(self.min_lines).unwrap_or(usize::MAX)),
}
}
/// The first and last baselines.
pub fn alignment_lines(&self) -> cranpose_ui_layout::AlignmentLines {
let layout = &self.layout;
cranpose_ui_layout::AlignmentLines::new(
Some(layout.line_baseline(0)),
Some(layout.line_baseline(layout.line_count() - 1)),
)
}
/// True when the text did not fit: it had more lines than the line limit,
/// or was wider than the widest it could be.
pub fn did_overflow(&self) -> bool {
self.layout.did_overflow_height() || self.layout.did_overflow_width()
}
// The layout as a draw scope carries it to a renderer, made once for
// every frame that draws the same text.
pub(crate) fn layout_handle(&self) -> TextLayoutHandle {
self.layout_handle
.get_or_init(|| TextLayoutHandle::from(Arc::new(self.layout.clone())))
.clone()
}
// This layout at `widths`, at which it breaks into the same lines: itself when its width there is its own, else a copy whose
// lines `align` places across that width. The copy keeps the text as a
// renderer draws it and the style's hash, and takes the allocation of
// `spare` when nothing else holds it.
pub(crate) fn at_widths(
self: &Rc<Self>,
widths: (f32, f32),
align: TextAlign,
spare: Option<Rc<Self>>,
) -> Rc<Self> {
let width = self.layout.width_between(widths.0, widths.1);
if width.to_bits() == self.layout.width().to_bits() {
return Rc::clone(self);
}
self.aligned_at(widths, align, spare)
}
// The copy `at_widths` makes. Out of line: the copy costs more than the
// call, and a layout held at its own width takes none.
#[inline(never)]
fn aligned_at(
self: &Rc<Self>,
widths: (f32, f32),
align: TextAlign,
spare: Option<Rc<Self>>,
) -> Rc<Self> {
let mut aligned = Self {
text: Arc::clone(&self.text),
visual_style: Arc::clone(&self.visual_style),
layout: self.layout.clone(),
max_width: self.max_width,
min_lines: self.min_lines,
visual_style_hash: self.visual_style_hash.clone(),
layout_handle: OnceCell::new(),
};
aligned.realign(widths, align);
if let Some(mut spare) = spare
&& let Some(slot) = Rc::get_mut(&mut spare)
{
*slot = aligned;
return spare;
}
Rc::new(aligned)
}
// Places the lines across `(min_width, max_width)`, at which the layout
// breaks into the same lines, as `align` says.
pub(crate) fn realign(&mut self, (min_width, max_width): (f32, f32), align: TextAlign) {
self.layout.align(min_width, max_width, align);
self.max_width = max_width;
self.layout_handle = OnceCell::new();
}
// `realign` when the layout breaks into the same lines between
// `(min_width, max_width)`; `false`, leaving it as it was, when not.
pub(crate) fn try_realign(
&mut self,
(min_width, max_width): (f32, f32),
align: TextAlign,
) -> bool {
let held = self.layout.try_align(min_width, max_width, align);
if held {
self.max_width = max_width;
self.layout_handle = OnceCell::new();
}
held
}
/// The display text as a renderer draws it: the text's own content, so
/// every frame and scene rebuild drawing this layout hands over the same
/// allocation.
pub fn render_text(&self) -> Arc<RenderString> {
Arc::clone(&self.text)
}
/// [`TextStyle::render_hash`] of `visual_style`, hashed once for the
/// layout, or handed over by the node that knows its style's hash.
pub fn visual_style_hash(&self) -> u64 {
if let Some(hash) = self.visual_style_hash.get() {
return hash.get();
}
let hash = self.visual_style.render_hash();
let _ = self.visual_style_hash.set(PackedU64::new(hash));
hash
}
/// The layout's queries, as Compose's `TextLayoutResult` gives them.
pub fn result(&self) -> TextLayoutResult {
TextLayoutResult::new(self.layout.clone())
}
}
impl PartialEq for PreparedTextLayout {
fn eq(&self, other: &Self) -> bool {
self.text == other.text
&& self.visual_style == other.visual_style
&& self.metrics() == other.metrics()
&& self.alignment_lines() == other.alignment_lines()
&& self.did_overflow() == other.did_overflow()
}
}