nativeview-layout 0.1.0-rc.1

Shared NodeStore layout layer: StoreLayoutEngine / TextMeasurer traits and the default direct Taffy engine
Documentation
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Pluggable text measurement (`TextMeasurer`), the inherited [`TextStyle`] every measurer
//! receives, and the deterministic cross-platform `HeuristicTextMeasurer` fallback used by every
//! layout engine.

use std::sync::Arc;

use nativeview_abi::{NvTextParagraphView, xxh64};
use nativeview_store::{Length, NodeId, NodeStore, Rgba, StrSpan};
use nativeview_tables::tables::{
    FontStyle, OverflowWrap, TextAlign, TextDecoration, Typography, VerticalAlign, WhiteSpace,
    WordBreak,
};

use crate::paragraph::measure::{
    default_measure_paragraph, heuristic_hit_test_paragraph, heuristic_measure_paragraph,
};

/// Fully resolved, inherited typography of one node: everything a host needs to measure text
/// exactly as it paints it. `F` is the family: a [`ResolvedFamily`] into the store while layout
/// keeps it per node ([`ResolvedTextStyle`]), a borrowed `&str` when handed to a [`TextMeasurer`].
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct TextStyle<F> {
    pub color: Rgba,
    /// CSS font-family list as authored; empty = the host's default UI font.
    pub font_family: F,
    /// Logical points, finite and `>= 0`.
    pub font_size: f32,
    /// CSS weight (`100..=900` once a host clamps it).
    pub font_weight: u16,
    pub font_style: FontStyle,
    /// Extra advance after every character, logical points.
    pub letter_spacing: f32,
    /// Points = absolute; percent = unit-less multiplier × 100 of `font_size`; auto = `normal`.
    pub line_height: Length,
    pub white_space: WhiteSpace,
    pub word_break: WordBreak,
    pub overflow_wrap: OverflowWrap,
    pub text_align: TextAlign,
    pub text_decoration: TextDecoration,
    pub vertical_align: VerticalAlign,
    /// Maximum painted lines (`0` = unlimited); measured height never exceeds them.
    pub max_lines: u32,
}

/// A node's resolved font family: where it lives in the store plus the `xxh64` of its text.
/// Equality is by content hash alone, so an in-place `StrArena` replace that keeps the span
/// (`"Arial"` → `"Inter"`) still compares unequal, while the same family at another span does not.
#[derive(Debug, Clone, Copy, Default)]
pub struct ResolvedFamily {
    pub span: StrSpan,
    pub hash: u64,
}

impl ResolvedFamily {
    /// The family `span` of `store`, hashed now (`0` for the empty default family).
    #[inline]
    pub fn new(store: &NodeStore, span: StrSpan) -> Self {
        let text = store.text(span);
        Self { span, hash: if text.is_empty() { 0 } else { xxh64(text.as_bytes(), 0) } }
    }
}

impl PartialEq for ResolvedFamily {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

/// Inherited typography kept per node during a layout pass.
pub type ResolvedTextStyle = TextStyle<ResolvedFamily>;

impl<F: Default> Default for TextStyle<F> {
    fn default() -> Self {
        Self {
            color: Rgba::BLACK,
            font_family: F::default(),
            font_size: HeuristicTextMeasurer::DEFAULT_FONT_SIZE,
            font_weight: 400,
            font_style: FontStyle::Normal,
            letter_spacing: 0.0,
            line_height: Length::AUTO,
            white_space: WhiteSpace::Normal,
            word_break: WordBreak::Normal,
            overflow_wrap: OverflowWrap::Normal,
            text_align: TextAlign::Start,
            text_decoration: TextDecoration::EMPTY,
            vertical_align: VerticalAlign::Baseline,
            max_lines: 0,
        }
    }
}

impl<F> TextStyle<F> {
    /// The same style with the family mapped to another representation.
    #[inline]
    pub fn map_family<'s, G>(&'s self, f: impl FnOnce(&'s F) -> G) -> TextStyle<G> {
        TextStyle {
            color: self.color,
            font_family: f(&self.font_family),
            font_size: self.font_size,
            font_weight: self.font_weight,
            font_style: self.font_style,
            letter_spacing: self.letter_spacing,
            line_height: self.line_height,
            white_space: self.white_space,
            word_break: self.word_break,
            overflow_wrap: self.overflow_wrap,
            text_align: self.text_align,
            text_decoration: self.text_decoration,
            vertical_align: self.vertical_align,
            max_lines: self.max_lines,
        }
    }

