diff --git a/Cargo.toml b/Cargo.toml index e90d691d..7d5fec2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,7 @@ [workspace] members = [ "font-renderer", + "gfx-utils", "partitioner", "path-utils", "demo/server", diff --git a/gfx-utils/Cargo.toml b/gfx-utils/Cargo.toml new file mode 100644 index 00000000..39b25118 --- /dev/null +++ b/gfx-utils/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "gfx-utils" +version = "0.1.0" +authors = ["Patrick Walton "] + +[dependencies] +euclid = "0.17" \ No newline at end of file diff --git a/gfx-utils/src/lib.rs b/gfx-utils/src/lib.rs new file mode 100644 index 00000000..94925683 --- /dev/null +++ b/gfx-utils/src/lib.rs @@ -0,0 +1,51 @@ +// pathfinder/gfx-utils/lib.rs +// +// Copyright © 2018 The Pathfinder Project Developers. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +extern crate euclid; + +use euclid::{Point2D, Size2D, Vector2D}; +use std::cmp; + +pub struct ShelfBinPacker { + next: Point2D, + max_size: Size2D, + padding: Vector2D, + shelf_height: i32, +} + +impl ShelfBinPacker { + pub fn new(max_size: &Size2D, padding: &Vector2D) -> ShelfBinPacker { + ShelfBinPacker { + next: padding.to_point(), + max_size: *max_size, + padding: *padding, + shelf_height: 0, + } + } + + pub fn add(&mut self, size: &Size2D) -> Result, ()> { + let mut next = self.next; + let mut lower_right = Point2D::new(next.x + size.width, next.y + size.height) + + self.padding; + if lower_right.x > self.max_size.width { + next = Point2D::new(0, next.y + self.shelf_height); + self.shelf_height = 0; + lower_right = Point2D::new(size.width, next.y + size.height) + self.padding; + } + if lower_right.x > self.max_size.width || lower_right.y > self.max_size.height { + return Err(()) + } + self.shelf_height = cmp::max(self.shelf_height, size.height); + self.next = next + Vector2D::new(size.width + self.padding.x * 2, 0); + Ok(next) + } +} + +