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
/*!
A [Piet](https://crates.io/crates/piet) backend for [Plotters](https://crates.io/crates/plotters). This lets you draw plots on a Piet render context.
*/

use piet_common::{kurbo, Color, LineCap, Piet, RenderContext, StrokeStyle};
use plotters_backend::{BackendColor, BackendCoord, DrawingBackend, DrawingErrorKind};

#[derive(Debug, PartialEq, Eq)]
pub struct Error {}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "plotters-piet error")
    }
}

impl std::error::Error for Error {}

/// The piet backend.
///
/// Note that the size of the piet context has to be specified here.
pub struct PietBackend<'a, 'b> {
    pub size: (u32, u32),
    pub render_ctx: &'a mut Piet<'b>,
}

impl<'a, 'b> std::fmt::Debug for PietBackend<'a, 'b> {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        fmt.debug_struct("PietBackend")
            .field("size", &self.size)
            .field("render_ctx", &"(not printable)")
            .finish()
    }
}

impl<'a, 'b> DrawingBackend for PietBackend<'a, 'b> {
    type ErrorType = Error;

    fn get_size(&self) -> (u32, u32) {
        self.size
    }

    fn ensure_prepared(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        Ok(())
    }

    fn present(&mut self) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        self.render_ctx
            .finish()
            .map_err(|_| DrawingErrorKind::DrawingError(Error {}))
    }

    fn draw_pixel(
        &mut self,
        point: BackendCoord,
        color: BackendColor,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        let x = point.0 as f64;
        let y = point.1 as f64;
        self.render_ctx.fill(
            kurbo::Rect::new(x, y, x + 1., y + 1.),
            &plotters_color_to_piet(&color),
        );
        Ok(())
    }

    fn draw_line<S: plotters_backend::BackendStyle>(
        &mut self,
        from: BackendCoord,
        to: BackendCoord,
        style: &S,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        let from = plotters_point_to_kurbo_mid(from);
        let to = plotters_point_to_kurbo_mid(to);

        self.render_ctx.stroke_styled(
            kurbo::Line::new(from, to),
            &plotters_color_to_piet(&style.color()),
            style.stroke_width() as f64,
            &STROKE_STYLE_SQUARE_CAP,
        );
        Ok(())
    }

    fn draw_rect<S: plotters_backend::BackendStyle>(
        &mut self,
        upper_left: BackendCoord,
        bottom_right: BackendCoord,
        style: &S,
        fill: bool,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        let color = plotters_color_to_piet(&style.color());

        if fill {
            let upper_left = plotters_point_to_kurbo_corner(upper_left);
            let mut bottom_right = plotters_point_to_kurbo_corner(bottom_right);
            bottom_right.x += 1.;
            bottom_right.y += 1.;
            let rect = kurbo::Rect::new(upper_left.x, upper_left.y, bottom_right.x, bottom_right.y);

            self.render_ctx.fill(rect, &color);
        } else {
            let upper_left = plotters_point_to_kurbo_mid(upper_left);
            let bottom_right = plotters_point_to_kurbo_mid(bottom_right);
            let rect = kurbo::Rect::new(upper_left.x, upper_left.y, bottom_right.x, bottom_right.y);

            self.render_ctx
                .stroke(rect, &color, style.stroke_width() as f64);
        }

        Ok(())
    }

    fn draw_path<S: plotters_backend::BackendStyle, I: IntoIterator<Item = BackendCoord>>(
        &mut self,
        path: I,
        style: &S,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        if style.color().alpha == 0.0 {
            return Ok(());
        }

        let path: Vec<kurbo::PathEl> = plotters_path_to_kurbo(path).collect();

        self.render_ctx.stroke_styled(
            &*path,
            &plotters_color_to_piet(&style.color()),
            style.stroke_width() as f64,
            &STROKE_STYLE_SQUARE_CAP,
        );
        Ok(())
    }

    fn draw_circle<S: plotters_backend::BackendStyle>(
        &mut self,
        center: BackendCoord,
        radius: u32,
        style: &S,
        fill: bool,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        let center = plotters_point_to_kurbo_mid(center);
        let color = plotters_color_to_piet(&style.color());
        let circle = kurbo::Circle::new(center, radius as f64);

        if fill {
            self.render_ctx.fill(circle, &color);
        } else {
            self.render_ctx
                .stroke(circle, &color, style.stroke_width() as f64);
        }
        Ok(())
    }

    fn fill_polygon<S: plotters_backend::BackendStyle, I: IntoIterator<Item = BackendCoord>>(
        &mut self,
        vert: I,
        style: &S,
    ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
        if style.color().alpha == 0.0 {
            return Ok(());
        }

        let path: Vec<kurbo::PathEl> = plotters_path_to_kurbo(vert)
            .chain(std::iter::once(kurbo::PathEl::ClosePath))
            .collect();
        self.render_ctx
            .fill(&*path, &plotters_color_to_piet(&style.color()));
        Ok(())
    }

    // For now we use the default text drawing provided by plotters. This is definitely slower,
    // but at least we don't have to worry about matching the font size and offset which turns
    // out to be trickier than expected.
    // fn draw_text<TStyle: plotters_backend::BackendTextStyle>(
    //     &mut self,
    //     text: &str,
    //     style: &TStyle,
    //     pos: BackendCoord,
    // ) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
    //     let pos = plotters_point_to_kurbo(pos);
    //     let color = plotters_color_to_piet(&style.color());

    //     let text_api = self.render_ctx.text();
    //     let font_family = match style.family() {
    //         plotters_backend::FontFamily::Serif => Ok(FontFamily::SERIF),
    //         plotters_backend::FontFamily::SansSerif => Ok(FontFamily::SANS_SERIF),
    //         plotters_backend::FontFamily::Monospace => Ok(FontFamily::MONOSPACE),
    //         plotters_backend::FontFamily::Name(name) => text_api
    //             .font_family(name)
    //             .ok_or(piet_common::Error::MissingFont),
    //     };

    //     let (font_style, weight) = match style.style() {
    //         plotters_backend::FontStyle::Normal => (FontStyle::Regular, FontWeight::REGULAR),
    //         plotters_backend::FontStyle::Oblique => (FontStyle::Italic, FontWeight::REGULAR),
    //         plotters_backend::FontStyle::Italic => (FontStyle::Italic, FontWeight::REGULAR),
    //         plotters_backend::FontStyle::Bold => (FontStyle::Regular, FontWeight::BOLD),
    //     };

    //     let alignment = match style.anchor().h_pos {
    //         plotters_backend::text_anchor::HPos::Left => TextAlignment::Start,
    //         plotters_backend::text_anchor::HPos::Right => TextAlignment::End,
    //         plotters_backend::text_anchor::HPos::Center => TextAlignment::Center,
    //     };

    //     let layout = text_api
    //         .new_text_layout(String::from(text))
    //         .font(font_family.unwrap(), style.size())
    //         .text_color(color)
    //         .alignment(alignment)
    //         .default_attribute(TextAttribute::Style(font_style))
    //         .default_attribute(TextAttribute::Weight(weight))
    //         .build()
    //         .unwrap();

    //     // todo: style.anchor().v_pos
    //     // todo: style.transform()

    //     self.render_ctx.draw_text(&layout, pos);
    //     Ok(())
    // }
}

