k2f_layout 0.2.3

K2F deterministic layout compiler
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
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
use crate::Theme;
use crate::{measure_node, Size, SizeConstraint};
use crate::{LayoutContext, LayoutEngine};
use k2f_core::{
    CanvasMode, CellAlign, FixedSizeHint, GridTrack, LayoutHint, Manifest, NodeContent, PageConfig,
    Pt, RunningBlockNode, RunningBlockPosition, SemanticNode,
};

fn make_text(id: &str, content: &str) -> SemanticNode {
    SemanticNode {
        id: id.to_string(),
        role: "body".to_string(),
        variant: None,
        preserve_whitespace: None,
        list_id: None,
        depth: None,
        marker_type: None,
        content: NodeContent::Text(content.to_string()),
        modifiers: vec![],
        layout: None,
        ..Default::default()
    }
}

// Make a container with N text nodes
fn make_content(count: usize) -> SemanticNode {
    let mut children = Vec::new();
    for i in 0..count {
        children.push(make_text(&format!("t{}", i), "Line"));
    }
    SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        variant: None,
        preserve_whitespace: None,
        list_id: None,
        depth: None,
        marker_type: None,
        content: NodeContent::Container { children },
        modifiers: vec![],
        layout: None,
        ..Default::default()
    }
}

#[test]
fn test_pagination_breaks() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    // 1. Create content: 10 lines.
    let root = make_content(10);

    // 2. Compute a deterministic single-line height, then choose a page height
    // that fits exactly 4 items per page (so the expected page count is stable).
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)]; // top, right, bottom, left
    let width = Pt(200_000);
    let tmp_page_config = PageConfig {
        width,
        height: Pt(1_000_000), // placeholder for measurement only
        margin: margins,
    };
    let content_width = crate::pagination::content_width(&tmp_page_config);
    let sample = make_text("sample", "Line");
    let sample_size = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(content_width, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap();
    let line_h = sample_size.height;

    let items_per_page: i128 = 4;
    let page_height = margins[0] + margins[2] + (line_h * items_per_page);
    let page_config = PageConfig {
        width,
        height: page_height,
        margin: margins,
    };

    let manifest = Manifest {
        title: "Test".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config: page_config.clone(),
        root,
        running_blocks: vec![],
    };

    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();

    assert_eq!(
        result.pages.len(),
        3,
        "Expected 3 pages for 10 items @ 4 per page"
    );

    // Verify page/root dimensions align with PageConfig.
    for page in &result.pages {
        assert_eq!(page.width, page_config.width);
        assert_eq!(page.height, page_config.height);
        assert_eq!(page.root.width, page_config.width);
        assert_eq!(page.root.height, page_config.height);
    }

    // Verify explicit distribution (4,4,2) and margin offsets.
    assert_eq!(result.pages[0].root.children.len(), 4);
    assert_eq!(result.pages[1].root.children.len(), 4);
    assert_eq!(result.pages[2].root.children.len(), 2);

    let first = &result.pages[0].root.children[0];
    assert_eq!(first.x, margins[3]);
    assert_eq!(first.y, margins[0]);

    let second = &result.pages[0].root.children[1];
    assert_eq!(second.x, margins[3]);
    assert_eq!(second.y, margins[0] + line_h);

    let first_page_2 = &result.pages[1].root.children[0];
    assert_eq!(first_page_2.x, margins[3]);
    assert_eq!(first_page_2.y, margins[0]);
}

#[test]
fn test_infinite_canvas() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    let root = make_content(10); // ~120pt total

    // Infinite mode should result in 1 page; height must expand deterministically.
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)]; // top, right, bottom, left
    let width = Pt(200_000);
    let tmp_page_config = PageConfig {
        width,
        height: Pt(1_000_000), // placeholder for measurement only
        margin: margins,
    };
    let content_width = crate::pagination::content_width(&tmp_page_config);
    let sample = make_text("sample", "Line");
    let sample_size = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(content_width, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap();
    let line_h = sample_size.height;

    // Intentionally small initial height (fits only 2 items) to force expansion.
    let page_config = PageConfig {
        width,
        height: margins[0] + margins[2] + (line_h * 2),
        margin: margins,
    };

    let manifest = Manifest {
        title: "Test".to_string(),
        canvas_mode: CanvasMode::Infinite,
        page_config: page_config.clone(),
        root,
        running_blocks: vec![],
    };

    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();

    assert_eq!(
        result.pages.len(),
        1,
        "Infinite mode should only have 1 page"
    );
    assert_eq!(result.pages[0].root.children.len(), 10);

    // Page/root dimensions must remain aligned after expansion.
    assert_eq!(result.pages[0].width, page_config.width);
    assert_eq!(result.pages[0].root.width, page_config.width);
    assert_eq!(result.pages[0].height, result.pages[0].root.height);

    let expected_height = margins[0] + (line_h * 10) + margins[2];
    assert_eq!(
        result.pages[0].height, expected_height,
        "Infinite page height should expand to exactly fit content + margins"
    );

    let first = &result.pages[0].root.children[0];
    let last = &result.pages[0].root.children[9];
    assert_eq!(first.x, margins[3]);
    assert_eq!(first.y, margins[0]);
    assert_eq!(last.y, margins[0] + (line_h * 9));
}

