65 #ifndef STB_INCLUDE_STB_RECT_PACK_H
66 #define STB_INCLUDE_STB_RECT_PACK_H
68 #define STB_RECT_PACK_VERSION 1
71 #define STBRP_DEF static
73 #define STBRP_DEF extern
84 #ifdef STBRP_LARGE_RECTS
205 #ifdef STB_RECT_PACK_IMPLEMENTATION
208 #define STBRP_SORT qsort
213 #define STBRP_ASSERT assert
218 #define STBRP__NOTUSED(v) (void)(v)
219 #define STBRP__CDECL __cdecl
221 #define STBRP__NOTUSED(v) (void)sizeof(v)
227 STBRP__INIT_skyline = 1
233 case STBRP__INIT_skyline:
244 if (allow_out_of_mem)
265 #ifndef STBRP_LARGE_RECTS
269 for (
i=0;
i < num_nodes-1; ++
i)
270 nodes[
i].
next = &nodes[
i+1];
272 context->
init_mode = STBRP__INIT_skyline;
286 #ifdef STBRP_LARGE_RECTS
287 context->
extra[1].
y = (1<<30);
289 context->
extra[1].
y = 65535;
299 int min_y, visited_width, waste_area;
307 while (node->
next->
x <= x0)
318 while (node->
x < x1) {
319 if (node->
y > min_y) {
323 waste_area += visited_width * (node->
y - min_y);
327 visited_width += node->
next->
x - x0;
329 visited_width += node->
next->
x - node->
x;
332 int under_width = node->
next->
x - node->
x;
333 if (under_width + visited_width >
width)
334 under_width =
width - visited_width;
335 waste_area += under_width * (min_y - node->
y);
336 visited_width += under_width;
341 *pwaste = waste_area;
353 int best_waste = (1<<30), best_x, best_y = (1 << 30);
354 stbrp__findresult fr;
373 y = stbrp__skyline_find_min_y(c, node, node->
x,
width, &waste);
384 if (
y < best_y || (
y == best_y && waste < best_waste)) {
395 best_x = (best ==
NULL) ? 0 : (*best)->
x;
422 int xpos = tail->
x -
width;
426 while (node->
next->
x <= xpos) {
431 y = stbrp__skyline_find_min_y(c, node, xpos,
width, &waste);
434 if (
y < best_y || waste < best_waste || (waste==best_waste && xpos < best_x)) {
456 stbrp__findresult res = stbrp__skyline_find_best_pos(context,
width,
height);
464 res.prev_link =
NULL;
479 cur = *res.prev_link;
480 if (cur->
x < res.x) {
486 *res.prev_link = node;
502 if (cur->
x < res.x +
width)
507 while (cur->
x < context->
width) {
533 static int STBRP__CDECL rect_height_compare(
const void *
a,
const void *
b)
541 return (
p->w > q->
w) ? -1 : (
p->w < q->
w);
545 static int STBRP__CDECL rect_original_order(
const void *
a,
const void *
b)
552 #ifdef STBRP_LARGE_RECTS
553 #define STBRP__MAXVAL 0xffffffff
555 #define STBRP__MAXVAL 0xffff
560 int i, all_rects_packed = 1;
563 for (
i=0;
i < num_rects; ++
i) {
568 STBRP_SORT(rects, num_rects,
sizeof(rects[0]), rect_height_compare);
570 for (
i=0;
i < num_rects; ++
i) {
571 if (rects[
i].
w == 0 || rects[
i].
h == 0) {
572 rects[
i].
x = rects[
i].
y = 0;
574 stbrp__findresult fr = stbrp__skyline_pack_rectangle(context, rects[
i].
w, rects[
i].
h);
579 rects[
i].
x = rects[
i].
y = STBRP__MAXVAL;
585 STBRP_SORT(rects, num_rects,
sizeof(rects[0]), rect_original_order);
588 for (
i=0;
i < num_rects; ++
i) {
589 rects[
i].
was_packed = !(rects[
i].
x == STBRP__MAXVAL && rects[
i].
y == STBRP__MAXVAL);
590 if (!rects[
i].was_packed)
591 all_rects_packed = 0;
595 return all_rects_packed;