lightweight-pdf-layout 0.1.0

Layoutable trait, pagination and text wrapping for lightweight-pdf
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
//! `Table` layout (Phase 3, `plan/phases/phase-3-tables.md`): fixed/flex
//! column widths, cell content reuses the ordinary `Layoutable` machinery
//! (word-wrap included), row height auto-grows with the tallest cell
//! (Grundprinzip 5), a row is never split mid-row (atomic unit), the
//! header repeats on every continuation page.

use crate::geometry::{Constraints, Rect, Size};
use crate::layoutable::{LayoutCtx, LayoutResult, Layoutable};
use crate::render_node::{align_offset, RenderNode};
use crate::warnings::{LayoutWarning, LayoutWarningKind};
use lightweight_pdf_core::{Color, ColumnWidth, Common, Element, Table, TableColumn};

const EPS: f32 = 0.01;

/// Fixed columns keep their exact width; the leftover space is shared
/// proportionally among flex columns by weight (taffy `flex-grow`
/// analogy). The last flex column absorbs any float-rounding remainder so
/// the widths sum *exactly* to `available_width` (phase-3 DoD).
fn resolve_column_widths(columns: &[TableColumn], available_width: f32) -> Vec<f32> {
    let fixed_total: f32 = columns
        .iter()
        .filter_map(|c| match c.width {
            ColumnWidth::Fixed(w) => Some(w),
            ColumnWidth::Flex(_) => None,
        })
        .sum();
    let flex_sum: f32 = columns
        .iter()
        .filter_map(|c| match c.width {
            ColumnWidth::Flex(w) => Some(w),
            ColumnWidth::Fixed(_) => None,
        })
        .sum();
    let leftover = (available_width - fixed_total).max(0.0);

    let mut widths = Vec::with_capacity(columns.len());
    let mut last_flex_idx = None;
    for (i, c) in columns.iter().enumerate() {
        match c.width {
            ColumnWidth::Fixed(w) => widths.push(w),
            ColumnWidth::Flex(weight) => {
                widths.push(if flex_sum > 0.0 { leftover * (weight / flex_sum) } else { 0.0 });
                last_flex_idx = Some(i);
            }
        }
    }
    if let Some(i) = last_flex_idx {
        let sum: f32 = widths.iter().sum();
        widths[i] += available_width - sum;
    }
    widths
}

fn measure_row_height(ctx: &LayoutCtx, cells: &[Element], col_widths: &[f32], cell_padding: f32) -> f32 {
    cells
        .iter()
        .zip(col_widths.iter())
        .map(|(cell, w)| {
            let inner_w = (w - 2.0 * cell_padding).max(0.0);
            cell.measure(
                ctx,
                Constraints {
                    max_width: inner_w,
                    max_height: f32::INFINITY,
                },
            )
            .height
                + 2.0 * cell_padding
        })
        .fold(0.0f32, f32::max)
}

/// The smallest worthwhile placement for a `Table` when there's already
/// other content on the page (used by `Column`'s "is it worth starting
/// here" check, mirroring `Text`'s per-line granularity instead of
/// treating a whole table as one atomic block).
pub fn table_min_unit(ctx: &LayoutCtx, table: &Table, width: f32) -> f32 {
    let col_widths = resolve_column_widths(&table.columns, (width - 2.0 * table.common.padding).max(0.0));
    let header_h = table
        .header
        .as_ref()
        .map(|h| measure_row_height(ctx, h, &col_widths, table.cell_padding))
        .unwrap_or(0.0);
    let first_row_h = table
        .rows
        .first()
        .map(|r| measure_row_height(ctx, r, &col_widths, table.cell_padding))
        .unwrap_or(0.0);
    header_h + first_row_h
}

