use nativeview_abi::{
ArenaOffset, ByteLen, NodeId, NvParagraphSpan, NvTextRun, ParagraphRunCount,
ParagraphRunIndex, TextRunFlags, Xxh64Hasher, xxh64, NV_BASE_DIR_AUTO, NV_BASE_DIR_LTR,
NV_BASE_DIR_RTL, NV_LINE_HEIGHT_AUTO, NV_LINE_HEIGHT_PERCENT, NV_LINE_HEIGHT_POINTS,
NV_PARAGRAPH_FLAG_HAS_INTERACTIVE_RUNS, NV_PARAGRAPH_FLAG_MULTI_STYLE,
NV_PARAGRAPH_FLAG_STORE_TEXT_SLAB,
};
use nativeview_engine::InteractionTracker;
use nativeview_store::{NodeStore, Rgba, Span};
use nativeview_tables::interaction::{CursorKind, visit_state_rules_in_cascade};
use nativeview_tables::tables::{
Accessibility, AriaRole, Direction, Interaction, Layout, PointerCaptureMode, TextContent,
Typography, Visual, WhiteSpace, is_node_display_none,
};
use crate::measure::{ResolvedTextStyle, resolve_inherited_text};
use crate::taffy_store::responsive::{is_node_show_on_hidden, visit_node_responsive_patches};
use crate::taffy_store::responsive::text::{
ResponsiveTextEnv, apply_patch_to_resolved_text_style, responsive_inherited_text,
};
use crate::MAX_TREE_DEPTH;
use super::{InlineDfsFrame, ParagraphMeta, ParagraphStore};
impl ParagraphStore {
#[allow(clippy::too_many_arguments)]
pub(super) fn compile_paragraph(
&mut self,
store: &NodeStore,
pid: NodeId,
parent_text: ResolvedTextStyle,
parent_rtl: bool,
interaction: Option<&InteractionTracker>,
responsive: Option<ResponsiveTextEnv<'_>>,
) -> (bool, bool) {
let mut root_style = match responsive {
Some(env) => responsive_inherited_text(store, pid, parent_text, env),
None => resolve_inherited_text(store, pid, parent_text),
};
let run_cenv = responsive.map(|env| (env.container_env_of)(pid));
let mut root_bg = Rgba::TRANSPARENT;
let mut root_hidden = false;
apply_interaction_state_rules(
store,
pid,
interaction,
&mut root_style,
&mut root_bg,
&mut root_hidden,
false,
);
let base_direction = match store.try_row::<Layout>(pid).map(|l| l.direction()) {
Some(Direction::Ltr) => NV_BASE_DIR_LTR,
Some(Direction::Rtl) => NV_BASE_DIR_RTL,
_ if parent_rtl => NV_BASE_DIR_RTL,
_ => NV_BASE_DIR_AUTO,
};
let children = store.children(pid);
let single_run_fast = children.is_empty();
let (span_text_off, span_text_len, use_store_slab, para_flags) = if single_run_fast {
self.scratch_runs.clear();
let tc_span = store
.try_row::<TextContent>(pid)
.map_or(nativeview_store::StrSpan::EMPTY, |tc| tc.text_span());
let mut flags = NV_PARAGRAPH_FLAG_STORE_TEXT_SLAB;
if !tc_span.is_empty() {
let run = build_text_run(
0,
tc_span.len(),
pid,
&root_style,
Rgba::TRANSPARENT,
false,
false,
);
if run.text_decoration != 0 {
flags |= NV_PARAGRAPH_FLAG_MULTI_STYLE;
}
self.scratch_runs.push(run);
}
(tc_span.off(), tc_span.len(), true, flags)
} else {
self.scratch_text.clear();
self.scratch_runs.clear();
self.dfs_stack.clear();
if let Some(tc) = store.try_row::<TextContent>(pid) {
let own_text = tc.text();
if !own_text.is_empty() {
let off = self.scratch_text.len() as u32;
self.scratch_text.extend_from_slice(own_text.as_bytes());
self.scratch_runs.push(build_text_run(
off,
own_text.len() as u32,
pid,
&root_style,
Rgba::TRANSPARENT,
false,
false,
));
}
}
let root_hidden_inline = store
.try_row::<Visual>(pid)
.is_some_and(|v| v.visibility().resolve(false));
for &cid in children.iter().rev() {
self.dfs_stack.push(InlineDfsFrame {
id: cid,
style: root_style,
background: Rgba::TRANSPARENT,
hidden: root_hidden_inline,
display_none: false,
is_interactive: false,
depth: 1,
});
}
let mut any_interactive = false;
let mut any_bg_or_dec = false;
while let Some(mut frame) = self.dfs_stack.pop() {
let nid = frame.id;
if (frame.depth as usize) > MAX_TREE_DEPTH || !store.is_live(nid) {
continue;
}
let nidx = nid.index();
if nidx < self.owner_paragraph.len() {
self.owner_paragraph[nidx] = pid;
self.was_inline[nidx] = true;
}
let node_disp_none = frame.display_none
|| is_node_display_none(store, nid)
|| responsive.is_some_and(|env| is_node_show_on_hidden(store, nid, env.media));
if node_disp_none {
for &child in store.children(nid).iter().rev() {
self.dfs_stack.push(InlineDfsFrame {
id: child,
display_none: true,
depth: frame.depth.saturating_add(1),
..frame
});
}
continue;
}
frame.style = resolve_inherited_text(store, nid, frame.style);
if let Some(vis) = store.try_row::<Visual>(nid) {
let bg = vis.background();
if bg.a() > 0 {
frame.background = bg;
}
frame.hidden = vis.visibility().resolve(frame.hidden);
}
if is_inline_node_interactive(store, nid) {
frame.is_interactive = true;
}
if let (Some(env), Some(cenv)) = (responsive, run_cenv) {
visit_node_responsive_patches(store, nid, env.media, cenv, |_, pv| {
apply_patch_to_resolved_text_style(pv, &mut frame.style);
if let Some(bg) = pv.get_prop(Visual::BACKGROUND) {
frame.background = bg;
}
if let Some(vis) = pv.get_prop(Visual::VISIBILITY) {
frame.hidden = vis.resolve(frame.hidden);
}
});
}
apply_interaction_state_rules(
store,
nid,
interaction,
&mut frame.style,
&mut frame.background,
&mut frame.hidden,
true,
);
if let Some(tc) = store.try_row::<TextContent>(nid) {
let s = tc.text();
if !s.is_empty() {
let off = self.scratch_text.len() as u32;
self.scratch_text.extend_from_slice(s.as_bytes());
let run = build_text_run(
off,
s.len() as u32,
nid,
&frame.style,
frame.background,
frame.is_interactive,
frame.hidden,
);
if frame.is_interactive {
any_interactive = true;
}
if run.background != 0 || run.text_decoration != 0 {
any_bg_or_dec = true;
}
self.scratch_runs.push(run);
}
}
for &child in store.children(nid).iter().rev() {
self.dfs_stack.push(InlineDfsFrame {
id: child,
depth: frame.depth.saturating_add(1),
..frame
});
}
}
let mut flags = 0u8;
if any_interactive {
flags |= NV_PARAGRAPH_FLAG_HAS_INTERACTIVE_RUNS;
}
if self.scratch_runs.len() > 1 || any_bg_or_dec {
flags |= NV_PARAGRAPH_FLAG_MULTI_STYLE;
}
(0u32, self.scratch_text.len() as u32, false, flags)
};
let text_bytes: &[u8] = if use_store_slab {
let start = span_text_off as usize;
let end = start.saturating_add(span_text_len as usize);
store.text_slab().get(start..end).unwrap_or(&[])
} else {
&self.scratch_text
};
let mut new_span = NvParagraphSpan {
owner_node_id: pid,
text_off: ArenaOffset(span_text_off),
text_len: ByteLen(span_text_len),
run_off: ParagraphRunIndex::ZERO,
run_len: ParagraphRunCount(self.scratch_runs.len() as u32),
max_lines: root_style.max_lines,
text_align: root_style.text_align as u8,
white_space: root_style.white_space as u8,
word_break: root_style.word_break as u8,
overflow_wrap: root_style.overflow_wrap as u8,
base_direction,
flags: para_flags,
_reserved: 0,
};
let (new_layout_hash, new_paint_hash) = compute_paragraph_hashes(
&new_span,
text_bytes,
store.text_slab(),
&self.scratch_runs,
);
let pidx = pid.index();
let existing = self.paragraph_slot(pid);
if let Some(slot) = existing {
let su = slot as usize;
let old_meta = self.meta[su];
let layout_changed = old_meta.layout_hash != new_layout_hash;
let paint_changed = old_meta.paint_hash != new_paint_hash;
self.meta[su].base_style = parent_text;
self.meta[su].base_rtl = parent_rtl;
if !layout_changed && !paint_changed {
if use_store_slab {
self.spans[su].text_off = new_span.text_off;
self.spans[su].text_len = new_span.text_len;
}
let existing_runs = self.runs.get(old_meta.run_span);
let run_offsets_moved = existing_runs.len() != self.scratch_runs.len()
|| existing_runs.iter().zip(&self.scratch_runs).any(|(old_r, new_r)| {
old_r.font_family_off != new_r.font_family_off
|| old_r.font_family_len != new_r.font_family_len
});
if run_offsets_moved {
let refreshed = self.runs.replace(old_meta.run_span, &self.scratch_runs);
self.spans[su].run_off = ParagraphRunIndex(refreshed.off());
self.spans[su].run_len = ParagraphRunCount(refreshed.len());
self.meta[su].run_span = refreshed;
}
return (false, false);
}
let new_run_span = self.runs.replace(old_meta.run_span, &self.scratch_runs);
let new_text_span = if use_store_slab {
self.text.free(old_meta.text_span);
Span::EMPTY
} else {
self.text.replace(old_meta.text_span, &self.scratch_text)
};
if !use_store_slab {
new_span.text_off = ArenaOffset(new_text_span.off());
new_span.text_len = ByteLen(new_text_span.len());
}
new_span.run_off = ParagraphRunIndex(new_run_span.off());
new_span.run_len = ParagraphRunCount(new_run_span.len());
self.spans[su] = new_span;
self.meta[su] = ParagraphMeta {
run_span: new_run_span,
text_span: new_text_span,
layout_hash: new_layout_hash,
paint_hash: new_paint_hash,
base_style: parent_text,
base_rtl: parent_rtl,
};
(layout_changed, paint_changed)
} else {
let new_run_span = self.runs.replace(Span::EMPTY, &self.scratch_runs);
let new_text_span = if use_store_slab {
Span::EMPTY
} else {
self.text.replace(Span::EMPTY, &self.scratch_text)
};
if !use_store_slab {
new_span.text_off = ArenaOffset(new_text_span.off());
new_span.text_len = ByteLen(new_text_span.len());
}
new_span.run_off = ParagraphRunIndex(new_run_span.off());
new_span.run_len = ParagraphRunCount(new_run_span.len());
let slot = self.spans.len() as u32;
self.node_to_para[pidx] = slot;
self.spans.push(new_span);
self.meta.push(ParagraphMeta {
run_span: new_run_span,
text_span: new_text_span,
layout_hash: new_layout_hash,
paint_hash: new_paint_hash,
base_style: parent_text,
base_rtl: parent_rtl,
});
(true, true)
}
}
}
#[inline]
fn is_inline_node_interactive(store: &NodeStore, id: NodeId) -> bool {
store.try_row::<Interaction>(id).is_some_and(|i| {
!i.listeners().is_empty()
|| i.cursor() != CursorKind::Auto
|| i.disabled()
|| i.focusable()
|| i.capture_mode() != PointerCaptureMode::None
|| !i.state_rules().is_none()
|| i.tab_index().is_some()
}) || store
.try_row::<Accessibility>(id)
.is_some_and(|a| a.role() == Some(AriaRole::Link))
}
fn apply_interaction_state_rules(
store: &NodeStore,
id: NodeId,
interaction: Option<&InteractionTracker>,
style: &mut ResolvedTextStyle,
background: &mut Rgba,
hidden: &mut bool,
apply_inline_visual: bool,
) {
let Some(tracker) = interaction else {
return;
};
let state = tracker.state(id);
if state.is_empty() {
return;
}
let Some(rules) = crate::scene::state_rules_of(store, id) else {
return;
};
visit_state_rules_in_cascade(rules, state, |patch| {
if let Some(c) = patch.get_prop(Typography::COLOR) {
style.color = c;
}
if let Some(fs) = patch.get_prop(Typography::FONT_SIZE)
&& fs.is_finite()
&& fs > 0.0
{
style.font_size = fs;
}
if let Some(fw) = patch.get_prop(Typography::FONT_WEIGHT) {
style.font_weight = fw;
}
if let Some(st) = patch.get_prop(Typography::FONT_STYLE) {
style.font_style = st;
}
if let Some(td) = patch.get_prop(Typography::TEXT_DECORATION) {
style.text_decoration = td;
}
if let Some(va) = patch.get_prop(Typography::VERTICAL_ALIGN) {
style.vertical_align = va;
}
if let Some(ls) = patch.get_prop(Typography::LETTER_SPACING)
&& ls.is_finite()
{
style.letter_spacing = ls;
}
if let Some(lh) = patch.get_prop(Typography::LINE_HEIGHT)
&& (lh.is_auto() || (lh.value().is_finite() && lh.value() >= 0.0))
{
style.line_height = lh;
}
if apply_inline_visual {
if let Some(bg) = patch.get_prop(Visual::BACKGROUND) {
*background = bg;
}
if let Some(vis) = patch.get_prop(Visual::VISIBILITY) {
*hidden = vis.resolve(*hidden);
}
}
});
}
#[allow(clippy::too_many_arguments)]
#[inline]
fn build_text_run(
text_off: u32,
text_len: u32,
node_id: NodeId,
style: &ResolvedTextStyle,
background: Rgba,
is_interactive: bool,
hidden: bool,
) -> NvTextRun {
let font_size = if style.font_size.is_finite() && style.font_size > 0.0 {
style.font_size
} else {
16.0
};
let letter_spacing = if style.letter_spacing.is_finite() {
style.letter_spacing
} else {
0.0
};
let (line_height, line_height_unit) = if style.line_height.is_auto() {
(0.0, NV_LINE_HEIGHT_AUTO)
} else if style.line_height.is_percent() {
let v = style.line_height.value();
(if v.is_finite() { v } else { 0.0 }, NV_LINE_HEIGHT_PERCENT)
} else {
let v = style.line_height.value();
(if v.is_finite() { v } else { 0.0 }, NV_LINE_HEIGHT_POINTS)
};
let mut run_flags = TextRunFlags::EMPTY;
if is_interactive {
run_flags |= TextRunFlags::INTERACTIVE;
}
if matches!(style.white_space, WhiteSpace::NoWrap | WhiteSpace::Pre) {
run_flags |= TextRunFlags::NOWRAP;
}
if hidden {
run_flags |= TextRunFlags::HIDDEN;
}
NvTextRun {
text_off: ArenaOffset(text_off),
text_len: ByteLen(text_len),
node_id,
color: style.color.0,
background: background.0,
font_size,
letter_spacing,
line_height,
font_family_off: ArenaOffset(style.font_family.span.off()),
font_family_len: ByteLen(style.font_family.span.len()),
font_weight: style.font_weight.clamp(100, 900),
line_height_unit,
font_style: style.font_style as u8,
text_decoration: style.text_decoration.bits(),
vertical_align: style.vertical_align as u8,
run_flags,
_reserved: 0,
}
}
fn compute_paragraph_hashes(
span: &NvParagraphSpan,
text_bytes: &[u8],
families_slab: &[u8],
runs: &[NvTextRun],
) -> (u64, u64) {
let mut lh = Xxh64Hasher::new(0x5041_5241_4C41_5931);
lh.update(&span.max_lines.to_le_bytes());
lh.update(&[
span.white_space,
span.word_break,
span.overflow_wrap,
span.base_direction,
]);
lh.update_u64(text_bytes.len() as u64);
lh.update(text_bytes);
lh.update_u64(runs.len() as u64);
for r in runs {
let fam_hash = if r.font_family_len.raw() == 0 {
0u64
} else {
let start = r.font_family_off.as_usize();
let end = start.saturating_add(r.font_family_len.as_usize());
families_slab
.get(start..end)
.map_or(0, |b| xxh64(b, 0))
};
lh.update(&r.text_len.raw().to_le_bytes());
lh.update(&r.font_size.to_bits().to_le_bytes());
lh.update(&r.letter_spacing.to_bits().to_le_bytes());
lh.update(&r.line_height.to_bits().to_le_bytes());
lh.update_u64(fam_hash);
lh.update(&r.font_weight.to_le_bytes());
lh.update(&[
r.line_height_unit,
r.font_style,
r.vertical_align,
r.run_flags.raw() & TextRunFlags::NOWRAP.raw(),
]);
}
let layout_hash = lh.digest();
let mut ph = Xxh64Hasher::new(0x5041_5241_5041_494E);
ph.update_u64(layout_hash);
ph.update(&[span.text_align, span.flags]);
for r in runs {
ph.update(&r.node_id.raw().to_le_bytes());
ph.update(&r.color.to_le_bytes());
ph.update(&r.background.to_le_bytes());
ph.update(&[r.text_decoration, r.run_flags.raw()]);
}
(layout_hash, ph.digest())
}