    /// `false` for `white-space: nowrap | pre`: lines break only at `\n`, never at `max_width`.
    #[inline]
    pub fn wraps(&self) -> bool {
        !matches!(self.white_space, WhiteSpace::NoWrap | WhiteSpace::Pre)
    }

    /// The line height in points, `None` for `normal` (the font's own metrics).
    #[inline]
    pub fn line_height_points(&self) -> Option<f32> {
        let lh = if self.line_height.is_percent() {
            self.line_height.value() * self.font_size / 100.0
        } else {
            self.line_height.resolve(self.font_size)?
        };
        (lh.is_finite() && lh > 0.0).then_some(lh)
    }
}

impl<F: PartialEq> TextStyle<F> {
    /// `true` when both styles measure identically: every field except paint-only `color`,
    /// floats compared by bits (so `NaN` never forces endless re-measurement).
    #[inline]
    pub fn measure_eq(&self, other: &Self) -> bool {
        self.font_family == other.font_family
            && self.font_size.to_bits() == other.font_size.to_bits()
            && self.font_weight == other.font_weight
            && self.font_style == other.font_style
            && self.letter_spacing.to_bits() == other.letter_spacing.to_bits()
            && self.line_height == other.line_height
            && self.white_space == other.white_space
            && self.word_break == other.word_break
            && self.overflow_wrap == other.overflow_wrap
            && self.vertical_align == other.vertical_align
            && self.max_lines == other.max_lines
    }

    /// `true` when all typography fields (including paint-only `color`, `text_align`,
    /// `text_decoration`) match bitwise.
    #[inline]
    pub fn style_eq(&self, other: &Self) -> bool {
        self.measure_eq(other)
            && self.color == other.color
            && self.text_align == other.text_align
            && self.text_decoration == other.text_decoration
    }
}

impl<F: AsRef<str>> TextStyle<F> {
    /// The measurer view of an owned-family style.
    #[inline]
    pub fn borrowed(&self) -> TextStyle<&str> {
        self.map_family(AsRef::as_ref)
    }
}

impl TextStyle<&str> {
    /// Default typography at `font_size` (the host's default family).
    pub fn sized(font_size: f32) -> Self {
        Self {
            font_size,
            ..Self::default()
        }
    }
}

impl ResolvedTextStyle {
    /// The measurer view of this style, its family read from `store`.
    #[inline]
    pub fn in_store<'a>(&self, store: &'a NodeStore) -> TextStyle<&'a str> {
        self.map_family(|family| store.text(family.span))
    }
}

/// Resolves one node's inherited [`ResolvedTextStyle`] from its parent's resolved typography and
/// any explicit fields on `id`'s [`Typography`] row. Shared by every layout engine so invalid
/// (`< 0` or non-finite) sizes and spacings fall back identically across Taffy and Yoga.
#[inline]
pub fn resolve_inherited_text(
    store: &NodeStore,
    id: NodeId,
    mut parent: ResolvedTextStyle,
) -> ResolvedTextStyle {
    let Some(t) = store.try_row::<Typography>(id) else {
        return parent;
    };
    if t.is_color_explicit() {
        parent.color = t.color();
    }
    let fs = t.font_size();
    if t.is_font_size_explicit() && fs.is_finite() && fs >= 0.0 {
        parent.font_size = fs;
    }
    if t.is_font_weight_explicit() {
        parent.font_weight = t.font_weight();
    }
    let lh = t.line_height();
    // `NaN` / `inf` / negative heights are invalid CSS: keep the inherited value.
    if t.is_line_height_explicit() && (lh.is_auto() || (lh.value().is_finite() && lh.value() >= 0.0)) {
        parent.line_height = lh;
    }
    let ls = t.letter_spacing();
    if t.is_letter_spacing_explicit() && ls.is_finite() {
        parent.letter_spacing = ls;
    }
    if t.is_font_family_explicit() {
        parent.font_family = ResolvedFamily::new(store, t.font_family_span());
    }
    if t.is_max_lines_explicit() {
        parent.max_lines = t.max_lines().map_or(0, |n| u32::from(n.get()));
    }
    if t.is_font_style_explicit() {
        parent.font_style = t.font_style();
    }
    if t.is_white_space_explicit() {
        parent.white_space = t.white_space();
    }
    if t.is_word_break_explicit() {
        parent.word_break = t.word_break();
    }
    if t.is_overflow_wrap_explicit() {
        parent.overflow_wrap = t.overflow_wrap();
    }
    if t.is_text_align_explicit() {
        parent.text_align = t.text_align();
    }
    if t.is_text_decoration_explicit() {
        parent.text_decoration = t.text_decoration();
    }
    if t.is_vertical_align_explicit() {
        parent.vertical_align = t.vertical_align();
    }
    parent
}

