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
use std::cmp::max;
use {Config, Rect};
mod packer;
pub use self::packer::DensePacker;
#[derive(Clone)]
pub struct Packer {
config: Config,
packer : DensePacker,
}
impl Packer {
pub fn new(config: Config) -> Packer {
let width = max(0, config.width + config.rectangle_padding - 2*config.border_padding);
let height = max(0, config.height + config.rectangle_padding - 2*config.border_padding);
Packer {
config: config,
packer: DensePacker::new(width, height),
}
}
pub fn config(&self) -> Config {
self.config
}
pub fn pack(&mut self, width : i32, height : i32, allow_rotation : bool) -> Option<Rect> {
if width <= 0 || height <= 0 {
return None
}
if let Some(mut rect) = self.packer.pack(width + self.config.rectangle_padding, height + self.config.rectangle_padding, allow_rotation) {
rect.width -= self.config.rectangle_padding;
rect.height -= self.config.rectangle_padding;
rect.x += self.config.border_padding;
rect.y += self.config.border_padding;
Some(rect)
} else {
None
}
}
pub fn can_pack(&self, width : i32, height : i32, allow_rotation : bool) -> bool {
self.packer.can_pack(width + self.config.rectangle_padding, height + self.config.rectangle_padding, allow_rotation)
}
}