use crate::area::AreaId;
use fop_types::Length;
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TableLayoutMode {
Auto,
#[default]
Fixed,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BorderCollapse {
#[default]
Separate,
Collapse,
}
impl fmt::Display for BorderCollapse {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BorderCollapse::Separate => write!(f, "separate"),
BorderCollapse::Collapse => write!(f, "collapse"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ColumnWidth {
Fixed(Length),
Proportional(f64),
Auto,
}
#[derive(Debug, Clone)]
pub struct ColumnInfo {
pub width_spec: ColumnWidth,
pub computed_width: Length,
pub min_width: Length,
pub max_width: Length,
}
impl ColumnInfo {
pub fn new(width_spec: ColumnWidth) -> Self {
Self {
width_spec,
computed_width: Length::ZERO,
min_width: Length::ZERO,
max_width: Length::ZERO,
}
}
pub fn with_widths(width_spec: ColumnWidth, min_width: Length, max_width: Length) -> Self {
Self {
width_spec,
computed_width: Length::ZERO,
min_width,
max_width,
}
}
}
#[derive(Debug, Clone)]
pub struct GridCell {
pub row: usize,
pub col: usize,
pub rowspan: usize,
pub colspan: usize,
pub content_id: Option<AreaId>,
}
#[derive(Debug, Clone, Copy)]
pub struct CollapsedBorder {
pub width: Length,
pub color: fop_types::Color,
pub style: crate::area::BorderStyle,
}
impl CollapsedBorder {
pub fn new(width: Length, color: fop_types::Color, style: crate::area::BorderStyle) -> Self {
Self {
width,
color,
style,
}
}
pub fn none() -> Self {
Self {
width: Length::ZERO,
color: fop_types::Color::BLACK,
style: crate::area::BorderStyle::None,
}
}
pub fn is_visible(&self) -> bool {
self.width > Length::ZERO
&& !matches!(
self.style,
crate::area::BorderStyle::None | crate::area::BorderStyle::Hidden
)
}
}
pub struct TableLayout {
pub(super) available_width: Length,
pub(super) border_spacing: Length,
pub(super) layout_mode: TableLayoutMode,
pub(super) border_collapse: BorderCollapse,
}
impl TableLayout {
pub fn new(available_width: Length) -> Self {
Self {
available_width,
border_spacing: Length::from_pt(2.0),
layout_mode: TableLayoutMode::Fixed,
border_collapse: BorderCollapse::Separate,
}
}
pub fn with_border_spacing(mut self, spacing: Length) -> Self {
self.border_spacing = spacing;
self
}
pub fn with_layout_mode(mut self, mode: TableLayoutMode) -> Self {
self.layout_mode = mode;
self
}
pub fn with_border_collapse(mut self, collapse: BorderCollapse) -> Self {
self.border_collapse = collapse;
self
}
pub fn layout_mode(&self) -> TableLayoutMode {
self.layout_mode
}
pub fn border_collapse(&self) -> BorderCollapse {
self.border_collapse
}
}
#[derive(Debug, Clone, Copy)]
pub struct CellCollapsedBorders {
pub top: CollapsedBorder,
pub right: CollapsedBorder,
pub bottom: CollapsedBorder,
pub left: CollapsedBorder,
}
impl Default for CellCollapsedBorders {
fn default() -> Self {
Self {
top: CollapsedBorder::none(),
right: CollapsedBorder::none(),
bottom: CollapsedBorder::none(),
left: CollapsedBorder::none(),
}
}
}