k2f_pptx 0.3.2

K2F lock → PPTX 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
use crate::ir::TextAlign;
use k2f_core::{GeometryNode, GlyphPosition, Pt};

const MIN_SLACK: i128 = 2_000;
/// Historical body default: 7.5pt. Half of 11–12pt faces still land here.
const MAX_LINE_CLUSTER_TOL: i128 = 7_500;
const MIN_LINE_CLUSTER_TOL: i128 = 2_000;

pub fn infer_text_align(geo: &GeometryNode, text: &str) -> TextAlign {
    let lines = source_lines(geo);
    if lines.is_empty() {
        return TextAlign::Left;
    }
    let box_w = geo.width.0;
    if lines.len() >= 2 && is_justify(&lines, text, box_w) {
        return TextAlign::Justify;
    }
    let mut best: Option<(i128, i128, i128)> = None;
    for glyphs in &lines {
        let (slack, left, right) = line_gaps(glyphs, box_w);
        if best.map(|(s, _, _)| slack > s).unwrap_or(true) {
            best = Some((slack, left, right));
        }
    }
    match best {
        Some((_, left, right)) => infer_from_gaps(left, right),
        None => TextAlign::Left,
    }
}

pub(crate) fn source_glyphs(geo: &GeometryNode) -> Vec<&GlyphPosition> {
    geo.glyphs
        .iter()
        .filter(|g| g.cluster != GlyphPosition::CLUSTER_NOT_SOURCE)
        .collect()
}

pub(crate) fn source_lines(geo: &GeometryNode) -> Vec<Vec<&GlyphPosition>> {
    let mut glyphs = source_glyphs(geo);
    if glyphs.is_empty() {
        return Vec::new();
    }
    glyphs.sort_by_key(|g| (g.y_offset.0, g.x_offset.0));
    let tol = line_cluster_tol(geo);

    let mut lines: Vec<Vec<&GlyphPosition>> = Vec::new();
    for g in glyphs {
        if let Some(line) = lines.iter_mut().find(|line| {
            line.iter()
                .any(|existing| (existing.y_offset.0 - g.y_offset.0).abs() <= tol)
        }) {
            line.push(g);
        } else {
            lines.push(vec![g]);
        }
    }
    lines.sort_by_key(|line| {
        let mut ys: Vec<i128> = line.iter().map(|g| g.y_offset.0).collect();
        ys.sort_unstable();
        ys[ys.len() / 2]
    });
    for line in &mut lines {
        line.sort_by_key(|g| g.x_offset.0);
    }
    lines
}

/// Same-line glyphs stay together; wrapped lines of this face must split.
/// Half the lock font, clamped so 5–7pt body (6.2pt leading) is not merged
/// by the historical 7.5pt cap. 11–12pt tests keep that cap.
///
/// Use the **largest** face in the box, not `text_runs[0]`. A leading
/// superscript/subscript run is often smaller; half of that face is too
/// tight and splits raised digits onto their own "lines", so line spacing
/// collapses to the super→body gap (~4pt) and multi-line affiliations crush.
pub(crate) fn body_font_size(geo: &GeometryNode) -> i128 {
    geo.text_runs
        .iter()
        .map(|r| r.style.font_size.0.abs())
        .filter(|n| *n > 0)
        .max()
        .unwrap_or(12_000)
}

fn line_cluster_tol(geo: &GeometryNode) -> i128 {
    (body_font_size(geo) / 2).clamp(MIN_LINE_CLUSTER_TOL, MAX_LINE_CLUSTER_TOL)
}

/// Office wrap on a glyph-tight lock box reflows the last word onto a clipped
/// second line when host bold/metrics are wider than rustybuzz. Keep wrap only
/// when the lock already wrapped *within* a paragraph. One-line-tall boxes
/// (pills, title/date rows) must not wrap. Explicit `\n` lines stay
/// `wrap=none` even in a padded cell that still fits N+1 lines — host wrap
/// of a long second paragraph would paint a third row over the next cell
/// (invoice line-items, two-line labels).
pub(crate) fn should_wrap_lock(
    geo: Option<&GeometryNode>,
    font_size: Pt,
    text: Option<&str>,
) -> bool {
    let Some(geo) = geo else {
        return true;
    };
    let lines = source_lines(geo);
    let paras = text.map(source_paragraphs).unwrap_or(0);
    if lines.len() >= 2 && (paras == 0 || lines.len() > paras) {
        return true;
    }
    if lines.len() >= 2 {
        return false;
    }
    let Some(line) = lines.first() else {
        return true;
    };
    if !fits_n_lines(geo, line, font_size, 2) {
        return false;
    }
    let (slack, _, _) = line_gaps(line, geo.width.0);
    slack >= MIN_SLACK
}

