k2f_idml 0.3.2

K2F lock → IDML exporter
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
mod font;
mod lists;
mod runs;
mod tracking;

use crate::align::{
    autosize_reference, body_lines, has_full_width_lock_line, has_midword_lock_wrap,
    infer_text_align_for, line_gaps, lock_break_char_indices, lock_ink_height,
    should_autosize_width, should_autosize_width_for_nobreak, should_pin_lock_breaks,
    source_glyphs, source_lines,
};
use crate::coord::millipt_to_pt;
use crate::ir::{TextAlign, TextBox, TextRun};
use font::FontCtx;
use k2f_core::{
    GeometryNode, ListMarkerType, Modifier, NodeContent, Rect, SemanticNode, TextGlyphRun,
};
use std::collections::BTreeMap;

pub(crate) use font::FontCtx as TextFonts;
pub(crate) use lists::list_start_at;

pub fn textbox_from_draw(
    node: &SemanticNode,
    rect: &Rect,
    paint_runs: &[TextGlyphRun],
    geo: Option<&GeometryNode>,
    fonts: &BTreeMap<String, Vec<u8>>,
) -> Option<TextBox> {
    textbox_from_draw_ctx(node, rect, paint_runs, geo, &FontCtx::new(fonts), 1, None)
}

pub(crate) fn textbox_from_draw_ctx(
    node: &SemanticNode,
    rect: &Rect,
    paint_runs: &[TextGlyphRun],
    geo: Option<&GeometryNode>,
    fonts: &FontCtx,
    list_start: u32,
    page_tokens: Option<(usize, usize)>,
) -> Option<TextBox> {
    if node.role == "math" || matches!(node.content, NodeContent::Math(_)) {
        return None;
    }
    let raw = k2f_core::node_text(node)?;
    if raw.is_empty() {
        return None;
    }
    let mut runs = runs::runs_from_paint(raw, paint_runs, &node.modifiers, geo, fonts);
    if let Some((total, current)) = page_tokens {
        runs = lists::expand_page_tokens(runs, total, current);
    }
    if runs.is_empty() {
        return None;
    }
    let mut align = geo
        .map(|g| infer_text_align_for(g, raw, &node.modifiers))
        .unwrap_or(TextAlign::Left);
    let font_size = paint_runs
        .iter()
        .map(|r| r.style.font_size)
        .max_by_key(|p| p.0.abs())
        .unwrap_or(k2f_core::Pt(12_000));
    let nlines = geo
        .map(|g| body_lines(g, &node.modifiers).len())
        .unwrap_or(0);
    let numbered = node.marker_type == Some(ListMarkerType::Number);
    let bullet =
        !numbered && (node.role == "list_item" || node.marker_type == Some(ListMarkerType::Bullet));
    let is_list = bullet || numbered;
    if is_list {
        align = TextAlign::Left;
    }
    // Literal markers change first-line width; lock wrap points and NoBreak
    // would overset or double-indent. Lists reflow with hanging indent.
    let pin = !is_list && should_pin_lock_breaks(geo, font_size, Some(raw), align, &node.modifiers);
    if pin {
        if let Some(g) = geo {
            insert_lock_breaks(
                &mut runs,
                &lock_break_char_indices(g, raw, &node.modifiers),
            );
        }
    }
    if bullet {
        lists::prepend_literal_bullet(&mut runs);
    } else if numbered {
        lists::prepend_literal_number(&mut runs, list_start.max(1));
    }
    // Tight 2-line display titles keep NoBreak + NBSP (PPTX wrap=none analog).
    // Column wrap must reflow in InDesign: pinning those lines as `<Br/>` and
    // gluing spaces double-wraps when the host frame is a different width.
    // Spaceless mid-word lock wraps are not column wrap — InDesign cannot
    // break the token, so those frames stay pinned instead of host-reflow.
    let midword = geo.is_some_and(|g| has_midword_lock_wrap(g, raw, &node.modifiers));
    let host_reflow =
        !midword && (nlines >= 3 || (nlines >= 2 && geo.is_some_and(has_full_width_lock_line)));
    let no_break = !is_list && !matches!(align, TextAlign::Justify) && !host_reflow;
    if no_break {
        glue_lock_line_spaces(&mut runs);
    }
    let leading = leading_pt(geo, &node.modifiers);
    for run in &mut runs {
        run.leading_pt = leading;
    }
    let (mut inset_top, mut inset_left, mut inset_bottom, inset_right) =
        insets(geo, align, &node.modifiers);
    let mut first_line_indent_pt = infer_first_line_indent(geo, align, &node.modifiers);
    let mut left_indent_pt = 0.0;
    if is_list {
        // Hang wrap lines by the literal `•\u{00A0}` / `{n}.\u{00A0}` width, not
        // the lock gutter (often 16pt). Otherwise wrap lines indent past line-1 text.
        let marker = if numbered {
            format!("{}.\u{00A0}", list_start.max(1))
        } else {
            "•\u{00A0}".into()
        };
        let size = runs.first().map(|r| r.size_pt).unwrap_or(12.0);
        let family = runs.first().map(|r| r.font_name.as_str()).unwrap_or("");
        let marker_w = lists::literal_marker_width_pt(fonts, family, size, &marker);
        let gutter = lists::list_hanging_pt(geo);
        let hang = if gutter > 0.01 {
            marker_w.min(gutter).max(0.01)
        } else {
            marker_w.max(0.01)
        };
        inset_left = lists::list_outer_pad_pt(geo) + (gutter - hang).max(0.0);
        left_indent_pt = hang;
        first_line_indent_pt = -hang;
    }
    // WidthOnly on wrapping multi-line frames can collapse to a narrow column.
    // NoBreak lines cannot wrap, so WidthOnly grows to the longest lock line
    // instead of oversetting. Still gated on tight ink so wide footers do not
    // shrink and re-anchor. UseNoLineBreaks is single-line only: on 2+ lock
    // lines it sizes the frame as one unwrapped paragraph and ItemTransform-
    // grows through a two-column gutter.
    let autosize_width = if no_break {
        (nlines <= 1 && should_autosize_width(geo, align))
            || should_autosize_width_for_nobreak(geo, align)
    } else {
        nlines <= 1 && should_autosize_width(geo, align)
    };
    let autosize_refer = if no_break && matches!(align, TextAlign::Left) {
        "CenterLeftPoint"
    } else {
        autosize_reference(geo, align)
    };
    let autosize_no_wrap = nlines <= 1;
    // Paint does not clip glyphs whose line origin sits on/past box height.
    // Skip HeightOnly when WidthOnly is on: extra wrap is already prevented.
    // Stacked column body is lock-positioned: HeightOnly shrinks when host
    // wrap uses fewer lines and inflates the gap to the next frame. Author
    // `\n` stacks (titles, affiliations) still HeightOnly so lines are not
    // clipped. Justified copy already stays lock-height.
    let mut autosize_height = nlines >= 2
        && !autosize_width
        && !matches!(align, TextAlign::Justify)
        && (raw.contains('\n') || !host_reflow);
    let mut rect = rect.clone();
    if let Some(ink) = lock_ink_height(geo, font_size) {
        if ink.0 > rect.height.0 {
            rect.height = ink;
        }
    }
    let vert_center = vert_center(geo, &rect, font_size)
        || symmetric_vertical_padding(inset_top, inset_bottom);
    if vert_center {
        inset_top = 0.0;
        inset_bottom = 0.0;
        // HeightOnly shrinks the frame to ink height; CenterAlign needs the
        // full lock rect to vertically center multi-line body in a card.
        autosize_height = false;
    }
    Some(TextBox {
        node_id: node.id.clone(),
        rect,
        runs,
        align,
        inset_top,
        inset_left,
        inset_bottom,
        inset_right,
        first_line_indent_pt,
        left_indent_pt,
        vert_center,
        autosize_width,
        autosize_refer,
        autosize_no_wrap,
        autosize_height,
        no_break,
        semantic_newlines: raw.contains('\n'),
        lock_line_count: nlines,
        // K2F paint puts the first baseline at font ascent when y_offset is 0
        // (`rect.y + ascent`). LeadingOffset would sit at `Leading` instead
        // and add (leading − ascent) of air at the top of every multi-line
        // frame. Ascent matches paint.
        first_baseline_leading_offset: false,
        full_width_lock_line: geo.is_some_and(has_full_width_lock_line),
    })
}