#[test]
fn test_running_blocks_injection_does_not_change_pagination() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    // Baseline: 10 lines -> (4,4,2) pagination as in test_pagination_breaks.
    let root = make_content(10);

    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)]; // top, right, bottom, left
    let width = Pt(200_000);
    let tmp_page_config = PageConfig {
        width,
        height: Pt(1_000_000),
        margin: margins,
    };
    let content_width = crate::pagination::content_width(&tmp_page_config);
    let sample = make_text("sample", "Line");
    let sample_size = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(content_width, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap();
    let line_h = sample_size.height;

    let items_per_page: i128 = 4;
    let page_height = margins[0] + margins[2] + (line_h * items_per_page);
    let page_config = PageConfig {
        width,
        height: page_height,
        margin: margins,
    };

    let baseline = Manifest {
        title: "Test".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config: page_config.clone(),
        root: root.clone(),
        running_blocks: vec![],
    };
    let baseline_result = LayoutEngine::layout(&baseline, &ctx).unwrap();

    let with_footer = Manifest {
        title: "Test".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config: page_config.clone(),
        root,
        running_blocks: vec![RunningBlockNode {
            position: RunningBlockPosition::Footer,
            node: make_text("rb_footer", "Page {{page_current}} of {{page_total}}"),
        }],
    };

    let result = LayoutEngine::layout(&with_footer, &ctx).unwrap();

    assert_eq!(result.pages.len(), baseline_result.pages.len());

    // Content distribution should remain identical, plus one extra child per page for the footer.
    assert_eq!(baseline_result.pages[0].root.children.len(), 4);
    assert_eq!(baseline_result.pages[1].root.children.len(), 4);
    assert_eq!(baseline_result.pages[2].root.children.len(), 2);

    assert_eq!(result.pages[0].root.children.len(), 5);
    assert_eq!(result.pages[1].root.children.len(), 5);
    assert_eq!(result.pages[2].root.children.len(), 3);

    // Footer positioning must stay within the bottom margin band, anchored at the band start.
    let footer_y_start = page_config.height - page_config.margin[2];
    for page in &result.pages {
        let footer = page.root.children.last().unwrap();
        assert_eq!(footer.id, "rb_footer");
        assert_eq!(footer.y, footer_y_start);
        assert!(footer.y >= footer_y_start && footer.y <= page.height);
        assert_eq!(footer.x, page_config.margin[3]);
    }
}

fn make_text_role(id: &str, role: &str, content: &str) -> SemanticNode {
    let mut n = make_text(id, content);
    n.role = role.to_string();
    n
}

#[test]
fn keep_with_next_moves_heading_with_body() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    let sample = make_text("sample", "Line");
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)];
    let width = Pt(200_000);
    let tmp = PageConfig {
        width,
        height: Pt(1_000_000),
        margin: margins,
    };
    let cw = crate::pagination::content_width(&tmp);
    let line_h = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(cw, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap()
    .height;

    // Page fits exactly 2 lines. Heading + body is 2 lines; with a filler first,
    // heading would orphan at the bottom without keep_with_next.
    let page_config = PageConfig {
        width,
        height: margins[0] + margins[2] + (line_h * 2),
        margin: margins,
    };

    let mut heading = make_text_role("h", "header", "Title");
    heading.keep_with_next = true;
    let body = make_text("b", "Body");
    let filler = make_text("f", "Fill");

    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![filler, heading, body],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "kwn".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();
    assert_eq!(result.pages.len(), 2);
    let p0: Vec<_> = result.pages[0]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    let p1: Vec<_> = result.pages[1]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    assert_eq!(p0, vec!["f"]);
    assert_eq!(p1, vec!["h", "b"]);
}

