f-office-docx 0.3.0

DOCX model and Word's layout rules.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
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
589
590
591
592
593
594
595
596
//! The scripts after a base of an equation (`m:sSub`, `m:sSup` and `m:sSubSup`): once a script
//! is measured, it starts where the corners of the base's last glyph and of its own first glyph
//! put it, the two scripts of one base move apart when their inks come near each other, and the
//! scripts leave a space after them. The limits of an n-ary operator inside a line stand beside
//! it as scripts do, moved by the operator's ink or apart.

use std::ops::Range;

use f_office_units::Length;

use super::gap_minimums;
use super::structures::ending_glyph;
use crate::body::{BarSide, ScriptMark, ScriptSide};
use crate::grid::Device;
use crate::lines::Piece;
use crate::params;
use crate::props::RunProps;
use crate::text::Shaper;

/// The scripts of a base whose end is still to come: the index of the base's last glyph among a
/// paragraph's pieces, if the base ends in a glyph; the italic correction that the base holds as
/// its own when it ends in an overbar; whether the scripts are the parts of a
/// stack; whether a glyph of the same equation follows them; and the side of the script being
/// measured, with the index of its first piece; the pieces of the subscript of a stack once
/// it is measured; the pieces of the base when it is a box, and whether it is one only by its
/// delimiters; and, for the limits of an n-ary operator, whether they take the drops from the
/// operator's ink.
pub(in crate::layout) struct OpenScripts {
    base: Option<usize>,
    held: Length,
    stacked: bool,
    followed: bool,
    script: Option<(ScriptSide, usize)>,
    subscript: Option<Range<usize>>,
    base_box: Option<(Range<usize>, bool)>,
    limits: Option<Drops>,
}

impl OpenScripts {
    /// The scripts of a base that ends in `base`, before the first of them.
    fn new(base: Option<usize>, stacked: bool, followed: bool) -> Self {
        Self {
            base,
            held: Length::ZERO,
            stacked,
            followed,
            script: None,
            subscript: None,
            base_box: None,
            limits: None,
        }
    }
}

/// Whether the limits of an n-ary operator inside a line take the drops from the operator's
/// ink, as an integral's do, or move apart as the two scripts of a base do.
#[derive(Clone, Copy)]
enum Drops {
    Ink,
    Apart,
}

/// Places a mark of the scripts of a base in a paragraph's `pieces`, with the properties
/// `props` of its run, at the size of the base. At a script's end, its last glyph takes its
/// italic correction when a glyph of the same equation follows the scripts, and the space
/// between the end of the base's last glyph and the script's start ([`Shaper::script_lead`]) goes into the
/// advance of that glyph, or, in a stack, into a shift at the start of the script's part, as
/// each part of the stack starts at its own place. At the end of the scripts, the math font's
/// space after scripts ([`Shaper::space_after_script`]) goes into the advance of the last glyph,
/// or into the shift that ends a stack. At the end of the superscript of a stack, the two
/// scripts move apart ([`keep_apart`]). Each script of a base that is a box keeps the math
/// font's drops from it ([`keep_from_box`]). A base that ends in a bar structure, at the shift
/// `ended` with the side of its bar, readies its scripts by that bar ([`on_bar`]).
/// The limits of an n-ary operator take neither the
/// corners nor the space after them: at its end, each limit of an integral moves away from
/// the operator's ink ([`keep_from_ink`]), and the two limits of another operator move apart.
pub(in crate::layout) fn mark(
    (shaper, device): (&mut Shaper, Device),
    (open, pieces): (&mut Vec<OpenScripts>, &mut Vec<Piece>),
    (mark, ended): (ScriptMark, Option<(usize, BarSide)>),
    props: &RunProps,
) {
    match mark {
        ScriptMark::BoxStart { delimited } => {
            let mut scripts = OpenScripts::new(None, false, false);
            scripts.base_box = Some((pieces.len()..pieces.len(), delimited));
            open.push(scripts);
        }
        ScriptMark::Base {
            stacked,
            followed,
            boxed,
        } => {
            end_base(shaper, (open, pieces), (stacked, followed, boxed), props);
            if let (Some(scripts), Some(ended)) = (open.last_mut(), ended) {
                on_bar(pieces, scripts, ended);
            }
        }
        ScriptMark::Operator { stacked, drops } => {
            let mut scripts = OpenScripts::new(ending_glyph(pieces), stacked, false);
            scripts.limits = Some(if drops { Drops::Ink } else { Drops::Apart });
            open.push(scripts);
        }
        ScriptMark::Start(side) => {
            if let Some(scripts) = open.last_mut() {
                scripts.script = Some((side, pieces.len()));
                if scripts.stacked && scripts.limits.is_none() {
                    pieces.push(Piece::Shift(Length::ZERO));
                }
            }
        }
        ScriptMark::End => {
            if let Some(scripts) = open.last_mut() {
                end((shaper, device), pieces, scripts, props);
            }
        }
        ScriptMark::After => after(shaper, (open, pieces), props),
    }
}

