Blame src-nuklear/stb_rect_pack.h

b53a5c
// stb_rect_pack.h - v1.01 - public domain - rectangle packing
b53a5c
// Sean Barrett 2014
b53a5c
//
b53a5c
// Useful for e.g. packing rectangular textures into an atlas.
b53a5c
// Does not do rotation.
b53a5c
//
b53a5c
// Before #including,
b53a5c
//
b53a5c
//    #define STB_RECT_PACK_IMPLEMENTATION
b53a5c
//
b53a5c
// in the file that you want to have the implementation.
b53a5c
//
b53a5c
// Not necessarily the awesomest packing method, but better than
b53a5c
// the totally naive one in stb_truetype (which is primarily what
b53a5c
// this is meant to replace).
b53a5c
//
b53a5c
// Has only had a few tests run, may have issues.
b53a5c
//
b53a5c
// More docs to come.
b53a5c
//
b53a5c
// No memory allocations; uses qsort() and assert() from stdlib.
b53a5c
// Can override those by defining STBRP_SORT and STBRP_ASSERT.
b53a5c
//
b53a5c
// This library currently uses the Skyline Bottom-Left algorithm.
b53a5c
//
b53a5c
// Please note: better rectangle packers are welcome! Please
b53a5c
// implement them to the same API, but with a different init
b53a5c
// function.
b53a5c
//
b53a5c
// Credits
b53a5c
//
b53a5c
//  Library
b53a5c
//    Sean Barrett
b53a5c
//  Minor features
b53a5c
//    Martins Mozeiko
b53a5c
//    github:IntellectualKitty
b53a5c
//
b53a5c
//  Bugfixes / warning fixes
b53a5c
//    Jeremy Jaussaud
b53a5c
//    Fabian Giesen
b53a5c
//
b53a5c
// Version history:
b53a5c
//
b53a5c
//     1.01  (2021-07-11)  always use large rect mode, expose STBRP__MAXVAL in public section
b53a5c
//     1.00  (2019-02-25)  avoid small space waste; gracefully fail too-wide rectangles
b53a5c
//     0.99  (2019-02-07)  warning fixes
b53a5c
//     0.11  (2017-03-03)  return packing success/fail result
b53a5c
//     0.10  (2016-10-25)  remove cast-away-const to avoid warnings
b53a5c
//     0.09  (2016-08-27)  fix compiler warnings
b53a5c
//     0.08  (2015-09-13)  really fix bug with empty rects (w=0 or h=0)
b53a5c
//     0.07  (2015-09-13)  fix bug with empty rects (w=0 or h=0)
b53a5c
//     0.06  (2015-04-15)  added STBRP_SORT to allow replacing qsort
b53a5c
//     0.05:  added STBRP_ASSERT to allow replacing assert
b53a5c
//     0.04:  fixed minor bug in STBRP_LARGE_RECTS support
b53a5c
//     0.01:  initial release
b53a5c
//
b53a5c
// LICENSE
b53a5c
//
b53a5c
//   See end of file for license information.
b53a5c
b53a5c
//////////////////////////////////////////////////////////////////////////////
b53a5c
//
b53a5c
//       INCLUDE SECTION
b53a5c
//
b53a5c
b53a5c
#ifndef STB_INCLUDE_STB_RECT_PACK_H
b53a5c
#define STB_INCLUDE_STB_RECT_PACK_H
b53a5c
b53a5c
#define STB_RECT_PACK_VERSION  1
b53a5c
b53a5c
#ifdef STBRP_STATIC
b53a5c
#define STBRP_DEF static
b53a5c
#else
b53a5c
#define STBRP_DEF extern
b53a5c
#endif
b53a5c
b53a5c
#ifdef __cplusplus
b53a5c
extern "C" {
b53a5c
#endif
b53a5c
b53a5c
typedef struct stbrp_context stbrp_context;
b53a5c
typedef struct stbrp_node    stbrp_node;
b53a5c
typedef struct stbrp_rect    stbrp_rect;
b53a5c
b53a5c
typedef int            stbrp_coord;
b53a5c
b53a5c
#define STBRP__MAXVAL  0x7fffffff
b53a5c
// Mostly for internal use, but this is the maximum supported coordinate value.
b53a5c
b53a5c
STBRP_DEF int stbrp_pack_rects (stbrp_context *context, stbrp_rect *rects, int num_rects);
b53a5c
// Assign packed locations to rectangles. The rectangles are of type
b53a5c
// 'stbrp_rect' defined below, stored in the array 'rects', and there
b53a5c
// are 'num_rects' many of them.
b53a5c
//
b53a5c
// Rectangles which are successfully packed have the 'was_packed' flag
b53a5c
// set to a non-zero value and 'x' and 'y' store the minimum location
b53a5c
// on each axis (i.e. bottom-left in cartesian coordinates, top-left
b53a5c
// if you imagine y increasing downwards). Rectangles which do not fit
b53a5c
// have the 'was_packed' flag set to 0.
b53a5c
//
b53a5c
// You should not try to access the 'rects' array from another thread
b53a5c
// while this function is running, as the function temporarily reorders
b53a5c
// the array while it executes.
b53a5c
//
b53a5c
// To pack into another rectangle, you need to call stbrp_init_target
b53a5c
// again. To continue packing into the same rectangle, you can call
b53a5c
// this function again. Calling this multiple times with multiple rect
b53a5c
// arrays will probably produce worse packing results than calling it
b53a5c
// a single time with the full rectangle array, but the option is
b53a5c
// available.
b53a5c
//
b53a5c
// The function returns 1 if all of the rectangles were successfully
b53a5c
// packed and 0 otherwise.
b53a5c
b53a5c
struct stbrp_rect
b53a5c
{
b53a5c
   // reserved for your use:
b53a5c
   int            id;
b53a5c
b53a5c
   // input:
b53a5c
   stbrp_coord    w, h;
b53a5c
b53a5c
   // output:
b53a5c
   stbrp_coord    x, y;
b53a5c
   int            was_packed;  // non-zero if valid packing
b53a5c
b53a5c
}; // 16 bytes, nominally
b53a5c
b53a5c
b53a5c
STBRP_DEF void stbrp_init_target (stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes);
b53a5c
// Initialize a rectangle packer to:
b53a5c
//    pack a rectangle that is 'width' by 'height' in dimensions
b53a5c
//    using temporary storage provided by the array 'nodes', which is 'num_nodes' long
b53a5c
//
b53a5c
// You must call this function every time you start packing into a new target.
b53a5c
//
b53a5c
// There is no "shutdown" function. The 'nodes' memory must stay valid for
b53a5c
// the following stbrp_pack_rects() call (or calls), but can be freed after
b53a5c
// the call (or calls) finish.
b53a5c
//
b53a5c
// Note: to guarantee best results, either:
b53a5c
//       1. make sure 'num_nodes' >= 'width'
b53a5c
//   or  2. call stbrp_allow_out_of_mem() defined below with 'allow_out_of_mem = 1'
b53a5c
//
b53a5c
// If you don't do either of the above things, widths will be quantized to multiples
b53a5c
// of small integers to guarantee the algorithm doesn't run out of temporary storage.
b53a5c
//
b53a5c
// If you do #2, then the non-quantized algorithm will be used, but the algorithm
b53a5c
// may run out of temporary storage and be unable to pack some rectangles.
b53a5c
b53a5c
STBRP_DEF void stbrp_setup_allow_out_of_mem (stbrp_context *context, int allow_out_of_mem);
b53a5c
// Optionally call this function after init but before doing any packing to
b53a5c
// change the handling of the out-of-temp-memory scenario, described above.
b53a5c
// If you call init again, this will be reset to the default (false).
b53a5c
b53a5c
b53a5c
STBRP_DEF void stbrp_setup_heuristic (stbrp_context *context, int heuristic);
b53a5c
// Optionally select which packing heuristic the library should use. Different
b53a5c
// heuristics will produce better/worse results for different data sets.
b53a5c
// If you call init again, this will be reset to the default.
b53a5c
b53a5c
enum
b53a5c
{
b53a5c
   STBRP_HEURISTIC_Skyline_default=0,
b53a5c
   STBRP_HEURISTIC_Skyline_BL_sortHeight = STBRP_HEURISTIC_Skyline_default,
b53a5c
   STBRP_HEURISTIC_Skyline_BF_sortHeight
b53a5c
};
b53a5c
b53a5c
b53a5c
//////////////////////////////////////////////////////////////////////////////
b53a5c
//
b53a5c
// the details of the following structures don't matter to you, but they must
b53a5c
// be visible so you can handle the memory allocations for them
b53a5c
b53a5c
struct stbrp_node
b53a5c
{
b53a5c
   stbrp_coord  x,y;
b53a5c
   stbrp_node  *next;
b53a5c
};
b53a5c
b53a5c
struct stbrp_context
b53a5c
{
b53a5c
   int width;
b53a5c
   int height;
b53a5c
   int align;
b53a5c
   int init_mode;
b53a5c
   int heuristic;
b53a5c
   int num_nodes;
b53a5c
   stbrp_node *active_head;
b53a5c
   stbrp_node *free_head;
b53a5c
   stbrp_node extra[2]; // we allocate two extra nodes so optimal user-node-count is 'width' not 'width+2'
b53a5c
};
b53a5c
b53a5c
#ifdef __cplusplus
b53a5c
}
b53a5c
#endif
b53a5c
b53a5c
#endif
b53a5c
b53a5c
//////////////////////////////////////////////////////////////////////////////
b53a5c
//
b53a5c
//     IMPLEMENTATION SECTION
b53a5c
//
b53a5c
b53a5c
#ifdef STB_RECT_PACK_IMPLEMENTATION
b53a5c
#ifndef STBRP_SORT
b53a5c
#include <stdlib.h></stdlib.h>
b53a5c
#define STBRP_SORT qsort
b53a5c
#endif
b53a5c
b53a5c
#ifndef STBRP_ASSERT
b53a5c
#include <assert.h></assert.h>
b53a5c
#define STBRP_ASSERT assert
b53a5c
#endif
b53a5c
b53a5c
#ifdef _MSC_VER
b53a5c
#define STBRP__NOTUSED(v)  (void)(v)
b53a5c
#define STBRP__CDECL       __cdecl
b53a5c
#else
b53a5c
#define STBRP__NOTUSED(v)  (void)sizeof(v)
b53a5c
#define STBRP__CDECL
b53a5c
#endif
b53a5c
b53a5c
enum
b53a5c
{
b53a5c
   STBRP__INIT_skyline = 1
b53a5c
};
b53a5c
b53a5c
STBRP_DEF void stbrp_setup_heuristic(stbrp_context *context, int heuristic)
b53a5c
{
b53a5c
   switch (context->init_mode) {
b53a5c
      case STBRP__INIT_skyline:
b53a5c
         STBRP_ASSERT(heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight || heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight);
b53a5c
         context->heuristic = heuristic;
b53a5c
         break;
b53a5c
      default:
b53a5c
         STBRP_ASSERT(0);
b53a5c
   }
b53a5c
}
b53a5c
b53a5c
STBRP_DEF void stbrp_setup_allow_out_of_mem(stbrp_context *context, int allow_out_of_mem)
b53a5c
{
b53a5c
   if (allow_out_of_mem)
b53a5c
      // if it's ok to run out of memory, then don't bother aligning them;
b53a5c
      // this gives better packing, but may fail due to OOM (even though
b53a5c
      // the rectangles easily fit). @TODO a smarter approach would be to only
b53a5c
      // quantize once we've hit OOM, then we could get rid of this parameter.
b53a5c
      context->align = 1;
b53a5c
   else {
b53a5c
      // if it's not ok to run out of memory, then quantize the widths
b53a5c
      // so that num_nodes is always enough nodes.
b53a5c
      //
b53a5c
      // I.e. num_nodes * align >= width
b53a5c
      //                  align >= width / num_nodes
b53a5c
      //                  align = ceil(width/num_nodes)
b53a5c
b53a5c
      context->align = (context->width + context->num_nodes-1) / context->num_nodes;
b53a5c
   }
b53a5c
}
b53a5c
b53a5c
STBRP_DEF void stbrp_init_target(stbrp_context *context, int width, int height, stbrp_node *nodes, int num_nodes)
b53a5c
{
b53a5c
   int i;
b53a5c
b53a5c
   for (i=0; i < num_nodes-1; ++i)
b53a5c
      nodes[i].next = &nodes[i+1];
b53a5c
   nodes[i].next = NULL;
b53a5c
   context->init_mode = STBRP__INIT_skyline;
b53a5c
   context->heuristic = STBRP_HEURISTIC_Skyline_default;
b53a5c
   context->free_head = &nodes[0];
b53a5c
   context->active_head = &context->extra[0];
b53a5c
   context->width = width;
b53a5c
   context->height = height;
b53a5c
   context->num_nodes = num_nodes;
b53a5c
   stbrp_setup_allow_out_of_mem(context, 0);
b53a5c
b53a5c
   // node 0 is the full width, node 1 is the sentinel (lets us not store width explicitly)
b53a5c
   context->extra[0].x = 0;
b53a5c
   context->extra[0].y = 0;
b53a5c
   context->extra[0].next = &context->extra[1];
b53a5c
   context->extra[1].x = (stbrp_coord) width;
b53a5c
   context->extra[1].y = (1<<30);
b53a5c
   context->extra[1].next = NULL;
b53a5c
}
b53a5c
b53a5c
// find minimum y position if it starts at x1
b53a5c
static int stbrp__skyline_find_min_y(stbrp_context *c, stbrp_node *first, int x0, int width, int *pwaste)
b53a5c
{
b53a5c
   stbrp_node *node = first;
b53a5c
   int x1 = x0 + width;
b53a5c
   int min_y, visited_width, waste_area;
b53a5c
b53a5c
   STBRP__NOTUSED(c);
b53a5c
b53a5c
   STBRP_ASSERT(first->x <= x0);
b53a5c
b53a5c
   #if 0
b53a5c
   // skip in case we're past the node
b53a5c
   while (node->next->x <= x0)
b53a5c
      ++node;
b53a5c
   #else
b53a5c
   STBRP_ASSERT(node->next->x > x0); // we ended up handling this in the caller for efficiency
b53a5c
   #endif
b53a5c
b53a5c
   STBRP_ASSERT(node->x <= x0);
b53a5c
b53a5c
   min_y = 0;
b53a5c
   waste_area = 0;
b53a5c
   visited_width = 0;
b53a5c
   while (node->x < x1) {
b53a5c
      if (node->y > min_y) {
b53a5c
         // raise min_y higher.
b53a5c
         // we've accounted for all waste up to min_y,
b53a5c
         // but we'll now add more waste for everything we've visted
b53a5c
         waste_area += visited_width * (node->y - min_y);
b53a5c
         min_y = node->y;
b53a5c
         // the first time through, visited_width might be reduced
b53a5c
         if (node->x < x0)
b53a5c
            visited_width += node->next->x - x0;
b53a5c
         else
b53a5c
            visited_width += node->next->x - node->x;
b53a5c
      } else {
b53a5c
         // add waste area
b53a5c
         int under_width = node->next->x - node->x;
b53a5c
         if (under_width + visited_width > width)
b53a5c
            under_width = width - visited_width;
b53a5c
         waste_area += under_width * (min_y - node->y);
b53a5c
         visited_width += under_width;
b53a5c
      }
b53a5c
      node = node->next;
b53a5c
   }
b53a5c
b53a5c
   *pwaste = waste_area;
b53a5c
   return min_y;
b53a5c
}
b53a5c
b53a5c
typedef struct
b53a5c
{
b53a5c
   int x,y;
b53a5c
   stbrp_node **prev_link;
b53a5c
} stbrp__findresult;
b53a5c
b53a5c
static stbrp__findresult stbrp__skyline_find_best_pos(stbrp_context *c, int width, int height)
b53a5c
{
b53a5c
   int best_waste = (1<<30), best_x, best_y = (1 << 30);
b53a5c
   stbrp__findresult fr;
b53a5c
   stbrp_node **prev, *node, *tail, **best = NULL;
b53a5c
b53a5c
   // align to multiple of c->align
b53a5c
   width = (width + c->align - 1);
b53a5c
   width -= width % c->align;
b53a5c
   STBRP_ASSERT(width % c->align == 0);
b53a5c
b53a5c
   // if it can't possibly fit, bail immediately
b53a5c
   if (width > c->width || height > c->height) {
b53a5c
      fr.prev_link = NULL;
b53a5c
      fr.x = fr.y = 0;
b53a5c
      return fr;
b53a5c
   }
b53a5c
b53a5c
   node = c->active_head;
b53a5c
   prev = &c->active_head;
b53a5c
   while (node->x + width <= c->width) {
b53a5c
      int y,waste;
b53a5c
      y = stbrp__skyline_find_min_y(c, node, node->x, width, &waste);
b53a5c
      if (c->heuristic == STBRP_HEURISTIC_Skyline_BL_sortHeight) { // actually just want to test BL
b53a5c
         // bottom left
b53a5c
         if (y < best_y) {
b53a5c
            best_y = y;
b53a5c
            best = prev;
b53a5c
         }
b53a5c
      } else {
b53a5c
         // best-fit
b53a5c
         if (y + height <= c->height) {
b53a5c
            // can only use it if it first vertically
b53a5c
            if (y < best_y || (y == best_y && waste < best_waste)) {
b53a5c
               best_y = y;
b53a5c
               best_waste = waste;
b53a5c
               best = prev;
b53a5c
            }
b53a5c
         }
b53a5c
      }
b53a5c
      prev = &node->next;
b53a5c
      node = node->next;
b53a5c
   }
b53a5c
b53a5c
   best_x = (best == NULL) ? 0 : (*best)->x;
b53a5c
b53a5c
   // if doing best-fit (BF), we also have to try aligning right edge to each node position
b53a5c
   //
b53a5c
   // e.g, if fitting
b53a5c
   //
b53a5c
   //     ____________________
b53a5c
   //    |____________________|
b53a5c
   //
b53a5c
   //            into
b53a5c
   //
b53a5c
   //   |                         |
b53a5c
   //   |             ____________|
b53a5c
   //   |____________|
b53a5c
   //
b53a5c
   // then right-aligned reduces waste, but bottom-left BL is always chooses left-aligned
b53a5c
   //
b53a5c
   // This makes BF take about 2x the time
b53a5c
b53a5c
   if (c->heuristic == STBRP_HEURISTIC_Skyline_BF_sortHeight) {
b53a5c
      tail = c->active_head;
b53a5c
      node = c->active_head;
b53a5c
      prev = &c->active_head;
b53a5c
      // find first node that's admissible
b53a5c
      while (tail->x < width)
b53a5c
         tail = tail->next;
b53a5c
      while (tail) {
b53a5c
         int xpos = tail->x - width;
b53a5c
         int y,waste;
b53a5c
         STBRP_ASSERT(xpos >= 0);
b53a5c
         // find the left position that matches this
b53a5c
         while (node->next->x <= xpos) {
b53a5c
            prev = &node->next;
b53a5c
            node = node->next;
b53a5c
         }
b53a5c
         STBRP_ASSERT(node->next->x > xpos && node->x <= xpos);
b53a5c
         y = stbrp__skyline_find_min_y(c, node, xpos, width, &waste);
b53a5c
         if (y + height <= c->height) {
b53a5c
            if (y <= best_y) {
b53a5c
               if (y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
b53a5c
                  best_x = xpos;
b53a5c
                  STBRP_ASSERT(y <= best_y);
b53a5c
                  best_y = y;
b53a5c
                  best_waste = waste;
b53a5c
                  best = prev;
b53a5c
               }
b53a5c
            }
b53a5c
         }
b53a5c
         tail = tail->next;
b53a5c
      }
b53a5c
   }
b53a5c
b53a5c
   fr.prev_link = best;
b53a5c
   fr.x = best_x;
b53a5c
   fr.y = best_y;
b53a5c
   return fr;
b53a5c
}
b53a5c
b53a5c
static stbrp__findresult stbrp__skyline_pack_rectangle(stbrp_context *context, int width, int height)
b53a5c
{
b53a5c
   // find best position according to heuristic
b53a5c
   stbrp__findresult res = stbrp__skyline_find_best_pos(context, width, height);
b53a5c
   stbrp_node *node, *cur;
b53a5c
b53a5c
   // bail if:
b53a5c
   //    1. it failed
b53a5c
   //    2. the best node doesn't fit (we don't always check this)
b53a5c
   //    3. we're out of memory
b53a5c
   if (res.prev_link == NULL || res.y + height > context->height || context->free_head == NULL) {
b53a5c
      res.prev_link = NULL;
b53a5c
      return res;
b53a5c
   }
b53a5c
b53a5c
   // on success, create new node
b53a5c
   node = context->free_head;
b53a5c
   node->x = (stbrp_coord) res.x;
b53a5c
   node->y = (stbrp_coord) (res.y + height);
b53a5c
b53a5c
   context->free_head = node->next;
b53a5c
b53a5c
   // insert the new node into the right starting point, and
b53a5c
   // let 'cur' point to the remaining nodes needing to be
b53a5c
   // stiched back in
b53a5c
b53a5c
   cur = *res.prev_link;
b53a5c
   if (cur->x < res.x) {
b53a5c
      // preserve the existing one, so start testing with the next one
b53a5c
      stbrp_node *next = cur->next;
b53a5c
      cur->next = node;
b53a5c
      cur = next;
b53a5c
   } else {
b53a5c
      *res.prev_link = node;
b53a5c
   }
b53a5c
b53a5c
   // from here, traverse cur and free the nodes, until we get to one
b53a5c
   // that shouldn't be freed
b53a5c
   while (cur->next && cur->next->x <= res.x + width) {
b53a5c
      stbrp_node *next = cur->next;
b53a5c
      // move the current node to the free list
b53a5c
      cur->next = context->free_head;
b53a5c
      context->free_head = cur;
b53a5c
      cur = next;
b53a5c
   }
b53a5c
b53a5c
   // stitch the list back in
b53a5c
   node->next = cur;
b53a5c
b53a5c
   if (cur->x < res.x + width)
b53a5c
      cur->x = (stbrp_coord) (res.x + width);
b53a5c
b53a5c
#ifdef _DEBUG
b53a5c
   cur = context->active_head;
b53a5c
   while (cur->x < context->width) {
b53a5c
      STBRP_ASSERT(cur->x < cur->next->x);
b53a5c
      cur = cur->next;
b53a5c
   }
b53a5c
   STBRP_ASSERT(cur->next == NULL);
b53a5c
b53a5c
   {
b53a5c
      int count=0;
b53a5c
      cur = context->active_head;
b53a5c
      while (cur) {
b53a5c
         cur = cur->next;
b53a5c
         ++count;
b53a5c
      }
b53a5c
      cur = context->free_head;
b53a5c
      while (cur) {
b53a5c
         cur = cur->next;
b53a5c
         ++count;
b53a5c
      }
b53a5c
      STBRP_ASSERT(count == context->num_nodes+2);
b53a5c
   }
b53a5c
#endif
b53a5c
b53a5c
   return res;
b53a5c
}
b53a5c
b53a5c
static int STBRP__CDECL rect_height_compare(const void *a, const void *b)
b53a5c
{
b53a5c
   const stbrp_rect *p = (const stbrp_rect *) a;
b53a5c
   const stbrp_rect *q = (const stbrp_rect *) b;
b53a5c
   if (p->h > q->h)
b53a5c
      return -1;
b53a5c
   if (p->h < q->h)
b53a5c
      return  1;
b53a5c
   return (p->w > q->w) ? -1 : (p->w < q->w);
b53a5c
}
b53a5c
b53a5c
static int STBRP__CDECL rect_original_order(const void *a, const void *b)
b53a5c
{
b53a5c
   const stbrp_rect *p = (const stbrp_rect *) a;
b53a5c
   const stbrp_rect *q = (const stbrp_rect *) b;
b53a5c
   return (p->was_packed < q->was_packed) ? -1 : (p->was_packed > q->was_packed);
b53a5c
}
b53a5c
b53a5c
STBRP_DEF int stbrp_pack_rects(stbrp_context *context, stbrp_rect *rects, int num_rects)
b53a5c
{
b53a5c
   int i, all_rects_packed = 1;
b53a5c
b53a5c
   // we use the 'was_packed' field internally to allow sorting/unsorting
b53a5c
   for (i=0; i < num_rects; ++i) {
b53a5c
      rects[i].was_packed = i;
b53a5c
   }
b53a5c
b53a5c
   // sort according to heuristic
b53a5c
   STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_height_compare);