/// Pluggable text & multi-run paragraph measurement / hit-test provider used by layout engines.
pub trait TextMeasurer: Send + Sync {
    /// Measures single-run `text` with resolved typography under an optional `max_width`
    /// constraint (`Some(0.0)` is a min-content probe). Returns `(width, height)` in logical
    /// points.
    fn measure(&self, text: &str, style: &TextStyle<&str>, max_width: Option<f32>) -> (f32, f32);

    /// Measures a compiled multi-run paragraph (`NvTextParagraphView`) under optional `max_width`.
    fn measure_paragraph(
        &self,
        paragraph: &NvTextParagraphView,
        max_width: Option<f32>,
    ) -> (f32, f32) {
        default_measure_paragraph(self, paragraph, max_width)
    }

    /// Hit-tests content-box local `(local_x, local_y)` inside a paragraph laid out to
    /// `box_width`, returning the hit run's owning `NodeId` (`None` on trailing whitespace/miss).
    fn hit_test_paragraph(
        &self,
        paragraph: &NvTextParagraphView,
        box_width: f32,
        local_x: f32,
        local_y: f32,
    ) -> Option<NodeId> {
        heuristic_hit_test_paragraph(paragraph, box_width, local_x, local_y)
    }
}

/// Default deterministic text measurer matching NV's cross-platform heuristic
/// (`font_size * 0.58 + letter_spacing` per character, `font_size * 1.35` `normal` line height),
/// with explicit `\n` line breaks, `max_width` wrapping (none for `nowrap` / `pre`), explicit
/// line heights, and `max_lines`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct HeuristicTextMeasurer;

impl HeuristicTextMeasurer {
    pub const CHAR_WIDTH_FACTOR: f32 = 0.58;
    pub const LINE_HEIGHT_FACTOR: f32 = 1.35;
    pub const DEFAULT_FONT_SIZE: f32 = 16.0;
}

impl TextMeasurer for HeuristicTextMeasurer {
    fn measure(&self, text: &str, style: &TextStyle<&str>, max_width: Option<f32>) -> (f32, f32) {
        if text.is_empty() || style.font_size == 0.0 {
            return (0.0, 0.0);
        }
        let fs = if style.font_size.is_finite() && style.font_size > 0.0 {
            style.font_size
        } else {
            Self::DEFAULT_FONT_SIZE
        };
        let sized = TextStyle { font_size: fs, ..*style };
        let char_w = (fs * Self::CHAR_WIDTH_FACTOR + style.letter_spacing).max(0.0);
        let line_h = sized.line_height_points().unwrap_or(fs * Self::LINE_HEIGHT_FACTOR);
        let height = |lines: f32| {
            let lines = lines.max(1.0);
            let capped = if style.max_lines > 0 { lines.min(style.max_lines as f32) } else { lines };
            capped * line_h
        };
        let line_widths = || text.split('\n').map(|line| line.chars().count() as f32 * char_w);

        match max_width.filter(|_| style.wraps()) {
            Some(mw) if !mw.is_finite() || mw <= 0.0 => {
                // Min-content probe (e.g. Yoga `AtMost 0` or Taffy `MinContent`).
                (0.0, height(text.split('\n').count() as f32))
            }
            Some(mw) => {
                // Allow a few-ULP float32 tolerance (~1e-5 relative/absolute) so padding+border
                // add/sub round-trips (e.g. `(6.38_f32 + 18.0) - 18.0 = 6.3799992 < 6.3800001`)
                // do not spuriously wrap an exact-fit single line onto two lines.
                let wrap_eps = (mw * 1e-5).max(1e-5);
                let (mut max_line_w, mut total_lines) = (0.0f32, 0.0f32);
                for raw_w in line_widths() {
                    if raw_w > mw + wrap_eps {
                        max_line_w = max_line_w.max(mw);
                        total_lines += ((raw_w - wrap_eps) / mw).ceil().max(1.0);
                    } else {
                        max_line_w = max_line_w.max(raw_w);
                        total_lines += 1.0;
                    }
                }
                (max_line_w.max(1.0).min(mw), height(total_lines))
            }
            None => {
                let (max_line_w, lines) =
                    line_widths().fold((0.0f32, 0.0f32), |(w, n), lw| (w.max(lw), n + 1.0));
                (max_line_w.max(1.0), height(lines))
            }
        }
    }

