strata-layout 0.1.2

Chromium-parity Box Model, Flexbox (L1), and CSS Grid (L2) layout engine in pure Rust
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
//! Intrinsic text dimension measurement, OpenType shaping, and line breaking facade.

use crate::typography::{
    InlineFormattingContext, InlineLayoutOptions, InlineLayoutResult, VerticalAlignment,
    WhiteSpaceMode,
};

/// Global text measurer callback type for exact platform font metrics measurement.
pub type TextMeasurementCallback = fn(&str, f32, f32, Option<f32>, bool) -> (f32, f32);

/// Global styled text measurer callback for exact font metrics measurement with weight and style.
pub type StyledTextMeasurementCallback = fn(
    &str,
    f32,
    f32,
    Option<f32>,
    bool,
    strata_style::types::FontWeight,
    strata_style::types::FontStyle,
) -> (f32, f32);

/// Global text measurer callback that also receives the requested font family atom.
pub type FamilyTextMeasurementCallback =
    fn(&str, f32, f32, Option<f32>, bool, strata_primitives::AtomId) -> (f32, f32);

/// Global styled text measurer callback that also receives the requested font family atom.
pub type FamilyStyledTextMeasurementCallback = fn(
    &str,
    f32,
    f32,
    Option<f32>,
    bool,
    strata_primitives::AtomId,
    strata_style::types::FontWeight,
    strata_style::types::FontStyle,
) -> (f32, f32);

/// Global font metrics callback returning `(ascent_px, descent_px, line_gap_px)`.
pub type FontMetricsCallback = fn(strata_primitives::AtomId, f32, bool) -> (f32, f32, f32);

static TEXT_MEASURER: std::sync::RwLock<Option<TextMeasurementCallback>> =
    std::sync::RwLock::new(None);
static STYLED_TEXT_MEASURER: std::sync::RwLock<Option<StyledTextMeasurementCallback>> =
    std::sync::RwLock::new(None);
static FAMILY_TEXT_MEASURER: std::sync::RwLock<Option<FamilyTextMeasurementCallback>> =
    std::sync::RwLock::new(None);
static FAMILY_STYLED_TEXT_MEASURER: std::sync::RwLock<Option<FamilyStyledTextMeasurementCallback>> =
    std::sync::RwLock::new(None);
static FONT_METRICS: std::sync::RwLock<Option<FontMetricsCallback>> = std::sync::RwLock::new(None);
static FONT_GENERATION: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(1);

/// Returns the current font configuration generation. Caches key on this value
/// so registering new platform measurers invalidates every measurement.
pub fn font_generation() -> u64 {
    FONT_GENERATION.load(std::sync::atomic::Ordering::Relaxed)
}

fn bump_font_generation() {
    FONT_GENERATION.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}

/// Registers a global platform text measurement callback (e.g. PangoCairo).
pub fn register_text_measurer(measurer: TextMeasurementCallback) {
    if let Ok(mut lock) = TEXT_MEASURER.write() {
        *lock = Some(measurer);
    }
    bump_font_generation();
}

/// Registers a global platform styled text measurement callback (e.g. PangoCairo).
pub fn register_styled_text_measurer(measurer: StyledTextMeasurementCallback) {
    if let Ok(mut lock) = STYLED_TEXT_MEASURER.write() {
        *lock = Some(measurer);
    }
    bump_font_generation();
}

/// Registers a global platform text measurement callback that is family aware.
pub fn register_family_text_measurer(measurer: FamilyTextMeasurementCallback) {
    if let Ok(mut lock) = FAMILY_TEXT_MEASURER.write() {
        *lock = Some(measurer);
    }
    bump_font_generation();
}

/// Registers a global platform styled text measurement callback that is family aware.
pub fn register_family_styled_text_measurer(measurer: FamilyStyledTextMeasurementCallback) {
    if let Ok(mut lock) = FAMILY_STYLED_TEXT_MEASURER.write() {
        *lock = Some(measurer);
    }
    bump_font_generation();
}

/// Registers a global platform font metrics callback (ascent, descent, line gap).
pub fn register_font_metrics(metrics: FontMetricsCallback) {
    if let Ok(mut lock) = FONT_METRICS.write() {
        *lock = Some(metrics);
    }
    bump_font_generation();
}

/// Retrieves the active registered text measurer callback, if any.
pub fn get_text_measurer() -> Option<TextMeasurementCallback> {
    TEXT_MEASURER.read().ok().and_then(|lock| *lock)
}

