Skip to main content

fastpack_core/algorithms/
packer.rs

1use crate::{
2    error::CoreError,
3    types::{
4        config::{LayoutConfig, SpriteConfig},
5        rect::{Rect, Size},
6        sprite::Sprite,
7    },
8};
9
10/// Input consumed by a packing algorithm.
11pub struct PackInput {
12    /// Pre-processed sprites (trimmed, extruded, scaled to target variant).
13    pub sprites: Vec<Sprite>,
14    /// Atlas layout constraints.
15    pub config: LayoutConfig,
16    /// Sprite-level settings (e.g. common divisors).
17    pub sprite_config: SpriteConfig,
18}
19
20/// Position of one sprite within the atlas.
21pub struct Placement {
22    /// Matches `Sprite::id`.
23    pub sprite_id: String,
24    /// Destination rectangle within the atlas texture.
25    pub dest: Rect,
26    /// `true` when the sprite was rotated 90° clockwise.
27    pub rotated: bool,
28}
29
30/// A sprite that was successfully placed, paired with its destination in the atlas.
31pub struct PlacedSprite {
32    /// Pixel data and metadata for this sprite.
33    pub sprite: Sprite,
34    /// Position in the atlas where this sprite was placed.
35    pub placement: Placement,
36}
37
38/// Output produced by a single atlas packing pass.
39pub struct PackOutput {
40    /// Successfully placed sprites with their atlas positions.
41    pub placed: Vec<PlacedSprite>,
42    /// Actual atlas dimensions used.
43    pub atlas_size: Size,
44    /// Sprites that did not fit; forwarded to the next sheet in multipack mode.
45    pub overflow: Vec<Sprite>,
46}
47
48/// Common interface implemented by all packing algorithms.
49pub trait Packer: Send + Sync {
50    /// Pack sprites onto a single sheet and return placements.
51    fn pack(&self, input: PackInput) -> Result<PackOutput, CoreError>;
52
53    /// Short human-readable name shown in log output.
54    fn name(&self) -> &'static str;
55}