fulgur 0.3.0

HTML/CSS to PDF conversion library
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
use std::collections::{BTreeMap, HashMap};

use crate::config::{Margin, PageSize};

/// Which edge of the page a set of margin boxes belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Edge {
    Top,
    Bottom,
    Left,
    Right,
}

/// Rectangle describing a margin box's position and size in page coordinates (points).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MarginBoxRect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

/// The 16 CSS page margin box positions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum MarginBoxPosition {
    TopLeftCorner,
    TopLeft,
    TopCenter,
    TopRight,
    TopRightCorner,
    LeftTop,
    LeftMiddle,
    LeftBottom,
    RightTop,
    RightMiddle,
    RightBottom,
    BottomLeftCorner,
    BottomLeft,
    BottomCenter,
    BottomRight,
    BottomRightCorner,
}

impl MarginBoxPosition {
    /// Parse a CSS at-keyword name (without the `@`) into a `MarginBoxPosition`.
    ///
    /// Accepts names like `"top-center"`, `"bottom-left-corner"`, etc.
    pub fn from_at_keyword(name: &str) -> Option<Self> {
        // CSS at-rule names are ASCII case-insensitive.
        // cssparser does not lowercase at-rule names before passing them here.
        let lower = name.to_ascii_lowercase();
        match lower.as_str() {
            "top-left-corner" => Some(Self::TopLeftCorner),
            "top-left" => Some(Self::TopLeft),
            "top-center" => Some(Self::TopCenter),
            "top-right" => Some(Self::TopRight),
            "top-right-corner" => Some(Self::TopRightCorner),
            "left-top" => Some(Self::LeftTop),
            "left-middle" => Some(Self::LeftMiddle),
            "left-bottom" => Some(Self::LeftBottom),
            "right-top" => Some(Self::RightTop),
            "right-middle" => Some(Self::RightMiddle),
            "right-bottom" => Some(Self::RightBottom),
            "bottom-left-corner" => Some(Self::BottomLeftCorner),
            "bottom-left" => Some(Self::BottomLeft),
            "bottom-center" => Some(Self::BottomCenter),
            "bottom-right" => Some(Self::BottomRight),
            "bottom-right-corner" => Some(Self::BottomRightCorner),
            _ => None,
        }
    }

    /// Compute the bounding rectangle for this margin box position.
    ///
    /// Coordinates are in page-space points with origin at top-left of page.
    pub fn bounding_rect(&self, page_size: PageSize, margin: Margin) -> MarginBoxRect {
        let content_width = page_size.width - margin.left - margin.right;
        let content_height = page_size.height - margin.top - margin.bottom;
        let third_w = content_width / 3.0;
        let third_h = content_height / 3.0;

        match self {
            // --- Top edge corners ---
            Self::TopLeftCorner => MarginBoxRect {
                x: 0.0,
                y: 0.0,
                width: margin.left,
                height: margin.top,
            },
            Self::TopRightCorner => MarginBoxRect {
                x: page_size.width - margin.right,
                y: 0.0,
                width: margin.right,
                height: margin.top,
            },

            // --- Top edge positions ---
            Self::TopLeft => MarginBoxRect {
                x: margin.left,
                y: 0.0,
                width: third_w,
                height: margin.top,
            },
            Self::TopCenter => MarginBoxRect {
                x: margin.left,
                y: 0.0,
                width: content_width,
                height: margin.top,
            },
            Self::TopRight => MarginBoxRect {
                x: margin.left + 2.0 * third_w,
                y: 0.0,
                width: third_w,
                height: margin.top,
            },

            // --- Bottom edge corners ---
            Self::BottomLeftCorner => MarginBoxRect {
                x: 0.0,
                y: page_size.height - margin.bottom,
                width: margin.left,
                height: margin.bottom,
            },
            Self::BottomRightCorner => MarginBoxRect {
                x: page_size.width - margin.right,
                y: page_size.height - margin.bottom,
                width: margin.right,
                height: margin.bottom,
            },

            // --- Bottom edge positions ---
            Self::BottomLeft => MarginBoxRect {
                x: margin.left,
                y: page_size.height - margin.bottom,
                width: third_w,
                height: margin.bottom,
            },
            Self::BottomCenter => MarginBoxRect {
                x: margin.left,
                y: page_size.height - margin.bottom,
                width: content_width,
                height: margin.bottom,
            },
            Self::BottomRight => MarginBoxRect {
                x: margin.left + 2.0 * third_w,
                y: page_size.height - margin.bottom,
                width: third_w,
                height: margin.bottom,
            },

            // --- Left edge positions ---
            Self::LeftTop => MarginBoxRect {
                x: 0.0,
                y: margin.top,
                width: margin.left,
                height: third_h,
            },
            Self::LeftMiddle => MarginBoxRect {
                x: 0.0,
                y: margin.top + third_h,
                width: margin.left,
                height: third_h,
            },
            Self::LeftBottom => MarginBoxRect {
                x: 0.0,
                y: margin.top + 2.0 * third_h,
                width: margin.left,
                height: third_h,
            },

            // --- Right edge positions ---
            Self::RightTop => MarginBoxRect {
                x: page_size.width - margin.right,
                y: margin.top,
                width: margin.right,
                height: third_h,
            },
            Self::RightMiddle => MarginBoxRect {
                x: page_size.width - margin.right,
                y: margin.top + third_h,
                width: margin.right,
                height: third_h,
            },
            Self::RightBottom => MarginBoxRect {
                x: page_size.width - margin.right,
                y: margin.top + 2.0 * third_h,
                width: margin.right,
                height: third_h,
            },
        }
    }
}

