| 1 |
use super::*; |
| 2 |
|
| 3 |
#[test] |
| 4 |
fn test_bitmap_backend() { |
| 5 |
use plotters::prelude::*; |
| 6 |
|
| 7 |
let mut buffer = vec![0; 10 * 10 * 3]; |
| 8 |
|
| 9 |
{ |
| 10 |
let back = BitMapBackend::with_buffer(&mut buffer, (10, 10)); |
| 11 |
|
| 12 |
let area = back.into_drawing_area(); |
| 13 |
area.fill(&WHITE).unwrap(); |
| 14 |
area.draw(&PathElement::new(vec![(0, 0), (10, 10)], RED.filled())) |
| 15 |
.unwrap(); |
| 16 |
area.present().unwrap(); |
| 17 |
} |
| 18 |
|
| 19 |
for i in 0..10 { |
| 20 |
assert_eq!(buffer[i * 33], 255); |
| 21 |
assert_eq!(buffer[i * 33 + 1], 0); |
| 22 |
assert_eq!(buffer[i * 33 + 2], 0); |
| 23 |
buffer[i * 33] = 255; |
| 24 |
buffer[i * 33 + 1] = 255; |
| 25 |
buffer[i * 33 + 2] = 255; |
| 26 |
} |
| 27 |
|
| 28 |
assert!(buffer.into_iter().all(|x| x == 255)); |
| 29 |
} |
| 30 |
|
| 31 |
#[cfg(test)] |
| 32 |
#[test] |
| 33 |
fn test_bitmap_backend_fill_half() { |
| 34 |
use plotters::prelude::*; |
| 35 |
let mut buffer = vec![0; 10 * 10 * 3]; |
| 36 |
|
| 37 |
{ |
| 38 |
let back = BitMapBackend::with_buffer(&mut buffer, (10, 10)); |
| 39 |
|
| 40 |
let area = back.into_drawing_area(); |
| 41 |
area.draw(&Rectangle::new([(0, 0), (5, 10)], RED.filled())) |
| 42 |
.unwrap(); |
| 43 |
area.present().unwrap(); |
| 44 |
} |
| 45 |
for x in 0..10 { |
| 46 |
for y in 0..10 { |
| 47 |
assert_eq!( |
| 48 |
buffer[(y * 10 + x) as usize * 3 + 0], |
| 49 |
if x < 5 { 255 } else { 0 } |
| 50 |
); |
| 51 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], 0); |
| 52 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], 0); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
let mut buffer = vec![0; 10 * 10 * 3]; |
| 57 |
|
| 58 |
{ |
| 59 |
let back = BitMapBackend::with_buffer(&mut buffer, (10, 10)); |
| 60 |
|
| 61 |
let area = back.into_drawing_area(); |
| 62 |
area.draw(&Rectangle::new([(0, 0), (10, 5)], RED.filled())) |
| 63 |
.unwrap(); |
| 64 |
area.present().unwrap(); |
| 65 |
} |
| 66 |
for x in 0..10 { |
| 67 |
for y in 0..10 { |
| 68 |
assert_eq!( |
| 69 |
buffer[(y * 10 + x) as usize * 3 + 0], |
| 70 |
if y < 5 { 255 } else { 0 } |
| 71 |
); |
| 72 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], 0); |
| 73 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], 0); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
#[cfg(test)] |
| 79 |
#[test] |
| 80 |
fn test_bitmap_backend_blend() { |
| 81 |
use plotters::prelude::*; |
| 82 |
let mut buffer = vec![255; 10 * 10 * 3]; |
| 83 |
|
| 84 |
{ |
| 85 |
let back = BitMapBackend::with_buffer(&mut buffer, (10, 10)); |
| 86 |
|
| 87 |
let area = back.into_drawing_area(); |
| 88 |
area.draw(&Rectangle::new( |
| 89 |
[(0, 0), (5, 10)], |
| 90 |
RGBColor(0, 100, 200).mix(0.2).filled(), |
| 91 |
)) |
| 92 |
.unwrap(); |
| 93 |
area.present().unwrap(); |
| 94 |
} |
| 95 |
|
| 96 |
for x in 0..10 { |
| 97 |
for y in 0..10 { |
| 98 |
let (r, g, b) = if x < 5 { |
| 99 |
(205, 225, 245) |
| 100 |
} else { |
| 101 |
(255, 255, 255) |
| 102 |
}; |
| 103 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 0], r); |
| 104 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], g); |
| 105 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], b); |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
#[cfg(test)] |
| 111 |
#[test] |
| 112 |
fn test_bitmap_backend_split_and_fill() { |
| 113 |
use plotters::prelude::*; |
| 114 |
let mut buffer = vec![255; 10 * 10 * 3]; |
| 115 |
|
| 116 |
{ |
| 117 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (10, 10)); |
| 118 |
|
| 119 |
for (sub_backend, color) in back.split(&[5]).into_iter().zip([&RED, &GREEN].iter()) { |
| 120 |
sub_backend.into_drawing_area().fill(*color).unwrap(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
for x in 0..10 { |
| 125 |
for y in 0..10 { |
| 126 |
let (r, g, b) = if y < 5 { (255, 0, 0) } else { (0, 255, 0) }; |
| 127 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 0], r); |
| 128 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 1], g); |
| 129 |
assert_eq!(buffer[(y * 10 + x) as usize * 3 + 2], b); |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#[cfg(test)] |
| 135 |
#[test] |
| 136 |
fn test_draw_rect_out_of_range() { |
| 137 |
use plotters::prelude::*; |
| 138 |
let mut buffer = vec![0; 1099 * 1000 * 3]; |
| 139 |
|
| 140 |
{ |
| 141 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000)); |
| 142 |
|
| 143 |
back.draw_line((1100, 0), (1100, 999), &RED.to_rgba()) |
| 144 |
.unwrap(); |
| 145 |
back.draw_line((0, 1100), (999, 1100), &RED.to_rgba()) |
| 146 |
.unwrap(); |
| 147 |
back.draw_rect((1100, 0), (1100, 999), &RED.to_rgba(), true) |
| 148 |
.unwrap(); |
| 149 |
} |
| 150 |
|
| 151 |
for x in 0..1000 { |
| 152 |
for y in 0..1000 { |
| 153 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 0], 0); |
| 154 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 1], 0); |
| 155 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 2], 0); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
#[cfg(test)] |
| 161 |
#[test] |
| 162 |
fn test_draw_rect_exclude_bottom_right() { |
| 163 |
use plotters::prelude::*; |
| 164 |
let mut buffer = vec![0; 100 * 100 * 3]; |
| 165 |
|
| 166 |
{ |
| 167 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (100, 100)); |
| 168 |
|
| 169 |
back.draw_rect((0, 0), (0, 0), &RED.to_rgba(), true) |
| 170 |
.unwrap(); |
| 171 |
} |
| 172 |
|
| 173 |
assert_eq!(&buffer[0..3], &[0, 0, 0]); |
| 174 |
} |
| 175 |
|
| 176 |
#[cfg(test)] |
| 177 |
#[test] |
| 178 |
fn test_draw_line_out_of_range() { |
| 179 |
use plotters::prelude::*; |
| 180 |
let mut buffer = vec![0; 1000 * 1000 * 3]; |
| 181 |
|
| 182 |
{ |
| 183 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000)); |
| 184 |
|
| 185 |
back.draw_line((-1000, -1000), (2000, 2000), &WHITE.to_rgba()) |
| 186 |
.unwrap(); |
| 187 |
|
| 188 |
back.draw_line((999, -1000), (999, 2000), &WHITE.to_rgba()) |
| 189 |
.unwrap(); |
| 190 |
} |
| 191 |
|
| 192 |
for x in 0..1000 { |
| 193 |
for y in 0..1000 { |
| 194 |
let expected_value = if x == y || x == 999 { 255 } else { 0 }; |
| 195 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 0], expected_value); |
| 196 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 1], expected_value); |
| 197 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 2], expected_value); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
#[cfg(test)] |
| 203 |
#[test] |
| 204 |
fn test_bitmap_blend_large() { |
| 205 |
use plotters::prelude::*; |
| 206 |
let mut buffer = vec![0; 1000 * 1000 * 3]; |
| 207 |
|
| 208 |
for fill_color in [RED, GREEN, BLUE].iter() { |
| 209 |
buffer.iter_mut().for_each(|x| *x = 0); |
| 210 |
|
| 211 |
{ |
| 212 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000)); |
| 213 |
|
| 214 |
back.draw_rect((0, 0), (1000, 1000), &WHITE.mix(0.1), true) |
| 215 |
.unwrap(); // should be (24, 24, 24) |
| 216 |
back.draw_rect((0, 0), (100, 100), &fill_color.mix(0.5), true) |
| 217 |
.unwrap(); // should be (139, 24, 24) |
| 218 |
} |
| 219 |
|
| 220 |
for x in 0..1000 { |
| 221 |
for y in 0..1000 { |
| 222 |
let expected_value = if x < 100 && y < 100 { |
| 223 |
let (r, g, b) = fill_color.to_rgba().rgb(); |
| 224 |
( |
| 225 |
if r > 0 { 139 } else { 12 }, |
| 226 |
if g > 0 { 139 } else { 12 }, |
| 227 |
if b > 0 { 139 } else { 12 }, |
| 228 |
) |
| 229 |
} else { |
| 230 |
(24, 24, 24) |
| 231 |
}; |
| 232 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 0], expected_value.0); |
| 233 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 1], expected_value.1); |
| 234 |
assert_eq!(buffer[(y * 1000 + x) as usize * 3 + 2], expected_value.2); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
#[cfg(test)] |
| 241 |
#[test] |
| 242 |
fn test_bitmap_bgrx_pixel_format() { |
| 243 |
use crate::{bitmap_pixel::BGRXPixel, BitMapBackend}; |
| 244 |
use plotters::prelude::{Color, RGBColor, BLACK, BLUE, GREEN, RED, WHITE, YELLOW}; |
| 245 |
let mut rgb_buffer = vec![0; 1000 * 1000 * 3]; |
| 246 |
let mut bgrx_buffer = vec![0; 1000 * 1000 * 4]; |
| 247 |
|
| 248 |
{ |
| 249 |
let mut rgb_back = BitMapBackend::with_buffer(&mut rgb_buffer, (1000, 1000)); |
| 250 |
let mut bgrx_back = |
| 251 |
BitMapBackend::<BGRXPixel>::with_buffer_and_format(&mut bgrx_buffer, (1000, 1000)) |
| 252 |
.unwrap(); |
| 253 |
|
| 254 |
rgb_back |
| 255 |
.draw_rect((0, 0), (1000, 1000), &BLACK, true) |
| 256 |
.unwrap(); |
| 257 |
bgrx_back |
| 258 |
.draw_rect((0, 0), (1000, 1000), &BLACK, true) |
| 259 |
.unwrap(); |
| 260 |
|
| 261 |
rgb_back |
| 262 |
.draw_rect( |
| 263 |
(0, 0), |
| 264 |
(1000, 1000), |
| 265 |
&RGBColor(0xaa, 0xbb, 0xcc).mix(0.85), |
| 266 |
true, |
| 267 |
) |
| 268 |
.unwrap(); |
| 269 |
bgrx_back |
| 270 |
.draw_rect( |
| 271 |
(0, 0), |
| 272 |
(1000, 1000), |
| 273 |
&RGBColor(0xaa, 0xbb, 0xcc).mix(0.85), |
| 274 |
true, |
| 275 |
) |
| 276 |
.unwrap(); |
| 277 |
|
| 278 |
rgb_back |
| 279 |
.draw_rect((0, 0), (1000, 1000), &RED.mix(0.85), true) |
| 280 |
.unwrap(); |
| 281 |
bgrx_back |
| 282 |
.draw_rect((0, 0), (1000, 1000), &RED.mix(0.85), true) |
| 283 |
.unwrap(); |
| 284 |
|
| 285 |
rgb_back.draw_circle((300, 300), 100, &GREEN, true).unwrap(); |
| 286 |
bgrx_back |
| 287 |
.draw_circle((300, 300), 100, &GREEN, true) |
| 288 |
.unwrap(); |
| 289 |
|
| 290 |
rgb_back.draw_rect((10, 10), (50, 50), &BLUE, true).unwrap(); |
| 291 |
bgrx_back |
| 292 |
.draw_rect((10, 10), (50, 50), &BLUE, true) |
| 293 |
.unwrap(); |
| 294 |
|
| 295 |
rgb_back |
| 296 |
.draw_rect((10, 10), (50, 50), &WHITE, true) |
| 297 |
.unwrap(); |
| 298 |
bgrx_back |
| 299 |
.draw_rect((10, 10), (50, 50), &WHITE, true) |
| 300 |
.unwrap(); |
| 301 |
|
| 302 |
rgb_back |
| 303 |
.draw_rect((10, 10), (15, 50), &YELLOW, true) |
| 304 |
.unwrap(); |
| 305 |
bgrx_back |
| 306 |
.draw_rect((10, 10), (15, 50), &YELLOW, true) |
| 307 |
.unwrap(); |
| 308 |
} |
| 309 |
|
| 310 |
for x in 0..1000 { |
| 311 |
for y in 0..1000 { |
| 312 |
assert!( |
| 313 |
(rgb_buffer[y * 3000 + x * 3 + 0] as i32 |
| 314 |
- bgrx_buffer[y * 4000 + x * 4 + 2] as i32) |
| 315 |
.abs() |
| 316 |
<= 1 |
| 317 |
); |
| 318 |
assert!( |
| 319 |
(rgb_buffer[y * 3000 + x * 3 + 1] as i32 |
| 320 |
- bgrx_buffer[y * 4000 + x * 4 + 1] as i32) |
| 321 |
.abs() |
| 322 |
<= 1 |
| 323 |
); |
| 324 |
assert!( |
| 325 |
(rgb_buffer[y * 3000 + x * 3 + 2] as i32 |
| 326 |
- bgrx_buffer[y * 4000 + x * 4 + 0] as i32) |
| 327 |
.abs() |
| 328 |
<= 1 |
| 329 |
); |
| 330 |
} |
| 331 |
} |
| 332 |
} |
| 333 |
#[cfg(test)] |
| 334 |
#[test] |
| 335 |
fn test_draw_simple_lines() { |
| 336 |
use plotters::prelude::*; |
| 337 |
let mut buffer = vec![0; 1000 * 1000 * 3]; |
| 338 |
|
| 339 |
{ |
| 340 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000)); |
| 341 |
back.draw_line((500, 0), (500, 1000), &WHITE.filled().stroke_width(5)) |
| 342 |
.unwrap(); |
| 343 |
} |
| 344 |
|
| 345 |
let nz_count = buffer.into_iter().filter(|x| *x != 0).count(); |
| 346 |
|
| 347 |
assert_eq!(nz_count, 6 * 1000 * 3); |
| 348 |
} |
| 349 |
|
| 350 |
#[cfg(test)] |
| 351 |
#[test] |
| 352 |
fn test_bitmap_blit() { |
| 353 |
let src_bitmap: Vec<u8> = (0..100) |
| 354 |
.map(|y| (0..300).map(move |x| ((x * y) % 253) as u8)) |
| 355 |
.flatten() |
| 356 |
.collect(); |
| 357 |
|
| 358 |
use plotters::prelude::*; |
| 359 |
let mut buffer = vec![0; 1000 * 1000 * 3]; |
| 360 |
|
| 361 |
{ |
| 362 |
let mut back = BitMapBackend::with_buffer(&mut buffer, (1000, 1000)); |
| 363 |
back.blit_bitmap((500, 500), (100, 100), &src_bitmap[..]) |
| 364 |
.unwrap(); |
| 365 |
} |
| 366 |
|
| 367 |
for y in 0..1000 { |
| 368 |
for x in 0..1000 { |
| 369 |
if x >= 500 && x < 600 && y >= 500 && y < 600 { |
| 370 |
let lx = x - 500; |
| 371 |
let ly = y - 500; |
| 372 |
assert_eq!(buffer[y * 3000 + x * 3 + 0] as usize, (ly * lx * 3) % 253); |
| 373 |
assert_eq!( |
| 374 |
buffer[y * 3000 + x * 3 + 1] as usize, |
| 375 |
(ly * (lx * 3 + 1)) % 253 |
| 376 |
); |
| 377 |
assert_eq!( |
| 378 |
buffer[y * 3000 + x * 3 + 2] as usize, |
| 379 |
(ly * (lx * 3 + 2)) % 253 |
| 380 |
); |
| 381 |
} else { |
| 382 |
assert_eq!(buffer[y * 3000 + x * 3 + 0], 0); |
| 383 |
assert_eq!(buffer[y * 3000 + x * 3 + 1], 0); |
| 384 |
assert_eq!(buffer[y * 3000 + x * 3 + 2], 0); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
#[cfg(all(not(target_arch = "wasm32"), feature = "image"))] |
| 391 |
#[cfg(test)] |
| 392 |
mod test { |
| 393 |
use crate::BitMapBackend; |
| 394 |
use image::{ImageBuffer, Rgb}; |
| 395 |
use plotters::prelude::*; |
| 396 |
use plotters::style::text_anchor::{HPos, Pos, VPos}; |
| 397 |
use std::fs; |
| 398 |
use std::path::Path; |
| •399 |
use std::path::PathBuf; |
| 400 |
|
| •401 |
fn get_dst_dir() -> PathBuf { |
| 402 |
if let Some(cargo_target_dir) = option_env!("CARGO_TARGET_DIR") { |
| 403 |
return Path::new(cargo_target_dir).join("bitmap"); |
| 404 |
} else { |
| 405 |
return Path::new("target/test/bitmap").to_path_buf(); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
fn checked_save_file(name: &str, content: &[u8], w: u32, h: u32) { |
| 410 |
/* |
| 411 |
Please use the PNG file to manually verify the results. |
| 412 |
*/ |
| 413 |
assert!(content.iter().any(|x| *x != 0)); |
| •414 |
fs::create_dir_all(get_dst_dir()).unwrap(); |
| 415 |
let file_name = format!("{}.png", name); |
| •416 |
let file_path = get_dst_dir().join(file_name); |
| 417 |
println!("{:?} created", file_path); |
| 418 |
let img = ImageBuffer::<Rgb<u8>, &[u8]>::from_raw(w, h, content).unwrap(); |
| 419 |
img.save(&file_path).unwrap(); |
| 420 |
} |
| 421 |
|
| 422 |
fn draw_mesh_with_custom_ticks(tick_size: i32, test_name: &str) { |
| 423 |
let (width, height) = (500, 500); |
| 424 |
let mut buffer = vec![0; (width * height * 3) as usize]; |
| 425 |
{ |
| 426 |
let root = BitMapBackend::with_buffer(&mut buffer, (width, height)).into_drawing_area(); |
| 427 |
root.fill(&WHITE).unwrap(); |
| 428 |
|
| 429 |
let mut chart = ChartBuilder::on(&root) |
| 430 |
.caption("This is a test", ("sans-serif", 20)) |
| 431 |
.set_all_label_area_size(40) |
| 432 |
.build_cartesian_2d(0..10, 0..10) |
| 433 |
.unwrap(); |
| 434 |
|
| 435 |
chart |
| 436 |
.configure_mesh() |
| 437 |
.set_all_tick_mark_size(tick_size) |
| 438 |
.draw() |
| 439 |
.unwrap(); |
| 440 |
} |
| 441 |
checked_save_file(test_name, &buffer, width, height); |
| 442 |
} |
| 443 |
|
| 444 |
#[test] |
| 445 |
fn test_draw_mesh_no_ticks() { |
| 446 |
draw_mesh_with_custom_ticks(0, "test_draw_mesh_no_ticks"); |
| 447 |
} |
| 448 |
|
| 449 |
#[test] |
| 450 |
fn test_draw_mesh_negative_ticks() { |
| 451 |
draw_mesh_with_custom_ticks(-10, "test_draw_mesh_negative_ticks"); |
| 452 |
} |
| 453 |
|
| 454 |
#[test] |
| 455 |
fn test_text_draw() { |
| 456 |
let (width, height) = (1500, 800); |
| 457 |
let mut buffer = vec![0; (width * height * 3) as usize]; |
| 458 |
{ |
| 459 |
let root = BitMapBackend::with_buffer(&mut buffer, (width, height)).into_drawing_area(); |
| 460 |
root.fill(&WHITE).unwrap(); |
| 461 |
let root = root |
| 462 |
.titled("Image Title", ("sans-serif", 60).into_font()) |
| 463 |
.unwrap(); |
| 464 |
|
| 465 |
let mut chart = ChartBuilder::on(&root) |
| 466 |
.caption("All anchor point positions", ("sans-serif", 20)) |
| 467 |
.set_all_label_area_size(40) |
| 468 |
.build_cartesian_2d(0..100, 0..50) |
| 469 |
.unwrap(); |
| 470 |
|
| 471 |
chart |
| 472 |
.configure_mesh() |
| 473 |
.disable_x_mesh() |
| 474 |
.disable_y_mesh() |
| 475 |
.x_desc("X Axis") |
| 476 |
.y_desc("Y Axis") |
| 477 |
.draw() |
| 478 |
.unwrap(); |
| 479 |
|
| 480 |
let ((x1, y1), (x2, y2), (x3, y3)) = ((-30, 30), (0, -30), (30, 30)); |
| 481 |
|
| 482 |
for (dy, trans) in [ |
| 483 |
FontTransform::None, |
| 484 |
FontTransform::Rotate90, |
| 485 |
FontTransform::Rotate180, |
| 486 |
FontTransform::Rotate270, |
| 487 |
] |
| 488 |
.iter() |
| 489 |
.enumerate() |
| 490 |
{ |
| 491 |
for (dx1, h_pos) in [HPos::Left, HPos::Right, HPos::Center].iter().enumerate() { |
| 492 |
for (dx2, v_pos) in [VPos::Top, VPos::Center, VPos::Bottom].iter().enumerate() { |
| 493 |
let x = 150_i32 + (dx1 as i32 * 3 + dx2 as i32) * 150; |
| 494 |
let y = 120 + dy as i32 * 150; |
| 495 |
let draw = |x, y, text| { |
| 496 |
root.draw(&Circle::new((x, y), 3, &BLACK.mix(0.5))).unwrap(); |
| 497 |
let style = TextStyle::from(("sans-serif", 20).into_font()) |
| 498 |
.pos(Pos::new(*h_pos, *v_pos)) |
| 499 |
.transform(trans.clone()); |
| 500 |
root.draw_text(text, &style, (x, y)).unwrap(); |
| 501 |
}; |
| 502 |
draw(x + x1, y + y1, "dood"); |
| 503 |
draw(x + x2, y + y2, "dog"); |
| 504 |
draw(x + x3, y + y3, "goog"); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
} |
| 509 |
checked_save_file("test_text_draw", &buffer, width, height); |
| 510 |
} |
| 511 |
|
| 512 |
#[test] |
| 513 |
fn test_text_clipping() { |
| 514 |
let (width, height) = (500_i32, 500_i32); |
| 515 |
let mut buffer = vec![0; (width * height * 3) as usize]; |
| 516 |
{ |
| 517 |
let root = BitMapBackend::with_buffer(&mut buffer, (width as u32, height as u32)) |
| 518 |
.into_drawing_area(); |
| 519 |
root.fill(&WHITE).unwrap(); |
| 520 |
|
| 521 |
let style = TextStyle::from(("sans-serif", 20).into_font()) |
| 522 |
.pos(Pos::new(HPos::Center, VPos::Center)); |
| 523 |
root.draw_text("TOP LEFT", &style, (0, 0)).unwrap(); |
| 524 |
root.draw_text("TOP CENTER", &style, (width / 2, 0)) |
| 525 |
.unwrap(); |
| 526 |
root.draw_text("TOP RIGHT", &style, (width, 0)).unwrap(); |
| 527 |
|
| 528 |
root.draw_text("MIDDLE LEFT", &style, (0, height / 2)) |
| 529 |
.unwrap(); |
| 530 |
root.draw_text("MIDDLE RIGHT", &style, (width, height / 2)) |
| 531 |
.unwrap(); |
| 532 |
|
| 533 |
root.draw_text("BOTTOM LEFT", &style, (0, height)).unwrap(); |
| 534 |
root.draw_text("BOTTOM CENTER", &style, (width / 2, height)) |
| 535 |
.unwrap(); |
| 536 |
root.draw_text("BOTTOM RIGHT", &style, (width, height)) |
| 537 |
.unwrap(); |
| 538 |
} |
| 539 |
checked_save_file("test_text_clipping", &buffer, width as u32, height as u32); |
| 540 |
} |
| 541 |
|
| 542 |
#[test] |
| 543 |
fn test_series_labels() { |
| 544 |
let (width, height) = (500, 500); |
| 545 |
let mut buffer = vec![0; (width * height * 3) as usize]; |
| 546 |
{ |
| 547 |
let root = BitMapBackend::with_buffer(&mut buffer, (width, height)).into_drawing_area(); |
| 548 |
root.fill(&WHITE).unwrap(); |
| 549 |
|
| 550 |
let mut chart = ChartBuilder::on(&root) |
| 551 |
.caption("All series label positions", ("sans-serif", 20)) |
| 552 |
.set_all_label_area_size(40) |
| 553 |
.build_cartesian_2d(0..50, 0..50) |
| 554 |
.unwrap(); |
| 555 |
|
| 556 |
chart |
| 557 |
.configure_mesh() |
| 558 |
.disable_x_mesh() |
| 559 |
.disable_y_mesh() |
| 560 |
.draw() |
| 561 |
.unwrap(); |
| 562 |
|
| 563 |
chart |
| 564 |
.draw_series(std::iter::once(Circle::new((5, 15), 5, &RED))) |
| 565 |
.expect("Drawing error") |
| 566 |
.label("Series 1") |
| 567 |
.legend(|(x, y)| Circle::new((x, y), 3, RED.filled())); |
| 568 |
|
| 569 |
chart |
| 570 |
.draw_series(std::iter::once(Circle::new((5, 15), 10, &BLUE))) |
| 571 |
.expect("Drawing error") |
| 572 |
.label("Series 2") |
| 573 |
.legend(|(x, y)| Circle::new((x, y), 3, BLUE.filled())); |
| 574 |
|
| 575 |
for pos in vec![ |
| 576 |
SeriesLabelPosition::UpperLeft, |
| 577 |
SeriesLabelPosition::MiddleLeft, |
| 578 |
SeriesLabelPosition::LowerLeft, |
| 579 |
SeriesLabelPosition::UpperMiddle, |
| 580 |
SeriesLabelPosition::MiddleMiddle, |
| 581 |
SeriesLabelPosition::LowerMiddle, |
| 582 |
SeriesLabelPosition::UpperRight, |
| 583 |
SeriesLabelPosition::MiddleRight, |
| 584 |
SeriesLabelPosition::LowerRight, |
| 585 |
SeriesLabelPosition::Coordinate(70, 70), |
| 586 |
] |
| 587 |
.into_iter() |
| 588 |
{ |
| 589 |
chart |
| 590 |
.configure_series_labels() |
| 591 |
.border_style(&BLACK.mix(0.5)) |
| 592 |
.position(pos) |
| 593 |
.draw() |
| 594 |
.expect("Drawing error"); |
| 595 |
} |
| 596 |
} |
| 597 |
checked_save_file("test_series_labels", &buffer, width, height); |
| 598 |
} |
| 599 |
|
| 600 |
#[test] |
| 601 |
fn test_draw_pixel_alphas() { |
| 602 |
let (width, height) = (100_i32, 100_i32); |
| 603 |
let mut buffer = vec![0; (width * height * 3) as usize]; |
| 604 |
{ |
| 605 |
let root = BitMapBackend::with_buffer(&mut buffer, (width as u32, height as u32)) |
| 606 |
.into_drawing_area(); |
| 607 |
root.fill(&WHITE).unwrap(); |
| 608 |
for i in -20..20 { |
| 609 |
let alpha = i as f64 * 0.1; |
| 610 |
root.draw_pixel((50 + i, 50 + i), &BLACK.mix(alpha)) |
| 611 |
.unwrap(); |
| 612 |
} |
| 613 |
} |
| 614 |
checked_save_file( |
| 615 |
"test_draw_pixel_alphas", |
| 616 |
&buffer, |
| 617 |
width as u32, |
| 618 |
height as u32, |
| 619 |
); |
| 620 |
} |
| 621 |
} |