1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//!  A module for computing position on an image.

// from rust

// from external crate

// from local crate
use error::RasterResult;

/// Enumeration for different anchor positions.
#[derive(Debug)]
pub enum PositionMode {
    TopLeft,
    TopCenter,
    TopRight,
    CenterLeft,
    Center,
    CenterRight,
    BottomLeft,
    BottomCenter,
    BottomRight,
}

/// Struct for computing position on an image.
pub struct Position {
    position: PositionMode,
    offset_x: i32,
    offset_y: i32,
}

impl Position {
    pub fn new(position: PositionMode, offset_x: i32, offset_y: i32) -> Position {
        Position {
            position: position,
            offset_x: offset_x,
            offset_y: offset_y,
        }
    }

    /// Get X and Y position based on parameters.
    // Will this ever fail?
    pub fn get_x_y(
        &self,
        canvas_width: i32,
        canvas_height: i32,
        image_width: i32,
        image_height: i32,
    ) -> RasterResult<(i32, i32)> {
        let offset_x = self.offset_x;
        let offset_y = self.offset_y;

        Ok(match self.position {
            PositionMode::TopLeft => (offset_x, offset_y),
            PositionMode::TopCenter => {
                let x = ((canvas_width / 2) - (image_width / 2)) + offset_x;
                (x, offset_y)
            }
            PositionMode::TopRight => {
                let x = (canvas_width - image_width) + offset_x;
                (x, offset_y)
            }
            PositionMode::CenterLeft => {
                let y = ((canvas_height / 2) - (image_height / 2)) + offset_x;
                (offset_x, y)
            }
            PositionMode::Center => {
                let x = ((canvas_width / 2) - (image_width / 2)) + offset_x;
                let y = ((canvas_height / 2) - (image_height / 2)) + offset_y;
                (x, y)
            }
            PositionMode::CenterRight => {
                let x = (canvas_width - image_width) + offset_x;
                let y = ((canvas_height / 2) - (image_height / 2)) + offset_y;
                (x, y)
            }
            PositionMode::BottomLeft => {
                let y = (canvas_height - image_height) + offset_y;
                (offset_x, y)
            }
            PositionMode::BottomCenter => {
                let x = ((canvas_width / 2) - (image_width / 2)) + offset_x;
                let y = (canvas_height - image_height) + offset_y;
                (x, y)
            }
            PositionMode::BottomRight => {
                let x = (canvas_width - image_width) + offset_y;
                let y = (canvas_height - image_height) + offset_y;
                (x, y)
            }
        })
    }
}