    fn measure_paragraph(
        &self,
        paragraph: &NvTextParagraphView,
        max_width: Option<f32>,
    ) -> (f32, f32) {
        heuristic_measure_paragraph(paragraph, max_width)
    }

    fn hit_test_paragraph(
        &self,
        paragraph: &NvTextParagraphView,
        box_width: f32,
        local_x: f32,
        local_y: f32,
    ) -> Option<NodeId> {
        heuristic_hit_test_paragraph(paragraph, box_width, local_x, local_y)
    }
}

impl<F> TextMeasurer for F
where
    F: Fn(&str, &TextStyle<&str>, Option<f32>) -> (f32, f32) + Send + Sync,
{
    fn measure(&self, text: &str, style: &TextStyle<&str>, max_width: Option<f32>) -> (f32, f32) {
        (self)(text, style, max_width)
    }
}

impl<T: TextMeasurer + ?Sized> TextMeasurer for Arc<T> {
    fn measure(&self, text: &str, style: &TextStyle<&str>, max_width: Option<f32>) -> (f32, f32) {
        (**self).measure(text, style, max_width)
    }

    fn measure_paragraph(
        &self,
        paragraph: &NvTextParagraphView,
        max_width: Option<f32>,
    ) -> (f32, f32) {
        (**self).measure_paragraph(paragraph, max_width)
    }

    fn hit_test_paragraph(
        &self,
        paragraph: &NvTextParagraphView,
        box_width: f32,
        local_x: f32,
        local_y: f32,
    ) -> Option<NodeId> {
        (**self).hit_test_paragraph(paragraph, box_width, local_x, local_y)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn sized(fs: f32) -> TextStyle<&'static str> {
        TextStyle::sized(fs)
    }

    #[test]
    fn heuristic_text_measurer_matches_single_line_and_wraps() {
        let m = HeuristicTextMeasurer;

        // Empty text produces zero size.
        assert_eq!(m.measure("", &sized(16.0), None), (0.0, 0.0));

        // Single-line unconstrained matches font_size * 0.58 and font_size * 1.35.
        let (w, h) = m.measure("Hello World", &sized(20.0), None);
        assert!((w - 11.0 * 20.0 * 0.58).abs() < 1e-4);
        assert!((h - 20.0 * 1.35).abs() < 1e-4);

        // Wrapping under max_width increases line count proportionally.
        let (ww, wh) = m.measure("01234567890123456789", &sized(20.0), Some(100.0));
        assert!((ww - 100.0).abs() < 1e-4);
        // 20 * 11.6 = 232px -> ceil(232 / 100) = 3 lines
        assert!((wh - 3.0 * 20.0 * 1.35).abs() < 1e-4);

        // Explicit newline increases line count.
        let (nw, nh) = m.measure("Line 1\nLine 2", &sized(16.0), None);
        assert!((nw - 6.0 * 16.0 * 0.58).abs() < 1e-4);
        assert!((nh - 2.0 * 16.0 * 1.35).abs() < 1e-4);

        // Float32 padding+border round-trip (`(w + 18.0) - 18.0` losing 1 ULP) must not
        // spuriously wrap a single-line string onto two lines.
        let (single_w, single_h) = m.measure("8", &sized(11.0), None);
        let round_trip_mw = (single_w + 18.0_f32) - 18.0_f32;
        assert_eq!(m.measure("8", &sized(11.0), Some(round_trip_mw)).1, single_h);

        // Closure blanket impl & Arc<dyn TextMeasurer> work identically.
        let custom: Arc<dyn TextMeasurer> =
            Arc::new(|text: &str, s: &TextStyle<&str>, _mw: Option<f32>| {
                (text.len() as f32 * s.font_size, s.font_size * 2.0)
            });
        assert_eq!(custom.measure("abc", &sized(10.0), None), (30.0, 20.0));
    }

