summaryrefslogtreecommitdiffstats
path: root/cairo-renderer/src/lib.rs
blob: 830e6bc4235bab6c3559937ff9ee230d9d66ab75 (plain)
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
use diaphragm_core::{
    styles::{DefinedDashStyle, Pattern},
    text::{DefinedFontDescription, FontDescription},
    Renderer,
};

pub struct CairoRenderer {
    ctx: cairo::Context,
}

impl CairoRenderer {
    pub fn new() -> Self {
        // TODO: properly
        let surface = cairo::SvgSurface::for_stream(1920., 1080., std::io::stdout()).unwrap();
        let ctx = cairo::Context::new(&surface).unwrap();

        Self { ctx }
    }
}

impl Renderer for CairoRenderer {
    fn move_to(&mut self, x: f64, y: f64) {
        self.ctx.move_to(x, y)
    }

    fn stroke(&mut self) {
        self.ctx.stroke().unwrap();
    }

    fn fill(&mut self) {
        self.ctx.fill().unwrap();
    }

    fn fill_preserve(&mut self) {
        self.ctx.fill_preserve().unwrap();
    }

    fn set_pattern(&mut self, pattern: &Pattern) {
        match pattern {
            Pattern::Solid(color) => {
                let (red, green, blue, alpha) = color.rgba();
                self.ctx.set_source_rgba(red, green, blue, alpha)
            }
            Pattern::None => {}
            _ => panic!("Unhandled pattern"),
        }
    }

    fn set_dash(&mut self, dash: &DefinedDashStyle) {
        self.ctx.set_dash(dash.dashes(), dash.offset());
    }

    fn clear_dash(&mut self) {
        self.ctx.set_dash(&[], 0.);
    }

    fn set_line_width(&mut self, width: f64) {
        self.ctx.set_line_width(width);
    }

    fn line_to(&mut self, x: f64, y: f64) {
        self.ctx.line_to(x, y)
    }

    fn rectangle(&mut self, x: f64, y: f64, width: f64, height: f64) {
        self.ctx.rectangle(x, y, width, height);
    }

    fn text_extents(&self, text: &str, font: &FontDescription) -> (f64, f64) {
        // Pango gives us integer back, so we lose some precision. We use this for that.
        const TEST_ABSOLUTE_SIZE: f64 = 1_000_000.;

        let layout = pangocairo::create_layout(&self.ctx);
        let mut font_desc = pango::FontDescription::from_string(&font.family);
        font_desc.set_absolute_size(TEST_ABSOLUTE_SIZE);
        layout.set_font_description(Some(&font_desc));
        layout.set_markup(text);

        // TODO: get height from the baseline
        // let mut layout_iter = layout.iter();
        // let _height = loop {
        //     if layout_iter.at_last_line() {
        //         break layout_iter.baseline();
        //     }
        //     layout_iter.next_line();
        // };

        let (_, extents) = layout.pixel_extents();
        (
            extents.width() as f64 / TEST_ABSOLUTE_SIZE,
            extents.height() as f64 / TEST_ABSOLUTE_SIZE,
        )

        //let (width, height) = layout.get_pixel_size();
        //(width as f64, height as f64)
    }

    fn show_text(&mut self, text: &str, font: &DefinedFontDescription) {
        let layout = pangocairo::create_layout(&self.ctx);
        let mut font_desc = pango::FontDescription::from_string(&font.family);

        font_desc.set_absolute_size(font.size as _);
        layout.set_font_description(Some(&font_desc));
        layout.set_markup(text);

        //self.ctx.set_font_size(dbg!(font.size));
        pangocairo::show_layout(&self.ctx, &layout);
    }
}