crcbl-ui 0.20.0

Crucible immediate-mode GUI
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
//! `text-overflow: ellipsis`: a text span's lines cut to fit its box.
//!
//! # Where the property applies
//!
//! CSS gives `text-overflow` to a block container that clips its inline
//! content. Here the text lives in a span, a leaf box whose content is its
//! text and nothing else — the block container of that text — so the span's
//! own style decides: **a span is cut when it is `text-overflow: ellipsis`,
//! `white-space: nowrap` and its `overflow` clips** (`hidden` or `scroll`).
//! `text-overflow` is not inherited, as in CSS, so a block setting it cuts none
//! of the spans inside it.
//!
//! # How a line is cut
//!
//! * Each line — the text between newlines — is judged on its own, against the
//!   span's content-box width as layout measured it (the unrounded one, the
//!   width a wrapped span is broken at). A line that fits is left whole.
//! * A line that does not is cut at a **cluster** boundary
//!   ([`crate::font::cluster`]): the longest prefix that, with its trailing
//!   white space dropped and the ellipsis after it, fits. So a mark is never
//!   parted from its base, nor an emoji sequence or a flag cut in two. A
//!   ligature the shaper makes of two clusters can still be cut between them,
//!   and then draws as its two parts.
//! * The ellipsis is `…` (U+2026) in a font that has the glyph, and `...` in
//!   one that would draw `.notdef` for it — the bitmap font always.
//! * **A line too narrow for even the ellipsis shows nothing**, rather than
//!   drawing a partially clipped ellipsis. The span's own overflow clip applies
//!   to its text at the padding box independently of this line-cutting rule.
//!
//! The cut is taken after layout from the width the box was given, so it never
//! moves a box: the box is sized by the whole text, as CSS sizes it.

use std::sync::Arc;

use super::emit::content_width;
use super::style::{NodeStyle, TextOverflow, WhiteSpace};
use super::{Content, Shown, Ui};
use crate::font::layout::{TextLayout, WRAP_EPSILON};
use crate::font::{Font, GlyphId, cluster};
use crate::text::{FontAtlas, NOTDEF_INDEX, glyph_index};
use crate::widget::NATURAL_FONT_SIZE;

/// What a cut line ends with in a font that has the glyph.
pub(super) const ELLIPSIS: &str = "\u{2026}";

/// What a cut line ends with in a font that does not.
pub(super) const ELLIPSIS_FALLBACK: &str = "...";

impl Ui {
    /// Decides what every node shows: a text span under the ellipsis rule its
    /// text cut to the width it was just laid out at, everything else what it
    /// was built with. See the module docs.
    ///
    /// A `nowrap` span in a parsed font is handed the layout its measurement
    /// kept, when it shows its text whole, so neither this nor drawing it
    /// shapes that text again. **Its cut is kept beside that layout** with the
    /// width it was cut at, and handed back while both are unchanged, so a
    /// steady frame cuts none of its spans again. A wrapped span in a parsed
    /// font, which is never cut, is handed the layout the measure cache keeps
    /// at the width it was laid out at.
    pub(super) fn cut_text(&mut self, atlas: &FontAtlas) {
        self.cut.clear();
        for node in &mut self.nodes {
            node.shown = Shown::Whole;
            node.shown_layout = None;
            let Content::Text { start, end } = node.content else {
                continue;
            };
            let style = &node.style;
            let text = &self.text[start..end];
            // A clone, so the measure borrows it rather than the node it is
            // about to cut.
            let font = node.font.clone();
            let nowrap = font
                .as_ref()
                .filter(|_| style.white_space == WhiteSpace::NoWrap)
                .map(|font| (font, style.font_size, style.text_line_height(font)));
            if let Some(font) = &font
                && style.white_space == WhiteSpace::Normal
            {
                let width = content_width(&self.store.get(node.slot).unrounded);
                node.shown_layout = Some(self.measure.wrapped_layout(
                    node.content_hash,
                    font,
                    text,
                    style.font_size,
                    style.text_line_height(font),
                    width,
                ));
                continue;
            }
            let measured = nowrap.and_then(|(font, size, line_height)| {
                self.measure
                    .nowrap_layout(node.content_hash, font, text, size, line_height)
            });
            if !applies(style) {
                node.shown_layout = measured.cloned();
                continue;
            }
            let width = content_width(&self.store.get(node.slot).unrounded);
            let at = self.cut.len();
            if let Some((font, size, line_height)) = nowrap
                && let Some(kept) =
                    self.measure
                        .kept_cut(node.content_hash, font, text, size, line_height, width)
            {
                node.shown_layout.clone_from(&kept.layout);
                if let Some(shown) = &kept.shown {
                    self.cut.push_str(shown);
                    node.shown = Shown::Cut {
                        start: at,
                        end: self.cut.len(),
                    };
                }
                continue;
            }
            let measure = match &font {
                None => Measure::Bitmap {
                    atlas,
                    scale: style.font_size / NATURAL_FONT_SIZE,
                },
                Some(font) => Measure::Parsed {
                    font,
                    size: style.font_size,
                    line_height: style.text_line_height(font),
                    measured,
                },
            };
            let cut = cut(&measure, text, width, &mut self.cut);
            let shown = if cut.any {
                node.shown = Shown::Cut {
                    start: at,
                    end: self.cut.len(),
                };
                Some(Box::from(&self.cut[at..]))
            } else {
                self.cut.truncate(at);
                None
            };
            if measured.is_some() {
                self.measure.keep_cut(
                    node.content_hash,
                    KeptCut {
                        width: width.to_bits(),
                        shown,
                        layout: cut.layout.clone(),
                    },
                );
            }
            node.shown_layout = cut.layout;
        }
    }
}