#[allow(clippy::too_many_arguments)]
fn layout_row_cells(
    ctx: &LayoutCtx,
    cells: &[Element],
    columns: &[TableColumn],
    col_widths: &[f32],
    row_area: Rect,
    cell_padding: f32,
    warnings: &mut Vec<LayoutWarning>,
    page: usize,
) -> Vec<RenderNode> {
    let mut nodes = Vec::with_capacity(cells.len());
    let mut cursor_x = row_area.x;
    for ((cell, col), w) in cells.iter().zip(columns.iter()).zip(col_widths.iter()) {
        let inner_w = (w - 2.0 * cell_padding).max(0.0);
        let content_h = (row_area.height - 2.0 * cell_padding).max(0.0);
        let cell_size = cell.measure(
            ctx,
            Constraints {
                max_width: inner_w,
                max_height: f32::INFINITY,
            },
        );
        let box_width = cell_size.width.min(inner_w).max(0.0);
        let x_offset = align_offset(col.align, inner_w, box_width);
        let cell_area = Rect {
            x: cursor_x + cell_padding + x_offset,
            y: row_area.y + cell_padding,
            width: box_width,
            height: content_h,
        };
        match cell.layout(ctx, cell_area, warnings, page) {
            LayoutResult::Fit(node) => nodes.push(node),
            LayoutResult::Split { current, .. } => {
                // Cells never split (a row is an atomic unit,
                // Grundprinzip 5's table addendum) — keep what fit,
                // ContentOverflow already implied by TextClipped from the
                // cell's own fixed-size handling if applicable.
                nodes.push(current);
            }
        }
        cursor_x += *w;
    }
    nodes
}

#[allow(clippy::too_many_arguments)]
fn render_row(
    ctx: &LayoutCtx,
    table: &Table,
    cells: &[Element],
    col_widths: &[f32],
    y: f32,
    inner: &Rect,
    row_height: f32,
    background: Option<Color>,
    warnings: &mut Vec<LayoutWarning>,
    page: usize,
) -> RenderNode {
    let row_area = Rect {
        x: inner.x,
        y: inner.y + y,
        width: inner.width,
        height: row_height,
    };
    let nodes = layout_row_cells(ctx, cells, &table.columns, col_widths, row_area, table.cell_padding, warnings, page);
    RenderNode::Group {
        area: row_area,
        clip: true,
        background,
        border: None,
        children: nodes,
    }
}

impl Layoutable for Table {
    fn measure(&self, ctx: &LayoutCtx, constraints: Constraints) -> Size {
        let width = self.common.width.unwrap_or(constraints.max_width);
        let inner_width = (width - 2.0 * self.common.padding).max(0.0);
        let col_widths = resolve_column_widths(&self.columns, inner_width);
        let mut total = 0.0f32;
        if let Some(header) = &self.header {
            total += measure_row_height(ctx, header, &col_widths, self.cell_padding);
        }
        for row in &self.rows {
            total += measure_row_height(ctx, row, &col_widths, self.cell_padding);
        }
        Size {
            width,
            height: self.common.height.unwrap_or(total + 2.0 * self.common.padding),
        }
    }

