lightweight_pdf_core/
table.rs1use crate::element::Element;
7use crate::style::{Align, Color, Common};
8
9#[derive(Clone, Copy, Debug)]
14pub enum ColumnWidth {
15 Fixed(f32),
16 Flex(f32),
17}
18
19#[derive(Clone, Copy, Debug)]
20pub struct TableColumn {
21 pub width: ColumnWidth,
22 pub align: Align,
23}
24
25impl TableColumn {
26 pub fn fixed(width: f32) -> Self {
27 TableColumn {
28 width: ColumnWidth::Fixed(width),
29 align: Align::Start,
30 }
31 }
32
33 pub fn flex(weight: f32) -> Self {
34 TableColumn {
35 width: ColumnWidth::Flex(weight),
36 align: Align::Start,
37 }
38 }
39
40 pub fn align(mut self, align: Align) -> Self {
41 self.align = align;
42 self
43 }
44}
45
46#[derive(Clone, Debug, Default)]
47pub struct Table {
48 pub columns: Vec<TableColumn>,
49 pub header: Option<Vec<Element>>,
50 pub rows: Vec<Vec<Element>>,
51 pub striped: Option<Color>,
55 pub cell_padding: f32,
58 pub row_offset: usize,
64 pub common: Common,
65}
66
67impl Table {
68 pub fn new() -> Self {
69 Table {
70 cell_padding: 4.0,
71 ..Default::default()
72 }
73 }
74
75 pub fn columns(mut self, columns: impl IntoIterator<Item = TableColumn>) -> Self {
76 self.columns = columns.into_iter().collect();
77 self
78 }
79
80 pub fn header(mut self, cells: impl IntoIterator<Item = impl Into<Element>>) -> Self {
81 self.header = Some(cells.into_iter().map(Into::into).collect());
82 self
83 }
84
85 pub fn rows(mut self, rows: impl IntoIterator<Item = impl IntoIterator<Item = impl Into<Element>>>) -> Self {
86 self.rows = rows.into_iter().map(|row| row.into_iter().map(Into::into).collect()).collect();
87 self
88 }
89
90 pub fn striped(mut self, color: Color) -> Self {
91 self.striped = Some(color);
92 self
93 }
94
95 pub fn cell_padding(mut self, padding: f32) -> Self {
96 self.cell_padding = padding;
97 self
98 }
99
100 pub fn width(mut self, width: f32) -> Self {
101 self.common.width = Some(width);
102 self
103 }
104
105 pub fn height(mut self, height: f32) -> Self {
106 self.common.height = Some(height);
107 self
108 }
109
110 pub fn flex(mut self, factor: f32) -> Self {
111 self.common.flex = Some(factor);
112 self
113 }
114
115 pub fn keep_with_next(mut self) -> Self {
116 self.common.keep_with_next = true;
117 self
118 }
119}