/// Closes the scripts of a base: unless they are the limits of an operator, the math font's
/// space after scripts goes into the advance of the last glyph, or into the shift that ends a
/// stack.
fn after(
    shaper: &mut Shaper,
    (open, pieces): (&mut Vec<OpenScripts>, &mut Vec<Piece>),
    props: &RunProps,
) {
    if open.pop().is_some_and(|scripts| scripts.limits.is_none())
        && let Some(face) = shaper.face(props, true)
    {
        let space = shaper.space_after_script(face);
        if let Some(Piece::Glyph(glyph)) = ending_glyph(pieces).and_then(|at| pieces.get_mut(at)) {
            shaper.widen_glyph(glyph, space);
        } else {
            add_space(pieces, space);
        }
    }
}

/// Opens the scripts of a base whose end has come, the last of `pieces`, which are `stacked`
/// and `followed`. A base that may be a box (`boxed`) ends the scripts that its start opened,
/// whose box then runs to the base's end; a base that ends in delimiters is a box only when its
/// last glyph, the closing delimiter, draws a grown glyph ([`Shaper::draws_grown_glyph`]). A
/// base whose last glyph stands on the structure's baseline at another size than the structure's
/// control size (`props`) is also a box around that glyph.
fn end_base(
    shaper: &mut Shaper,
    (open, pieces): (&mut Vec<OpenScripts>, &[Piece]),
    (stacked, followed, boxed): (bool, bool, bool),
    props: &RunProps,
) {
    let base = ending_glyph(pieces);
    let grown = match base.and_then(|at| pieces.get(at)) {
        Some(Piece::Glyph(glyph)) => shaper.draws_grown_glyph(glyph),
        _ => false,
    };
    let sized = sized_base_box(shaper, pieces, base, props);
    if let Some(scripts) = open.last_mut().filter(|_| boxed) {
        *scripts = OpenScripts {
            base_box: scripts
                .base_box
                .as_ref()
                .filter(|(_, delimited)| !delimited || grown)
                .map(|(r, delimited)| (r.start..pieces.len(), *delimited))
                .or(sized),
            ..OpenScripts::new(base, stacked, followed)
        };
    } else {
        let mut scripts = OpenScripts::new(base, stacked, followed);
        scripts.base_box = sized;
        open.push(scripts);
    }
}

/// The single-glyph box of a script base's last glyph when it stands on the structure's baseline
/// at a different size than the structure's control face (`props`).
fn sized_base_box(
    shaper: &mut Shaper,
    pieces: &[Piece],
    base: Option<usize>,
    props: &RunProps,
) -> Option<(Range<usize>, bool)> {
    let at = base?;
    let Some(Piece::Glyph(glyph)) = pieces.get(at) else {
        return None;
    };
    let (glyph_rise, glyph_size) = shaper
        .faces()
        .get(glyph.face)
        .map(|face| (face.rise, face.size))?;
    let ctrl_face = shaper
        .face(props, true)
        .and_then(|face| shaper.faces().get(face))?;
    (glyph_rise == ctrl_face.rise && glyph_size != ctrl_face.size).then_some((at..at + 1, false))
}

