1use crate::font::registry::FontRegistry;
2use crate::layout::block::{BlockLayout, BlockLayoutParams, layout_block};
3use crate::types::{DecorationKind, DecorationRect};
4
5#[derive(Clone)]
7pub struct TableLayoutParams {
8 pub table_id: usize,
9 pub rows: usize,
10 pub columns: usize,
11 pub column_widths: Vec<f32>,
13 pub border_width: f32,
14 pub cell_spacing: f32,
15 pub cell_padding: f32,
16 pub cells: Vec<CellLayoutParams>,
18}
19
20#[derive(Clone)]
22pub struct CellLayoutParams {
23 pub row: usize,
24 pub column: usize,
25 pub blocks: Vec<BlockLayoutParams>,
26 pub background_color: Option<[f32; 4]>,
27}
28
29#[non_exhaustive]
31pub struct TableLayout {
32 pub table_id: usize,
33 pub y: f32,
35 pub total_height: f32,
37 pub total_width: f32,
39 pub column_xs: Vec<f32>,
41 pub column_content_widths: Vec<f32>,
43 pub row_ys: Vec<f32>,
45 pub row_heights: Vec<f32>,
47 pub cell_layouts: Vec<CellLayout>,
49 pub border_width: f32,
50 pub cell_padding: f32,
51}
52
53#[non_exhaustive]
54pub struct CellLayout {
55 pub row: usize,
56 pub column: usize,
57 pub blocks: Vec<BlockLayout>,
58 pub background_color: Option<[f32; 4]>,
59}
60
61pub fn layout_table(
63 registry: &FontRegistry,
64 params: &TableLayoutParams,
65 available_width: f32,
66 scale_factor: f32,
67 font_scale: f32,
68) -> TableLayout {
69 let cols = params.columns.max(1);
70 let rows = params.rows.max(1);
71 let border = params.border_width;
72 let padding = params.cell_padding;
73 let spacing = params.cell_spacing;
74
75 let total_overhead =
77 border * 2.0 + spacing * (cols as f32 - 1.0).max(0.0) + padding * 2.0 * cols as f32;
78 let content_area = (available_width - total_overhead).max(0.0);
79
80 let column_content_widths =
82 compute_column_widths(¶ms.column_widths, cols, content_area, padding);
83
84 let mut column_xs = Vec::with_capacity(cols);
86 let mut x = border;
87 for (c, &col_w) in column_content_widths.iter().enumerate() {
88 column_xs.push(x + padding);
89 x += padding * 2.0 + col_w;
90 if c < cols - 1 {
91 x += spacing;
92 }
93 }
94 let total_width = x + border;
95
96 let mut cell_layouts = Vec::new();
98 let mut row_heights = vec![0.0f32; rows];
100
101 for cell_params in ¶ms.cells {
102 let r = cell_params.row;
103 let c = cell_params.column;
104 if c >= cols || r >= rows {
105 continue;
106 }
107
108 let cell_width = column_content_widths[c];
109 let mut cell_blocks = Vec::new();
110 let mut cell_height = 0.0f32;
111
112 for block_params in &cell_params.blocks {
113 let block = layout_block(registry, block_params, cell_width, scale_factor, font_scale);
114 cell_height += block.height;
115 cell_blocks.push(block);
116 }
117
118 let mut block_y = 0.0f32;
120 for block in &mut cell_blocks {
121 block.y = block_y;
122 block_y += block.height;
123 }
124
125 row_heights[r] = row_heights[r].max(cell_height);
126
127 cell_layouts.push(CellLayout {
128 row: r,
129 column: c,
130 blocks: cell_blocks,
131 background_color: cell_params.background_color,
132 });
133 }
134
135 let mut row_ys = Vec::with_capacity(rows);
137 let mut y = border;
138 for (r, &row_h) in row_heights.iter().enumerate() {
139 row_ys.push(y + padding);
140 y += padding * 2.0 + row_h;
141 if r < rows - 1 {
142 y += spacing;
143 }
144 }
145 let total_height = y + border;
146
147 TableLayout {
148 table_id: params.table_id,
149 y: 0.0, total_height,
151 total_width,
152 column_xs,
153 column_content_widths,
154 row_ys,
155 row_heights,
156 cell_layouts,
157 border_width: border,
158 cell_padding: padding,
159 }
160}
161
162fn compute_column_widths(
163 specified: &[f32],
164 cols: usize,
165 content_area: f32,
166 _padding: f32,
167) -> Vec<f32> {
168 let default_col_width = if content_area.is_finite() {
172 (content_area / cols as f32).max(1.0)
173 } else {
174 200.0
175 };
176
177 if specified.is_empty() || specified.len() != cols {
178 return vec![default_col_width; cols];
179 }
180
181 let total: f32 = specified.iter().sum();
183 if total <= 0.0 {
184 return vec![default_col_width; cols];
185 }
186
187 specified
188 .iter()
189 .map(|&s| (s / total) * content_area)
190 .collect()
191}
192
193pub fn generate_table_decorations(table: &TableLayout, scroll_offset: f32) -> Vec<DecorationRect> {
195 generate_table_decorations_at(table, 0.0, 0.0, scroll_offset)
196}
197
198pub fn generate_table_decorations_at(
203 table: &TableLayout,
204 offset_x: f32,
205 offset_y: f32,
206 scroll_offset: f32,
207) -> Vec<DecorationRect> {
208 let mut decorations = Vec::new();
209 let table_y = offset_y + table.y - scroll_offset;
210
211 if table.border_width > 0.0 {
213 let bw = table.border_width;
214 let color = [0.6, 0.6, 0.6, 1.0]; decorations.push(DecorationRect {
217 rect: [offset_x, table_y, table.total_width, bw],
218 color,
219 kind: DecorationKind::TableBorder,
220 });
221 decorations.push(DecorationRect {
223 rect: [
224 offset_x,
225 table_y + table.total_height - bw,
226 table.total_width,
227 bw,
228 ],
229 color,
230 kind: DecorationKind::TableBorder,
231 });
232 decorations.push(DecorationRect {
234 rect: [offset_x, table_y, bw, table.total_height],
235 color,
236 kind: DecorationKind::TableBorder,
237 });
238 decorations.push(DecorationRect {
240 rect: [
241 offset_x + table.total_width - bw,
242 table_y,
243 bw,
244 table.total_height,
245 ],
246 color,
247 kind: DecorationKind::TableBorder,
248 });
249
250 for r in 1..table.row_ys.len() {
252 let row_y = table.row_ys[r] - table.cell_padding;
253 decorations.push(DecorationRect {
254 rect: [offset_x, table_y + row_y - bw / 2.0, table.total_width, bw],
255 color,
256 kind: DecorationKind::TableBorder,
257 });
258 }
259
260 for c in 1..table.column_xs.len() {
262 let col_x = table.column_xs[c] - table.cell_padding;
263 decorations.push(DecorationRect {
264 rect: [offset_x + col_x - bw / 2.0, table_y, bw, table.total_height],
265 color,
266 kind: DecorationKind::TableBorder,
267 });
268 }
269 }
270
271 for cell in &table.cell_layouts {
273 if let Some(bg_color) = cell.background_color
274 && cell.row < table.row_ys.len()
275 && cell.column < table.column_xs.len()
276 {
277 let cx = table.column_xs[cell.column] - table.cell_padding;
278 let cy = table.row_ys[cell.row] - table.cell_padding;
279 let cw = table.column_content_widths[cell.column] + table.cell_padding * 2.0;
280 let ch = table.row_heights[cell.row] + table.cell_padding * 2.0;
281 decorations.push(DecorationRect {
282 rect: [offset_x + cx, table_y + cy, cw, ch],
283 color: bg_color,
284 kind: DecorationKind::TableCellBackground,
285 });
286 }
287 }
288
289 decorations
290}