merging stash edits
use crate::coord::Shift;
use crate::drawing::{DrawingArea, IntoDrawingArea};
-use base64;
use image::{png::PngEncoder, ImageBuffer, ImageError, Pixel, Rgb, RgbImage};
use plotters_bitmap::BitMapBackend;
use plotters_svg::SVGBackend;
+use plotters_backend::DrawingBackend;
use std::ops::Deref;
/// The wrapper for the generated SVG
@@ -45,10 +45,10 @@ pub fn evcxr_figure<
SVGWrapper(buffer, "".to_string())
}
-#[cfg(feature = "evcxr_bitmap_figure")]
+#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
pub struct BitMapWrapper(String, String);
-#[cfg(feature = "evcxr_bitmap_figure")]
+#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
impl BitMapWrapper {
pub fn evcxr_display(&self) {
println!("{:?}", self);
@@ -60,7 +60,8 @@ impl BitMapWrapper {
}
}
-#[cfg(feature = "evcxr_bitmap_figure")]
+// #[cfg(feature = "evcxr_bitmap_figure")]
+#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
impl std::fmt::Debug for BitMapWrapper {
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
let enc = self.0.as_str();
@@ -72,7 +73,8 @@ impl std::fmt::Debug for BitMapWrapper {
}
}
-#[cfg(feature = "evcxr_bitmap_figure")]
+// #[cfg(feature = "evcxr_bitmap_figure")]
+#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
fn encode_png<P, Container>(img: &ImageBuffer<P, Container>) -> Result<Vec<u8>, ImageError>
where
P: Pixel<Subpixel = u8> + 'static,
@@ -84,23 +86,28 @@ where
Ok(buf)
}
-#[cfg(feature = "evcxr_bitmap_figure")]
+// #[cfg(feature = "evcxr_bitmap_figure")]
/// Start drawing an evcxr figure
+#[cfg(all(feature = "evcxr", feature = "bitmap_backend"))]
pub fn evcxr_bitmap_figure<
Draw: FnOnce(DrawingArea<BitMapBackend, Shift>) -> Result<(), Box<dyn std::error::Error>>,
>(
size: (u32, u32),
draw: Draw,
-) -> BitMapWrapper {
+) -> SVGWrapper {
let pixel_size = 3;
let mut buf = Vec::new();
buf.resize((size.0 as usize) * (size.1 as usize) * pixel_size, 0);
let root = BitMapBackend::with_buffer(&mut buf, size).into_drawing_area();
draw(root).expect("Drawing failure");
let img = RgbImage::from_raw(size.0, size.1, buf).unwrap();
- let enc_buf = encode_png(&img).unwrap();
- let buffer = base64::encode(&enc_buf);
- BitMapWrapper(buffer, "".to_string())
+ let mut buffer = "".to_string();
+ let svg_root = SVGBackend::with_string(&mut buffer, size).into_drawing_area();
+ svg_root.blit_bitmap((0, 0), size, buf).expect("Failure converting to SVG");
+ // draw(svg_root).expect("Failure converting bitmap to SVG");
+ // let enc_buf = encode_png(&img).unwrap();
+ // let buffer = base64::encode(&enc_buf);
+ SVGWrapper(buffer, "".to_string())
}
// #[cfg(feature = "evcxr_bitmap_figure")]
