Skip to main content

laser_pdf/
serde_elements.rs

1pub mod elements;
2
3use std::{ops::Index, rc::Rc};
4
5use crate::{CompositeElement, CompositeElementCallback, fonts::truetype::TruetypeFont};
6use elements::*;
7
8pub type Font = Rc<TruetypeFont>;
9
10pub trait SerdeElement {
11    fn element(
12        &self,
13        fonts: &impl for<'a> Index<&'a str, Output = Font>,
14        callback: impl CompositeElementCallback,
15    );
16}
17
18pub struct SerdeElementElement<'a, E: SerdeElement, F: for<'b> Index<&'b str, Output = Font>> {
19    pub element: &'a E,
20    pub fonts: &'a F,
21}
22
23impl<'a, E: SerdeElement, F: for<'b> Index<&'b str, Output = Font>> CompositeElement
24    for SerdeElementElement<'a, E, F>
25{
26    fn element(&self, callback: impl CompositeElementCallback) {
27        self.element.element(self.fonts, callback);
28    }
29}
30
31#[macro_export]
32macro_rules! define_serde_element_value {
33    ($enum_name:ident {$($type:ident $(<$($rest:ident),*>)*),*,}) => {
34        #[derive(Clone, serde::Deserialize)]
35        pub enum $enum_name {
36            $($type ($type $(<$($rest)*>)*)),*
37        }
38
39        impl $crate::serde_elements::SerdeElement for $enum_name {
40            fn element(
41                &self,
42                fonts: &impl for<'a> core::ops::Index<&'a str, Output = $crate::serde_elements::Font>,
43                callback: impl $crate::CompositeElementCallback,
44            ) {
45                match self {
46                    $($enum_name::$type(val) => $crate::serde_elements::SerdeElement
47                        ::element(val, fonts, callback)),*
48                }
49            }
50        }
51    };
52}
53
54define_serde_element_value!(ElementValue {
55    None,
56    Empty,
57    Debug<ElementValue>,
58    Text,
59    RichText,
60    VGap,
61    HAlign<ElementValue>,
62    Padding<ElementValue>,
63    StyledBox<ElementValue>,
64    Line,
65    Image,
66    Rectangle,
67    Circle,
68    Column<ElementValue>,
69    Row<ElementValue>,
70    BreakList<ElementValue>,
71    Stack<ElementValue>,
72    TableRow<ElementValue>,
73    Titled<ElementValue>,
74    TitleOrBreak<ElementValue>,
75    RepeatAfterBreak<ElementValue>,
76    RepeatBottom<ElementValue>,
77    PinBelow<ElementValue>,
78    ForceBreak,
79    BreakWhole<ElementValue>,
80    MinFirstHeight<ElementValue>,
81    AlignLocationBottom<ElementValue>,
82    AlignPreferredHeightBottom<ElementValue>,
83    ExpandToPreferredHeight<ElementValue>,
84    CenterInPreferredHeight<ElementValue>,
85    ShrinkToFit<ElementValue>,
86    Rotate<ElementValue>,
87    MaxWidth<ElementValue>,
88    Page<ElementValue>,
89});