stb_rect_pack

Types

Heuristic {...}{.pure.} = enum
  hSkylineDefault,          ## Use whatever the default heuristic is.
  hSkylineBLSortHeight, hSkylineBFSortHeight
RectPackContext = object
  raw: ptr stbrp_context
  nodes: ptr stbrp_node
  node_len: cint
Rect = object
  id*: cint                  ## reserved for your use
  width*: cint
  height*: cint
  x*: cint
  y*: cint
  was_packed*: bool

Procs

proc newRectPackContext(width, height, nodes: int): ref RectPackContext {...}{.
    raises: [], tags: [].}
Initialize a rectangle packer to:
pack a rectangle that is 'width' by 'height' in dimensions using temporary storage provided by the array 'nodes', which is 'num_nodes' long

You must call this function every time you start packing into a new target.

There is no "shutdown" function. The 'nodes' memory must stay valid for the following stbrp_pack_rects() call (or calls), but can be freed after the call (or calls) finish.

Note: to guarantee best results: make sure 'num_nodes' >= 'width'

proc setHeuristic(context: ref RectPackContext; heuristic: Heuristic) {...}{.
    raises: [], tags: [].}
Optionally select which packing heuristic the library should use. Different heuristics will produce better/worse results for different data sets. If you call init again, this will be reset to the default.
proc packRects(context: ref RectPackContext; rects: openArray[Rect]): bool {...}{.
    raises: [], tags: [].}

Assign packed locations to rectangles. The rectangles are of type 'stbrp_rect' defined below, stored in the array 'rects', and there are 'num_rects' many of them.

Rectangles which are successfully packed have the 'was_packed' flag set to a non-zero value and 'x' and 'y' store the minimum location on each axis (i.e. bottom-left in cartesian coordinates, top-left if you imagine y increasing downwards). Rectangles which do not fit have the 'was_packed' flag set to 0.

You should not try to access the 'rects' array from another thread while this function is running, as the function temporarily reorders the array while it executes.

To pack into another rectangle, you need to call stbrp_init_target again. To continue packing into the same rectangle, you can call this function again. Calling this multiple times with multiple rect arrays will probably produce worse packing results than calling it a single time with the full rectangle array, but the option is available.

The function returns 1 if all of the rectangles were successfully packed and 0 otherwise.