1use crate::coord::cartesian::{Cartesian2d, MeshLine};
2use crate::coord::ranged1d::{KeyPointHint, Ranged};
3use crate::coord::{CoordTranslate, Shift};
4use crate::element::{Drawable, PointCollection};
5use crate::style::text_anchor::{HPos, Pos, VPos};
6use crate::style::{Color, SizeDesc, TextStyle};
7
8use plotters_backend::{BackendCoord, DrawingBackend, DrawingErrorKind};
10
11use std::borrow::Borrow;
12use std::cell::RefCell;
13use std::error::Error;
14use std::iter::{once, repeat};
15use std::ops::Range;
16use std::rc::Rc;
17
18#[derive(Clone, Debug)]
20struct Rect {
21 x0: i32,
22 y0: i32,
23 x1: i32,
24 y1: i32,
25}
26
27impl Rect {
28 fn split<'a, BPI: IntoIterator<Item = &'a i32> + 'a>(
30 &'a self,
31 break_points: BPI,
32 vertical: bool,
33 ) -> impl Iterator<Item = Rect> + 'a {
34 let (mut x0, mut y0) = (self.x0, self.y0);
35 let (full_x, full_y) = (self.x1, self.y1);
36 break_points
37 .into_iter()
38 .chain(once(if vertical { &self.y1 } else { &self.x1 }))
39 .map(move |&p| {
40 let x1 = if vertical { full_x } else { p };
41 let y1 = if vertical { p } else { full_y };
42 let ret = Rect { x0, y0, x1, y1 };
43
44 if vertical {
45 y0 = y1
46 } else {
47 x0 = x1;
48 }
49
50 ret
51 })
52 }
53
54 fn split_evenly<'a>(&'a self, (row, col): (usize, usize)) -> impl Iterator<Item = Rect> + 'a {
56 fn compute_evenly_split(from: i32, to: i32, n: usize, idx: usize) -> i32 {
57 let size = (to - from) as usize;
58 from + idx as i32 * (size / n) as i32 + idx.min(size % n) as i32
59 }
60 (0..row)
61 .map(move |x| repeat(x).zip(0..col))
62 .flatten()
63 .map(move |(ri, ci)| Self {
64 y0: compute_evenly_split(self.y0, self.y1, row, ri),
65 y1: compute_evenly_split(self.y0, self.y1, row, ri + 1),
66 x0: compute_evenly_split(self.x0, self.x1, col, ci),
67 x1: compute_evenly_split(self.x0, self.x1, col, ci + 1),
68 })
69 }
70
71 fn split_grid(
72 &self,
73 x_breaks: impl Iterator<Item = i32>,
74 y_breaks: impl Iterator<Item = i32>,
75 ) -> impl Iterator<Item = Rect> {
76 let mut xs = vec![self.x0, self.x1];
77 let mut ys = vec![self.y0, self.y1];
78 xs.extend(x_breaks.map(|v| v + self.x0));
79 ys.extend(y_breaks.map(|v| v + self.y0));
80
81 xs.sort();
82 ys.sort();
83
84 let xsegs: Vec<_> = xs
85 .iter()
86 .zip(xs.iter().skip(1))
87 .map(|(a, b)| (*a, *b))
88 .collect();
89 let ysegs: Vec<_> = ys
90 .iter()
91 .zip(ys.iter().skip(1))
92 .map(|(a, b)| (*a, *b))
93 .collect();
94
95 ysegs
96 .into_iter()
97 .map(move |(y0, y1)| {
98 xsegs
99 .clone()
100 .into_iter()
101 .map(move |(x0, x1)| Self { x0, y0, x1, y1 })
102 })
103 .flatten()
104 }
105
106 fn truncate(&self, p: (i32, i32)) -> (i32, i32) {
108 (p.0.min(self.x1).max(self.x0), p.1.min(self.y1).max(self.y0))
109 }
110}
111
112pub struct DrawingArea<DB: DrawingBackend, CT: CoordTranslate> {
118 backend: Rc<RefCell<DB>>,
119 rect: Rect,
120 coord: CT,
121}
122
123impl<DB: DrawingBackend, CT: CoordTranslate + Clone> Clone for DrawingArea<DB, CT> {
124 fn clone(&self) -> Self {
125 Self {
126 backend: self.backend.clone(),
127 rect: self.rect.clone(),
128 coord: self.coord.clone(),
129 }
130 }
131}
132
133#[derive(Debug)]
135pub enum DrawingAreaErrorKind<E: Error + Send + Sync> {
136 BackendError(DrawingErrorKind<E>),
138 SharingError,
142 LayoutError,
144}
145
146impl<E: Error + Send + Sync> std::fmt::Display for DrawingAreaErrorKind<E> {
147 fn fmt(&self, fmt: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
148 match self {
149 DrawingAreaErrorKind::BackendError(e) => write!(fmt, "backend error: {}", e),
150 DrawingAreaErrorKind::SharingError => {
151 write!(fmt, "Multiple backend operation in progress")
152 }
153 DrawingAreaErrorKind::LayoutError => write!(fmt, "Bad layout"),
154 }
155 }
156}
157
158impl<E: Error + Send + Sync> Error for DrawingAreaErrorKind<E> {}
159
160#[allow(type_alias_bounds)]
161type DrawingAreaError<T: DrawingBackend> = DrawingAreaErrorKind<T::ErrorType>;
162
163impl<DB: DrawingBackend> From<DB> for DrawingArea<DB, Shift> {
164 fn from(backend: DB) -> Self {
165 Self::with_rc_cell(Rc::new(RefCell::new(backend)))
166 }
167}
168
169impl<'a, DB: DrawingBackend> From<&'a Rc<RefCell<DB>>> for DrawingArea<DB, Shift> {
170 fn from(backend: &'a Rc<RefCell<DB>>) -> Self {
171 Self::with_rc_cell(backend.clone())
172 }
173}
174
175pub trait IntoDrawingArea: DrawingBackend + Sized {
177 fn into_drawing_area(self) -> DrawingArea<Self, Shift>;
179}
180
181impl<T: DrawingBackend> IntoDrawingArea for T {
182 fn into_drawing_area(self) -> DrawingArea<T, Shift> {
183 self.into()
184 }
185}
186
187impl<DB: DrawingBackend, X: Ranged, Y: Ranged> DrawingArea<DB, Cartesian2d<X, Y>> {
188 pub fn draw_mesh<DrawFunc, YH: KeyPointHint, XH: KeyPointHint>(
190 &self,
191 mut draw_func: DrawFunc,
192 y_count_max: YH,
193 x_count_max: XH,
194 ) -> Result<(), DrawingAreaErrorKind<DB::ErrorType>>
195 where
196 DrawFunc: FnMut(&mut DB, MeshLine<X, Y>) -> Result<(), DrawingErrorKind<DB::ErrorType>>,
197 {
198 self.backend_ops(move |b| {
199 self.coord
200 .draw_mesh(y_count_max, x_count_max, |line| draw_func(b, line))
201 })
202 }
203
204 pub fn get_x_range(&self) -> Range<X::ValueType> {
206 self.coord.get_x_range()
207 }
208
209 pub fn get_y_range(&self) -> Range<Y::ValueType> {
211 self.coord.get_y_range()
212 }
213
214 pub fn get_x_axis_pixel_range(&self) -> Range<i32> {
215 self.coord.get_x_axis_pixel_range()
216 }
217
218 pub fn get_y_axis_pixel_range(&self) -> Range<i32> {
219 self.coord.get_y_axis_pixel_range()
220 }
221}
222
223impl<DB: DrawingBackend, CT: CoordTranslate> DrawingArea<DB, CT> {
224 pub fn get_base_pixel(&self) -> BackendCoord {
226 (self.rect.x0, self.rect.y0)
227 }
228
229 pub fn strip_coord_spec(&self) -> DrawingArea<DB, Shift> {
231 DrawingArea {
232 rect: self.rect.clone(),
233 backend: self.backend.clone(),
234 coord: Shift((self.rect.x0, self.rect.y0)),
235 }
236 }
237
238 pub fn dim_in_pixel(&self) -> (u32, u32) {
240 (
241 (self.rect.x1 - self.rect.x0) as u32,
242 (self.rect.y1 - self.rect.y0) as u32,
243 )
244 }
245
246 pub fn relative_to_height(&self, p: f64) -> f64 {
248 f64::from((self.rect.y1 - self.rect.y0).max(0)) * (p.min(1.0).max(0.0))
249 }
250
251 pub fn relative_to_width(&self, p: f64) -> f64 {
253 f64::from((self.rect.x1 - self.rect.x0).max(0)) * (p.min(1.0).max(0.0))
254 }
255
256 pub fn get_pixel_range(&self) -> (Range<i32>, Range<i32>) {
258 (self.rect.x0..self.rect.x1, self.rect.y0..self.rect.y1)
259 }
260
261 fn backend_ops<R, O: FnOnce(&mut DB) -> Result<R, DrawingErrorKind<DB::ErrorType>>>(
263 &self,
264 ops: O,
265 ) -> Result<R, DrawingAreaError<DB>> {
266 if let Ok(mut db) = self.backend.try_borrow_mut() {
267 db.ensure_prepared()
268 .map_err(DrawingAreaErrorKind::BackendError)?;
269 ops(&mut db).map_err(DrawingAreaErrorKind::BackendError)
270 } else {
271 Err(DrawingAreaErrorKind::SharingError)
272 }
273 }
274
275 pub fn fill<ColorType: Color>(&self, color: &ColorType) -> Result<(), DrawingAreaError<DB>> {
277 self.backend_ops(|backend| {
278 backend.draw_rect(
279 (self.rect.x0, self.rect.y0),
280 (self.rect.x1 - 1, self.rect.y1 - 1),
281 color,
282 true,
283 )
284 })
285 }
286
287 pub fn draw_pixel<ColorType: Color>(
289 &self,
290 pos: CT::From,
291 color: &ColorType,
292 ) -> Result<(), DrawingAreaError<DB>> {
293 let pos = self.coord.translate(&pos);
294 self.backend_ops(|b| b.draw_pixel(pos, color.color()))
295 }
296
297 pub fn present(&self) -> Result<(), DrawingAreaError<DB>> {
299 self.backend_ops(|b| b.present())
300 }
301
302 pub fn draw<'a, E>(&self, element: &'a E) -> Result<(), DrawingAreaError<DB>>
304 where
305 &'a E: PointCollection<'a, CT::From>,
306 E: Drawable<DB>,
307 {
308 let backend_coords = element.point_iter().into_iter().map(|p| {
309 let b = p.borrow();
310 self.rect.truncate(self.coord.translate(b))
311 });
312 self.backend_ops(move |b| element.draw(backend_coords, b, self.dim_in_pixel()))
313 }
314
315 pub fn map_coordinate(&self, coord: &CT::From) -> BackendCoord {
317 self.coord.translate(coord)
318 }
319
320 pub fn estimate_text_size(
329 &self,
330 text: &str,
331 style: &TextStyle,
332 ) -> Result<(u32, u32), DrawingAreaError<DB>> {
333 self.backend_ops(move |b| b.estimate_text_size(text, style))
334 }
335}
336
337impl<DB: DrawingBackend> DrawingArea<DB, Shift> {
338 fn with_rc_cell(backend: Rc<RefCell<DB>>) -> Self {
339 let (x1, y1) = RefCell::borrow(backend.borrow()).get_size();
340 Self {
341 rect: Rect {
342 x0: 0,
343 y0: 0,
344 x1: x1 as i32,
345 y1: y1 as i32,
346 },
347 backend,
348 coord: Shift((0, 0)),
349 }
350 }
351
352 pub fn shrink<A: SizeDesc, B: SizeDesc, C: SizeDesc, D: SizeDesc>(
354 mut self,
355 left_upper: (A, B),
356 dimension: (C, D),
357 ) -> DrawingArea<DB, Shift> {
358 let left_upper = (left_upper.0.in_pixels(&self), left_upper.1.in_pixels(&self));
359 let dimension = (dimension.0.in_pixels(&self), dimension.1.in_pixels(&self));
360 self.rect.x0 = self.rect.x1.min(self.rect.x0 + left_upper.0);
361 self.rect.y0 = self.rect.y1.min(self.rect.y0 + left_upper.1);
362
363 self.rect.x1 = self.rect.x0.max(self.rect.x0 + dimension.0);
364 self.rect.y1 = self.rect.y0.max(self.rect.y0 + dimension.1);
365
366 self.coord = Shift((self.rect.x0, self.rect.y0));
367
368 self
369 }
370
371 pub fn apply_coord_spec<CT: CoordTranslate>(&self, coord_spec: CT) -> DrawingArea<DB, CT> {
373 DrawingArea {
374 rect: self.rect.clone(),
375 backend: self.backend.clone(),
376 coord: coord_spec,
377 }
378 }
379
380 pub fn margin<ST: SizeDesc, SB: SizeDesc, SL: SizeDesc, SR: SizeDesc>(
382 &self,
383 top: ST,
384 bottom: SB,
385 left: SL,
386 right: SR,
387 ) -> DrawingArea<DB, Shift> {
388 let left = left.in_pixels(self);
389 let right = right.in_pixels(self);
390 let top = top.in_pixels(self);
391 let bottom = bottom.in_pixels(self);
392 DrawingArea {
393 rect: Rect {
394 x0: self.rect.x0 + left,
395 y0: self.rect.y0 + top,
396 x1: self.rect.x1 - right,
397 y1: self.rect.y1 - bottom,
398 },
399 backend: self.backend.clone(),
400 coord: Shift((self.rect.x0 + left, self.rect.y0 + top)),
401 }
402 }
403
404 pub fn split_vertically<S: SizeDesc>(&self, y: S) -> (Self, Self) {
406 let y = y.in_pixels(self);
407 let split_point = [y + self.rect.y0];
408 let mut ret = self.rect.split(split_point.iter(), true).map(|rect| Self {
409 rect: rect.clone(),
410 backend: self.backend.clone(),
411 coord: Shift((rect.x0, rect.y0)),
412 });
413
414 (ret.next().unwrap(), ret.next().unwrap())
415 }
416
417 pub fn split_horizontally<S: SizeDesc>(&self, x: S) -> (Self, Self) {
419 let x = x.in_pixels(self);
420 let split_point = [x + self.rect.x0];
421 let mut ret = self.rect.split(split_point.iter(), false).map(|rect| Self {
422 rect: rect.clone(),
423 backend: self.backend.clone(),
424 coord: Shift((rect.x0, rect.y0)),
425 });
426
427 (ret.next().unwrap(), ret.next().unwrap())
428 }
429
430 pub fn split_evenly(&self, (row, col): (usize, usize)) -> Vec<Self> {
432 self.rect
433 .split_evenly((row, col))
434 .map(|rect| Self {
435 rect: rect.clone(),
436 backend: self.backend.clone(),
437 coord: Shift((rect.x0, rect.y0)),
438 })
439 .collect()
440 }
441
442 pub fn split_by_breakpoints<
444 XSize: SizeDesc,
445 YSize: SizeDesc,
446 XS: AsRef<[XSize]>,
447 YS: AsRef<[YSize]>,
448 >(
449 &self,
450 xs: XS,
451 ys: YS,
452 ) -> Vec<Self> {
453 self.rect
454 .split_grid(
455 xs.as_ref().iter().map(|x| x.in_pixels(self)),
456 ys.as_ref().iter().map(|x| x.in_pixels(self)),
457 )
458 .map(|rect| Self {
459 rect: rect.clone(),
460 backend: self.backend.clone(),
461 coord: Shift((rect.x0, rect.y0)),
462 })
463 .collect()
464 }
465
466 pub fn titled<'a, S: Into<TextStyle<'a>>>(
468 &self,
469 text: &str,
470 style: S,
471 ) -> Result<Self, DrawingAreaError<DB>> {
472 let style = style.into();
473
474 let x_padding = (self.rect.x1 - self.rect.x0) / 2;
475
476 let (_, text_h) = self.estimate_text_size(text, &style)?;
477 let y_padding = (text_h / 2).min(5) as i32;
478
479 let style = &style.pos(Pos::new(HPos::Center, VPos::Top));
480
481 self.backend_ops(|b| {
482 b.draw_text(
483 text,
484 style,
485 (self.rect.x0 + x_padding, self.rect.y0 + y_padding),
486 )
487 })?;
488
489 Ok(Self {
490 rect: Rect {
491 x0: self.rect.x0,
492 y0: self.rect.y0 + y_padding * 2 + text_h as i32,
493 x1: self.rect.x1,
494 y1: self.rect.y1,
495 },
496 backend: self.backend.clone(),
497 coord: Shift((self.rect.x0, self.rect.y0 + y_padding * 2 + text_h as i32)),
498 })
499 }
500
501 pub fn draw_text(
503 &self,
504 text: &str,
505 style: &TextStyle,
506 pos: BackendCoord,
507 ) -> Result<(), DrawingAreaError<DB>> {
508 self.backend_ops(|b| b.draw_text(text, style, (pos.0 + self.rect.x0, pos.1 + self.rect.y0)))
509 }
510}
511
512impl<DB: DrawingBackend, CT: CoordTranslate> DrawingArea<DB, CT> {
513 pub fn into_coord_spec(self) -> CT {
514 self.coord
515 }
516
517 pub fn as_coord_spec(&self) -> &CT {
518 &self.coord
519 }
520}
521
522#[cfg(test)]
523mod drawing_area_tests {
524 use crate::{create_mocked_drawing_area, prelude::*};
525 #[test]
526 fn test_filling() {
527 let drawing_area = create_mocked_drawing_area(1024, 768, |m| {
528 m.check_draw_rect(|c, _, f, u, d| {
529 assert_eq!(c, WHITE.to_rgba());
530 assert_eq!(f, true);
531 assert_eq!(u, (0, 0));
532 assert_eq!(d, (1023, 767));
533 });
534
535 m.drop_check(|b| {
536 assert_eq!(b.num_draw_rect_call, 1);
537 assert_eq!(b.draw_count, 1);
538 });
539 });
540
541 drawing_area.fill(&WHITE).expect("Drawing Failure");
542 }
543
544 #[test]
545 fn test_split_evenly() {
546 let colors = vec![
547 &RED, &BLUE, &YELLOW, &WHITE, &BLACK, &MAGENTA, &CYAN, &BLUE, &RED,
548 ];
549 let drawing_area = create_mocked_drawing_area(902, 900, |m| {
550 for col in 0..3 {
551 for row in 0..3 {
552 let colors = colors.clone();
553 m.check_draw_rect(move |c, _, f, u, d| {
554 assert_eq!(c, colors[col * 3 + row].to_rgba());
555 assert_eq!(f, true);
556 assert_eq!(u, (300 * row as i32 + 2.min(row) as i32, 300 * col as i32));
557 assert_eq!(
558 d,
559 (
560 300 + 300 * row as i32 + 2.min(row + 1) as i32 - 1,
561 300 + 300 * col as i32 - 1
562 )
563 );
564 });
565 }
566 }
567 m.drop_check(|b| {
568 assert_eq!(b.num_draw_rect_call, 9);
569 assert_eq!(b.draw_count, 9);
570 });
571 });
572
573 drawing_area
574 .split_evenly((3, 3))
575 .iter_mut()
576 .zip(colors.iter())
577 .for_each(|(d, c)| {
578 d.fill(*c).expect("Drawing Failure");
579 });
580 }
581
582 #[test]
583 fn test_split_horizontally() {
584 let drawing_area = create_mocked_drawing_area(1024, 768, |m| {
585 m.check_draw_rect(|c, _, f, u, d| {
586 assert_eq!(c, RED.to_rgba());
587 assert_eq!(f, true);
588 assert_eq!(u, (0, 0));
589 assert_eq!(d, (345 - 1, 768 - 1));
590 });
591
592 m.check_draw_rect(|c, _, f, u, d| {
593 assert_eq!(c, BLUE.to_rgba());
594 assert_eq!(f, true);
595 assert_eq!(u, (345, 0));
596 assert_eq!(d, (1024 - 1, 768 - 1));
597 });
598
599 m.drop_check(|b| {
600 assert_eq!(b.num_draw_rect_call, 2);
601 assert_eq!(b.draw_count, 2);
602 });
603 });
604
605 let (left, right) = drawing_area.split_horizontally(345);
606 left.fill(&RED).expect("Drawing Error");
607 right.fill(&BLUE).expect("Drawing Error");
608 }
609
610 #[test]
611 fn test_split_vertically() {
612 let drawing_area = create_mocked_drawing_area(1024, 768, |m| {
613 m.check_draw_rect(|c, _, f, u, d| {
614 assert_eq!(c, RED.to_rgba());
615 assert_eq!(f, true);
616 assert_eq!(u, (0, 0));
617 assert_eq!(d, (1024 - 1, 345 - 1));
618 });
619
620 m.check_draw_rect(|c, _, f, u, d| {
621 assert_eq!(c, BLUE.to_rgba());
622 assert_eq!(f, true);
623 assert_eq!(u, (0, 345));
624 assert_eq!(d, (1024 - 1, 768 - 1));
625 });
626
627 m.drop_check(|b| {
628 assert_eq!(b.num_draw_rect_call, 2);
629 assert_eq!(b.draw_count, 2);
630 });
631 });
632
633 let (left, right) = drawing_area.split_vertically(345);
634 left.fill(&RED).expect("Drawing Error");
635 right.fill(&BLUE).expect("Drawing Error");
636 }
637
638 #[test]
639 fn test_split_grid() {
640 let colors = vec![
641 &RED, &BLUE, &YELLOW, &WHITE, &BLACK, &MAGENTA, &CYAN, &BLUE, &RED,
642 ];
643 let breaks: [i32; 5] = [100, 200, 300, 400, 500];
644
645 for nxb in 0..=5 {
646 for nyb in 0..=5 {
647 let drawing_area = create_mocked_drawing_area(1024, 768, |m| {
648 for row in 0..=nyb {
649 for col in 0..=nxb {
650 let get_bp = |full, limit, id| {
651 (if id == 0 {
652 0
653 } else if id > limit {
654 full
655 } else {
656 breaks[id as usize - 1]
657 }) as i32
658 };
659
660 let expected_u = (get_bp(1024, nxb, col), get_bp(768, nyb, row));
661 let expected_d = (
662 get_bp(1024, nxb, col + 1) - 1,
663 get_bp(768, nyb, row + 1) - 1,
664 );
665 let expected_color =
666 colors[(row * (nxb + 1) + col) as usize % colors.len()];
667
668 m.check_draw_rect(move |c, _, f, u, d| {
669 assert_eq!(c, expected_color.to_rgba());
670 assert_eq!(f, true);
671 assert_eq!(u, expected_u);
672 assert_eq!(d, expected_d);
673 });
674 }
675 }
676
677 m.drop_check(move |b| {
678 assert_eq!(b.num_draw_rect_call, ((nxb + 1) * (nyb + 1)) as u32);
679 assert_eq!(b.draw_count, ((nyb + 1) * (nxb + 1)) as u32);
680 });
681 });
682
683 let result = drawing_area
684 .split_by_breakpoints(&breaks[0..nxb as usize], &breaks[0..nyb as usize]);
685 for i in 0..result.len() {
686 result[i]
687 .fill(colors[i % colors.len()])
688 .expect("Drawing Error");
689 }
690 }
691 }
692 }
693 #[test]
694 fn test_titled() {
695 let drawing_area = create_mocked_drawing_area(1024, 768, |m| {
696 m.check_draw_text(|c, font, size, _pos, text| {
697 assert_eq!(c, BLACK.to_rgba());
698 assert_eq!(font, "serif");
699 assert_eq!(size, 30.0);
700 assert_eq!("This is the title", text);
701 });
702 m.check_draw_rect(|c, _, f, u, d| {
703 assert_eq!(c, WHITE.to_rgba());
704 assert_eq!(f, true);
705 assert_eq!(u.0, 0);
706 assert!(u.1 > 0);
707 assert_eq!(d, (1024 - 1, 768 - 1));
708 });
709 m.drop_check(|b| {
710 assert_eq!(b.num_draw_text_call, 1);
711 assert_eq!(b.num_draw_rect_call, 1);
712 assert_eq!(b.draw_count, 2);
713 });
714 });
715
716 drawing_area
717 .titled("This is the title", ("serif", 30))
718 .unwrap()
719 .fill(&WHITE)
720 .unwrap();
721 }
722
723 #[test]
724 fn test_margin() {
725 let drawing_area = create_mocked_drawing_area(1024, 768, |m| {
726 m.check_draw_rect(|c, _, f, u, d| {
727 assert_eq!(c, WHITE.to_rgba());
728 assert_eq!(f, true);
729 assert_eq!(u, (3, 1));
730 assert_eq!(d, (1024 - 4 - 1, 768 - 2 - 1));
731 });
732
733 m.drop_check(|b| {
734 assert_eq!(b.num_draw_rect_call, 1);
735 assert_eq!(b.draw_count, 1);
736 });
737 });
738
739 drawing_area
740 .margin(1, 2, 3, 4)
741 .fill(&WHITE)
742 .expect("Drawing Failure");
743 }
744
745 #[test]
746 fn test_ranges() {
747 let drawing_area = create_mocked_drawing_area(1024, 768, |_m| {})
748 .apply_coord_spec(Cartesian2d::<
749 crate::coord::types::RangedCoordi32,
750 crate::coord::types::RangedCoordu32,
751 >::new(-100..100, 0..200, (0..1024, 0..768)));
752
753 let x_range = drawing_area.get_x_range();
754 assert_eq!(x_range, -100..100);
755
756 let y_range = drawing_area.get_y_range();
757 assert_eq!(y_range, 0..200);
758 }
759
760 #[test]
761 fn test_relative_size() {
762 let drawing_area = create_mocked_drawing_area(1024, 768, |_m| {});
763
764 assert_eq!(102.4, drawing_area.relative_to_width(0.1));
765 assert_eq!(384.0, drawing_area.relative_to_height(0.5));
766
767 assert_eq!(1024.0, drawing_area.relative_to_width(1.3));
768 assert_eq!(768.0, drawing_area.relative_to_height(1.5));
769
770 assert_eq!(0.0, drawing_area.relative_to_width(-0.2));
771 assert_eq!(0.0, drawing_area.relative_to_height(-0.5));
772 }
773
774 #[test]
775 fn test_relative_split() {
776 let drawing_area = create_mocked_drawing_area(1000, 1200, |m| {
777 let mut counter = 0;
778 m.check_draw_rect(move |c, _, f, u, d| {
779 assert_eq!(f, true);
780
781 match counter {
782 0 => {
783 assert_eq!(c, RED.to_rgba());
784 assert_eq!(u, (0, 0));
785 assert_eq!(d, (300 - 1, 600 - 1));
786 }
787 1 => {
788 assert_eq!(c, BLUE.to_rgba());
789 assert_eq!(u, (300, 0));
790 assert_eq!(d, (1000 - 1, 600 - 1));
791 }
792 2 => {
793 assert_eq!(c, GREEN.to_rgba());
794 assert_eq!(u, (0, 600));
795 assert_eq!(d, (300 - 1, 1200 - 1));
796 }
797 3 => {
798 assert_eq!(c, WHITE.to_rgba());
799 assert_eq!(u, (300, 600));
800 assert_eq!(d, (1000 - 1, 1200 - 1));
801 }
802 _ => panic!("Too many draw rect"),
803 }
804
805 counter += 1;
806 });
807
808 m.drop_check(|b| {
809 assert_eq!(b.num_draw_rect_call, 4);
810 assert_eq!(b.draw_count, 4);
811 });
812 });
813
814 let split =
815 drawing_area.split_by_breakpoints([(30).percent_width()], [(50).percent_height()]);
816
817 split[0].fill(&RED).unwrap();
818 split[1].fill(&BLUE).unwrap();
819 split[2].fill(&GREEN).unwrap();
820 split[3].fill(&WHITE).unwrap();
821 }
822
823 #[test]
824 fn test_relative_shrink() {
825 let drawing_area = create_mocked_drawing_area(1000, 1200, |m| {
826 m.check_draw_rect(move |_, _, _, u, d| {
827 assert_eq!((100, 100), u);
828 assert_eq!((300 - 1, 700 - 1), d);
829 });
830
831 m.drop_check(|b| {
832 assert_eq!(b.num_draw_rect_call, 1);
833 assert_eq!(b.draw_count, 1);
834 });
835 })
836 .shrink(((10).percent_width(), 100), (200, (50).percent_height()));
837
838 drawing_area.fill(&RED).unwrap();
839 }
840}