1 use crate::coord::Shift;
2 use crate::drawing::{DrawingArea, IntoDrawingArea};
3 use plotters_backend::DrawingBackend;
4 use plotters_svg::SVGBackend;
5
6 #[cfg(feature = "evcxr_bitmap")]
7 use plotters_bitmap::BitMapBackend;
8
9 /// The wrapper for the generated SVG
10 pub struct SVGWrapper(String, String);
11
12 impl SVGWrapper {
13 /// Displays the contents of the `SVGWrapper` struct.
evcxr_display(&self)14 pub fn evcxr_display(&self) {
15 println!("{:?}", self);
16 }
17 /// Sets the style of the `SVGWrapper` struct.
style<S: Into<String>>(mut self, style: S) -> Self18 pub fn style<S: Into<String>>(mut self, style: S) -> Self {
19 self.1 = style.into();
20 self
21 }
22 }
23
24 impl std::fmt::Debug for SVGWrapper {
fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result25 fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
26 let svg = self.0.as_str();
27 write!(
28 formatter,
29 "EVCXR_BEGIN_CONTENT text/html\n<div style=\"{}\">{}</div>\nEVCXR_END_CONTENT",
30 self.1, svg
31 )
32 }
33 }
34
35 /// Start drawing an evcxr figure
evcxr_figure< Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>, >( size: (u32, u32), draw: Draw, ) -> SVGWrapper36 pub fn evcxr_figure<
37 Draw: FnOnce(DrawingArea<SVGBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
38 >(
39 size: (u32, u32),
40 draw: Draw,
41 ) -> SVGWrapper {
42 let mut buffer = "".to_string();
43 let root = SVGBackend::with_string(&mut buffer, size).into_drawing_area();
44 draw(root).expect("Drawing failure");
45 SVGWrapper(buffer, "".to_string())
46 }
47
48 /// Start drawing an evcxr figure
49 #[cfg(feature = "evcxr_bitmap")]
evcxr_bitmap_figure< Draw: FnOnce(DrawingArea<BitMapBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>, >( size: (u32, u32), draw: Draw, ) -> SVGWrapper50 pub fn evcxr_bitmap_figure<
51 Draw: FnOnce(DrawingArea<BitMapBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
52 >(
53 size: (u32, u32),
54 draw: Draw,
55 ) -> SVGWrapper {
56 const PIXEL_SIZE: usize = 3;
57 let mut buf = Vec::new();
58 buf.resize((size.0 as usize) * (size.1 as usize) * PIXEL_SIZE, 0);
59 let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area();
60 draw(root).expect("Drawing failure");
61 let mut buffer = "".to_string();
62 {
63 let mut svg_root = SVGBackend::with_string(&mut buffer, size);
64 svg_root
65 .blit_bitmap((0, 0), size, &buf)
66 .expect("Failure converting to SVG");
67 }
68 SVGWrapper(buffer, "".to_string())
69 }
70