fn glue_lock_line_spaces(runs: &mut [TextRun]) {
    for run in runs {
        if run.auto_page_number {
            continue;
        }
        if run.text.contains(' ') {
            run.text = run.text.replace(' ', "\u{00A0}");
        }
    }
}

fn insert_lock_breaks(runs: &mut Vec<TextRun>, break_chars: &[usize]) {
    if break_chars.is_empty() || runs.is_empty() {
        return;
    }
    let full: String = runs.iter().map(|r| r.text.as_str()).collect();
    let mut bytes: Vec<usize> = break_chars
        .iter()
        .filter_map(|&ci| {
            full.char_indices()
                .nth(ci)
                .map(|(i, _)| i)
                .or_else(|| (ci >= full.chars().count()).then_some(full.len()))
        })
        .collect();
    bytes.sort_unstable();
    bytes.dedup();
    for byte in bytes.into_iter().rev() {
        insert_newline_at_byte(runs, byte);
    }
}

fn insert_newline_at_byte(runs: &mut Vec<TextRun>, byte: usize) {
    let mut pos = 0usize;
    for i in 0..runs.len() {
        let a = pos;
        let b = pos + runs[i].text.len();
        if byte < a || byte > b {
            pos = b;
            continue;
        }
        let off = byte - a;
        if off == 0 {
            if i > 0 && runs[i - 1].text.ends_with('\n') {
                return;
            }
            if runs[i].text.starts_with('\n') {
                return;
            }
            let mut nl = runs[i].clone();
            nl.text = "\n".into();
            nl.auto_page_number = false;
            runs.insert(i, nl);
            return;
        }
        if off == runs[i].text.len() {
            if runs[i].text.ends_with('\n') {
                return;
            }
            if i + 1 < runs.len() && runs[i + 1].text.starts_with('\n') {
                return;
            }
            runs[i].text.push('\n');
            return;
        }
        if !runs[i].text.is_char_boundary(off) {
            return;
        }
        let rest = runs[i].text[off..].to_string();
        runs[i].text.truncate(off);
        if !runs[i].text.ends_with('\n') {
            runs[i].text.push('\n');
        }
        if !rest.is_empty() {
            let mut tail = runs[i].clone();
            tail.text = rest;
            runs.insert(i + 1, tail);
        }
        return;
    }
}

