dxpdf 0.2.14

A fast DOCX-to-PDF converter powered by Skia
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
//! Table layout — 3-pass column sizing, cell layout, border rendering.
//!
//! Pass 1: Compute column widths from grid definitions or equal distribution.
//! Pass 2: Lay out each cell with tight width constraints, determine row heights.
//! Pass 3: Position cells and emit border commands.

use crate::render::dimension::Pt;
use crate::render::geometry::PtSize;

use super::BoxConstraints;

mod borders;
mod emit;
mod grid;
mod measure;
mod split;
mod types;

pub use grid::compute_column_widths;
pub use types::*;

use emit::{emit_split_row, emit_table_rows, TableCommandBuffers};
use grid::build_row_groups;
use measure::measure_table_rows;
use split::{find_row_cut, split_row_at, RowCutInput};

/// Lay out a table: compute column widths, lay out cells, emit borders.
///
/// §17.4.38: `suppress_first_row_top` suppresses the top border of the first row
/// for adjacent table border collapse.
pub fn layout_table(
    rows: &[TableRowInput],
    col_widths: &[Pt],
    _constraints: &BoxConstraints,
    default_line_height: Pt,
    borders: Option<&TableBorderConfig>,
    measure_text: super::paragraph::MeasureTextFn<'_>,
    suppress_first_row_top: bool,
) -> TableLayout {
    if rows.is_empty() || col_widths.is_empty() {
        return TableLayout {
            commands: Vec::new(),
            size: PtSize::ZERO,
        };
    }

    let measured = measure_table_rows(
        rows,
        col_widths,
        default_line_height,
        borders,
        measure_text,
        suppress_first_row_top,
    );

    let mut commands = Vec::new();
    let mut content_commands = Vec::new();
    let mut border_commands = Vec::new();
    let mut cursor_y = Pt::ZERO;

    // Monolithic table: no top border override needed — borders are resolved correctly.
    emit_table_rows(
        &measured,
        rows,
        0..measured.rows.len(),
        &mut cursor_y,
        &mut TableCommandBuffers {
            commands: &mut commands,
            content_commands: &mut content_commands,
            border_commands: &mut border_commands,
        },
        None,
    );

    commands.append(&mut content_commands);
    commands.append(&mut border_commands);

    TableLayout {
        commands,
        size: PtSize::new(measured.table_width, cursor_y),
    }
}

/// One page-slice of a table, produced by `layout_table_paginated`.
#[derive(Debug)]
pub struct TableSlice {
    /// Draw commands positioned relative to this slice's top-left origin (0,0).
    pub commands: Vec<super::draw_command::DrawCommand>,
    /// Size of this slice.
    pub size: PtSize,
}

/// Pagination parameters for `layout_table_paginated`.
pub struct TablePaginationConfig {
    /// Available height on the first page.
    pub available_height: Pt,
    /// Full page height for continuation pages.
    pub page_height: Pt,
    /// Whether to suppress the first row's top border (adjacent table collapse).
    pub suppress_first_row_top: bool,
}

