Skip to main content

normordis_pdf/elements/
fixed_line.rs

1use super::{Element, LayoutMode, RenderContext};
2use crate::{
3    layout::{BorderStyle, FixedBox, OverflowPolicy},
4    styles::RgbColor,
5};
6
7/// A horizontal or vertical decorative line at a fixed position.
8///
9/// Useful for separators in form templates, certidões, and similar layouts.
10/// Does not participate in `PageFlow`.
11#[derive(Debug, Clone)]
12pub struct FixedLineElement {
13    pub x1_mm: f64,
14    pub y1_mm: f64,
15    pub x2_mm: f64,
16    pub y2_mm: f64,
17    /// Stroke width in mm.
18    pub width_mm: f64,
19    pub color: RgbColor,
20    pub style: BorderStyle,
21}
22
23impl FixedLineElement {
24    pub fn new(x1_mm: f64, y1_mm: f64, x2_mm: f64, y2_mm: f64, color: RgbColor) -> Self {
25        Self {
26            x1_mm,
27            y1_mm,
28            x2_mm,
29            y2_mm,
30            width_mm: 0.3,
31            color,
32            style: BorderStyle::Solid,
33        }
34    }
35}
36
37impl Element for FixedLineElement {
38    fn layout_mode(&self) -> LayoutMode {
39        let x = self.x1_mm.min(self.x2_mm);
40        let y = self.y1_mm.min(self.y2_mm);
41        let w = (self.x2_mm - self.x1_mm).abs().max(self.width_mm);
42        let h = (self.y2_mm - self.y1_mm).abs().max(self.width_mm);
43
44        LayoutMode::Fixed(FixedBox {
45            x_mm: x,
46            y_mm: y,
47            width_mm: w,
48            height_mm: h,
49            overflow: OverflowPolicy::Overflow,
50            border: None,
51            background: None,
52            padding_mm: 0.0,
53            z_index: 0,
54            ua_role: None,
55            ua_alt: None,
56        })
57    }
58
59    fn estimated_height_mm(&self) -> f64 {
60        0.0
61    }
62
63    fn render(&self, ctx: &mut RenderContext) -> crate::Result<super::RenderResult> {
64        let width_pt = (self.width_mm * 72.0 / 25.4) as f32;
65        ctx.backend.draw_line(
66            self.x1_mm, self.y1_mm,
67            self.x2_mm, self.y2_mm,
68            width_pt, &self.color,
69        )?;
70        Ok(super::RenderResult::done())
71    }
72}