aboutsummaryrefslogtreecommitdiff
path: root/src/shapes.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/shapes.rs')
-rw-r--r--src/shapes.rs97
1 files changed, 38 insertions, 59 deletions
diff --git a/src/shapes.rs b/src/shapes.rs
index 2b8db2c..bf408be 100644
--- a/src/shapes.rs
+++ b/src/shapes.rs
@@ -1,95 +1,74 @@
+use cairo::{Context, Error};
+use std::f64::consts::PI;
use wmap_parser::Shape;
/// Draws a vertex shape at the given position
pub fn draw_vertex(
- ctx: &cairo::Context,
+ context: &Context,
shape: &Shape,
x: f64,
y: f64,
width: f64,
height: f64,
-) -> Result<(), cairo::Error> {
+) -> Result<(), Error> {
match shape {
- Shape::Circle => draw_circle(ctx, x, y, width, height),
- Shape::Square => draw_square(ctx, x, y, width, height),
- Shape::Triangle => draw_triangle(ctx, x, y, width, height),
- Shape::X => draw_x(ctx, x, y, width, height),
+ Shape::Circle => draw_circle(context, x, y, width, height),
+ Shape::Square => draw_square(context, x, y, width, height),
+ Shape::Triangle => draw_triangle(context, x, y, width, height),
+ Shape::X => draw_x(context, x, y, width, height),
}
}
-fn draw_circle(
- ctx: &cairo::Context,
- x: f64,
- y: f64,
- width: f64,
- height: f64,
-) -> Result<(), cairo::Error> {
- ctx.save()?;
- ctx.translate(x + width / 2.0, y + height / 2.0);
- ctx.scale(width / 2.0, height / 2.0);
- ctx.arc(0.0, 0.0, 1.0, 0.0, 2.0 * std::f64::consts::PI);
- ctx.restore()?;
- ctx.fill()?;
+fn draw_circle(context: &Context, x: f64, y: f64, width: f64, height: f64) -> Result<(), Error> {
+ context.save()?;
+ context.translate(x + width / 2.0, y + height / 2.0);
+ context.scale(width / 2.0, height / 2.0);
+ context.arc(0.0, 0.0, 1.0, 0.0, 2.0 * PI);
+ context.restore()?;
+ context.fill()?;
Ok(())
}
-fn draw_square(
- ctx: &cairo::Context,
- x: f64,
- y: f64,
- width: f64,
- height: f64,
-) -> Result<(), cairo::Error> {
- ctx.rectangle(x, y, width, height);
- ctx.fill()?;
+fn draw_square(context: &Context, x: f64, y: f64, width: f64, height: f64) -> Result<(), Error> {
+ context.rectangle(x, y, width, height);
+ context.fill()?;
Ok(())
}
-fn draw_triangle(
- ctx: &cairo::Context,
- x: f64,
- y: f64,
- width: f64,
- height: f64,
-) -> Result<(), cairo::Error> {
- ctx.move_to(x + width / 2.0, y);
- ctx.line_to(x + width, y + height);
- ctx.line_to(x, y + height);
- ctx.close_path();
- ctx.fill()?;
+fn draw_triangle(context: &Context, x: f64, y: f64, width: f64, height: f64) -> Result<(), Error> {
+ context.move_to(x + width / 2.0, y);
+ context.line_to(x + width, y + height);
+ context.line_to(x, y + height);
+ context.close_path();
+ context.fill()?;
Ok(())
}
-fn draw_x(
- ctx: &cairo::Context,
- x: f64,
- y: f64,
- width: f64,
- height: f64,
-) -> Result<(), cairo::Error> {
+fn draw_x(context: &Context, x: f64, y: f64, width: f64, height: f64) -> Result<(), Error> {
let line_width = 2.0;
- ctx.set_line_width(line_width);
- ctx.move_to(x, y);
- ctx.line_to(x + width, y + height);
- ctx.move_to(x + width, y);
- ctx.line_to(x, y + height);
- ctx.stroke()?;
+ context.set_line_width(line_width);
+ context.move_to(x, y);
+ context.line_to(x + width, y + height);
+ context.move_to(x + width, y);
+ context.line_to(x, y + height);
+ context.stroke()?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
+ use cairo::{Context, Format, ImageSurface};
#[test]
fn test_draw_shapes() {
// Just verify the functions don't panic
- let surface = cairo::ImageSurface::create(cairo::Format::Rgb24, 100, 100).unwrap();
- let ctx = cairo::Context::new(&surface).unwrap();
+ let surface = ImageSurface::create(Format::Rgb24, 100, 100).unwrap();
+ let context = Context::new(&surface).unwrap();
- assert!(draw_vertex(&ctx, &Shape::Circle, 0.0, 0.0, 25.0, 25.0).is_ok());
- assert!(draw_vertex(&ctx, &Shape::Square, 0.0, 0.0, 25.0, 25.0).is_ok());
- assert!(draw_vertex(&ctx, &Shape::Triangle, 0.0, 0.0, 25.0, 25.0).is_ok());
- assert!(draw_vertex(&ctx, &Shape::X, 0.0, 0.0, 25.0, 25.0).is_ok());
+ assert!(draw_vertex(&context, &Shape::Circle, 0.0, 0.0, 25.0, 25.0).is_ok());
+ assert!(draw_vertex(&context, &Shape::Square, 0.0, 0.0, 25.0, 25.0).is_ok());
+ assert!(draw_vertex(&context, &Shape::Triangle, 0.0, 0.0, 25.0, 25.0).is_ok());
+ assert!(draw_vertex(&context, &Shape::X, 0.0, 0.0, 25.0, 25.0).is_ok());
}
}