Sign in

    moon_binpack

    Industrial-grade 2D rectangle bin packing and texture atlas layout engine in pure MoonBit.

    bin-packing
    rect-pack
    texture-atlas
    maxrects
    skyline
    guillotine
    shelf
    css-sprite
    layout
    game-dev
    sprite-sheet
    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    12 hours ago
    Downloads
    1

    No dependencies

    #moon_binpack

    CI MoonBit License Tests Dependencies

    moon_binpack is an industrial-grade, zero-dependency 2D rectangle bin packing and anti-bleeding texture atlas baking engine implemented in 100% pure MoonBit.

    Targeted at 2D game rendering, Web/UI font glyph caching, CSS sprite generation, and WebGPU/WebGL build pipelines, moon_binpack provides a clear, decoupled two-tier architecture:
    1. Layer 1: Pure Geometric Topology Engine: Solves the NP-hard 2D Bin Packing Problem (2D-BPP) across 4 classic algorithm families (MaxRects, Skyline, Guillotine, Shelf) and features an automated 18-heuristic arena (best_pack) with multi-page overflow and Power-of-Two (POT) auto-growth.
    2. Layer 2: Physical Extrusion Texture Baker (Zero GPU Bleeding): In-memory RGBA pixel canvas with sub-image blitting, physical 1-pixel edge extrusion, shader half-texel UV inset compensation (supporting both DirectX/WebGPU and WebGL/OpenGL vertical axes), and pure MoonBit uncompressed 32-bit Windows Bitmap (.bmp) and Truevision TGA (.tga) encoders.
    3. Web & Game Engine Exporters: Standalone responsive SVG vector visualizers, standard JSON manifests, Pixi.js / Phaser JSON Hash format, CSS sprite rule generators, CSV CAD/spreadsheet data tables, and self-contained interactive dark-mode HTML inspection reports.


    #Features

    #Layer 1: Geometric Layout Engine

    • MaxRects (Maximal Rectangles):
      • Tracks all maximal empty spaces for optimal packing density.
      • 5 heuristic placement rules: BestShortSideFit, BestLongSideFit, BestAreaFit, BottomLeftRule, ContactPointRule.
      • Supports 90° rotation and area-descending batch insertion.
    • Skyline (Dynamic Contour Line):
      • High-speed contour line tracking for low computational overhead.
      • Heuristics: BottomLeft, MinWasteFit with dynamic segment splitting and contiguous elevation merging.
    • Guillotine Cut Packing:
      • Partitions empty space with recursive edge-to-edge guillotine cuts.
      • 6 selection heuristics: BestAreaFit, BestShortSideFit, BestLongSideFit, WorstAreaFit, WorstShortSideFit, WorstLongSideFit.
      • 6 split heuristics: ShorterLeftoverAxis, LongerLeftoverAxis, MinimizeArea, MaximizeArea, ShorterAxis, LongerAxis.
      • Adjacent free rectangle merging and batch insertion.
    • Shelf Packing:
      • Ultra-fast algorithm tailored for uniform-height items such as font glyph caches and icon sets.
      • 5 heuristics: NextFit, FirstFit, BestAreaFit, BestHeightFit, WorstAreaFit.
    • AtlasPacker & 18-Heuristic Competitive Arena (best_pack):
      • Automatically tests all 18 heuristics across all 4 algorithm families to select the most space-efficient layout.
      • Automatic multi-page overflow and Power-of-Two (POT: 64 to 4096) adaptive bin sizing.
      • Configurable inner padding and outer border margin.

    #Layer 2: Extruded Pixel Texture Baker (Zero GPU Bleeding)

    • Physical 1-Pixel Edge Extrusion (extrude_rect):
      • Replicates outer 1-pixel border colors outward into surrounding padding margins.
      • Physically eliminates dark borders and neighboring color bleeding caused by GPU bilinear filtering and mipmap interpolation.
    • Shader Half-Texel Inset UV Compensation (to_uv_ex):
      • Calculates shader-ready normalized UV intervals inset by half a texel:
      • Dual vertical axis support: top-left origin (DirectX / Vulkan / Metal / WebGPU) and bottom-left origin (flip_v = true for WebGL / OpenGL).
    • Pure MoonBit Native Image Encoders:
      • Windows Bitmap (to_bmp, to_bmp_bytes): 32-bit uncompressed DIB (.bmp) with standard BM header.
      • Truevision TGA (to_tga, to_tga_bytes): 32-bit uncompressed true-color (.tga) widely supported by game engines.
    • In-Memory Canvas Utilities:
      • Sub-image blitting with 90° rotation, solid/outline rect drawing (draw_rect), canvas clearing, and Porter-Duff alpha blending (blend_pixel).

    #Multi-Ecosystem Exporters

    • SVG (to_svg): Scalable, dark-mode SVG with occupancy badge and individual coordinate labels.
    • Standard JSON (to_json): Standardized coordinate manifest for generic WebGL / Canvas loaders.
    • Pixi.js / Phaser JSON (to_pixi_json): TexturePacker compatible JSON Hash format directly loadable in modern HTML5 game engines.
    • CSS Sprites (to_css): Negative offset background positions for web UI icons.
    • CSV (to_csv): Flat table for CAD / CNC nesting analysis and statistical inspection.
    • Interactive HTML Report (to_html): Dark-mode self-contained visual inspection dashboard with metrics cards, SVG layout preview, and coordinate data table.


    #Installation

    Add moon_binpack to your MoonBit project:

    moon add zmjknn/moon_binpack

    Or declare it in your moon.mod:

    ///|
    import {
    "zmjknn/moon_binpack",
    }


    #Quick Start

    #1. 18-Heuristic Arena (best_pack)

    let items = [
    @moon_binpack.Size::new(128, 64),
    @moon_binpack.Size::new(64, 128),
    @moon_binpack.Size::new(96, 96),
    @moon_binpack.Size::new(48, 48),
    ]

    let (best_res, winning_algo) = @moon_binpack.best_pack(
    items,
    max_width=512,
    max_height=512,
    )

    println("Winning Heuristic: " + winning_algo)
    println("Occupancy: " + (best_res.stats.occupancy * 100.0).to_string() + "%")

    #2. Physical 1-Pixel Edge Extrusion & Dual BMP / TGA Baking (Layer 2)

    let img1 = @moon_binpack.PixelBuffer::new(48, 32, color=@moon_binpack.Rgba::from_hex(0xFF4444))
    let img2 = @moon_binpack.PixelBuffer::new(32, 48, color=@moon_binpack.Rgba::from_hex(0x44FF44))

    let items = [
    @moon_binpack.ImageItem::new(1, img1),
    @moon_binpack.ImageItem::new(2, img2),
    ]

    // Automatically benchmarks layout and bakes with 1-pixel outer edge extrusion
    let baker = @moon_binpack.AtlasBaker::new(extrude_margin=1)
    let (baked_pages, winner) = baker.bake_best(items)

    let page0 = baked_pages[0]

    // Pure MoonBit zero-dependency image encoding
    let bmp_bytes = page0.image.to_bmp_bytes()
    let tga_bytes = page0.image.to_tga_bytes()

    // Half-texel UV coordinates for WebGPU / DirectX shaders
    let uv_webgpu = page0.uv_frames[0]

    #3. Exporters: SVG, CSS, PixiJS JSON, CSV, and HTML

    // Scalable SVG preview
    ///|
    let svg = @moon_binpack.to_svg(512, 512, best_res.rects, title="Atlas Preview")

    // TexturePacker / Pixi.js / Phaser JSON manifest

    ///|
    let pixi_json = @moon_binpack.to_pixi_json(
    512,
    512,
    best_res.rects,
    image_name="atlas.png",
    )

    // Production CSS sprite classes

    ///|
    let css = @moon_binpack.to_css(best_res.rects, class_prefix="sprite")

    // CSV layout table for spreadsheet / CAD

    ///|
    let csv = @moon_binpack.to_csv(best_res.rects)

    // Standalone interactive dark-mode HTML inspection dashboard

    ///|
    let html = @moon_binpack.to_html(
    512,
    512,
    best_res.rects,
    title="Quality Inspection Report",
    )


    #Command Line Interface (CLI) Demo

    Run the built-in CLI demonstration:

    moon run cmd/main


    #Testing

    Execute the automated test suite:

    moon test

    Result:

    Total tests: 48, passed: 48, failed: 0.


    #References & Attribution

    • Jukka Jylänki, "A Thousand Ways to Pack the Bin - A Practical Approach to Two-Dimensional Rectangle Bin Packing"
    • RectangleBinPack (C++) by Jukka Jylänki (Public Domain)
    • rect_packer (Rust) by Pao (MIT)


    #License

    Licensed under the Apache License, Version 2.0.

    AtlasBaker

    pub struct AtlasBaker {
    options : AtlasOptions
    extrude_margin : Int
    } derive(
    Debug
    )

    High-level 2D Texture Atlas Baker that packs sub-images into pixel buffers and applies physical 1-pixel edge extrusion to completely eliminate GPU sampling bleeding.

    AtlasBaker::bake

    fn AtlasBaker::bake(self : AtlasBaker, items : Array[ImageItem]) -> Array[BakedAtlas]

    Bakes an array of image items into one or more pixel buffer atlas pages, automatically performing:
    1. Geometric packing via AtlasPacker
    2. Pixel copying (blitting) with rotation support
    3. Physical 1-pixel edge color extrusion to eliminate GPU sampling bleeding
    4. Half-texel UV coordinate calculation for GPU shaders

    AtlasBaker::bake_best

    fn AtlasBaker::bake_best(self : AtlasBaker, items : Array[ImageItem]) -> (Array[BakedAtlas], String)

    Automatically benchmarks all 18 heuristics via best_pack and bakes image items into the most space-efficient atlas page layout. Returns the array of baked pages and the winning algorithm name.

    AtlasBaker::new

    fn AtlasBaker::new(options? : AtlasOptions, extrude_margin? : Int) -> AtlasBaker

    AtlasOptions

    pub(all) struct AtlasOptions {
    max_width : Int
    max_height : Int
    padding : Int
    border_padding : Int
    allow_rotation : Bool
    auto_grow_pot : Bool
    heuristic : MaxRectsHeuristic
    } derive(Eq,
    Debug
    )

    Configuration options for AtlasPacker.

    AtlasOptions::default

    fn AtlasOptions::default() -> AtlasOptions

    AtlasPacker

    pub struct AtlasPacker {
    options : AtlasOptions
    } derive(
    Debug
    )

    High-level 2D texture atlas packer supporting padding, rotation, multi-bin overflow, and adaptive power-of-two growth.

    AtlasPacker::new

    fn AtlasPacker::new(options? : AtlasOptions) -> AtlasPacker

    AtlasPacker::pack

    fn AtlasPacker::pack(self : AtlasPacker, items : Array[Size]) -> AtlasResult

    Packs an array of item sizes into one or more bins according to configured options.

    AtlasPacker::pack_guillotine

    fn AtlasPacker::pack_guillotine(self : AtlasPacker, items : Array[Size], merge? : Bool, rect_choice? : GuillotineHeuristic, split_method? : SplitHeuristic) -> AtlasResult

    Packs an array of item sizes using the Guillotine algorithm with multi-bin overflow and POT growth.

    AtlasPacker::pack_shelf

    fn AtlasPacker::pack_shelf(self : AtlasPacker, items : Array[Size], heuristic? : ShelfHeuristic) -> AtlasResult

    Packs an array of item sizes using the Shelf algorithm with multi-bin overflow and POT growth.

    AtlasPacker::pack_skyline

    fn AtlasPacker::pack_skyline(self : AtlasPacker, items : Array[Size], heuristic? : SkylineHeuristic) -> AtlasResult

    Packs an array of item sizes using the Skyline algorithm with multi-bin overflow and POT growth.

    AtlasResult

    pub(all) struct AtlasResult {
    pages : Array[Bin]
    rects : Array[Rect]
    unpacked : Array[Size]
    stats : PackingStats
    } derive(Eq,
    Debug
    )

    Result of multi-bin atlas packing.

    BakedAtlas

    pub(all) struct BakedAtlas {
    page_index : Int
    image : PixelBuffer
    frames : Array[Rect]
    uv_frames : Array[UvRect]
    } derive(
    Debug
    )

    Result of baking multiple sub-images into an atlas page.

    Bin

    pub(all) struct Bin {
    width : Int
    height : Int
    } derive(Default, Eq,
    Debug
    )

    Represents a target bin with width and height boundaries.

    Bin::area

    fn Bin::area(self : Bin) -> Int

    Bin::new

    fn Bin::new(width : Int, height : Int) -> Bin

    GuillotineHeuristic

    pub(all) enum GuillotineHeuristic {
    BestAreaFit
    BestShortSideFit
    BestLongSideFit
    WorstAreaFit
    WorstShortSideFit
    WorstLongSideFit
    } derive(Eq,
    Debug
    )

    Free rectangle choice heuristics for Guillotine bin packing algorithm.

    GuillotinePacker

    pub struct GuillotinePacker {
    bin_width : Int
    bin_height : Int
    free_rectangles : Array[Rect]
    used_rectangles : Array[Rect]
    used_surface_area : Int
    } derive(
    Debug
    )

    2D bin packer based on the Guillotine cut algorithm.

    GuillotinePacker::bin_height

    fn GuillotinePacker::bin_height(self : GuillotinePacker) -> Int

    GuillotinePacker::bin_width

    fn GuillotinePacker::bin_width(self : GuillotinePacker) -> Int

    GuillotinePacker::insert

    fn GuillotinePacker::insert(self : GuillotinePacker, width : Int, height : Int, id? : Int, merge? : Bool, rect_choice? : GuillotineHeuristic, split_method? : SplitHeuristic) -> Rect?

    Attempts to insert a rectangle of dimensions (width, height).

    GuillotinePacker::insert_batch

    fn GuillotinePacker::insert_batch(self : GuillotinePacker, sizes : Array[Size], merge? : Bool, rect_choice? : GuillotineHeuristic, split_method? : SplitHeuristic) -> (Array[Rect], Array[Size])

    Packs an array of sizes into the bin with pre-sorting by area descending.

    GuillotinePacker::merge_free_rectangles

    fn GuillotinePacker::merge_free_rectangles(self : GuillotinePacker) -> Unit

    Merges adjacent free rectangles that form a single larger rectangle.

    GuillotinePacker::new

    fn GuillotinePacker::new(bin_width : Int, bin_height : Int) -> GuillotinePacker

    GuillotinePacker::occupancy

    fn GuillotinePacker::occupancy(self : GuillotinePacker) -> Double

    GuillotinePacker::used_rectangles

    fn GuillotinePacker::used_rectangles(self : GuillotinePacker) -> Array[Rect]

    ImageItem

    pub(all) struct ImageItem {
    id : Int
    image : PixelBuffer
    } derive(
    Debug
    )

    Sub-image item to be packed and baked into a master atlas.

    ImageItem::new

    fn ImageItem::new(id : Int, image : PixelBuffer) -> ImageItem

    MaxRectsHeuristic

    pub(all) enum MaxRectsHeuristic {
    BestShortSideFit
    BestLongSideFit
    BestAreaFit
    BottomLeftRule
    ContactPointRule
    } derive(Eq,
    Debug
    )

    Heuristic options for MaxRects bin packing algorithm.

    MaxRectsPacker

    pub struct MaxRectsPacker {
    bin_width : Int
    bin_height : Int
    free_rectangles : Array[Rect]
    used_rectangles : Array[Rect]
    } derive(
    Debug
    )

    2D bin packer based on the Maximal Rectangles (MaxRects) algorithm.

    MaxRectsPacker::bin_height

    fn MaxRectsPacker::bin_height(self : MaxRectsPacker) -> Int

    MaxRectsPacker::bin_width

    fn MaxRectsPacker::bin_width(self : MaxRectsPacker) -> Int

    MaxRectsPacker::insert

    fn MaxRectsPacker::insert(self : MaxRectsPacker, width : Int, height : Int, id? : Int, heuristic? : MaxRectsHeuristic, allow_rotation? : Bool) -> Rect?

    Inserts a rectangle with dimensions (width, height) using the specified heuristic.

    MaxRectsPacker::insert_batch

    fn MaxRectsPacker::insert_batch(self : MaxRectsPacker, sizes : Array[Size], heuristic? : MaxRectsHeuristic, allow_rotation? : Bool) -> (Array[Rect], Array[Size])

    Packs an array of sizes into the bin with pre-sorting by area descending.

    MaxRectsPacker::new

    fn MaxRectsPacker::new(bin_width : Int, bin_height : Int) -> MaxRectsPacker

    MaxRectsPacker::occupancy

    fn MaxRectsPacker::occupancy(self : MaxRectsPacker) -> Double

    MaxRectsPacker::used_rectangles

    fn MaxRectsPacker::used_rectangles(self : MaxRectsPacker) -> Array[Rect]

    PackingStats

    pub(all) struct PackingStats {
    total_items : Int
    packed_items : Int
    unpacked_items : Int
    total_bin_area : Int
    used_area : Int
    occupancy : Double
    bins_used : Int
    } derive(Default, Eq,
    Debug
    )

    Packing performance and occupancy statistics.

    PackingStats::compute

    fn PackingStats::compute(total_items : Int, packed : Array[Rect], unpacked : Array[Size], bins_used : Int, bin_area : Int) -> PackingStats

    PixelBuffer

    pub struct PixelBuffer {
    width : Int
    height : Int
    pixels : FixedArray[Byte]
    } derive(
    Debug
    )

    A 2D RGBA bitmap image buffer stored in row-major order.

    PixelBuffer::blend_pixel

    fn PixelBuffer::blend_pixel(self : PixelBuffer, x : Int, y : Int, color : Rgba) -> Unit

    Performs standard alpha blending (Porter-Duff Over) of color onto pixel at (x, y).

    PixelBuffer::blit

    fn PixelBuffer::blit(self : PixelBuffer, src : PixelBuffer, dst_x : Int, dst_y : Int, rotated? : Bool) -> Unit

    Copies (blits) pixels from src into self at destination position (dst_x, dst_y). Supports optional 90-degree clockwise rotation.

    PixelBuffer::clear

    fn PixelBuffer::clear(self : PixelBuffer, color? : Rgba) -> Unit

    Resets the pixel buffer by filling all pixels with color.

    PixelBuffer::draw_rect

    fn PixelBuffer::draw_rect(self : PixelBuffer, x : Int, y : Int, width : Int, height : Int, color : Rgba, fill? : Bool) -> Unit

    Draws a rectangle outline or filled rectangle on the canvas.

    PixelBuffer::extrude_rect

    fn PixelBuffer::extrude_rect(self : PixelBuffer, rect : Rect, margin? : Int) -> Unit

    Eliminates GPU bilinear texture sampling bleeding by duplicating the outer 1-pixel border of rect into the surrounding padding margin.

    PixelBuffer::fill

    fn PixelBuffer::fill(self : PixelBuffer, color : Rgba) -> Unit

    PixelBuffer::get_pixel

    fn PixelBuffer::get_pixel(self : PixelBuffer, x : Int, y : Int) -> Rgba

    PixelBuffer::height

    fn PixelBuffer::height(self : PixelBuffer) -> Int

    PixelBuffer::new

    fn PixelBuffer::new(width : Int, height : Int, color? : Rgba) -> PixelBuffer

    PixelBuffer::set_pixel

    fn PixelBuffer::set_pixel(self : PixelBuffer, x : Int, y : Int, color : Rgba) -> Unit

    PixelBuffer::to_bmp

    fn PixelBuffer::to_bmp(self : PixelBuffer) -> FixedArray[Byte]

    Encodes the pixel buffer into a standard uncompressed 32-bit BMP (Windows Bitmap) byte array. Uses a top-down row orientation (-height) so pixel rows can be written directly without vertical flipping.

    PixelBuffer::to_bmp_bytes

    fn PixelBuffer::to_bmp_bytes(self : PixelBuffer) -> Bytes

    Converts the pixel buffer into an immutable Bytes object containing standard BMP file data.

    PixelBuffer::to_tga

    fn PixelBuffer::to_tga(self : PixelBuffer) -> FixedArray[Byte]

    Encodes the pixel buffer into a standard uncompressed 32-bit Truevision TGA file byte array. Widely used in 2D/3D game engines (OpenGL, Vulkan, DirectX) as an uncompressed raw asset format.

    PixelBuffer::to_tga_bytes

    fn PixelBuffer::to_tga_bytes(self : PixelBuffer) -> Bytes

    Converts the pixel buffer into an immutable Bytes object containing standard TGA file data.

    PixelBuffer::width

    fn PixelBuffer::width(self : PixelBuffer) -> Int

    Rect

    pub(all) struct Rect {
    x : Int
    y : Int
    width : Int
    height : Int
    id : Int
    rotated : Bool
    bin_index : Int
    } derive(Default, Eq,
    Debug
    )

    Represents a 2D rectangle with position (x, y), dimensions (width, height), an optional identifier, rotation flag, and bin page index.

    Rect::area

    fn Rect::area(self : Rect) -> Int

    Rect::bottom

    fn Rect::bottom(self : Rect) -> Int

    Rect::contains

    fn Rect::contains(self : Rect, other : Rect) -> Bool

    Returns true if this rectangle completely contains other.

    Rect::intersection

    fn Rect::intersection(self : Rect, other : Rect) -> Rect?

    Computes the intersection rectangle between self and other, if one exists.

    Rect::intersects

    fn Rect::intersects(self : Rect, other : Rect) -> Bool

    Returns true if this rectangle intersects with other (strict overlap with non-zero area).

    Rect::new

    fn Rect::new(x : Int, y : Int, width : Int, height : Int, id? : Int, rotated? : Bool, bin_index? : Int) -> Rect

    Rect::perimeter

    fn Rect::perimeter(self : Rect) -> Int

    Rect::right

    fn Rect::right(self : Rect) -> Int

    Rect::to_uv

    fn Rect::to_uv(self : Rect, atlas_width : Int, atlas_height : Int, half_texel_inset? : Bool) -> UvRect

    Computes normalized UV texture coordinates for self within an atlas of (atlas_width, atlas_height). Applies half-texel offset compensation by default.

    Rect::to_uv_ex

    fn Rect::to_uv_ex(self : Rect, atlas_width : Int, atlas_height : Int, half_texel_inset? : Bool, flip_v? : Bool) -> UvRect

    Computes normalized UV texture coordinates for self within an atlas of (atlas_width, atlas_height). When half_texel_inset = true, applies half-texel offset compensation: u_min = (x + 0.5) / atlas_width, u_max = (x + width - 0.5) / atlas_width to strictly prevent bilinear interpolation sampling bleeding across sprite boundaries. When flip_v = true, inverts the vertical axis for bottom-left origin coordinate systems (e.g. WebGL/OpenGL).

    Rgba

    pub(all) struct Rgba {
    r : Byte
    g : Byte
    b : Byte
    a : Byte
    } derive(Default, Eq,
    Debug
    )

    A 32-bit RGBA color representation (8 bits per channel).

    Rgba::from_hex

    fn Rgba::from_hex(hex : Int) -> Rgba

    Rgba::new

    fn Rgba::new(r : Byte, g : Byte, b : Byte, a? : Byte) -> Rgba

    Rgba::transparent

    fn Rgba::transparent() -> Rgba

    Shelf

    A horizontal shelf in the bin.

    ShelfHeuristic

    pub(all) enum ShelfHeuristic {
    NextFit
    FirstFit
    BestAreaFit
    BestHeightFit
    WorstAreaFit
    } derive(Eq,
    Debug
    )

    Heuristics for Shelf bin packing algorithm.

    ShelfPacker

    pub struct ShelfPacker {
    bin_width : Int
    bin_height : Int
    shelves : Array[Shelf]
    used_rectangles : Array[Rect]
    current_y : Int
    used_surface_area : Int
    } derive(
    Debug
    )

    2D bin packer based on horizontal shelves.

    ShelfPacker::bin_height

    fn ShelfPacker::bin_height(self : ShelfPacker) -> Int

    ShelfPacker::bin_width

    fn ShelfPacker::bin_width(self : ShelfPacker) -> Int

    ShelfPacker::insert

    fn ShelfPacker::insert(self : ShelfPacker, width : Int, height : Int, id? : Int, heuristic? : ShelfHeuristic) -> Rect?

    Attempts to insert a rectangle of dimensions (width, height) using the specified heuristic.

    ShelfPacker::insert_batch

    fn ShelfPacker::insert_batch(self : ShelfPacker, sizes : Array[Size], heuristic? : ShelfHeuristic) -> (Array[Rect], Array[Size])

    Packs an array of sizes into the bin using the Shelf algorithm with pre-sorting by height descending.

    ShelfPacker::new

    fn ShelfPacker::new(bin_width : Int, bin_height : Int) -> ShelfPacker

    ShelfPacker::occupancy

    fn ShelfPacker::occupancy(self : ShelfPacker) -> Double

    ShelfPacker::used_rectangles

    fn ShelfPacker::used_rectangles(self : ShelfPacker) -> Array[Rect]

    Size

    pub(all) struct Size {
    width : Int
    height : Int
    } derive(Default, Eq,
    Debug
    )

    Represents a 2D size with integer width and height.

    Size::area

    fn Size::area(self : Size) -> Int

    Size::new

    fn Size::new(width : Int, height : Int) -> Size

    SkylineHeuristic

    pub(all) enum SkylineHeuristic {
    BottomLeft
    MinWasteFit
    } derive(Eq,
    Debug
    )

    Heuristics for Skyline bin packing algorithm.

    SkylineNode

    pub(all) struct SkylineNode {
    x : Int
    y : Int
    width : Int
    } derive(Default, Eq,
    Debug
    )

    A horizontal segment in the Skyline bin packing algorithm.

    SkylineNode::new

    fn SkylineNode::new(x : Int, y : Int, width : Int) -> SkylineNode

    SkylinePacker

    pub struct SkylinePacker {
    bin_width : Int
    bin_height : Int
    skyline : Array[SkylineNode]
    used_rectangles : Array[Rect]
    used_surface_area : Int
    } derive(
    Debug
    )

    2D bin packer based on the Skyline algorithm with Min-Waste and Bottom-Left heuristics.

    SkylinePacker::bin_height

    fn SkylinePacker::bin_height(self : SkylinePacker) -> Int

    SkylinePacker::bin_width

    fn SkylinePacker::bin_width(self : SkylinePacker) -> Int

    SkylinePacker::insert

    fn SkylinePacker::insert(self : SkylinePacker, width : Int, height : Int, id? : Int, heuristic? : SkylineHeuristic, allow_rotation? : Bool) -> Rect?

    Inserts a rectangle of dimensions (width, height) using the Skyline algorithm.

    SkylinePacker::insert_batch

    fn SkylinePacker::insert_batch(self : SkylinePacker, sizes : Array[Size], heuristic? : SkylineHeuristic, allow_rotation? : Bool) -> (Array[Rect], Array[Size])

    Packs an array of sizes into the bin with pre-sorting by area descending using Skyline.

    SkylinePacker::new

    fn SkylinePacker::new(bin_width : Int, bin_height : Int) -> SkylinePacker

    SkylinePacker::occupancy

    fn SkylinePacker::occupancy(self : SkylinePacker) -> Double

    SkylinePacker::used_rectangles

    fn SkylinePacker::used_rectangles(self : SkylinePacker) -> Array[Rect]

    SplitHeuristic

    pub(all) enum SplitHeuristic {
    ShorterLeftoverAxis
    LongerLeftoverAxis
    MinimizeArea
    MaximizeArea
    ShorterAxis
    LongerAxis
    } derive(Eq,
    Debug
    )

    Split heuristics for Guillotine cut algorithm.

    UvRect

    pub(all) struct UvRect {
    u_min : Double
    v_min : Double
    u_max : Double
    v_max : Double
    } derive(Default, Eq,
    Debug
    )

    Normalized UV texture coordinates in [0.0, 1.0] for GPU rendering pipelines.

    UvRect::new

    fn UvRect::new(u_min : Double, v_min : Double, u_max : Double, v_max : Double) -> UvRect

    best_pack

    fn best_pack(items : Array[Size], max_width? : Int, max_height? : Int, padding? : Int, border_padding? : Int, allow_rotation? : Bool, auto_grow_pot? : Bool) -> (AtlasResult, String)

    Multi-algorithm heuristic arena that tests 18 heuristics across 4 algorithm families (MaxRects, Skyline, Guillotine, Shelf) to find the most compact layout for a given set of items. Returns the winning AtlasResult and the name of the winning algorithm.

    font_glyph_cache

    fn font_glyph_cache() -> Array[Size]

    Realistic benchmark dataset simulating a vector font glyph cache: 80 characters representing Chinese ideographs (Hanzi), Japanese Kana, and extended Latin glyph bounding boxes with uniform or stepped heights:
    • 16px font size row (width varies 6px..16px)
    • 24px font size row (width varies 8px..24px)
    • 32px font size row (width varies 12px..32px)
    • 48px headline glyphs (width varies 16px..48px)

    game_ui_sprites

    fn game_ui_sprites() -> Array[Size]

    Realistic benchmark dataset simulating a typical 2D RPG / action game UI sprite sheet: contains 64 items across diverse standard categories:
    • Small system icons (16x16, 24x24)
    • Inventory item tiles and action badges (32x32, 40x40, 48x48)
    • Skill icons and avatar portraits (64x64, 80x80)
    • Horizontal progress bars and status gauges (128x24, 160x32, 200x40)
    • Vertical equipment cards and weapon slots (48x128, 64x160)
    • Character portrait cards and banner cards (120x80, 160x100)

    large_scale_sprites

    fn large_scale_sprites() -> Array[Size]

    Large-scale dataset of 160 mixed items to stress-test multi-bin automatic overflow, memory stability, and execution speed across multiple atlas pages.

    pathological_aspect_ratios

    fn pathological_aspect_ratios() -> Array[Size]

    Pathological / stress test dataset containing 32 extreme aspect ratio items: needle-like horizontal and vertical strips (aspect ratios up to 128:1), prime dimension shapes, and large awkward blocks.

    to_css

    fn to_css(rects : Array[Rect], class_prefix? : String, image_url? : String) -> String

    Exports packed rectangles into standard CSS Sprite rules. Generates a common base class and individual utility classes with width, height, and negative background-position offsets.

    to_csv

    fn to_csv(rects : Array[Rect]) -> String

    Exports packed rectangles into standard CSV format for CAD, CNC, and spreadsheet analysis.

    to_html

    fn to_html(bin_width : Int, bin_height : Int, rects : Array[Rect], title? : String) -> String

    Exports packed rectangles into a standalone, interactive HTML visual inspection report.

    to_json

    fn to_json(bin_width : Int, bin_height : Int, rects : Array[Rect]) -> String

    Exports packed rectangles into a standard JSON texture atlas manifest format.

    to_pixi_json

    fn to_pixi_json(bin_width : Int, bin_height : Int, rects : Array[Rect], image_name? : String) -> String

    Exports packed rectangles into TexturePacker / Pixi.js / Phaser JSON Hash format.

    to_svg

    fn to_svg(bin_width : Int, bin_height : Int, rects : Array[Rect], title? : String) -> String

    Exports a packed rectangle layout to a self-contained, responsive SVG image.