lib/std/lang/layout/text.rad 26.8 KiB raw
1
//! Text renderer for layouts.
2
//!
3
//! Renders a layout as plain text with a line width and an indentation unit.
4
//! Each box is flat if it fits and broken otherwise. A box fits if, with it and
5
//! every box nested in it flat, the line stays within the width up to the next
6
//! line break, counting the content that follows the box. Boxes in that
7
//! content can break at their first soft placement. The content is summarized
8
//! by a [`Span`] that each box passes to its children, so rendering does not
9
//! allocate.
10
//!
11
//! Comments do not count toward the width, because they cannot move. A comment
12
//! longer than the width does not stop the content before it from fitting. Any
13
//! content after a comment on the same line cannot fit.
14
//!
15
//! A choice takes its first alternative that fits as a box does, and its last
16
//! alternative otherwise. An alternative that starts on a new line fits if
17
//! its first line fits.
18
//!
19
//! Consecutive rows that have a tab stop of a column, and that each fit on
20
//! one line, are a run. The rows of a run are padded with spaces at the stop
21
//! to the widest one, unless a line of the run would then pass the width, or
22
//! the column after the padding would be at or past it. Each row of the run
23
//! is measured once to find the run, so rendering allocates nothing.
24
//!
25
//! Newlines and indentation are written lazily, before the next text on a
26
//! line, so the output has no trailing whitespace and empty lines are empty.
27
28
use std::lang::sexpr;
29
30
/// Text rendering options.
31
export record Options {
32
    /// Maximum line width in columns.
33
    width: u32,
34
    /// Spaces per indentation level.
35
    indent: u32,
36
}
37
38
/// Decision for the soft placements of the children of a box.
39
union Mode: Copy {
40
    /// Soft placements stay on the line.
41
    Flat,
42
    /// Soft placements start a new line.
43
    Broken,
44
}
45
46
/// What ended the text written or measured on the current line so far.
47
union Tail: Copy {
48
    /// Code, which later content may follow on the same line.
49
    Code,
50
    /// A comment, which runs to the end of the line.
51
    Comment,
52
}
53
54
/// How a measured span of a line ends.
55
union Status: Copy {
56
    /// The span ends where more content may follow on the same line.
57
    Open,
58
    /// The span ends at a line break.
59
    Closed,
60
    /// The span cannot fit in the width at any start column.
61
    Bad,
62
}
63
64
/// Measure of flat content on one line, relative to its start column.
65
record Span: Copy {
66
    /// Columns from the start to the end of the last text, which is not a
67
    /// comment.
68
    reach: u32,
69
    /// Columns from the start to the end, including trailing gaps.
70
    end: u32,
71
    /// What ended the last text, including a comment.
72
    tail: Tail,
73
    /// How the span ends.
74
    status: Status,
75
}
76
77
/// Output position and pending whitespace.
78
record State {
79
    /// Rendering options.
80
    options: Options,
81
    /// Column after the pending whitespace, counting an indented line start.
82
    col: u32,
83
    /// Newlines not written yet.
84
    newlines: u32,
85
    /// Spaces not written yet, between text on one line.
86
    spaces: u32,
87
    /// Number of bytes of text written.
88
    written: u32,
89
    /// What ended the line so far.
90
    tail: Tail,
91
    /// Spaces that the tab stop of each column adds, set for the row that is
92
    /// rendered.
93
    pads: [u32; super::CELL_COLUMNS],
94
}
95
96
/// Rows of consecutive children whose tab stops of one column line up.
97
record Run: Copy {
98
    /// Index of the child after the last row of the run.
99
    end: u32,
100
    /// Width that the rows are padded to at the stop.
101
    target: u32,
102
    /// Whether the rows are padded.
103
    aligned: bool,
104
}
105
106
/// Render a layout as text.
107
///
108
/// Boxes are flat when they fit in `options.width` columns. The output ends
109
/// with one newline if anything was written.
110
export fn render(
111
    root: *super::Layout, options: Options, out: &mut opaque sexpr::Output
112
) {
113
    let mut st = state(options);
114
    renderLayout(&mut st, out, root, 0, emptySpan(), false);
115
    if st.written > 0 {
116
        out.write("\n");
117
    }
118
}
119
120
/// Render first alternatives with fixed line breaks and indentation.
121
/// Soft placements stay flat unless their box is explicitly broken. Tab stops add no padding.
122
export fn renderFixed(
123
    root: *super::Layout, indent: u32, out: &mut opaque sexpr::Output
124
) {
125
    let mut st = state(Options { width: 0, indent });
126
    fixedLayout(&mut st, out, root, Mode::Flat, 0);
127
    if st.written > 0 {
128
        out.write("\n");
129
    }
130
}
131
132
/// Initialize output position and pending whitespace for a renderer.
133
fn state(options: Options) -> State {
134
    return State {
135
        options,
136
        col: 0,
137
        newlines: 0,
138
        spaces: 0,
139
        written: 0,
140
        tail: Tail::Code,
141
        pads: [0; super::CELL_COLUMNS],
142
    };
143
}
144
145
/// Render fixed placements without line measurements or alignment runs.
146
fn fixedLayout(
147
    st: &mut State, out: &mut opaque sexpr::Output, layout: *super::Layout, mode: Mode, indent: u32
148
) {
149
    match layout.item {
150
        case super::Item::Text { text, role } => writeText(st, out, text, role),
151
        case super::Item::Blank, super::Item::Tab(_), super::Item::BrokenText { .. } => {}
152
        case super::Item::Broken(inner) => fixedLayout(st, out, inner, Mode::Broken, indent),
153
        case super::Item::Choice(alts) => fixedLayout(st, out, alts[0], mode, indent),
154
        case super::Item::Box(children) => {
155
            for child in children {
156
                if let case super::Item::BrokenText { text, role } = child.layout.item {
157
                    if mode == Mode::Broken {
158
                        place(st, child.placement, mode, indent);
159
                        writeText(st, out, text, role);
160
                    }
161
                } else {
162
                    let level = place(st, child.placement, mode, indent);
163
                    fixedLayout(st, out, child.layout, Mode::Flat, level);
164
                }
165
            }
166
        }
167
    }
168
}
169
170
/// Number of columns of a gap.
171
fn gapWidth(gap: super::Gap) -> u32 {
172
    match gap {
173
        case super::Gap::None  => return 0,
174
        case super::Gap::Space => return 1,
175
    }
176
}
177
178
/// Gap before a child placed with `placement` in a box with the given mode,
179
/// or nil if the placement starts a new line.
180
fn inlineGap(placement: super::Placement, mode: Mode) -> ?super::Gap {
181
    match placement {
182
        case super::Placement::Inline(gap) => return gap,
183
        case super::Placement::SoftLine(gap) => {
184
            if mode == Mode::Flat {
185
                return gap;
186
            }
187
            return nil;
188
        }
189
        case super::Placement::SoftIndent(gap) => {
190
            if mode == Mode::Flat {
191
                return gap;
192
            }
193
            return nil;
194
        }
195
        else => return nil,
196
    }
197
}
198
199
/// Span of no content.
200
fn emptySpan() -> Span {
201
    return Span { reach: 0, end: 0, tail: Tail::Code, status: Status::Open };
202
}
203
204
/// Span of `b` placed after `a` with a gap of `gap` columns, where `a` is open.
205
fn join(a: Span, gap: u32, b: Span, width: u32) -> Span {
206
    let mut reach = a.reach;
207
    let mut tail = a.tail;
208
    let mut status = b.status;
209
    if b.reach > 0 {
210
        set reach = a.end + gap + b.reach;
211
        set tail = b.tail;
212
        // A comment runs to the end of the line.
213
        if a.tail == Tail::Comment or reach > width {
214
            set status = Status::Bad;
215
        }
216
    } else if b.tail == Tail::Comment {
217
        // A comment alone takes no width, and ends the line.
218
        set tail = Tail::Comment;
219
        if a.tail == Tail::Comment {
220
            set status = Status::Bad;
221
        }
222
    }
223
    if a.status == Status::Bad {
224
        set status = Status::Bad;
225
    }
226
    return Span { reach, end: a.end + gap + b.end, tail, status };
227
}
228
229
/// Measure a text fragment. A comment takes no width.
230
fn measureText(text: *[u8], role: super::Role, width: u32) -> Span {
231
    if role == super::Role::Comment {
232
        return Span {
233
            reach: 0, end: 0, tail: Tail::Comment, status: Status::Open
234
        };
235
    }
236
    return Span {
237
        reach: text.len,
238
        end: text.len,
239
        tail: Tail::Comment if role == super::Role::Comment else Tail::Code,
240
        status: Status::Bad if text.len > width else Status::Open,
241
    };
242
}
243
244
/// Children of the box that a broken layout wraps.
245
fn brokenChildren(inner: *super::Layout) -> *[super::Child] {
246
    let case super::Item::Box(children) = inner.item else {
247
        panic "brokenChildren: broken layout is not a box";
248
    };
249
    return children;
250
}
251
252
/// Measure a layout with every nested box flat, every choice at its first
253
/// alternative, and every broken box up to its earliest possible line break.
254
fn measureLayout(layout: *super::Layout, width: u32) -> Span {
255
    match layout.item {
256
        case super::Item::Text { text, role } =>
257
            return measureText(text, role, width),
258
        case super::Item::Box(children) =>
259
            return measureChildren(children, 0, Mode::Flat, Mode::Flat, width),
260
        case super::Item::BrokenText { .. } => return emptySpan(),
261
        case super::Item::Blank => {
262
            let mut span = emptySpan();
263
            set span.status = Status::Closed;
264
            return span;
265
        }
266
        case super::Item::Choice(alts) => return measureLayout(alts[0], width),
267
        case super::Item::Tab(_)       => return emptySpan(),
268
        case super::Item::Broken(_)    => return measureOpening(layout, width),
269
    }
270
}
271
272
/// Measure a layout up to its earliest possible line break, with every
273
/// nested box broken, and every choice at the alternative that breaks first.
274
fn measureOpening(layout: *super::Layout, width: u32) -> Span {
275
    match layout.item {
276
        case super::Item::Box(children) =>
277
            return measureChildren(
278
                children, 0, Mode::Broken, Mode::Broken, width
279
            ),
280
        case super::Item::Choice(alts) => {
281
            let mut first = measureOpening(alts[0], width);
282
            for i in 1 .. alts.len {
283
                let span = measureOpening(alts[i], width);
284
                if breaksBefore(span, first) {
285
                    set first = span;
286
                }
287
            }
288
            return first;
289
        }
290
        case super::Item::Broken(inner) => return measureOpening(inner, width),
291
        else                            => return measureLayout(layout, width),
292
    }
293
}
294
295
/// Whether the span `a` leaves more room on the line than `b`: it ends at a
296
/// line break where `b` does not, `b` cannot fit, or it ends earlier in the
297
/// same way.
298
fn breaksBefore(a: Span, b: Span) -> bool {
299
    if a.status == b.status {
300
        return a.end < b.end;
301
    }
302
    return a.status == Status::Closed or b.status == Status::Bad;
303
}
304
305
/// Measure the children from index `from` on, up to the first line break,
306
/// in a box with the given mode. Nested boxes are measured flat when `nested`
307
/// is flat, or up to their earliest possible line break otherwise.
308
fn measureChildren(
309
    children: *[super::Child],
310
    from: u32,
311
    mode: Mode,
312
    nested: Mode,
313
    width: u32
314
) -> Span {
315
    let mut span = emptySpan();
316
    for i in from .. children.len {
317
        let child = children[i];
318
        if mode == Mode::Flat {
319
            if let case super::Item::BrokenText { .. } = child.layout.item {
320
                continue;
321
            }
322
        }
323
        // A child on a new line ends the line before it, so it is not measured.
324
        let gap = inlineGap(child.placement, mode) else {
325
            set span.status = Status::Closed;
326
            return span;
327
        };
328
        let mut part = emptySpan();
329
        if let case super::Item::BrokenText { text, role } = child.layout.item {
330
            set part = measureText(text, role, width);
331
        } else if nested == Mode::Flat {
332
            set part = measureLayout(child.layout, width);
333
        } else {
334
            set part = measureOpening(child.layout, width);
335
        }
336
        set span = join(span, gapWidth(gap), part, width);
337
        if span.status <> Status::Open {
338
            return span;
339
        }
340
    }
341
    return span;
342
}
343
344
/// Check whether a layout fits when flat at `col`, given the span `rest` of
345
/// the content that follows it.
346
fn fits(
347
    layout: *super::Layout, width: u32, col: u32, tail: Tail, rest: Span
348
) -> bool {
349
    let start = Span { reach: col, end: col, tail, status: Status::Open };
350
    let mut span = join(start, 0, measureLayout(layout, width), width);
351
    if span.status == Status::Open {
352
        set span = join(span, 0, rest, width);
353
    }
354
    return span.status <> Status::Bad;
355
}
356
357
/// Indentation level of a child that starts a new line whatever the mode of
358
/// its box, if it does.
359
fn lineLevel(placement: super::Placement, indent: u32) -> ?u32 {
360
    match placement {
361
        case super::Placement::Line   => return indent,
362
        case super::Placement::Indent => return indent + 1,
363
        else                          => return nil,
364
    }
365
}
366
367
/// Check whether the alternative `alt` of a choice fits at the current
368
/// column, in a box at indentation `indent`. An alternative that starts on a
369
/// new line is measured from the start of that line.
370
fn fitsAlternative(
371
    alt: *super::Layout, st: &State, indent: u32, rest: Span
372
) -> bool {
373
    let width = st.options.width;
374
    if let case super::Item::Box(children) = alt.item; children.len > 0 {
375
        if let level = lineLevel(children[0].placement, indent) {
376
            let mut after = measureChildren(
377
                children, 1, Mode::Flat, Mode::Flat, width
378
            );
379
            if after.status == Status::Open {
380
                set after = join(after, 0, rest, width);
381
            }
382
            let col = level * st.options.indent;
383
            return fits(children[0].layout, width, col, Tail::Code, after);
384
        }
385
    }
386
    return fits(alt, width, st.col, st.tail, rest);
387
}
388
389
/// Start a new line at the given indentation level.
390
fn newline(st: &mut State, level: u32) {
391
    set st.newlines += 1;
392
    set st.spaces = 0;
393
    set st.col = level * st.options.indent;
394
    set st.tail = Tail::Code;
395
}
396
397
/// Write text, preceded by the pending newlines, indentation and spaces.
398
fn writeText(
399
    st: &mut State,
400
    out: &mut opaque sexpr::Output,
401
    text: *[u8],
402
    role: super::Role
403
) {
404
    if text.len == 0 {
405
        return;
406
    }
407
    if st.newlines > 0 or st.written == 0 {
408
        if st.written > 0 {
409
            for _ in 0 .. st.newlines {
410
                out.write("\n");
411
            }
412
        }
413
        for _ in 0 .. st.col {
414
            out.write(" ");
415
        }
416
        set st.newlines = 0;
417
    } else {
418
        for _ in 0 .. st.spaces {
419
            out.write(" ");
420
        }
421
    }
422
    out.write(text);
423
    set st.spaces = 0;
424
    set st.col += text.len;
425
    set st.written += text.len;
426
    set st.tail = Tail::Comment
427
        if role == super::Role::Comment
428
        else Tail::Code;
429
}
430
431
/// Apply the placement of a child in a box at indentation `indent`.
432
/// Returns the indentation level of the child.
433
fn place(
434
    st: &mut State,
435
    placement: super::Placement,
436
    mode: Mode,
437
    indent: u32
438
) -> u32 {
439
    if let gap = inlineGap(placement, mode) {
440
        if st.tail == Tail::Comment {
441
            newline(st, indent);
442
        } else if st.newlines == 0 and st.written > 0 {
443
            set st.spaces += gapWidth(gap);
444
            set st.col += gapWidth(gap);
445
        }
446
        return indent;
447
    }
448
    match placement {
449
        case super::Placement::Indent, super::Placement::SoftIndent(_) => {
450
            newline(st, indent + 1);
451
            return indent + 1;
452
        }
453
        else => {
454
            newline(st, indent);
455
            return indent;
456
        }
457
    }
458
}
459
460
/// Render a layout whose box indentation is `indent`. `rest` describes the
461
/// content that follows the layout. `inFlat` is set if the layout is in a plain
462
/// box that is flat, so a box in it is flat without a measure, and `rest` is
463
/// not used.
464
fn renderLayout(
465
    st: &mut State,
466
    out: &mut opaque sexpr::Output,
467
    layout: *super::Layout,
468
    indent: u32,
469
    rest: Span,
470
    inFlat: bool
471
) {
472
    match layout.item {
473
        case super::Item::Text { text, role }                   => writeText(st, out, text, role),
474
        case super::Item::Blank, super::Item::BrokenText { .. } => {}
475
        case super::Item::Box(children) => {
476
            let flat = inFlat
477
                or fits(layout, st.options.width, st.col, st.tail, rest);
478
            let mode = Mode::Flat if flat else Mode::Broken;
479
            renderChildren(
480
                st, out, children, mode, indent, rest, flat and layout.plain
481
            );
482
        }
483
        case super::Item::Broken(inner) =>
484
            renderChildren(
485
                st, out, brokenChildren(inner), Mode::Broken, indent, rest, false
486
            ),
487
        case super::Item::Tab(column) => {
488
            let pad = st.pads[column];
489
            set st.pads[column] = 0;
490
            set st.spaces += pad;
491
            set st.col += pad;
492
        }
493
        case super::Item::Choice(alts) => {
494
            let mut pick = alts[alts.len - 1];
495
            for alt in &alts[.. alts.len - 1] {
496
                if fitsAlternative(alt, st, indent, rest) {
497
                    set pick = alt;
498
                    break;
499
                }
500
            }
501
            renderLayout(st, out, pick, indent, rest, false);
502
        }
503
    }
504
}
505
506
/// Indentation level of a child that starts a line in a box with the given
507
/// mode and indentation, if it does.
508
fn rowLevel(placement: super::Placement, mode: Mode, indent: u32) -> ?u32 {
509
    match placement {
510
        case super::Placement::Line   => return indent,
511
        case super::Placement::Indent => return indent + 1,
512
        case super::Placement::SoftLine(_) => {
513
            if mode == Mode::Broken {
514
                return indent;
515
            }
516
            return nil;
517
        }
518
        case super::Placement::SoftIndent(_) => {
519
            if mode == Mode::Broken {
520
                return indent + 1;
521
            }
522
            return nil;
523
        }
524
        else => return nil,
525
    }
526
}
527
528
/// Indentation level of the child `at` if it starts a line: by its placement,
529
/// or because it is the first child of a box that starts at the start of a
530
/// line, as the items of a broken list do.
531
fn rowStart(
532
    st: &State, children: *[super::Child], at: u32, mode: Mode, indent: u32
533
) -> ?u32 {
534
    if let level = rowLevel(children[at].placement, mode, indent) {
535
        return level;
536
    }
537
    if at == 0 and st.newlines > 0 and st.options.indent > 0 {
538
        return st.col / st.options.indent;
539
    }
540
    return nil;
541
}
542
543
/// Index of the child after the row that starts at the child `from`: the
544
/// first child that starts a line.
545
fn rowEnd(
546
    children: *[super::Child], from: u32, mode: Mode, indent: u32
547
) -> u32 {
548
    let mut end = from + 1;
549
    while end < children.len
550
        and rowLevel(children[end].placement, mode, indent) == nil
551
    {
552
        set end += 1;
553
    }
554
    return end;
555
}
556
557
/// Whether a layout holds the tab stop of `column`.
558
fn hasTab(layout: *super::Layout, column: u32) -> bool {
559
    return (layout.tabs & (1 << column)) <> 0;
560
}
561
562
/// Whether the row `from..end` has the tab stop of `column`.
563
fn rowHasTab(
564
    children: *[super::Child], from: u32, end: u32, column: u32
565
) -> bool {
566
    for k in from .. end {
567
        if hasTab(children[k].layout, column) {
568
            return true;
569
        }
570
    }
571
    return false;
572
}
573
574
/// Measure the row `from..end` flat, up to its first line break.
575
fn measureRow(
576
    children: *[super::Child], from: u32, end: u32, mode: Mode, width: u32
577
) -> Span {
578
    let mut span = emptySpan();
579
    for k in from .. end {
580
        let child = children[k];
581
        let mut gap: u32 = 0;
582
        if k > from {
583
            if let g = inlineGap(child.placement, mode) {
584
                set gap = gapWidth(g);
585
            }
586
        }
587
        let mut part = emptySpan();
588
        if let case super::Item::BrokenText { text, role } = child.layout.item {
589
            if mode == Mode::Flat {
590
                continue;
591
            }
592
            set part = measureText(text, role, width);
593
        } else {
594
            set part = measureLayout(child.layout, width);
595
        }
596
        set span = join(span, gap, part, width);
597
        if span.status <> Status::Open {
598
            return span;
599
        }
600
    }
601
    return span;
602
}
603
604
/// Measure the content of the layouts `children` before the tab stop of
605
/// `column`, flat, or nil if there is no such stop. The first child has a gap
606
/// before it only if `gapFirst`.
607
fn measureToTab(
608
    children: *[super::Child],
609
    from: u32,
610
    end: u32,
611
    gapFirst: bool,
612
    mode: Mode,
613
    column: u32,
614
    width: u32
615
) -> ?Span {
616
    let mut span = emptySpan();
617
    for k in from .. end {
618
        let child = children[k];
619
        let mut gap: u32 = 0;
620
        if k > from or gapFirst {
621
            if let g = inlineGap(child.placement, mode) {
622
                set gap = gapWidth(g);
623
            }
624
        }
625
        if let case super::Item::Tab(found) = child.layout.item {
626
            if found == column {
627
                return span;
628
            }
629
        }
630
        if let case super::Item::BrokenText { text, role } = child.layout.item {
631
            if mode == Mode::Broken {
632
                let part = measureText(text, role, width);
633
                set span = join(span, gap, part, width);
634
            }
635
            continue;
636
        }
637
        if let case super::Item::Box(inner) = child.layout.item; hasTab(child.layout, column) {
638
            let before = measureToTab(
639
                inner, 0, inner.len, true, Mode::Flat, column, width
640
            );
641
            if let found = before {
642
                return join(span, gap, found, width);
643
            }
644
        }
645
        set span = join(span, gap, measureLayout(child.layout, width), width);
646
    }
647
    return nil;
648
}
649
650
/// Width of the row `from..end` before its tab stop of `column`, with the
651
/// padding at the stops of lower columns. Zero if the row has no such stop.
652
fn stopWidth(
653
    children: *[super::Child],
654
    from: u32,
655
    end: u32,
656
    mode: Mode,
657
    column: u32,
658
    runs: &[Run],
659
    at: u32,
660
    width: u32
661
) -> u32 {
662
    let before = measureToTab(
663
        children, from, end, false, mode, column, width
664
    ) else {
665
        return 0;
666
    };
667
    let mut total = before.end;
668
    for lower in 0 .. column {
669
        set total += stopPad(children, from, end, mode, lower, runs, at, width);
670
    }
671
    return total;
672
}
673
674
/// Spaces that the row `from..end` adds at its tab stop of `column`, given the
675
/// run of the column, where the row is the child `at`.
676
fn stopPad(
677
    children: *[super::Child],
678
    from: u32,
679
    end: u32,
680
    mode: Mode,
681
    column: u32,
682
    runs: &[Run],
683
    at: u32,
684
    width: u32
685
) -> u32 {
686
    let run = runs[column];
687
    if not run.aligned or at >= run.end {
688
        return 0;
689
    }
690
    if not rowHasTab(children, from, end, column) {
691
        return 0;
692
    }
693
    return run.target
694
        - stopWidth(children, from, end, mode, column, runs, at, width);
695
}
696
697
/// Find the run of rows that starts at the child `from`, for the tab stops of
698
/// `column`. A child that is not a row with such a stop, or does not fit on
699
/// one line, is a run of its own that is not aligned.
700
fn scanRun(
701
    st: &State,
702
    children: *[super::Child],
703
    from: u32,
704
    mode: Mode,
705
    indent: u32,
706
    column: u32,
707
    runs: &[Run]
708
) -> Run {
709
    let alone = Run { end: from + 1, target: 0, aligned: false };
710
    let level = rowStart(st, children, from, mode, indent) else {
711
        return alone;
712
    };
713
    let width = st.options.width;
714
    let start = level * st.options.indent;
715
    let mut j = from;
716
    let mut target: u32 = 0;
717
    let mut after: u32 = 0;
718
    while j < children.len {
719
        if j > from {
720
            let here = rowLevel(children[j].placement, mode, indent) else {
721
                break;
722
            };
723
            if here <> level {
724
                break;
725
            }
726
        }
727
        let end = rowEnd(children, j, mode, indent);
728
        if not rowHasTab(children, j, end, column) {
729
            break;
730
        }
731
        let span = measureRow(children, j, end, mode, width);
732
        // The row fits on the line where it is.
733
        if span.status <> Status::Open or start + span.reach > width {
734
            break;
735
        }
736
        let w = stopWidth(children, j, end, mode, column, runs, j, width);
737
        let own = measureToTab(children, j, end, false, mode, column, width);
738
        let mut before = w;
739
        if let found = own {
740
            set before = found.end;
741
        }
742
        // The padding of lower columns lengthens the row.
743
        let reach = span.reach + w - before;
744
        if w > target {
745
            set target = w;
746
        }
747
        if reach > w and reach - w > after {
748
            set after = reach - w;
749
        }
750
        set j = end;
751
    }
752
    if j == from {
753
        return alone;
754
    }
755
    return Run {
756
        end: j,
757
        target,
758
        aligned: start + target + 1 < width and start + target + after <= width,
759
    };
760
}
761
762
/// Set the spaces that the tab stops of the row that starts at the child `at`
763
/// add, finding the runs that it starts.
764
fn padRow(
765
    st: &mut State,
766
    children: *[super::Child],
767
    at: u32,
768
    mode: Mode,
769
    indent: u32,
770
    runs: &mut [Run]
771
) {
772
    if rowStart(st, children, at, mode, indent) == nil {
773
        return;
774
    }
775
    let end = rowEnd(children, at, mode, indent);
776
    for column in 0 .. super::CELL_COLUMNS {
777
        if not rowHasTab(children, at, end, column) {
778
            continue;
779
        }
780
        if at >= runs[column].end {
781
            set runs[column] = scanRun(
782
                st, children, at, mode, indent, column, runs
783
            );
784
        }
785
        // A box that starts a row finds the pad of the row it is in.
786
        let pad = stopPad(
787
            children, at, end, mode, column, runs, at, st.options.width
788
        );
789
        if pad > 0 {
790
            set st.pads[column] = pad;
791
        }
792
    }
793
}
794
795
/// Render the children of a box with the given mode.
796
fn renderChildren(
797
    st: &mut State,
798
    out: &mut opaque sexpr::Output,
799
    children: *[super::Child],
800
    mode: Mode,
801
    indent: u32,
802
    rest: Span,
803
    inFlat: bool
804
) {
805
    let mut runs =
806
        [
807
            Run { end: 0, target: 0, aligned: false }; super::CELL_COLUMNS
808
        ];
809
    for i in 0 .. children.len {
810
        let child = children[i];
811
        match child.layout.item {
812
            case super::Item::BrokenText { text, role } => {
813
                if mode == Mode::Broken {
814
                    place(st, child.placement, mode, indent);
815
                    writeText(st, out, text, role);
816
                }
817
            }
818
            case super::Item::Text { .. }, super::Item::Blank => {
819
                let level = place(st, child.placement, mode, indent);
820
                padRow(st, children, i, mode, indent, &mut runs[..]);
821
                renderLayout(st, out, child.layout, level, emptySpan(), inFlat);
822
            }
823
            else => {
824
                let level = place(st, child.placement, mode, indent);
825
                padRow(st, children, i, mode, indent, &mut runs[..]);
826
                let mut next = emptySpan();
827
                if not inFlat {
828
                    set next = measureChildren(
829
                        children, i + 1, mode, Mode::Broken, st.options.width
830
                    );
831
                    if next.status == Status::Open {
832
                        set next = join(next, 0, rest, st.options.width);
833
                    }
834
                }
835
                renderLayout(st, out, child.layout, level, next, inFlat);
836
            }
837
        }
838
    }
839
}