Skip to content

Commit ad13289

Browse files
committed
Merge branch 's-textalign' of git://github.com/nuald/plotters into pr-87
2 parents b8aadaf + 556725f commit ad13289

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

src/drawing/backend_impl/cairo.rs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ mod test {
386386
#[test]
387387
fn test_text_draw() {
388388
let buffer: Vec<u8> = vec![];
389-
let (width, height) = (1000, 600);
389+
let (width, height) = (1500, 800);
390390
let surface = cairo::PsSurface::for_stream(width.into(), height.into(), buffer);
391391
let cr = CairoContext::new(&surface);
392392
let root = CairoBackend::new(&cr, (width, height))
@@ -411,6 +411,8 @@ mod test {
411411
.draw()
412412
.unwrap();
413413

414+
let ((x1, y1), (x2, y2), (x3, y3)) = ((-30, 30), (0, -30), (30, 30));
415+
414416
for (dy, trans) in [
415417
FontTransform::None,
416418
FontTransform::Rotate90,
@@ -422,13 +424,18 @@ mod test {
422424
{
423425
for (dx1, h_pos) in [HPos::Left, HPos::Right, HPos::Center].iter().enumerate() {
424426
for (dx2, v_pos) in [VPos::Top, VPos::Center, VPos::Bottom].iter().enumerate() {
425-
let x = 100_i32 + (dx1 as i32 * 3 + dx2 as i32) * 100;
426-
let y = 100 + dy as i32 * 100;
427-
root.draw(&Circle::new((x, y), 3, &BLACK.mix(0.5))).unwrap();
428-
let style = TextStyle::from(("sans-serif", 20).into_font())
429-
.pos(Pos::new(*h_pos, *v_pos))
430-
.transform(trans.clone());
431-
root.draw_text("test", &style, (x, y)).unwrap();
427+
let x = 150_i32 + (dx1 as i32 * 3 + dx2 as i32) * 150;
428+
let y = 120 + dy as i32 * 150;
429+
let draw = |x, y, text| {
430+
root.draw(&Circle::new((x, y), 3, &BLACK.mix(0.5))).unwrap();
431+
let style = TextStyle::from(("sans-serif", 20).into_font())
432+
.pos(Pos::new(*h_pos, *v_pos))
433+
.transform(trans.clone());
434+
root.draw_text(text, &style, (x, y)).unwrap();
435+
};
436+
draw(x + x1, y + y1, "dood");
437+
draw(x + x2, y + y2, "dog");
438+
draw(x + x3, y + y3, "goog");
432439
}
433440
}
434441
}
@@ -437,7 +444,9 @@ mod test {
437444
let content = String::from_utf8(buffer).unwrap();
438445
checked_save_file("test_text_draw", &content);
439446

440-
assert_eq!(content.matches("test").count(), 36);
447+
assert_eq!(content.matches("dog").count(), 36);
448+
assert_eq!(content.matches("dood").count(), 36);
449+
assert_eq!(content.matches("goog").count(), 36);
441450
}
442451

443452
#[test]
@@ -534,4 +543,25 @@ mod test {
534543
let content = String::from_utf8(buffer).unwrap();
535544
checked_save_file("test_series_labels", &content);
536545
}
546+
547+
#[test]
548+
fn test_draw_pixel_alphas() {
549+
let buffer: Vec<u8> = vec![];
550+
let (width, height) = (100_i32, 100_i32);
551+
let surface = cairo::PsSurface::for_stream(width.into(), height.into(), buffer);
552+
let cr = CairoContext::new(&surface);
553+
let root = CairoBackend::new(&cr, (width as u32, height as u32))
554+
.unwrap()
555+
.into_drawing_area();
556+
557+
for i in -20..20 {
558+
let alpha = i as f64 * 0.1;
559+
root.draw_pixel((50 + i, 50 + i), &BLACK.mix(alpha))
560+
.unwrap();
561+
}
562+
563+
let buffer = *surface.finish_output_stream().unwrap().downcast().unwrap();
564+
let content = String::from_utf8(buffer).unwrap();
565+
checked_save_file("test_draw_pixel_alphas", &content);
566+
}
537567
}

0 commit comments

Comments
 (0)