/// Lay out a table with page splitting at row boundaries.
///
/// §17.4.49: header rows repeat on each continuation page.
/// §17.4.1: `cantSplit` rows are kept together (moved to next page if needed).
///
/// Returns one `TableSlice` per page.
pub fn layout_table_paginated(
    rows: &[TableRowInput],
    col_widths: &[Pt],
    _constraints: &BoxConstraints,
    default_line_height: Pt,
    borders: Option<&TableBorderConfig>,
    measure_text: super::paragraph::MeasureTextFn<'_>,
    pagination: &TablePaginationConfig,
) -> Vec<TableSlice> {
    let available_height = pagination.available_height;
    let page_height = pagination.page_height;
    let suppress_first_row_top = pagination.suppress_first_row_top;
    if rows.is_empty() || col_widths.is_empty() {
        return vec![TableSlice {
            commands: Vec::new(),
            size: PtSize::ZERO,
        }];
    }

    let measured = measure_table_rows(
        rows,
        col_widths,
        default_line_height,
        borders,
        measure_text,
        suppress_first_row_top,
    );

    // §17.4.49: contiguous header rows from index 0.
    let header_count = rows
        .iter()
        .take_while(|r| r.is_header == Some(true))
        .count();
    let header_height: Pt = measured.rows[..header_count]
        .iter()
        .map(|mr| mr.height + mr.border_gap_below)
        .sum();

    let groups = build_row_groups(rows, &measured);

    // Each slice is a list of items to emit in order: either a range of
    // measured rows (the common case) or a custom (split) row with its own
    // MeasuredRow data.
    let mut slices: Vec<Vec<SliceItem>> = Vec::new();
    let mut current_slice: Vec<SliceItem> = Vec::new();
    let mut remaining = available_height;

    for group in &groups {
        if group.height <= remaining {
            current_slice.push(SliceItem::Range(group.start..group.end));
            remaining -= group.height;
            continue;
        }

        // §17.4.49: header rows are atomic with respect to splitting — they
        // must remain intact so the same row content can repeat verbatim on
        // continuation slices. Non-header rows fall through to the normal
        // §17.4.1 split path.
        let is_header = group.start < header_count;

        // Doesn't fit. Try to split (§17.4.1) before spilling the whole
        // group to the next page. Only non-header single-row groups are
        // splittable — vMerge spans and cantSplit rows set `splittable=false`.
        if !is_header && group.splittable && group.end - group.start == 1 {
            let row_idx = group.start;
            let cut_input = RowCutInput {
                mr: &measured.rows[row_idx],
                row: &rows[row_idx],
                available: remaining,
            };
            if let Some(cut) = find_row_cut(&cut_input) {
                let parts = split_row_at(&measured.rows[row_idx], &cut);
                current_slice.push(SliceItem::Split {
                    row_idx,
                    mr: parts.first,
                });
                slices.push(std::mem::take(&mut current_slice));
                // New page: start with header rows (if any).
                remaining = page_height;
                if header_count > 0 {
                    current_slice.push(SliceItem::Range(0..header_count));
                    remaining -= header_height;
                }

                // Iteratively place the continuation, splitting again each
                // time it exceeds the new page's remaining space.
                let mut pending = parts.second;
                loop {
                    if pending.height <= remaining {
                        remaining -= pending.height;
                        current_slice.push(SliceItem::Continuation {
                            row_idx,
                            mr: pending,
                        });
                        break;
                    }
                    let sub_cut_input = RowCutInput {
                        mr: &pending,
                        row: &rows[row_idx],
                        available: remaining,
                    };
                    match find_row_cut(&sub_cut_input) {
                        Some(sub_cut) => {
                            let sub = split_row_at(&pending, &sub_cut);
                            current_slice.push(SliceItem::Continuation {
                                row_idx,
                                mr: sub.first,
                            });
                            slices.push(std::mem::take(&mut current_slice));
                            remaining = page_height;
                            if header_count > 0 {
                                current_slice.push(SliceItem::Range(0..header_count));
                                remaining -= header_height;
                            }
                            pending = sub.second;
                        }
                        None => {
                            // Not even one line of the continuation fits.
                            // This should be rare — a row taller than a
                            // full page of content. Emit it anyway and log.
                            log::warn!(
                                "[table] row {} continuation ({:.1}pt) exceeds \
                                 page content height ({:.1}pt available)",
                                row_idx,
                                pending.height.raw(),
                                remaining.raw(),
                            );
                            current_slice.push(SliceItem::Continuation {
                                row_idx,
                                mr: pending,
                            });
                            break;
                        }
                    }
                }
                continue;
            }
        }

        // No split possible — move the whole group to the next page.
        slices.push(std::mem::take(&mut current_slice));
        remaining = page_height;
        // §17.4.49: prepend the repeating header rows only when this group
        // sits past the headers. When advancing because a header row itself
        // doesn't fit, the row is part of the table's first appearance —
        // emitting `Range(0..header_count)` here would duplicate it.
        if !is_header && header_count > 0 {
            current_slice.push(SliceItem::Range(0..header_count));
            remaining -= header_height;
        }
        if group.height > remaining {
            log::warn!(
                "[table] row group {}-{} ({:.1}pt) exceeds page height ({:.1}pt available)",
                group.start,
                group.end,
                group.height.raw(),
                remaining.raw(),
            );
        }
        current_slice.push(SliceItem::Range(group.start..group.end));
        remaining -= group.height;
    }
    slices.push(current_slice);

    // §17.4.38: the table's outer top border, used to restore the top edge
    // on continuation page slices where border conflict resolution or adjacent
    // table collapse removed it.
    let outer_top_border = borders.and_then(|b| b.top);

    // Emit draw commands for each slice.
    slices
        .iter()
        .enumerate()
        .map(|(slice_idx, items)| {
            let mut commands = Vec::new();
            let mut content_commands = Vec::new();
            let mut border_commands = Vec::new();
            let mut cursor_y = Pt::ZERO;
            for (item_idx, item) in items.iter().enumerate() {
                // First item on each continuation slice (slice_idx > 0) needs
                // its top border restored if it was resolved away. The first
                // slice does NOT get an override — `suppress_first_row_top`
                // semantics are preserved there.
                let top_override = if slice_idx > 0 && item_idx == 0 {
                    outer_top_border
                } else {
                    None
                };
                match item {
                    SliceItem::Range(range) => {
                        emit_table_rows(
                            &measured,
                            rows,
                            range.clone(),
                            &mut cursor_y,
                            &mut TableCommandBuffers {
                                commands: &mut commands,
                                content_commands: &mut content_commands,
                                border_commands: &mut border_commands,
                            },
                            top_override,
                        );
                    }
                    SliceItem::Split { row_idx, mr } | SliceItem::Continuation { row_idx, mr } => {
                        let has_next = item_idx + 1 < items.len();
                        emit_split_row(
                            mr,
                            &rows[*row_idx],
                            &mut cursor_y,
                            &mut TableCommandBuffers {
                                commands: &mut commands,
                                content_commands: &mut content_commands,
                                border_commands: &mut border_commands,
                            },
                            top_override,
                            has_next,
                        );
                    }
                }
            }
            commands.append(&mut content_commands);
            commands.append(&mut border_commands);
            TableSlice {
                commands,
                size: PtSize::new(measured.table_width, cursor_y),
            }
        })
        .collect()
}

