c2pdf/
dimensions.rs

1//! Contains [`Dimensions`] struct and implementations to simplify passing
2//! document dimensions where needed
3
4use printpdf::Mm;
5
6/// Stores the dimensions of the page
7#[allow(missing_docs)]
8#[derive(Debug, Clone)]
9pub struct Dimensions {
10  pub width: Mm,
11  pub height: Mm,
12  pub margin_left: Mm,
13  pub margin_right: Mm,
14  pub margin_top: Mm,
15  pub margin_bottom: Mm,
16}
17impl Default for Dimensions {
18  /// Initialises a default `Dimensions`.
19  /// Default document size is A4 (210mm by 297mm)
20  fn default() -> Self {
21    Self {
22      width: Mm(210.0),
23      height: Mm(297.0),
24      margin_top: Mm(20.0),
25      margin_bottom: Mm(5.0),
26      margin_left: Mm(10.0),
27      margin_right: Mm(10.0),
28    }
29  }
30}
31impl Dimensions {
32  /// Initialises new [`Dimensions`] with default margins, and given `width`, and `height`
33  pub fn new_default_margins(width: Mm, height: Mm) -> Self {
34    Self {
35      width,
36      height,
37      ..Default::default()
38    }
39  }
40  /// Initialises a new [`Dimensions`]
41  pub fn new(
42    width: Mm,
43    height: Mm,
44    margin_top: Mm,
45    margin_bottom: Mm,
46    margin_left: Mm,
47    margin_right: Mm,
48  ) -> Self {
49    Self {
50      width,
51      height,
52      margin_left,
53      margin_right,
54      margin_top,
55      margin_bottom,
56    }
57  }
58  /// Computes the maximum text width (in millimetres)
59  pub fn max_text_width(&self) -> Mm {
60    self.width - self.margin_left - self.margin_right
61  }
62  /// Computes the maximum height that all the lines of text can be
63  /// on a single page
64  pub fn max_text_height(&self) -> Mm {
65    self.height - self.margin_top - self.margin_bottom
66  }
67}