/// Map an edge to its (left, center, right) non-corner positions.
fn edge_positions(edge: Edge) -> (MarginBoxPosition, MarginBoxPosition, MarginBoxPosition) {
    match edge {
        Edge::Top => (
            MarginBoxPosition::TopLeft,
            MarginBoxPosition::TopCenter,
            MarginBoxPosition::TopRight,
        ),
        Edge::Bottom => (
            MarginBoxPosition::BottomLeft,
            MarginBoxPosition::BottomCenter,
            MarginBoxPosition::BottomRight,
        ),
        // Left/Right don't use this helper in compute_edge_layout,
        // but we provide a mapping for completeness.
        Edge::Left => (
            MarginBoxPosition::LeftTop,
            MarginBoxPosition::LeftMiddle,
            MarginBoxPosition::LeftBottom,
        ),
        Edge::Right => (
            MarginBoxPosition::RightTop,
            MarginBoxPosition::RightMiddle,
            MarginBoxPosition::RightBottom,
        ),
    }
}

/// Distribute available space between two items based on their max-content widths.
fn flex_distribute(a_max: f32, b_max: f32, available: f32) -> (f32, f32) {
    let total = a_max + b_max;
    if total == 0.0 {
        return (available / 2.0, available / 2.0);
    }
    let a_factor = a_max / total;
    if total <= available {
        let flex_space = available - total;
        let a = a_max + flex_space * a_factor;
        let b = b_max + flex_space * (1.0 - a_factor);
        (a, b)
    } else {
        let a = available * a_factor;
        let b = available * (1.0 - a_factor);
        (a, b)
    }
}

/// Distribute available width among up to 3 positions (left, center, right).
/// Returns the computed width for each defined position.
fn distribute_widths(
    left_max: Option<f32>,
    center_max: Option<f32>,
    right_max: Option<f32>,
    available: f32,
) -> (Option<f32>, Option<f32>, Option<f32>) {
    let defined_count =
        left_max.is_some() as u8 + center_max.is_some() as u8 + right_max.is_some() as u8;

    if defined_count == 0 {
        return (None, None, None);
    }

    // 1 position defined: gets full available width
    if defined_count == 1 {
        return (
            left_max.map(|_| available),
            center_max.map(|_| available),
            right_max.map(|_| available),
        );
    }

    if let Some(c_max) = center_max {
        // Center defined (with or without L/R)
        let ac_max = left_max.unwrap_or(0.0) + right_max.unwrap_or(0.0);
        let (c_width, ac_width) = flex_distribute(c_max, ac_max, available);
        let half_ac = ac_width / 2.0;
        (
            left_max.map(|_| half_ac),
            Some(c_width),
            right_max.map(|_| half_ac),
        )
    } else {
        // C not defined, L + R
        let l_max = left_max.unwrap_or(0.0);
        let r_max = right_max.unwrap_or(0.0);
        let (l_width, r_width) = flex_distribute(l_max, r_max, available);
        (Some(l_width), None, Some(r_width))
    }
}