/// One item inside a page slice's emit list.
enum SliceItem {
    /// Emit the contiguous range of measured rows (normal case).
    Range(std::ops::Range<usize>),
    /// Emit a partial row (first half) at the bottom of a page. Shares the
    /// row's `TableRowInput` with `row_idx` but carries partitioned
    /// commands and modified borders.
    Split {
        row_idx: usize,
        mr: types::MeasuredRow,
    },
    /// Emit the continuation (second half) of a split row at the top of a
    /// continuation page.
    Continuation {
        row_idx: usize,
        mr: types::MeasuredRow,
    },
}

#[cfg(test)]
mod tests {
    use super::super::draw_command::DrawCommand;
    use super::*;
    use crate::render::geometry::PtEdgeInsets;
    use crate::render::layout::fragment::{FontProps, Fragment, TextMetrics};
    use crate::render::layout::paragraph::ParagraphStyle;
    use crate::render::layout::section::LayoutBlock;
    use crate::render::resolve::color::RgbColor;
    use std::rc::Rc;

    fn text_frag(text: &str, width: f32) -> Fragment {
        Fragment::Text {
            text: text.into(),
            font: FontProps {
                family: Rc::from("Test"),
                size: Pt::new(12.0),
                bold: false,
                italic: false,
                underline: false,
                char_spacing: Pt::ZERO,
                underline_position: Pt::ZERO,
                underline_thickness: Pt::ZERO,
            },
            color: RgbColor::BLACK,
            width: Pt::new(width),
            trimmed_width: Pt::new(width),
            metrics: TextMetrics {
                ascent: Pt::new(10.0),
                descent: Pt::new(4.0),
                leading: Pt::ZERO,
            },
            hyperlink_url: None,
            shading: None,
            border: None,
            baseline_offset: Pt::ZERO,
            text_offset: Pt::ZERO,
        }
    }

    fn simple_cell(text: &str) -> TableCellInput {
        TableCellInput {
            blocks: vec![LayoutBlock::Paragraph {
                fragments: vec![text_frag(text, 30.0)],
                style: ParagraphStyle::default(),
                page_break_before: false,
                footnotes: vec![],
                floating_images: vec![],
                floating_shapes: vec![],
            }],
            margins: PtEdgeInsets::ZERO,
            grid_span: 1,
            shading: None,
            cell_borders: None,
            vertical_merge: None,
            vertical_align: CellVAlign::Top,
        }
    }

    fn body_constraints() -> BoxConstraints {
        BoxConstraints::loose(PtSize::new(Pt::new(400.0), Pt::new(1000.0)))
    }

    // ── layout_table ─────────────────────────────────────────────────────

    #[test]
    fn empty_table() {
        let result = layout_table(
            &[],
            &[],
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );
        assert!(result.commands.is_empty());
        assert_eq!(result.size, PtSize::ZERO);
    }