pub(crate) fn cell_runs(
    node: Option<&SemanticNode>,
    paint_runs: &[TextGlyphRun],
    geo: Option<&GeometryNode>,
    fonts: &FontCtx,
    header_bold: bool,
) -> Vec<TextRun> {
    let Some(node) = node else {
        return Vec::new();
    };
    let Some(text) = k2f_core::node_text(node) else {
        return Vec::new();
    };
    if text.is_empty() {
        return Vec::new();
    }
    let mut runs = runs::runs_from_paint(text, paint_runs, &node.modifiers, geo, fonts);
    if header_bold && paint_runs.is_empty() {
        for r in &mut runs {
            r.bold = true;
        }
    }
    let leading = leading_pt(geo, &node.modifiers);
    for r in &mut runs {
        r.leading_pt = leading;
    }
    runs
}

pub(crate) fn insets(
    geo: Option<&GeometryNode>,
    align: TextAlign,
    modifiers: &[Modifier],
) -> (f64, f64, f64, f64) {
    let Some(geo) = geo else {
        return (0.0, 0.0, 0.0, 0.0);
    };
    let glyphs = source_glyphs(geo);
    if glyphs.is_empty() {
        return (0.0, 0.0, 0.0, 0.0);
    }
    let top = glyphs
        .iter()
        .map(|g| g.y_offset.0)
        .min()
        .unwrap_or(0)
        .max(0);
    let top_pt = millipt_to_pt(top).max(0.0);
    let lines = body_lines(geo, modifiers);
    let bottom_pt = if lines.len() >= 2 {
        let last = lines.last().unwrap()[0].y_offset.0;
        let leading = (lines[1][0].y_offset.0 - lines[0][0].y_offset.0).abs();
        millipt_to_pt((geo.height.0 - last - leading).max(0)).max(0.0)
    } else {
        0.0
    };
    match align {
        TextAlign::Left => {
            let left = glyphs
                .iter()
                .map(|g| g.x_offset.0)
                .min()
                .unwrap_or(0)
                .max(0);
            (top_pt, millipt_to_pt(left).max(0.0), bottom_pt, 0.0)
        }
        TextAlign::Right => {
            let box_w = geo.width.0;
            let right = glyphs
                .iter()
                .map(|g| box_w - (g.x_offset.0 + g.x_advance.0))
                .min()
                .unwrap_or(0)
                .max(0);
            (top_pt, 0.0, bottom_pt, millipt_to_pt(right).max(0.0))
        }
        TextAlign::Center | TextAlign::Justify => (top_pt, 0.0, bottom_pt, 0.0),
    }
}

