rhwp 0.9.1

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
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
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
use super::*;
use crate::model::control::FormType;
use crate::model::shape::TextWrap;
use crate::renderer::composer::CharOverlapInfo;
use crate::renderer::equation::layout::{LayoutBox, LayoutKind};
use crate::renderer::render_tree::{
    BoundingBox, EllipseNode, EquationNode, FieldMarkerType, FootnoteMarkerNode, FormObjectNode,
    ImageNode, LineNode, PageBackgroundNode, PageNode, PathNode, PlaceholderNode, RawSvgNode,
    RectangleNode, RenderLayerInfo, RenderNode, RenderNodeType, TableCellNode, TableNode,
    TextLineNode, TextRunNode,
};
use crate::renderer::{LineStyle, PathCommand, ShapeStyle, TabLeaderInfo, TextStyle};

#[test]
fn builds_body_clip_layer() {
    let mut tree = PageRenderTree::new(0, 800.0, 600.0);
    tree.root.node_type = RenderNodeType::Page(PageNode {
        page_index: 0,
        width: 800.0,
        height: 600.0,
        section_index: 0,
    });
    let body = RenderNode::new(
        1,
        RenderNodeType::Body {
            clip_rect: Some(BoundingBox::new(10.0, 20.0, 300.0, 400.0)),
        },
        BoundingBox::new(10.0, 20.0, 300.0, 400.0),
    );
    tree.root.children.push(body);

    let mut builder = LayerBuilder::new(RenderProfile::Screen);
    let layer_tree = builder.build(&tree);

    assert_eq!(layer_tree.page_width, 800.0);
    match &layer_tree.root.kind {
        LayerNodeKind::Group { children, .. } => {
            assert_eq!(children.len(), 1);
            match &children[0].kind {
                LayerNodeKind::ClipRect {
                    clip, clip_kind, ..
                } => {
                    assert_eq!(clip.x, 10.0);
                    assert_eq!(*clip_kind, ClipKind::Body);
                }
                other => panic!("expected clip rect, got {other:?}"),
            }
        }
        other => panic!("expected root group, got {other:?}"),
    }
}

#[test]
fn normal_builder_export_keeps_text_fallback_and_font_native_sidecar_resources() {
    let bbox = BoundingBox::new(10.0, 20.0, 16.0, 16.0);
    let mut run = text_run("\u{E100}");
    run.char_shape_id = Some(2);
    run.style.font_size = 16.0;
    run.baseline = 12.0;
    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    tree.root
        .children
        .push(RenderNode::new(1, RenderNodeType::TextRun(run), bbox));
    let font = include_bytes!("../../../tests/fixtures/fonts/RHWPBitmapSvgGlyphSmoke.ttf");
    let mut builder = LayerBuilder::new(RenderProfile::Screen);

    let layer_tree = builder.build_with_embedded_fonts(
        &tree,
        &[EmbeddedFontFace {
            char_shape_id: 2,
            language_index: 0,
            family: "RHWP Bitmap SVG Glyph Smoke",
            alternate_family: None,
            bytes: font,
            face_index: 0,
        }],
    );

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    let LayerNodeKind::Leaf { ops } = &children[0].kind else {
        panic!("expected text leaf");
    };
    assert!(matches!(
        ops.as_slice(),
        [
            PaintOp::TextRun { .. },
            PaintOp::GlyphRun { .. },
            PaintOp::GlyphOutline { .. }
        ]
    ));
    assert_eq!(layer_tree.resources.image_count(), 1);
    assert_eq!(layer_tree.resources.font_blob_count(), 1);
    assert_eq!(layer_tree.resources.font_resources().blobs.len(), 1);
    assert_eq!(layer_tree.resources.font_resources().faces.len(), 1);
}

