Crate smol_atlas

Crate smol_atlas 

Source
Expand description

Shelf Best Height Fit 2D atlas allocator (Rust port of smol-atlas).

This crate implements a dynamic 2D atlas allocator using the Shelf Best Height Fit heuristic. Items are added with add and removed with remove; shelves are never merged or resized, and free spans within each shelf are tracked as sorted segments.

§Example

use smol_atlas::Atlas;

// Create a 128x128 atlas
let mut atlas = Atlas::new(128, 128);

// Insert a few items
let a = atlas.add(32, 16).expect("fits");
let b = atlas.add(16, 16).expect("fits");

// Query positions
let (ax, ay) = (atlas.item_x(a).unwrap(), atlas.item_y(a).unwrap());
let (bx, by) = (atlas.item_x(b).unwrap(), atlas.item_y(b).unwrap());
assert!(ax <= bx || ay <= by);

// Remove one item and insert another to reuse space
assert!(atlas.remove(a));
let c = atlas.add(32, 16).expect("should reuse free span");
assert_eq!(atlas.item_y(c), Some(ay));

Structs§

Atlas
Shelf-based 2D atlas allocator (dimensions are u32).
ItemId
Opaque handle to an item stored inside the atlas.