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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// 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: wrapping

use super::{NotReady, RunSpecial, TextDisplay};
use crate::conv::{to_u32, to_usize};
use crate::fonts::{fonts, FontLibrary};
use crate::shaper::GlyphRun;
use crate::{Action, Align, Range, Vec2};
use smallvec::SmallVec;
use unicode_bidi::Level;

#[derive(Clone, Debug)]
pub struct RunPart {
    pub text_end: u32,
    pub glyph_run: u32,
    pub glyph_range: Range,
    pub offset: Vec2,
}

#[derive(Clone, Debug)]
pub struct Line {
    pub text_range: Range, // range in text
    pub run_range: Range,  // range in wrapped_runs
    pub top: f32,
    pub bottom: f32,
}

#[derive(Clone, Debug)]
struct PartInfo {
    run: u32,
    offset: f32,
    len: f32,
    len_no_space: f32,
    glyph_range: Range,
    end_space: bool,
}

impl TextDisplay {
    /// Measure the maximum line length without wrapping
    ///
    /// This is a significantly faster way to calculate the required line length
    /// than [`Self::prepare_lines`].
    ///
    /// The return value is at most `limit` and is unaffected by alignment and
    /// wrap configuration of [`crate::Environment`].
    pub fn measure_width(&self, limit: f32) -> Result<f32, NotReady> {
        if self.action > Action::Wrap {
            return Err(NotReady);
        }

        let mut max_line_len = 0.0f32;
        let mut caret = 0.0;
        let mut line_len = 0.0;

        for run in self.runs.iter() {
            let num_parts = run.num_parts();
            let (_, part_len_no_space, part_len) = run.part_lengths(0..num_parts);

            if part_len_no_space > 0.0 {
                line_len = caret + part_len_no_space;
                if line_len >= limit {
                    return Ok(limit);
                }
            }
            caret += part_len;

            if run.special == RunSpecial::HardBreak {
                max_line_len = max_line_len.max(line_len);
                caret = 0.0;
                line_len = 0.0;
            }
        }

        Ok(max_line_len.max(line_len))
    }

    /// Prepare lines ("wrap")
    ///
    /// This does text layout, with wrapping if enabled.
    ///
    /// Returns:
    ///
    /// -   `Err(NotReady)` if required action is greater than [`Action::Wrap`]
    /// -   `Ok(bounding_corner)` on success
    pub fn prepare_lines(
        &mut self,
        bounds: Vec2,
        wrap: bool,
        align: (Align, Align),
    ) -> Result<Vec2, NotReady> {
        if self.action > Action::Wrap {
            return Err(NotReady);
        }
        self.action = Action::None;

        let fonts = fonts();
        let mut adder = LineAdder::new(bounds, align);
        let width_bound = adder.width_bound;
        let justify = align.0 == Align::Stretch;
        let mut parts = Vec::with_capacity(16);

        // Almost everything in "this" method depends on the line direction, so
        // we determine that then call the appropriate implementation.
        let mut level = unicode_bidi::LTR_LEVEL;
        let mut last_hard_break = true;

        // Tuples: (index, part, num_parts)
        let mut start = (0, 0, 0);
        let mut end = start;

        let mut caret = 0.0;
        let mut index = start.0;

        let end_index = self.runs.len();
        'a: while index < end_index {
            let run = &self.runs[index];
            let num_parts = run.num_parts();

            let hard_break = run.special == RunSpecial::HardBreak;
            let allow_break = run.special != RunSpecial::NoBreak;
            let tab = run.special == RunSpecial::HTab;

            if last_hard_break {
                level = run.level;
                last_hard_break = false;
            }

            let mut last_part = start.1;
            let mut part = last_part + 1;
            while part <= num_parts {
                let (part_offset, part_len_no_space, mut part_len) =
                    run.part_lengths(last_part..part);
                if tab {
                    // Tab runs have no glyph; instead we calculate part_len
                    // based on the current line length.

                    // TODO(bidi): we should really calculate this after
                    // re-ordering the line based on full line contents,
                    // then use a checkpoint reset if too long.

                    let sf = fonts.get_face(run.face_id).scale_by_dpu(run.dpu);
                    // TODO: custom tab sizes?
                    let tab_size = sf.h_advance(sf.face().glyph_index(' ')) * 8.0;
                    let stops = (caret / tab_size).floor() + 1.0;
                    part_len = tab_size * stops - caret;
                }

                let line_len = caret + part_len_no_space;
                if wrap && line_len > width_bound && end.2 > 0 {
                    // Add up to last valid break point then wrap and reset
                    let slice = &mut parts[0..end.2];
                    adder.add_line(fonts, level, &self.runs, slice, true);

                    end.2 = 0;
                    start = end;
                    parts.clear();
                    caret = 0.0;
                    index = start.0;
                    continue 'a;
                }
                caret += part_len;
                let glyph_range = run.to_glyph_range(last_part..part);
                let checkpoint = part < num_parts || allow_break;
                if !checkpoint
                    || (justify && part_len_no_space > 0.0)
                    || parts
                        .last()
                        .map(|part| to_usize(part.run) < index)
                        .unwrap_or(true)
                {
                    parts.push(PartInfo {
                        run: to_u32(index),
                        offset: part_offset,
                        len: part_len,
                        len_no_space: part_len_no_space,
                        glyph_range,
                        end_space: false, // set later
                    });
                } else {
                    // Combine with last part (not strictly necessary)
                    if let Some(part) = parts.last_mut() {
                        if run.level.is_ltr() {
                            part.glyph_range.end = glyph_range.end;
                        } else {
                            part.offset = part_offset;
                            part.glyph_range.start = glyph_range.start;
                        }
                        debug_assert!(part.glyph_range.start <= part.glyph_range.end);
                        if part_len_no_space > 0.0 {
                            part.len_no_space = part.len + part_len_no_space;
                        }
                        part.len += part_len;
                    }
                }
                if checkpoint {
                    end = (index, part, parts.len());
                }
                last_part = part;
                part += 1;
            }

            index += 1;
            start.1 = 0;

            if hard_break || index == end_index {
                if parts.len() > 0 {
                    // It should not be possible for a line to end with a no-break, so:
                    debug_assert_eq!(parts.len(), end.2);
                    adder.add_line(fonts, level, &self.runs, &mut parts, false);
                }

                start = (index, 0, 0);
                end = start;
                parts.clear();

                caret = 0.0;
                index = start.0;
                last_hard_break = true;
            }
        }