    #[test]
    fn single_cell_table() {
        let rows = vec![TableRowInput {
            cells: vec![simple_cell("hello")],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        let col_widths = vec![Pt::new(200.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        assert_eq!(result.size.width.raw(), 200.0);
        assert_eq!(result.size.height.raw(), 14.0);

        let text_count = result
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(text_count, 1);
    }

    #[test]
    fn two_by_two_table() {
        let rows = vec![
            TableRowInput {
                cells: vec![simple_cell("a"), simple_cell("b")],
                height_rule: None,
                is_header: None,
                cant_split: None,
                grid_before: 0,
                grid_after: 0,
            },
            TableRowInput {
                cells: vec![simple_cell("c"), simple_cell("d")],
                height_rule: None,
                is_header: None,
                cant_split: None,
                grid_before: 0,
                grid_after: 0,
            },
        ];
        let col_widths = vec![Pt::new(100.0), Pt::new(100.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        assert_eq!(result.size.width.raw(), 200.0);
        assert_eq!(result.size.height.raw(), 28.0); // 2 rows * 14pt

        let text_count = result
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(text_count, 4);
    }

    #[test]
    fn row_height_is_max_of_cells() {
        // Cell A has 1 line (14pt), Cell B has 2 lines (28pt) because text wraps
        let rows = vec![TableRowInput {
            cells: vec![
                simple_cell("short"),
                TableCellInput {
                    blocks: vec![LayoutBlock::Paragraph {
                        fragments: vec![text_frag("long ", 60.0), text_frag("text", 60.0)],
                        style: ParagraphStyle::default(),
                        page_break_before: false,
                        footnotes: vec![],
                        floating_images: vec![],
                        floating_shapes: vec![],
                    }],
                    margins: PtEdgeInsets::ZERO,
                    grid_span: 1,
                    shading: None,
                    cell_borders: None,
                    vertical_merge: None,
                    vertical_align: CellVAlign::Top,
                },
            ],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        // Column B is only 80 wide, so "long " + "text" (120) wraps
        let col_widths = vec![Pt::new(200.0), Pt::new(80.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        assert_eq!(result.size.height.raw(), 28.0, "row height = tallest cell");
    }

    #[test]
    fn min_row_height_respected() {
        let rows = vec![TableRowInput {
            cells: vec![simple_cell("x")],
            height_rule: Some(RowHeightRule::AtLeast(Pt::new(40.0))),
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        let col_widths = vec![Pt::new(200.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        assert_eq!(
            result.size.height.raw(),
            40.0,
            "min height > content height"
        );
    }

    #[test]
    fn cell_shading_emits_rect() {
        let rows = vec![TableRowInput {
            cells: vec![TableCellInput {
                blocks: vec![LayoutBlock::Paragraph {
                    fragments: vec![text_frag("x", 10.0)],
                    style: ParagraphStyle::default(),
                    page_break_before: false,
                    footnotes: vec![],
                    floating_images: vec![],
                    floating_shapes: vec![],
                }],
                margins: PtEdgeInsets::ZERO,
                grid_span: 1,
                shading: Some(RgbColor {
                    r: 200,
                    g: 200,
                    b: 200,
                }),
                cell_borders: None,
                vertical_merge: None,
                vertical_align: CellVAlign::Top,
            }],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        let col_widths = vec![Pt::new(100.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        let rect_count = result
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Rect { .. }))
            .count();
        assert_eq!(rect_count, 1, "shading produces a Rect command");
    }

    #[test]
    fn grid_span_widens_cell() {
        let rows = vec![TableRowInput {
            cells: vec![TableCellInput {
                blocks: vec![LayoutBlock::Paragraph {
                    fragments: vec![text_frag("spanning", 30.0)],
                    style: ParagraphStyle::default(),
                    page_break_before: false,
                    footnotes: vec![],
                    floating_images: vec![],
                    floating_shapes: vec![],
                }],
                margins: PtEdgeInsets::ZERO,
                grid_span: 2, // spans both columns
                shading: None,
                cell_borders: None,
                vertical_merge: None,
                vertical_align: CellVAlign::Top,
            }],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        let col_widths = vec![Pt::new(100.0), Pt::new(100.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        // Cell gets full 200pt width, text should still render
        assert_eq!(result.size.width.raw(), 200.0);
        let text_count = result
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(text_count, 1);
    }

    /// Helper: collect all Text command x-positions in command order.
    fn text_x_positions(commands: &[DrawCommand]) -> Vec<f32> {
        commands
            .iter()
            .filter_map(|c| match c {
                DrawCommand::Text { position, .. } => Some(position.x.raw()),
                _ => None,
            })
            .collect()
    }

    #[test]
    fn grid_before_offsets_first_cell_x() {
        // §17.4.17: gridBefore=1 + wBefore skips the first grid column. With
        // a 4-column grid [10, 100, 200, 10] and a row with gridBefore=1 and
        // gridAfter=1, the two cells must occupy columns 1 and 2 — not 0 and 1.
        let rows = vec![TableRowInput {
            cells: vec![simple_cell("A"), simple_cell("B")],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 1,
            grid_after: 1,
        }];
        let col_widths = vec![Pt::new(10.0), Pt::new(100.0), Pt::new(200.0), Pt::new(10.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        let xs = text_x_positions(&result.commands);
        assert_eq!(xs.len(), 2, "two text fragments expected");
        assert_eq!(xs[0], 10.0, "first cell starts at col 1's left edge (10pt)");
        assert_eq!(
            xs[1], 110.0,
            "second cell starts after col 1 (10 + 100 = 110pt)"
        );

        // The table's overall width is unchanged by gridBefore/gridAfter —
        // they only leave whitespace within rows.
        assert_eq!(result.size.width.raw(), 320.0);
    }

    #[test]
    fn grid_after_does_not_overflow() {
        // §17.4.16: gridAfter=2 leaves the rightmost two columns of a 4-column
        // grid empty for this row. Cells must fit within the leftmost two
        // columns and not overflow into the gridAfter region.
        let rows = vec![TableRowInput {
            cells: vec![simple_cell("X"), simple_cell("Y")],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 2,
        }];
        let col_widths = vec![Pt::new(10.0), Pt::new(100.0), Pt::new(200.0), Pt::new(10.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        let xs = text_x_positions(&result.commands);
        assert_eq!(xs.len(), 2);
        assert_eq!(xs[0], 0.0, "first cell at col 0");
        assert_eq!(xs[1], 10.0, "second cell at col 1");
        // The cells together occupy 10 + 100 = 110pt; the right 210pt are
        // empty (gridAfter region). Total table width stays at 320pt.
        assert_eq!(result.size.width.raw(), 320.0);
    }

    #[test]
    fn grid_before_first_cell_uses_inside_v_left_border() {
        // §17.4.17 + §17.4.38: with gridBefore>0, the row's first cell is not
        // at the table's left edge, so its left border must come from
        // `inside_v`, not `left`. Mirror for gridAfter>0 and the right edge.
        //
        // We use distinct border widths to identify which table border was
        // applied: `left`/`right` = 4pt, `inside_v` = 1pt. With grid_before=1
        // and grid_after=1 in a 4-column grid, both cells must use only the
        // 1pt borders for left/right edges; no 4pt border rect should appear.
        let rows = vec![TableRowInput {
            cells: vec![simple_cell("A"), simple_cell("B")],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 1,
            grid_after: 1,
        }];
        let col_widths = vec![Pt::new(10.0), Pt::new(50.0), Pt::new(50.0), Pt::new(10.0)];
        let borders = TableBorderConfig {
            top: None,
            bottom: None,
            left: Some(TableBorderLine {
                width: Pt::new(4.0),
                color: RgbColor::BLACK,
                style: TableBorderStyle::Single,
            }),
            right: Some(TableBorderLine {
                width: Pt::new(4.0),
                color: RgbColor::BLACK,
                style: TableBorderStyle::Single,
            }),
            inside_h: None,
            inside_v: Some(TableBorderLine {
                width: Pt::new(1.0),
                color: RgbColor::BLACK,
                style: TableBorderStyle::Single,
            }),
        };
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            Some(&borders),
            None,
            false,
        );

        // Vertical border rects have width equal to the border thickness
        // (depth) and height >= 1. Find any 4pt-thick border rect.
        let has_thick_vertical = result.commands.iter().any(|c| match c {
            DrawCommand::Rect { rect, color } if *color == RgbColor::BLACK => {
                rect.size.width.raw() == 4.0 && rect.size.height.raw() > 1.0
            }
            _ => false,
        });
        assert!(
            !has_thick_vertical,
            "no 4pt-thick vertical border should appear: gridBefore/gridAfter \
             mean cells aren't at the table's left/right edges, so left/right \
             borders are not applied"
        );

        // Inside_v (1pt) should appear at the boundary between cell A and cell B.
        let has_inside_v = result.commands.iter().any(|c| match c {
            DrawCommand::Rect { rect, color } if *color == RgbColor::BLACK => {
                rect.size.width.raw() == 1.0
            }
            _ => false,
        });
        assert!(
            has_inside_v,
            "1pt inside_v border between cells must appear"
        );
    }

    #[test]
    fn vmerge_across_rows_with_different_grid_before() {
        // §17.4.85 + §17.4.17: a cell at grid_col 1 in row A (gridBefore=1)
        // can merge vertically with a Continue cell at grid_col 1 in row B
        // (gridBefore=0) — the merge is per absolute grid column. Row B's
        // cell at grid_col 0 has no above-cell (it's in row A's gridBefore
        // region) and must layout independently.
        let row_a = TableRowInput {
            cells: vec![
                TableCellInput {
                    blocks: vec![LayoutBlock::Paragraph {
                        fragments: vec![text_frag("Restart", 30.0)],
                        style: ParagraphStyle::default(),
                        page_break_before: false,
                        footnotes: vec![],
                        floating_images: vec![],
                        floating_shapes: vec![],
                    }],
                    margins: PtEdgeInsets::ZERO,
                    grid_span: 1,
                    shading: None,
                    cell_borders: None,
                    vertical_merge: Some(VerticalMergeState::Restart),
                    vertical_align: CellVAlign::Top,
                },
                simple_cell("header"),
            ],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 1,
            grid_after: 0,
        };
        let row_b = TableRowInput {
            cells: vec![
                simple_cell("row1col0"),
                TableCellInput {
                    blocks: vec![],
                    margins: PtEdgeInsets::ZERO,
                    grid_span: 1,
                    shading: None,
                    cell_borders: None,
                    vertical_merge: Some(VerticalMergeState::Continue),
                    vertical_align: CellVAlign::Top,
                },
                simple_cell("row1col2"),
            ],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        };
        let col_widths = vec![Pt::new(50.0), Pt::new(100.0), Pt::new(150.0)];
        let result = layout_table(
            &[row_a, row_b],
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        let position_of = |needle: &str| -> Option<(f32, f32)> {
            result.commands.iter().find_map(|c| match c {
                DrawCommand::Text { position, text, .. } if text.as_ref() == needle => {
                    Some((position.x.raw(), position.y.raw()))
                }
                _ => None,
            })
        };

        let (restart_x, _) = position_of("Restart").expect("Restart text present");
        let (header_x, header_y) = position_of("header").expect("header text present");
        let (col0_x, col0_y) = position_of("row1col0").expect("row1col0 text present");
        let (col2_x, _) = position_of("row1col2").expect("row1col2 text present");

        // Row A respects gridBefore=1: first cell at col 1's left edge (50pt).
        assert_eq!(restart_x, 50.0, "Restart cell starts at grid col 1");
        assert_eq!(header_x, 150.0, "header cell starts at grid col 2 (50+100)");

        // Row B has its own grid_before=0 — the col-0 cell exists and lays
        // out independently. The col-1 cell is a Continue (no content).
        // Row B's col-2 cell is unaffected by the merge.
        assert_eq!(col0_x, 0.0, "row1col0 starts at grid col 0");
        assert_eq!(col2_x, 150.0, "row1col2 starts at grid col 2 (50+100)");

        // Row B's col-0 cell sits in row 2 (y > 0); it's not stretched by
        // the vMerge happening at col 1.
        assert!(col0_y > 0.0, "row1col0 is on the second row");
        assert_eq!(
            col0_y,
            header_y + 14.0,
            "row1col0 sits exactly one row-height below the row 0 header"
        );
    }

    #[test]
    fn cell_margins_affect_layout() {
        let rows = vec![TableRowInput {
            cells: vec![TableCellInput {
                blocks: vec![LayoutBlock::Paragraph {
                    fragments: vec![text_frag("text", 30.0)],
                    style: ParagraphStyle::default(),
                    page_break_before: false,
                    footnotes: vec![],
                    floating_images: vec![],
                    floating_shapes: vec![],
                }],
                margins: PtEdgeInsets::new(
                    Pt::new(5.0),
                    Pt::new(10.0),
                    Pt::new(5.0),
                    Pt::new(10.0),
                ),
                grid_span: 1,
                shading: None,
                cell_borders: None,
                vertical_merge: None,
                vertical_align: CellVAlign::Top,
            }],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        let col_widths = vec![Pt::new(200.0)];
        let result = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        // Row height = content(14) + top(5) + bottom(5) = 24
        assert_eq!(result.size.height.raw(), 24.0);

        // Text should be offset by left margin
        if let Some(DrawCommand::Text { position, .. }) = result.commands.first() {
            assert_eq!(position.x.raw(), 10.0, "left margin");
        }
    }

    #[test]
    fn suppress_first_row_top_removes_top_borders() {
        let border_line = TableBorderLine {
            width: Pt::new(0.5),
            color: RgbColor::BLACK,
            style: TableBorderStyle::Single,
        };
        let borders = TableBorderConfig {
            top: Some(border_line),
            bottom: Some(border_line),
            left: Some(border_line),
            right: Some(border_line),
            inside_h: None,
            inside_v: None,
        };
        let rows = vec![TableRowInput {
            cells: vec![simple_cell("a")],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }];
        let col_widths = vec![Pt::new(100.0)];

        // Without suppression: top border present.
        let normal = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            Some(&borders),
            None,
            false,
        );
        let normal_borders: Vec<_> = normal
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Rect { color, .. } if *color == RgbColor::BLACK))
            .collect();

        // With suppression: top border removed.
        let suppressed = layout_table(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            Some(&borders),
            None,
            true,
        );
        let suppressed_borders: Vec<_> = suppressed
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Rect { color, .. } if *color == RgbColor::BLACK))
            .collect();

        // Normal has 4 borders (top, bottom, left, right).
        assert_eq!(normal_borders.len(), 4, "all 4 borders present");
        // Suppressed has 3 borders (bottom, left, right — no top).
        assert_eq!(suppressed_borders.len(), 3, "top border suppressed");
    }

    #[test]
    fn valign_bottom_on_vmerge_restart_uses_span_height() {
        // §17.4.85 + §17.4.84: vAlign on a vMerge=Restart cell should
        // apply across the whole merged span, not just the first row.
        //
        // Table: 2 rows × 2 cols.
        //  Row 0: [restart "Total", top-align "header"]
        //  Row 1: [continue,         top-align "value"]
        // Default single-line height is 14pt per row → span = 28pt.
        // Restart cell has bottom alignment, content height 14pt → text
        // should sit 14pt below the cell's top, i.e. inside row 1.
        let row0 = TableRowInput {
            cells: vec![
                TableCellInput {
                    blocks: vec![LayoutBlock::Paragraph {
                        fragments: vec![text_frag("Total", 20.0)],
                        style: ParagraphStyle::default(),
                        page_break_before: false,
                        footnotes: vec![],
                        floating_images: vec![],
                        floating_shapes: vec![],
                    }],
                    margins: PtEdgeInsets::ZERO,
                    grid_span: 1,
                    shading: None,
                    cell_borders: None,
                    vertical_merge: Some(VerticalMergeState::Restart),
                    vertical_align: CellVAlign::Bottom,
                },
                simple_cell("header"),
            ],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        };
        let row1 = TableRowInput {
            cells: vec![
                TableCellInput {
                    blocks: vec![],
                    margins: PtEdgeInsets::ZERO,
                    grid_span: 1,
                    shading: None,
                    cell_borders: None,
                    vertical_merge: Some(VerticalMergeState::Continue),
                    vertical_align: CellVAlign::Top,
                },
                simple_cell("value"),
            ],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        };
        let col_widths = vec![Pt::new(100.0), Pt::new(100.0)];
        let result = layout_table(
            &[row0, row1],
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            false,
        );

        // Text position for "Total" comes first (row 0, cell 0).
        let total_y = result
            .commands
            .iter()
            .find_map(|c| match c {
                DrawCommand::Text { position, text, .. } if text.as_ref() == "Total" => {
                    Some(position.y.raw())
                }
                _ => None,
            })
            .expect("Total text present");
        let header_y = result
            .commands
            .iter()
            .find_map(|c| match c {
                DrawCommand::Text { position, text, .. } if text.as_ref() == "header" => {
                    Some(position.y.raw())
                }
                _ => None,
            })
            .expect("header text present");

        // "header" is top-aligned in row 0; "Total" should be bottom-
        // aligned across the 2-row span, so roughly one row-height below.
        assert!(
            total_y > header_y + 10.0,
            "Total (bottom-valigned merged) should sit well below header (top-valigned row 0): \
             total_y={total_y}, header_y={header_y}"
        );
    }

    // ── Row splitting (§17.4.1) ──────────────────────────────────────────

    /// Build a single-row table with one cell whose paragraph contains
    /// `n_lines` narrow text fragments that wrap to separate lines.
    fn tall_row(n_lines: usize) -> TableRowInput {
        // Width=30 for each fragment; cell content width ≈ 40 ⇒ each
        // fragment wraps to its own line (default line height 14pt).
        let fragments: Vec<Fragment> = (0..n_lines)
            .map(|i| text_frag(&format!("L{i} "), 30.0))
            .collect();
        TableRowInput {
            cells: vec![TableCellInput {
                blocks: vec![LayoutBlock::Paragraph {
                    fragments,
                    style: ParagraphStyle::default(),
                    page_break_before: false,
                    footnotes: vec![],
                    floating_images: vec![],
                    floating_shapes: vec![],
                }],
                margins: PtEdgeInsets::ZERO,
                grid_span: 1,
                shading: None,
                cell_borders: None,
                vertical_merge: None,
                vertical_align: CellVAlign::Top,
            }],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        }
    }

    #[test]
    fn splittable_row_breaks_across_pages() {
        // Row with 6 lines (84pt). Available = 50pt on page 1 ⇒ only ~3
        // lines fit. The row should split: first slice has ~3 lines,
        // second slice has the rest.
        let rows = vec![tall_row(6)];
        let col_widths = vec![Pt::new(40.0)];
        let slices = layout_table_paginated(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::new(50.0),
                page_height: Pt::new(200.0),
                suppress_first_row_top: false,
            },
        );

        assert!(
            slices.len() >= 2,
            "expected at least 2 slices, got {}",
            slices.len()
        );

        let text_y = |slice: &TableSlice| -> Vec<f32> {
            slice
                .commands
                .iter()
                .filter_map(|c| match c {
                    DrawCommand::Text { position, .. } => Some(position.y.raw()),
                    _ => None,
                })
                .collect()
        };

        let s0 = text_y(&slices[0]);
        let s1 = text_y(&slices[1]);
        assert!(!s0.is_empty(), "slice 0 should contain some lines");
        assert!(!s1.is_empty(), "slice 1 should contain continuation lines");
        assert_eq!(
            s0.len() + s1.len(),
            6,
            "every line should be emitted exactly once across slices"
        );
        // Slice 1's lines should sit near the top (near y=0) since we
        // rebased them.
        let min_s1_y = s1.iter().copied().fold(f32::INFINITY, f32::min);
        assert!(
            min_s1_y < 20.0,
            "slice 1 top text should be near y=0, was {min_s1_y}"
        );
    }

    #[test]
    fn cant_split_row_moves_whole_to_next_page() {
        // cantSplit=true ⇒ entire row moves when it doesn't fit.
        let mut row = tall_row(6);
        row.cant_split = Some(true);
        let rows = vec![row];
        let col_widths = vec![Pt::new(40.0)];
        let slices = layout_table_paginated(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::new(50.0),
                page_height: Pt::new(200.0),
                suppress_first_row_top: false,
            },
        );

        assert_eq!(slices.len(), 2, "should still produce 2 slices");
        // Slice 0 is empty (or at most has no text); all 6 lines land on slice 1.
        let count0 = slices[0]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        let count1 = slices[1]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(count0, 0, "first slice has no text with cantSplit");
        assert_eq!(count1, 6, "all 6 lines on second slice");
    }

    #[test]
    fn splittable_row_spans_three_or_more_pages() {
        // Row with 15 lines (≈210pt at 14pt line height).
        // Page 1 has 50pt → ~3 lines fit.
        // Page 2 and on have ~70pt → ~5 lines fit each.
        // Expected: 3+ slices, every line emitted exactly once.
        let rows = vec![tall_row(15)];
        let col_widths = vec![Pt::new(40.0)];
        let slices = layout_table_paginated(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::new(50.0),
                page_height: Pt::new(70.0),
                suppress_first_row_top: false,
            },
        );

        assert!(
            slices.len() >= 3,
            "expected ≥3 slices for a 15-line row split across pages, got {}",
            slices.len()
        );

        // Every original line should appear exactly once across all slices.
        let total_lines: usize = slices
            .iter()
            .map(|s| {
                s.commands
                    .iter()
                    .filter(|c| matches!(c, DrawCommand::Text { .. }))
                    .count()
            })
            .sum();
        assert_eq!(
            total_lines, 15,
            "all 15 lines should be emitted across the slices exactly once"
        );
    }

    #[test]
    fn vmerge_span_is_not_split_mid_cell() {
        // A vMerge span must stay atomic even when its content would split.
        let row0 = TableRowInput {
            cells: vec![TableCellInput {
                blocks: vec![LayoutBlock::Paragraph {
                    fragments: (0..4).map(|i| text_frag(&format!("L{i} "), 30.0)).collect(),
                    style: ParagraphStyle::default(),
                    page_break_before: false,
                    footnotes: vec![],
                    floating_images: vec![],
                    floating_shapes: vec![],
                }],
                margins: PtEdgeInsets::ZERO,
                grid_span: 1,
                shading: None,
                cell_borders: None,
                vertical_merge: Some(VerticalMergeState::Restart),
                vertical_align: CellVAlign::Top,
            }],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        };
        let row1 = TableRowInput {
            cells: vec![TableCellInput {
                blocks: vec![],
                margins: PtEdgeInsets::ZERO,
                grid_span: 1,
                shading: None,
                cell_borders: None,
                vertical_merge: Some(VerticalMergeState::Continue),
                vertical_align: CellVAlign::Top,
            }],
            height_rule: None,
            is_header: None,
            cant_split: None,
            grid_before: 0,
            grid_after: 0,
        };
        let col_widths = vec![Pt::new(40.0)];
        let slices = layout_table_paginated(
            &[row0, row1],
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::new(30.0),
                page_height: Pt::new(200.0),
                suppress_first_row_top: false,
            },
        );

        // Should still page: the merge group doesn't fit on page 1, so it
        // moves intact to page 2 — never split mid-cell.
        let count0 = slices[0]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(count0, 0, "vMerge span must not split across pages");
    }

    /// Regression: a table whose every row has `tblHeader=1` (a Word template
    /// pattern, e.g. the trailing "Anhang" tables in the Volvo Annahme-Protokoll)
    /// must still respect `available_height`. The previous implementation
    /// unconditionally added every header row to the first slice, overflowing
    /// the page when the table arrived near the bottom of a stacked page.
    #[test]
    fn all_header_rows_paginate_when_exceeding_available() {
        let mut r0 = tall_row(2);
        r0.is_header = Some(true);
        r0.cant_split = Some(true);
        let mut r1 = tall_row(2);
        r1.is_header = Some(true);
        r1.cant_split = Some(true);
        let mut r2 = tall_row(2);
        r2.is_header = Some(true);
        r2.cant_split = Some(true);

        let rows = vec![r0, r1, r2];
        let col_widths = vec![Pt::new(40.0)];

        // Available 30pt fits at most one row; the remaining rows must spill.
        let slices = layout_table_paginated(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::new(30.0),
                page_height: Pt::new(200.0),
                suppress_first_row_top: false,
            },
        );

        assert!(
            slices.len() >= 2,
            "expected ≥2 slices, got {}",
            slices.len()
        );
        assert!(
            slices[0].size.height <= Pt::new(30.0),
            "slice 0 height {:.1}pt exceeds available 30pt — header rows \
             ignoring the fitting check",
            slices[0].size.height.raw()
        );

        // Each header row appears exactly once — no double-emission, since
        // an all-header table has no "subsequent" rows to head.
        let total_text: usize = slices
            .iter()
            .map(|s| {
                s.commands
                    .iter()
                    .filter(|c| matches!(c, DrawCommand::Text { .. }))
                    .count()
            })
            .sum();
        assert_eq!(total_text, 6, "all rows emitted exactly once");
    }

    /// Regression: when `available_height` is zero (cursor at the page
    /// bottom), an all-header table must produce an empty first slice and
    /// emit content on a fresh continuation page.
    #[test]
    fn all_header_rows_with_no_space_advances_page() {
        let mut r0 = tall_row(2);
        r0.is_header = Some(true);
        r0.cant_split = Some(true);

        let rows = vec![r0];
        let col_widths = vec![Pt::new(40.0)];

        let slices = layout_table_paginated(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::ZERO,
                page_height: Pt::new(200.0),
                suppress_first_row_top: false,
            },
        );

        assert_eq!(slices.len(), 2, "empty first slice + content slice");
        let count0 = slices[0]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        let count1 = slices[1]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(count0, 0, "first slice empty when no space available");
        assert_eq!(count1, 2, "all content moves to continuation");
    }

    /// Regression: when a non-header row triggers a page break, header rows
    /// must still be re-emitted at the top of the continuation slice.
    #[test]
    fn header_repeats_on_continuation_when_body_overflows() {
        let mut header = tall_row(1); // 14pt
        header.is_header = Some(true);
        header.cant_split = Some(true);
        let body0 = tall_row(2); // 28pt
        let mut body1 = tall_row(2); // 28pt
        body1.cant_split = Some(true);

        let rows = vec![header, body0, body1];
        let col_widths = vec![Pt::new(40.0)];

        // Available 50pt: header (14) + body0 (28) = 42 fits; body1 (28) spills.
        let slices = layout_table_paginated(
            &rows,
            &col_widths,
            &body_constraints(),
            Pt::new(14.0),
            None,
            None,
            &TablePaginationConfig {
                available_height: Pt::new(50.0),
                page_height: Pt::new(200.0),
                suppress_first_row_top: false,
            },
        );

        assert_eq!(slices.len(), 2);
        let count0 = slices[0]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        let count1 = slices[1]
            .commands
            .iter()
            .filter(|c| matches!(c, DrawCommand::Text { .. }))
            .count();
        assert_eq!(count0, 3, "slice 0: header (1) + body0 (2)");
        assert_eq!(count1, 3, "slice 1: header repeated (1) + body1 (2)");
    }
}