From ae286be5a9a5c1150dfa7627f2b30a73238b506c Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Wed, 3 Jan 2018 22:15:25 +0000 Subject: Layout: No need to expose layout_inline_container(). --- render/layout.c | 4258 ++++++++++++++++++++++++++++--------------------------- render/layout.h | 13 - 2 files changed, 2134 insertions(+), 2137 deletions(-) diff --git a/render/layout.c b/render/layout.c index fa49667ff..b19d93f44 100644 --- a/render/layout.c +++ b/render/layout.c @@ -2634,493 +2634,340 @@ static bool layout_block_object(struct box *block) /** - * Layout a block formatting context. + * Insert a float into a container. * - * \param block BLOCK, INLINE_BLOCK, or TABLE_CELL to layout - * \param viewport_height Height of viewport in pixels or -ve if unknown - * \param content Memory pool for any new boxes - * \return true on success, false on memory exhaustion + * \param cont block formatting context block, used to contain float + * \param b box to add to float * - * This function carries out layout of a block and its children, as described - * in CSS 2.1 9.4.1. + * This sorts floats in order of descending bottom edges. */ -static bool -layout_block_context(struct box *block, - int viewport_height, - html_content *content) +static void add_float_to_container(struct box *cont, struct box *b) { - struct box *box; - int cx, cy; /**< current coordinates */ - int max_pos_margin = 0; - int max_neg_margin = 0; - int y = 0; - int lm, rm; - struct box *margin_collapse = NULL; - bool in_margin = false; - css_fixed gadget_size; - css_unit gadget_unit; /* Checkbox / radio buttons */ - - assert(block->type == BOX_BLOCK || - block->type == BOX_INLINE_BLOCK || - block->type == BOX_TABLE_CELL); - assert(block->width != UNKNOWN_WIDTH); - assert(block->width != AUTO); + struct box *box = cont->float_children; + int b_bottom = b->y + b->height; - block->float_children = NULL; - block->cached_place_below_level = 0; - block->clear_level = 0; + assert(b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT); - /* special case if the block contains an object */ - if (block->object) { - int temp_width = block->width; - if (!layout_block_object(block)) - return false; - layout_get_object_dimensions(block, &temp_width, - &block->height, INT_MIN, INT_MAX, - INT_MIN, INT_MAX); - return true; - } else if (block->flags & REPLACE_DIM) { - return true; + 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; + } } +} - /* special case if the block contains an radio button or checkbox */ - if (block->gadget && (block->gadget->type == GADGET_RADIO || - block->gadget->type == GADGET_CHECKBOX)) { - /* form checkbox or radio button - * if width or height is AUTO, set it to 1em */ - gadget_unit = CSS_UNIT_EM; - gadget_size = INTTOFIX(1); - if (block->height == AUTO) - block->height = FIXTOINT(nscss_len2px(gadget_size, - gadget_unit, block->style)); + +/** + * Split a text box. + * + * \param content memory pool for any new boxes + * \param fstyle style for text in text box + * \param split_box box with text to split + * \param new_length new length for text in split_box, after splitting + * \param new_width new width for text in split_box, after splitting + * \return true on success, false on memory exhaustion + * + * A new box is created and inserted into the box tree after split_box, + * containing the text after new_length excluding the initial space character. + */ +static bool +layout_text_box_split(html_content *content, + plot_font_style_t *fstyle, + struct box *split_box, + size_t new_length, + int new_width) +{ + int space_width = split_box->space; + struct box *c2; + const struct gui_layout_table *font_func = content->font_func; + bool space = (split_box->text[new_length] == ' '); + int used_length = new_length + (space ? 1 : 0); + + if ((space && space_width == 0) || space_width == UNKNOWN_WIDTH) { + /* We're need to add a space, and we don't know how big + * it's to be, OR we have a space of unknown width anyway; + * Calculate space width */ + font_func->width(fstyle, " ", 1, &space_width); } - box = block->children; - /* set current coordinates to top-left of the block */ - cx = 0; - y = cy = block->padding[TOP]; - if (box) - box->y = block->padding[TOP]; + if (split_box->space == UNKNOWN_WIDTH) + split_box->space = space_width; + if (!space) + space_width = 0; - /* Step through the descendants of the block in depth-first order, but - * not into the children of boxes which aren't blocks. For example, if - * the tree passed to this function looks like this (box->type shown): - * - * block -> BOX_BLOCK - * BOX_BLOCK * (1) - * BOX_INLINE_CONTAINER * (2) - * BOX_INLINE - * BOX_TEXT - * ... - * BOX_BLOCK * (3) - * BOX_TABLE * (4) - * BOX_TABLE_ROW - * BOX_TABLE_CELL - * ... - * BOX_TABLE_CELL - * ... - * BOX_BLOCK * (5) - * BOX_INLINE_CONTAINER * (6) - * BOX_TEXT - * ... - * then the while loop will visit each box marked with *, setting box - * to each in the order shown. */ - while (box) { - enum css_overflow_e overflow_x = CSS_OVERFLOW_VISIBLE; - enum css_overflow_e overflow_y = CSS_OVERFLOW_VISIBLE; + /* Create clone of split_box, c2 */ + c2 = talloc_memdup(content->bctx, split_box, sizeof *c2); + if (!c2) + return false; + c2->flags |= CLONE; - assert(box->type == BOX_BLOCK || box->type == BOX_TABLE || - box->type == BOX_INLINE_CONTAINER); + /* Set remaining text in c2 */ + c2->text += used_length; - /* Tables are laid out before being positioned, because the - * position depends on the width which is calculated in - * table layout. Blocks and inline containers are positioned - * before being laid out, because width is not dependent on - * content, and the position is required during layout for - * correct handling of floats. - */ + /* Set c2 according to the remaining text */ + c2->width -= new_width + space_width; + c2->flags &= ~MEASURED; /* width has been estimated */ + c2->length = split_box->length - used_length; - if (box->style && - (css_computed_position(box->style) == - CSS_POSITION_ABSOLUTE || - css_computed_position(box->style) == - CSS_POSITION_FIXED)) { - box->x = box->parent->padding[LEFT]; - /* absolute positioned; this element will establish - * its own block context when it gets laid out later, - * so no need to look at its children now. */ - goto advance_to_next_box; - } + /* Update split_box for its reduced text */ + split_box->width = new_width; + split_box->flags |= MEASURED; + split_box->length = new_length; + split_box->space = space_width; - /* If we don't know which box the current margin collapses - * through to, find out. Update the pos/neg margin values. */ - if (margin_collapse == NULL) { - margin_collapse = layout_next_margin_block(box, block, - viewport_height, - &max_pos_margin, &max_neg_margin); - /* We have a margin that has not yet been applied. */ - in_margin = true; - } + /* Insert c2 into box list */ + c2->next = split_box->next; + split_box->next = c2; + c2->prev = split_box; + if (c2->next) + c2->next->prev = c2; + else + c2->parent->last = c2; - /* Clearance. */ - y = 0; - if (box->style && css_computed_clear(box->style) != - CSS_CLEAR_NONE) - y = layout_clear(block->float_children, - css_computed_clear(box->style)); + NSLOG(layout, DEBUG, + "split_box %p len: %" PRIsizet " \"%.*s\"", + split_box, + split_box->length, + (int)split_box->length, + split_box->text); + NSLOG(layout, DEBUG, + " new_box %p len: %" PRIsizet " \"%.*s\"", + c2, + c2->length, + (int)c2->length, + c2->text); - /* Find box's overflow properties */ - if (box->style) { - overflow_x = css_computed_overflow_x(box->style); - overflow_y = css_computed_overflow_y(box->style); - } + return true; +} - /* Blocks establishing a block formatting context get minimum - * left and right margins to avoid any floats. */ - lm = rm = 0; - if (box->type == BOX_BLOCK || box->flags & IFRAME) { - if (!box->object && !(box->flags & IFRAME) && - !(box->flags & REPLACE_DIM) && - box->style && - (overflow_x != CSS_OVERFLOW_VISIBLE || - overflow_y != CSS_OVERFLOW_VISIBLE)) { - /* box establishes new block formatting context - * so available width may be diminished due to - * floats. */ - int x0, x1, top; - struct box *left, *right; - top = cy + max_pos_margin - max_neg_margin; - top = (top > y) ? top : y; - x0 = cx; - x1 = cx + box->parent->width - - box->parent->padding[LEFT] - - box->parent->padding[RIGHT]; - find_sides(block->float_children, top, top, - &x0, &x1, &left, &right); - /* calculate min required left & right margins - * needed to avoid floats */ - lm = x0 - cx; - rm = cx + box->parent->width - - box->parent->padding[LEFT] - - box->parent->padding[RIGHT] - - x1; - } - layout_block_find_dimensions(box->parent->width, - viewport_height, lm, rm, box); - if (box->type == BOX_BLOCK && !(box->flags & IFRAME)) { - layout_block_add_scrollbar(box, RIGHT); - layout_block_add_scrollbar(box, BOTTOM); - } - } else if (box->type == BOX_TABLE) { - if (box->style != NULL) { - enum css_width_e wtype; - css_fixed width = 0; - css_unit unit = CSS_UNIT_PX; +/** + * Compute dimensions of box, margins, paddings, and borders for a floating + * element using shrink-to-fit. Also used for inline-blocks. + * + * \param available_width Max width available in pixels + * \param style Box's style + * \param box Box for which to find dimensions + * Box margins, borders, paddings, width and + * height are updated. + */ +static void +layout_float_find_dimensions(int available_width, + const css_computed_style *style, + struct box *box) +{ + int width, height, max_width, min_width, max_height, min_height; + int *margin = box->margin; + int *padding = box->padding; + struct box_border *border = box->border; + enum css_overflow_e overflow_x = css_computed_overflow_x(style); + enum css_overflow_e overflow_y = css_computed_overflow_y(style); + int scrollbar_width_x = + (overflow_x == CSS_OVERFLOW_SCROLL || + overflow_x == CSS_OVERFLOW_AUTO) ? + SCROLLBAR_WIDTH : 0; + int scrollbar_width_y = + (overflow_y == CSS_OVERFLOW_SCROLL || + overflow_y == CSS_OVERFLOW_AUTO) ? + SCROLLBAR_WIDTH : 0; - wtype = css_computed_width(box->style, &width, - &unit); + layout_find_dimensions(available_width, -1, box, style, &width, &height, + &max_width, &min_width, &max_height, &min_height, + margin, padding, border); - if (wtype == CSS_WIDTH_AUTO) { - /* max available width may be - * diminished due to floats. */ - int x0, x1, top; - struct box *left, *right; - top = cy + max_pos_margin - - max_neg_margin; - top = (top > y) ? top : y; - x0 = cx; - x1 = cx + box->parent->width - - box->parent->padding[LEFT] - - box->parent->padding[RIGHT]; - find_sides(block->float_children, - top, top, &x0, &x1, - &left, &right); - /* calculate min required left & right - * margins needed to avoid floats */ - lm = x0 - cx; - rm = cx + box->parent->width - - box->parent->padding[LEFT] - - box->parent->padding[RIGHT] - - x1; - } - } - if (!layout_table(box, box->parent->width - lm - rm, - content)) - return false; - layout_solve_width(box, box->parent->width, box->width, - lm, rm, -1, -1); - } + if (margin[LEFT] == AUTO) + margin[LEFT] = 0; + if (margin[RIGHT] == AUTO) + margin[RIGHT] = 0; - /* Position box: horizontal. */ - box->x = box->parent->padding[LEFT] + box->margin[LEFT] + - box->border[LEFT].width; - cx += box->x; + if (box->gadget == NULL) { + padding[RIGHT] += scrollbar_width_y; + padding[BOTTOM] += scrollbar_width_x; + } - /* Position box: vertical. */ - if (box->border[TOP].width) { - box->y += box->border[TOP].width; - cy += box->border[TOP].width; - } + if (box->object && !(box->flags & REPLACE_DIM) && + content_get_type(box->object) != CONTENT_HTML) { + /* Floating replaced element, with intrinsic width or height. + * See 10.3.6 and 10.6.2 */ + layout_get_object_dimensions(box, &width, &height, + min_width, max_width, min_height, max_height); + } else if (box->gadget && (box->gadget->type == GADGET_TEXTBOX || + box->gadget->type == GADGET_PASSWORD || + box->gadget->type == GADGET_FILE || + box->gadget->type == GADGET_TEXTAREA)) { + css_fixed size = 0; + css_unit unit = CSS_UNIT_EM; - /* Vertical margin */ - if (((box->type == BOX_BLOCK && - (box->flags & HAS_HEIGHT)) || - box->type == BOX_TABLE || - (box->type == BOX_INLINE_CONTAINER && - box != box->parent->children) || - margin_collapse == box) && - in_margin == true) { - /* Margin goes above this box. */ - cy += max_pos_margin - max_neg_margin; - box->y += max_pos_margin - max_neg_margin; + /* Give sensible dimensions to gadgets, with auto width/height, + * that don't shrink to fit contained text. */ + assert(box->style); - /* Current margin has been applied. */ - in_margin = false; - max_pos_margin = max_neg_margin = 0; + if (box->gadget->type == GADGET_TEXTBOX || + box->gadget->type == GADGET_PASSWORD || + box->gadget->type == GADGET_FILE) { + if (width == AUTO) { + size = INTTOFIX(10); + width = FIXTOINT(nscss_len2px(size, unit, + box->style)); + } + if (box->gadget->type == GADGET_FILE && + height == AUTO) { + size = FLTTOFIX(1.5); + height = FIXTOINT(nscss_len2px(size, unit, + box->style)); + } } - - /* Handle clearance */ - if (box->type != BOX_INLINE_CONTAINER && - (y > 0) && (cy < y)) { - /* box clears something*/ - box->y += y - cy; - cy = y; + if (box->gadget->type == GADGET_TEXTAREA) { + if (width == AUTO) { + size = INTTOFIX(10); + width = FIXTOINT(nscss_len2px(size, unit, + box->style)); + } + if (height == AUTO) { + size = INTTOFIX(4); + height = FIXTOINT(nscss_len2px(size, unit, + box->style)); + } } + } else if (width == AUTO) { + /* CSS 2.1 section 10.3.5 */ + width = min(max(box->min_width, available_width), + box->max_width); - /* Unless the box has an overflow style of visible, the box - * establishes a new block context. */ - if (box->type == BOX_BLOCK && box->style && - (overflow_x != CSS_OVERFLOW_VISIBLE || - overflow_y != CSS_OVERFLOW_VISIBLE)) { + /* width includes margin, borders and padding */ + if (width == available_width) { + width -= box->margin[LEFT] + box->border[LEFT].width + + box->padding[LEFT] + + box->padding[RIGHT] + + box->border[RIGHT].width + + box->margin[RIGHT]; + } else { + /* width was obtained from a min_width or max_width + * value, so need to use the same method for calculating + * mbp as was used in layout_minmax_block() */ + int fixed = 0; + float frac = 0; + calculate_mbp_width(box->style, LEFT, true, true, true, + &fixed, &frac); + calculate_mbp_width(box->style, RIGHT, true, true, true, + &fixed, &frac); + if (fixed < 0) + fixed = 0; - layout_block_context(box, viewport_height, content); + width -= fixed; + } - cy += box->padding[TOP]; + if (max_width >= 0 && width > max_width) width = max_width; + if (min_width > 0 && width < min_width) width = min_width; - if (box->height == AUTO) { - box->height = 0; - layout_block_add_scrollbar(box, BOTTOM); - } + } else { + if (max_width >= 0 && width > max_width) width = max_width; + if (min_width > 0 && width < min_width) width = min_width; + width -= scrollbar_width_y; + } - cx -= box->x; - cy += box->height + box->padding[BOTTOM] + - box->border[BOTTOM].width; - y = box->y + box->padding[TOP] + box->height + - box->padding[BOTTOM] + - box->border[BOTTOM].width; + box->width = width; + box->height = height; - /* Skip children, because they are done in the new - * block context */ - goto advance_to_next_box; - } + if (margin[TOP] == AUTO) + margin[TOP] = 0; + if (margin[BOTTOM] == AUTO) + margin[BOTTOM] = 0; +} - NSLOG(layout, DEBUG, "box %p, cx %i, cy %i", box, cx, cy); - /* Layout (except tables). */ - if (box->object) { - if (!layout_block_object(box)) - return false; +/** + * Layout the contents of a float or inline block. + * + * \param b float or inline block box + * \param width available width + * \param content memory pool for any new boxes + * \return true on success, false on memory exhaustion + */ +static bool layout_float(struct box *b, int width, html_content *content) +{ + assert(b->type == BOX_TABLE || b->type == BOX_BLOCK || + b->type == BOX_INLINE_BLOCK); + layout_float_find_dimensions(width, b->style, b); + if (b->type == BOX_TABLE) { + if (!layout_table(b, width, content)) + return false; + if (b->margin[LEFT] == AUTO) + b->margin[LEFT] = 0; + if (b->margin[RIGHT] == AUTO) + b->margin[RIGHT] = 0; + if (b->margin[TOP] == AUTO) + b->margin[TOP] = 0; + if (b->margin[BOTTOM] == AUTO) + b->margin[BOTTOM] = 0; + } else + return layout_block_context(b, -1, content); + return true; +} - } else if (box->type == BOX_INLINE_CONTAINER) { - box->width = box->parent->width; - if (!layout_inline_container(box, box->width, block, - cx, cy, content)) - return false; - } else if (box->type == BOX_TABLE) { - /* Move down to avoid floats if necessary. */ - int x0, x1; - struct box *left, *right; - y = cy; - while (1) { - enum css_width_e wtype; - css_fixed width = 0; - css_unit unit = CSS_UNIT_PX; - - wtype = css_computed_width(box->style, - &width, &unit); - - x0 = cx; - x1 = cx + box->parent->width; - find_sides(block->float_children, y, - y + box->height, - &x0, &x1, &left, &right); - if (wtype == CSS_WIDTH_AUTO) - break; - if (box->width <= x1 - x0) - break; - if (!left && !right) - break; - else if (!left) - y = right->y + right->height + 1; - else if (!right) - y = left->y + left->height + 1; - else if (left->y + left->height < - right->y + right->height) - y = left->y + left->height + 1; - else - y = right->y + right->height + 1; - } - box->x += x0 - cx; - cx = x0; - box->y += y - cy; - cy = y; - } - - /* Advance to next box. */ - if (box->type == BOX_BLOCK && !box->object && !(box->iframe) && - box->children) { - /* Down into children. */ - - if (box == margin_collapse) { - /* Current margin collapsed though to this box. - * Unset margin_collapse. */ - margin_collapse = NULL; - } - - y = box->padding[TOP]; - box = box->children; - box->y = y; - cy += y; - continue; - } else if (box->type == BOX_BLOCK || box->object || - box->flags & IFRAME) - cy += box->padding[TOP]; - - if (box->type == BOX_BLOCK && box->height == AUTO) { - box->height = 0; - layout_block_add_scrollbar(box, BOTTOM); - } - - cy += box->height + box->padding[BOTTOM] + - box->border[BOTTOM].width; - cx -= box->x; - y = box->y + box->padding[TOP] + box->height + - box->padding[BOTTOM] + - box->border[BOTTOM].width; - - advance_to_next_box: - if (!box->next) { - /* No more siblings: - * up to first ancestor with a sibling. */ - - do { - if (box == margin_collapse) { - /* Current margin collapsed though to - * this box. Unset margin_collapse. */ - margin_collapse = NULL; - } - - /* Apply bottom margin */ - if (max_pos_margin < box->margin[BOTTOM]) - max_pos_margin = box->margin[BOTTOM]; - else if (max_neg_margin < -box->margin[BOTTOM]) - max_neg_margin = -box->margin[BOTTOM]; - - box = box->parent; - if (box == block) - break; - - /* Margin is invalidated if this is a box - * margins can't collapse through. */ - if (box->type == BOX_BLOCK && - box->flags & MAKE_HEIGHT) { - margin_collapse = NULL; - in_margin = false; - max_pos_margin = max_neg_margin = 0; - } - - if (box->height == AUTO) { - box->height = y - box->padding[TOP]; - - if (box->type == BOX_BLOCK) - layout_block_add_scrollbar(box, - BOTTOM); - } else - cy += box->height - - (y - box->padding[TOP]); - - /* Apply any min-height and max-height to - * boxes in normal flow */ - if (box->style && - css_computed_position(box->style) != - CSS_POSITION_ABSOLUTE && - layout_apply_minmax_height(box, - NULL)) { - /* Height altered */ - /* Set current cy */ - cy += box->height - - (y - box->padding[TOP]); - } - - cy += box->padding[BOTTOM] + - box->border[BOTTOM].width; - cx -= box->x; - y = box->y + box->padding[TOP] + box->height + - box->padding[BOTTOM] + - box->border[BOTTOM].width; +/** + * Position a float in the first available space. + * + * \param c float box to position + * \param width available width + * \param cx x coordinate relative to cont to place float right of + * \param y y coordinate relative to cont to place float below + * \param cont ancestor box which defines horizontal space, for floats + */ +static void +place_float_below(struct box *c, int width, int cx, int y, struct box *cont) +{ + int x0, x1, yy; + struct box *left; + struct box *right; - } while (box->next == NULL); - if (box == block) - break; - } + yy = y > cont->cached_place_below_level ? + y : cont->cached_place_below_level; - /* To next sibling. */ + NSLOG(layout, DEBUG, + "c %p, width %i, cx %i, y %i, cont %p", c, + width, cx, y, cont); - if (box == margin_collapse) { - /* Current margin collapsed though to this box. - * Unset margin_collapse. */ - margin_collapse = NULL; + do { + y = yy; + x0 = cx; + x1 = cx + width; + find_sides(cont->float_children, y, y + c->height, &x0, &x1, + &left, &right); + if (left != 0 && right != 0) { + yy = (left->y + left->height < + right->y + right->height ? + left->y + left->height : + right->y + right->height); + } else if (left == 0 && right != 0) { + yy = right->y + right->height; + } else if (left != 0 && right == 0) { + yy = left->y + left->height; } + } while ((left != 0 || right != 0) && (c->width > x1 - x0)); - if (max_pos_margin < box->margin[BOTTOM]) - max_pos_margin = box->margin[BOTTOM]; - else if (max_neg_margin < -box->margin[BOTTOM]) - max_neg_margin = -box->margin[BOTTOM]; - - box = box->next; - box->y = y; - } - - /* Account for bottom margin of last contained block */ - cy += max_pos_margin - max_neg_margin; - - /* Increase height to contain any floats inside (CSS 2.1 10.6.7). */ - for (box = block->float_children; box; box = box->next_float) { - y = box->y + box->height + box->padding[BOTTOM] + - box->border[BOTTOM].width + box->margin[BOTTOM]; - if (cy < y) - cy = y; - } - - if (block->height == AUTO) { - block->height = cy - block->padding[TOP]; - if (block->type == BOX_BLOCK) - layout_block_add_scrollbar(block, BOTTOM); - } - - if (block->style && css_computed_position(block->style) != - CSS_POSITION_ABSOLUTE) { - /* Block is in normal flow */ - layout_apply_minmax_height(block, NULL); - } - - if (block->gadget && - (block->gadget->type == GADGET_TEXTAREA || - block->gadget->type == GADGET_PASSWORD || - block->gadget->type == GADGET_TEXTBOX)) { - int ta_width = block->padding[LEFT] + block->width + - block->padding[RIGHT]; - int ta_height = block->padding[TOP] + block->height + - block->padding[BOTTOM]; - textarea_set_layout(block->gadget->data.text.ta, - ta_width, ta_height, - block->padding[TOP], block->padding[RIGHT], - block->padding[BOTTOM], block->padding[LEFT]); + if (c->type == BOX_FLOAT_LEFT) { + c->x = x0; + } else { + c->x = x1 - c->width; } - - return true; + c->y = y; + cont->cached_place_below_level = y; } @@ -3160,1944 +3007,2107 @@ static int line_height(const css_computed_style *style) /** - * Layout list markers. + * Position a line of boxes in inline formatting context. + * + * \param first box at start of line + * \param width available width on input, updated with actual width on output + * (may be incorrect if the line gets split?) + * \param y coordinate of top of line, updated on exit to bottom + * \param cx coordinate of left of line relative to cont + * \param cy coordinate of top of line relative to cont + * \param cont ancestor box which defines horizontal space, for floats + * \param indent apply any first-line indent + * \param has_text_children at least one TEXT in the inline_container + * \param next_box updated to first box for next line, or 0 at end + * \param content memory pool for any new boxes + * \return true on success, false on memory exhaustion */ -static void -layout_lists(struct box *box, - const struct gui_layout_table *font_func) +static bool +layout_line(struct box *first, + int *width, + int *y, + int cx, + int cy, + struct box *cont, + bool indent, + bool has_text_children, + html_content *content, + struct box **next_box) { - struct box *child; - struct box *marker; - plot_font_style_t fstyle; - - for (child = box->children; child; child = child->next) { - if (child->list_marker) { - marker = child->list_marker; - if (marker->object) { - marker->width = - content_get_width(marker->object); - marker->x = -marker->width; - marker->height = - content_get_height(marker->object); - marker->y = (line_height(marker->style) - - marker->height) / 2; - } else if (marker->text) { - if (marker->width == UNKNOWN_WIDTH) { - font_plot_style_from_css(marker->style, - &fstyle); - font_func->width(&fstyle, - marker->text, - marker->length, - &marker->width); - marker->flags |= MEASURED; - } - marker->x = -marker->width; - marker->y = 0; - marker->height = line_height(marker->style); - } else { - marker->x = 0; - marker->y = 0; - marker->width = 0; - marker->height = 0; - } - /* Gap between marker and content */ - marker->x -= 4; - } - layout_lists(child, font_func); - } -} + int height, used_height; + int x0 = 0; + int x1 = *width; + int x, h, x_previous; + int fy = cy; + struct box *left; + struct box *right; + struct box *b; + struct box *split_box = 0; + struct box *d; + struct box *br_box = 0; + bool move_y = false; + bool place_below = false; + int space_before = 0, space_after = 0; + unsigned int inline_count = 0; + unsigned int i; + const struct gui_layout_table *font_func = content->font_func; + plot_font_style_t fstyle; + NSLOG(layout, DEBUG, + "first %p, first->text '%.*s', width %i, y %i, cx %i, cy %i", + first, + (int)first->length, + first->text, + *width, + *y, + cx, + cy); -/** - * Compute box offsets for a relatively or absolutely positioned box with - * respect to a box. - * - * \param box box to compute offsets for - * \param containing_block box to compute percentages with respect to - * \param top updated to top offset, or AUTO - * \param right updated to right offset, or AUTO - * \param bottom updated to bottom offset, or AUTO - * \param left updated to left offset, or AUTO - * - * See CSS 2.1 9.3.2. containing_block must have width and height. - */ -static void -layout_compute_offsets(struct box *box, - struct box *containing_block, - int *top, - int *right, - int *bottom, - int *left) -{ - uint32_t type; - css_fixed value = 0; - css_unit unit = CSS_UNIT_PX; + /* find sides at top of line */ + x0 += cx; + x1 += cx; + find_sides(cont->float_children, cy, cy, &x0, &x1, &left, &right); + x0 -= cx; + x1 -= cx; - assert(containing_block->width != UNKNOWN_WIDTH && - containing_block->width != AUTO && - containing_block->height != AUTO); + if (indent) + x0 += layout_text_indent(first->parent->parent->style, *width); - /* left */ - type = css_computed_left(box->style, &value, &unit); - if (type == CSS_LEFT_SET) { - if (unit == CSS_UNIT_PCT) { - *left = FPCT_OF_INT_TOINT(value, - containing_block->width); - } else { - *left = FIXTOINT(nscss_len2px(value, unit, box->style)); - } - } else { - *left = AUTO; - } + if (x1 < x0) + x1 = x0; - /* right */ - type = css_computed_right(box->style, &value, &unit); - if (type == CSS_RIGHT_SET) { - if (unit == CSS_UNIT_PCT) { - *right = FPCT_OF_INT_TOINT(value, - containing_block->width); - } else { - *right = FIXTOINT(nscss_len2px(value, unit, - box->style)); - } - } else { - *right = AUTO; - } + /* get minimum line height from containing block. + * this is the line-height if there are text children and also in the + * case of an initially empty text input */ + if (has_text_children || first->parent->parent->gadget) + used_height = height = + line_height(first->parent->parent->style); + else + /* inline containers with no text are usually for layout and + * look better with no minimum line-height */ + used_height = height = 0; - /* top */ - type = css_computed_top(box->style, &value, &unit); - if (type == CSS_TOP_SET) { - if (unit == CSS_UNIT_PCT) { - *top = FPCT_OF_INT_TOINT(value, - containing_block->height); - } else { - *top = FIXTOINT(nscss_len2px(value, unit, box->style)); - } - } else { - *top = AUTO; - } + /* pass 1: find height of line assuming sides at top of line: loop + * body executed at least once + * keep in sync with the loop in layout_minmax_line() */ - /* bottom */ - type = css_computed_bottom(box->style, &value, &unit); - if (type == CSS_BOTTOM_SET) { - if (unit == CSS_UNIT_PCT) { - *bottom = FPCT_OF_INT_TOINT(value, - containing_block->height); - } else { - *bottom = FIXTOINT(nscss_len2px(value, unit, - box->style)); - } - } else { - *bottom = AUTO; - } -} + NSLOG(layout, DEBUG, "x0 %i, x1 %i, x1 - x0 %i", x0, x1, x1 - x0); -/** - * Layout and position an absolutely positioned box. - * - * \param box absolute box to layout and position - * \param containing_block containing block - * \param cx position of box relative to containing_block - * \param cy position of box relative to containing_block - * \param content memory pool for any new boxes - * \return true on success, false on memory exhaustion - */ -static bool -layout_absolute(struct box *box, - struct box *containing_block, - int cx, int cy, - html_content *content) -{ - int static_left, static_top; /* static position */ - int top, right, bottom, left; - int width, height, max_width, min_width; - int *margin = box->margin; - int *padding = box->padding; - struct box_border *border = box->border; - int available_width = containing_block->width; - int space; + for (x = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) { + int min_width, max_width, min_height, max_height; - assert(box->type == BOX_BLOCK || box->type == BOX_TABLE || - box->type == BOX_INLINE_BLOCK); + assert(b->type == BOX_INLINE || b->type == BOX_INLINE_BLOCK || + b->type == BOX_FLOAT_LEFT || + b->type == BOX_FLOAT_RIGHT || + b->type == BOX_BR || b->type == BOX_TEXT || + b->type == BOX_INLINE_END); - /* The static position is where the box would be if it was not - * absolutely positioned. The x and y are filled in by - * layout_block_context(). */ - static_left = cx + box->x; - static_top = cy + box->y; - if (containing_block->type == BOX_BLOCK || - containing_block->type == BOX_INLINE_BLOCK || - containing_block->type == BOX_TABLE_CELL) { - /* Block level container => temporarily increase containing - * block dimensions to include padding (we restore this - * again at the end) */ - containing_block->width += containing_block->padding[LEFT] + - containing_block->padding[RIGHT]; - containing_block->height += containing_block->padding[TOP] + - containing_block->padding[BOTTOM]; - } else { - /** \todo inline containers */ - } + NSLOG(layout, DEBUG, "pass 1: b %p, x %i", b, x); - layout_compute_offsets(box, containing_block, - &top, &right, &bottom, &left); - /* Pass containing block into layout_find_dimensions via the float - * containing block box member. This is unused for absolutely positioned - * boxes because a box can't be floated and absolutely positioned. */ - box->float_container = containing_block; - layout_find_dimensions(available_width, -1, box, box->style, - &width, &height, &max_width, &min_width, - 0, 0, margin, padding, border); - box->float_container = NULL; + if (b->type == BOX_BR) + break; - /* 10.3.7 */ - NSLOG(layout, DEBUG, - "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", - left, margin[LEFT], border[LEFT].width, padding[LEFT], width, - padding[RIGHT], border[RIGHT].width, margin[RIGHT], right, - containing_block->width); + if (b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT) + continue; + if (b->type == BOX_INLINE_BLOCK && + (css_computed_position(b->style) == + CSS_POSITION_ABSOLUTE || + css_computed_position(b->style) == + CSS_POSITION_FIXED)) + continue; + assert(b->style != NULL); + font_plot_style_from_css(b->style, &fstyle); - if (left == AUTO && width == AUTO && right == AUTO) { - if (margin[LEFT] == AUTO) - margin[LEFT] = 0; - if (margin[RIGHT] == AUTO) - margin[RIGHT] = 0; - left = static_left; + x += space_after; - width = min(max(box->min_width, available_width), - box->max_width); - width -= box->margin[LEFT] + box->border[LEFT].width + - box->padding[LEFT] + box->padding[RIGHT] + - box->border[RIGHT].width + box->margin[RIGHT]; - - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) width = max_width; - if (width < min_width) width = min_width; - - right = containing_block->width - - left - - margin[LEFT] - border[LEFT].width - padding[LEFT] - - width - - padding[RIGHT] - border[RIGHT].width - margin[RIGHT]; - } else if (left != AUTO && width != AUTO && right != AUTO) { - - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) width = max_width; - if (min_width > 0 && width < min_width) width = min_width; + if (b->type == BOX_INLINE_BLOCK) { + if (b->max_width != UNKNOWN_WIDTH) + if (!layout_float(b, *width, content)) + return false; + h = b->border[TOP].width + b->padding[TOP] + b->height + + b->padding[BOTTOM] + + b->border[BOTTOM].width; + if (height < h) + height = h; + x += b->margin[LEFT] + b->border[LEFT].width + + b->padding[LEFT] + b->width + + b->padding[RIGHT] + + b->border[RIGHT].width + + b->margin[RIGHT]; + space_after = 0; + continue; + } - if (margin[LEFT] == AUTO && margin[RIGHT] == AUTO) { - space = containing_block->width - - left - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - right; - if (space < 0) { - margin[LEFT] = 0; - margin[RIGHT] = space; + if (b->type == BOX_INLINE) { + /* calculate borders, margins, and padding */ + layout_find_dimensions(*width, -1, b, b->style, 0, 0, + 0, 0, 0, 0, b->margin, b->padding, + b->border); + for (i = 0; i != 4; i++) + if (b->margin[i] == AUTO) + b->margin[i] = 0; + x += b->margin[LEFT] + b->border[LEFT].width + + b->padding[LEFT]; + if (b->inline_end) { + b->inline_end->margin[RIGHT] = b->margin[RIGHT]; + b->inline_end->padding[RIGHT] = + b->padding[RIGHT]; + b->inline_end->border[RIGHT] = + b->border[RIGHT]; } else { - margin[LEFT] = margin[RIGHT] = space / 2; + x += b->padding[RIGHT] + + b->border[RIGHT].width + + b->margin[RIGHT]; } - } else if (margin[LEFT] == AUTO) { - margin[LEFT] = containing_block->width - - left - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT] - - right; - } else if (margin[RIGHT] == AUTO) { - margin[RIGHT] = containing_block->width - - left - margin[LEFT] - - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - right; - } else { - right = containing_block->width - - left - margin[LEFT] - - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT]; + } else if (b->type == BOX_INLINE_END) { + b->width = 0; + if (b->space == UNKNOWN_WIDTH) { + font_func->width(&fstyle, " ", 1, &b->space); + /** \todo handle errors */ + } + space_after = b->space; + + x += b->padding[RIGHT] + b->border[RIGHT].width + + b->margin[RIGHT]; + continue; } - } else { - if (margin[LEFT] == AUTO) - margin[LEFT] = 0; - if (margin[RIGHT] == AUTO) - margin[RIGHT] = 0; - if (left == AUTO && width == AUTO && right != AUTO) { - available_width -= right; + if (!b->object && !(b->flags & IFRAME) && !b->gadget && + !(b->flags & REPLACE_DIM)) { + /* inline non-replaced, 10.3.1 and 10.6.1 */ + b->height = line_height(b->style ? b->style : + b->parent->parent->style); + if (height < b->height) + height = b->height; - width = min(max(box->min_width, available_width), - box->max_width); - width -= box->margin[LEFT] + box->border[LEFT].width + - box->padding[LEFT] + box->padding[RIGHT] + - box->border[RIGHT].width + box->margin[RIGHT]; + if (!b->text) { + b->width = 0; + space_after = 0; + continue; + } - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) - width = max_width; - if (width < min_width) - width = min_width; + if (b->width == UNKNOWN_WIDTH) { + /** \todo handle errors */ - left = containing_block->width - - margin[LEFT] - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT] - - right; - } else if (left == AUTO && width != AUTO && right == AUTO) { + /* If it's a select element, we must use the + * width of the widest option text */ + if (b->parent->parent->gadget && + b->parent->parent->gadget->type + == GADGET_SELECT) { + int opt_maxwidth = 0; + struct form_option *o; - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) - width = max_width; - if (min_width > 0 && width < min_width) - width = min_width; + for (o = b->parent->parent->gadget-> + data.select.items; o; + o = o->next) { + int opt_width; + font_func->width(&fstyle, + o->text, + strlen(o->text), + &opt_width); - left = static_left; - right = containing_block->width - - left - margin[LEFT] - - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT]; - } else if (left != AUTO && width == AUTO && right == AUTO) { - available_width -= left; + if (opt_maxwidth < opt_width) + opt_maxwidth =opt_width; + } + b->width = opt_maxwidth; + if (nsoption_bool(core_select_menu)) + b->width += SCROLLBAR_WIDTH; + } else { + font_func->width(&fstyle, b->text, + b->length, &b->width); + b->flags |= MEASURED; + } + } - width = min(max(box->min_width, available_width), - box->max_width); - width -= box->margin[LEFT] + box->border[LEFT].width + - box->padding[LEFT] + box->padding[RIGHT] + - box->border[RIGHT].width + box->margin[RIGHT]; + /* If the current text has not been measured (i.e. its + * width was estimated after splitting), and it fits on + * the line, measure it properly, so next box is placed + * correctly. */ + if (b->text && (x + b->width < x1 - x0) && + !(b->flags & MEASURED) && + b->next) { + font_func->width(&fstyle, b->text, + b->length, &b->width); + b->flags |= MEASURED; + } - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) - width = max_width; - if (width < min_width) - width = min_width; + x += b->width; + if (b->space == UNKNOWN_WIDTH) { + font_func->width(&fstyle, " ", 1, &b->space); + /** \todo handle errors */ + } + space_after = b->space; + continue; + } - right = containing_block->width - - left - margin[LEFT] - - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT]; - } else if (left == AUTO && width != AUTO && right != AUTO) { + space_after = 0; - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) - width = max_width; - if (width < min_width) - width = min_width; + /* inline replaced, 10.3.2 and 10.6.2 */ + assert(b->style); - left = containing_block->width - - margin[LEFT] - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT] - - right; - } else if (left != AUTO && width == AUTO && right != AUTO) { - width = containing_block->width - - left - margin[LEFT] - - border[LEFT].width - - padding[LEFT] - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT] - - right; + layout_find_dimensions(*width, -1, b, b->style, + &b->width, &b->height, &max_width, &min_width, + &max_height, &min_height, NULL, NULL, NULL); - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) - width = max_width; - if (width < min_width) - width = min_width; + if (b->object && !(b->flags & REPLACE_DIM)) { + layout_get_object_dimensions(b, &b->width, &b->height, + min_width, max_width, + min_height, max_height); + } else if (b->flags & IFRAME) { + /* TODO: should we look at the content dimensions? */ + if (b->width == AUTO) + b->width = 400; + if (b->height == AUTO) + b->height = 300; - } else if (left != AUTO && width != AUTO && right == AUTO) { + /* We reformat the iframe browser window to new + * dimensions in pass 2 */ + } else { + /* form control with no object */ + if (b->width == AUTO) + b->width = FIXTOINT(nscss_len2px(INTTOFIX(1), + CSS_UNIT_EM, b->style)); + if (b->height == AUTO) + b->height = FIXTOINT(nscss_len2px(INTTOFIX(1), + CSS_UNIT_EM, b->style)); + } - /* Adjust for {min|max}-width */ - if (max_width >= 0 && width > max_width) - width = max_width; - if (width < min_width) - width = min_width; + /* Reformat object to new box size */ + if (b->object && content_get_type(b->object) == CONTENT_HTML && + b->width != + content_get_available_width(b->object)) { + css_fixed value = 0; + css_unit unit = CSS_UNIT_PX; + enum css_height_e htype = css_computed_height(b->style, + &value, &unit); - right = containing_block->width - - left - margin[LEFT] - - border[LEFT].width - - padding[LEFT] - width - padding[RIGHT] - - border[RIGHT].width - margin[RIGHT]; + content_reformat(b->object, false, b->width, b->height); + + if (htype == CSS_HEIGHT_AUTO) + b->height = content_get_height(b->object); } - } - NSLOG(layout, DEBUG, - "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", - left, margin[LEFT], border[LEFT].width, padding[LEFT], width, - padding[RIGHT], border[RIGHT].width, margin[RIGHT], right, - containing_block->width); + if (height < b->height) + height = b->height; - box->x = left + margin[LEFT] + border[LEFT].width - cx; - if (containing_block->type == BOX_BLOCK || - containing_block->type == BOX_INLINE_BLOCK || - containing_block->type == BOX_TABLE_CELL) { - /* Block-level ancestor => reset container's width */ - containing_block->width -= containing_block->padding[LEFT] + - containing_block->padding[RIGHT]; - } else { - /** \todo inline ancestors */ + x += b->width; } - box->width = width; - box->height = height; - if (box->type == BOX_BLOCK || box->type == BOX_INLINE_BLOCK || - box->object || box->flags & IFRAME) { - if (!layout_block_context(box, -1, content)) - return false; - } else if (box->type == BOX_TABLE) { - /* layout_table also expects the containing block to be - * stored in the float_container field */ - box->float_container = containing_block; - /* \todo layout_table considers margins etc. again */ - if (!layout_table(box, width, content)) - return false; - box->float_container = NULL; - layout_solve_width(box, box->parent->width, box->width, 0, 0, - -1, -1); - } + /* find new sides using this height */ + x0 = cx; + x1 = cx + *width; + find_sides(cont->float_children, cy, cy + height, &x0, &x1, + &left, &right); + x0 -= cx; + x1 -= cx; - /* 10.6.4 */ - NSLOG(layout, DEBUG, - "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", - top, margin[TOP], border[TOP].width, padding[TOP], height, - padding[BOTTOM], border[BOTTOM].width, margin[BOTTOM], bottom, - containing_block->height); + if (indent) + x0 += layout_text_indent(first->parent->parent->style, *width); - if (top == AUTO && height == AUTO && bottom == AUTO) { - top = static_top; - height = box->height; - if (margin[TOP] == AUTO) - margin[TOP] = 0; - if (margin[BOTTOM] == AUTO) - margin[BOTTOM] = 0; - bottom = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - height - padding[BOTTOM] - - border[BOTTOM].width - margin[BOTTOM]; - } else if (top != AUTO && height != AUTO && bottom != AUTO) { - if (margin[TOP] == AUTO && margin[BOTTOM] == AUTO) { - space = containing_block->height - - top - border[TOP].width - padding[TOP] - - height - padding[BOTTOM] - - border[BOTTOM].width - bottom; - margin[TOP] = margin[BOTTOM] = space / 2; - } else if (margin[TOP] == AUTO) { - margin[TOP] = containing_block->height - - top - border[TOP].width - padding[TOP] - - height - padding[BOTTOM] - - border[BOTTOM].width - margin[BOTTOM] - - bottom; - } else if (margin[BOTTOM] == AUTO) { - margin[BOTTOM] = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - bottom; - } else { - bottom = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - margin[BOTTOM]; - } - } else { - if (margin[TOP] == AUTO) - margin[TOP] = 0; - if (margin[BOTTOM] == AUTO) - margin[BOTTOM] = 0; - if (top == AUTO && height == AUTO && bottom != AUTO) { - height = box->height; - top = containing_block->height - - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - margin[BOTTOM] - bottom; - } else if (top == AUTO && height != AUTO && bottom == AUTO) { - top = static_top; - bottom = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - margin[BOTTOM]; - } else if (top != AUTO && height == AUTO && bottom == AUTO) { - height = box->height; - bottom = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - margin[BOTTOM]; - } else if (top == AUTO && height != AUTO && bottom != AUTO) { - top = containing_block->height - - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - margin[BOTTOM] - bottom; - } else if (top != AUTO && height == AUTO && bottom != AUTO) { - height = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - padding[BOTTOM] - - border[BOTTOM].width - margin[BOTTOM] - - bottom; - } else if (top != AUTO && height != AUTO && bottom == AUTO) { - bottom = containing_block->height - - top - margin[TOP] - border[TOP].width - - padding[TOP] - height - - padding[BOTTOM] - border[BOTTOM].width - - margin[BOTTOM]; - } - } + if (x1 < x0) + x1 = x0; - NSLOG(layout, DEBUG, - "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", - top, margin[TOP], border[TOP].width, padding[TOP], height, - padding[BOTTOM], border[BOTTOM].width, margin[BOTTOM], bottom, - containing_block->height); + space_after = space_before = 0; - box->y = top + margin[TOP] + border[TOP].width - cy; - if (containing_block->type == BOX_BLOCK || - containing_block->type == BOX_INLINE_BLOCK || - containing_block->type == BOX_TABLE_CELL) { - /* Block-level ancestor => reset container's height */ - containing_block->height -= containing_block->padding[TOP] + - containing_block->padding[BOTTOM]; - } else { - /** \todo Inline ancestors */ - } - box->height = height; - layout_apply_minmax_height(box, containing_block); + /* pass 2: place boxes in line: loop body executed at least once */ - return true; -} + NSLOG(layout, DEBUG, "x0 %i, x1 %i, x1 - x0 %i", x0, x1, x1 - x0); + for (x = x_previous = 0, b = first; x <= x1 - x0 && b; b = b->next) { -/** - * Recursively layout and position absolutely positioned boxes. - * - * \param box tree of boxes to layout - * \param containing_block current containing block - * \param cx position of box relative to containing_block - * \param cy position of box relative to containing_block - * \param content memory pool for any new boxes - * \return true on success, false on memory exhaustion - */ -static bool -layout_position_absolute(struct box *box, - struct box *containing_block, - int cx, int cy, - html_content *content) -{ - struct box *c; + NSLOG(layout, DEBUG, "pass 2: b %p, x %i", b, x); - for (c = box->children; c; c = c->next) { - if ((c->type == BOX_BLOCK || c->type == BOX_TABLE || - c->type == BOX_INLINE_BLOCK) && - (css_computed_position(c->style) == + if (b->type == BOX_INLINE_BLOCK && + (css_computed_position(b->style) == CSS_POSITION_ABSOLUTE || - css_computed_position(c->style) == + css_computed_position(b->style) == CSS_POSITION_FIXED)) { - if (!layout_absolute(c, containing_block, - cx, cy, content)) - return false; - if (!layout_position_absolute(c, c, 0, 0, content)) - return false; - } else if (c->style && css_computed_position(c->style) == - CSS_POSITION_RELATIVE) { - if (!layout_position_absolute(c, c, 0, 0, content)) - return false; - } else { - int px, py; - if (c->style && (css_computed_float(c->style) == - CSS_FLOAT_LEFT || - css_computed_float(c->style) == - CSS_FLOAT_RIGHT)) { - /* Float x/y coords are relative to nearest - * ansestor with float_children, rather than - * relative to parent. Need to get x/y relative - * to parent */ - struct box *p; - px = c->x; - py = c->y; - for (p = box->parent; p && !p->float_children; - p = p->parent) { - px -= p->x; - py -= p->y; - } - } else { - /* Not a float, so box x/y coords are relative - * to parent */ - px = c->x; - py = c->y; - } - if (!layout_position_absolute(c, containing_block, - cx + px, cy + py, content)) - return false; - } - } + b->x = x + space_after; - return true; -} + } else if (b->type == BOX_INLINE || + b->type == BOX_INLINE_BLOCK || + b->type == BOX_TEXT || + b->type == BOX_INLINE_END) { + assert(b->width != UNKNOWN_WIDTH); + x_previous = x; + x += space_after; + b->x = x; -/** - * Compute a box's relative offset as per CSS 2.1 9.4.3 - * - * \param box Box to compute relative offsets for. - * \param x Receives relative offset in x. - * \param y Receives relative offset in y. - */ -static void layout_compute_relative_offset(struct box *box, int *x, int *y) -{ - int left, right, top, bottom; - struct box *containing_block; - - assert(box && box->parent && box->style && - css_computed_position(box->style) == - CSS_POSITION_RELATIVE); - - if (box->float_container && - (css_computed_float(box->style) == CSS_FLOAT_LEFT || - css_computed_float(box->style) == CSS_FLOAT_RIGHT)) { - containing_block = box->float_container; - } else { - containing_block = box->parent; - } + if ((b->type == BOX_INLINE && !b->inline_end) || + b->type == BOX_INLINE_BLOCK) { + b->x += b->margin[LEFT] + b->border[LEFT].width; + x = b->x + b->padding[LEFT] + b->width + + b->padding[RIGHT] + + b->border[RIGHT].width + + b->margin[RIGHT]; + } else if (b->type == BOX_INLINE) { + b->x += b->margin[LEFT] + b->border[LEFT].width; + x = b->x + b->padding[LEFT] + b->width; + } else if (b->type == BOX_INLINE_END) { + b->height = b->inline_end->height; + x += b->padding[RIGHT] + + b->border[RIGHT].width + + b->margin[RIGHT]; + } else { + x += b->width; + } - layout_compute_offsets(box, containing_block, - &top, &right, &bottom, &left); + space_before = space_after; + if (b->object || b->flags & REPLACE_DIM || + b->flags & IFRAME) + space_after = 0; + else if (b->text || b->type == BOX_INLINE_END) { + if (b->space == UNKNOWN_WIDTH) { + font_plot_style_from_css(b->style, + &fstyle); + /** \todo handle errors */ + font_func->width(&fstyle, " ", 1, + &b->space); + } + space_after = b->space; + } else { + space_after = 0; + } + split_box = b; + move_y = true; + inline_count++; + } else if (b->type == BOX_BR) { + b->x = x; + b->width = 0; + br_box = b; + b = b->next; + split_box = 0; + move_y = true; + break; - if (left == AUTO && right == AUTO) - left = right = 0; - else if (left == AUTO) - /* left is auto => computed = -right */ - left = -right; - else if (right == AUTO) - /* right is auto => computed = -left */ - right = -left; - else { - /* over constrained => examine direction property - * of containing block */ - if (containing_block->style && - css_computed_direction( - containing_block->style) == - CSS_DIRECTION_RTL) { - /* right wins */ - left = -right; } else { - /* assume LTR in all other cases */ - right = -left; - } - } + /* float */ + NSLOG(layout, DEBUG, "float %p", b); - assert(left == -right); + d = b->children; + d->float_children = 0; + d->cached_place_below_level = 0; + b->float_container = d->float_container = cont; - if (top == AUTO && bottom == AUTO) { - top = bottom = 0; - } else if (top == AUTO) { - top = -bottom; - } else { - /* bottom is AUTO, or neither are AUTO */ - bottom = -top; - } + if (!layout_float(d, *width, content)) + return false; - NSLOG(layout, DEBUG, "left %i, right %i, top %i, bottom %i", left, - right, top, bottom); + NSLOG(layout, DEBUG, + "%p : %d %d", + d, + d->margin[TOP], + d->border[TOP].width); - *x = left; - *y = top; -} + d->x = d->margin[LEFT] + d->border[LEFT].width; + d->y = d->margin[TOP] + d->border[TOP].width; + b->width = d->margin[LEFT] + d->border[LEFT].width + + d->padding[LEFT] + d->width + + d->padding[RIGHT] + + d->border[RIGHT].width + + d->margin[RIGHT]; + b->height = d->margin[TOP] + d->border[TOP].width + + d->padding[TOP] + d->height + + d->padding[BOTTOM] + + d->border[BOTTOM].width + + d->margin[BOTTOM]; + if (b->width > (x1 - x0) - x) + place_below = true; + if (d->style && (css_computed_clear(d->style) == + CSS_CLEAR_NONE || + (css_computed_clear(d->style) == + CSS_CLEAR_LEFT && left == 0) || + (css_computed_clear(d->style) == + CSS_CLEAR_RIGHT && + right == 0) || + (css_computed_clear(d->style) == + CSS_CLEAR_BOTH && + left == 0 && right == 0)) && + (!place_below || + (left == 0 && right == 0 && x == 0)) && + cy >= cont->clear_level && + cy >= cont->cached_place_below_level) { + /* + not cleared or, + * cleared and there are no floats to clear + * + fits without needing to be placed below or, + * this line is empty with no floats + * + current y, cy, is below the clear level + * + * Float affects current line */ + if (b->type == BOX_FLOAT_LEFT) { + b->x = cx + x0; + if (b->width > 0) + x0 += b->width; + left = b; + } else { + b->x = cx + x1 - b->width; + if (b->width > 0) + x1 -= b->width; + right = b; + } + b->y = cy; + } else { + /* cleared or doesn't fit on line */ + /* place below into next available space */ + int fcy = (cy > cont->clear_level) ? cy : + cont->clear_level; + fcy = (fcy > cont->cached_place_below_level) ? + fcy : + cont->cached_place_below_level; + fy = (fy > fcy) ? fy : fcy; + fy = (fy == cy) ? fy + height : fy; -/** - * Adjust positions of relatively positioned boxes. - * - * \param root box to adjust the position of - * \param fp box which forms the block formatting context for children of - * "root" which are floats - * \param fx x offset due to intervening relatively positioned boxes - * between current box, "root", and the block formatting context - * box, "fp", for float children of "root" - * \param fy y offset due to intervening relatively positioned boxes - * between current box, "root", and the block formatting context - * box, "fp", for float children of "root" - */ -static void -layout_position_relative(struct box *root, struct box *fp, int fx, int fy) -{ - struct box *box; /* for children of "root" */ - struct box *fn; /* for block formatting context box for children of - * "box" */ - struct box *fc; /* for float children of the block formatting context, - * "fp" */ - int x, y; /* for the offsets resulting from any relative - * positioning on the current block */ - int fnx, fny; /* for affsets which apply to flat children of "box" */ + place_float_below(b, *width, cx, fy, cont); + fy = b->y; + if (d->style && ( + (css_computed_clear(d->style) == + CSS_CLEAR_LEFT && left != 0) || + (css_computed_clear(d->style) == + CSS_CLEAR_RIGHT && + right != 0) || + (css_computed_clear(d->style) == + CSS_CLEAR_BOTH && + (left != 0 || right != 0)))) { + /* to be cleared below existing + * floats */ + if (b->type == BOX_FLOAT_LEFT) + b->x = cx; + else + b->x = cx + *width - b->width; - /**\todo ensure containing box is large enough after moving boxes */ + fcy = layout_clear(cont->float_children, + css_computed_clear(d->style)); + if (fcy > cont->clear_level) + cont->clear_level = fcy; + if (b->y < fcy) + b->y = fcy; + } + if (b->type == BOX_FLOAT_LEFT) + left = b; + else + right = b; + } + add_float_to_container(cont, b); - assert(root); + split_box = 0; + } + } - /* Normal children */ - for (box = root->children; box; box = box->next) { + if (x1 - x0 < x && split_box) { + /* the last box went over the end */ + size_t split = 0; + int w; + bool no_wrap = css_computed_white_space( + split_box->style) == CSS_WHITE_SPACE_NOWRAP || + css_computed_white_space( + split_box->style) == CSS_WHITE_SPACE_PRE; - if (box->type == BOX_TEXT) - continue; + x = x_previous; - /* If relatively positioned, get offsets */ - if (box->style && css_computed_position(box->style) == - CSS_POSITION_RELATIVE) - layout_compute_relative_offset(box, &x, &y); - else - x = y = 0; + if (!no_wrap && + (split_box->type == BOX_INLINE || + split_box->type == BOX_TEXT) && + !split_box->object && + !(split_box->flags & REPLACE_DIM) && + !(split_box->flags & IFRAME) && + !split_box->gadget && split_box->text) { - /* Adjust float coordinates. - * (note float x and y are relative to their block formatting - * context box and not their parent) */ - if (box->style && (css_computed_float(box->style) == - CSS_FLOAT_LEFT || - css_computed_float(box->style) == - CSS_FLOAT_RIGHT) && - (fx != 0 || fy != 0)) { - /* box is a float and there is a float offset to - * apply */ - for (fc = fp->float_children; fc; fc = fc->next_float) { - if (box == fc->children) { - /* Box is floated in the block - * formatting context block, fp. - * Apply float offsets. */ - box->x += fx; - box->y += fy; - fx = fy = 0; - } - } + font_plot_style_from_css(split_box->style, &fstyle); + /** \todo handle errors */ + font_func->split(&fstyle, + split_box->text, + split_box->length, + x1 - x0 - x - space_before, + &split, + &w); } - if (box->float_children) { - fn = box; - fnx = fny = 0; - } else { - fn = fp; - fnx = fx + x; - fny = fy + y; - } + /* split == 0 implies that text can't be split */ - /* recurse first */ - layout_position_relative(box, fn, fnx, fny); + if (split == 0) + w = split_box->width; - /* Ignore things we're not interested in. */ - if (!box->style || (box->style && - css_computed_position(box->style) != - CSS_POSITION_RELATIVE)) - continue; - box->x += x; - box->y += y; + NSLOG(layout, DEBUG, + "splitting: split_box %p \"%.*s\", spilt %zu, w %i, " + "left %p, right %p, inline_count %u", + split_box, + (int)split_box->length, + split_box->text, + split, + w, + left, + right, + inline_count); - /* Handle INLINEs - their "children" are in fact - * the sibling boxes between the INLINE and - * INLINE_END boxes */ - if (box->type == BOX_INLINE && box->inline_end) { - struct box *b; - for (b = box->next; b && b != box->inline_end; - b = b->next) { - b->x += x; - b->y += y; + if ((split == 0 || x1 - x0 <= x + space_before + w) && + !left && !right && inline_count == 1) { + /* first word of box doesn't fit, but no floats and + * first box on line so force in */ + if (split == 0 || split == split_box->length) { + /* only one word in this box, or not text + * or white-space:nowrap */ + b = split_box->next; + } else { + /* cut off first word for this line */ + if (!layout_text_box_split(content, &fstyle, + split_box, split, w)) + return false; + b = split_box->next; } - } - } -} + x += space_before + w; + NSLOG(layout, DEBUG, "forcing"); -/* exported function documented in render/layout.h */ -bool layout_document(html_content *content, int width, int height) -{ - bool ret; - struct box *doc = content->layout; - const struct gui_layout_table *font_func = content->font_func; + } else if ((split == 0 || x1 - x0 <= x + space_before + w) && + inline_count == 1) { + /* first word of first box doesn't fit, but a float is + * taking some of the width so move below it */ + assert(left || right); + used_height = 0; + if (left) { - layout_minmax_block(doc, font_func); + NSLOG(layout, DEBUG, + "cy %i, left->y %i, left->height %i", + cy, + left->y, + left->height); - layout_block_find_dimensions(width, height, 0, 0, doc); - doc->x = doc->margin[LEFT] + doc->border[LEFT].width; - doc->y = doc->margin[TOP] + doc->border[TOP].width; - width -= doc->margin[LEFT] + doc->border[LEFT].width + - doc->padding[LEFT] + doc->padding[RIGHT] + - doc->border[RIGHT].width + doc->margin[RIGHT]; - if (width < 0) { - width = 0; - } - doc->width = width; + used_height = left->y + left->height - cy + 1; - ret = layout_block_context(doc, height, content); + NSLOG(layout, DEBUG, "used_height %i", + used_height); - /* make and fill available height */ - if (doc->y + doc->padding[TOP] + doc->height + doc->padding[BOTTOM] + - doc->border[BOTTOM].width + doc->margin[BOTTOM] < - height) { - doc->height = height - (doc->y + doc->padding[TOP] + - doc->padding[BOTTOM] + - doc->border[BOTTOM].width + - doc->margin[BOTTOM]); - if (doc->children) - doc->children->height = doc->height - - (doc->children->margin[TOP] + - doc->children->border[TOP].width + - doc->children->padding[TOP] + - doc->children->padding[BOTTOM] + - doc->children->border[BOTTOM].width + - doc->children->margin[BOTTOM]); - } + } + if (right && used_height < + right->y + right->height - cy + 1) + used_height = right->y + right->height - cy + 1; - layout_lists(doc, font_func); - layout_position_absolute(doc, doc, 0, 0, content); - layout_position_relative(doc, doc, 0, 0); + if (used_height < 0) + used_height = 0; - layout_calculate_descendant_bboxes(doc); + b = split_box; - return ret; -} + NSLOG(layout, DEBUG, "moving below float"); + } else if (split == 0 || x1 - x0 <= x + space_before + w) { + /* first word of box doesn't fit so leave box for next + * line */ + b = split_box; -/** - * Insert a float into a container. - * - * \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) -{ - struct box *box = cont->float_children; - int b_bottom = b->y + b->height; + NSLOG(layout, DEBUG, "leaving for next line"); - assert(b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT); + } else { + /* fit as many words as possible */ + assert(split != 0); - 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; - } - } -} + NSLOG(layout, DEBUG, "'%.*s' %i %zu %i", + (int)split_box->length, split_box->text, + x1 - x0, split, w); + if (split != split_box->length) { + if (!layout_text_box_split(content, &fstyle, + split_box, split, w)) + return false; + b = split_box->next; + } + x += space_before + w; -/** - * Split a text box. - * - * \param content memory pool for any new boxes - * \param fstyle style for text in text box - * \param split_box box with text to split - * \param new_length new length for text in split_box, after splitting - * \param new_width new width for text in split_box, after splitting - * \return true on success, false on memory exhaustion - * - * A new box is created and inserted into the box tree after split_box, - * containing the text after new_length excluding the initial space character. - */ -static bool -layout_text_box_split(html_content *content, - plot_font_style_t *fstyle, - struct box *split_box, - size_t new_length, - int new_width) -{ - int space_width = split_box->space; - struct box *c2; - const struct gui_layout_table *font_func = content->font_func; - bool space = (split_box->text[new_length] == ' '); - int used_length = new_length + (space ? 1 : 0); + NSLOG(layout, DEBUG, "fitting words"); - if ((space && space_width == 0) || space_width == UNKNOWN_WIDTH) { - /* We're need to add a space, and we don't know how big - * it's to be, OR we have a space of unknown width anyway; - * Calculate space width */ - font_func->width(fstyle, " ", 1, &space_width); + } + move_y = true; } - if (split_box->space == UNKNOWN_WIDTH) - split_box->space = space_width; - if (!space) - space_width = 0; - - /* Create clone of split_box, c2 */ - c2 = talloc_memdup(content->bctx, split_box, sizeof *c2); - if (!c2) - return false; - c2->flags |= CLONE; - - /* Set remaining text in c2 */ - c2->text += used_length; - - /* Set c2 according to the remaining text */ - c2->width -= new_width + space_width; - c2->flags &= ~MEASURED; /* width has been estimated */ - c2->length = split_box->length - used_length; - - /* Update split_box for its reduced text */ - split_box->width = new_width; - split_box->flags |= MEASURED; - split_box->length = new_length; - split_box->space = space_width; + /* set positions */ + switch (css_computed_text_align(first->parent->parent->style)) { + case CSS_TEXT_ALIGN_RIGHT: + case CSS_TEXT_ALIGN_LIBCSS_RIGHT: + x0 = x1 - x; + break; + case CSS_TEXT_ALIGN_CENTER: + case CSS_TEXT_ALIGN_LIBCSS_CENTER: + x0 = (x0 + (x1 - x)) / 2; + break; + case CSS_TEXT_ALIGN_LEFT: + case CSS_TEXT_ALIGN_LIBCSS_LEFT: + case CSS_TEXT_ALIGN_JUSTIFY: + /* leave on left */ + break; + case CSS_TEXT_ALIGN_DEFAULT: + /* None; consider text direction */ + switch (css_computed_direction(first->parent->parent->style)) { + case CSS_DIRECTION_LTR: + /* leave on left */ + break; + case CSS_DIRECTION_RTL: + x0 = x1 - x; + break; + } + break; + } - /* Insert c2 into box list */ - c2->next = split_box->next; - split_box->next = c2; - c2->prev = split_box; - if (c2->next) - c2->next->prev = c2; - else - c2->parent->last = c2; + for (d = first; d != b; d = d->next) { + d->flags &= ~NEW_LINE; - NSLOG(layout, DEBUG, - "split_box %p len: %" PRIsizet " \"%.*s\"", - split_box, - split_box->length, - (int)split_box->length, - split_box->text); - NSLOG(layout, DEBUG, - " new_box %p len: %" PRIsizet " \"%.*s\"", - c2, - c2->length, - (int)c2->length, - c2->text); + if (d->type == BOX_INLINE_BLOCK && + (css_computed_position(d->style) == + CSS_POSITION_ABSOLUTE || + css_computed_position(d->style) == + CSS_POSITION_FIXED)) { + /* positioned inline-blocks: + * set static position (x,y) only, rest of positioning + * is handled later */ + d->x += x0; + d->y = *y; + continue; + } else if ((d->type == BOX_INLINE && + ((d->object || d->gadget) == false) && + !(d->flags & IFRAME) && + !(d->flags & REPLACE_DIM)) || + d->type == BOX_BR || + d->type == BOX_TEXT || + d->type == BOX_INLINE_END) { + /* regular (non-replaced) inlines */ + d->x += x0; + d->y = *y - d->padding[TOP]; + + if (d->type == BOX_TEXT && d->height > used_height) { + /* text */ + used_height = d->height; + } + } else if ((d->type == BOX_INLINE) || + d->type == BOX_INLINE_BLOCK) { + /* replaced inlines and inline-blocks */ + d->x += x0; + d->y = *y + d->border[TOP].width + d->margin[TOP]; + h = d->margin[TOP] + d->border[TOP].width + + d->padding[TOP] + d->height + + d->padding[BOTTOM] + + d->border[BOTTOM].width + + d->margin[BOTTOM]; + if (used_height < h) + used_height = h; + } + } + + first->flags |= NEW_LINE; + + assert(b != first || (move_y && 0 < used_height && (left || right))); + + /* handle vertical-align by adjusting box y values */ + /** \todo proper vertical alignment handling */ + for (d = first; d != b; d = d->next) { + if ((d->type == BOX_INLINE && d->inline_end) || + d->type == BOX_BR || + d->type == BOX_TEXT || + d->type == BOX_INLINE_END) { + css_fixed value = 0; + css_unit unit = CSS_UNIT_PX; + switch (css_computed_vertical_align(d->style, &value, + &unit)) { + case CSS_VERTICAL_ALIGN_SUPER: + case CSS_VERTICAL_ALIGN_TOP: + case CSS_VERTICAL_ALIGN_TEXT_TOP: + /* already at top */ + break; + case CSS_VERTICAL_ALIGN_SUB: + case CSS_VERTICAL_ALIGN_BOTTOM: + case CSS_VERTICAL_ALIGN_TEXT_BOTTOM: + d->y += used_height - d->height; + break; + default: + case CSS_VERTICAL_ALIGN_BASELINE: + d->y += 0.75 * (used_height - d->height); + break; + } + } + } + + /* handle clearance for br */ + if (br_box && css_computed_clear(br_box->style) != CSS_CLEAR_NONE) { + int clear_y = layout_clear(cont->float_children, + css_computed_clear(br_box->style)); + if (used_height < clear_y - cy) + used_height = clear_y - cy; + } + if (move_y) + *y += used_height; + *next_box = b; + *width = x; /* return actual width */ return true; } /** - * Compute dimensions of box, margins, paddings, and borders for a floating - * element using shrink-to-fit. Also used for inline-blocks. + * Layout lines of text or inline boxes with floats. * - * \param available_width Max width available in pixels - * \param style Box's style - * \param box Box for which to find dimensions - * Box margins, borders, paddings, width and - * height are updated. + * \param box inline container box + * \param width horizontal space available + * \param cont ancestor box which defines horizontal space, for floats + * \param cx box position relative to cont + * \param cy box position relative to cont + * \param content memory pool for any new boxes + * \return true on success, false on memory exhaustion */ -static void -layout_float_find_dimensions(int available_width, - const css_computed_style *style, - struct box *box) +static bool layout_inline_container(struct box *inline_container, int width, + struct box *cont, int cx, int cy, html_content *content) { - int width, height, max_width, min_width, max_height, min_height; - int *margin = box->margin; - int *padding = box->padding; - struct box_border *border = box->border; - enum css_overflow_e overflow_x = css_computed_overflow_x(style); - enum css_overflow_e overflow_y = css_computed_overflow_y(style); - int scrollbar_width_x = - (overflow_x == CSS_OVERFLOW_SCROLL || - overflow_x == CSS_OVERFLOW_AUTO) ? - SCROLLBAR_WIDTH : 0; - int scrollbar_width_y = - (overflow_y == CSS_OVERFLOW_SCROLL || - overflow_y == CSS_OVERFLOW_AUTO) ? - SCROLLBAR_WIDTH : 0; + bool first_line = true; + bool has_text_children; + struct box *c, *next; + int y = 0; + int curwidth,maxwidth = width; - layout_find_dimensions(available_width, -1, box, style, &width, &height, - &max_width, &min_width, &max_height, &min_height, - margin, padding, border); + assert(inline_container->type == BOX_INLINE_CONTAINER); - if (margin[LEFT] == AUTO) - margin[LEFT] = 0; - if (margin[RIGHT] == AUTO) - margin[RIGHT] = 0; + NSLOG(layout, DEBUG, + "inline_container %p, width %i, cont %p, cx %i, cy %i", + inline_container, + width, + cont, + cx, + cy); - if (box->gadget == NULL) { - padding[RIGHT] += scrollbar_width_y; - padding[BOTTOM] += scrollbar_width_x; - } - if (box->object && !(box->flags & REPLACE_DIM) && - content_get_type(box->object) != CONTENT_HTML) { - /* Floating replaced element, with intrinsic width or height. - * See 10.3.6 and 10.6.2 */ - layout_get_object_dimensions(box, &width, &height, - min_width, max_width, min_height, max_height); - } else if (box->gadget && (box->gadget->type == GADGET_TEXTBOX || - box->gadget->type == GADGET_PASSWORD || - box->gadget->type == GADGET_FILE || - box->gadget->type == GADGET_TEXTAREA)) { - css_fixed size = 0; - css_unit unit = CSS_UNIT_EM; + has_text_children = false; + for (c = inline_container->children; c; c = c->next) { + bool is_pre = false; - /* Give sensible dimensions to gadgets, with auto width/height, - * that don't shrink to fit contained text. */ - assert(box->style); + if (c->style) { + enum css_white_space_e whitespace; - if (box->gadget->type == GADGET_TEXTBOX || - box->gadget->type == GADGET_PASSWORD || - box->gadget->type == GADGET_FILE) { - if (width == AUTO) { - size = INTTOFIX(10); - width = FIXTOINT(nscss_len2px(size, unit, - box->style)); - } - if (box->gadget->type == GADGET_FILE && - height == AUTO) { - size = FLTTOFIX(1.5); - height = FIXTOINT(nscss_len2px(size, unit, - box->style)); - } - } - if (box->gadget->type == GADGET_TEXTAREA) { - if (width == AUTO) { - size = INTTOFIX(10); - width = FIXTOINT(nscss_len2px(size, unit, - box->style)); - } - if (height == AUTO) { - size = INTTOFIX(4); - height = FIXTOINT(nscss_len2px(size, unit, - box->style)); - } + whitespace = css_computed_white_space(c->style); + + is_pre = (whitespace == CSS_WHITE_SPACE_PRE || + whitespace == CSS_WHITE_SPACE_PRE_LINE || + whitespace == CSS_WHITE_SPACE_PRE_WRAP); } - } else if (width == AUTO) { - /* CSS 2.1 section 10.3.5 */ - width = min(max(box->min_width, available_width), - box->max_width); - /* width includes margin, borders and padding */ - if (width == available_width) { - width -= box->margin[LEFT] + box->border[LEFT].width + - box->padding[LEFT] + - box->padding[RIGHT] + - box->border[RIGHT].width + - box->margin[RIGHT]; - } else { - /* width was obtained from a min_width or max_width - * value, so need to use the same method for calculating - * mbp as was used in layout_minmax_block() */ - int fixed = 0; - float frac = 0; - calculate_mbp_width(box->style, LEFT, true, true, true, - &fixed, &frac); - calculate_mbp_width(box->style, RIGHT, true, true, true, - &fixed, &frac); - if (fixed < 0) - fixed = 0; + if ((!c->object && !(c->flags & REPLACE_DIM) && + !(c->flags & IFRAME) && + c->text && (c->length || is_pre)) || + c->type == BOX_BR) + has_text_children = true; + } - width -= fixed; - } + /** \todo fix wrapping so that a box with horizontal scrollbar will + * shrink back to 'width' if no word is wider than 'width' (Or just set + * curwidth = width and have the multiword lines wrap to the min width) + */ + for (c = inline_container->children; c; ) { - if (max_width >= 0 && width > max_width) width = max_width; - if (min_width > 0 && width < min_width) width = min_width; + NSLOG(layout, DEBUG, "c %p", c); - } else { - if (max_width >= 0 && width > max_width) width = max_width; - if (min_width > 0 && width < min_width) width = min_width; - width -= scrollbar_width_y; + curwidth = inline_container->width; + if (!layout_line(c, &curwidth, &y, cx, cy + y, cont, first_line, + has_text_children, content, &next)) + return false; + maxwidth = max(maxwidth,curwidth); + c = next; + first_line = false; } - box->width = width; - box->height = height; + inline_container->width = maxwidth; + inline_container->height = y; - if (margin[TOP] == AUTO) - margin[TOP] = 0; - if (margin[BOTTOM] == AUTO) - margin[BOTTOM] = 0; + return true; } /** - * Layout the contents of a float or inline block. + * Layout a block formatting context. * - * \param b float or inline block box - * \param width available width - * \param content memory pool for any new boxes + * \param block BLOCK, INLINE_BLOCK, or TABLE_CELL to layout + * \param viewport_height Height of viewport in pixels or -ve if unknown + * \param content Memory pool for any new boxes * \return true on success, false on memory exhaustion - */ -static bool layout_float(struct box *b, int width, html_content *content) -{ - assert(b->type == BOX_TABLE || b->type == BOX_BLOCK || - b->type == BOX_INLINE_BLOCK); - layout_float_find_dimensions(width, b->style, b); - if (b->type == BOX_TABLE) { - if (!layout_table(b, width, content)) - return false; - if (b->margin[LEFT] == AUTO) - b->margin[LEFT] = 0; - if (b->margin[RIGHT] == AUTO) - b->margin[RIGHT] = 0; - if (b->margin[TOP] == AUTO) - b->margin[TOP] = 0; - if (b->margin[BOTTOM] == AUTO) - b->margin[BOTTOM] = 0; - } else - return layout_block_context(b, -1, content); - return true; -} - - -/** - * Position a float in the first available space. * - * \param c float box to position - * \param width available width - * \param cx x coordinate relative to cont to place float right of - * \param y y coordinate relative to cont to place float below - * \param cont ancestor box which defines horizontal space, for floats + * This function carries out layout of a block and its children, as described + * in CSS 2.1 9.4.1. */ -static void -place_float_below(struct box *c, int width, int cx, int y, struct box *cont) +static bool +layout_block_context(struct box *block, + int viewport_height, + html_content *content) { - int x0, x1, yy; - struct box *left; - struct box *right; - - yy = y > cont->cached_place_below_level ? - y : cont->cached_place_below_level; + struct box *box; + int cx, cy; /**< current coordinates */ + int max_pos_margin = 0; + int max_neg_margin = 0; + int y = 0; + int lm, rm; + struct box *margin_collapse = NULL; + bool in_margin = false; + css_fixed gadget_size; + css_unit gadget_unit; /* Checkbox / radio buttons */ - NSLOG(layout, DEBUG, - "c %p, width %i, cx %i, y %i, cont %p", c, - width, cx, y, cont); + assert(block->type == BOX_BLOCK || + block->type == BOX_INLINE_BLOCK || + block->type == BOX_TABLE_CELL); + assert(block->width != UNKNOWN_WIDTH); + assert(block->width != AUTO); - do { - y = yy; - x0 = cx; - x1 = cx + width; - find_sides(cont->float_children, y, y + c->height, &x0, &x1, - &left, &right); - if (left != 0 && right != 0) { - yy = (left->y + left->height < - right->y + right->height ? - left->y + left->height : - right->y + right->height); - } else if (left == 0 && right != 0) { - yy = right->y + right->height; - } else if (left != 0 && right == 0) { - yy = left->y + left->height; - } - } while ((left != 0 || right != 0) && (c->width > x1 - x0)); + block->float_children = NULL; + block->cached_place_below_level = 0; + block->clear_level = 0; - if (c->type == BOX_FLOAT_LEFT) { - c->x = x0; - } else { - c->x = x1 - c->width; + /* special case if the block contains an object */ + if (block->object) { + int temp_width = block->width; + if (!layout_block_object(block)) + return false; + layout_get_object_dimensions(block, &temp_width, + &block->height, INT_MIN, INT_MAX, + INT_MIN, INT_MAX); + return true; + } else if (block->flags & REPLACE_DIM) { + return true; } - c->y = y; - cont->cached_place_below_level = y; -} + /* special case if the block contains an radio button or checkbox */ + if (block->gadget && (block->gadget->type == GADGET_RADIO || + block->gadget->type == GADGET_CHECKBOX)) { + /* form checkbox or radio button + * if width or height is AUTO, set it to 1em */ + gadget_unit = CSS_UNIT_EM; + gadget_size = INTTOFIX(1); + if (block->height == AUTO) + block->height = FIXTOINT(nscss_len2px(gadget_size, + gadget_unit, block->style)); + } -/** - * Position a line of boxes in inline formatting context. - * - * \param first box at start of line - * \param width available width on input, updated with actual width on output - * (may be incorrect if the line gets split?) - * \param y coordinate of top of line, updated on exit to bottom - * \param cx coordinate of left of line relative to cont - * \param cy coordinate of top of line relative to cont - * \param cont ancestor box which defines horizontal space, for floats - * \param indent apply any first-line indent - * \param has_text_children at least one TEXT in the inline_container - * \param next_box updated to first box for next line, or 0 at end - * \param content memory pool for any new boxes - * \return true on success, false on memory exhaustion - */ -static bool -layout_line(struct box *first, - int *width, - int *y, - int cx, - int cy, - struct box *cont, - bool indent, - bool has_text_children, - html_content *content, - struct box **next_box) -{ - int height, used_height; - int x0 = 0; - int x1 = *width; - int x, h, x_previous; - int fy = cy; - struct box *left; - struct box *right; - struct box *b; - struct box *split_box = 0; - struct box *d; - struct box *br_box = 0; - bool move_y = false; - bool place_below = false; - int space_before = 0, space_after = 0; - unsigned int inline_count = 0; - unsigned int i; - const struct gui_layout_table *font_func = content->font_func; - plot_font_style_t fstyle; + box = block->children; + /* set current coordinates to top-left of the block */ + cx = 0; + y = cy = block->padding[TOP]; + if (box) + box->y = block->padding[TOP]; - NSLOG(layout, DEBUG, - "first %p, first->text '%.*s', width %i, y %i, cx %i, cy %i", - first, - (int)first->length, - first->text, - *width, - *y, - cx, - cy); + /* Step through the descendants of the block in depth-first order, but + * not into the children of boxes which aren't blocks. For example, if + * the tree passed to this function looks like this (box->type shown): + * + * block -> BOX_BLOCK + * BOX_BLOCK * (1) + * BOX_INLINE_CONTAINER * (2) + * BOX_INLINE + * BOX_TEXT + * ... + * BOX_BLOCK * (3) + * BOX_TABLE * (4) + * BOX_TABLE_ROW + * BOX_TABLE_CELL + * ... + * BOX_TABLE_CELL + * ... + * BOX_BLOCK * (5) + * BOX_INLINE_CONTAINER * (6) + * BOX_TEXT + * ... + * then the while loop will visit each box marked with *, setting box + * to each in the order shown. */ + while (box) { + enum css_overflow_e overflow_x = CSS_OVERFLOW_VISIBLE; + enum css_overflow_e overflow_y = CSS_OVERFLOW_VISIBLE; - /* find sides at top of line */ - x0 += cx; - x1 += cx; - find_sides(cont->float_children, cy, cy, &x0, &x1, &left, &right); - x0 -= cx; - x1 -= cx; + assert(box->type == BOX_BLOCK || box->type == BOX_TABLE || + box->type == BOX_INLINE_CONTAINER); - if (indent) - x0 += layout_text_indent(first->parent->parent->style, *width); + /* Tables are laid out before being positioned, because the + * position depends on the width which is calculated in + * table layout. Blocks and inline containers are positioned + * before being laid out, because width is not dependent on + * content, and the position is required during layout for + * correct handling of floats. + */ - if (x1 < x0) - x1 = x0; + if (box->style && + (css_computed_position(box->style) == + CSS_POSITION_ABSOLUTE || + css_computed_position(box->style) == + CSS_POSITION_FIXED)) { + box->x = box->parent->padding[LEFT]; + /* absolute positioned; this element will establish + * its own block context when it gets laid out later, + * so no need to look at its children now. */ + goto advance_to_next_box; + } - /* get minimum line height from containing block. - * this is the line-height if there are text children and also in the - * case of an initially empty text input */ - if (has_text_children || first->parent->parent->gadget) - used_height = height = - line_height(first->parent->parent->style); - else - /* inline containers with no text are usually for layout and - * look better with no minimum line-height */ - used_height = height = 0; + /* If we don't know which box the current margin collapses + * through to, find out. Update the pos/neg margin values. */ + if (margin_collapse == NULL) { + margin_collapse = layout_next_margin_block(box, block, + viewport_height, + &max_pos_margin, &max_neg_margin); + /* We have a margin that has not yet been applied. */ + in_margin = true; + } - /* pass 1: find height of line assuming sides at top of line: loop - * body executed at least once - * keep in sync with the loop in layout_minmax_line() */ + /* Clearance. */ + y = 0; + if (box->style && css_computed_clear(box->style) != + CSS_CLEAR_NONE) + y = layout_clear(block->float_children, + css_computed_clear(box->style)); - NSLOG(layout, DEBUG, "x0 %i, x1 %i, x1 - x0 %i", x0, x1, x1 - x0); + /* Find box's overflow properties */ + if (box->style) { + overflow_x = css_computed_overflow_x(box->style); + overflow_y = css_computed_overflow_y(box->style); + } + /* Blocks establishing a block formatting context get minimum + * left and right margins to avoid any floats. */ + lm = rm = 0; - for (x = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) { - int min_width, max_width, min_height, max_height; + if (box->type == BOX_BLOCK || box->flags & IFRAME) { + if (!box->object && !(box->flags & IFRAME) && + !(box->flags & REPLACE_DIM) && + box->style && + (overflow_x != CSS_OVERFLOW_VISIBLE || + overflow_y != CSS_OVERFLOW_VISIBLE)) { + /* box establishes new block formatting context + * so available width may be diminished due to + * floats. */ + int x0, x1, top; + struct box *left, *right; + top = cy + max_pos_margin - max_neg_margin; + top = (top > y) ? top : y; + x0 = cx; + x1 = cx + box->parent->width - + box->parent->padding[LEFT] - + box->parent->padding[RIGHT]; + find_sides(block->float_children, top, top, + &x0, &x1, &left, &right); + /* calculate min required left & right margins + * needed to avoid floats */ + lm = x0 - cx; + rm = cx + box->parent->width - + box->parent->padding[LEFT] - + box->parent->padding[RIGHT] - + x1; + } + layout_block_find_dimensions(box->parent->width, + viewport_height, lm, rm, box); + if (box->type == BOX_BLOCK && !(box->flags & IFRAME)) { + layout_block_add_scrollbar(box, RIGHT); + layout_block_add_scrollbar(box, BOTTOM); + } + } else if (box->type == BOX_TABLE) { + if (box->style != NULL) { + enum css_width_e wtype; + css_fixed width = 0; + css_unit unit = CSS_UNIT_PX; - assert(b->type == BOX_INLINE || b->type == BOX_INLINE_BLOCK || - b->type == BOX_FLOAT_LEFT || - b->type == BOX_FLOAT_RIGHT || - b->type == BOX_BR || b->type == BOX_TEXT || - b->type == BOX_INLINE_END); + wtype = css_computed_width(box->style, &width, + &unit); + + if (wtype == CSS_WIDTH_AUTO) { + /* max available width may be + * diminished due to floats. */ + int x0, x1, top; + struct box *left, *right; + top = cy + max_pos_margin - + max_neg_margin; + top = (top > y) ? top : y; + x0 = cx; + x1 = cx + box->parent->width - + box->parent->padding[LEFT] - + box->parent->padding[RIGHT]; + find_sides(block->float_children, + top, top, &x0, &x1, + &left, &right); + /* calculate min required left & right + * margins needed to avoid floats */ + lm = x0 - cx; + rm = cx + box->parent->width - + box->parent->padding[LEFT] - + box->parent->padding[RIGHT] - + x1; + } + } + if (!layout_table(box, box->parent->width - lm - rm, + content)) + return false; + layout_solve_width(box, box->parent->width, box->width, + lm, rm, -1, -1); + } + /* Position box: horizontal. */ + box->x = box->parent->padding[LEFT] + box->margin[LEFT] + + box->border[LEFT].width; + cx += box->x; - NSLOG(layout, DEBUG, "pass 1: b %p, x %i", b, x); + /* Position box: vertical. */ + if (box->border[TOP].width) { + box->y += box->border[TOP].width; + cy += box->border[TOP].width; + } + /* Vertical margin */ + if (((box->type == BOX_BLOCK && + (box->flags & HAS_HEIGHT)) || + box->type == BOX_TABLE || + (box->type == BOX_INLINE_CONTAINER && + box != box->parent->children) || + margin_collapse == box) && + in_margin == true) { + /* Margin goes above this box. */ + cy += max_pos_margin - max_neg_margin; + box->y += max_pos_margin - max_neg_margin; - if (b->type == BOX_BR) - break; + /* Current margin has been applied. */ + in_margin = false; + max_pos_margin = max_neg_margin = 0; + } - if (b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT) - continue; - if (b->type == BOX_INLINE_BLOCK && - (css_computed_position(b->style) == - CSS_POSITION_ABSOLUTE || - css_computed_position(b->style) == - CSS_POSITION_FIXED)) - continue; + /* Handle clearance */ + if (box->type != BOX_INLINE_CONTAINER && + (y > 0) && (cy < y)) { + /* box clears something*/ + box->y += y - cy; + cy = y; + } - assert(b->style != NULL); - font_plot_style_from_css(b->style, &fstyle); + /* Unless the box has an overflow style of visible, the box + * establishes a new block context. */ + if (box->type == BOX_BLOCK && box->style && + (overflow_x != CSS_OVERFLOW_VISIBLE || + overflow_y != CSS_OVERFLOW_VISIBLE)) { - x += space_after; + layout_block_context(box, viewport_height, content); - if (b->type == BOX_INLINE_BLOCK) { - if (b->max_width != UNKNOWN_WIDTH) - if (!layout_float(b, *width, content)) - return false; - h = b->border[TOP].width + b->padding[TOP] + b->height + - b->padding[BOTTOM] + - b->border[BOTTOM].width; - if (height < h) - height = h; - x += b->margin[LEFT] + b->border[LEFT].width + - b->padding[LEFT] + b->width + - b->padding[RIGHT] + - b->border[RIGHT].width + - b->margin[RIGHT]; - space_after = 0; - continue; - } + cy += box->padding[TOP]; - if (b->type == BOX_INLINE) { - /* calculate borders, margins, and padding */ - layout_find_dimensions(*width, -1, b, b->style, 0, 0, - 0, 0, 0, 0, b->margin, b->padding, - b->border); - for (i = 0; i != 4; i++) - if (b->margin[i] == AUTO) - b->margin[i] = 0; - x += b->margin[LEFT] + b->border[LEFT].width + - b->padding[LEFT]; - if (b->inline_end) { - b->inline_end->margin[RIGHT] = b->margin[RIGHT]; - b->inline_end->padding[RIGHT] = - b->padding[RIGHT]; - b->inline_end->border[RIGHT] = - b->border[RIGHT]; - } else { - x += b->padding[RIGHT] + - b->border[RIGHT].width + - b->margin[RIGHT]; - } - } else if (b->type == BOX_INLINE_END) { - b->width = 0; - if (b->space == UNKNOWN_WIDTH) { - font_func->width(&fstyle, " ", 1, &b->space); - /** \todo handle errors */ + if (box->height == AUTO) { + box->height = 0; + layout_block_add_scrollbar(box, BOTTOM); } - space_after = b->space; - x += b->padding[RIGHT] + b->border[RIGHT].width + - b->margin[RIGHT]; - continue; + cx -= box->x; + cy += box->height + box->padding[BOTTOM] + + box->border[BOTTOM].width; + y = box->y + box->padding[TOP] + box->height + + box->padding[BOTTOM] + + box->border[BOTTOM].width; + + /* Skip children, because they are done in the new + * block context */ + goto advance_to_next_box; } - if (!b->object && !(b->flags & IFRAME) && !b->gadget && - !(b->flags & REPLACE_DIM)) { - /* inline non-replaced, 10.3.1 and 10.6.1 */ - b->height = line_height(b->style ? b->style : - b->parent->parent->style); - if (height < b->height) - height = b->height; + NSLOG(layout, DEBUG, "box %p, cx %i, cy %i", box, cx, cy); - if (!b->text) { - b->width = 0; - space_after = 0; - continue; - } + /* Layout (except tables). */ + if (box->object) { + if (!layout_block_object(box)) + return false; - if (b->width == UNKNOWN_WIDTH) { - /** \todo handle errors */ + } else if (box->type == BOX_INLINE_CONTAINER) { + box->width = box->parent->width; + if (!layout_inline_container(box, box->width, block, + cx, cy, content)) + return false; - /* If it's a select element, we must use the - * width of the widest option text */ - if (b->parent->parent->gadget && - b->parent->parent->gadget->type - == GADGET_SELECT) { - int opt_maxwidth = 0; - struct form_option *o; + } else if (box->type == BOX_TABLE) { + /* Move down to avoid floats if necessary. */ + int x0, x1; + struct box *left, *right; + y = cy; + while (1) { + enum css_width_e wtype; + css_fixed width = 0; + css_unit unit = CSS_UNIT_PX; - for (o = b->parent->parent->gadget-> - data.select.items; o; - o = o->next) { - int opt_width; - font_func->width(&fstyle, - o->text, - strlen(o->text), - &opt_width); + wtype = css_computed_width(box->style, + &width, &unit); - if (opt_maxwidth < opt_width) - opt_maxwidth =opt_width; - } - b->width = opt_maxwidth; - if (nsoption_bool(core_select_menu)) - b->width += SCROLLBAR_WIDTH; - } else { - font_func->width(&fstyle, b->text, - b->length, &b->width); - b->flags |= MEASURED; - } + x0 = cx; + x1 = cx + box->parent->width; + find_sides(block->float_children, y, + y + box->height, + &x0, &x1, &left, &right); + if (wtype == CSS_WIDTH_AUTO) + break; + if (box->width <= x1 - x0) + break; + if (!left && !right) + break; + else if (!left) + y = right->y + right->height + 1; + else if (!right) + y = left->y + left->height + 1; + else if (left->y + left->height < + right->y + right->height) + y = left->y + left->height + 1; + else + y = right->y + right->height + 1; } + box->x += x0 - cx; + cx = x0; + box->y += y - cy; + cy = y; + } - /* If the current text has not been measured (i.e. its - * width was estimated after splitting), and it fits on - * the line, measure it properly, so next box is placed - * correctly. */ - if (b->text && (x + b->width < x1 - x0) && - !(b->flags & MEASURED) && - b->next) { - font_func->width(&fstyle, b->text, - b->length, &b->width); - b->flags |= MEASURED; - } + /* Advance to next box. */ + if (box->type == BOX_BLOCK && !box->object && !(box->iframe) && + box->children) { + /* Down into children. */ - x += b->width; - if (b->space == UNKNOWN_WIDTH) { - font_func->width(&fstyle, " ", 1, &b->space); - /** \todo handle errors */ + if (box == margin_collapse) { + /* Current margin collapsed though to this box. + * Unset margin_collapse. */ + margin_collapse = NULL; } - space_after = b->space; + + y = box->padding[TOP]; + box = box->children; + box->y = y; + cy += y; continue; + } else if (box->type == BOX_BLOCK || box->object || + box->flags & IFRAME) + cy += box->padding[TOP]; + + if (box->type == BOX_BLOCK && box->height == AUTO) { + box->height = 0; + layout_block_add_scrollbar(box, BOTTOM); } - space_after = 0; + cy += box->height + box->padding[BOTTOM] + + box->border[BOTTOM].width; + cx -= box->x; + y = box->y + box->padding[TOP] + box->height + + box->padding[BOTTOM] + + box->border[BOTTOM].width; - /* inline replaced, 10.3.2 and 10.6.2 */ - assert(b->style); + advance_to_next_box: + if (!box->next) { + /* No more siblings: + * up to first ancestor with a sibling. */ - layout_find_dimensions(*width, -1, b, b->style, - &b->width, &b->height, &max_width, &min_width, - &max_height, &min_height, NULL, NULL, NULL); + do { + if (box == margin_collapse) { + /* Current margin collapsed though to + * this box. Unset margin_collapse. */ + margin_collapse = NULL; + } - if (b->object && !(b->flags & REPLACE_DIM)) { - layout_get_object_dimensions(b, &b->width, &b->height, - min_width, max_width, - min_height, max_height); - } else if (b->flags & IFRAME) { - /* TODO: should we look at the content dimensions? */ - if (b->width == AUTO) - b->width = 400; - if (b->height == AUTO) - b->height = 300; + /* Apply bottom margin */ + if (max_pos_margin < box->margin[BOTTOM]) + max_pos_margin = box->margin[BOTTOM]; + else if (max_neg_margin < -box->margin[BOTTOM]) + max_neg_margin = -box->margin[BOTTOM]; - /* We reformat the iframe browser window to new - * dimensions in pass 2 */ - } else { - /* form control with no object */ - if (b->width == AUTO) - b->width = FIXTOINT(nscss_len2px(INTTOFIX(1), - CSS_UNIT_EM, b->style)); - if (b->height == AUTO) - b->height = FIXTOINT(nscss_len2px(INTTOFIX(1), - CSS_UNIT_EM, b->style)); - } + box = box->parent; + if (box == block) + break; - /* Reformat object to new box size */ - if (b->object && content_get_type(b->object) == CONTENT_HTML && - b->width != - content_get_available_width(b->object)) { - css_fixed value = 0; - css_unit unit = CSS_UNIT_PX; - enum css_height_e htype = css_computed_height(b->style, - &value, &unit); + /* Margin is invalidated if this is a box + * margins can't collapse through. */ + if (box->type == BOX_BLOCK && + box->flags & MAKE_HEIGHT) { + margin_collapse = NULL; + in_margin = false; + max_pos_margin = max_neg_margin = 0; + } - content_reformat(b->object, false, b->width, b->height); + if (box->height == AUTO) { + box->height = y - box->padding[TOP]; - if (htype == CSS_HEIGHT_AUTO) - b->height = content_get_height(b->object); - } + if (box->type == BOX_BLOCK) + layout_block_add_scrollbar(box, + BOTTOM); + } else + cy += box->height - + (y - box->padding[TOP]); - if (height < b->height) - height = b->height; + /* Apply any min-height and max-height to + * boxes in normal flow */ + if (box->style && + css_computed_position(box->style) != + CSS_POSITION_ABSOLUTE && + layout_apply_minmax_height(box, + NULL)) { + /* Height altered */ + /* Set current cy */ + cy += box->height - + (y - box->padding[TOP]); + } - x += b->width; - } + cy += box->padding[BOTTOM] + + box->border[BOTTOM].width; + cx -= box->x; + y = box->y + box->padding[TOP] + box->height + + box->padding[BOTTOM] + + box->border[BOTTOM].width; - /* find new sides using this height */ - x0 = cx; - x1 = cx + *width; - find_sides(cont->float_children, cy, cy + height, &x0, &x1, - &left, &right); - x0 -= cx; - x1 -= cx; + } while (box->next == NULL); + if (box == block) + break; + } - if (indent) - x0 += layout_text_indent(first->parent->parent->style, *width); + /* To next sibling. */ - if (x1 < x0) - x1 = x0; + if (box == margin_collapse) { + /* Current margin collapsed though to this box. + * Unset margin_collapse. */ + margin_collapse = NULL; + } - space_after = space_before = 0; + if (max_pos_margin < box->margin[BOTTOM]) + max_pos_margin = box->margin[BOTTOM]; + else if (max_neg_margin < -box->margin[BOTTOM]) + max_neg_margin = -box->margin[BOTTOM]; - /* pass 2: place boxes in line: loop body executed at least once */ + box = box->next; + box->y = y; + } - NSLOG(layout, DEBUG, "x0 %i, x1 %i, x1 - x0 %i", x0, x1, x1 - x0); + /* Account for bottom margin of last contained block */ + cy += max_pos_margin - max_neg_margin; - for (x = x_previous = 0, b = first; x <= x1 - x0 && b; b = b->next) { + /* Increase height to contain any floats inside (CSS 2.1 10.6.7). */ + for (box = block->float_children; box; box = box->next_float) { + y = box->y + box->height + box->padding[BOTTOM] + + box->border[BOTTOM].width + box->margin[BOTTOM]; + if (cy < y) + cy = y; + } - NSLOG(layout, DEBUG, "pass 2: b %p, x %i", b, x); + if (block->height == AUTO) { + block->height = cy - block->padding[TOP]; + if (block->type == BOX_BLOCK) + layout_block_add_scrollbar(block, BOTTOM); + } - if (b->type == BOX_INLINE_BLOCK && - (css_computed_position(b->style) == - CSS_POSITION_ABSOLUTE || - css_computed_position(b->style) == - CSS_POSITION_FIXED)) { - b->x = x + space_after; + if (block->style && css_computed_position(block->style) != + CSS_POSITION_ABSOLUTE) { + /* Block is in normal flow */ + layout_apply_minmax_height(block, NULL); + } - } else if (b->type == BOX_INLINE || - b->type == BOX_INLINE_BLOCK || - b->type == BOX_TEXT || - b->type == BOX_INLINE_END) { - assert(b->width != UNKNOWN_WIDTH); + if (block->gadget && + (block->gadget->type == GADGET_TEXTAREA || + block->gadget->type == GADGET_PASSWORD || + block->gadget->type == GADGET_TEXTBOX)) { + int ta_width = block->padding[LEFT] + block->width + + block->padding[RIGHT]; + int ta_height = block->padding[TOP] + block->height + + block->padding[BOTTOM]; + textarea_set_layout(block->gadget->data.text.ta, + ta_width, ta_height, + block->padding[TOP], block->padding[RIGHT], + block->padding[BOTTOM], block->padding[LEFT]); + } - x_previous = x; - x += space_after; - b->x = x; + return true; +} - if ((b->type == BOX_INLINE && !b->inline_end) || - b->type == BOX_INLINE_BLOCK) { - b->x += b->margin[LEFT] + b->border[LEFT].width; - x = b->x + b->padding[LEFT] + b->width + - b->padding[RIGHT] + - b->border[RIGHT].width + - b->margin[RIGHT]; - } else if (b->type == BOX_INLINE) { - b->x += b->margin[LEFT] + b->border[LEFT].width; - x = b->x + b->padding[LEFT] + b->width; - } else if (b->type == BOX_INLINE_END) { - b->height = b->inline_end->height; - x += b->padding[RIGHT] + - b->border[RIGHT].width + - b->margin[RIGHT]; - } else { - x += b->width; - } - space_before = space_after; - if (b->object || b->flags & REPLACE_DIM || - b->flags & IFRAME) - space_after = 0; - else if (b->text || b->type == BOX_INLINE_END) { - if (b->space == UNKNOWN_WIDTH) { - font_plot_style_from_css(b->style, +/** + * Layout list markers. + */ +static void +layout_lists(struct box *box, + const struct gui_layout_table *font_func) +{ + struct box *child; + struct box *marker; + plot_font_style_t fstyle; + + for (child = box->children; child; child = child->next) { + if (child->list_marker) { + marker = child->list_marker; + if (marker->object) { + marker->width = + content_get_width(marker->object); + marker->x = -marker->width; + marker->height = + content_get_height(marker->object); + marker->y = (line_height(marker->style) - + marker->height) / 2; + } else if (marker->text) { + if (marker->width == UNKNOWN_WIDTH) { + font_plot_style_from_css(marker->style, &fstyle); - /** \todo handle errors */ - font_func->width(&fstyle, " ", 1, - &b->space); + font_func->width(&fstyle, + marker->text, + marker->length, + &marker->width); + marker->flags |= MEASURED; } - space_after = b->space; + marker->x = -marker->width; + marker->y = 0; + marker->height = line_height(marker->style); } else { - space_after = 0; + marker->x = 0; + marker->y = 0; + marker->width = 0; + marker->height = 0; } - split_box = b; - move_y = true; - inline_count++; - } else if (b->type == BOX_BR) { - b->x = x; - b->width = 0; - br_box = b; - b = b->next; - split_box = 0; - move_y = true; - break; + /* Gap between marker and content */ + marker->x -= 4; + } + layout_lists(child, font_func); + } +} + + +/** + * Compute box offsets for a relatively or absolutely positioned box with + * respect to a box. + * + * \param box box to compute offsets for + * \param containing_block box to compute percentages with respect to + * \param top updated to top offset, or AUTO + * \param right updated to right offset, or AUTO + * \param bottom updated to bottom offset, or AUTO + * \param left updated to left offset, or AUTO + * + * See CSS 2.1 9.3.2. containing_block must have width and height. + */ +static void +layout_compute_offsets(struct box *box, + struct box *containing_block, + int *top, + int *right, + int *bottom, + int *left) +{ + uint32_t type; + css_fixed value = 0; + css_unit unit = CSS_UNIT_PX; + + assert(containing_block->width != UNKNOWN_WIDTH && + containing_block->width != AUTO && + containing_block->height != AUTO); + /* left */ + type = css_computed_left(box->style, &value, &unit); + if (type == CSS_LEFT_SET) { + if (unit == CSS_UNIT_PCT) { + *left = FPCT_OF_INT_TOINT(value, + containing_block->width); } else { - /* float */ - NSLOG(layout, DEBUG, "float %p", b); + *left = FIXTOINT(nscss_len2px(value, unit, box->style)); + } + } else { + *left = AUTO; + } - d = b->children; - d->float_children = 0; - d->cached_place_below_level = 0; - b->float_container = d->float_container = cont; + /* right */ + type = css_computed_right(box->style, &value, &unit); + if (type == CSS_RIGHT_SET) { + if (unit == CSS_UNIT_PCT) { + *right = FPCT_OF_INT_TOINT(value, + containing_block->width); + } else { + *right = FIXTOINT(nscss_len2px(value, unit, + box->style)); + } + } else { + *right = AUTO; + } - if (!layout_float(d, *width, content)) - return false; + /* top */ + type = css_computed_top(box->style, &value, &unit); + if (type == CSS_TOP_SET) { + if (unit == CSS_UNIT_PCT) { + *top = FPCT_OF_INT_TOINT(value, + containing_block->height); + } else { + *top = FIXTOINT(nscss_len2px(value, unit, box->style)); + } + } else { + *top = AUTO; + } - NSLOG(layout, DEBUG, - "%p : %d %d", - d, - d->margin[TOP], - d->border[TOP].width); + /* bottom */ + type = css_computed_bottom(box->style, &value, &unit); + if (type == CSS_BOTTOM_SET) { + if (unit == CSS_UNIT_PCT) { + *bottom = FPCT_OF_INT_TOINT(value, + containing_block->height); + } else { + *bottom = FIXTOINT(nscss_len2px(value, unit, + box->style)); + } + } else { + *bottom = AUTO; + } +} - d->x = d->margin[LEFT] + d->border[LEFT].width; - d->y = d->margin[TOP] + d->border[TOP].width; - b->width = d->margin[LEFT] + d->border[LEFT].width + - d->padding[LEFT] + d->width + - d->padding[RIGHT] + - d->border[RIGHT].width + - d->margin[RIGHT]; - b->height = d->margin[TOP] + d->border[TOP].width + - d->padding[TOP] + d->height + - d->padding[BOTTOM] + - d->border[BOTTOM].width + - d->margin[BOTTOM]; - if (b->width > (x1 - x0) - x) - place_below = true; - if (d->style && (css_computed_clear(d->style) == - CSS_CLEAR_NONE || - (css_computed_clear(d->style) == - CSS_CLEAR_LEFT && left == 0) || - (css_computed_clear(d->style) == - CSS_CLEAR_RIGHT && - right == 0) || - (css_computed_clear(d->style) == - CSS_CLEAR_BOTH && - left == 0 && right == 0)) && - (!place_below || - (left == 0 && right == 0 && x == 0)) && - cy >= cont->clear_level && - cy >= cont->cached_place_below_level) { - /* + not cleared or, - * cleared and there are no floats to clear - * + fits without needing to be placed below or, - * this line is empty with no floats - * + current y, cy, is below the clear level - * - * Float affects current line */ - if (b->type == BOX_FLOAT_LEFT) { - b->x = cx + x0; - if (b->width > 0) - x0 += b->width; - left = b; - } else { - b->x = cx + x1 - b->width; - if (b->width > 0) - x1 -= b->width; - right = b; - } - b->y = cy; - } else { - /* cleared or doesn't fit on line */ - /* place below into next available space */ - int fcy = (cy > cont->clear_level) ? cy : - cont->clear_level; - fcy = (fcy > cont->cached_place_below_level) ? - fcy : - cont->cached_place_below_level; - fy = (fy > fcy) ? fy : fcy; - fy = (fy == cy) ? fy + height : fy; +/** + * Layout and position an absolutely positioned box. + * + * \param box absolute box to layout and position + * \param containing_block containing block + * \param cx position of box relative to containing_block + * \param cy position of box relative to containing_block + * \param content memory pool for any new boxes + * \return true on success, false on memory exhaustion + */ +static bool +layout_absolute(struct box *box, + struct box *containing_block, + int cx, int cy, + html_content *content) +{ + int static_left, static_top; /* static position */ + int top, right, bottom, left; + int width, height, max_width, min_width; + int *margin = box->margin; + int *padding = box->padding; + struct box_border *border = box->border; + int available_width = containing_block->width; + int space; - place_float_below(b, *width, cx, fy, cont); - fy = b->y; - if (d->style && ( - (css_computed_clear(d->style) == - CSS_CLEAR_LEFT && left != 0) || - (css_computed_clear(d->style) == - CSS_CLEAR_RIGHT && - right != 0) || - (css_computed_clear(d->style) == - CSS_CLEAR_BOTH && - (left != 0 || right != 0)))) { - /* to be cleared below existing - * floats */ - if (b->type == BOX_FLOAT_LEFT) - b->x = cx; - else - b->x = cx + *width - b->width; + assert(box->type == BOX_BLOCK || box->type == BOX_TABLE || + box->type == BOX_INLINE_BLOCK); - fcy = layout_clear(cont->float_children, - css_computed_clear(d->style)); - if (fcy > cont->clear_level) - cont->clear_level = fcy; - if (b->y < fcy) - b->y = fcy; - } - if (b->type == BOX_FLOAT_LEFT) - left = b; - else - right = b; + /* The static position is where the box would be if it was not + * absolutely positioned. The x and y are filled in by + * layout_block_context(). */ + static_left = cx + box->x; + static_top = cy + box->y; + + if (containing_block->type == BOX_BLOCK || + containing_block->type == BOX_INLINE_BLOCK || + containing_block->type == BOX_TABLE_CELL) { + /* Block level container => temporarily increase containing + * block dimensions to include padding (we restore this + * again at the end) */ + containing_block->width += containing_block->padding[LEFT] + + containing_block->padding[RIGHT]; + containing_block->height += containing_block->padding[TOP] + + containing_block->padding[BOTTOM]; + } else { + /** \todo inline containers */ + } + + layout_compute_offsets(box, containing_block, + &top, &right, &bottom, &left); + + /* Pass containing block into layout_find_dimensions via the float + * containing block box member. This is unused for absolutely positioned + * boxes because a box can't be floated and absolutely positioned. */ + box->float_container = containing_block; + layout_find_dimensions(available_width, -1, box, box->style, + &width, &height, &max_width, &min_width, + 0, 0, margin, padding, border); + box->float_container = NULL; + + /* 10.3.7 */ + NSLOG(layout, DEBUG, + "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", + left, margin[LEFT], border[LEFT].width, padding[LEFT], width, + padding[RIGHT], border[RIGHT].width, margin[RIGHT], right, + containing_block->width); + + + if (left == AUTO && width == AUTO && right == AUTO) { + if (margin[LEFT] == AUTO) + margin[LEFT] = 0; + if (margin[RIGHT] == AUTO) + margin[RIGHT] = 0; + left = static_left; + + width = min(max(box->min_width, available_width), + box->max_width); + width -= box->margin[LEFT] + box->border[LEFT].width + + box->padding[LEFT] + box->padding[RIGHT] + + box->border[RIGHT].width + box->margin[RIGHT]; + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) width = max_width; + if (width < min_width) width = min_width; + + right = containing_block->width - + left - + margin[LEFT] - border[LEFT].width - padding[LEFT] - + width - + padding[RIGHT] - border[RIGHT].width - margin[RIGHT]; + } else if (left != AUTO && width != AUTO && right != AUTO) { + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) width = max_width; + if (min_width > 0 && width < min_width) width = min_width; + + if (margin[LEFT] == AUTO && margin[RIGHT] == AUTO) { + space = containing_block->width - + left - border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - right; + if (space < 0) { + margin[LEFT] = 0; + margin[RIGHT] = space; + } else { + margin[LEFT] = margin[RIGHT] = space / 2; } - add_float_to_container(cont, b); + } else if (margin[LEFT] == AUTO) { + margin[LEFT] = containing_block->width - + left - border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT] - + right; + } else if (margin[RIGHT] == AUTO) { + margin[RIGHT] = containing_block->width - + left - margin[LEFT] - + border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - right; + } else { + right = containing_block->width - + left - margin[LEFT] - + border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT]; + } + } else { + if (margin[LEFT] == AUTO) + margin[LEFT] = 0; + if (margin[RIGHT] == AUTO) + margin[RIGHT] = 0; - split_box = 0; + if (left == AUTO && width == AUTO && right != AUTO) { + available_width -= right; + + width = min(max(box->min_width, available_width), + box->max_width); + width -= box->margin[LEFT] + box->border[LEFT].width + + box->padding[LEFT] + box->padding[RIGHT] + + box->border[RIGHT].width + box->margin[RIGHT]; + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) + width = max_width; + if (width < min_width) + width = min_width; + + left = containing_block->width - + margin[LEFT] - border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT] - + right; + } else if (left == AUTO && width != AUTO && right == AUTO) { + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) + width = max_width; + if (min_width > 0 && width < min_width) + width = min_width; + + left = static_left; + right = containing_block->width - + left - margin[LEFT] - + border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT]; + } else if (left != AUTO && width == AUTO && right == AUTO) { + available_width -= left; + + width = min(max(box->min_width, available_width), + box->max_width); + width -= box->margin[LEFT] + box->border[LEFT].width + + box->padding[LEFT] + box->padding[RIGHT] + + box->border[RIGHT].width + box->margin[RIGHT]; + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) + width = max_width; + if (width < min_width) + width = min_width; + + right = containing_block->width - + left - margin[LEFT] - + border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT]; + } else if (left == AUTO && width != AUTO && right != AUTO) { + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) + width = max_width; + if (width < min_width) + width = min_width; + + left = containing_block->width - + margin[LEFT] - border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT] - + right; + } else if (left != AUTO && width == AUTO && right != AUTO) { + width = containing_block->width - + left - margin[LEFT] - + border[LEFT].width - + padding[LEFT] - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT] - + right; + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) + width = max_width; + if (width < min_width) + width = min_width; + + } else if (left != AUTO && width != AUTO && right == AUTO) { + + /* Adjust for {min|max}-width */ + if (max_width >= 0 && width > max_width) + width = max_width; + if (width < min_width) + width = min_width; + + right = containing_block->width - + left - margin[LEFT] - + border[LEFT].width - + padding[LEFT] - width - padding[RIGHT] - + border[RIGHT].width - margin[RIGHT]; } } - if (x1 - x0 < x && split_box) { - /* the last box went over the end */ - size_t split = 0; - int w; - bool no_wrap = css_computed_white_space( - split_box->style) == CSS_WHITE_SPACE_NOWRAP || - css_computed_white_space( - split_box->style) == CSS_WHITE_SPACE_PRE; + NSLOG(layout, DEBUG, + "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", + left, margin[LEFT], border[LEFT].width, padding[LEFT], width, + padding[RIGHT], border[RIGHT].width, margin[RIGHT], right, + containing_block->width); - x = x_previous; + box->x = left + margin[LEFT] + border[LEFT].width - cx; + if (containing_block->type == BOX_BLOCK || + containing_block->type == BOX_INLINE_BLOCK || + containing_block->type == BOX_TABLE_CELL) { + /* Block-level ancestor => reset container's width */ + containing_block->width -= containing_block->padding[LEFT] + + containing_block->padding[RIGHT]; + } else { + /** \todo inline ancestors */ + } + box->width = width; + box->height = height; - if (!no_wrap && - (split_box->type == BOX_INLINE || - split_box->type == BOX_TEXT) && - !split_box->object && - !(split_box->flags & REPLACE_DIM) && - !(split_box->flags & IFRAME) && - !split_box->gadget && split_box->text) { + if (box->type == BOX_BLOCK || box->type == BOX_INLINE_BLOCK || + box->object || box->flags & IFRAME) { + if (!layout_block_context(box, -1, content)) + return false; + } else if (box->type == BOX_TABLE) { + /* layout_table also expects the containing block to be + * stored in the float_container field */ + box->float_container = containing_block; + /* \todo layout_table considers margins etc. again */ + if (!layout_table(box, width, content)) + return false; + box->float_container = NULL; + layout_solve_width(box, box->parent->width, box->width, 0, 0, + -1, -1); + } - font_plot_style_from_css(split_box->style, &fstyle); - /** \todo handle errors */ - font_func->split(&fstyle, - split_box->text, - split_box->length, - x1 - x0 - x - space_before, - &split, - &w); + /* 10.6.4 */ + NSLOG(layout, DEBUG, + "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", + top, margin[TOP], border[TOP].width, padding[TOP], height, + padding[BOTTOM], border[BOTTOM].width, margin[BOTTOM], bottom, + containing_block->height); + + if (top == AUTO && height == AUTO && bottom == AUTO) { + top = static_top; + height = box->height; + if (margin[TOP] == AUTO) + margin[TOP] = 0; + if (margin[BOTTOM] == AUTO) + margin[BOTTOM] = 0; + bottom = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - height - padding[BOTTOM] - + border[BOTTOM].width - margin[BOTTOM]; + } else if (top != AUTO && height != AUTO && bottom != AUTO) { + if (margin[TOP] == AUTO && margin[BOTTOM] == AUTO) { + space = containing_block->height - + top - border[TOP].width - padding[TOP] - + height - padding[BOTTOM] - + border[BOTTOM].width - bottom; + margin[TOP] = margin[BOTTOM] = space / 2; + } else if (margin[TOP] == AUTO) { + margin[TOP] = containing_block->height - + top - border[TOP].width - padding[TOP] - + height - padding[BOTTOM] - + border[BOTTOM].width - margin[BOTTOM] - + bottom; + } else if (margin[BOTTOM] == AUTO) { + margin[BOTTOM] = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + bottom; + } else { + bottom = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + margin[BOTTOM]; + } + } else { + if (margin[TOP] == AUTO) + margin[TOP] = 0; + if (margin[BOTTOM] == AUTO) + margin[BOTTOM] = 0; + if (top == AUTO && height == AUTO && bottom != AUTO) { + height = box->height; + top = containing_block->height - + margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + margin[BOTTOM] - bottom; + } else if (top == AUTO && height != AUTO && bottom == AUTO) { + top = static_top; + bottom = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + margin[BOTTOM]; + } else if (top != AUTO && height == AUTO && bottom == AUTO) { + height = box->height; + bottom = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + margin[BOTTOM]; + } else if (top == AUTO && height != AUTO && bottom != AUTO) { + top = containing_block->height - + margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + margin[BOTTOM] - bottom; + } else if (top != AUTO && height == AUTO && bottom != AUTO) { + height = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - padding[BOTTOM] - + border[BOTTOM].width - margin[BOTTOM] - + bottom; + } else if (top != AUTO && height != AUTO && bottom == AUTO) { + bottom = containing_block->height - + top - margin[TOP] - border[TOP].width - + padding[TOP] - height - + padding[BOTTOM] - border[BOTTOM].width - + margin[BOTTOM]; } + } - /* split == 0 implies that text can't be split */ - - if (split == 0) - w = split_box->width; - - - NSLOG(layout, DEBUG, - "splitting: split_box %p \"%.*s\", spilt %zu, w %i, " - "left %p, right %p, inline_count %u", - split_box, - (int)split_box->length, - split_box->text, - split, - w, - left, - right, - inline_count); - - if ((split == 0 || x1 - x0 <= x + space_before + w) && - !left && !right && inline_count == 1) { - /* first word of box doesn't fit, but no floats and - * first box on line so force in */ - if (split == 0 || split == split_box->length) { - /* only one word in this box, or not text - * or white-space:nowrap */ - b = split_box->next; - } else { - /* cut off first word for this line */ - if (!layout_text_box_split(content, &fstyle, - split_box, split, w)) - return false; - b = split_box->next; - } - x += space_before + w; - - NSLOG(layout, DEBUG, "forcing"); - - } else if ((split == 0 || x1 - x0 <= x + space_before + w) && - inline_count == 1) { - /* first word of first box doesn't fit, but a float is - * taking some of the width so move below it */ - assert(left || right); - used_height = 0; - if (left) { - - NSLOG(layout, DEBUG, - "cy %i, left->y %i, left->height %i", - cy, - left->y, - left->height); - - used_height = left->y + left->height - cy + 1; - - NSLOG(layout, DEBUG, "used_height %i", - used_height); - - } - if (right && used_height < - right->y + right->height - cy + 1) - used_height = right->y + right->height - cy + 1; - - if (used_height < 0) - used_height = 0; + NSLOG(layout, DEBUG, + "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i", + top, margin[TOP], border[TOP].width, padding[TOP], height, + padding[BOTTOM], border[BOTTOM].width, margin[BOTTOM], bottom, + containing_block->height); - b = split_box; + box->y = top + margin[TOP] + border[TOP].width - cy; + if (containing_block->type == BOX_BLOCK || + containing_block->type == BOX_INLINE_BLOCK || + containing_block->type == BOX_TABLE_CELL) { + /* Block-level ancestor => reset container's height */ + containing_block->height -= containing_block->padding[TOP] + + containing_block->padding[BOTTOM]; + } else { + /** \todo Inline ancestors */ + } + box->height = height; + layout_apply_minmax_height(box, containing_block); - NSLOG(layout, DEBUG, "moving below float"); + return true; +} - } else if (split == 0 || x1 - x0 <= x + space_before + w) { - /* first word of box doesn't fit so leave box for next - * line */ - b = split_box; - NSLOG(layout, DEBUG, "leaving for next line"); +/** + * Recursively layout and position absolutely positioned boxes. + * + * \param box tree of boxes to layout + * \param containing_block current containing block + * \param cx position of box relative to containing_block + * \param cy position of box relative to containing_block + * \param content memory pool for any new boxes + * \return true on success, false on memory exhaustion + */ +static bool +layout_position_absolute(struct box *box, + struct box *containing_block, + int cx, int cy, + html_content *content) +{ + struct box *c; + for (c = box->children; c; c = c->next) { + if ((c->type == BOX_BLOCK || c->type == BOX_TABLE || + c->type == BOX_INLINE_BLOCK) && + (css_computed_position(c->style) == + CSS_POSITION_ABSOLUTE || + css_computed_position(c->style) == + CSS_POSITION_FIXED)) { + if (!layout_absolute(c, containing_block, + cx, cy, content)) + return false; + if (!layout_position_absolute(c, c, 0, 0, content)) + return false; + } else if (c->style && css_computed_position(c->style) == + CSS_POSITION_RELATIVE) { + if (!layout_position_absolute(c, c, 0, 0, content)) + return false; } else { - /* fit as many words as possible */ - assert(split != 0); - - NSLOG(layout, DEBUG, "'%.*s' %i %zu %i", - (int)split_box->length, split_box->text, - x1 - x0, split, w); - - if (split != split_box->length) { - if (!layout_text_box_split(content, &fstyle, - split_box, split, w)) - return false; - b = split_box->next; + int px, py; + if (c->style && (css_computed_float(c->style) == + CSS_FLOAT_LEFT || + css_computed_float(c->style) == + CSS_FLOAT_RIGHT)) { + /* Float x/y coords are relative to nearest + * ansestor with float_children, rather than + * relative to parent. Need to get x/y relative + * to parent */ + struct box *p; + px = c->x; + py = c->y; + for (p = box->parent; p && !p->float_children; + p = p->parent) { + px -= p->x; + py -= p->y; + } + } else { + /* Not a float, so box x/y coords are relative + * to parent */ + px = c->x; + py = c->y; } - x += space_before + w; - - NSLOG(layout, DEBUG, "fitting words"); - + if (!layout_position_absolute(c, containing_block, + cx + px, cy + py, content)) + return false; } - move_y = true; } - /* set positions */ - switch (css_computed_text_align(first->parent->parent->style)) { - case CSS_TEXT_ALIGN_RIGHT: - case CSS_TEXT_ALIGN_LIBCSS_RIGHT: - x0 = x1 - x; - break; - case CSS_TEXT_ALIGN_CENTER: - case CSS_TEXT_ALIGN_LIBCSS_CENTER: - x0 = (x0 + (x1 - x)) / 2; - break; - case CSS_TEXT_ALIGN_LEFT: - case CSS_TEXT_ALIGN_LIBCSS_LEFT: - case CSS_TEXT_ALIGN_JUSTIFY: - /* leave on left */ - break; - case CSS_TEXT_ALIGN_DEFAULT: - /* None; consider text direction */ - switch (css_computed_direction(first->parent->parent->style)) { - case CSS_DIRECTION_LTR: - /* leave on left */ - break; - case CSS_DIRECTION_RTL: - x0 = x1 - x; - break; - } - break; - } + return true; +} - for (d = first; d != b; d = d->next) { - d->flags &= ~NEW_LINE; - if (d->type == BOX_INLINE_BLOCK && - (css_computed_position(d->style) == - CSS_POSITION_ABSOLUTE || - css_computed_position(d->style) == - CSS_POSITION_FIXED)) { - /* positioned inline-blocks: - * set static position (x,y) only, rest of positioning - * is handled later */ - d->x += x0; - d->y = *y; - continue; - } else if ((d->type == BOX_INLINE && - ((d->object || d->gadget) == false) && - !(d->flags & IFRAME) && - !(d->flags & REPLACE_DIM)) || - d->type == BOX_BR || - d->type == BOX_TEXT || - d->type == BOX_INLINE_END) { - /* regular (non-replaced) inlines */ - d->x += x0; - d->y = *y - d->padding[TOP]; +/** + * Compute a box's relative offset as per CSS 2.1 9.4.3 + * + * \param box Box to compute relative offsets for. + * \param x Receives relative offset in x. + * \param y Receives relative offset in y. + */ +static void layout_compute_relative_offset(struct box *box, int *x, int *y) +{ + int left, right, top, bottom; + struct box *containing_block; - if (d->type == BOX_TEXT && d->height > used_height) { - /* text */ - used_height = d->height; - } - } else if ((d->type == BOX_INLINE) || - d->type == BOX_INLINE_BLOCK) { - /* replaced inlines and inline-blocks */ - d->x += x0; - d->y = *y + d->border[TOP].width + d->margin[TOP]; - h = d->margin[TOP] + d->border[TOP].width + - d->padding[TOP] + d->height + - d->padding[BOTTOM] + - d->border[BOTTOM].width + - d->margin[BOTTOM]; - if (used_height < h) - used_height = h; - } + assert(box && box->parent && box->style && + css_computed_position(box->style) == + CSS_POSITION_RELATIVE); + + if (box->float_container && + (css_computed_float(box->style) == CSS_FLOAT_LEFT || + css_computed_float(box->style) == CSS_FLOAT_RIGHT)) { + containing_block = box->float_container; + } else { + containing_block = box->parent; } - first->flags |= NEW_LINE; - - assert(b != first || (move_y && 0 < used_height && (left || right))); + layout_compute_offsets(box, containing_block, + &top, &right, &bottom, &left); - /* handle vertical-align by adjusting box y values */ - /** \todo proper vertical alignment handling */ - for (d = first; d != b; d = d->next) { - if ((d->type == BOX_INLINE && d->inline_end) || - d->type == BOX_BR || - d->type == BOX_TEXT || - d->type == BOX_INLINE_END) { - css_fixed value = 0; - css_unit unit = CSS_UNIT_PX; - switch (css_computed_vertical_align(d->style, &value, - &unit)) { - case CSS_VERTICAL_ALIGN_SUPER: - case CSS_VERTICAL_ALIGN_TOP: - case CSS_VERTICAL_ALIGN_TEXT_TOP: - /* already at top */ - break; - case CSS_VERTICAL_ALIGN_SUB: - case CSS_VERTICAL_ALIGN_BOTTOM: - case CSS_VERTICAL_ALIGN_TEXT_BOTTOM: - d->y += used_height - d->height; - break; - default: - case CSS_VERTICAL_ALIGN_BASELINE: - d->y += 0.75 * (used_height - d->height); - break; - } + if (left == AUTO && right == AUTO) + left = right = 0; + else if (left == AUTO) + /* left is auto => computed = -right */ + left = -right; + else if (right == AUTO) + /* right is auto => computed = -left */ + right = -left; + else { + /* over constrained => examine direction property + * of containing block */ + if (containing_block->style && + css_computed_direction( + containing_block->style) == + CSS_DIRECTION_RTL) { + /* right wins */ + left = -right; + } else { + /* assume LTR in all other cases */ + right = -left; } } - /* handle clearance for br */ - if (br_box && css_computed_clear(br_box->style) != CSS_CLEAR_NONE) { - int clear_y = layout_clear(cont->float_children, - css_computed_clear(br_box->style)); - if (used_height < clear_y - cy) - used_height = clear_y - cy; + assert(left == -right); + + if (top == AUTO && bottom == AUTO) { + top = bottom = 0; + } else if (top == AUTO) { + top = -bottom; + } else { + /* bottom is AUTO, or neither are AUTO */ + bottom = -top; } - if (move_y) - *y += used_height; - *next_box = b; - *width = x; /* return actual width */ - return true; + NSLOG(layout, DEBUG, "left %i, right %i, top %i, bottom %i", left, + right, top, bottom); + + *x = left; + *y = top; } -/* exported function documented in render/layout.h */ -bool layout_inline_container(struct box *inline_container, int width, - struct box *cont, int cx, int cy, html_content *content) +/** + * Adjust positions of relatively positioned boxes. + * + * \param root box to adjust the position of + * \param fp box which forms the block formatting context for children of + * "root" which are floats + * \param fx x offset due to intervening relatively positioned boxes + * between current box, "root", and the block formatting context + * box, "fp", for float children of "root" + * \param fy y offset due to intervening relatively positioned boxes + * between current box, "root", and the block formatting context + * box, "fp", for float children of "root" + */ +static void +layout_position_relative(struct box *root, struct box *fp, int fx, int fy) { - bool first_line = true; - bool has_text_children; - struct box *c, *next; - int y = 0; - int curwidth,maxwidth = width; + struct box *box; /* for children of "root" */ + struct box *fn; /* for block formatting context box for children of + * "box" */ + struct box *fc; /* for float children of the block formatting context, + * "fp" */ + int x, y; /* for the offsets resulting from any relative + * positioning on the current block */ + int fnx, fny; /* for affsets which apply to flat children of "box" */ - assert(inline_container->type == BOX_INLINE_CONTAINER); + /**\todo ensure containing box is large enough after moving boxes */ - NSLOG(layout, DEBUG, - "inline_container %p, width %i, cont %p, cx %i, cy %i", - inline_container, - width, - cont, - cx, - cy); + assert(root); + /* Normal children */ + for (box = root->children; box; box = box->next) { - has_text_children = false; - for (c = inline_container->children; c; c = c->next) { - bool is_pre = false; + if (box->type == BOX_TEXT) + continue; - if (c->style) { - enum css_white_space_e whitespace; + /* If relatively positioned, get offsets */ + if (box->style && css_computed_position(box->style) == + CSS_POSITION_RELATIVE) + layout_compute_relative_offset(box, &x, &y); + else + x = y = 0; - whitespace = css_computed_white_space(c->style); + /* Adjust float coordinates. + * (note float x and y are relative to their block formatting + * context box and not their parent) */ + if (box->style && (css_computed_float(box->style) == + CSS_FLOAT_LEFT || + css_computed_float(box->style) == + CSS_FLOAT_RIGHT) && + (fx != 0 || fy != 0)) { + /* box is a float and there is a float offset to + * apply */ + for (fc = fp->float_children; fc; fc = fc->next_float) { + if (box == fc->children) { + /* Box is floated in the block + * formatting context block, fp. + * Apply float offsets. */ + box->x += fx; + box->y += fy; + fx = fy = 0; + } + } + } - is_pre = (whitespace == CSS_WHITE_SPACE_PRE || - whitespace == CSS_WHITE_SPACE_PRE_LINE || - whitespace == CSS_WHITE_SPACE_PRE_WRAP); + if (box->float_children) { + fn = box; + fnx = fny = 0; + } else { + fn = fp; + fnx = fx + x; + fny = fy + y; } - if ((!c->object && !(c->flags & REPLACE_DIM) && - !(c->flags & IFRAME) && - c->text && (c->length || is_pre)) || - c->type == BOX_BR) - has_text_children = true; + /* recurse first */ + layout_position_relative(box, fn, fnx, fny); + + /* Ignore things we're not interested in. */ + if (!box->style || (box->style && + css_computed_position(box->style) != + CSS_POSITION_RELATIVE)) + continue; + + box->x += x; + box->y += y; + + /* Handle INLINEs - their "children" are in fact + * the sibling boxes between the INLINE and + * INLINE_END boxes */ + if (box->type == BOX_INLINE && box->inline_end) { + struct box *b; + for (b = box->next; b && b != box->inline_end; + b = b->next) { + b->x += x; + b->y += y; + } + } } +} - /** \todo fix wrapping so that a box with horizontal scrollbar will - * shrink back to 'width' if no word is wider than 'width' (Or just set - * curwidth = width and have the multiword lines wrap to the min width) - */ - for (c = inline_container->children; c; ) { - NSLOG(layout, DEBUG, "c %p", c); +/* exported function documented in render/layout.h */ +bool layout_document(html_content *content, int width, int height) +{ + bool ret; + struct box *doc = content->layout; + const struct gui_layout_table *font_func = content->font_func; - curwidth = inline_container->width; - if (!layout_line(c, &curwidth, &y, cx, cy + y, cont, first_line, - has_text_children, content, &next)) - return false; - maxwidth = max(maxwidth,curwidth); - c = next; - first_line = false; + layout_minmax_block(doc, font_func); + + layout_block_find_dimensions(width, height, 0, 0, doc); + doc->x = doc->margin[LEFT] + doc->border[LEFT].width; + doc->y = doc->margin[TOP] + doc->border[TOP].width; + width -= doc->margin[LEFT] + doc->border[LEFT].width + + doc->padding[LEFT] + doc->padding[RIGHT] + + doc->border[RIGHT].width + doc->margin[RIGHT]; + if (width < 0) { + width = 0; } + doc->width = width; - inline_container->width = maxwidth; - inline_container->height = y; + ret = layout_block_context(doc, height, content); - return true; + /* make and fill available height */ + if (doc->y + doc->padding[TOP] + doc->height + doc->padding[BOTTOM] + + doc->border[BOTTOM].width + doc->margin[BOTTOM] < + height) { + doc->height = height - (doc->y + doc->padding[TOP] + + doc->padding[BOTTOM] + + doc->border[BOTTOM].width + + doc->margin[BOTTOM]); + if (doc->children) + doc->children->height = doc->height - + (doc->children->margin[TOP] + + doc->children->border[TOP].width + + doc->children->padding[TOP] + + doc->children->padding[BOTTOM] + + doc->children->border[BOTTOM].width + + doc->children->margin[BOTTOM]); + } + + layout_lists(doc, font_func); + layout_position_absolute(doc, doc, 0, 0, content); + layout_position_relative(doc, doc, 0, 0); + + layout_calculate_descendant_bboxes(doc); + + return ret; } diff --git a/render/layout.h b/render/layout.h index 7a579ca22..0aa0da3e9 100644 --- a/render/layout.h +++ b/render/layout.h @@ -42,19 +42,6 @@ struct gui_layout_table; */ bool layout_document(struct html_content *content, int width, int height); -/** - * Layout lines of text or inline boxes with floats. - * - * \param box inline container box - * \param width horizontal space available - * \param cont ancestor box which defines horizontal space, for floats - * \param cx box position relative to cont - * \param cy box position relative to cont - * \param content memory pool for any new boxes - * \return true on success, false on memory exhaustion - */ -bool layout_inline_container(struct box *box, int width, struct box *cont, int cx, int cy, struct html_content *content); - /** * Recursively calculate the descendant_[xy][01] values for a laid-out box tree * and inform iframe browser windows of their size and position. -- cgit v1.2.3