#[test]
fn builder_keeps_oversized_compact_image_unresolved() {
    let mut bmp = vec![0u8; 58];
    bmp[..2].copy_from_slice(b"BM");
    bmp[2..6].copy_from_slice(&58u32.to_le_bytes());
    bmp[10..14].copy_from_slice(&54u32.to_le_bytes());
    bmp[14..18].copy_from_slice(&40u32.to_le_bytes());
    bmp[18..22].copy_from_slice(&8193i32.to_le_bytes());
    bmp[22..26].copy_from_slice(&8193i32.to_le_bytes());
    bmp[26..28].copy_from_slice(&1u16.to_le_bytes());
    bmp[28..30].copy_from_slice(&8u16.to_le_bytes());
    bmp[30..34].copy_from_slice(&1u32.to_le_bytes());
    bmp[54..58].copy_from_slice(&[0, 1, 0, 1]);
    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    tree.root.children.push(RenderNode::new(
        1,
        RenderNodeType::Image(ImageNode::new(1, Some(bmp))),
        BoundingBox::new(0.0, 0.0, 10.0, 10.0),
    ));

    let layer_tree = LayerBuilder::new(RenderProfile::Screen).build(&tree);

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    let LayerNodeKind::Leaf { ops } = &children[0].kind else {
        panic!("expected image leaf");
    };
    assert!(matches!(
        ops.as_slice(),
        [PaintOp::Image { resolved: None, .. }]
    ));
}

#[test]
fn copies_render_layer_metadata_to_layer_node() {
    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    let layer = RenderLayerInfo::new(Some(TextWrap::BehindText), 7, 42);
    let table = RenderNode::new(
        1,
        RenderNodeType::Table(TableNode {
            row_count: 1,
            col_count: 1,
            border_fill_id: 0,
            section_index: Some(0),
            para_index: Some(0),
            control_index: Some(0),
            cell_context: None,
        }),
        BoundingBox::new(0.0, 0.0, 10.0, 10.0),
    )
    .with_layer(layer);
    tree.root.children.push(table);

    let mut builder = LayerBuilder::new(RenderProfile::Screen);
    let layer_tree = builder.build(&tree);
    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    let copied = children[0].layer.expect("table layer copied");

    assert_eq!(copied.text_wrap, Some(TextWrap::BehindText));
    assert_eq!(copied.z_order, 7);
    assert_eq!(copied.stable_index, 42);
}

#[test]
fn builds_textbox_clip_layer() {
    let mut tree = PageRenderTree::new(0, 800.0, 600.0);
    let mut textbox = RenderNode::new(
        7,
        RenderNodeType::TextBox,
        BoundingBox::new(50.0, 80.0, 240.0, 120.0),
    );
    textbox.children.push(RenderNode::new(
        8,
        RenderNodeType::TextRun(text_run("글상자")),
        BoundingBox::new(60.0, 90.0, 60.0, 20.0),
    ));
    tree.root.children.push(textbox);

    let mut builder = LayerBuilder::new(RenderProfile::Screen);
    let layer_tree = builder.build(&tree);

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    assert_eq!(children.len(), 1);

    let LayerNodeKind::ClipRect {
        clip,
        clip_kind,
        child,
    } = &children[0].kind
    else {
        panic!("expected textbox clip");
    };
    assert_eq!(*clip_kind, ClipKind::TextBox);
    assert_eq!(clip.x, 50.0);
    assert_eq!(clip.y, 80.0);
    assert_eq!(clip.width, 240.0);
    assert_eq!(clip.height, 120.0);

    let LayerNodeKind::Group {
        group_kind,
        children: textbox_children,
        ..
    } = &child.kind
    else {
        panic!("expected clipped textbox group");
    };
    assert!(matches!(group_kind, GroupKind::TextBox));
    assert!(matches!(
        &textbox_children[0].kind,
        LayerNodeKind::Leaf { .. }
    ));
}