    fn layout(&self, ctx: &LayoutCtx, area: Rect, warnings: &mut Vec<LayoutWarning>, page: usize) -> LayoutResult {
        let inner = area.shrink(self.common.padding);
        let col_widths = resolve_column_widths(&self.columns, inner.width);
        let bound_height = self.common.height.map(|h| h - 2.0 * self.common.padding).unwrap_or(inner.height);
        let header_height = self
            .header
            .as_ref()
            .map(|h| measure_row_height(ctx, h, &col_widths, self.cell_padding))
            .unwrap_or(0.0);

        let mut rendered = Vec::new();
        let mut cursor_y = 0.0f32;

        if let Some(header) = &self.header {
            rendered.push(render_row(
                ctx,
                self,
                header,
                &col_widths,
                cursor_y,
                &inner,
                header_height,
                None,
                warnings,
                page,
            ));
            cursor_y += header_height;
        }

        for (i, row) in self.rows.iter().enumerate() {
            let absolute_i = self.row_offset + i;
            let row_height = measure_row_height(ctx, row, &col_widths, self.cell_padding);
            let remaining = (bound_height - cursor_y).max(0.0);
            let stripe = self.striped.filter(|_| absolute_i % 2 == 1);

            if row_height <= remaining + EPS {
                rendered.push(render_row(
                    ctx,
                    self,
                    row,
                    &col_widths,
                    cursor_y,
                    &inner,
                    row_height,
                    stripe,
                    warnings,
                    page,
                ));
                cursor_y += row_height;
                continue;
            }

            if cursor_y <= header_height + EPS {
                // Only the header (or nothing) placed so far: this row is
                // atomic and doesn't fit even a fresh page — force it,
                // clip, warn (Grundprinzip 7), then move on.
                rendered.push(render_row(
                    ctx,
                    self,
                    row,
                    &col_widths,
                    cursor_y,
                    &inner,
                    remaining,
                    stripe,
                    warnings,
                    page,
                ));
                warnings.push(LayoutWarning {
                    kind: LayoutWarningKind::ForcedPageBreak,
                    page,
                    element_hint: format!("Table row {absolute_i} larger than one page"),
                });
                cursor_y = bound_height;
                continue;
            }

            // Doesn't fit — move this row and everything after it to a
            // continuation page, which repeats the header.
            if let Some(fixed_height) = self.common.height {
                if !self.rows[i..].is_empty() {
                    warnings.push(LayoutWarning {
                        kind: LayoutWarningKind::ContentOverflow,
                        page,
                        element_hint: "Table content exceeds its fixed height".to_string(),
                    });
                }
                return LayoutResult::Fit(RenderNode::Group {
                    area: Rect {
                        height: fixed_height,
                        ..area
                    },
                    clip: true,
                    background: self.common.background,
                    border: self.common.border,
                    children: rendered,
                });
            }

            let remainder = Table {
                columns: self.columns.clone(),
                header: self.header.clone(),
                rows: self.rows[i..].to_vec(),
                striped: self.striped,
                cell_padding: self.cell_padding,
                row_offset: absolute_i,
                common: Common {
                    height: None,
                    ..self.common
                },
            };
            let current = RenderNode::Group {
                area: Rect { height: cursor_y, ..area },
                clip: true,
                background: self.common.background,
                border: self.common.border,
                children: rendered,
            };
            return LayoutResult::Split {
                current,
                remainder: Element::Table(remainder),
            };
        }

        let outer_height = self.common.height.unwrap_or(cursor_y + 2.0 * self.common.padding);
        LayoutResult::Fit(RenderNode::Group {
            area: Rect {
                height: outer_height,
                ..area
            },
            clip: true,
            background: self.common.background,
            border: self.common.border,
            children: rendered,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::warnings::LayoutWarningKind;
    use lightweight_pdf_core::{Align, Element, Text as TextEl};

    struct FixedMetrics;
    impl crate::font_resolver::FontMetrics for FixedMetrics {
        fn advance(&self, ch: char) -> f32 {
            if ch == ' ' {
                300.0
            } else {
                600.0
            }
        }
        fn ascent(&self) -> f32 {
            800.0
        }
        fn descent(&self) -> f32 {
            -200.0
        }
    }
    struct FixedResolver;
    impl crate::font_resolver::FontResolver for FixedResolver {
        fn metrics(&self, _key: lightweight_pdf_core::FontKey) -> &dyn crate::font_resolver::FontMetrics {
            &FixedMetrics
        }
    }
    fn ctx() -> LayoutCtx<'static> {
        LayoutCtx { resolver: &FixedResolver }
    }

    fn row(cells: &[&str]) -> Vec<Element> {
        cells.iter().map(|c| Element::Text(TextEl::new(*c))).collect()
    }

    #[test]
    fn column_widths_sum_exactly_to_available_width() {
        let columns = vec![
            TableColumn::flex(1.0),
            TableColumn::fixed(37.3),
            TableColumn::flex(2.0),
            TableColumn::fixed(19.9),
        ];
        let widths = resolve_column_widths(&columns, 400.0);
        let sum: f32 = widths.iter().sum();
        assert!((sum - 400.0).abs() < 1e-3, "widths must sum exactly to available width, got {sum}");
        assert_eq!(widths[1], 37.3);
        assert_eq!(widths[3], 19.9);
    }

    #[test]
    fn header_repeats_and_all_rows_survive_a_page_split() {
        let table = Table::new()
            .columns([TableColumn::flex(1.0)])
            .header(["Beschreibung"])
            .rows((0..20).map(|i| row(&[Box::leak(format!("Zeile {i}").into_boxed_str())])));
        let c = ctx();
        let mut warnings = Vec::new();
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: 200.0,
            height: 60.0, // room for header + a couple of rows only
        };
        let mut pages = Vec::new();
        let mut current = Element::Table(table);
        loop {
            match current.layout(&c, area, &mut warnings, pages.len() + 1) {
                LayoutResult::Fit(node) => {
                    pages.push(node);
                    break;
                }
                LayoutResult::Split { current: node, remainder } => {
                    pages.push(node);
                    current = remainder;
                }
            }
            if pages.len() > 100 {
                panic!("pagination did not terminate");
            }
        }
        assert!(pages.len() > 1, "expected the table to span multiple pages");

        // Every page (after the first) must repeat the header as its
        // first row, and every original data row must appear exactly
        // once across all pages, in order.
        let mut seen_rows = Vec::new();
        for page in &pages {
            let RenderNode::Group { children, .. } = page else {
                panic!("expected a Group");
            };
            assert!(!children.is_empty(), "every page must render at least the header");
            for row_node in children {
                let RenderNode::Group { children: cells, .. } = row_node else {
                    panic!("expected row Group");
                };
                let RenderNode::Group { children: text_wrap, .. } = &cells[0] else {
                    panic!("expected clipped text wrapper");
                };
                let RenderNode::TextLines { lines, .. } = &text_wrap[0] else {
                    panic!("expected TextLines");
                };
                seen_rows.push(lines.join(" "));
            }
        }
        let header_count = seen_rows.iter().filter(|s| *s == "Beschreibung").count();
        assert_eq!(header_count, pages.len(), "header must repeat on every page exactly once");
        let data_rows: Vec<_> = seen_rows.iter().filter(|s| *s != "Beschreibung").collect();
        assert_eq!(data_rows.len(), 20, "no row may be lost or duplicated across the split");
        for (i, row) in data_rows.iter().enumerate() {
            assert_eq!(*row, &format!("Zeile {i}"), "rows must stay in order");
        }
    }

    #[test]
    fn cell_hard_breaks_a_token_wider_than_the_column() {
        let table = Table::new().columns([TableColumn::fixed(30.0)]).rows([row(&["ABCDEFGHIJ"])]); // 10 chars * 6pt = 60pt, column inner width ~22pt
        let c = ctx();
        let mut warnings = Vec::new();
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: 30.0,
            height: 200.0,
        };
        let LayoutResult::Fit(RenderNode::Group { children, .. }) = Element::Table(table).layout(&c, area, &mut warnings, 1) else {
            panic!("expected Fit");
        };
        let RenderNode::Group { children: cells, .. } = &children[0] else {
            panic!("expected row group");
        };
        let RenderNode::Group { children: text_wrap, .. } = &cells[0] else {
            panic!("expected clipped text wrapper");
        };
        let RenderNode::TextLines { lines, .. } = &text_wrap[0] else {
            panic!("expected TextLines");
        };
        assert!(lines.len() > 1, "a token wider than the column must hard-break onto multiple lines");
    }