        let bounding_corner = adder.finish(bounds, align);
        self.wrapped_runs = adder.runs;
        self.lines = adder.lines;
        #[cfg(feature = "num_glyphs")]
        {
            self.num_glyphs = adder.num_glyphs;
        }
        self.l_bound = adder.l_bound;
        self.r_bound = bounding_corner.0;
        Ok(bounding_corner)
    }

    /// Vertically align lines
    ///
    /// Returns the bottom-right bounding corner.
    pub fn vertically_align(&mut self, bound: f32, v_align: Align) -> Result<Vec2, NotReady> {
        if self.action > Action::VAlign {
            return Err(NotReady);
        }
        self.action = Action::None;

        if self.lines.is_empty() {
            return Ok(Vec2(0.0, 0.0));
        }

        let top = self.lines.first().unwrap().top;
        let bottom = self.lines.last().unwrap().bottom;
        let height = bottom - top;
        let new_offset = match v_align {
            _ if height >= bound => 0.0,
            Align::Default | Align::TL | Align::Stretch => 0.0,
            Align::Center => 0.5 * (bound - height),
            Align::BR => bound - height,
        };
        let offset = new_offset - top;

        if offset != 0.0 {
            for run in &mut self.wrapped_runs {
                run.offset.1 += offset;
            }
            for line in &mut self.lines {
                line.top += offset;
                line.bottom += offset;
            }
        }

        Ok(Vec2(self.r_bound, bottom))
    }
}

#[derive(Default)]
struct LineAdder {
    runs: SmallVec<[RunPart; 1]>,
    lines: SmallVec<[Line; 1]>,
    line_gap: f32,
    l_bound: f32,
    r_bound: f32,
    vcaret: f32,
    #[cfg(feature = "num_glyphs")]
    num_glyphs: u32,
    halign: Align,
    width_bound: f32,
}
impl LineAdder {
    fn new(bounds: Vec2, align: (Align, Align)) -> Self {
        LineAdder {
            halign: align.0,
            width_bound: bounds.0,
            ..Default::default()
        }
    }