/// A span's text as [`Ui::cut_text`] cut it at one width, kept beside the
/// layout of that text in the measure cache: what the cut showed, at what
/// width.
#[derive(Clone, Debug)]
pub(crate) struct KeptCut {
    /// The bits of the content-box width it was cut at.
    pub(super) width: u32,
    /// The text cut to fit, `None` when it was shown whole.
    shown: Option<Box<str>>,
    /// What [`Cut::layout`] answered.
    layout: Option<Arc<TextLayout>>,
}

/// Whether a text span in `style` is cut to fit; see the module docs.
fn applies(style: &NodeStyle) -> bool {
    style.text_overflow == TextOverflow::Ellipsis
        && style.white_space == WhiteSpace::NoWrap
        && style.overflow.clips()
}

/// The font a span is measured in, at its size.
pub(super) enum Measure<'a> {
    /// The built-in bitmap font, `scale` times its natural size.
    Bitmap { atlas: &'a FontAtlas, scale: f32 },
    /// A parsed font, laid out unbroken.
    Parsed {
        font: &'a Font,
        size: f32,
        line_height: f32,
        /// The span's whole text as layout measured it, when that kept it.
        measured: Option<&'a Arc<TextLayout>>,
    },
}

impl Measure<'_> {
    /// `line`'s width in pixels, measured as layout measures it, and in a
    /// parsed font the layout it was measured from: the one layout kept, when
    /// `line` is the span's whole text.
    pub(super) fn width(&self, line: &str) -> (f32, Option<Arc<TextLayout>>) {
        match self {
            Self::Bitmap { atlas, scale } => (atlas.text_width(line, *scale), None),
            Self::Parsed {
                font,
                size,
                line_height,
                measured,
            } => {
                let layout = measured
                    .filter(|layout| layout.lays_out(font, line, *size, *line_height))
                    .map_or_else(
                        || Arc::new(TextLayout::nowrap(font, line, *size, *line_height)),
                        Arc::clone,
                    );
                (layout.width(), Some(layout))
            }
        }
    }

    /// Each prefix of the line `layout` lays out that ends at one of `ends`,
    /// with `ellipsis` after it, settled against `fit` from `layout`'s
    /// shaping where it can be: see [`Estimate`]. `None` in the bitmap font,
    /// which measures without shaping, and for a line a hard break other than
    /// a newline breaks in two.
    fn estimate(
        &self,
        layout: &TextLayout,
        ends: &[usize],
        ellipsis: &str,
        fit: f32,
    ) -> Option<Estimate> {
        let Self::Parsed { font, size, .. } = self else {
            return None;
        };
        if layout.lines().len() != 1 {
            return None;
        }
        let scale = font.metrics().scale(*size);
        let ellipsis: f32 = ellipsis
            .chars()
            .map(|c| font.advance(font.glyph_id(c)) * scale)
            .sum();
        let slack = ELLIPSIS_KERN_EM * size;
        // Every glyph's and run of spaces' start, advance and whether a cut
        // there is safe, in logical order; a run of spaces always is.
        let mut starts: Vec<(usize, f32, bool)> = layout
            .shaped_advances()
            .map(|(byte, advance, unsafe_to_break)| (byte, advance, !unsafe_to_break))
            .chain(
                layout
                    .space_runs()
                    .iter()
                    .map(|&(byte, advance)| (byte, advance, true)),
            )
            .collect();
        starts.sort_by_key(|&(byte, ..)| byte);
        let (mut next, mut before) = (0, 0.0);
        let settled = ends
            .iter()
            .map(|&end| {
                // `ends` never falls, so the advances summed so far stay.
                while let Some(&(byte, advance, _)) = starts.get(next)
                    && byte < end
                {
                    before += advance;
                    next += 1;
                }
                // A prefix ending inside a glyph's cluster — a ligature's —
                // or where a cut could shape the glyphs before it otherwise
                // is not settled.
                let exact = end == 0
                    || starts
                        .get(next)
                        .is_some_and(|&(byte, _, safe)| byte == end && safe);
                let width = before + ellipsis;
                if !exact || (width - fit).abs() <= slack {
                    None
                } else {
                    Some(width < fit)
                }
            })
            .collect();
        Some(Estimate { settled })
    }

    /// The ellipsis this font can draw.
    pub(super) fn ellipsis(&self) -> &'static str {
        let c = '\u{2026}';
        let has = match self {
            Self::Bitmap { .. } => glyph_index(c) != NOTDEF_INDEX,
            Self::Parsed { font, .. } => font.glyph_id(c) != GlyphId::NOTDEF,
        };
        if has { ELLIPSIS } else { ELLIPSIS_FALLBACK }
    }
}