#[test]
fn preserves_leaf_payloads() {
    let mut tree = PageRenderTree::new(0, 800.0, 600.0);
    tree.root.children.push(RenderNode::new(
        1,
        RenderNodeType::PageBackground(PageBackgroundNode {
            background_color: Some(0x00FFFFFF),
            border_color: None,
            border_width: 0.0,
            gradient: None,
            image: None,
        }),
        BoundingBox::new(0.0, 0.0, 800.0, 600.0),
    ));
    tree.root.children.push(RenderNode::new(
        2,
        RenderNodeType::TableCell(TableCellNode {
            col: 0,
            row: 0,
            col_span: 1,
            row_span: 1,
            border_fill_id: 0,
            text_direction: 0,
            clip: true,
            closed_reflow_frame: false,
            page_fragment: false,
            model_cell_index: None,
            model_para_range: None,
        }),
        BoundingBox::new(100.0, 200.0, 150.0, 80.0),
    ));

    let mut builder = LayerBuilder::new(RenderProfile::Screen);
    let layer_tree = builder.build(&tree);

    match &layer_tree.root.kind {
        LayerNodeKind::Group { children, .. } => {
            assert_eq!(children.len(), 2);
            match &children[0].kind {
                LayerNodeKind::Leaf { ops } => {
                    assert!(matches!(ops[0], PaintOp::PageBackground { .. }));
                }
                other => panic!("expected leaf, got {other:?}"),
            }
            match &children[1].kind {
                LayerNodeKind::ClipRect { clip_kind, .. } => {
                    assert_eq!(*clip_kind, ClipKind::TableCell);
                }
                other => panic!("expected clip rect, got {other:?}"),
            }
        }
        other => panic!("expected root group, got {other:?}"),
    }
}

#[test]
fn lowers_all_leaf_variants_to_explicit_paint_ops() {
    let cases: Vec<(RenderNodeType, fn(&PaintOp) -> bool, &'static str)> = vec![
        (
            RenderNodeType::PageBackground(PageBackgroundNode {
                background_color: Some(0x00FFFFFF),
                border_color: None,
                border_width: 0.0,
                gradient: None,
                image: None,
            }),
            |op| matches!(op, PaintOp::PageBackground { .. }),
            "PageBackground",
        ),
        (
            RenderNodeType::TextRun(text_run("leaf")),
            |op| matches!(op, PaintOp::TextRun { .. }),
            "TextRun",
        ),
        (
            RenderNodeType::FootnoteMarker(FootnoteMarkerNode {
                source_run: None,
                number: 1,
                text: "1)".to_string(),
                base_font_size: 12.0,
                baseline: 7.0,
                font_family: "serif".to_string(),
                color: 0x00000000,
                section_index: 0,
                para_index: 0,
                control_index: 0,
            }),
            |op| matches!(op, PaintOp::FootnoteMarker { .. }),
            "FootnoteMarker",
        ),
        (
            RenderNodeType::Line(LineNode::new(0.0, 0.0, 12.0, 0.0, LineStyle::default())),
            |op| matches!(op, PaintOp::Line { .. }),
            "Line",
        ),
        (
            RenderNodeType::Rectangle(RectangleNode::new(0.0, ShapeStyle::default(), None)),
            |op| matches!(op, PaintOp::Rectangle { .. }),
            "Rectangle",
        ),
        (
            RenderNodeType::Ellipse(EllipseNode::new(ShapeStyle::default(), None)),
            |op| matches!(op, PaintOp::Ellipse { .. }),
            "Ellipse",
        ),
        (
            RenderNodeType::Path(PathNode::new(
                vec![PathCommand::MoveTo(0.0, 0.0), PathCommand::LineTo(8.0, 4.0)],
                ShapeStyle::default(),
                None,
            )),
            |op| matches!(op, PaintOp::Path { .. }),
            "Path",
        ),
        (
            RenderNodeType::Image(ImageNode::new(1, Some(vec![0x89, b'P', b'N', b'G']))),
            |op| matches!(op, PaintOp::Image { .. }),
            "Image",
        ),
        (
            RenderNodeType::Equation(equation_node()),
            |op| matches!(op, PaintOp::Equation { .. }),
            "Equation",
        ),
        (
            RenderNodeType::FormObject(form_object_node()),
            |op| matches!(op, PaintOp::FormObject { .. }),
            "FormObject",
        ),
        (
            RenderNodeType::Placeholder(PlaceholderNode::new(
                0x00F0F0F0,
                0x00000000,
                "OLE".to_string(),
            )),
            |op| matches!(op, PaintOp::Placeholder { .. }),
            "Placeholder",
        ),
        (
            RenderNodeType::RawSvg(RawSvgNode::new("<g><path d=\"M0 0L1 1\"/></g>".to_string())),
            |op| matches!(op, PaintOp::RawSvg { .. }),
            "RawSvg",
        ),
    ];

    for (idx, (node_type, is_expected_op, label)) in cases.into_iter().enumerate() {
        let mut tree = PageRenderTree::new(0, 100.0, 100.0);
        tree.root.children.push(RenderNode::new(
            100 + idx as u32,
            node_type,
            BoundingBox::new(1.0, 2.0, 30.0, 20.0),
        ));

        let mut builder = LayerBuilder::new(RenderProfile::Screen);
        let layer_tree = builder.build(&tree);

        let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
            panic!("expected root group for {label}");
        };
        let LayerNodeKind::Leaf { ops } = &children[0].kind else {
            panic!("expected leaf for {label}, got {:?}", children[0].kind);
        };
        assert_eq!(ops.len(), 1, "{label} should lower to one paint op");
        assert!(is_expected_op(&ops[0]), "{label} lowered to {:?}", ops[0]);
    }
}

