kas-text 0.9.3

Text layout and font management
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
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE-APACHE file or at:
//     https://www.apache.org/licenses/LICENSE-2.0

//! Text preparation: line breaking and BIDI

#![allow(clippy::unnecessary_unwrap)]

use super::TextDisplay;
use crate::conv::{to_u32, to_usize};
use crate::fonts::{self, FaceId, FontSelector, NoFontMatch};
use crate::format::FormattableText;
use crate::util::ends_with_hard_break;
use crate::{Direction, Range, shaper};
use icu_properties::props::{EmojiModifier, EmojiPresentation, RegionalIndicator, Script};
use icu_properties::{CodePointMapData, CodePointSetData};
use icu_segmenter::LineSegmenter;
use std::sync::OnceLock;
use unicode_bidi::{BidiInfo, LTR_LEVEL, RTL_LEVEL};

#[derive(Clone, Copy, Debug, PartialEq)]
pub(crate) enum RunSpecial {
    None,
    /// Run ends with a hard break
    HardBreak,
    /// Run does not end with a break
    NoBreak,
    /// Run is a horizontal tab (run is a single char only)
    HTab,
}

impl TextDisplay {
    /// Update font size
    ///
    /// [Requires status][Self#status-of-preparation]: level runs have been
    /// prepared and are valid in all ways except size (`dpem`).
    ///
    /// This updates the result of [`TextDisplay::prepare_runs`] due to change
    /// in font size.
    pub fn resize_runs<F: FormattableText + ?Sized>(&mut self, text: &F, mut dpem: f32) {
        let mut font_tokens = text.font_tokens(dpem);
        let mut next_fmt = font_tokens.next();

        let text = text.as_str();

        for run in &mut self.runs {
            while let Some(fmt) = next_fmt.as_ref() {
                if fmt.start > run.range.start {
                    break;
                }
                dpem = fmt.dpem;
                next_fmt = font_tokens.next();
            }

            let input = shaper::Input {
                text,
                dpem,
                level: run.level,
                script: run.script,
            };
            let mut breaks = Default::default();
            std::mem::swap(&mut breaks, &mut run.breaks);
            if run.level.is_rtl() {
                breaks.reverse();
            }
            *run = shaper::shape(input, run.range, run.face_id, breaks, run.special);
        }
    }

    /// Resolve font face and shape run
    ///
    /// This may sub-divide text as required to find matching fonts.
    fn push_run(
        &mut self,
        font: FontSelector,
        input: shaper::Input,
        range: Range,
        mut breaks: tinyvec::TinyVec<[shaper::GlyphBreak; 4]>,
        special: RunSpecial,
        first_real: Option<char>,
    ) -> Result<(), NoFontMatch> {
        let fonts = fonts::library();
        let font_id = fonts.select_font(&font, input.script.into())?;
        let text = &input.text[range.to_std()];

        // Find a font face
        let mut face_id = None;
        if let Some(c) = first_real {
            face_id = fonts
                .face_for_char(font_id, None, c)
                .expect("invalid FontId");
        }

        let mut face = match face_id {
            Some(id) => id,
            None => {
                // We failed to find a font face for the run
                fonts.first_face_for(font_id).expect("invalid FontId")
            }
        };

        let mut start = 0;
        for (index, c) in text.char_indices() {
            let index = to_u32(index);
            if let Some(new_face) = fonts
                .face_for_char(font_id, Some(face), c)
                .expect("invalid FontId")
                && new_face != face
            {
                if index > start {
                    let sub_range = Range {
                        start: range.start + start,
                        end: range.start + index,
                    };
                    let mut j = 0;
                    for i in 0..breaks.len() {
                        if breaks[i].index < sub_range.end {
                            j = i + 1;
                        }
                    }
                    let rest = breaks.split_off(j);

                    self.runs.push(shaper::shape(
                        input,
                        sub_range,
                        face,
                        breaks,
                        RunSpecial::NoBreak,
                    ));
                    breaks = rest;
                    start = index;
                }

                face = new_face;
            }
        }

        let sub_range = Range {
            start: range.start + start,
            end: range.end,
        };
        self.runs
            .push(shaper::shape(input, sub_range, face, breaks, special));
        Ok(())
    }