/// `wrap=none` on a wide center/justify/right frame makes hosts ignore `algn`
/// and paint at `lIns` (lock-left of the box). Turn wrap on when leftover
/// width shows the alignment. Narrow glyph-tight pills (≥85% full) stay
/// `wrap=none` so they do not clip a second row. Wide near-full center lines
/// (cover subtitles) still need square wrap — otherwise hosts shift them by
/// the full left gap. Multi-line frames use `any`: a short centered second
/// line must force wrap even when line 1 is nearly full. Left stays
/// `wrap=none` — painting at `lIns` is correct.
pub(crate) fn host_wrap(geo: Option<&GeometryNode>, align: TextAlign, wrap: bool) -> bool {
    if wrap {
        return true;
    }
    if matches!(align, TextAlign::Left) {
        return false;
    }
    let Some(geo) = geo else {
        return false;
    };
    let lines = source_lines(geo);
    if lines.is_empty() {
        return false;
    }
    let box_w = geo.width.0;
    lines.iter().any(|line| {
        let (_, left, right) = line_gaps(line, box_w);
        match align {
            // Right: only left leftover proves alignment; right may be a thin
            // pad (< MIN_SLACK) or flush. Same fill check as Right insets.
            TextAlign::Right => {
                left >= MIN_SLACK && !line_fills_padded_width(right, left, box_w)
            }
            TextAlign::Center | TextAlign::Justify => {
                left >= MIN_SLACK
                    && right >= MIN_SLACK
                    && (!line_fills_padded_width(left, right, box_w)
                        || box_w >= WIDE_CENTER_MIN)
            }
            TextAlign::Left => false,
        }
    })
}

/// ~200pt. Page-width / hero frames, not chip/pill boxes (~80–90pt).
const WIDE_CENTER_MIN: i128 = 200_000;

fn line_fills_padded_width(pad: i128, opposite_slack: i128, box_w: i128) -> bool {
    let avail = box_w.saturating_sub(pad.max(0));
    if avail <= 0 {
        return false;
    }
    let content = avail.saturating_sub(opposite_slack.max(0));
    content.saturating_mul(100) >= avail.saturating_mul(85)
}

fn source_paragraphs(text: &str) -> usize {
    text.split('\n').count().max(1)
}

fn fits_n_lines(geo: &GeometryNode, line: &[&GlyphPosition], font_size: Pt, n: usize) -> bool {
    let y = line.iter().map(|g| g.y_offset.0).min().unwrap_or(0).max(0);
    let content_h = geo.height.0.saturating_sub(y);
    let n = i128::try_from(n).unwrap_or(2).max(1);
    content_h >= font_size.0.saturating_mul(n)
}

/// Lock-wrapped paragraph: insert `\n` at lock line starts so each visual
/// line is its own paragraph. Impress ignores DrawingML `spcPts` line
/// spacing on wrapped runs *and* on one-line paragraphs; stacking with
/// `spcAft` (lock delta − face) is what actually opens the leading.
/// Justify still wraps (hard breaks would drop last-line raggedness).
pub(crate) fn should_pin_lock_breaks(
    geo: Option<&GeometryNode>,
    _font_size: Pt,
    text: Option<&str>,
    align: TextAlign,
) -> bool {
    if matches!(align, TextAlign::Justify) {
        return false;
    }
    let Some(geo) = geo else {
        return false;
    };
    let Some(text) = text else {
        return false;
    };
    let lines = source_lines(geo);
    lines.len() >= 2 && lines.len() > source_paragraphs(text)
}