#[test]
fn preserves_leaf_node_children_after_own_paint_op() {
    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    let mut form = RenderNode::new(
        1,
        RenderNodeType::FormObject(form_object_node()),
        BoundingBox::new(10.0, 10.0, 60.0, 24.0),
    );
    form.children.push(RenderNode::new(
        2,
        RenderNodeType::TextRun(text_run("label")),
        BoundingBox::new(14.0, 14.0, 40.0, 16.0),
    ));
    tree.root.children.push(form);

    let mut builder = LayerBuilder::new(RenderProfile::Screen);
    let layer_tree = builder.build(&tree);

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    let LayerNodeKind::Group {
        children: form_children,
        ..
    } = &children[0].kind
    else {
        panic!("expected form node with children to lower to a group");
    };
    assert_eq!(form_children.len(), 2);

    let LayerNodeKind::Leaf { ops: form_ops } = &form_children[0].kind else {
        panic!("expected own form paint op first");
    };
    assert!(matches!(form_ops[0], PaintOp::FormObject { .. }));

    let LayerNodeKind::Leaf { ops: label_ops } = &form_children[1].kind else {
        panic!("expected child text paint op second");
    };
    assert!(matches!(label_ops[0], PaintOp::TextRun { .. }));
}

#[test]
fn lowers_text_special_visuals_to_external_paint_ops() {
    let mut run = text_run("special\ttext");
    run.field_marker = FieldMarkerType::FieldBegin;
    run.is_para_end = true;
    run.style.tab_leaders.push(TabLeaderInfo {
        start_x: 12.0,
        end_x: 36.0,
        fill_type: 3,
    });
    run.style.underline = UnderlineType::Bottom;
    run.style.strikethrough = true;
    run.style.emphasis_dot = 2;

    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    tree.root.children.push(RenderNode::new(
        1,
        RenderNodeType::TextRun(run),
        BoundingBox::new(1.0, 2.0, 80.0, 20.0),
    ));

    let mut builder =
        LayerBuilder::new(RenderProfile::Screen).with_output_options(LayerOutputOptions {
            show_paragraph_marks: true,
            show_control_codes: true,
            ..Default::default()
        });
    let layer_tree = builder.build(&tree);

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    let LayerNodeKind::Leaf { ops } = &children[0].kind else {
        panic!("expected text leaf");
    };

    assert!(matches!(ops[0], PaintOp::TextRun { .. }));
    assert!(!ops
        .iter()
        .any(|op| matches!(op, PaintOp::CharOverlap { .. })));
    assert!(ops
        .iter()
        .any(|op| matches!(op, PaintOp::TextControlMark { .. })));
    assert!(ops.iter().any(|op| matches!(op, PaintOp::TabLeader { .. })));
    assert!(ops.iter().any(|op| matches!(
        op,
        PaintOp::TextDecoration {
            kind: TextDecorationKind::Underline,
            ..
        }
    )));
    assert!(ops.iter().any(|op| matches!(
        op,
        PaintOp::TextDecoration {
            kind: TextDecorationKind::Strikethrough,
            ..
        }
    )));
    assert!(ops.iter().any(|op| matches!(
        op,
        PaintOp::TextDecoration {
            kind: TextDecorationKind::EmphasisDot,
            ..
        }
    )));
}