    #[test]
    fn heuristic_text_measurer_honours_the_resolved_typography() {
        let m = HeuristicTextMeasurer;
        let text = "0123456789";
        let base = m.measure(text, &sized(20.0), None);

        // Letter spacing widens every character.
        let spaced = TextStyle { letter_spacing: 2.0, ..sized(20.0) };
        assert!((m.measure(text, &spaced, None).0 - (base.0 + 10.0 * 2.0)).abs() < 1e-3);

        // `nowrap` / `pre` ignore the width constraint (also the min-content probe).
        for ws in [WhiteSpace::NoWrap, WhiteSpace::Pre] {
            let s = TextStyle { white_space: ws, ..sized(20.0) };
            assert_eq!(m.measure(text, &s, Some(50.0)), base, "{ws:?}");
            assert_eq!(m.measure(text, &s, Some(0.0)), base, "{ws:?}");
        }
        let pre_wrap = TextStyle { white_space: WhiteSpace::PreWrap, ..sized(20.0) };
        assert!(m.measure(text, &pre_wrap, Some(50.0)).1 > base.1);

        // Line heights: points are absolute, percent is a multiplier of the font size.
        let points = TextStyle { line_height: Length::points(30.0), ..sized(20.0) };
        assert_eq!(m.measure("a\nb", &points, None).1, 60.0);
        let percent = TextStyle { line_height: Length::percent(150.0), ..sized(20.0) };
        assert_eq!(m.measure("a\nb", &percent, None).1, 60.0);
        assert_eq!(percent.line_height_points(), Some(30.0));
        assert_eq!(sized(20.0).line_height_points(), None);

        // `max_lines` caps wrapped and explicit lines.
        let two = TextStyle { max_lines: 2, ..points };
        assert_eq!(m.measure(text, &two, Some(20.0)).1, 60.0);
        assert_eq!(m.measure("a\nb\nc\nd", &two, None).1, 60.0);
    }

    #[test]
    fn resolution_sanitizes_line_height_and_hashes_the_family() {
        let mut store = NodeStore::new();
        let mut tx = store.tx();
        let [parent, nan, negative, family] = [(); 4].map(|_| tx.create());
        tx.edit::<Typography>(parent).line_height(Length::points(18.0)).font_family("Inter");
        // Raw `NaN` points bits (tag `00`): no constructor produces them, a wire frame can.
        tx.edit::<Typography>(nan)
            .line_height(Length(f32::NAN.to_bits() & !0b11));
        tx.edit::<Typography>(negative).line_height(Length::points(-4.0));
        tx.edit::<Typography>(family).font_family("Arial");
        tx.commit_into(&mut nativeview_store::ChangeSet::default());
        let p = resolve_inherited_text(&store, parent, ResolvedTextStyle::default());
        for child in [nan, negative] {
            let r = resolve_inherited_text(&store, child, p);
            assert_eq!(r.line_height, Length::points(18.0), "invalid heights inherit");
            assert!(r.measure_eq(&r), "bitwise float equality is reflexive");
        }
        let inter = p.font_family;
        assert_eq!(inter.hash, xxh64(b"Inter", 0));
        let arial = resolve_inherited_text(&store, family, p);
        assert!(!arial.measure_eq(&p), "same-length families differ by content");
        assert!(arial.measure_eq(&TextStyle { color: Rgba::WHITE, ..arial }), "color is paint-only");
        assert_eq!(ResolvedTextStyle::default().font_family.hash, 0);
    }
}