/// Extra indent of the first lock line vs later lines (Left only).
/// Common left padding stays on the frame (`inset_left`); this is `FirstLineIndent`.
/// Superscript/subscript-only rows (™, author marks) are not wrap lines.
pub(crate) fn infer_first_line_indent(
    geo: Option<&GeometryNode>,
    align: TextAlign,
    modifiers: &[Modifier],
) -> f64 {
    if !matches!(align, TextAlign::Left) {
        return 0.0;
    }
    let Some(geo) = geo else {
        return 0.0;
    };
    let lines = body_lines(geo, modifiers);
    if lines.len() < 2 {
        return 0.0;
    }
    let first = lines[0]
        .iter()
        .map(|g| g.x_offset.0)
        .min()
        .unwrap_or(0);
    let rest = lines[1..]
        .iter()
        .flat_map(|line| line.iter().map(|g| g.x_offset.0))
        .min()
        .unwrap_or(first);
    millipt_to_pt((first - rest).max(0))
}

pub(crate) fn leading_pt(geo: Option<&GeometryNode>, modifiers: &[Modifier]) -> Option<f64> {
    let geo = geo?;
    let lines = body_lines(geo, modifiers);
    if lines.len() < 2 {
        return None;
    }
    let mut ys: Vec<i128> = lines
        .iter()
        .filter_map(|line| line.iter().map(|g| g.y_offset.0).min())
        .collect();
    if ys.len() < 2 {
        return None;
    }
    ys.sort_unstable();
    let mut gaps: Vec<i128> = ys
        .windows(2)
        .map(|w| (w[1] - w[0]).abs())
        .filter(|g| *g > 0)
        .collect();
    if gaps.is_empty() {
        return None;
    }
    gaps.sort_unstable();
    Some(millipt_to_pt(gaps[gaps.len() / 2]))
}

/// Lock geometry with equal top/bottom glyph slack is vertically centered in
/// K2F even when the first-line y/h ratio is below the optical-center band.
pub(crate) fn symmetric_vertical_padding(top_pt: f64, bottom_pt: f64) -> bool {
    const MIN_PT: f64 = 2.0;
    if top_pt < MIN_PT || bottom_pt < MIN_PT {
        return false;
    }
    let delta = (top_pt - bottom_pt).abs();
    let slack = top_pt + bottom_pt;
    delta * 10.0 <= slack.max(MIN_PT * 2.0)
}