fn plotters_color_to_piet(col: &BackendColor) -> piet_common::Color {
    Color::rgba8(col.rgb.0, col.rgb.1, col.rgb.2, (col.alpha * 256.) as u8)
}

fn plotters_point_to_kurbo_mid((x, y): BackendCoord) -> kurbo::Point {
    kurbo::Point {
        x: x as f64 + 0.5,
        y: y as f64 + 0.5,
    }
}

fn plotters_point_to_kurbo_corner((x, y): BackendCoord) -> kurbo::Point {
    kurbo::Point {
        x: x as f64,
        y: y as f64,
    }
}

/// This is basically just an iterator map that applies a different function on
/// the first item as on the later items.
/// We need this because the piet direct2d backend doesn't like it if a path
/// consists entirely of `LineTo` entries, it requires the first entry to be
/// a `MoveTo` entry.
struct PlottersPathToKurbo<I> {
    iter: I,
    first: bool,
}

impl<I> PlottersPathToKurbo<I> {
    fn new(path: I) -> PlottersPathToKurbo<I> {
        PlottersPathToKurbo {
            iter: path,
            first: true,
        }
    }
}

impl<I> Iterator for PlottersPathToKurbo<I>
where
    I: Iterator<Item = BackendCoord>,
{
    type Item = kurbo::PathEl;

    fn next(&mut self) -> Option<Self::Item> {
        self.iter.next().map(|point| {
            let point = plotters_point_to_kurbo_mid(point);

            if self.first {
                self.first = false;
                kurbo::PathEl::MoveTo(point)
            } else {
                kurbo::PathEl::LineTo(point)
            }
        })
    }
}

fn plotters_path_to_kurbo(
    path: impl IntoIterator<Item = BackendCoord>,
) -> impl Iterator<Item = kurbo::PathEl> {
    PlottersPathToKurbo::new(path.into_iter())
}

const STROKE_STYLE_SQUARE_CAP: StrokeStyle = StrokeStyle::new().line_cap(LineCap::Square);

#[cfg(test)]
mod tests {
    use super::*;
    use piet_common::RenderContext;
    use plotters::prelude::*;

    #[test]
    fn fill_root_white() {
        let width = 3;
        let height = 2;

        let mut device = piet_common::Device::new().unwrap();
        let mut bitmap = device.bitmap_target(width, height, 1.0).unwrap();

        {
            let mut render_ctx = bitmap.render_context();

            let piet_backend = PietBackend {
                size: (width as u32, height as u32),
                render_ctx: &mut render_ctx,
            };

            let root = piet_backend.into_drawing_area();
            root.fill(&WHITE).unwrap();

            render_ctx.finish().unwrap();
        }

        let mut buf = [0; 6 * 4];
        bitmap
            .copy_raw_pixels(piet_common::ImageFormat::RgbaPremul, &mut buf)
            .unwrap();

        assert_eq!(buf, [255; 6 * 4]);
    }

    #[test]
    fn test_plotters_path_to_kurbo() {
        let path = vec![(1, 2), (3, 4), (5, 6)];

        let kurbo_path: Vec<kurbo::PathEl> = plotters_path_to_kurbo(path).collect();

        assert_eq!(
            kurbo_path,
            vec![
                kurbo::PathEl::MoveTo(kurbo::Point { x: 1.5, y: 2.5 }),
                kurbo::PathEl::LineTo(kurbo::Point { x: 3.5, y: 4.5 }),
                kurbo::PathEl::LineTo(kurbo::Point { x: 5.5, y: 6.5 }),
            ]
        );
    }
}