    /// Break text into level runs
    ///
    /// [Requires status][Self#status-of-preparation]: none.
    ///
    /// Must be called again if any of `text`, `direction` or `font` change.
    /// If only `dpem` changes, [`Self::resize_runs`] may be called instead.
    ///
    /// The text is broken into a set of contiguous "level runs". These runs are
    /// maximal slices of the `text` which do not contain explicit line breaks
    /// and have a single text direction according to the
    /// [Unicode Bidirectional Algorithm](http://www.unicode.org/reports/tr9/).
    pub fn prepare_runs<F: FormattableText + ?Sized>(
        &mut self,
        text: &F,
        direction: Direction,
        mut font: FontSelector,
        mut dpem: f32,
    ) -> Result<(), NoFontMatch> {
        // This method constructs a list of "hard lines" (the initial line and any
        // caused by a hard break), each composed of a list of "level runs" (the
        // result of splitting and reversing according to Unicode TR9 aka
        // Bidirectional algorithm), plus a list of "soft break" positions
        // (where wrapping may introduce new lines depending on available space).

        self.runs.clear();

        let mut font_tokens = text.font_tokens(dpem);
        let mut next_fmt = font_tokens.next();
        if let Some(fmt) = next_fmt.as_ref()
            && fmt.start == 0
        {
            font = fmt.font;
            dpem = fmt.dpem;
            next_fmt = font_tokens.next();
        }

        let text = text.as_str();

        let default_para_level = match direction {
            Direction::Auto => None,
            Direction::AutoRtl => {
                use unicode_bidi::Direction::*;
                match unicode_bidi::get_base_direction(text) {
                    Ltr | Rtl => None,
                    Mixed => Some(RTL_LEVEL),
                }
            }
            Direction::Ltr => Some(LTR_LEVEL),
            Direction::Rtl => Some(RTL_LEVEL),
        };
        let info = BidiInfo::new(text, default_para_level);
        let levels = info.levels;
        assert_eq!(text.len(), levels.len());

        let mut input = shaper::Input {
            text,
            dpem,
            level: levels.first().cloned().unwrap_or(LTR_LEVEL),
            script: Script::Unknown,
        };

        let mut start = 0;
        let mut breaks = Default::default();

        // TODO: allow segmenter configuration
        let segmenter = LineSegmenter::new_auto(Default::default());
        let mut break_iter = segmenter.segment_str(text);
        let mut next_break = break_iter.next();

        let mut first_real = None;
        let mut emoji_state = EmojiState::None;
        let mut emoji_start = 0;
        let mut emoji_end = 0;

        let mut last_is_control = false;
        let mut last_is_htab = false;
        let mut non_control_end = 0;

        for (index, c) in text
            .char_indices()
            .chain(std::iter::once((text.len(), '\0')))
        {
            // Handling for control chars
            if !last_is_control {
                non_control_end = index;
            }
            let is_htab = c == '\t';
            let mut require_break = last_is_htab;
            let is_control = c.is_control();

            // Is wrapping allowed at this position?
            let is_break = next_break == Some(index);
            let hard_break = is_break && ends_with_hard_break(&text[..index]);
            if is_break {
                next_break = break_iter.next();
            }

            let script = CodePointMapData::<Script>::new().get(c);

            let emoji_break = emoji_state.advance(c);
            let mut new_emoji_start = emoji_start;
            let mut is_emoji = false;
            let prohibit_break = match emoji_break {
                EmojiBreak::None => false,
                EmojiBreak::Start => {
                    require_break = true;
                    new_emoji_start = index;
                    false
                }
                EmojiBreak::Prohibit => {
                    emoji_end = index;
                    true
                }
                EmojiBreak::End => {
                    require_break = true;
                    emoji_end = index;
                    debug_assert!(emoji_end > emoji_start);
                    is_emoji = true;
                    false
                }
                EmojiBreak::Restart => {
                    require_break = true;
                    emoji_end = index;
                    new_emoji_start = index;
                    debug_assert!(emoji_end > emoji_start);
                    is_emoji = true;
                    false
                }
                EmojiBreak::Error => {
                    is_emoji = emoji_end > emoji_start;
                    require_break = is_emoji;
                    false
                }
            };

            // Force end of current run?
            require_break |= levels
                .get(index)
                .map(|level| *level != input.level)
                .unwrap_or(true);

            if let Some(fmt) = next_fmt.as_ref()
                && to_usize(fmt.start) == index
            {
                require_break = true;
            }

            let mut new_script = None;
            if is_real(script) {
                if first_real.is_none() {
                    first_real = Some(c);
                }
                if script != input.script {
                    new_script = Some(script);
                    require_break |= is_real(input.script);
                }
            }

            if !prohibit_break && (hard_break || require_break) {
                let special = match () {
                    _ if hard_break => RunSpecial::HardBreak,
                    _ if last_is_htab => RunSpecial::HTab,
                    _ if last_is_control || is_break => RunSpecial::None,
                    _ => RunSpecial::NoBreak,
                };

                if is_emoji {
                    let range = (emoji_start..emoji_end).into();
                    let face = emoji_face_id()?;
                    self.runs
                        .push(shaper::shape(input, range, face, breaks, special));
                } else {
                    // NOTE: the range may be empty; we need it anyway (unless
                    // we modify the last run's special property).
                    let range = (start..non_control_end).into();
                    self.push_run(font, input, range, breaks, special, first_real)?;
                };
                first_real = None;

                start = index;
                non_control_end = index;
                if let Some(level) = levels.get(index) {
                    input.level = *level;
                }
                input.script = script;
                breaks = Default::default();
            } else if is_break && !is_control {
                breaks.push(shaper::GlyphBreak::new(to_u32(index)));
            }

            if let Some(fmt) = next_fmt.as_ref()
                && to_usize(fmt.start) == index
            {
                font = fmt.font;
                input.dpem = fmt.dpem;
                next_fmt = font_tokens.next();
                debug_assert!(
                    next_fmt
                        .as_ref()
                        .map(|fmt| to_usize(fmt.start) > index)
                        .unwrap_or(true)
                );
            }

            last_is_control = is_control;
            last_is_htab = is_htab;
            emoji_start = new_emoji_start;
            if let Some(script) = new_script {
                input.script = script;
            }
        }

        let hard_break = ends_with_hard_break(&text);

        // Following a hard break we have an implied empty line.
        if hard_break {
            let range = (text.len()..text.len()).into();
            input.level = default_para_level.unwrap_or(LTR_LEVEL);
            breaks = Default::default();
            self.push_run(font, input, range, breaks, RunSpecial::None, None)?;
        }

        /*
        println!("text: {}", text);
        let fonts = fonts::library();
        for run in &self.runs {
            let slice = &text[run.range];
            print!(
                "\t{:?}, text[{}..{}]: '{}', ",
                run.level, run.range.start, run.range.end, slice
            );
            match run.special {
                RunSpecial::None => (),
                RunSpecial::HardBreak => print!("HardBreak, "),
                RunSpecial::NoBreak => print!("NoBreak, "),
                RunSpecial::HTab => print!("HTab, "),
            }
            print!("breaks=[");
            let mut iter = run.breaks.iter();
            if let Some(b) = iter.next() {
                print!("{}", b.index);
            }
            for b in iter {
                print!(", {}", b.index);
            }
            print!("]");
            if let Some(name) = fonts.get_face_store(run.face_id).name_full() {
                print!(", {name}");
            }
            println!();
        }
        */
        Ok(())
    }
}