/// Retrieves the active registered styled text measurer callback, if any.
pub fn get_styled_text_measurer() -> Option<StyledTextMeasurementCallback> {
    STYLED_TEXT_MEASURER.read().ok().and_then(|lock| *lock)
}

/// Retrieves the active registered family-aware text measurer callback, if any.
pub fn get_family_text_measurer() -> Option<FamilyTextMeasurementCallback> {
    FAMILY_TEXT_MEASURER.read().ok().and_then(|lock| *lock)
}

/// Retrieves the active registered family-aware styled text measurer callback, if any.
pub fn get_family_styled_text_measurer() -> Option<FamilyStyledTextMeasurementCallback> {
    FAMILY_STYLED_TEXT_MEASURER
        .read()
        .ok()
        .and_then(|lock| *lock)
}

/// Retrieves the active registered font metrics callback, if any.
pub fn get_font_metrics() -> Option<FontMetricsCallback> {
    FONT_METRICS.read().ok().and_then(|lock| *lock)
}

pub fn is_monospace_family(font_family: strata_primitives::AtomId) -> bool {
    if font_family == strata_primitives::AtomId::FONT_MONO {
        return true;
    }
    if let Some(family_str) = strata_primitives::atom::resolve(font_family) {
        family_str.contains("mono")
            || family_str.contains("monospace")
            || family_str.contains("Courier")
            || family_str.contains("Consolas")
            || family_str.contains("DejaVu Sans Mono")
    } else {
        false
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
struct MeasureCacheKey {
    text_hash: u64,
    text_len: usize,
    font_size_bits: u32,
    line_height_bits: u32,
    max_width_bits: u32,
    is_monospace: bool,
}

#[derive(Clone, Copy)]
struct MeasureCacheEntry {
    key: MeasureCacheKey,
    bounds: (f32, f32),
}

const CACHE_SIZE: usize = 1024;

#[derive(Clone, Copy, PartialEq, Eq, Hash)]
struct StyledMeasureKey {
    text_hash: u64,
    text_len: usize,
    font_size_bits: u32,
    line_height_bits: u32,
    max_width_bits: u32,
    flags: u32,
    font_family: u32,
    font_weight: u8,
    font_style: u8,
    generation: u64,
}

const STYLED_CACHE_LIMIT: usize = 16_384;

thread_local! {
    static MEASURE_CACHE: std::cell::RefCell<[Option<MeasureCacheEntry>; CACHE_SIZE]> = const { std::cell::RefCell::new([None; CACHE_SIZE]) };
    static STYLED_MEASURE_CACHE: std::cell::RefCell<std::collections::HashMap<StyledMeasureKey, (f32, f32)>> =
        std::cell::RefCell::new(std::collections::HashMap::new());
    static TEXT_CACHE_HITS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
    static TEXT_CACHE_MISSES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

/// Returns cumulative `(hits, misses)` for thread-local text/glyph layout measurement caches.
pub fn text_measure_cache_stats() -> (u64, u64) {
    let hits = TEXT_CACHE_HITS.with(|c| c.get());
    let misses = TEXT_CACHE_MISSES.with(|c| c.get());
    (hits, misses)
}

fn hash_text(text: &str) -> u64 {
    use std::hash::Hasher;
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    hasher.write(text.as_bytes());
    hasher.finish()
}

#[allow(clippy::too_many_arguments)]
fn styled_measure_key(
    text_hash: u64,
    text_len: usize,
    font_size: f32,
    line_height_multiplier: f32,
    max_width: Option<f32>,
    is_monospace: bool,
    font_family: Option<strata_primitives::AtomId>,
    font_weight: strata_style::types::FontWeight,
    font_style: strata_style::types::FontStyle,
) -> StyledMeasureKey {
    let flags = (is_monospace as u32) | ((max_width.is_some() as u32) << 1);
    StyledMeasureKey {
        text_hash,
        text_len,
        font_size_bits: font_size.to_bits(),
        line_height_bits: line_height_multiplier.to_bits(),
        max_width_bits: max_width.map(|w| w.to_bits()).unwrap_or(u32::MAX),
        flags,
        font_family: font_family.map(|f| f.0).unwrap_or(u32::MAX),
        font_weight: font_weight as u8,
        font_style: font_style as u8,
        generation: font_generation(),
    }
}

fn styled_cache_get(key: &StyledMeasureKey) -> Option<(f32, f32)> {
    let hit = STYLED_MEASURE_CACHE.with(|cache| cache.borrow().get(key).copied());
    if hit.is_some() {
        TEXT_CACHE_HITS.with(|c| c.set(c.get().wrapping_add(1)));
    } else {
        TEXT_CACHE_MISSES.with(|c| c.set(c.get().wrapping_add(1)));
    }
    hit
}

fn styled_cache_put(key: StyledMeasureKey, bounds: (f32, f32)) {
    STYLED_MEASURE_CACHE.with(|cache| {
        let mut map = cache.borrow_mut();
        if map.len() >= STYLED_CACHE_LIMIT {
            map.clear();
        }
        map.insert(key, bounds);
    });
}

/// Measures intrinsic text bounds (width, height) using subpixel typography and line breaking.
pub fn measure_text_bounds(
    text: &str,
    font_size: f32,
    line_height_multiplier: f32,
    max_width: Option<f32>,
) -> (f32, f32) {
    measure_text_bounds_ext(text, font_size, line_height_multiplier, max_width, false)
}

/// Measures intrinsic text bounds honoring the CSS `white-space` mode.
///
/// `white-space: nowrap` and the pre* modes must not soft-wrap, so the
/// measurement pass has to receive the same line-breaking mode as the final
/// inline layout. Without this, a `truncate` element is measured as multiple
/// lines and then painted as a single one.
pub fn measure_text_bounds_with_white_space(
    text: &str,
    font_size: f32,
    line_height_multiplier: f32,
    max_width: Option<f32>,
    is_monospace: bool,
    white_space: strata_style::types::WhiteSpace,
) -> (f32, f32) {
    if text.is_empty() {
        return (0.0, font_size * line_height_multiplier);
    }
    let mode = match white_space {
        strata_style::types::WhiteSpace::Normal => WhiteSpaceMode::Normal,
        strata_style::types::WhiteSpace::NoWrap => WhiteSpaceMode::NoWrap,
        strata_style::types::WhiteSpace::Pre => WhiteSpaceMode::Pre,
        strata_style::types::WhiteSpace::PreWrap => WhiteSpaceMode::PreWrap,
        strata_style::types::WhiteSpace::PreLine => WhiteSpaceMode::PreLine,
    };
    let ifc = InlineFormattingContext::new();
    let options = InlineLayoutOptions {
        font_size,
        line_height: Some(font_size * line_height_multiplier),
        max_width,
        white_space: mode,
        vertical_align: VerticalAlignment::Baseline,
        is_monospace,
        exclusion_space: None,
        bfc_offset_y: 0.0,
    };
    let result = ifc.layout_text(text, &options);
    let line_h = font_size * line_height_multiplier;
    let total_height = result.line_count.max(1) as f32 * line_h;
    (result.total_width, total_height)
}

/// Measures intrinsic text bounds with explicit monospace font flag.
pub fn measure_text_bounds_ext(
    text: &str,
    font_size: f32,
    line_height_multiplier: f32,
    max_width: Option<f32>,
    is_monospace: bool,
) -> (f32, f32) {
    if text.is_empty() {
        return (0.0, font_size * line_height_multiplier);
    }

    if let Some(measurer) = get_text_measurer() {
        let key = styled_measure_key(
            hash_text(text),
            text.len(),
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
            None,
            strata_style::types::FontWeight::Normal,
            strata_style::types::FontStyle::Normal,
        );
        if let Some(hit) = styled_cache_get(&key) {
            return hit;
        }
        let bounds = measurer(
            text,
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
        );
        styled_cache_put(key, bounds);
        return bounds;
    }

    use std::hash::Hasher;
    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    hasher.write(text.as_bytes());
    let text_hash = hasher.finish();

    let font_size_bits = font_size.to_bits();
    let line_height_bits = line_height_multiplier.to_bits();
    let max_width_bits = max_width.map(|w| w.to_bits()).unwrap_or(u32::MAX);

    let key = MeasureCacheKey {
        text_hash,
        text_len: text.len(),
        font_size_bits,
        line_height_bits,
        max_width_bits,
        is_monospace,
    };

    let mono_bit = if is_monospace { 1u64 } else { 0u64 };
    let idx =
        ((text_hash ^ (font_size_bits as u64) ^ ((max_width_bits as u64) << 16) ^ (mono_bit << 24))
            as usize)
            % CACHE_SIZE;

    let cached = MEASURE_CACHE.with(|cache| {
        let cell = cache.borrow();
        if let Some(entry) = &cell[idx] {
            if entry.key == key {
                return Some(entry.bounds);
            }
        }
        None
    });

    if let Some(bounds) = cached {
        TEXT_CACHE_HITS.with(|c| c.set(c.get().wrapping_add(1)));
        return bounds;
    }
    TEXT_CACHE_MISSES.with(|c| c.set(c.get().wrapping_add(1)));

    let ifc = InlineFormattingContext::new();
    let explicit_lh = font_size * line_height_multiplier;
    let options = InlineLayoutOptions {
        font_size,
        line_height: Some(explicit_lh),
        max_width,
        white_space: WhiteSpaceMode::Normal,
        vertical_align: VerticalAlignment::Baseline,
        is_monospace,
        exclusion_space: None,
        bfc_offset_y: 0.0,
    };

    let result = ifc.layout_text(text, &options);
    let single_line_height = font_size * line_height_multiplier;
    let total_height = result.line_count.max(1) as f32 * single_line_height;
    let bounds = (result.total_width, total_height);

    MEASURE_CACHE.with(|cache| {
        let mut cell = cache.borrow_mut();
        cell[idx] = Some(MeasureCacheEntry { key, bounds });
    });

    bounds
}

/// Measures intrinsic text bounds while requesting a specific font family.
///
/// Prefers the registered family-aware measurer so caret and selection metrics
/// match the physical face used for paint. Falls back to the legacy measurement
/// path when no family-aware measurer is registered.
pub fn measure_text_bounds_with_family(
    text: &str,
    font_size: f32,
    line_height_multiplier: f32,
    max_width: Option<f32>,
    is_monospace: bool,
    font_family: strata_primitives::AtomId,
) -> (f32, f32) {
    if text.is_empty() {
        return (0.0, font_size * line_height_multiplier);
    }

    if let Some(measurer) = get_family_text_measurer() {
        let key = styled_measure_key(
            hash_text(text),
            text.len(),
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
            Some(font_family),
            strata_style::types::FontWeight::Normal,
            strata_style::types::FontStyle::Normal,
        );
        if let Some(hit) = styled_cache_get(&key) {
            return hit;
        }
        let bounds = measurer(
            text,
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
            font_family,
        );
        styled_cache_put(key, bounds);
        return bounds;
    }

    measure_text_bounds_ext(
        text,
        font_size,
        line_height_multiplier,
        max_width,
        is_monospace,
    )
}

/// Measures intrinsic text bounds with family, weight, and style information.
///
/// Prefers the registered family-aware styled measurer (e.g. PangoCairo) so
/// intrinsic sizing does not have to run a full inline shaping pass.
#[allow(clippy::too_many_arguments)]
pub fn measure_text_bounds_styled_with_family(
    text: &str,
    font_size: f32,
    line_height_multiplier: f32,
    max_width: Option<f32>,
    is_monospace: bool,
    font_family: strata_primitives::AtomId,
    font_weight: strata_style::types::FontWeight,
    font_style: strata_style::types::FontStyle,
) -> (f32, f32) {
    if text.is_empty() {
        return (0.0, font_size * line_height_multiplier);
    }

    if let Some(measurer) = get_family_styled_text_measurer() {
        let key = styled_measure_key(
            hash_text(text),
            text.len(),
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
            Some(font_family),
            font_weight,
            font_style,
        );
        if let Some(hit) = styled_cache_get(&key) {
            return hit;
        }
        let bounds = measurer(
            text,
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
            font_family,
            font_weight,
            font_style,
        );
        styled_cache_put(key, bounds);
        return bounds;
    }

    if let Some(font) =
        crate::typography::opentype::lookup_font_for_style(font_family, font_weight, font_style)
    {
        let key = styled_measure_key(
            hash_text(text),
            text.len(),
            font_size,
            line_height_multiplier,
            max_width,
            is_monospace,
            Some(font_family),
            font_weight,
            font_style,
        );
        if let Some(hit) = styled_cache_get(&key) {
            return hit;
        }

        let mut total_w = 0.0f32;
        let mut prev_gid: Option<u16> = None;
        for c in text.chars() {
            let gid = font.glyph_id(c);
            let adv = font.glyph_advance(gid, font_size);
            if let Some(pg) = prev_gid {
                let kern = font.pair_kerning(pg, gid, font_size);
                total_w += kern;
            }
            total_w += adv;
            prev_gid = Some(gid);
        }

        let total_h = font_size * line_height_multiplier;
        let bounds = (total_w, total_h);
        styled_cache_put(key, bounds);
        return bounds;
    }

    measure_text_bounds_with_family(
        text,
        font_size,
        line_height_multiplier,
        max_width,
        is_monospace,
        font_family,
    )
}

/// Advanced text layout returning detailed line boxes, glyph runs, baselines, and geometry.
pub fn layout_inline_text(text: &str, options: &InlineLayoutOptions) -> InlineLayoutResult {
    let ifc = InlineFormattingContext::new();
    ifc.layout_text(text, options)
}