#[test]
fn lowers_space_and_tab_control_marks_without_an_end_marker() {
    let run = text_run("space\tmark");
    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    tree.root.children.push(RenderNode::new(
        1,
        RenderNodeType::TextRun(run),
        BoundingBox::new(1.0, 2.0, 80.0, 20.0),
    ));

    let mut builder =
        LayerBuilder::new(RenderProfile::Screen).with_output_options(LayerOutputOptions {
            show_control_codes: true,
            ..Default::default()
        });
    let layer_tree = builder.build(&tree);

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    let LayerNodeKind::Leaf { ops } = &children[0].kind else {
        panic!("expected text leaf");
    };
    assert!(ops
        .iter()
        .any(|op| matches!(op, PaintOp::TextControlMark { .. })));
}

#[test]
fn either_view_option_preserves_legacy_text_mark_union_semantics() {
    let mut paragraph_run = text_run("plain");
    paragraph_run.is_para_end = true;
    let paragraph_ops = text_run_ops(
        BoundingBox::new(0.0, 0.0, 40.0, 10.0),
        paragraph_run,
        LayerOutputOptions {
            show_control_codes: true,
            ..Default::default()
        },
    );
    assert!(paragraph_ops
        .iter()
        .any(|op| matches!(op, PaintOp::TextControlMark { .. })));

    let space_ops = text_run_ops(
        BoundingBox::new(0.0, 0.0, 40.0, 10.0),
        text_run("a b"),
        LayerOutputOptions {
            show_paragraph_marks: true,
            ..Default::default()
        },
    );
    assert!(space_ops
        .iter()
        .any(|op| matches!(op, PaintOp::TextControlMark { .. })));
}

#[test]
fn overlap_runs_do_not_gain_normal_text_decorations_or_tab_leaders() {
    let mut run = text_run("overlap");
    run.char_overlap = Some(CharOverlapInfo {
        border_type: 1,
        inner_char_size: 90,
        composition: None,
        empty_ink: false,
    });
    run.style.tab_leaders.push(TabLeaderInfo {
        start_x: 2.0,
        end_x: 20.0,
        fill_type: 1,
    });
    run.style.underline = UnderlineType::Bottom;
    run.style.strikethrough = true;
    run.style.emphasis_dot = 1;

    let ops = text_run_ops(
        BoundingBox::new(0.0, 0.0, 40.0, 10.0),
        run,
        LayerOutputOptions::default(),
    );

    assert!(ops
        .iter()
        .any(|op| matches!(op, PaintOp::CharOverlap { .. })));
    assert!(!ops.iter().any(|op| matches!(op, PaintOp::TabLeader { .. })));
    assert!(!ops
        .iter()
        .any(|op| matches!(op, PaintOp::TextDecoration { .. })));
}

#[test]
fn excludes_editor_only_nodes_from_print_profile() {
    let editor_line = RenderNode::new(
        1,
        RenderNodeType::Line(crate::renderer::render_tree::LineNode::new(
            0.0,
            0.0,
            20.0,
            0.0,
            crate::renderer::LineStyle::default(),
        )),
        BoundingBox::new(0.0, 0.0, 20.0, 1.0),
    )
    .with_editor_only();

    let mut tree = PageRenderTree::new(0, 100.0, 100.0);
    tree.root.children.push(editor_line);

    let screen = LayerBuilder::new(RenderProfile::Screen).build(&tree);
    let print = LayerBuilder::new(RenderProfile::Print).build(&tree);

    let LayerNodeKind::Group {
        children: screen_children,
        ..
    } = &screen.root.kind
    else {
        panic!("expected screen root group");
    };
    let LayerNodeKind::Group {
        children: print_children,
        ..
    } = &print.root.kind
    else {
        panic!("expected print root group");
    };

    assert_eq!(screen_children.len(), 1);
    assert!(print_children.is_empty());
}