/// Character indices (in `text`) where lock lines after the first begin.
/// Skip a line that already follows an explicit `\n` — pinning that index
/// would emit a blank Office paragraph (hero titles with a hard break plus wrap).
pub(crate) fn lock_break_char_indices(geo: &GeometryNode, text: &str) -> Vec<usize> {
    let chars: Vec<char> = text.chars().collect();
    source_lines(geo)
        .iter()
        .skip(1)
        .filter_map(|line| line.first().map(|g| g.cluster as usize))
        .filter(|&idx| {
            idx < chars.len() && chars[idx] != '\n' && (idx == 0 || chars[idx - 1] != '\n')
        })
        .collect()
}

pub(crate) fn line_gaps(glyphs: &[&GlyphPosition], box_w: i128) -> (i128, i128, i128) {
    let left = glyphs.iter().map(|g| g.x_offset.0).min().unwrap_or(0);
    let right_edge = glyphs
        .iter()
        .map(|g| g.x_offset.0 + g.x_advance.0)
        .max()
        .unwrap_or(0);
    let right = box_w - right_edge;
    (left.saturating_add(right), left, right)
}

/// Lock paint does not clip. A wrapped last line can start at or past
/// `geo.height` when the box was sized for N−1 lines. Office frames clip to
/// `cy`, so expand to that line's ink (`y_offset + font_size`).
pub(crate) fn lock_ink_height(geo: Option<&GeometryNode>, font_size: Pt) -> Option<Pt> {
    let geo = geo?;
    let lines = source_lines(geo);
    let last = lines.last()?;
    let last_y = last.iter().map(|g| g.y_offset.0).min().unwrap_or(0).max(0);
    let ink = last_y.saturating_add(font_size.0.max(0));
    if ink > geo.height.0 {
        Some(Pt(ink))
    } else {
        None
    }
}

/// Exact Office line spacing is a per-paragraph box, including trailing
/// leading after the last line. Lock leading is the gap *between* lines.
/// Pin the last paragraph to the face size so that extra leading does not
/// paint over the next node (PPTX overflow) or get clipped (Writer).
pub(crate) fn last_line_spc_pts(font_size: Pt) -> Option<i32> {
    i32::try_from(font_size.0 / 10).ok().map(|v| v.max(100))
}

pub(crate) fn line_spacing_spc_pts(geo: Option<&GeometryNode>) -> Option<i32> {
    let geo = geo?;
    let lines = source_lines(geo);
    if lines.len() < 2 {
        return None;
    }
    let baseline = |line: &[&GlyphPosition]| -> i128 {
        let mut ys: Vec<i128> = line.iter().map(|g| g.y_offset.0).collect();
        ys.sort_unstable();
        ys[ys.len() / 2]
    };
    // Ignore super/sub → body gaps; real leading is ≥ ~0.8× body face.
    let min_delta = body_font_size(geo)
        .saturating_mul(4)
        .saturating_div(5)
        .max(MIN_LINE_CLUSTER_TOL);
    for i in 0..lines.len() - 1 {
        let delta = (baseline(&lines[i + 1]) - baseline(&lines[i])).abs();
        if delta >= min_delta {
            return i32::try_from(delta / 10).ok().map(|v| v.max(100));
        }
    }
    None
}

fn infer_from_gaps(left: i128, right: i128) -> TextAlign {
    let slack = left.saturating_add(right);
    if slack < MIN_SLACK {
        return TextAlign::Left;
    }
    let delta = left - right;
    // Equal leftover on both sides is padding (pills, chips), not a wide
    // left-aligned frame. Hosts need that slack for metrics, so center.
    if delta.abs() * 5 <= slack && left >= MIN_SLACK && right >= MIN_SLACK {
        TextAlign::Center
    } else if delta > slack / 4 {
        TextAlign::Right
    } else {
        TextAlign::Left
    }
}

/// Lock leftover above the first baseline vs below the last line box.
pub(crate) fn vert_center(geo: &GeometryNode, font_size: Pt) -> bool {
    // Compact pills (~1.9× face) still have leftover to center in. Reject
    // only when the box is no taller than the face.
    if geo.height.0 <= font_size.0 {
        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);
    let last_y = lines[lines.len() - 1]
        .iter()
        .map(|g| g.y_offset.0)
        .min()
        .unwrap_or(first_y);
    let fs = font_size.0.max(1);
    let top = first_y;
    let bot_after = (h - last_y - fs).max(0);
    let lo = top.min(bot_after);
    let hi = top.max(bot_after);
    lo >= 2_000 && lo.saturating_mul(2) >= hi
}

