1
1
use cairo:: { Context as CairoContext , FontSlant , FontWeight , Status as CairoStatus } ;
2
2
3
3
#[ allow( unused_imports) ]
4
- use crate :: drawing:: backend:: { BackendCoord , BackendStyle , DrawingBackend , DrawingErrorKind } ;
5
- use crate :: style:: text_anchor:: { HPos , VPos } ;
6
- #[ allow( unused_imports) ]
7
- use crate :: style:: { Color , FontDesc , FontStyle , FontTransform , RGBAColor , TextStyle } ;
4
+ use plotters_backend:: { BackendCoord , BackendStyle , DrawingBackend , DrawingErrorKind , FontStyle , FontTransform , BackendColor , BackendTextStyle } ;
5
+ use plotters_backend:: text_anchor:: { HPos , VPos } ;
8
6
9
7
/// The drawing backend that is backed with a Cairo context
10
8
pub struct CairoBackend < ' a > {
@@ -50,13 +48,13 @@ impl<'a> CairoBackend<'a> {
50
48
}
51
49
}
52
50
53
- fn set_color ( & self , color : & RGBAColor ) -> Result < ( ) , DrawingErrorKind < CairoError > > {
51
+ fn set_color ( & self , color : & BackendColor ) -> Result < ( ) , DrawingErrorKind < CairoError > > {
54
52
self . call_cairo ( |c| {
55
53
c. set_source_rgba (
56
- f64:: from ( color. rgb ( ) . 0 ) / 255.0 ,
57
- f64:: from ( color. rgb ( ) . 1 ) / 255.0 ,
58
- f64:: from ( color. rgb ( ) . 2 ) / 255.0 ,
59
- color. alpha ( ) ,
54
+ f64:: from ( color. rgb . 0 ) / 255.0 ,
55
+ f64:: from ( color. rgb . 1 ) / 255.0 ,
56
+ f64:: from ( color. rgb . 2 ) / 255.0 ,
57
+ color. alpha ,
60
58
)
61
59
} )
62
60
}
@@ -65,21 +63,21 @@ impl<'a> CairoBackend<'a> {
65
63
self . call_cairo ( |c| c. set_line_width ( f64:: from ( width) ) )
66
64
}
67
65
68
- fn set_font < ' b > ( & self , font : & FontDesc < ' b > ) -> Result < ( ) , DrawingErrorKind < CairoError > > {
69
- let actual_size = font. get_size ( ) ;
66
+ fn set_font < S : BackendTextStyle > ( & self , font : & S ) -> Result < ( ) , DrawingErrorKind < CairoError > > {
67
+ let actual_size = font. size ( ) ;
70
68
self . call_cairo ( |c| {
71
- match font. get_style ( ) {
69
+ match font. style ( ) {
72
70
FontStyle :: Normal => {
73
- c. select_font_face ( font. get_name ( ) , FontSlant :: Normal , FontWeight :: Normal )
71
+ c. select_font_face ( font. family ( ) . as_str ( ) , FontSlant :: Normal , FontWeight :: Normal )
74
72
}
75
73
FontStyle :: Bold => {
76
- c. select_font_face ( font. get_name ( ) , FontSlant :: Normal , FontWeight :: Bold )
74
+ c. select_font_face ( font. family ( ) . as_str ( ) , FontSlant :: Normal , FontWeight :: Bold )
77
75
}
78
76
FontStyle :: Oblique => {
79
- c. select_font_face ( font. get_name ( ) , FontSlant :: Oblique , FontWeight :: Normal )
77
+ c. select_font_face ( font. family ( ) . as_str ( ) , FontSlant :: Oblique , FontWeight :: Normal )
80
78
}
81
79
FontStyle :: Italic => {
82
- c. select_font_face ( font. get_name ( ) , FontSlant :: Italic , FontWeight :: Normal )
80
+ c. select_font_face ( font. family ( ) . as_str ( ) , FontSlant :: Italic , FontWeight :: Normal )
83
81
}
84
82
} ;
85
83
c. set_font_size ( actual_size) ;
@@ -125,15 +123,15 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
125
123
fn draw_pixel (
126
124
& mut self ,
127
125
point : BackendCoord ,
128
- color : & RGBAColor ,
126
+ color : BackendColor ,
129
127
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
130
128
self . call_cairo ( |c| {
131
129
c. rectangle ( f64:: from ( point. 0 ) , f64:: from ( point. 1 ) , 1.0 , 1.0 ) ;
132
130
c. set_source_rgba (
133
- f64:: from ( color. rgb ( ) . 0 ) / 255.0 ,
134
- f64:: from ( color. rgb ( ) . 1 ) / 255.0 ,
135
- f64:: from ( color. rgb ( ) . 2 ) / 255.0 ,
136
- color. alpha ( ) ,
131
+ f64:: from ( color. rgb . 0 ) / 255.0 ,
132
+ f64:: from ( color. rgb . 1 ) / 255.0 ,
133
+ f64:: from ( color. rgb . 2 ) / 255.0 ,
134
+ color. alpha ,
137
135
) ;
138
136
c. fill ( ) ;
139
137
} )
@@ -145,7 +143,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
145
143
to : BackendCoord ,
146
144
style : & S ,
147
145
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
148
- self . set_color ( & style. as_color ( ) ) ?;
146
+ self . set_color ( & style. color ( ) ) ?;
149
147
self . set_stroke_width ( style. stroke_width ( ) ) ?;
150
148
151
149
self . call_cairo ( |c| {
@@ -162,7 +160,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
162
160
style : & S ,
163
161
fill : bool ,
164
162
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
165
- self . set_color ( & style. as_color ( ) ) ?;
163
+ self . set_color ( & style. color ( ) ) ?;
166
164
self . set_stroke_width ( style. stroke_width ( ) ) ?;
167
165
168
166
self . call_cairo ( |c| {
@@ -185,7 +183,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
185
183
path : I ,
186
184
style : & S ,
187
185
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
188
- self . set_color ( & style. as_color ( ) ) ?;
186
+ self . set_color ( & style. color ( ) ) ?;
189
187
self . set_stroke_width ( style. stroke_width ( ) ) ?;
190
188
191
189
let mut path = path. into_iter ( ) ;
@@ -205,7 +203,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
205
203
path : I ,
206
204
style : & S ,
207
205
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
208
- self . set_color ( & style. as_color ( ) ) ?;
206
+ self . set_color ( & style. color ( ) ) ?;
209
207
self . set_stroke_width ( style. stroke_width ( ) ) ?;
210
208
211
209
let mut path = path. into_iter ( ) ;
@@ -233,7 +231,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
233
231
style : & S ,
234
232
fill : bool ,
235
233
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
236
- self . set_color ( & style. as_color ( ) ) ?;
234
+ self . set_color ( & style. color ( ) ) ?;
237
235
self . set_stroke_width ( style. stroke_width ( ) ) ?;
238
236
239
237
self . call_cairo ( |c| {
@@ -254,29 +252,28 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
254
252
} )
255
253
}
256
254
257
- fn estimate_text_size < ' b > (
255
+ fn estimate_text_size < S : BackendTextStyle > (
258
256
& self ,
259
257
text : & str ,
260
- font : & FontDesc < ' b > ,
258
+ font : & S ,
261
259
) -> Result < ( u32 , u32 ) , DrawingErrorKind < Self :: ErrorType > > {
262
- self . set_font ( & font) ?;
260
+ self . set_font ( font) ?;
263
261
self . call_cairo ( |c| {
264
262
let extents = c. text_extents ( text) ;
265
263
( extents. width as u32 , extents. height as u32 )
266
264
} )
267
265
}
268
266
269
- fn draw_text (
267
+ fn draw_text < S : BackendTextStyle > (
270
268
& mut self ,
271
269
text : & str ,
272
- style : & TextStyle ,
270
+ style : & S ,
273
271
pos : BackendCoord ,
274
272
) -> Result < ( ) , DrawingErrorKind < Self :: ErrorType > > {
275
- let font = & style. font ;
276
- let color = & style. color ;
273
+ let color = style. color ( ) ;
277
274
let ( mut x, mut y) = ( pos. 0 , pos. 1 ) ;
278
275
279
- let degree = match font . get_transform ( ) {
276
+ let degree = match style . transform ( ) {
280
277
FontTransform :: None => 0.0 ,
281
278
FontTransform :: Rotate90 => 90.0 ,
282
279
FontTransform :: Rotate180 => 180.0 ,
@@ -294,17 +291,17 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
294
291
y = 0 ;
295
292
}
296
293
297
- self . set_font ( & font ) ?;
294
+ self . set_font ( style ) ?;
298
295
self . set_color ( & color) ?;
299
296
300
297
self . call_cairo ( |c| {
301
298
let extents = c. text_extents ( text) ;
302
- let dx = match style. pos . h_pos {
299
+ let dx = match style. anchor ( ) . h_pos {
303
300
HPos :: Left => 0.0 ,
304
301
HPos :: Right => -extents. width ,
305
302
HPos :: Center => -extents. width / 2.0 ,
306
303
} ;
307
- let dy = match style. pos . v_pos {
304
+ let dy = match style. anchor ( ) . v_pos {
308
305
VPos :: Top => extents. height ,
309
306
VPos :: Center => extents. height / 2.0 ,
310
307
VPos :: Bottom => 0.0 ,
@@ -324,8 +321,8 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
324
321
#[ cfg( test) ]
325
322
mod test {
326
323
use super :: * ;
327
- use crate :: prelude:: * ;
328
- use crate :: style :: text_anchor:: { HPos , Pos , VPos } ;
324
+ use plotters :: prelude:: * ;
325
+ use plotters_backend :: text_anchor:: { HPos , Pos , VPos } ;
329
326
use std:: fs;
330
327
use std:: path:: Path ;
331
328
0 commit comments