    fn add_line(
        &mut self,
        fonts: &FontLibrary,
        line_level: Level,
        runs: &[GlyphRun],
        parts: &mut [PartInfo],
        is_wrap: bool,
    ) {
        assert!(parts.len() > 0);
        let line_start = self.runs.len();
        let line_is_rtl = line_level.is_rtl();

        // Iterate runs to determine max ascent, level, etc.
        let mut last_run = u32::MAX;
        let (mut ascent, mut descent, mut line_gap) = (0f32, 0f32, 0f32);
        let mut max_level = line_level;
        for part in parts.iter() {
            if last_run == part.run {
                continue;
            }
            last_run = part.run;
            let run = &runs[to_usize(last_run)];

            let scale_font = fonts.get_face(run.face_id).scale_by_dpu(run.dpu);
            ascent = ascent.max(scale_font.ascent());
            descent = descent.min(scale_font.descent());
            line_gap = line_gap.max(scale_font.line_gap());

            debug_assert!(run.level >= line_level);
            max_level = max_level.max(run.level);
        }

        if !self.lines.is_empty() {
            self.vcaret += line_gap.max(self.line_gap);
        }
        self.vcaret += ascent;
        self.line_gap = line_gap;

        let line_text_start = {
            let part = &parts[0];
            let run = &runs[to_usize(part.run)];
            if run.level.is_ltr() {
                if part.glyph_range.start() < run.glyphs.len() {
                    run.glyphs[part.glyph_range.start()].index
                } else {
                    run.range.start
                }
            } else {
                let end = part.glyph_range.end();
                if 0 < end && end < run.glyphs.len() {
                    run.glyphs[end - 1].index
                } else {
                    run.range.start
                }
            }
        };

        // Adjust the (logical) tail: optionally exclude last glyph, calculate
        // line_text_end, trim whitespace for the purposes of layout.
        let mut line_text_end;
        let mut line_len = 0.0;
        {
            let part = &mut parts[parts.len() - 1];
            let run = &runs[to_usize(part.run)];

            if is_wrap && part.len > part.len_no_space {
                // When wrapping on whitespace: exclude the last glyph
                // This excludes only one glyph; the main aim is to make the
                // 'End' key exclude the wrapping position (which is also the
                // next line's start). It also avoids highlighting whitespace
                // when selecting wrapped bidi text, for a single space.
                if part.glyph_range.start < part.glyph_range.end {
                    if run.level.is_ltr() {
                        part.glyph_range.end -= 1;
                    } else {
                        part.glyph_range.start += 1;
                    }
                }
            }

            line_text_end = run.range.end;
            if run.level.is_ltr() {
                if part.glyph_range.end() < run.glyphs.len() {
                    line_text_end = run.glyphs[part.glyph_range.end()].index;
                }
            } else {
                let start = part.glyph_range.start();
                if 0 < start && start < run.glyphs.len() {
                    line_text_end = run.glyphs[start - 1].index
                }
            }

            // With bidi text, the logical end may not actually be at the end;
            // we must not allow spaces here to move other content.
            for part in parts.iter_mut().rev() {
                part.end_space = true;

                if part.len_no_space > 0.0 {
                    break;
                }
            }
            for part in parts.iter() {
                line_len += if part.end_space {
                    part.len_no_space
                } else {
                    part.len
                };
            }
        }

        // Unic TR#9 L2: reverse items on the line
        // This implementation does not correspond directly to the Unicode
        // algorithm, which assumes that shaping happens *after* re-arranging
        // chars (but also *before*, in order to calculate line-wrap points).
        // Our shaper(s) accept both LTR and RTL input; additionally, our line
        // wrapping must explicitly handle both LTR and RTL lines; the missing
        // step is to rearrange non-wrapped runs on the line.

        {
            let mut level = max_level;
            while level > Level::ltr() {
                let mut start = None;
                for i in 0..parts.len() {
                    let part_level = runs[to_usize(parts[i].run)].level;
                    if let Some(s) = start {
                        if part_level < level {
                            parts[s..i].reverse();
                            start = None;
                        }
                    } else if part_level >= level {
                        start = Some(i);
                    }
                }
                if let Some(s) = start {
                    parts[s..].reverse();
                }
                level.lower(1).unwrap();
            }
        }

        let spare = self.width_bound - line_len;
        let mut is_gap = SmallVec::<[bool; 16]>::new();
        let mut per_gap = 0.0;
        let mut caret = match self.halign {
            Align::Default => match line_is_rtl {
                false => 0.0,
                true => spare,
            },
            Align::TL => 0.0,
            Align::Center => 0.5 * spare,
            Align::BR => spare,
            Align::Stretch if !is_wrap => match line_is_rtl {
                false => 0.0,
                true => spare,
            },
            Align::Stretch => {
                let len = parts.len();
                is_gap.resize(len, false);
                let mut num_gaps = 0;
                for (i, part) in parts[..len - 1].iter().enumerate() {
                    let run = &runs[to_usize(part.run)];
                    let not_at_end = if run.level.is_ltr() {
                        part.glyph_range.end() < run.glyphs.len()
                    } else {
                        part.glyph_range.start > 0
                    };
                    if not_at_end || run.special != RunSpecial::NoBreak {
                        is_gap[i] = true;
                        num_gaps += 1;
                    }
                }

                // Apply per-level reversing to is_gap
                let mut start = 0;
                let mut level = runs[to_usize(parts[0].run)].level;
                for i in 1..len {
                    let new_level = runs[to_usize(parts[i].run)].level;
                    if level != new_level {
                        if level > line_level {
                            is_gap[start..i].reverse();
                        }
                        start = i;
                        level = new_level;
                    }
                }
                if level > line_level {
                    is_gap[start..len - 1].reverse();
                }

                // Shift left 1 part for RTL lines
                if line_level.is_rtl() {
                    is_gap.copy_within(1..len, 0);
                }

                per_gap = spare / (num_gaps as f32);
                0.0
            }
        };

        self.l_bound = self.l_bound.min(caret);
        let mut end_caret = caret;

        for (i, part) in parts.iter().enumerate() {
            let run = &runs[to_usize(part.run)];

            #[cfg(feature = "num_glyphs")]
            {
                self.num_glyphs += to_u32(part.glyph_range.len());
            }

            let mut text_end = run.range.end;
            let mut offset = 0.0;
            if run.level.is_ltr() {
                if part.glyph_range.end() < run.glyphs.len() {
                    text_end = run.glyphs[part.glyph_range.end()].index;
                }
            } else {
                let start = part.glyph_range.start();
                if 0 < start && start < run.glyphs.len() {
                    text_end = run.glyphs[start - 1].index
                }
                offset = part.len_no_space - part.len;
            }

            let xoffset = if part.end_space {
                end_caret - part.offset + offset
            } else {
                caret - part.offset
            };
            self.runs.push(RunPart {
                text_end,
                glyph_run: part.run,
                glyph_range: part.glyph_range,
                offset: Vec2(xoffset, self.vcaret),
            });
            if part.end_space {
                caret += part.len_no_space;
                end_caret += part.len;
            } else {
                caret += part.len;
                end_caret = caret;
            }

            if is_gap.len() > 0 && is_gap[i] {
                caret += per_gap;
                end_caret += per_gap;
            }
        }

        self.r_bound = self.r_bound.max(caret);

        // Other parts of this library expect runs to be in logical order, so
        // we re-order now (does not affect display position).
        // TODO: should we change this, e.g. for visual-order navigation?
        self.runs[line_start..].sort_by_key(|run| run.text_end);

        let top = self.vcaret - ascent;
        self.vcaret -= descent;
        // Vertically align lines to the nearest pixel (improves rendering):
        self.vcaret = self.vcaret.round();

        self.lines.push(Line {
            text_range: Range::from(line_text_start..line_text_end),
            run_range: (line_start..self.runs.len()).into(),
            top,
            bottom: self.vcaret,
        });
    }

    /// Returns the bottom-right bounding corner.
    fn finish(&mut self, bounds: Vec2, align: (Align, Align)) -> Vec2 {
        let height = self.vcaret;
        let offset = match align.1 {
            _ if !(height < bounds.1) => 0.0,
            Align::Default | Align::TL | Align::Stretch => 0.0, // nothing to do
            Align::Center => 0.5 * (bounds.1 - height),
            Align::BR => bounds.1 - height,
        };
        if offset != 0.0 {
            for run in &mut self.runs {
                run.offset.1 += offset;
            }
            for line in &mut self.lines {
                line.top += offset;
                line.bottom += offset;
            }
        }

        Vec2(self.r_bound, height + offset)
    }
}