fn is_justify(lines: &[Vec<&GlyphPosition>], text: &str, box_w: i128) -> bool {
    let last = lines.last().expect("non-empty");
    let last_spaces = space_advances(last, text);
    if last_spaces.is_empty() {
        return false;
    }
    let mut sorted = last_spaces.clone();
    sorted.sort_unstable();
    let median = sorted[sorted.len() / 2];
    let (last_slack, _, _) = line_gaps(last, box_w);
    for line in &lines[..lines.len() - 1] {
        let (slack, _, _) = line_gaps(line, box_w);
        for adv in space_advances(line, text) {
            if adv > median.saturating_mul(3) / 2 && last_slack > slack {
                return true;
            }
        }
    }
    false
}

fn space_advances(glyphs: &[&GlyphPosition], text: &str) -> Vec<i128> {
    glyphs
        .iter()
        .filter(|g| char_at(text, g.cluster as usize) == Some(' '))
        .map(|g| g.x_advance.0)
        .collect()
}

fn char_at(text: &str, cluster: usize) -> Option<char> {
    text.chars().nth(cluster)
}

#[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 geo(width: i128, glyphs: Vec<GlyphPosition>) -> GeometryNode {
        GeometryNode {
            id: "g".into(),
            x: Pt(0),
            y: Pt(0),
            width: Pt(width),
            height: Pt(40_000),
            glyphs,
            text_runs: vec![],
            fill_rects: vec![],
            children: vec![],
        }
    }

    fn with_font(mut g: GeometryNode, size: i128) -> GeometryNode {
        g.text_runs = vec![k2f_core::TextGlyphRun {
            glyph_range: [0, g.glyphs.len()],
            style: k2f_core::TextPaintStyle {
                font_family: "default".into(),
                font_size: Pt(size),
                color: "#000000".into(),
                bold: false,
                italic: false,
                strikethrough: false,
                underline: false,
            },
        }];
        g
    }

    #[test]
    fn infer_text_align_unit() {
        let w = 100_000i128;
        assert_eq!(
            infer_text_align(&geo(w, vec![glyph(0, 0, 40_000, 0)]), "A"),
            TextAlign::Left
        );
        assert_eq!(
            infer_text_align(&geo(w, vec![glyph(0, 30_000, 40_000, 0)]), "A"),
            TextAlign::Center
        );
        assert_eq!(
            infer_text_align(&geo(w, vec![glyph(0, 60_000, 40_000, 0)]), "A"),
            TextAlign::Right
        );
        assert_eq!(
            infer_text_align(&geo(87_000, vec![glyph(0, 6_500, 74_000, 0)]), "A"),
            TextAlign::Center
        );
        let just = geo(
            w,
            vec![
                glyph(0, 0, 20_000, 0),
                glyph(1, 20_000, 40_000, 0),
                glyph(2, 60_000, 20_000, 0),
                glyph(4, 0, 20_000, 14_000),
                glyph(5, 20_000, 10_000, 14_000),
                glyph(6, 30_000, 20_000, 14_000),
            ],
        );
        assert_eq!(infer_text_align(&just, "A B\nA B"), TextAlign::Justify);
    }

    #[test]
    fn wrap_only_when_lock_has_slack_or_multiple_lines() {
        let fs = Pt(10_000);
        let tight = geo(40_000, vec![glyph(0, 0, 40_000, 0)]);
        assert!(!should_wrap_lock(Some(&tight), fs, None));
        let slack = geo(100_000, vec![glyph(0, 0, 40_000, 0)]);
        assert!(should_wrap_lock(Some(&slack), fs, None));
        let wrapped = geo(
            40_000,
            vec![glyph(0, 0, 40_000, 0), glyph(1, 0, 20_000, 14_000)],
        );
        assert!(should_wrap_lock(Some(&wrapped), fs, None));
        assert!(should_wrap_lock(None, fs, None));
    }

    #[test]
    fn wrap_off_when_single_line_frame_is_too_short() {
        let mut short = geo(250_000, vec![glyph(0, 0, 220_000, 500)]);
        short.height = Pt(13_000);
        assert!(!should_wrap_lock(Some(&short), Pt(9_500), None));
        let mut padded = geo(87_000, vec![glyph(0, 6_500, 74_000, 2_500)]);
        padded.height = Pt(13_250);
        assert!(!should_wrap_lock(Some(&padded), Pt(7_500), None));
    }

    #[test]
    fn host_wrap_square_for_wide_center_one_liner() {
        let fs = Pt(12_000);
        let mut wide = geo(100_000, vec![glyph(0, 30_000, 40_000, 0)]);
        wide.height = Pt(13_000);
        assert!(!should_wrap_lock(Some(&wide), fs, None));
        assert!(host_wrap(
            Some(&wide),
            TextAlign::Center,
            should_wrap_lock(Some(&wide), fs, None)
        ));
        let mut left = geo(100_000, vec![glyph(0, 0, 40_000, 0)]);
        left.height = Pt(13_000);
        assert!(!host_wrap(
            Some(&left),
            TextAlign::Left,
            should_wrap_lock(Some(&left), fs, None)
        ));
        let mut pill = geo(87_000, vec![glyph(0, 6_500, 74_000, 0)]);
        pill.height = Pt(13_250);
        assert!(!host_wrap(
            Some(&pill),
            TextAlign::Center,
            should_wrap_lock(Some(&pill), fs, None)
        ));
        // Page-width near-full center line (cover subtitle): still square so
        // hosts honor algn — wrap=none would shift by the full left gap.
        let mut wide_full = geo(511_000, vec![glyph(0, 20_500, 470_000, 0)]);
        wide_full.height = Pt(15_000);
        assert!(host_wrap(Some(&wide_full), TextAlign::Center, false));
        // Soft-wrapped hero title: line 1 nearly full, line 2 short centered.
        let mut hero = geo(
            511_000,
            vec![
                glyph(0, 75_000, 361_000, 0),
                glyph(1, 178_000, 155_000, 29_000),
            ],
        );
        hero.height = Pt(58_000);
        assert!(host_wrap(Some(&hero), TextAlign::Center, false));
    }

    #[test]
    fn host_wrap_square_for_wide_right_one_liner() {
        let fs = Pt(7_500);
        // Unfilled table cell: short number, right-aligned, thin right pad.
        let mut cell = geo(78_000, vec![glyph(0, 50_000, 22_000, 4_200)]);
        cell.height = Pt(17_400);
        assert!(!should_wrap_lock(Some(&cell), fs, Some("$680.0")));
        assert!(host_wrap(
            Some(&cell),
            TextAlign::Right,
            should_wrap_lock(Some(&cell), fs, Some("$680.0"))
        ));
        let mut flush = geo(78_000, vec![glyph(0, 55_000, 23_000, 4_200)]);
        flush.height = Pt(17_400);
        assert!(host_wrap(Some(&flush), TextAlign::Right, false));
        let mut tight = geo(40_000, vec![glyph(0, 2_000, 36_000, 0)]);
        tight.height = Pt(13_000);
        assert!(!host_wrap(Some(&tight), TextAlign::Right, false));
    }

    #[test]
    fn small_font_wrapped_lines_are_not_merged() {
        let fs = Pt(5_200);
        let mut g = with_font(
            geo(
                196_000,
                vec![glyph(0, 0, 190_000, 0), glyph(1, 0, 40_000, 6_240)],
            ),
            5_200,
        );
        g.height = Pt(12_480);
        assert_eq!(source_lines(&g).len(), 2);
        assert!(should_wrap_lock(Some(&g), fs, None));
        assert_eq!(line_spacing_spc_pts(Some(&g)), Some(624));
    }

    #[test]
    fn wrap_off_when_explicit_lines_fill_the_frame() {
        let fs = Pt(7_200);
        let mut g = geo(
            76_000,
            vec![glyph(0, 0, 70_000, 0), glyph(1, 0, 74_000, 8_000)],
        );
        g.height = Pt(16_000);
        assert!(!should_wrap_lock(
            Some(&g),
            fs,
            Some("bwHPC Symposium\n23.10.2023 - Mannheim")
        ));
        assert!(should_wrap_lock(
            Some(&g),
            fs,
            Some("one paragraph that the lock wrapped onto two lines")
        ));
        g.height = Pt(16_000);
        assert!(should_pin_lock_breaks(
            Some(&g),
            fs,
            Some("one paragraph that the lock wrapped onto two lines"),
            TextAlign::Left
        ));
        assert!(!should_pin_lock_breaks(
            Some(&g),
            fs,
            Some("bwHPC Symposium\n23.10.2023 - Mannheim"),
            TextAlign::Left
        ));
        g.height = Pt(40_000);
        assert!(
            !should_wrap_lock(
                Some(&g),
                fs,
                Some("Crystal Cloud Core Platform Commitment\nDedicated multi-tenant control plane, 99.99% uptime SLA")
            ),
            "padded two-line cells must not host-wrap a long second paragraph"
        );
        assert!(
            should_pin_lock_breaks(
                Some(&g),
                fs,
                Some("one paragraph that the lock wrapped onto two lines"),
                TextAlign::Left
            ),
            "PPTX pins lock wraps so Impress can take spcAft between one-line paras"
        );
    }

    #[test]
    fn overflowing_last_line_expands_ink_height() {
        let fs = Pt(13_000);
        let mut g = geo(
            250_000,
            vec![
                glyph(0, 0, 200_000, 0),
                glyph(1, 0, 160_000, 18_200),
                glyph(2, 0, 200_000, 36_400),
                glyph(3, 0, 80_000, 54_600),
            ],
        );
        g.height = Pt(54_600);
        assert_eq!(lock_ink_height(Some(&g), fs), Some(Pt(67_600)));
        g.height = Pt(80_000);
        assert_eq!(lock_ink_height(Some(&g), fs), None);
    }

    #[test]
    fn lock_breaks_skip_lines_that_already_follow_newline() {
        // "Ab\nCd wrap" — line 2 already follows `\n`; only the wrap needs a pin.
        let text = "Ab\nCd wrap";
        let g = geo(
            40_000,
            vec![
                glyph(0, 0, 12_000, 0),
                glyph(1, 12_000, 12_000, 0),
                glyph(3, 0, 12_000, 12_000),
                glyph(4, 12_000, 12_000, 12_000),
                glyph(6, 0, 12_000, 24_000),
                glyph(7, 12_000, 12_000, 24_000),
            ],
        );
        assert_eq!(lock_break_char_indices(&g, text), vec![6]);
        let wrap_only = "one two";
        let wrapped = geo(
            40_000,
            vec![glyph(0, 0, 20_000, 0), glyph(4, 0, 20_000, 12_000)],
        );
        assert_eq!(lock_break_char_indices(&wrapped, wrap_only), vec![4]);
    }

    #[test]
    fn compact_padded_cell_is_vert_centered() {
        // th/td with ~7.5pt pad around 7.5pt type (future-systems / similar).
        let fs = Pt(7_500);
        let mut g = with_font(geo(80_000, vec![glyph(0, 6_000, 40_000, 7_500)]), 7_500);
        g.height = Pt(22_500);
        assert!(vert_center(&g, fs));
        let mut stretch = with_font(geo(80_000, vec![glyph(0, 6_000, 40_000, 7_500)]), 7_500);
        stretch.height = Pt(40_000);
        assert!(!vert_center(&stretch, fs));
    }

    #[test]
    fn compact_format_chip_is_vert_centered() {
        // Same-node pills just under 2× face (hero right labels: 14pt in 26.8pt).
        let fs = Pt(14_000);
        let mut g = with_font(geo(58_794, vec![glyph(0, 12_000, 8_000, 5_000)]), 14_000);
        g.height = Pt(26_800);
        assert!(vert_center(&g, fs));
        let mut tight = with_font(geo(58_794, vec![glyph(0, 12_000, 8_000, 0)]), 14_000);
        tight.height = Pt(14_000);
        assert!(!vert_center(&tight, fs));
    }
}