#[test]
fn avoid_block_taller_than_page_errors() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);
    let page_config = PageConfig {
        width: Pt(200_000),
        height: Pt(50_000),
        margin: [Pt(5_000); 4],
    };
    let mut img = SemanticNode {
        id: "logo".to_string(),
        role: "body".to_string(),
        content: NodeContent::Image {
            src: "assets/images/logo.png".to_string(),
            width: Pt(80_000),
            height: Pt(80_000),
        },
        ..Default::default()
    };
    img.break_inside = k2f_core::BreakInside::Avoid;
    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![img],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "ov".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let err = LayoutEngine::layout(&manifest, &ctx).unwrap_err();
    assert!(err.contains("UNSPLITTABLE_OVERFLOW"), "got {err}");
}

#[test]
fn nested_unpadded_stack_splits_at_child_boundaries() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    let sample = make_text("sample", "Line");
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)];
    let width = Pt(200_000);
    let tmp = PageConfig {
        width,
        height: Pt(1_000_000),
        margin: margins,
    };
    let cw = crate::pagination::content_width(&tmp);
    let line_h = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(cw, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap()
    .height;
    let page_config = PageConfig {
        width,
        height: margins[0] + margins[2] + (line_h * 2),
        margin: margins,
    };

    let article = SemanticNode {
        id: "article".to_string(),
        role: "body".to_string(),
        content: NodeContent::Container {
            children: vec![
                make_text("a", "Line"),
                make_text("b", "Line"),
                make_text("c", "Line"),
            ],
        },
        ..Default::default()
    };
    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![article],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "nested".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();
    assert_eq!(result.pages.len(), 2);
    let p0: Vec<_> = result.pages[0]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    let p1: Vec<_> = result.pages[1]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    assert_eq!(p0, vec!["a", "b"]);
    assert_eq!(p1, vec!["c"]);
}

#[test]
fn avoid_signature_moves_as_one_block() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    let sample = make_text("sample", "Line");
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)];
    let width = Pt(200_000);
    let tmp = PageConfig {
        width,
        height: Pt(1_000_000),
        margin: margins,
    };
    let cw = crate::pagination::content_width(&tmp);
    let line_h = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(cw, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap()
    .height;
    let page_config = PageConfig {
        width,
        height: margins[0] + margins[2] + (line_h * 2),
        margin: margins,
    };

    let mut signature = SemanticNode {
        id: "signatures".to_string(),
        role: "body".to_string(),
        content: NodeContent::Container {
            children: vec![make_text("sig_a", "Line"), make_text("sig_b", "Line")],
        },
        ..Default::default()
    };
    signature.break_inside = k2f_core::BreakInside::Avoid;
    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![make_text("f", "Line"), signature],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "sig".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();
    assert_eq!(result.pages.len(), 2);
    let p0: Vec<_> = result.pages[0]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    let p1: Vec<_> = result.pages[1]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    assert_eq!(p0, vec!["f"]);
    assert_eq!(p1, vec!["signatures"]);
    assert_eq!(result.pages[1].root.children[0].children.len(), 2);
}

#[test]
fn splittable_body_text_uses_remaining_page_space() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    let sample = make_text("sample", "Line");
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)];
    let width = Pt(200_000);
    let tmp = PageConfig {
        width,
        height: Pt(1_000_000),
        margin: margins,
    };
    let cw = crate::pagination::content_width(&tmp);
    let line_h = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(cw, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap()
    .height;

    // Five body lines fit on one page; filler consumes four.
    let page_config = PageConfig {
        width,
        height: margins[0] + margins[2] + (line_h * 5),
        margin: margins,
    };

    let filler = make_text("f", "A\nB\nC\nD");
    let long_body = make_text(
        "para",
        "word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word",
    );
    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![filler, long_body],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "split".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();
    assert_eq!(result.pages.len(), 2, "paragraph should split across pages");
    assert!(
        !result.pages[0].root.children.is_empty(),
        "page 0 should contain filler and/or paragraph fragment"
    );
    assert!(
        !result.pages[1].root.children.is_empty(),
        "page 1 should contain paragraph continuation"
    );
}

