nativeview_layout/paragraph/
mod.rs1mod compile;
6pub mod measure;
7mod sync;
8
9use nativeview_abi::{
10 NodeId, NvParagraphSpan, NvStrSlice, NvTextParagraphView, NvTextRun,
11 NV_PARAGRAPH_FLAG_STORE_TEXT_SLAB, NV_PARAGRAPH_NONE,
12};
13use nativeview_store::{NodeStore, Rgba, Span, SpanPool};
14
15use crate::measure::ResolvedTextStyle;
16use crate::taffy_store::responsive::text::ResponsiveTextEnv;
17
18#[derive(Clone, Copy, Default)]
22pub struct InheritedText<'a> {
23 pub text: &'a [ResolvedTextStyle],
24 pub rtl: &'a [bool],
25 pub responsive: Option<ResponsiveTextEnv<'a>>,
26}
27
28#[derive(Debug, Clone, Copy)]
29pub(super) struct InlineDfsFrame {
30 pub(super) id: NodeId,
31 pub(super) style: ResolvedTextStyle,
32 pub(super) background: Rgba,
33 pub(super) hidden: bool,
34 pub(super) display_none: bool,
35 pub(super) is_interactive: bool,
36 pub(super) depth: u16,
37}
38
39#[derive(Debug, Clone, Copy)]
42pub(super) struct ParagraphMeta {
43 pub(super) run_span: Span<NvTextRun>,
44 pub(super) text_span: Span<u8>,
45 pub(super) layout_hash: u64,
46 pub(super) paint_hash: u64,
47 pub(super) base_style: ResolvedTextStyle,
48 pub(super) base_rtl: bool,
49}
50
51#[derive(Debug, Clone, Default)]
55pub struct ParagraphStore {
56 pub(super) node_to_para: Vec<u32>,
58 pub(super) owner_paragraph: Vec<NodeId>,
60 pub(super) was_inline: Vec<bool>,
62 pub(super) spans: Vec<NvParagraphSpan>,
64 pub(super) meta: Vec<ParagraphMeta>,
66 pub(super) runs: SpanPool<NvTextRun>,
68 pub(super) text: SpanPool<u8>,
70 pub(super) dirty_layout_paras: Vec<NodeId>,
72 pub(super) dirty_paint_paras: Vec<NodeId>,
73 pub(super) box_to_inline: Vec<NodeId>,
74 pub(super) inline_to_box: Vec<NodeId>,
75 pub(super) dirty_paras: Vec<NodeId>,
77 pub(super) dirty_stamp: Vec<u32>,
78 pub(super) dirty_epoch: u32,
79 pub(super) paint_dirty_stamp: Vec<u32>,
80 pub(super) paint_dirty_epoch: u32,
81 pub(super) layout_dirty_stamp: Vec<u32>,
82 pub(super) layout_dirty_epoch: u32,
83 pub(super) created_stamp: Vec<u32>,
84 pub(super) created_epoch: u32,
85 pub(super) scratch_text: Vec<u8>,
86 pub(super) scratch_runs: Vec<NvTextRun>,
87 pub(super) dfs_stack: Vec<InlineDfsFrame>,
88 pub(super) subtree_stack: Vec<NodeId>,
89}
90
91impl ParagraphStore {
92 pub fn new() -> Self {
93 Self::default()
94 }
95
96 pub fn reset(&mut self) {
97 self.node_to_para.clear();
98 self.owner_paragraph.clear();
99 self.was_inline.clear();
100 self.spans.clear();
101 self.meta.clear();
102 self.runs = SpanPool::default();
103 self.text = SpanPool::default();
104 self.dirty_layout_paras.clear();
105 self.dirty_paint_paras.clear();
106 self.box_to_inline.clear();
107 self.inline_to_box.clear();
108 self.dirty_paras.clear();
109 self.dirty_stamp.clear();
110 self.dirty_epoch = 0;
111 self.paint_dirty_stamp.clear();
112 self.paint_dirty_epoch = 0;
113 self.layout_dirty_stamp.clear();
114 self.layout_dirty_epoch = 0;
115 self.created_stamp.clear();
116 self.created_epoch = 0;
117 self.scratch_text.clear();
118 self.scratch_runs.clear();
119 self.dfs_stack.clear();
120 self.subtree_stack.clear();
121 }
122
123 pub fn ensure_capacity(&mut self, cap: usize) {
124 if self.node_to_para.len() < cap {
125 self.node_to_para.resize(cap, NV_PARAGRAPH_NONE);
126 self.owner_paragraph.resize(cap, NodeId::NULL);
127 self.was_inline.resize(cap, false);
128 self.dirty_stamp.resize(cap, 0);
129 self.paint_dirty_stamp.resize(cap, 0);
130 self.layout_dirty_stamp.resize(cap, 0);
131 self.created_stamp.resize(cap, 0);
132 }
133 }
134
135 #[inline(always)]
136 pub fn node_to_paragraph_slice(&self) -> &[u32] {
137 &self.node_to_para
138 }
139
140 #[inline(always)]
141 pub fn spans(&self) -> &[NvParagraphSpan] {
142 &self.spans
143 }
144
145 #[inline(always)]
146 pub fn runs_slab(&self) -> &[NvTextRun] {
147 self.runs.data()
148 }
149
150 #[inline(always)]
151 pub fn text_slab(&self) -> &[u8] {
152 self.text.data()
153 }
154
155 #[inline(always)]
157 pub fn paragraph_slot(&self, id: NodeId) -> Option<u32> {
158 let slot = *self.node_to_para.get(id.index())?;
159 if slot == NV_PARAGRAPH_NONE {
160 return None;
161 }
162 let span = self.spans.get(slot as usize)?;
163 (span.owner_node_id == id).then_some(slot)
164 }
165
166 #[inline(always)]
168 pub fn paragraph_of(&self, id: NodeId) -> Option<&NvParagraphSpan> {
169 let slot = self.paragraph_slot(id)?;
170 self.spans.get(slot as usize)
171 }
172
173 #[inline(always)]
176 pub fn owner_paragraph(&self, id: NodeId) -> Option<NodeId> {
177 let owner = *self.owner_paragraph.get(id.index())?;
178 (!owner.is_null()).then_some(owner)
179 }
180
181 #[inline]
182 pub fn layout_hash(&self, id: NodeId) -> Option<u64> {
183 let slot = self.paragraph_slot(id)? as usize;
184 self.meta.get(slot).map(|m| m.layout_hash)
185 }
186
187 #[inline]
188 pub fn paint_hash(&self, id: NodeId) -> Option<u64> {
189 let slot = self.paragraph_slot(id)? as usize;
190 self.meta.get(slot).map(|m| m.paint_hash)
191 }
192
193 #[inline]
194 pub fn paragraph_runs_slice(&self, id: NodeId) -> &[NvTextRun] {
195 let Some(slot) = self.paragraph_slot(id) else {
196 return &[];
197 };
198 self.runs.get(self.meta[slot as usize].run_span)
199 }
200
201 #[inline]
204 pub fn paragraph_text_str<'a>(&'a self, store: &'a NodeStore, id: NodeId) -> Option<&'a str> {
205 let span = self.paragraph_of(id)?;
206 let bytes = self.span_text_bytes(store, span);
207 core::str::from_utf8(bytes).ok()
208 }
209
210 #[inline]
211 fn span_text_bytes<'a>(&'a self, store: &'a NodeStore, span: &NvParagraphSpan) -> &'a [u8] {
212 let len = span.text_len.as_usize();
213 if len == 0 {
214 return &[];
215 }
216 let off = span.text_off.as_usize();
217 let Some(end) = off.checked_add(len) else {
218 return &[];
219 };
220 let slab = if (span.flags & NV_PARAGRAPH_FLAG_STORE_TEXT_SLAB) != 0 {
221 store.text_slab()
222 } else {
223 self.text.data()
224 };
225 slab.get(off..end).unwrap_or(&[])
226 }
227
228 #[inline]
230 pub fn paragraph_view<'a>(
231 &'a self,
232 store: &'a NodeStore,
233 id: NodeId,
234 ) -> Option<NvTextParagraphView> {
235 let slot = self.paragraph_slot(id)?;
236 self.paragraph_view_by_slot(store, slot)
237 }
238
239 #[inline]
241 pub fn paragraph_view_by_slot<'a>(
242 &'a self,
243 store: &'a NodeStore,
244 slot: u32,
245 ) -> Option<NvTextParagraphView> {
246 let span = self.spans.get(slot as usize)?;
247 let text_bytes = self.span_text_bytes(store, span);
248 let families = store.text_slab();
249 let runs = self.runs.get(self.meta[slot as usize].run_span);
250 Some(NvTextParagraphView {
251 max_lines: span.max_lines,
252 run_count: runs.len() as u32,
253 text_align: span.text_align,
254 white_space: span.white_space,
255 word_break: span.word_break,
256 overflow_wrap: span.overflow_wrap,
257 base_direction: span.base_direction,
258 flags: span.flags,
259 _reserved: 0,
260 text: NvStrSlice {
261 ptr: nativeview_abi::slab_ptr(text_bytes),
262 len: text_bytes.len(),
263 },
264 font_families_slab: NvStrSlice {
265 ptr: nativeview_abi::slab_ptr(families),
266 len: families.len(),
267 },
268 runs: nativeview_abi::slab_ptr(runs),
269 })
270 }
271
272 #[inline]
273 pub fn dirty_layout_paragraphs(&self) -> &[NodeId] {
274 &self.dirty_layout_paras
275 }
276
277 #[inline]
278 pub fn dirty_paint_paragraphs(&self) -> &[NodeId] {
279 &self.dirty_paint_paras
280 }
281
282 #[inline]
283 pub fn box_to_inline_transitions(&self) -> &[NodeId] {
284 &self.box_to_inline
285 }
286
287 #[inline]
288 pub fn inline_to_box_transitions(&self) -> &[NodeId] {
289 &self.inline_to_box
290 }
291
292 #[inline]
293 pub(super) fn bump_epoch(epoch: &mut u32, stamps: &mut [u32]) -> u32 {
294 *epoch = epoch.wrapping_add(1);
295 if *epoch == 0 {
296 stamps.fill(0);
297 *epoch = 1;
298 }
299 *epoch
300 }
301
302 #[inline]
303 pub(super) fn queue_dirty(&mut self, id: NodeId, push_list: bool) {
304 let idx = id.index();
305 if idx < self.dirty_stamp.len() && self.dirty_stamp[idx] != self.dirty_epoch {
306 self.dirty_stamp[idx] = self.dirty_epoch;
307 if push_list {
308 self.dirty_paras.push(id);
309 }
310 }
311 }
312
313 #[inline]
314 pub(super) fn record_layout_dirty(&mut self, id: NodeId) {
315 let idx = id.index();
316 if idx < self.layout_dirty_stamp.len()
317 && self.layout_dirty_stamp[idx] != self.layout_dirty_epoch
318 {
319 self.layout_dirty_stamp[idx] = self.layout_dirty_epoch;
320 self.dirty_layout_paras.push(id);
321 }
322 }
323
324 #[inline]
325 pub(super) fn record_paint_dirty(&mut self, id: NodeId) {
326 let idx = id.index();
327 if idx < self.paint_dirty_stamp.len()
328 && self.paint_dirty_stamp[idx] != self.paint_dirty_epoch
329 {
330 self.paint_dirty_stamp[idx] = self.paint_dirty_epoch;
331 self.dirty_paint_paras.push(id);
332 }
333 }
334
335 #[inline]
336 pub(super) fn is_created_this_frame(&self, id: NodeId) -> bool {
337 let idx = id.index();
338 idx < self.created_stamp.len() && self.created_stamp[idx] == self.created_epoch
339 }
340
341 pub(super) fn reset_frame_deltas(&mut self) {
342 self.dirty_layout_paras.clear();
343 self.dirty_paint_paras.clear();
344 self.box_to_inline.clear();
345 self.inline_to_box.clear();
346 self.dirty_paras.clear();
347 Self::bump_epoch(&mut self.dirty_epoch, &mut self.dirty_stamp);
348 Self::bump_epoch(&mut self.paint_dirty_epoch, &mut self.paint_dirty_stamp);
349 Self::bump_epoch(&mut self.layout_dirty_epoch, &mut self.layout_dirty_stamp);
350 }
351}