use nativeview_abi::{
NodeId, NvTextParagraphView, NvTextRun, TextRunFlags, NV_LINE_HEIGHT_AUTO,
NV_LINE_HEIGHT_PERCENT,
};
use nativeview_store::{Length, Rgba};
use nativeview_tables::tables::{
FontStyle, OverflowWrap, TextAlign, TextDecoration, VerticalAlign, WhiteSpace, WordBreak,
};
use crate::measure::{HeuristicTextMeasurer, TextMeasurer, TextStyle};
#[inline]
pub fn paragraph_text_str(view: &NvTextParagraphView) -> &str {
if view.text.ptr.is_null() || view.text.len == 0 {
return "";
}
let bytes = unsafe { core::slice::from_raw_parts(view.text.ptr, view.text.len) };
core::str::from_utf8(bytes).unwrap_or("")
}
#[inline]
pub fn paragraph_runs(view: &NvTextParagraphView) -> &[NvTextRun] {
if view.runs.is_null() || view.run_count == 0 {
return &[];
}
unsafe { core::slice::from_raw_parts(view.runs, view.run_count as usize) }
}
#[inline]
pub fn run_font_family<'a>(view: &'a NvTextParagraphView, run: &NvTextRun) -> &'a str {
if view.font_families_slab.ptr.is_null() || run.font_family_len.raw() == 0 {
return "";
}
let start = run.font_family_off.as_usize();
let Some(end) = start.checked_add(run.font_family_len.as_usize()) else {
return "";
};
if end > view.font_families_slab.len {
return "";
}
let bytes = unsafe {
core::slice::from_raw_parts(
view.font_families_slab.ptr.add(start),
run.font_family_len.as_usize(),
)
};
core::str::from_utf8(bytes).unwrap_or("")
}
#[inline]
pub fn run_text_slice<'a>(full_text: &'a str, run: &NvTextRun) -> &'a str {
let start = run.text_off.as_usize();
let Some(end) = start.checked_add(run.text_len.as_usize()) else {
return "";
};
full_text.get(start..end).unwrap_or("")
}
#[inline]
pub fn run_line_height_points(run: &NvTextRun) -> Option<f32> {
if run.line_height_unit == NV_LINE_HEIGHT_AUTO || !run.line_height.is_finite() {
return None;
}
let fs = sanitize_run_font_size(run.font_size);
let lh = if run.line_height_unit == NV_LINE_HEIGHT_PERCENT {
run.line_height * fs / 100.0
} else {
run.line_height
};
(lh.is_finite() && lh > 0.0).then_some(lh)
}
#[inline]
pub fn sanitize_run_font_size(font_size: f32) -> f32 {
if font_size.is_finite() && font_size > 0.0 {
font_size
} else {
HeuristicTextMeasurer::DEFAULT_FONT_SIZE
}
}
#[inline]
pub fn paragraph_wraps(white_space: u8) -> bool {
!matches!(
WhiteSpace::from_word(u32::from(white_space)),
WhiteSpace::NoWrap | WhiteSpace::Pre
)
}
#[inline]
pub fn single_run_text_style<'a>(
view: &'a NvTextParagraphView,
run: &NvTextRun,
) -> TextStyle<&'a str> {
let line_height = match run.line_height_unit {
NV_LINE_HEIGHT_AUTO => Length::AUTO,
NV_LINE_HEIGHT_PERCENT => Length::percent(run.line_height),
_ => Length::points(run.line_height),
};
TextStyle {
color: Rgba(run.color),
font_family: run_font_family(view, run),
font_size: run.font_size,
font_weight: run.font_weight,
font_style: FontStyle::from_word(u32::from(run.font_style)),
letter_spacing: run.letter_spacing,
line_height,
white_space: WhiteSpace::from_word(u32::from(view.white_space)),
word_break: WordBreak::from_word(u32::from(view.word_break)),
overflow_wrap: OverflowWrap::from_word(u32::from(view.overflow_wrap)),
text_align: TextAlign::from_word(u32::from(view.text_align)),
text_decoration: TextDecoration::from_bits_truncate(run.text_decoration),
vertical_align: VerticalAlign::from_word(u32::from(run.vertical_align)),
max_lines: view.max_lines,
}
}
pub fn default_measure_paragraph<M: TextMeasurer + ?Sized>(
measurer: &M,
view: &NvTextParagraphView,
max_width: Option<f32>,
) -> (f32, f32) {
let text = paragraph_text_str(view);
let runs = paragraph_runs(view);
if text.is_empty() || runs.is_empty() {
return (0.0, 0.0);
}
if runs.len() == 1 {
let style = single_run_text_style(view, &runs[0]);
return measurer.measure(text, &style, max_width);
}
heuristic_measure_paragraph(view, max_width)
}
#[inline]
fn run_metrics(run: &NvTextRun) -> (f32, f32) {
let fs = sanitize_run_font_size(run.font_size);
if fs == 0.0 {
return (0.0, 0.0);
}
let ls = if run.letter_spacing.is_finite() {
run.letter_spacing
} else {
0.0
};
let char_w = (fs * HeuristicTextMeasurer::CHAR_WIDTH_FACTOR + ls).max(0.0);
let line_h =
run_line_height_points(run).unwrap_or(fs * HeuristicTextMeasurer::LINE_HEIGHT_FACTOR);
(char_w, line_h)
}
pub fn heuristic_measure_paragraph(
view: &NvTextParagraphView,
max_width: Option<f32>,
) -> (f32, f32) {
let text = paragraph_text_str(view);
let runs = paragraph_runs(view);
if text.is_empty() || runs.is_empty() {
return (0.0, 0.0);
}
if runs.len() == 1 {
let style = single_run_text_style(view, &runs[0]);
return HeuristicTextMeasurer.measure(text, &style, max_width);
}
let para_wraps = paragraph_wraps(view.white_space);
let max_lines = view.max_lines;
let effective_mw = if para_wraps { max_width } else { None };
if let Some(mw) = effective_mw
&& (!mw.is_finite() || mw <= 0.0)
{
let mut total_h = 0.0f32;
let mut cur_h = 0.0f32;
let mut lines = 0u32;
for run in runs {
let (_, lh) = run_metrics(run);
let slice = run_text_slice(text, run);
for ch in slice.chars() {
if ch == '\n' {
if max_lines == 0 || lines < max_lines {
total_h += cur_h.max(lh);
lines += 1;
}
cur_h = 0.0;
} else {
cur_h = cur_h.max(lh);
}
}
}
if max_lines == 0 || lines < max_lines {
let fallback_h = runs.first().map_or(0.0, |r| run_metrics(r).1);
total_h += if cur_h > 0.0 { cur_h } else { fallback_h };
}
return (0.0, total_h);
}
let wrap_mw = effective_mw.map(|mw| (mw, (mw * 1e-5).max(1e-5)));
let mut max_line_w = 0.0f32;
let mut total_h = 0.0f32;
let mut cur_w = 0.0f32;
let mut cur_h = 0.0f32;
let mut lines_done = 0u32;
let mut any_visible = false;
for run in runs {
let (char_w, lh) = run_metrics(run);
if lh > 0.0 {
any_visible = true;
}
let run_wraps = para_wraps && !run.run_flags.contains(TextRunFlags::NOWRAP);
let slice = run_text_slice(text, run);
for ch in slice.chars() {
if ch == '\n' {
let line_h = cur_h.max(lh);
if max_lines == 0 || lines_done < max_lines {
max_line_w = max_line_w.max(cur_w);
total_h += line_h;
lines_done += 1;
}
cur_w = 0.0;
cur_h = 0.0;
continue;
}
if run_wraps
&& let Some((mw, eps)) = wrap_mw
&& cur_w > 0.0
&& cur_w + char_w > mw + eps
{
if max_lines == 0 || lines_done < max_lines {
max_line_w = max_line_w.max(cur_w.min(mw));
total_h += cur_h.max(lh);
lines_done += 1;
}
cur_w = 0.0;
cur_h = 0.0;
}
cur_w += char_w;
cur_h = cur_h.max(lh);
}
}
if !any_visible {
return (0.0, 0.0);
}
if max_lines == 0 || lines_done < max_lines {
let clamped_cur = wrap_mw.map_or(cur_w, |(mw, _)| cur_w.min(mw));
max_line_w = max_line_w.max(clamped_cur);
let fallback_h = runs.first().map_or(0.0, |r| run_metrics(r).1);
total_h += if cur_h > 0.0 { cur_h } else { fallback_h };
}
let out_w = match wrap_mw {
Some((mw, _)) => max_line_w.max(1.0).min(mw),
None => max_line_w.max(1.0),
};
(out_w, total_h)
}
pub fn heuristic_hit_test_paragraph(
view: &NvTextParagraphView,
box_width: f32,
local_x: f32,
local_y: f32,
) -> Option<NodeId> {
if !local_x.is_finite()
|| !local_y.is_finite()
|| local_x < 0.0
|| local_y < 0.0
|| !box_width.is_finite()
{
return None;
}
let text = paragraph_text_str(view);
let runs = paragraph_runs(view);
if text.is_empty() || runs.is_empty() {
return None;
}
let para_wraps = paragraph_wraps(view.white_space);
let wrap_mw = if para_wraps && box_width > 0.0 {
Some((box_width, (box_width * 1e-5).max(1e-5)))
} else {
None
};
let align = TextAlign::from_word(u32::from(view.text_align));
let max_lines = view.max_lines;
let align_offset = |line_w: f32| -> f32 {
if box_width <= 0.0 || line_w >= box_width {
return 0.0;
}
match align {
TextAlign::Center => (box_width - line_w) * 0.5,
TextAlign::Right | TextAlign::End => box_width - line_w,
TextAlign::Start | TextAlign::Left | TextAlign::Justify => 0.0,
}
};
const LINE_SEG_CAP: usize = 64;
let mut segs: [(f32, f32, NodeId); LINE_SEG_CAP] = [(0.0, 0.0, NodeId::NULL); LINE_SEG_CAP];
let mut seg_len = 0usize;
let mut cur_x = 0.0f32;
let mut cur_h = 0.0f32;
let mut cur_y = 0.0f32;
let mut lines_done = 0u32;
let check_line = |segs: &[(f32, f32, NodeId)],
line_w: f32,
y_top: f32,
line_h: f32|
-> Option<NodeId> {
if local_y < y_top || local_y >= y_top + line_h {
return None;
}
let ox = align_offset(line_w);
for &(x0, x1, nid) in segs {
if local_x >= ox + x0 && local_x < ox + x1 && !nid.is_null() {
return Some(nid);
}
}
None
};
let push_seg = |segs: &mut [(f32, f32, NodeId); LINE_SEG_CAP],
seg_len: &mut usize,
x0: f32,
x1: f32,
nid: NodeId| {
if *seg_len > 0 && segs[*seg_len - 1].2 == nid && (segs[*seg_len - 1].1 - x0).abs() < 1e-4
{
segs[*seg_len - 1].1 = x1;
} else if *seg_len < LINE_SEG_CAP {
segs[*seg_len] = (x0, x1, nid);
*seg_len += 1;
}
};
for run in runs {
let (char_w, lh) = run_metrics(run);
let run_hidden = run.run_flags.contains(TextRunFlags::HIDDEN);
let run_wraps = para_wraps && !run.run_flags.contains(TextRunFlags::NOWRAP);
let slice = run_text_slice(text, run);
for ch in slice.chars() {
if ch == '\n' {
let line_h = cur_h.max(lh);
if max_lines == 0 || lines_done < max_lines {
if let Some(hit) = check_line(&segs[..seg_len], cur_x, cur_y, line_h) {
return Some(hit);
}
cur_y += line_h;
lines_done += 1;
}
seg_len = 0;
cur_x = 0.0;
cur_h = 0.0;
continue;
}
if run_wraps
&& let Some((mw, eps)) = wrap_mw
&& cur_x > 0.0
&& cur_x + char_w > mw + eps
{
let line_h = cur_h.max(lh);
if max_lines == 0 || lines_done < max_lines {
if let Some(hit) = check_line(&segs[..seg_len], cur_x, cur_y, line_h) {
return Some(hit);
}
cur_y += line_h;
lines_done += 1;
}
seg_len = 0;
cur_x = 0.0;
cur_h = 0.0;
}
if char_w > 0.0 {
if !run_hidden {
push_seg(&mut segs, &mut seg_len, cur_x, cur_x + char_w, run.node_id);
}
cur_x += char_w;
}
cur_h = cur_h.max(lh);
}
}
if (max_lines == 0 || lines_done < max_lines) && seg_len > 0 {
let fallback_h = runs.first().map_or(0.0, |r| run_metrics(r).1);
let line_h = if cur_h > 0.0 { cur_h } else { fallback_h };
if let Some(hit) = check_line(&segs[..seg_len], cur_x, cur_y, line_h) {
return Some(hit);
}
}
None
}