fn is_real(script: Script) -> bool {
    !matches!(script, Script::Common | Script::Unknown | Script::Inherited)
}

fn emoji_face_id() -> Result<FaceId, NoFontMatch> {
    static ONCE: OnceLock<Result<FaceId, NoFontMatch>> = OnceLock::new();
    *ONCE.get_or_init(|| {
        let fonts = fonts::library();
        let font = fonts.select_font(&FontSelector::EMOJI, Script::Common.into());
        font.map(|font_id| fonts.first_face_for(font_id).expect("invalid FontId"))
    })
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum EmojiBreak {
    /// Not an Emoji
    None,
    /// Start of an Emoji sequence
    Start,
    /// Mid Emoji sequence, valid
    Prohibit,
    /// End of a valid Emoji sequence
    End,
    /// End of one Emoji and start of another
    Restart,
    /// Error; revert to last known good index
    Error,
}

enum EmojiState {
    None,
    RI1,
    RI2,
    Emoji,
    EMod,
    VarSelector,
    TagModifier,
    ZWJ,
}

impl EmojiState {
    /// Advance the emoji state machine
    ///
    /// Returns whether a break should occur before `c`.
    fn advance(&mut self, c: char) -> EmojiBreak {
        // Reference: https://unicode.org/reports/tr51/#EBNF_and_Regex
        #[allow(non_snake_case)]
        fn end_unless_ZWJ(c: char, b: &mut EmojiBreak) -> EmojiState {
            if c == '\u{200D}' {
                EmojiState::ZWJ
            } else {
                *b = EmojiBreak::End;
                EmojiState::None
            }
        }
        let mut b = EmojiBreak::None;
        *self = match *self {
            EmojiState::None => {
                if CodePointSetData::new::<RegionalIndicator>().contains(c) {
                    b = EmojiBreak::Start;
                    EmojiState::RI1
                } else if CodePointSetData::new::<EmojiPresentation>().contains(c) {
                    b = EmojiBreak::Start;
                    EmojiState::Emoji
                } else {
                    EmojiState::None
                }
            }
            EmojiState::RI1 => {
                if CodePointSetData::new::<RegionalIndicator>().contains(c) {
                    b = EmojiBreak::Prohibit;
                    EmojiState::RI2
                } else {
                    b = EmojiBreak::Error;
                    EmojiState::None
                }
            }
            EmojiState::RI2 => end_unless_ZWJ(c, &mut b),
            EmojiState::Emoji => {
                if CodePointSetData::new::<EmojiModifier>().contains(c) {
                    EmojiState::EMod
                } else if c == '\u{FE0F}' {
                    EmojiState::VarSelector
                } else if ('\u{E0020}'..='\u{E007E}').contains(&c) {
                    EmojiState::TagModifier
                } else if c == '\u{200D}' {
                    EmojiState::ZWJ
                } else {
                    b = EmojiBreak::End;
                    EmojiState::None
                }
            }
            EmojiState::EMod => end_unless_ZWJ(c, &mut b),
            EmojiState::VarSelector => {
                if c == '\u{20E3}' {
                    end_unless_ZWJ(c, &mut b)
                } else {
                    b = EmojiBreak::End;
                    EmojiState::None
                }
            }
            EmojiState::TagModifier => {
                if ('\u{E0020}'..='\u{E007E}').contains(&c) {
                    EmojiState::TagModifier
                } else if c == '\u{E007F}' {
                    end_unless_ZWJ(c, &mut b)
                } else {
                    b = EmojiBreak::Error;
                    EmojiState::None
                }
            }
            EmojiState::ZWJ => {
                if CodePointSetData::new::<RegionalIndicator>().contains(c) {
                    EmojiState::RI1
                } else if CodePointSetData::new::<EmojiPresentation>().contains(c) {
                    EmojiState::Emoji
                } else {
                    b = EmojiBreak::Error;
                    EmojiState::None
                }
            }
        };
        if b == EmojiBreak::End {
            *self = if CodePointSetData::new::<RegionalIndicator>().contains(c) {
                b = EmojiBreak::Restart;
                EmojiState::RI1
            } else if CodePointSetData::new::<EmojiPresentation>().contains(c) {
                b = EmojiBreak::Restart;
                EmojiState::Emoji
            } else {
                EmojiState::None
            };
        }
        b
    }
}