add elements

This commit is contained in:
2026-04-03 01:26:54 +03:00
parent 1675d2611c
commit d7abf10dd0
31 changed files with 3600 additions and 177 deletions

View File

@@ -51,6 +51,8 @@ fn simple_template() -> Template {
height: 297.0,
},
fonts: vec!["Noto Sans".to_string()],
header: None,
footer: None,
root: ContainerElement {
id: "root".to_string(),
position: PositionMode::Flow,
@@ -66,6 +68,7 @@ fn simple_template() -> Template {
align: "stretch".to_string(),
justify: "start".to_string(),
style: ContainerStyle::default(),
break_inside: "auto".to_string(),
children: vec![TemplateElement::StaticText(StaticTextElement {
id: "title".to_string(),
position: PositionMode::Flow,
@@ -118,6 +121,8 @@ fn test_render_pdf_with_multiple_elements() {
height: 297.0,
},
fonts: vec!["Noto Sans".to_string()],
header: None,
footer: None,
root: ContainerElement {
id: "root".to_string(),
position: PositionMode::Flow,
@@ -133,6 +138,7 @@ fn test_render_pdf_with_multiple_elements() {
align: "stretch".to_string(),
justify: "start".to_string(),
style: ContainerStyle::default(),
break_inside: "auto".to_string(),
children: vec![
TemplateElement::StaticText(StaticTextElement {
id: "header".to_string(),
@@ -207,6 +213,8 @@ fn test_render_pdf_with_container_styles() {
height: 297.0,
},
fonts: vec!["Noto Sans".to_string()],
header: None,
footer: None,
root: ContainerElement {
id: "root".to_string(),
position: PositionMode::Flow,
@@ -227,6 +235,7 @@ fn test_render_pdf_with_container_styles() {
border_width: Some(1.0),
..Default::default()
},
break_inside: "auto".to_string(),
children: vec![TemplateElement::StaticText(StaticTextElement {
id: "text".to_string(),
position: PositionMode::Flow,
@@ -254,3 +263,79 @@ fn test_render_pdf_with_container_styles() {
assert!(!pdf_bytes.is_empty());
assert!(pdf_bytes.starts_with(b"%PDF"));
}
#[test]
fn test_page_break_produces_multiple_pages() {
let template = Template {
id: "pb_test".to_string(),
name: "Page Break Test".to_string(),
page: PageSettings { width: 210.0, height: 297.0 },
fonts: vec!["Noto Sans".to_string()],
header: None,
footer: None,
root: ContainerElement {
id: "root".to_string(),
position: PositionMode::Flow,
size: SizeConstraint::default(),
direction: "column".to_string(),
gap: 5.0,
padding: Padding { top: 15.0, right: 15.0, bottom: 15.0, left: 15.0 },
align: "stretch".to_string(),
justify: "start".to_string(),
style: ContainerStyle::default(),
break_inside: "auto".to_string(),
children: vec![
TemplateElement::StaticText(StaticTextElement {
id: "t1".to_string(),
position: PositionMode::Flow,
size: SizeConstraint { width: SizeValue::Fr { value: 1.0 }, height: SizeValue::Auto, ..Default::default() },
style: TextStyle { font_size: Some(18.0), ..Default::default() },
content: "Page 1 content".to_string(),
}),
TemplateElement::PageBreak(PageBreakElement { id: "pb1".to_string() }),
TemplateElement::StaticText(StaticTextElement {
id: "t2".to_string(),
position: PositionMode::Flow,
size: SizeConstraint { width: SizeValue::Fr { value: 1.0 }, height: SizeValue::Auto, ..Default::default() },
style: TextStyle { font_size: Some(18.0), ..Default::default() },
content: "Page 2 content".to_string(),
}),
],
},
};
let data = serde_json::json!({});
let fonts = load_test_fonts();
let layout = compute_layout(&template, &data, &fonts);
println!("Layout pages: {}", layout.pages.len());
for (i, page) in layout.pages.iter().enumerate() {
println!("Page {}: {} elements", i, page.elements.len());
for el in &page.elements {
println!(" - {} (type={}, y={:.1}mm, h={:.1}mm)", el.id, el.element_type, el.y_mm, el.height_mm);
}
}
assert_eq!(layout.pages.len(), 2, "Page break should produce 2 pages");
// Verify page 1 has t1 and page 2 has t2
let p1_ids: Vec<&str> = layout.pages[0].elements.iter().map(|e| e.id.as_str()).collect();
let p2_ids: Vec<&str> = layout.pages[1].elements.iter().map(|e| e.id.as_str()).collect();
println!("Page 1 IDs: {:?}", p1_ids);
println!("Page 2 IDs: {:?}", p2_ids);
assert!(p1_ids.contains(&"t1"), "Page 1 should contain t1");
assert!(p2_ids.contains(&"t2"), "Page 2 should contain t2");
// Render PDF and verify it's valid
let pdf_bytes = dreport_layout::pdf_render::render_pdf(&layout, &fonts).unwrap();
assert!(pdf_bytes.starts_with(b"%PDF"));
// Write PDF for manual inspection
let out_path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
.parent().unwrap()
.join("test_page_break.pdf");
std::fs::write(&out_path, &pdf_bytes).unwrap();
println!("Wrote: {}", out_path.display());
}