#[test]
fn preserves_structural_groups_and_clips_for_backend_replay() {
    let mut tree = PageRenderTree::new(0, 800.0, 600.0);
    tree.root.node_type = RenderNodeType::Page(PageNode {
        page_index: 0,
        width: 800.0,
        height: 600.0,
        section_index: 0,
    });

    let mut header = RenderNode::new(
        1,
        RenderNodeType::Header,
        BoundingBox::new(0.0, 0.0, 800.0, 80.0),
    );
    let mut header_line = RenderNode::new(
        2,
        RenderNodeType::TextLine(TextLineNode::with_para_vpos(20.0, 15.0, 0, 0, 0, 120)),
        BoundingBox::new(40.0, 20.0, 200.0, 20.0),
    );
    header_line.children.push(RenderNode::new(
        3,
        RenderNodeType::TextRun(text_run("머리말")),
        BoundingBox::new(40.0, 20.0, 60.0, 20.0),
    ));
    header.children.push(header_line);

    let footer = RenderNode::new(
        4,
        RenderNodeType::Footer,
        BoundingBox::new(0.0, 540.0, 800.0, 60.0),
    );

    let body_clip = BoundingBox::new(40.0, 90.0, 720.0, 420.0);
    let mut body = RenderNode::new(
        5,
        RenderNodeType::Body {
            clip_rect: Some(body_clip),
        },
        body_clip,
    );
    let mut column = RenderNode::new(
        6,
        RenderNodeType::Column(1),
        BoundingBox::new(40.0, 90.0, 340.0, 420.0),
    );
    let mut table = RenderNode::new(
        7,
        RenderNodeType::Table(TableNode {
            row_count: 1,
            col_count: 1,
            border_fill_id: 2,
            section_index: Some(0),
            para_index: Some(2),
            control_index: Some(0),
            cell_context: None,
        }),
        BoundingBox::new(60.0, 110.0, 180.0, 80.0),
    );
    let mut clipped_cell = RenderNode::new(
        8,
        RenderNodeType::TableCell(TableCellNode {
            col: 0,
            row: 0,
            col_span: 1,
            row_span: 1,
            border_fill_id: 3,
            text_direction: 0,
            clip: true,
            closed_reflow_frame: false,
            page_fragment: false,
            model_cell_index: Some(4),
            model_para_range: None,
        }),
        BoundingBox::new(60.0, 110.0, 180.0, 80.0),
    );
    let mut nested_table = RenderNode::new(
        9,
        RenderNodeType::Table(TableNode {
            row_count: 1,
            col_count: 1,
            border_fill_id: 5,
            section_index: Some(0),
            para_index: Some(2),
            control_index: Some(1),
            cell_context: None,
        }),
        BoundingBox::new(70.0, 120.0, 80.0, 40.0),
    );
    nested_table.children.push(RenderNode::new(
        10,
        RenderNodeType::TableCell(TableCellNode {
            col: 0,
            row: 0,
            col_span: 1,
            row_span: 1,
            border_fill_id: 6,
            text_direction: 0,
            clip: false,
            closed_reflow_frame: false,
            page_fragment: false,
            model_cell_index: None,
            model_para_range: None,
        }),
        BoundingBox::new(70.0, 120.0, 80.0, 40.0),
    ));
    clipped_cell.children.push(nested_table);
    table.children.push(clipped_cell);
    column.children.push(table);
    body.children.push(column);

    tree.root.children.push(header);
    tree.root.children.push(footer);
    tree.root.children.push(body);

    let mut builder = LayerBuilder::new(RenderProfile::Screen);
    let layer_tree = builder.build(&tree);

    let LayerNodeKind::Group { children, .. } = &layer_tree.root.kind else {
        panic!("expected root group");
    };
    assert_eq!(children.len(), 3);

    let LayerNodeKind::Group {
        group_kind,
        cache_hint,
        children: header_children,
    } = &children[0].kind
    else {
        panic!("expected header group");
    };
    assert!(matches!(group_kind, GroupKind::Header));
    assert_eq!(*cache_hint, CacheHint::StaticSubtree);
    let LayerNodeKind::Group {
        group_kind,
        children: line_children,
        ..
    } = &header_children[0].kind
    else {
        panic!("expected text line group");
    };
    match group_kind {
        GroupKind::TextLine(line) => {
            assert_eq!(line.para_index, Some(0));
            assert_eq!(line.vpos, Some(120));
        }
        other => panic!("expected text line group kind, got {other:?}"),
    }
    assert!(matches!(&line_children[0].kind, LayerNodeKind::Leaf { .. }));

    let LayerNodeKind::Group {
        group_kind,
        cache_hint,
        ..
    } = &children[1].kind
    else {
        panic!("expected footer group");
    };
    assert!(matches!(group_kind, GroupKind::Footer));
    assert_eq!(*cache_hint, CacheHint::StaticSubtree);

    let LayerNodeKind::ClipRect {
        clip,
        clip_kind,
        child,
    } = &children[2].kind
    else {
        panic!("expected body clip");
    };
    assert_eq!(clip.x, body_clip.x);
    assert_eq!(clip.y, body_clip.y);
    assert_eq!(clip.width, body_clip.width);
    assert_eq!(clip.height, body_clip.height);
    assert_eq!(*clip_kind, ClipKind::Body);

    let LayerNodeKind::Group {
        group_kind,
        children: body_children,
        ..
    } = &child.kind
    else {
        panic!("expected clipped body group");
    };
    assert!(matches!(group_kind, GroupKind::Body));

    let LayerNodeKind::Group {
        group_kind,
        children: column_children,
        ..
    } = &body_children[0].kind
    else {
        panic!("expected column group");
    };
    assert!(matches!(group_kind, GroupKind::Column(1)));

    let LayerNodeKind::Group {
        group_kind,
        children: table_children,
        ..
    } = &column_children[0].kind
    else {
        panic!("expected table group");
    };
    match group_kind {
        GroupKind::Table(table) => {
            assert_eq!(table.row_count, 1);
            assert_eq!(table.col_count, 1);
            assert_eq!(table.control_index, Some(0));
        }
        other => panic!("expected table group kind, got {other:?}"),
    }

    let LayerNodeKind::ClipRect {
        clip_kind, child, ..
    } = &table_children[0].kind
    else {
        panic!("expected table cell clip");
    };
    assert_eq!(*clip_kind, ClipKind::TableCell);

    let LayerNodeKind::Group {
        group_kind,
        children: cell_children,
        ..
    } = &child.kind
    else {
        panic!("expected clipped table cell group");
    };
    match group_kind {
        GroupKind::TableCell(cell) => {
            assert!(cell.clip);
            assert_eq!(cell.model_cell_index, Some(4));
        }
        other => panic!("expected table cell group kind, got {other:?}"),
    }
    assert!(matches!(
        &cell_children[0].kind,
        LayerNodeKind::Group {
            group_kind: GroupKind::Table(_),
            ..
        }
    ));
}