    #[test]
    fn row_height_grows_with_tallest_cell_without_moving_other_rows() {
        let table = Table::new().columns([TableColumn::fixed(30.0), TableColumn::fixed(30.0)]).rows([
            row(&["kurz", "kurz"]),
            row(&["ein sehr sehr sehr sehr langer Zellinhalt der umbricht", "kurz"]),
            row(&["kurz", "kurz"]),
        ]);
        let c = ctx();
        let mut warnings = Vec::new();
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: 60.0,
            height: 400.0,
        };
        let LayoutResult::Fit(RenderNode::Group { children: rows, .. }) = Element::Table(table).layout(&c, area, &mut warnings, 1) else {
            panic!("expected Fit");
        };
        assert_eq!(rows.len(), 3);
        let heights: Vec<f32> = rows
            .iter()
            .map(|r| match r {
                RenderNode::Group { area, .. } => area.height,
                _ => panic!("expected Group"),
            })
            .collect();
        assert!(heights[1] > heights[0], "the row with more content must be taller");
        assert_eq!(heights[0], heights[2], "unrelated rows keep their own (equal) height");

        // Rows must not overlap vertically: each row's y must be >= the
        // previous row's y + height.
        let ys: Vec<f32> = rows
            .iter()
            .map(|r| match r {
                RenderNode::Group { area, .. } => area.y,
                _ => unreachable!(),
            })
            .collect();
        assert!(ys[1] >= ys[0] + heights[0] - EPS);
        assert!(ys[2] >= ys[1] + heights[1] - EPS);
    }

    #[test]
    fn striped_alternates_and_survives_a_split() {
        let table = Table::new()
            .columns([TableColumn::flex(1.0)])
            .header(["H"])
            .striped(Color::rgb(240, 240, 240))
            .rows((0..6).map(|i| row(&[Box::leak(format!("R{i}").into_boxed_str())])));
        let c = ctx();
        let mut warnings = Vec::new();
        // Force a split after the header + 2 rows (each ~22.4pt: 14.4pt
        // line height + 2*4pt cell padding).
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: 100.0,
            height: 3.5 * 22.4,
        };
        let LayoutResult::Split { remainder, .. } = Element::Table(table).layout(&c, area, &mut warnings, 1) else {
            panic!("expected a Split");
        };
        let Element::Table(remainder_table) = remainder else {
            panic!("expected Table remainder");
        };
        // Row 2 (0-indexed) is the first row on the continuation page;
        // row_offset must reflect its true absolute index so striping
        // continues correctly instead of resetting.
        assert_eq!(remainder_table.row_offset, 2);
    }

    #[test]
    fn oversized_row_forces_its_own_page() {
        let table = Table::new()
            .columns([TableColumn::flex(1.0)])
            .rows([row(&["normal"]), row(&["a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\np"])]);
        let c = ctx();
        let mut warnings = Vec::new();
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: 100.0,
            height: 100.0,
        };
        let mut pages = 0;
        let mut current = Element::Table(table);
        loop {
            match current.layout(&c, area, &mut warnings, pages + 1) {
                LayoutResult::Fit(_) => {
                    pages += 1;
                    break;
                }
                LayoutResult::Split { remainder, .. } => {
                    pages += 1;
                    current = remainder;
                }
            }
            if pages > 50 {
                panic!("pagination did not terminate");
            }
        }
        assert!(pages >= 2, "the oversized row should push onto its own page");
        assert!(warnings.iter().any(|w| w.kind == LayoutWarningKind::ForcedPageBreak));
    }

    #[test]
    fn table_column_align_positions_short_content_in_the_column() {
        let table = Table::new()
            .columns([TableColumn::fixed(100.0).align(Align::End)])
            .rows([row(&["42"])]);
        let c = ctx();
        let mut warnings = Vec::new();
        let area = Rect {
            x: 0.0,
            y: 0.0,
            width: 100.0,
            height: 50.0,
        };
        let LayoutResult::Fit(RenderNode::Group { children: rows, .. }) = Element::Table(table).layout(&c, area, &mut warnings, 1) else {
            panic!("expected Fit");
        };
        let RenderNode::Group { children: cells, .. } = &rows[0] else {
            panic!("expected row group");
        };
        let RenderNode::Group { area: cell_area, .. } = &cells[0] else {
            panic!("expected clipped cell wrapper");
        };
        // "42" is much narrower than the 100pt column; End-align must
        // push it toward the right edge, not leave it at x=0.
        assert!(cell_area.x > 50.0, "expected right-aligned cell, got x={}", cell_area.x);
    }
}