1
1
use crate :: coord:: Shift ;
2
2
use crate :: drawing:: { DrawingArea , IntoDrawingArea } ;
3
- use base64;
4
3
use image:: { png:: PngEncoder , ImageBuffer , ImageError , Pixel , Rgb , RgbImage } ;
5
4
use plotters_bitmap:: BitMapBackend ;
6
5
use plotters_svg:: SVGBackend ;
6
+ use plotters_backend:: DrawingBackend ;
7
7
use std:: ops:: Deref ;
8
8
9
9
/// The wrapper for the generated SVG
@@ -45,10 +45,10 @@ pub fn evcxr_figure<
45
45
SVGWrapper ( buffer, "" . to_string ( ) )
46
46
}
47
47
48
- #[ cfg( feature = "evcxr_bitmap_figure" ) ]
48
+ #[ cfg( all ( feature = "evcxr" , feature = "bitmap_backend" ) ) ]
49
49
pub struct BitMapWrapper ( String , String ) ;
50
50
51
- #[ cfg( feature = "evcxr_bitmap_figure" ) ]
51
+ #[ cfg( all ( feature = "evcxr" , feature = "bitmap_backend" ) ) ]
52
52
impl BitMapWrapper {
53
53
pub fn evcxr_display ( & self ) {
54
54
println ! ( "{:?}" , self ) ;
@@ -60,7 +60,8 @@ impl BitMapWrapper {
60
60
}
61
61
}
62
62
63
- #[ cfg( feature = "evcxr_bitmap_figure" ) ]
63
+ // #[cfg(feature = "evcxr_bitmap_figure")]
64
+ #[ cfg( all( feature = "evcxr" , feature = "bitmap_backend" ) ) ]
64
65
impl std:: fmt:: Debug for BitMapWrapper {
65
66
fn fmt ( & self , formatter : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
66
67
let enc = self . 0 . as_str ( ) ;
@@ -72,7 +73,8 @@ impl std::fmt::Debug for BitMapWrapper {
72
73
}
73
74
}
74
75
75
- #[ cfg( feature = "evcxr_bitmap_figure" ) ]
76
+ // #[cfg(feature = "evcxr_bitmap_figure")]
77
+ #[ cfg( all( feature = "evcxr" , feature = "bitmap_backend" ) ) ]
76
78
fn encode_png < P , Container > ( img : & ImageBuffer < P , Container > ) -> Result < Vec < u8 > , ImageError >
77
79
where
78
80
P : Pixel < Subpixel = u8 > + ' static ,
@@ -84,23 +86,28 @@ where
84
86
Ok ( buf)
85
87
}
86
88
87
- #[ cfg( feature = "evcxr_bitmap_figure" ) ]
89
+ // #[cfg(feature = "evcxr_bitmap_figure")]
88
90
/// Start drawing an evcxr figure
91
+ #[ cfg( all( feature = "evcxr" , feature = "bitmap_backend" ) ) ]
89
92
pub fn evcxr_bitmap_figure <
90
93
Draw : FnOnce ( DrawingArea < BitMapBackend , Shift > ) -> Result < ( ) , Box < dyn std:: error:: Error > > ,
91
94
> (
92
95
size : ( u32 , u32 ) ,
93
96
draw : Draw ,
94
- ) -> BitMapWrapper {
97
+ ) -> SVGWrapper {
95
98
let pixel_size = 3 ;
96
99
let mut buf = Vec :: new ( ) ;
97
100
buf. resize ( ( size. 0 as usize ) * ( size. 1 as usize ) * pixel_size, 0 ) ;
98
101
let root = BitMapBackend :: with_buffer ( & mut buf, size) . into_drawing_area ( ) ;
99
102
draw ( root) . expect ( "Drawing failure" ) ;
100
103
let img = RgbImage :: from_raw ( size. 0 , size. 1 , buf) . unwrap ( ) ;
101
- let enc_buf = encode_png ( & img) . unwrap ( ) ;
102
- let buffer = base64:: encode ( & enc_buf) ;
103
- BitMapWrapper ( buffer, "" . to_string ( ) )
104
+ let mut buffer = "" . to_string ( ) ;
105
+ let svg_root = SVGBackend :: with_string ( & mut buffer, size) . into_drawing_area ( ) ;
106
+ svg_root. blit_bitmap ( ( 0 , 0 ) , size, buf) . expect ( "Failure converting to SVG" ) ;
107
+ // draw(svg_root).expect("Failure converting bitmap to SVG");
108
+ // let enc_buf = encode_png(&img).unwrap();
109
+ // let buffer = base64::encode(&enc_buf);
110
+ SVGWrapper ( buffer, "" . to_string ( ) )
104
111
}
105
112
106
113
// #[cfg(feature = "evcxr_bitmap_figure")]
0 commit comments