#[test]
fn nested_stack_splits_when_remainder_is_too_small() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);

    let sample = make_text("sample", "Line");
    let margins = [Pt(10_000), Pt(7_000), Pt(15_000), Pt(11_000)];
    let width = Pt(200_000);
    let tmp = PageConfig {
        width,
        height: Pt(1_000_000),
        margin: margins,
    };
    let cw = crate::pagination::content_width(&tmp);
    let line_h = measure_node(
        &sample,
        SizeConstraint::new(Size::ZERO, Size::new(cw, Pt(i128::MAX))),
        &ctx,
    )
    .unwrap()
    .height;

    // Two lines per page. A heading consumes the first; a 4-line inner stack
    // fits on one full page but not in the remaining line.
    let page_config = PageConfig {
        width,
        height: margins[0] + margins[2] + (line_h * 2),
        margin: margins,
    };

    let inner = SemanticNode {
        id: "sec".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![
                make_text("s0", "A"),
                make_text("s1", "B"),
                make_text("s2", "C"),
                make_text("s3", "D"),
            ],
        },
        ..Default::default()
    };
    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![make_text("h", "Title"), inner],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "stack-split".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();
    let p0: Vec<_> = result.pages[0]
        .root
        .children
        .iter()
        .map(|c| c.id.as_str())
        .collect();
    assert!(
        p0.contains(&"h") && p0.contains(&"s0"),
        "inner stack should leave its first child on page 0, not jump whole: {p0:?}"
    );
    assert!(
        !p0.contains(&"s3"),
        "later inner children should continue on the next page: {p0:?}"
    );
    assert!(result.pages.len() >= 2);
}

#[test]
fn break_before_page_starts_on_fresh_page() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);
    let page_config = PageConfig {
        width: Pt(200_000),
        height: Pt(400_000),
        margin: [Pt(10_000); 4],
    };
    let mut second = make_text("b", "Second");
    second.break_before = k2f_core::BreakBefore::Page;
    let root = SemanticNode {
        id: "root".to_string(),
        role: "section".to_string(),
        content: NodeContent::Container {
            children: vec![make_text("a", "First"), second],
        },
        ..Default::default()
    };
    let manifest = Manifest {
        title: "bb".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config,
        root,
        running_blocks: vec![],
    };
    let result = LayoutEngine::layout(&manifest, &ctx).unwrap();
    assert_eq!(result.pages.len(), 2);
    assert_eq!(result.pages[0].root.children.len(), 1);
    assert_eq!(result.pages[0].root.children[0].id, "a");
    assert_eq!(result.pages[1].root.children[0].id, "b");
}

fn paged_manifest(root: SemanticNode) -> Manifest {
    Manifest {
        title: "Test".to_string(),
        canvas_mode: CanvasMode::Paged,
        page_config: PageConfig {
            width: Pt(200_000),
            height: Pt(200_000),
            margin: [Pt(10_000), Pt(10_000), Pt(10_000), Pt(10_000)],
        },
        root,
        running_blocks: vec![],
    }
}

#[test]
fn root_grid_layout_is_an_error() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);
    let mut root = make_content(2);
    root.layout = Some(LayoutHint::Grid {
        columns: vec![GridTrack::Fr { fr: 1 }, GridTrack::Fr { fr: 1 }],
        rows: vec![],
        gap: 0,
        row_gap: None,
        column_gap: None,
        cell_align: Some(CellAlign::default()),
        size: FixedSizeHint::default(),
    });
    let err = LayoutEngine::layout(&paged_manifest(root), &ctx).unwrap_err();
    assert!(err.contains("root layout type grid"), "{err}");
    assert!(err.contains("root.grid"), "{err}");
}

#[test]
fn root_overlay_and_columns_layout_are_errors() {
    let fonts = crate::test_utils::test_fonts();
    let theme = Theme::default();
    let ctx = LayoutContext::new(&fonts, &theme);
    let mut overlay = make_content(2);
    overlay.layout = Some(LayoutHint::Overlay {
        size: FixedSizeHint::default(),
    });
    let err = LayoutEngine::layout(&paged_manifest(overlay), &ctx).unwrap_err();
    assert!(err.contains("root layout type overlay"), "{err}");

    let mut columns = make_content(2);
    columns.layout = Some(LayoutHint::Columns { count: 2, gap: 0 });
    let err = LayoutEngine::layout(&paged_manifest(columns), &ctx).unwrap_err();
    assert!(err.contains("root layout type columns"), "{err}");
}