From 948a93041dba3ec95dbb04bee523a9bbd4ca9918 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Wed, 20 Jan 2016 18:40:38 +0000 Subject: Sort float_children of containers by their bottom edge. --- render/layout.c | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) (limited to 'render') diff --git a/render/layout.c b/render/layout.c index 4381f296c..f1e321e7f 100644 --- a/render/layout.c +++ b/render/layout.c @@ -2092,11 +2092,36 @@ void find_sides(struct box *fl, int y0, int y1, * * \param cont block formatting context block, used to contain float * \param b box to add to float + * + * This sorts floats in order of descending bottom edges. */ static void add_float_to_container(struct box *cont, struct box *b) { - b->next_float = cont->float_children; - cont->float_children = b; + struct box *box = cont->float_children; + int b_bottom = b->y + b->height; + + assert(b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT); + + if (box == NULL) { + /* No other float children */ + b->next_float = NULL; + cont->float_children = b; + return; + } else if (b_bottom >= box->y + box->height) { + /* Goes at start of list */ + b->next_float = cont->float_children; + cont->float_children = b; + } else { + struct box *prev = NULL; + while (box != NULL && b_bottom < box->y + box->height) { + prev = box; + box = box->next_float; + } + if (prev != NULL) { + b->next_float = prev->next_float; + prev->next_float = b; + } + } } -- cgit v1.2.3