/// Ends the script whose end has come ([`end_script`]), drops the subscript of a base without a
/// superscript below the font's top limit ([`keep_below`]), and keeps the pieces of a stack's
/// subscript until the stack's superscript ends, when the two move apart ([`keep_apart`]).
fn end(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    scripts: &mut OpenScripts,
    props: &RunProps,
) {
    let script = scripts.script;
    match scripts.limits {
        Some(Drops::Ink) => {
            if let Some((side, start)) = scripts.script.take() {
                keep_from_ink((shaper, device), pieces, scripts.base, (side, start));
            }
            return;
        }
        Some(Drops::Apart) => scripts.script = None,
        None => end_attached((shaper, device), pieces, scripts, props),
    }
    match script {
        Some((ScriptSide::Sub, start)) if scripts.stacked => {
            scripts.subscript = Some(start..pieces.len());
        }
        Some((ScriptSide::Sub, start)) if scripts.limits.is_none() => {
            let moved = [(ScriptSide::Sub, start..pieces.len())];
            kerned_where_they_end((shaper, device), pieces, (scripts, &moved), |by, pieces| {
                keep_below(by, pieces, start..pieces.len(), props);
            });
        }
        Some((ScriptSide::Sup, start)) => {
            end_superscript((shaper, device), pieces, (scripts, start), props);
        }
        _ => {}
    }
}

/// Ends a superscript whose first piece is at `start`: the two scripts of a stack move apart
/// ([`keep_apart`]), a lone superscript rises ([`keep_above`]), and each script of a base that
/// is not an operator takes its corner kerns where it ends ([`kerned_where_they_end`]).
fn end_superscript(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    (scripts, start): (&mut OpenScripts, usize),
    props: &RunProps,
) {
    let superscript = start..pieces.len();
    let attached = scripts.limits.is_none();
    if let Some(subscript) = scripts.subscript.take() {
        let both = [
            (ScriptSide::Sub, subscript.clone()),
            (ScriptSide::Sup, superscript.clone()),
        ];
        let moved = if attached { &both[..] } else { &[] };
        kerned_where_they_end((shaper, device), pieces, (scripts, moved), |by, pieces| {
            keep_apart(by, pieces, (subscript, superscript), props);
        });
    } else if !scripts.stacked && attached {
        let moved = [(ScriptSide::Sup, superscript.clone())];
        kerned_where_they_end((shaper, device), pieces, (scripts, &moved), |by, pieces| {
            keep_above(by, pieces, superscript, props);
        });
    }
}

/// Ends the script of an attached base whose end has come ([`end_script`]) and, when the base is
/// a box, keeps the script's drop from the box ([`keep_from_box`]).
fn end_attached(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    scripts: &mut OpenScripts,
    props: &RunProps,
) {
    let script = scripts.script;
    end_script((shaper, device), pieces, scripts);
    if let (Some(script), Some((base, _))) = (script, scripts.base_box.clone()) {
        keep_from_box((shaper, device), pieces, (base, script), props);
    }
}

/// Ends the script whose end has come: its last glyph takes its italic correction when a glyph
/// of the same equation follows the scripts, and the script starts by the base's last glyph and
/// the script's first glyph.
fn end_script(
    (shaper, device): (&Shaper, Device),
    pieces: &mut [Piece],
    scripts: &mut OpenScripts,
) {
    let Some((side, start)) = scripts.script.take() else {
        return;
    };
    if scripts.followed {
        let last = pieces
            .get(start..)
            .and_then(ending_glyph)
            .map(|at| start + at);
        if let Some(Piece::Glyph(glyph)) = last.and_then(|at| pieces.get_mut(at)) {
            let italic = shaper
                .glyph_italic_correction(glyph)
                .unwrap_or(Length::ZERO);
            shaper.widen_glyph(glyph, italic);
        }
    }
    let script = (side, start..pieces.len());
    let Some(lead) = lead_of((shaper, device), pieces, scripts, script) else {
        give_back(pieces, scripts, (side, start));
        return;
    };
    add_lead(shaper, pieces, scripts, (start, lead));
}