pub(crate) fn vert_center(
    geo: Option<&GeometryNode>,
    rect: &Rect,
    font_size: k2f_core::Pt,
) -> bool {
    let Some(geo) = geo else {
        return false;
    };
    let lines = source_lines(geo);
    if lines.is_empty() {
        return false;
    }
    let h = geo.height.0;
    if h <= 0 {
        return false;
    }
    let first_y = lines[0]
        .iter()
        .map(|g| g.y_offset.0)
        .min()
        .unwrap_or(0)
        .max(0);
    // Shrink-wrapped pill/badge: equal top/bottom padding around one line.
    // First-glyph y/h ratio stays ~0.2–0.3 (ascent offset), so the old
    // 0.4–0.6 optical-center band misses VIP labels and time pills.
    // Grid-column date/meta lines fill their column width (zero horizontal
    // slack) but can match the symmetric-height formula — keep them top-aligned.
    if lines.len() == 1 {
        let (h_slack, _, _) = line_gaps(&lines[0], geo.width.0);
        if h_slack >= 2_000 {
            let naive_bottom = h.saturating_sub(first_y).saturating_sub(font_size.0.max(0));
            // Shrink-wrapped pills use equal top/bottom padding; line_height_mult >
            // 1.0 makes naive_bottom > first_y even when padding is symmetric.
            let bottom = if naive_bottom > first_y
                && first_y >= 1_000
                && h <= first_y * 2 + font_size.0 + 2_000
            {
                first_y
            } else {
                naive_bottom
            };
            let slack = first_y.saturating_add(bottom);
            let delta = first_y - bottom;
            if slack >= 2_000 && delta.abs() * 5 <= slack && slack * 10 > h {
                return true;
            }
            // Padded bars/badges: theme top/bottom padding match first_y; line
            // height is the remainder (line_height_mult > 1 still fits).
            if first_y >= 1_000 {
                let line_h = h.saturating_sub(first_y.saturating_mul(2));
                let max_h = (first_y + font_size.0).saturating_mul(2).saturating_add(2_000);
                if line_h >= font_size.0.saturating_mul(8) / 10 && h <= max_h {
                    return true;
                }
            }
        }
    }
    if rect.height.0 <= font_size.0.saturating_mul(2) {
        return false;
    }
    let ratio = first_y as f64 / h as f64;
    (0.4..=0.6).contains(&ratio)
}

#[cfg(test)]
mod tests {
    use super::*;
    use k2f_core::{GlyphPosition, Pt};

    fn glyph(cluster: u32, x_off: i128, x_adv: i128, y: i128) -> GlyphPosition {
        GlyphPosition {
            glyph_id: 1,
            cluster,
            x_offset: Pt(x_off),
            y_offset: Pt(y),
            x_advance: Pt(x_adv),
            y_advance: Pt(0),
        }
    }

    fn pill_geo(height: i128, y: i128) -> GeometryNode {
        GeometryNode {
            id: "badge".into(),
            x: Pt(0),
            y: Pt(0),
            width: Pt(80_000),
            height: Pt(height),
            glyphs: vec![glyph(0, 5_000, 50_000, y)],
            text_runs: vec![],
            fill_rects: vec![],
            children: vec![],
        }
    }

    #[test]
    fn symmetric_pill_with_line_height_mult_centers() {
        // badge role: padding 2000 + line_height 8050 (7pt * 1.15) + padding 2000
        let geo = pill_geo(12_050, 2_000);
        let rect = Rect {
            x: Pt(0),
            y: Pt(0),
            width: Pt(80_000),
            height: Pt(12_050),
        };
        assert!(
            vert_center(Some(&geo), &rect, Pt(7_000)),
            "line_height_mult > 1 must not block pill centering"
        );
    }

    #[test]
    fn tall_single_line_frame_does_not_center() {
        let geo = pill_geo(30_000, 2_000);
        let rect = Rect {
            x: Pt(0),
            y: Pt(0),
            width: Pt(80_000),
            height: Pt(30_000),
        };
        assert!(
            !vert_center(Some(&geo), &rect, Pt(7_000)),
            "tall frame is not a shrink-wrapped pill"
        );
    }

    #[test]
    fn padded_bar_with_symmetric_theme_padding_centers() {
        let geo = pill_geo(21_500, 4_000);
        let rect = Rect {
            x: Pt(36_000),
            y: Pt(133_850),
            width: Pt(213_018),
            height: Pt(21_500),
        };
        assert!(
            vert_center(Some(&geo), &rect, Pt(10_000)),
            "symmetric padded bar must vertically center"
        );
    }

