use super::border_rendering::border_width_to_px;
use crate::model::header_footer::MasterPage;
use crate::model::page::ColumnDef;
use crate::renderer::page_layout::{calculate_column_areas, LayoutRect, PageLayoutInfo};
use crate::renderer::render_tree::LineNode;
use crate::renderer::{hwpunit_to_px, LineStyle, StrokeDash};
pub(super) fn master_page_lines(
master: &MasterPage,
definition: &ColumnDef,
page: &PageLayoutInfo,
dpi: f64,
) -> Vec<LineNode> {
if master.text_width == 0 || master.text_height == 0 {
return Vec::new();
}
let frame = LayoutRect {
x: page.body_area.x,
y: page.body_area.y,
width: hwpunit_to_px(master.text_width as i32, dpi),
height: hwpunit_to_px(master.text_height as i32, dpi),
};
let mut layout = page.clone();
layout.body_area = frame;
layout.column_areas = calculate_column_areas(&frame, definition, dpi);
layout.separator_type = definition.separator_type;
layout.separator_width = definition.separator_width;
layout.separator_color = definition.separator_color;
separator_lines(&layout, frame.y, frame.y + frame.height)
}
pub(super) fn separator_lines(layout: &PageLayoutInfo, y_start: f64, y_end: f64) -> Vec<LineNode> {
let y_end = y_end.min(layout.body_area.y + layout.body_area.height);
if layout.column_areas.len() < 2 || layout.separator_type == 0 || y_end <= y_start {
return Vec::new();
}
let width = border_width_to_px(layout.separator_width).max(0.5);
let dash = match layout.separator_type {
2 | 6 => StrokeDash::Dash,
3 | 7 => StrokeDash::Dot,
4 => StrokeDash::DashDot,
5 => StrokeDash::DashDotDot,
_ => StrokeDash::Solid,
};
layout
.column_areas
.windows(2)
.map(|pair| {
let (left, right) = if pair[0].x <= pair[1].x {
(&pair[0], &pair[1])
} else {
(&pair[1], &pair[0])
};
let x = (left.x + left.width + right.x) / 2.0;
LineNode::new(
x,
y_start,
x,
y_end,
LineStyle {
color: layout.separator_color,
width,
dash,
..Default::default()
},
)
})
.collect()
}