fn text_run(text: &str) -> TextRunNode {
    TextRunNode {
        text: text.to_string(),
        style: TextStyle::default(),
        char_shape_id: None,
        para_shape_id: None,
        section_index: None,
        para_index: None,
        char_start: None,
        cell_context: None,
        is_para_end: false,
        is_line_break_end: false,
        rotation: 0.0,
        is_vertical: false,
        char_overlap: None,
        border_fill_id: 0,
        baseline: 12.0,
        field_marker: Default::default(),
        layout_positions: None,
        display_text: None,
    }
}

fn equation_node() -> EquationNode {
    EquationNode {
        svg_content: "<text>x</text>".to_string(),
        layout_box: LayoutBox {
            font_size: 12.0,
            x: 0.0,
            y: 0.0,
            width: 8.0,
            height: 12.0,
            baseline: 10.0,
            kind: LayoutKind::Text("x".to_string()),
        },
        color_str: "#000000".to_string(),
        color: 0x00000000,
        font_size: 12.0,
        script: String::new(),
        section_index: None,
        para_index: None,
        control_index: None,
        cell_index: None,
        cell_para_index: None,
        note_ref: None,
    }
}

fn form_object_node() -> FormObjectNode {
    FormObjectNode {
        form_type: FormType::PushButton,
        caption: "OK".to_string(),
        text: String::new(),
        display_text: None,
        appearance: Default::default(),
        fore_color: "#000000".to_string(),
        back_color: "#ffffff".to_string(),
        value: 0,
        enabled: true,
        section_index: 0,
        para_index: 0,
        control_index: 0,
        name: "button".to_string(),
        cell_location: None,
    }
}