    #[test]
    fn flush_left_column_line_stays_top_aligned() {
        let geo = GeometryNode {
            id: "pd.yrs".into(),
            x: Pt(0),
            y: Pt(0),
            width: Pt(144_333),
            height: Pt(10_800),
            glyphs: vec![glyph(0, 0, 80_000, 0)],
            text_runs: vec![],
            fill_rects: vec![],
            children: vec![],
        };
        let rect = Rect {
            x: Pt(225_334),
            y: Pt(553_304),
            width: Pt(144_333),
            height: Pt(10_800),
        };
        assert!(
            !vert_center(Some(&geo), &rect, Pt(8_000)),
            "flush-top column label must not vertically center"
        );
    }

    #[test]
    fn grid_column_meta_with_symmetric_height_stays_top_aligned() {
        let geo = GeometryNode {
            id: "app_harvard.meta".into(),
            x: Pt(0),
            y: Pt(0),
            width: Pt(112_358),
            height: Pt(22_120),
            glyphs: vec![glyph(0, 0, 112_358, 6_380)],
            text_runs: vec![],
            fill_rects: vec![],
            children: vec![],
        };
        let rect = Rect {
            x: Pt(0),
            y: Pt(0),
            width: Pt(112_358),
            height: Pt(22_120),
        };
        assert!(
            !vert_center(Some(&geo), &rect, Pt(7_800)),
            "full-width grid meta must not vertically center"
        );
    }

    fn line_geo(ys: &[i128]) -> GeometryNode {
        GeometryNode {
            id: "body".into(),
            x: Pt(0),
            y: Pt(0),
            width: Pt(400_000),
            height: Pt(80_000),
            glyphs: ys
                .iter()
                .enumerate()
                .map(|(i, y)| glyph(i as u32, 0, 80_000, *y))
                .collect(),
            text_runs: vec![],
            fill_rects: vec![],
            children: vec![],
        }
    }

    #[test]
    fn symmetric_multi_line_body_block_centers() {
        // doc.body.l.mid lock geometry: 9 lines, ~64pt equal top/bottom slack.
        let ys = [
            64_212, 77_987, 91_762, 119_312, 133_087, 146_862, 174_412, 188_187, 201_962,
        ];
        let mut geo = line_geo(&ys);
        geo.height = Pt(279_950);
        let rect = Rect {
            x: Pt(0),
            y: Pt(0),
            width: Pt(233_000),
            height: Pt(279_950),
        };
        assert!(
            !vert_center(Some(&geo), &rect, Pt(9_500)),
            "ratio band alone must not center multi-line body"
        );
        let (top, _, bottom, _) = insets(Some(&geo), TextAlign::Left, &[]);
        assert!(
            symmetric_vertical_padding(top, bottom),
            "equal top/bottom insets must detect vertical center"
        );
    }

    #[test]
    fn two_line_banner_with_symmetric_padding_centers() {
        let mut geo = line_geo(&[7_000, 18_050]);
        geo.height = Pt(36_100);
        let (top, _, bottom, _) = insets(Some(&geo), TextAlign::Left, &[]);
        assert!(
            symmetric_vertical_padding(top, bottom),
            "notice.banner style padded bar must center"
        );
    }

    #[test]
    fn flush_top_body_stays_top_aligned() {
        let ys: Vec<i128> = (0..5).map(|i| i * 14_000).collect();
        let geo = line_geo(&ys);
        let (top, _, bottom, _) = insets(Some(&geo), TextAlign::Left, &[]);
        assert!(
            !symmetric_vertical_padding(top, bottom),
            "flush-top column body must not vertically center"
        );
    }

    #[test]
    fn leading_uses_median_gap_not_first_pair() {
        // A superscript-sized first gap must not become paragraph leading.
        let geo = line_geo(&[0, 3_000, 17_000, 31_000]);
        let pt = leading_pt(Some(&geo), &[]).expect("leading");
        assert!(
            (pt - 14.0).abs() < 0.01,
            "median wrap gap is 14pt, got {pt}"
        );
    }
}