/// What [`cut`] did to a span's text.
pub(super) struct Cut {
    /// Whether any line was cut.
    pub(super) any: bool,
    /// What the text came out as, laid out unbroken in a parsed font, when it
    /// is one line and was laid out whole: the layout of the line that fit, or
    /// of the cut prefix and its ellipsis that did.
    pub(super) layout: Option<Arc<TextLayout>>,
}

/// Appends `text` to `out` with every line too wide for `width` cut to fit, as
/// the module docs say.
///
/// A line is cut by a binary search over its cluster boundaries. In a parsed
/// font each probe is first asked of the line's [`Estimate`], from its
/// shaping as a whole, and only the probes it cannot settle — and the prefix
/// the search lands on, whose layout is kept — are shaped with their
/// ellipsis. In a joining script the estimate settles only the probes that
/// end at a space, and every probe inside a word is shaped; in the bitmap
/// font every probe is measured.
///
/// **The cut lands where shaping every probe would only as far as the
/// estimate's margin, [`ELLIPSIS_KERN_EM`], holds**: that margin was
/// measured, not proven. Should the prefix landed on not fit after all, the
/// line is searched again with every probe shaped — but that catches only an
/// estimate that wrongly accepted the landing prefix. One that wrongly
/// rejected a prefix goes unnoticed, and the line is cut shorter than it
/// could be, though still to a prefix that fits.
pub(super) fn cut(measure: &Measure<'_>, text: &str, width: f32, out: &mut String) -> Cut {
    let fit = width.max(0.0) + WRAP_EPSILON;
    let mut scratch = String::new();
    let mut cut_any = false;
    // What the last line came out as, laid out.
    let mut shown = None;
    for (index, line) in text.split('\n').enumerate() {
        if index > 0 {
            out.push('\n');
        }
        let (whole, layout) = measure.width(line);
        if whole <= fit {
            out.push_str(line);
            shown = layout;
            continue;
        }
        cut_any = true;
        let ellipsis = measure.ellipsis();
        // Every cluster boundary short of the whole line, which is known not
        // to fit, as the end of the prefix a cut there leaves once its
        // trailing white space is dropped: the first is the empty prefix.
        let ends: Vec<usize> = cluster::clusters(line)
            .map(|range| line[..range.start].trim_end().len())
            .collect();
        let estimate = layout
            .as_deref()
            .and_then(|layout| measure.estimate(layout, &ends, ellipsis, fit));
        let mut probe = Probe {
            measure,
            line,
            ellipsis,
            fit,
            scratch: &mut scratch,
            fitted: None,
        };
        let settled = |index: usize| {
            estimate
                .as_ref()
                .and_then(|estimate| estimate.settles(index))
        };
        let mut at = search(ends.len(), |index| {
            settled(index).unwrap_or_else(|| probe.fits(ends[index]))
        });
        // The prefix landed on was settled by its estimate, so it is shaped
        // now, for its layout; should it not fit after all, the estimate was
        // wrong, and the line is searched again shaping every probe. A prefix
        // the estimate wrongly rejected is not caught here.
        if let Some(index) = at
            && probe
                .fitted
                .as_ref()
                .is_none_or(|(end, _)| *end != ends[index])
            && !probe.fits(ends[index])
        {
            at = search(ends.len(), |index| probe.fits(ends[index]));
        }
        let Some(at) = at else {
            continue;
        };
        out.push_str(&line[..ends[at]]);
        out.push_str(ellipsis);
        shown = probe.fitted.and_then(|(_, layout)| layout);
    }
    Cut {
        any: cut_any,
        // A layout of the last line is not one of the whole text.
        layout: shown.filter(|_| !text.contains('\n')),
    }
}