/// Compute the rects for all defined margin boxes on a given edge,
/// using CSS Paged Media flex-based width distribution.
/// `defined` maps non-corner positions to their max-content width.
/// Corner rects are NOT included — compute those separately with `bounding_rect`.
pub fn compute_edge_layout(
    edge: Edge,
    defined: &BTreeMap<MarginBoxPosition, f32>,
    page_size: PageSize,
    margin: Margin,
) -> HashMap<MarginBoxPosition, MarginBoxRect> {
    let mut result = HashMap::new();

    match edge {
        Edge::Top | Edge::Bottom => {
            let content_width = page_size.width - margin.left - margin.right;
            let (y, height) = if edge == Edge::Top {
                (0.0, margin.top)
            } else {
                (page_size.height - margin.bottom, margin.bottom)
            };

            let (left_pos, center_pos, right_pos) = edge_positions(edge);
            let left_max = defined.get(&left_pos).copied();
            let center_max = defined.get(&center_pos).copied();
            let right_max = defined.get(&right_pos).copied();

            let (l_w, c_w, r_w) = distribute_widths(left_max, center_max, right_max, content_width);

            // Compute x positions. When center is defined, left and right
            // slots are always equal width (half_ac), even if one is undefined.
            // This keeps center visually centered on the content area.
            if let Some(cw) = c_w {
                // Center-based layout: left_slot | center | right_slot
                let left_slot = l_w.unwrap_or_else(|| {
                    // Left undefined: reserve same space as right slot
                    r_w.unwrap_or(0.0)
                });
                let x_left = margin.left;
                let x_center = x_left + left_slot;
                let x_right = x_center + cw;

                if let Some(w) = l_w {
                    result.insert(
                        left_pos,
                        MarginBoxRect {
                            x: x_left,
                            y,
                            width: w,
                            height,
                        },
                    );
                }
                result.insert(
                    center_pos,
                    MarginBoxRect {
                        x: x_center,
                        y,
                        width: cw,
                        height,
                    },
                );
                if let Some(w) = r_w {
                    result.insert(
                        right_pos,
                        MarginBoxRect {
                            x: x_right,
                            y,
                            width: w,
                            height,
                        },
                    );
                }
            } else {
                // No center: sequential layout
                let mut x = margin.left;
                if let Some(w) = l_w {
                    result.insert(
                        left_pos,
                        MarginBoxRect {
                            x,
                            y,
                            width: w,
                            height,
                        },
                    );
                    x += w;
                }
                if let Some(w) = r_w {
                    result.insert(
                        right_pos,
                        MarginBoxRect {
                            x,
                            y,
                            width: w,
                            height,
                        },
                    );
                }
            }
        }
        Edge::Left | Edge::Right => {
            // Phase 3: just return bounding_rect for each defined position
            for &pos in defined.keys() {
                result.insert(pos, pos.bounding_rect(page_size, margin));
            }
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_from_at_keyword_valid() {
        assert_eq!(
            MarginBoxPosition::from_at_keyword("top-center"),
            Some(MarginBoxPosition::TopCenter)
        );
        assert_eq!(
            MarginBoxPosition::from_at_keyword("bottom-left-corner"),
            Some(MarginBoxPosition::BottomLeftCorner)
        );
        assert_eq!(
            MarginBoxPosition::from_at_keyword("right-middle"),
            Some(MarginBoxPosition::RightMiddle)
        );
        assert_eq!(
            MarginBoxPosition::from_at_keyword("top-left"),
            Some(MarginBoxPosition::TopLeft)
        );
    }

    #[test]
    fn test_from_at_keyword_invalid() {
        assert_eq!(MarginBoxPosition::from_at_keyword("center"), None);
        assert_eq!(MarginBoxPosition::from_at_keyword(""), None);
        assert_eq!(MarginBoxPosition::from_at_keyword("top-middle"), None);
    }

    #[test]
    fn test_bounding_rect_top_center() {
        let page = PageSize::A4; // 595.28 x 841.89
        let margin = Margin::uniform(72.0); // 1 inch all around
        let rect = MarginBoxPosition::TopCenter.bounding_rect(page, margin);

        let content_width = page.width - margin.left - margin.right;
        assert!((rect.x - margin.left).abs() < 0.01);
        assert!((rect.y - 0.0).abs() < 0.01);
        assert!((rect.width - content_width).abs() < 0.01);
        assert!((rect.height - margin.top).abs() < 0.01);
    }

    #[test]
    fn test_bounding_rect_bottom_center() {
        let page = PageSize::A4;
        let margin = Margin::uniform(72.0);
        let rect = MarginBoxPosition::BottomCenter.bounding_rect(page, margin);

        let content_width = page.width - margin.left - margin.right;
        assert!((rect.x - margin.left).abs() < 0.01);
        assert!((rect.y - (page.height - margin.bottom)).abs() < 0.01);
        assert!((rect.width - content_width).abs() < 0.01);
        assert!((rect.height - margin.bottom).abs() < 0.01);
    }

    #[test]
    fn test_bounding_rect_top_left_corner() {
        let page = PageSize::A4;
        let margin = Margin {
            top: 50.0,
            right: 40.0,
            bottom: 60.0,
            left: 70.0,
        };
        let rect = MarginBoxPosition::TopLeftCorner.bounding_rect(page, margin);

        assert!((rect.x - 0.0).abs() < 0.01);
        assert!((rect.y - 0.0).abs() < 0.01);
        assert!((rect.width - 70.0).abs() < 0.01);
        assert!((rect.height - 50.0).abs() < 0.01);
    }

    // --- flex_distribute tests ---

    #[test]
    fn test_flex_distribute_both_fit() {
        // a=100, b=200, available=600 → proportional: a=200, b=400
        let (a, b) = flex_distribute(100.0, 200.0, 600.0);
        assert!((a - 200.0).abs() < 0.01);
        assert!((b - 400.0).abs() < 0.01);
    }

    #[test]
    fn test_flex_distribute_overflow() {
        // a=300, b=600, available=450 → proportional shrink: a=150, b=300
        let (a, b) = flex_distribute(300.0, 600.0, 450.0);
        assert!((a - 150.0).abs() < 0.01);
        assert!((b - 300.0).abs() < 0.01);
    }

    #[test]
    fn test_flex_distribute_zero() {
        let (a, b) = flex_distribute(0.0, 0.0, 300.0);
        assert!((a - 150.0).abs() < 0.01);
        assert!((b - 150.0).abs() < 0.01);
    }

    // --- distribute_widths tests ---

    #[test]
    fn test_distribute_center_only() {
        let (l, c, r) = distribute_widths(None, Some(100.0), None, 600.0);
        assert!(l.is_none());
        assert!((c.unwrap() - 600.0).abs() < 0.01);
        assert!(r.is_none());
    }

    #[test]
    fn test_distribute_left_right() {
        let (l, c, r) = distribute_widths(Some(100.0), None, Some(200.0), 600.0);
        assert!(c.is_none());
        // flex_distribute(100, 200, 600) → (200, 400)
        assert!((l.unwrap() - 200.0).abs() < 0.01);
        assert!((r.unwrap() - 400.0).abs() < 0.01);
    }

    #[test]
    fn test_distribute_all_three() {
        // center=200, left=50, right=50, available=600
        // ac_max = 50+50 = 100
        // flex_distribute(200, 100, 600) → total=300, flex_space=300
        //   c_factor = 200/300 = 2/3, c = 200 + 300*2/3 = 400
        //   ac = 100 + 300*1/3 = 200, half_ac = 100
        let (l, c, r) = distribute_widths(Some(50.0), Some(200.0), Some(50.0), 600.0);
        assert!((c.unwrap() - 400.0).abs() < 0.01);
        assert!((l.unwrap() - 100.0).abs() < 0.01);
        assert!((r.unwrap() - 100.0).abs() < 0.01);
    }

    // --- compute_edge_layout tests ---

    #[test]
    fn test_compute_edge_layout_top_center_only() {
        let page = PageSize::A4;
        let margin = Margin::uniform(72.0);
        let content_width = page.width - margin.left - margin.right;

        let mut defined = BTreeMap::new();
        defined.insert(MarginBoxPosition::TopCenter, 100.0);

        let result = compute_edge_layout(Edge::Top, &defined, page, margin);
        assert_eq!(result.len(), 1);
        let rect = result[&MarginBoxPosition::TopCenter];
        assert!((rect.x - margin.left).abs() < 0.01);
        assert!((rect.width - content_width).abs() < 0.01);
        assert!((rect.y - 0.0).abs() < 0.01);
        assert!((rect.height - margin.top).abs() < 0.01);
    }

    #[test]
    fn test_compute_edge_layout_top_left_right() {
        let page = PageSize::A4;
        let margin = Margin::uniform(72.0);
        let content_width = page.width - margin.left - margin.right;

        let mut defined = BTreeMap::new();
        defined.insert(MarginBoxPosition::TopLeft, 100.0);
        defined.insert(MarginBoxPosition::TopRight, 200.0);

        let result = compute_edge_layout(Edge::Top, &defined, page, margin);
        assert_eq!(result.len(), 2);

        let left_rect = result[&MarginBoxPosition::TopLeft];
        let right_rect = result[&MarginBoxPosition::TopRight];

        // Widths sum to content_width
        assert!((left_rect.width + right_rect.width - content_width).abs() < 0.01);
        // No overlap: right starts where left ends
        assert!((right_rect.x - (left_rect.x + left_rect.width)).abs() < 0.01);
        // Left starts at margin.left
        assert!((left_rect.x - margin.left).abs() < 0.01);
    }

    #[test]
    fn test_compute_edge_layout_top_all_three() {
        let page = PageSize::A4;
        let margin = Margin::uniform(72.0);
        let content_width = page.width - margin.left - margin.right;

        let mut defined = BTreeMap::new();
        defined.insert(MarginBoxPosition::TopLeft, 50.0);
        defined.insert(MarginBoxPosition::TopCenter, 200.0);
        defined.insert(MarginBoxPosition::TopRight, 50.0);

        let result = compute_edge_layout(Edge::Top, &defined, page, margin);
        assert_eq!(result.len(), 3);

        let l = result[&MarginBoxPosition::TopLeft];
        let c = result[&MarginBoxPosition::TopCenter];
        let r = result[&MarginBoxPosition::TopRight];

        // Widths sum to content_width
        assert!((l.width + c.width + r.width - content_width).abs() < 0.01);
        // Correct x positions: left starts at margin.left
        assert!((l.x - margin.left).abs() < 0.01);
        // Center starts after left
        assert!((c.x - (l.x + l.width)).abs() < 0.01);
        // Right starts after center
        assert!((r.x - (c.x + c.width)).abs() < 0.01);
    }

    #[test]
    fn test_compute_edge_layout_center_right_no_left() {
        let page = PageSize::A4;
        let margin = Margin::uniform(72.0);
        let content_width = page.width - margin.left - margin.right;

        let mut defined = BTreeMap::new();
        defined.insert(MarginBoxPosition::TopCenter, 200.0);
        defined.insert(MarginBoxPosition::TopRight, 50.0);

        let result = compute_edge_layout(Edge::Top, &defined, page, margin);
        assert_eq!(result.len(), 2);

        let c = result[&MarginBoxPosition::TopCenter];
        let r = result[&MarginBoxPosition::TopRight];

        // Widths sum to content_width (center + right + left_slot)
        // Center should NOT start at margin.left — it should be offset
        // by the right slot width to stay centered.
        assert!(c.x > margin.left);
        // Right starts after center
        assert!((r.x - (c.x + c.width)).abs() < 0.01);
        // Right ends at content edge
        assert!((r.x + r.width - (margin.left + content_width)).abs() < 0.01);
    }
}