slideshow/
slideshow.rs

1// Rewrite a simple slideshow in Rust using pdfpdf
2extern crate pdfpdf;
3use pdfpdf::{Alignment, Color, Font, Pdf};
4
5struct Slideshow {
6    width: f64,
7    height: f64,
8    background_color: Color,
9    text_color: Color,
10    pdf: Pdf,
11}
12
13impl Slideshow {
14    pub fn new(
15        width: f64,
16        height: f64,
17        font: Font,
18        background_color: Color,
19        text_color: Color,
20    ) -> Self {
21        let mut this = Slideshow {
22            width: width,
23            height: height,
24            background_color: background_color,
25            text_color: text_color,
26            pdf: Pdf::new_uncompressed(),
27        };
28        this.pdf.font(font, 60);
29        this
30    }
31
32    pub fn add_title_slide(&mut self, text: &str) -> &mut Self {
33        self.add_text_slide(text);
34        self
35    }
36
37    pub fn add_text_slide(&mut self, text: &str) -> &mut Self {
38        // init the new slide
39        self.pdf
40            .add_page(self.width, self.height)
41            .set_color(&self.background_color.clone())
42            .draw_rectangle_filled(0.0, 0.0, self.width, self.height)
43            .set_color(&self.text_color.clone())
44            .draw_text(
45                self.width / 2.0,
46                self.height / 2.0,
47                Alignment::CenterCenter,
48                text,
49            );
50        self
51    }
52
53    pub fn write_to(&mut self, filename: &str) -> std::result::Result<(), std::io::Error> {
54        self.pdf.write_to(filename)
55    }
56}
57
58fn main() {
59    Slideshow::new(
60        1024.0,
61        769.0,
62        Font::Helvetica,
63        Color::gray(0),
64        Color::gray(255),
65    ).add_title_slide("Lessons from LATHER")
66        .add_text_slide("The Activity Problem\nOR\nRemove the spots")
67        .add_text_slide(
68            "1. Find/make a good model\n2. Run it. A lot.\n3. Listen at group meetings",
69        )
70        .add_text_slide("Easy to use\nWe're going to write a lot of scripts")
71        .add_text_slide("SOAP: 2.4 s\nLATHER: 0.006 s")
72        .add_text_slide("All I Really Need to Know I Learned in\nKindergarten")
73        .add_text_slide(
74            "All I Really Need to Know I Learned in\nMathematical Physics",
75        )
76        .write_to("lessons_from_lather.pdf")
77        .expect("Couldn't save slideshow");
78}