b53a5c
b53a5c
   for (i=0; i < num_rects; ++i) {
b53a5c
      if (rects[i].w == 0 || rects[i].h == 0) {
b53a5c
         rects[i].x = rects[i].y = 0;  // empty rect needs no space
b53a5c
      } else {
b53a5c
         stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[i].w, rects[i].h);
b53a5c
         if (fr.prev_link) {
b53a5c
            rects[i].x = (stbrp_coord) fr.x;
b53a5c
            rects[i].y = (stbrp_coord) fr.y;
b53a5c
         } else {
b53a5c
            rects[i].x = rects[i].y = STBRP__MAXVAL;
b53a5c
         }
b53a5c
      }
b53a5c
   }
b53a5c
b53a5c
   // unsort
b53a5c
   STBRP_SORT(rects, num_rects, sizeof(rects[0]), rect_original_order);
b53a5c
b53a5c
   // set was_packed flags and all_rects_packed status
b53a5c
   for (i=0; i < num_rects; ++i) {
b53a5c
      rects[i].was_packed = !(rects[i].x == STBRP__MAXVAL && rects[i].y == STBRP__MAXVAL);
b53a5c
      if (!rects[i].was_packed)
b53a5c
         all_rects_packed = 0;
b53a5c
   }
b53a5c
b53a5c
   // return the all_rects_packed status
