laser-pdf 0.5.0

A Rust library for programmatic PDF generation with precise, predictable layout control.
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
use std::ops::Index;

use elements::rotate::Rotation;

use crate::{
    elements::{h_align::HorizontalAlignment, row::Flex, text::TextAlign},
    *,
};

use super::{Font, SerdeElement, SerdeElementElement};

const fn default_false() -> bool {
    false
}

const fn default_0u8() -> u8 {
    0
}

#[derive(Clone, Serialize, Deserialize)]
pub enum SerdeLinkTarget {
    Uri(String),
}

impl SerdeLinkTarget {
    pub fn as_link_target(&self) -> LinkTarget<'_> {
        match self {
            SerdeLinkTarget::Uri(uri) => LinkTarget::Uri(uri),
        }
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct None;

impl SerdeElement for None {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        _: impl CompositeElementCallback,
    ) {
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Empty;

impl SerdeElement for Empty {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::empty::Empty);
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Debug<E> {
    pub element: Box<E>,

    #[serde(default = "default_0u8")]
    pub color: u8,

    #[serde(default = "default_false")]
    pub show_max_width: bool,

    #[serde(default = "default_false")]
    pub show_last_location_max_height: bool,
}

impl<E: SerdeElement> SerdeElement for Debug<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::debug::Debug {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            color: self.color,
            show_max_width: self.show_max_width,
            show_last_location_max_height: self.show_last_location_max_height,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Text {
    pub text: String,
    pub font: String,
    pub size: f32,
    pub color: u32,
    pub underline: bool,
    pub extra_character_spacing: f32,
    pub extra_word_spacing: f32,
    pub extra_line_height: f32,
    pub align: TextAlign,
    /// Link to be added as a link annotation
    #[serde(default)]
    pub link: Option<SerdeLinkTarget>,
}

impl SerdeElement for Text {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::text::Text {
            text: &self.text,
            font: &*fonts[&self.font],
            size: self.size,
            color: self.color,
            underline: self.underline,
            extra_character_spacing: self.extra_character_spacing,
            extra_word_spacing: self.extra_word_spacing,
            extra_line_height: self.extra_line_height,
            align: self.align,
            link: self.link.as_ref().map(SerdeLinkTarget::as_link_target),
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RichTextSpan {
    /// The text content to render
    pub text: String,
    /// Font reference
    pub font: String,
    /// Font size in points
    pub size: f32,
    /// Text color as RGBA (default: black 0x00_00_00_FF)
    pub color: u32,
    /// Whether to underline the text
    pub underline: bool,
    /// Additional spacing between characters
    pub extra_character_spacing: f32,
    /// Additional spacing between words
    pub extra_word_spacing: f32,
    /// Additional line height
    pub extra_line_height: f32,
    /// Link to be added as a link annotation
    #[serde(default)]
    pub link: Option<SerdeLinkTarget>,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RichText {
    pub spans: Vec<RichTextSpan>,
    pub align: TextAlign,
}

impl SerdeElement for RichText {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::rich_text::RichText {
            spans: self.spans.iter().map(|s| elements::rich_text::Span {
                text: &s.text,
                font: &*fonts[&s.font],
                size: s.size,
                color: s.color,
                underline: s.underline,
                extra_character_spacing: s.extra_character_spacing,
                extra_word_spacing: s.extra_word_spacing,
                extra_line_height: s.extra_line_height,
                link: s.link.as_ref().map(SerdeLinkTarget::as_link_target),
            }),
            align: self.align,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct VGap {
    pub gap: f32,
}

impl SerdeElement for VGap {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::v_gap::VGap(self.gap));
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct HAlign<E> {
    pub alignment: HorizontalAlignment,
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for HAlign<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::h_align::HAlign(
            self.alignment,
            SerdeElementElement {
                element: &*self.element,
                fonts,
            },
        ));
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Padding<E> {
    pub left: f32,
    pub right: f32,
    pub top: f32,
    pub bottom: f32,
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for Padding<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::padding::Padding {
            left: self.left,
            right: self.right,
            top: self.top,
            bottom: self.bottom,
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct StyledBox<E> {
    pub element: Box<E>,
    pub padding_left: f32,
    pub padding_right: f32,
    pub padding_top: f32,
    pub padding_bottom: f32,
    pub border_radius: f32,
    pub fill: Option<u32>,
    pub outline: Option<LineStyle>,
}

impl<E: SerdeElement> SerdeElement for StyledBox<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::styled_box::StyledBox {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            padding_left: self.padding_left,
            padding_right: self.padding_right,
            padding_top: self.padding_top,
            padding_bottom: self.padding_bottom,
            border_radius: self.border_radius,
            fill: self.fill,
            outline: self.outline,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Line {
    pub style: LineStyle,
}

impl SerdeElement for Line {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::line::Line { style: self.style });
    }
}

#[derive(Clone, Deserialize)]
pub struct Image {
    #[serde(rename = "path", deserialize_with = "crate::image::deserialize_image")]
    pub image: crate::image::Image,
}

impl SerdeElement for Image {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::image::ImageElement { image: &self.image });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Rectangle {
    pub size: (f32, f32),
    pub fill: Option<u32>,
    pub outline: Option<(f32, u32)>,
}

impl SerdeElement for Rectangle {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::rectangle::Rectangle {
            size: self.size,
            fill: self.fill,
            outline: self.outline,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Circle {
    pub radius: f32,
    pub fill: Option<u32>,
    pub outline: Option<(f32, u32)>,
}

impl SerdeElement for Circle {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::circle::Circle {
            radius: self.radius,
            fill: self.fill,
            outline: self.outline,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Column<E> {
    pub content: Vec<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse: bool,
}

impl<E: SerdeElement> SerdeElement for Column<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::column::Column {
            content: |mut content| {
                for element in &self.content {
                    content = content.add(&SerdeElementElement { element, fonts })?;
                }

                Option::None
            },
            gap: self.gap,
            collapse: self.collapse,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RowElement<E> {
    pub element: E,
    pub flex: Flex,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Row<E> {
    pub content: Vec<RowElement<E>>,
    pub gap: f32,
    pub expand: bool,
    pub collapse: bool,
}

impl<E: SerdeElement> SerdeElement for Row<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::row::Row {
            content: |content| {
                for RowElement { element, flex } in &self.content {
                    content.add(&SerdeElementElement { element, fonts }, *flex);
                }
            },
            gap: self.gap,
            expand: self.expand,
            collapse: self.collapse,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct BreakList<E> {
    pub content: Vec<E>,
    pub gap: f32,
}

impl<E: SerdeElement> SerdeElement for BreakList<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::break_list::BreakList {
            content: |mut content| {
                for element in &self.content {
                    content = content.add(&SerdeElementElement { element, fonts })?;
                }

                Option::None
            },
            gap: self.gap,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Stack<E> {
    pub content: Vec<E>,
    pub expand: bool,
}

impl<E: SerdeElement> SerdeElement for Stack<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::stack::Stack {
            content: |content| {
                for element in &self.content {
                    content.add(&SerdeElementElement { element, fonts });
                }
            },
            expand: self.expand,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TableRowElement<E> {
    pub element: E,
    pub flex: elements::table_row::Flex,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TableRow<E> {
    pub content: Vec<TableRowElement<E>>,
    pub line_style: LineStyle,

    #[serde(alias = "y_expand")]
    pub expand: bool,
}

impl<E: SerdeElement> SerdeElement for TableRow<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::table_row::TableRow {
            content: |content| {
                for TableRowElement { element, flex } in &self.content {
                    content.add(&SerdeElementElement { element, fonts }, *flex);
                }
            },
            line_style: self.line_style,
            expand: self.expand,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Titled<E> {
    pub title: Box<E>,
    pub content: Box<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse_on_empty_content: bool,
}

impl<E: SerdeElement> SerdeElement for Titled<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::titled::Titled {
            title: SerdeElementElement {
                element: &*self.title,
                fonts,
            },
            content: SerdeElementElement {
                element: &*self.content,
                fonts,
            },
            gap: self.gap,
            collapse_on_empty_content: self.collapse_on_empty_content,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct TitleOrBreak<E> {
    pub title: Box<E>,
    pub content: Box<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse_on_empty_content: bool,
}

impl<E: SerdeElement> SerdeElement for TitleOrBreak<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::title_or_break::TitleOrBreak {
            title: &SerdeElementElement {
                element: &*self.title,
                fonts,
            },
            content: &SerdeElementElement {
                element: &*self.content,
                fonts,
            },
            gap: self.gap,
            collapse_on_empty_content: self.collapse_on_empty_content,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ChangingTitle<E> {
    pub first_title: Box<E>,

    #[serde(alias = "second_title")]
    pub remaining_title: Box<E>,

    pub content: Box<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse: bool,
}

impl<E: SerdeElement> SerdeElement for ChangingTitle<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::changing_title::ChangingTitle {
            first_title: SerdeElementElement {
                element: &*self.first_title,
                fonts,
            },
            remaining_title: SerdeElementElement {
                element: &*self.remaining_title,
                fonts,
            },
            content: SerdeElementElement {
                element: &*self.content,
                fonts,
            },
            gap: self.gap,
            collapse: self.collapse,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RepeatAfterBreak<E> {
    pub title: Box<E>,
    pub content: Box<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse_on_empty_content: bool,
}

impl<E: SerdeElement> SerdeElement for RepeatAfterBreak<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::repeat_after_break::RepeatAfterBreak {
            title: &SerdeElementElement {
                element: &*self.title,
                fonts,
            },
            content: &SerdeElementElement {
                element: &*self.content,
                fonts,
            },
            gap: self.gap,
            collapse_on_empty_content: self.collapse_on_empty_content,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct RepeatBottom<E> {
    pub content: Box<E>,
    pub bottom: Box<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse: bool,
}

impl<E: SerdeElement> SerdeElement for RepeatBottom<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::repeat_bottom::RepeatBottom {
            content: SerdeElementElement {
                element: &*self.content,
                fonts,
            },
            bottom: SerdeElementElement {
                element: &*self.bottom,
                fonts,
            },
            gap: self.gap,
            collapse: self.collapse,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct PinBelow<E> {
    pub content: Box<E>,
    pub pinned_element: Box<E>,
    pub gap: f32,

    #[serde(default = "default_false")]
    pub collapse: bool,
}

impl<E: SerdeElement> SerdeElement for PinBelow<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::pin_below::PinBelow {
            content: SerdeElementElement {
                element: &*self.content,
                fonts,
            },
            pinned_element: SerdeElementElement {
                element: &*self.pinned_element,
                fonts,
            },
            gap: self.gap,
            collapse: self.collapse,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ForceBreak;

impl SerdeElement for ForceBreak {
    fn element(
        &self,
        _: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::force_break::ForceBreak);
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct BreakWhole<E> {
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for BreakWhole<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::break_whole::BreakWhole(&SerdeElementElement {
            element: &*self.element,
            fonts,
        }));
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct MinFirstHeight<E> {
    pub element: Box<E>,
    pub min_first_height: f32,
}

impl<E: SerdeElement> SerdeElement for MinFirstHeight<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::min_first_height::MinFirstHeight {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            min_first_height: self.min_first_height,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct AlignLocationBottom<E> {
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for AlignLocationBottom<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::align_location_bottom::AlignLocationBottom(
            SerdeElementElement {
                element: &*self.element,
                fonts,
            },
        ));
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct AlignPreferredHeightBottom<E> {
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for AlignPreferredHeightBottom<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(
            &elements::align_preferred_height_bottom::AlignPreferredHeightBottom(
                SerdeElementElement {
                    element: &*self.element,
                    fonts,
                },
            ),
        );
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ExpandToPreferredHeight<E> {
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for ExpandToPreferredHeight<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(
            &elements::expand_to_preferred_height::ExpandToPreferredHeight(SerdeElementElement {
                element: &*self.element,
                fonts,
            }),
        );
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct CenterInPreferredHeight<E> {
    pub element: Box<E>,
}

impl<E: SerdeElement> SerdeElement for CenterInPreferredHeight<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(
            &elements::center_in_preferred_height::CenterInPreferredHeight(SerdeElementElement {
                element: &*self.element,
                fonts,
            }),
        );
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ShrinkToFit<E> {
    pub element: Box<E>,
    pub min_height: f32,
}

impl<E: SerdeElement> SerdeElement for ShrinkToFit<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::shrink_to_fit::ShrinkToFit {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            min_height: self.min_height,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Rotate<E> {
    pub element: Box<E>,
    pub rotation: Rotation,
}

impl<E: SerdeElement> SerdeElement for Rotate<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::rotate::Rotate {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            rotation: self.rotation,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct MaxWidth<E> {
    pub element: Box<E>,
    pub max_width: f32,
}

impl<E: SerdeElement> SerdeElement for MaxWidth<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::max_width::MaxWidth {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            max_width: self.max_width,
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub enum PageNumberText {
    Current {
        before: String,
        after: String,
    },
    Total {
        before: String,
        after: String,
    },
    CurrentAndTotal {
        before: String,
        between: String,
        after: String,
    },
}

#[derive(Clone, Serialize, Deserialize)]
pub struct PageNumber {
    pub skip_pages: usize,
    pub pos: (elements::page::X, elements::page::Y),
    pub text: PageNumberText,
    pub font: String,
    pub size: f32,
    pub color: u32,
    pub underline: bool,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct DecorationElement<E> {
    pub element: E,
    pub pos: (elements::page::X, elements::page::Y),
    pub width: Option<f32>,
    pub skip_pages: usize,
    pub repeat: bool,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Page<E> {
    pub primary: Box<E>,
    pub border_left: f32,
    pub border_right: f32,
    pub border_top: f32,
    pub border_bottom: f32,
    pub decoration_elements: Vec<DecorationElement<E>>,
    pub page_numbers: Vec<PageNumber>,
}

impl<E: SerdeElement> SerdeElement for Page<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::page::Page {
            primary: SerdeElementElement {
                element: &*self.primary,
                fonts,
            },
            border_left: self.border_left,
            border_right: self.border_right,
            border_top: self.border_top,
            border_bottom: self.border_bottom,
            decoration_elements: |content, page, page_count| {
                for decoration_element in &self.decoration_elements {
                    if page == decoration_element.skip_pages
                        || decoration_element.repeat && page > decoration_element.skip_pages
                    {
                        content.add(
                            &SerdeElementElement {
                                element: &decoration_element.element,
                                fonts,
                            },
                            decoration_element.pos,
                            decoration_element.width,
                        );
                    }
                }

                for page_number in &self.page_numbers {
                    if page >= page_number.skip_pages {
                        content.add(
                            &elements::text::Text {
                                underline: page_number.underline,
                                color: page_number.color,
                                ..elements::text::Text::new(
                                    // Since the decoration_elements callback is only called when
                                    // drawing it shouldn't be a problem to be allocating a string here,
                                    // but it could potentially be optimized by reusing the buffer.
                                    &match page_number.text {
                                        PageNumberText::Current {
                                            ref before,
                                            ref after,
                                        } => {
                                            format!("{before}{}{after}", page + 1)
                                        }
                                        PageNumberText::Total {
                                            ref before,
                                            ref after,
                                        } => format!("{before}{page_count}{after}"),
                                        PageNumberText::CurrentAndTotal {
                                            ref before,
                                            ref between,
                                            ref after,
                                        } => {
                                            format!(
                                                "{before}{}{between}{page_count}{after}",
                                                page + 1
                                            )
                                        }
                                    },
                                    &*fonts[&page_number.font],
                                    page_number.size,
                                )
                            },
                            page_number.pos,
                            Option::None,
                        );
                    }
                }
            },
        });
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct Link<E> {
    pub element: Box<E>,
    pub target: SerdeLinkTarget,
}

impl<E: SerdeElement> SerdeElement for Link<E> {
    fn element(
        &self,
        fonts: &impl for<'a> Index<&'a str, Output = Font>,
        callback: impl CompositeElementCallback,
    ) {
        callback.call(&elements::link::Link {
            element: SerdeElementElement {
                element: &*self.element,
                fonts,
            },
            target: self.target.as_link_target(),
        });
    }
}