/// One line's prefixes measured with the ellipsis after them, for [`cut`].
struct Probe<'a> {
    measure: &'a Measure<'a>,
    line: &'a str,
    ellipsis: &'static str,
    /// The width a prefix must fit.
    fit: f32,
    scratch: &'a mut String,
    /// The end of the last prefix measured that fit, and its layout in a
    /// parsed font.
    fitted: Option<(usize, Option<Arc<TextLayout>>)>,
}

impl Probe<'_> {
    /// Whether the line's prefix ending at byte `end`, with the ellipsis
    /// after it, fits.
    fn fits(&mut self, end: usize) -> bool {
        self.scratch.clear();
        self.scratch.push_str(&self.line[..end]);
        self.scratch.push_str(self.ellipsis);
        let (width, layout) = self.measure.width(self.scratch);
        let fits = width <= self.fit;
        if fits {
            self.fitted = Some((end, layout));
        }
        fits
    }
}

/// The index of the prefix, of `count`, a line is cut to, binary searched with
/// `fits`; `None` when not even the empty prefix, the first, fits.
///
/// Kerning can make a longer prefix narrower, so the search keeps the
/// invariant that its `low` fits rather than relying on monotony: it lands on
/// a prefix that fits with the next one not fitting.
fn search(count: usize, mut fits: impl FnMut(usize) -> bool) -> Option<usize> {
    if !fits(0) {
        return None;
    }
    let (mut low, mut high) = (0, count);
    while high - low > 1 {
        let mid = low + (high - low) / 2;
        if fits(mid) {
            low = mid;
        } else {
            high = mid;
        }
    }
    Some(low)
}

/// How far from its [`Estimate`] a prefix and its ellipsis may shape, in ems,
/// where the estimate is otherwise exact: what the ellipsis kerns against the
/// prefix's last glyph by, and the ellipsis' own shaping against its nominal
/// advance, and the error sources [`Estimate`] lists. A sweep of Roboto,
/// FreeSans, FreeSerif, Liberation Serif, Adwaita Sans and the committed font
/// on 2026-10-31 found at most 0.11 em, Roboto's `T…`, and a review's sweep of
/// 18 system fonts the same; the margin is over twice that, for kerning in
/// fonts no sweep has seen, at the cost of shaping the probes it leaves
/// unsettled.
const ELLIPSIS_KERN_EM: f32 = 0.25;

/// A parsed-font line's cut prefixes measured from the line's shaping as a
/// whole, without shaping any of them.
///
/// A prefix's width is estimated as the advances of the glyphs and spaces the
/// whole line shaped before its end, plus the ellipsis' nominal advance. It
/// settles a probe only where the prefix ends at a space or where `harfrust`
/// says cutting before the glyph there shapes neither side differently, and
/// only when it is further from the width than [`ELLIPSIS_KERN_EM`]. Inside a
/// ligature, across a kerning pair or inside a word of a joining script it
/// settles nothing.
///
/// **Where it settles, it is not proven exact**; the margin has to cover:
///
/// * what the ellipsis kerns against the prefix's last glyph by, and its own
///   shaping against its nominal advance;
/// * a contextual rule — `calt`, a ligature, a word-final form — that fires
///   only when the ellipsis follows the prefix's last glyph, which the whole
///   line's shaping never flagged unsafe to break, as the ellipsis was not in
///   it;
/// * joining across a change of face: [`crate::font::shape::shape`] makes the
///   first glyph of each face's run safe to break, though Arabic joining reads
///   the neighbouring run as context;
/// * a mirrored bracket's advance, should cutting the line change the
///   bracket's direction.
struct Estimate {
    /// For each prefix: whether it fits, where its estimate is exact and
    /// further from the width than [`ELLIPSIS_KERN_EM`].
    settled: Vec<Option<bool>>,
}

impl Estimate {
    /// Whether prefix `index` fits, when its estimate settles it.
    fn settles(&self, index: usize) -> Option<bool> {
        self.settled[index]
    }
}