/// The space between the end of the base's last glyph and the start of the script on `side`
/// whose pieces are `script` ([`Shaper::script_lead`]), by the ink of the whole script at its
/// place; nothing when the base or the script has no glyph.
fn lead_of(
    (shaper, device): (&Shaper, Device),
    pieces: &[Piece],
    scripts: &OpenScripts,
    (side, script): (ScriptSide, Range<usize>),
) -> Option<Length> {
    let script = pieces.get(script)?;
    let first = script.iter().find_map(|piece| match piece {
        Piece::Glyph(glyph) if !glyph.part => Some(*glyph),
        _ => None,
    })?;
    let base = scripts.base.and_then(|at| match pieces.get(at) {
        Some(Piece::Glyph(glyph)) => Some(*glyph),
        _ => None,
    })?;
    let rise = device.shift_dots(shaper.faces().get(first.face)?.rise);
    let ink =
        gap_minimums::ink(shaper, &device, script).map(|(low, high)| (low - rise, high - rise));
    Some(shaper.script_lead(&base, (&first, ink), side))
}

/// Moves scripts by `step`, such as the two scripts of a base that move apart, and then starts
/// each script of `moved`, its side and the range of its pieces, by its corner kerns where
/// it ends: the lead that [`end_script`] gave it changes by as much as the lead at its new height
/// differs from the lead at its old one.
fn kerned_where_they_end<'a>(
    (shaper, device): (&mut Shaper<'a>, Device),
    pieces: &mut [Piece],
    (scripts, moved): (&OpenScripts, &[(ScriptSide, Range<usize>)]),
    step: impl FnOnce((&mut Shaper<'a>, Device), &mut [Piece]),
) {
    let before: Vec<Option<Length>> = moved
        .iter()
        .map(|script| lead_of((shaper, device), pieces, scripts, script.clone()))
        .collect();
    step((shaper, device), pieces);
    for (script, before) in moved.iter().zip(before) {
        let after = lead_of((shaper, device), pieces, scripts, script.clone());
        if let Some(change) = after.zip(before).map(|(after, before)| after - before) {
            add_lead(shaper, pieces, scripts, (script.1.start, change));
        }
    }
}

/// Adds `change` to the lead of the script whose first piece is at `start`: in a stack to the
/// shift that starts the script's part, else to the advance of the base's last glyph.
fn add_lead(
    shaper: &Shaper,
    pieces: &mut [Piece],
    scripts: &OpenScripts,
    (start, change): (usize, Length),
) {
    let at = if scripts.stacked {
        Some(start)
    } else {
        scripts.base
    };
    match at.and_then(|at| pieces.get_mut(at)) {
        Some(Piece::Shift(shift)) => *shift += change,
        Some(Piece::Glyph(glyph)) => shaper.widen_glyph(glyph, change),
        _ => {}
    }
}

/// Readies the scripts of a base that ends in a bar structure, whose end is the shift at `at`
/// and whose bar is on `side`. Past an overbar the scripts start at the bar's end, and the
/// italic correction that the shift holds is the structure's own, which a subscript gives back
/// ([`give_back`]). An underbar leaves its base's last glyph to the scripts, which start from
/// the end of that glyph's advance, as on the glyph alone.
fn on_bar(pieces: &mut [Piece], scripts: &mut OpenScripts, (at, side): (usize, BarSide)) {
    let Some(Piece::Shift(held)) = pieces.get_mut(at) else {
        return;
    };
    match side {
        BarSide::Top => scripts.held = *held,
        BarSide::Bottom => {
            *held = Length::ZERO;
            scripts.base = pieces.get(..at).and_then(ending_glyph);
        }
    }
}

/// Starts a subscript, whose first piece is at `start`, on a base that ends in an overbar back
/// by the italic correction that the bar holds past the base's advance: a superscript and the
/// glyph after the structure take it, and a subscript does not. A lone subscript moves by the
/// shift that ends the bar, one of a stack by the shift that starts its part.
fn give_back(pieces: &mut [Piece], scripts: &OpenScripts, (side, start): (ScriptSide, usize)) {
    if !matches!(side, ScriptSide::Sub) {
        return;
    }
    let at = if scripts.stacked {
        Some(start)
    } else {
        start.checked_sub(1)
    };
    if let Some(Piece::Shift(shift)) = at.and_then(|at| pieces.get_mut(at)) {
        *shift -= scripts.held;
    }
}

/// Moves the subscript `subscript` and the superscript `superscript` of one base, ranges of
/// `pieces`, apart when the gap from the bottom of the superscript's ink down to the top of the
/// subscript's ink is less than the math font's `SubSuperscriptGapMin`, at the size of the base
/// of the run `props`: the subscript drops by half of the shortfall, and the superscript rises
/// by half of it, or until the bottom of its ink lies `SuperscriptBottomMaxWithSubscript` above
/// the base's baseline when that is more. Each moves to the nearest dot.
fn keep_apart(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    (subscript, superscript): (Range<usize>, Range<usize>),
    props: &RunProps,
) {
    let Some(base) = shaper
        .face(props, true)
        .and_then(|face| shaper.faces().get(face))
    else {
        return;
    };
    let at = |(units, em): (i64, i64)| (base.size * units).div_round(em);
    let (minimum, bottom_max) = (
        at(params::MATH_SUB_SUPERSCRIPT_GAP_MIN),
        at(params::MATH_SUPERSCRIPT_BOTTOM_MAX_WITH_SUBSCRIPT),
    );
    let baseline = device.shift_dots(base.rise);
    let part = |range: &Range<usize>| pieces.get(range.clone()).unwrap_or(&[]);
    let (Some((_, top)), Some((bottom, _))) = (
        gap_minimums::ink(shaper, &device, part(&subscript)),
        gap_minimums::ink(shaper, &device, part(&superscript)),
    ) else {
        return;
    };
    let short = minimum - (bottom - top);
    if short <= Length::ZERO {
        return;
    }
    let half = short.div_toward_zero(2);
    let rise = half.max(bottom_max - (bottom - baseline));
    if let Some(part) = pieces.get_mut(subscript) {
        gap_minimums::move_part((shaper, device), part, -half);
    }
    if let Some(part) = pieces.get_mut(superscript) {
        gap_minimums::move_part((shaper, device), part, rise);
    }
}

/// Raises the superscript `superscript` of a base without a subscript, a range of `pieces`, until
/// the bottom of its ink lies the math font's `SuperscriptBottomMin` above the base's baseline,
/// at the size of the base of the run `props`, to the nearest dot. A superscript whose ink lies
/// higher keeps its place.
fn keep_above(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    superscript: Range<usize>,
    props: &RunProps,
) {
    let Some(base) = shaper
        .face(props, true)
        .and_then(|face| shaper.faces().get(face))
    else {
        return;
    };
    let (units, em) = params::MATH_SUPERSCRIPT_BOTTOM_MIN;
    let bottom_min = (base.size * units).div_round(em);
    let baseline = device.shift_dots(base.rise);
    let Some(part) = pieces.get_mut(superscript) else {
        return;
    };
    let Some((bottom, _)) = gap_minimums::ink(shaper, &device, part) else {
        return;
    };
    let rise = bottom_min - (bottom - baseline);
    if rise > Length::ZERO {
        gap_minimums::move_part((shaper, device), part, rise);
    }
}

/// Drops the subscript `subscript` of a base without a superscript, a range of `pieces`, until
/// the top of its ink lies
/// no more than the math font's `SubscriptTopMax` above the base's baseline, at the size of the
/// base of the run `props`, to the nearest dot. A subscript whose ink lies lower keeps its place.
fn keep_below(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    subscript: Range<usize>,
    props: &RunProps,
) {
    let Some(base) = shaper
        .face(props, true)
        .and_then(|face| shaper.faces().get(face))
    else {
        return;
    };
    let (units, em) = params::MATH_SUBSCRIPT_TOP_MAX;
    let top_max = (base.size * units).div_round(em);
    let baseline = device.shift_dots(base.rise);
    let Some(part) = pieces.get_mut(subscript) else {
        return;
    };
    let Some((_, top)) = gap_minimums::ink(shaper, &device, part) else {
        return;
    };
    let drop = top_max - (top - baseline);
    if drop < Length::ZERO {
        gap_minimums::move_part((shaper, device), part, drop);
    }
}

/// Moves a limit of an integral inside a line, the pieces of `pieces` from `start` on, on the
/// side `side`, away from the ink of the operator's glyph at `operator`, by the drops of the
/// operator's math font at its size ([`Shaper::limit_drops`], [`keep_clear`]).
fn keep_from_ink(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    operator: Option<usize>,
    (side, start): (ScriptSide, usize),
) {
    let Some(Piece::Glyph(glyph)) = operator.and_then(|at| pieces.get(at)) else {
        return;
    };
    let glyph = *glyph;
    let (Some(drops), Some(ink)) = (
        shaper.limit_drops(glyph.face),
        gap_minimums::ink(shaper, &device, std::slice::from_ref(&Piece::Glyph(glyph))),
    ) else {
        return;
    };
    if let Some(part) = pieces.get_mut(start..) {
        keep_clear((shaper, device), part, side, (ink, drops));
    }
}

/// Moves a script of a base that is a box, the pieces of `pieces` from `start` on, on the side
/// `side`, away from the box of the base's pieces `base` ([`gap_minimums::boxed`]), by the
/// drops of the math font at the size of the base of the run `props` ([`keep_clear`]).
fn keep_from_box(
    (shaper, device): (&mut Shaper, Device),
    pieces: &mut [Piece],
    (base, (side, start)): (Range<usize>, (ScriptSide, usize)),
    props: &RunProps,
) {
    let drops = shaper
        .face(props, true)
        .and_then(|face| shaper.limit_drops(face));
    let extent = gap_minimums::boxed(shaper, &device, pieces.get(base).unwrap_or(&[]));
    let (Some(drops), Some(extent)) = (drops, extent) else {
        return;
    };
    if let Some(part) = pieces.get_mut(start..) {
        keep_clear((shaper, device), part, side, (extent, drops));
    }
}

/// Moves the script `part` on the side `side` away from `extent`, the bottom and the top of the
/// ink or the box that it keeps from, by `drops`, the drops of the superscript and of the
/// subscript: the superscript rises until its baseline lies no further below the top than its
/// drop, and the subscript drops until its baseline lies at least its drop below the bottom,
/// each to the nearest dot. A script already that far keeps its place.
fn keep_clear(
    (shaper, device): (&mut Shaper, Device),
    part: &mut [Piece],
    side: ScriptSide,
    ((bottom, top), (upper, lower)): ((Length, Length), (Length, Length)),
) {
    let Some(baseline) = part.iter().find_map(|piece| match piece {
        Piece::Glyph(glyph) if !glyph.part => shaper.faces().get(glyph.face).map(|face| face.rise),
        _ => None,
    }) else {
        return;
    };
    let by = match side {
        ScriptSide::Sup => (top - upper - baseline).max(Length::ZERO),
        ScriptSide::Sub => (bottom - lower - baseline).min(Length::ZERO),
    };
    if by != Length::ZERO {
        gap_minimums::move_part((shaper, device), part, by);
    }
}

/// Adds `space` to the shift that ends the pieces, or after them when they end in another piece.
fn add_space(pieces: &mut Vec<Piece>, space: Length) {
    match pieces.last_mut() {
        Some(Piece::Shift(shift)) => *shift += space,
        _ => pieces.push(Piece::Shift(space)),
    }
}