b53a5c
   return all_rects_packed;
b53a5c
}
b53a5c
#endif
b53a5c
b53a5c
/*
b53a5c
------------------------------------------------------------------------------
b53a5c
This software is available under 2 licenses -- choose whichever you prefer.
b53a5c
------------------------------------------------------------------------------
b53a5c
ALTERNATIVE A - MIT License
b53a5c
Copyright (c) 2017 Sean Barrett
b53a5c
Permission is hereby granted, free of charge, to any person obtaining a copy of
b53a5c
this software and associated documentation files (the "Software"), to deal in
b53a5c
the Software without restriction, including without limitation the rights to
b53a5c
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
b53a5c
of the Software, and to permit persons to whom the Software is furnished to do
b53a5c
so, subject to the following conditions:
b53a5c
The above copyright notice and this permission notice shall be included in all
b53a5c
copies or substantial portions of the Software.
b53a5c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
b53a5c
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
b53a5c
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
b53a5c
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
b53a5c
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
b53a5c
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
b53a5c
SOFTWARE.
b53a5c
------------------------------------------------------------------------------
b53a5c
ALTERNATIVE B - Public Domain (www.unlicense.org)
b53a5c
This is free and unencumbered software released into the public domain.
b53a5c
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
b53a5c
software, either in source code form or as a compiled binary, for any purpose,
b53a5c
commercial or non-commercial, and by any means.
b53a5c
In jurisdictions that recognize copyright laws, the author or authors of this
b53a5c
software dedicate any and all copyright interest in the software to the public
b53a5c
domain. We make this dedication for the benefit of the public at large and to
b53a5c
the detriment of our heirs and successors. We intend this dedication to be an
b53a5c
overt act of relinquishment in perpetuity of all present and future rights to
b53a5c
this software under copyright law.
b53a5c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
b53a5c
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
b53a5c
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
b53a5c
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
b53a5c
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
b53a5c
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
b53a5c
------------------------------------------------------------------------------
b53a5c
*/