You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
netsurf/content/handlers/html/layout/document.c

2918 lines
80 KiB

/*
* Copyright 2005 Richard Wilson <info@tinct.net>
* Copyright 2006 James Bursa <bursa@users.sourceforge.net>
* Copyright 2008 Michael Drake <tlsa@netsurf-browser.org>
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* NetSurf is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* \file
* HTML layout implementation.
*
* Layout is carried out in two stages:
*
* 1. + calculation of minimum / maximum box widths, and
* + determination of whether block level boxes will have >zero height
*
* 2. + layout (position and dimensions)
*
* In most cases the functions for the two stages are a corresponding pair
* layout_minmax_X() and layout_X().
*/
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <dom/dom.h>
#include "utils/log.h"
#include "utils/talloc.h"
#include "utils/utils.h"
#include "utils/nsoption.h"
#include "utils/corestrings.h"
#include "utils/nsurl.h"
#include "netsurf/inttypes.h"
#include "netsurf/content.h"
#include "netsurf/browser_window.h"
#include "netsurf/layout.h"
#include "content/content.h"
#include "content/content_protected.h"
#include "css/utils.h"
#include "desktop/scrollbar.h"
#include "desktop/textarea.h"
#include "html/html.h"
#include "html/html_save.h"
#include "html/private.h"
#include "html/box.h"
#include "html/box_inspect.h"
#include "html/font.h"
#include "html/form_internal.h"
#include "html/table.h"
#include "html/layout.h"
#include "html/layout/internal.h"
#include "html/layout/block.h"
#include "html/layout/flex.h"
#include "html/layout/table.h"
#include "html/layout/line.h"
/** Array of per-side access functions for computed style margins. */
const css_len_func margin_funcs[4] = {
[TOP] = css_computed_margin_top,
[RIGHT] = css_computed_margin_right,
[BOTTOM] = css_computed_margin_bottom,
[LEFT] = css_computed_margin_left,
};
/** Array of per-side access functions for computed style paddings. */
const css_len_func padding_funcs[4] = {
[TOP] = css_computed_padding_top,
[RIGHT] = css_computed_padding_right,
[BOTTOM] = css_computed_padding_bottom,
[LEFT] = css_computed_padding_left,
};
/** Array of per-side access functions for computed style border_widths. */
const css_len_func border_width_funcs[4] = {
[TOP] = css_computed_border_top_width,
[RIGHT] = css_computed_border_right_width,
[BOTTOM] = css_computed_border_bottom_width,
[LEFT] = css_computed_border_left_width,
};
/** Array of per-side access functions for computed style border styles. */
const css_border_style_func border_style_funcs[4] = {
[TOP] = css_computed_border_top_style,
[RIGHT] = css_computed_border_right_style,
[BOTTOM] = css_computed_border_bottom_style,
[LEFT] = css_computed_border_left_style,
};
/** Array of per-side access functions for computed style border colors. */
const css_border_color_func border_color_funcs[4] = {
[TOP] = css_computed_border_top_color,
[RIGHT] = css_computed_border_right_color,
[BOTTOM] = css_computed_border_bottom_color,
[LEFT] = css_computed_border_left_color,
};
/* forward declaration to break cycles */
static void layout_minmax_block(
struct box *block,
const struct gui_layout_table *font_func,
const html_content *content);
/**
* Compute the size of replaced boxes with auto dimensions, according to
* content.
*
* \param box Box with object
* \param width Width value in px or AUTO. If AUTO, updated to value in px.
* \param height Height value in px or AUTO. If AUTO, updated to value in px.
* \param min_width Box's min width, as given by layout_find_dimensions.
* \param max_width Box's max width, as given by layout_find_dimensions.
* \param min_height Box's min height, as given by layout_find_dimensions.
* \param max_height Box's max height, as given by layout_find_dimensions.
*
* See CSS 2.1 sections 10.3 and 10.6.
*/
void
layout_get_object_dimensions(struct box *box,
int *width, int *height,
int min_width, int max_width,
int min_height, int max_height)
{
assert(box->object != NULL);
assert(width != NULL && height != NULL);
if (*width == AUTO && *height == AUTO) {
/* No given dimensions */
bool scaled = false;
int intrinsic_width = content_get_width(box->object);
int intrinsic_height = content_get_height(box->object);
/* use intrinsic dimensions */
*width = intrinsic_width;
*height = intrinsic_height;
/* Deal with min/max-width first */
if (min_width > 0 && min_width > *width) {
*width = min_width;
scaled = true;
}
if (max_width >= 0 && max_width < *width) {
*width = max_width;
scaled = true;
}
if (scaled && (intrinsic_width != 0)) {
/* Update height */
*height = (*width * intrinsic_height) /
intrinsic_width;
}
scaled = false;
/* Deal with min/max-height */
if (min_height > 0 && min_height > *height) {
*height = min_height;
scaled = true;
}
if (max_height >= 0 && max_height < *height) {
*height = max_height;
scaled = true;
}
if (scaled && (intrinsic_height != 0)) {
/* Update width */
*width = (*height * intrinsic_width) /
intrinsic_height;
}
} else if (*width == AUTO) {
/* Have given height; width is calculated from the given height
* and ratio of intrinsic dimensions */
int intrinsic_width = content_get_width(box->object);
int intrinsic_height = content_get_height(box->object);
if (intrinsic_height != 0)
*width = (*height * intrinsic_width) /
intrinsic_height;
else
*width = intrinsic_width;
if (min_width > 0 && min_width > *width)
*width = min_width;
if (max_width >= 0 && max_width < *width)
*width = max_width;
} else if (*height == AUTO) {
/* Have given width; height is calculated from the given width
* and ratio of intrinsic dimensions */
int intrinsic_width = content_get_width(box->object);
int intrinsic_height = content_get_height(box->object);
if (min_width > 0 && min_width > *width)
*width = min_width;
if (max_width >= 0 && max_width < *width)
*width = max_width;
if (intrinsic_width != 0)
*height = (*width * intrinsic_height) /
intrinsic_width;
else
*height = intrinsic_height;
}
}
/*
* Calculate the text-indent length.
*
* \param style style of block
* \param width width of containing block
* \return length of indent
*/
int layout_text_indent(
const css_unit_ctx *unit_len_ctx,
const css_computed_style *style, int width)
{
css_fixed value = 0;
css_unit unit = CSS_UNIT_PX;
css_computed_text_indent(style, &value, &unit);
if (unit == CSS_UNIT_PCT) {
return FPCT_OF_INT_TOINT(value, width);
} else {
return FIXTOINT(css_unit_len2device_px(style, unit_len_ctx,
value, unit));
}
}
/**
* Calculate minimum and maximum width of a table.
*
* \param table box of type TABLE
* \param font_func Font functions
* \param content The HTML content we are laying out.
* \post table->min_width and table->max_width filled in,
* 0 <= table->min_width <= table->max_width
*/
static void layout_minmax_table(struct box *table,
const struct gui_layout_table *font_func,
const html_content *content)
{
unsigned int i, j;
int border_spacing_h = 0;
int table_min = 0, table_max = 0;
int extra_fixed = 0;
float extra_frac = 0;
struct column *col;
struct box *row_group, *row, *cell;
enum css_width_e wtype;
css_fixed value = 0;
css_unit unit = CSS_UNIT_PX;
/* check if the widths have already been calculated */
if (table->max_width != UNKNOWN_MAX_WIDTH)
return;
if (table_calculate_column_types(&content->unit_len_ctx, table) == false) {
NSLOG(netsurf, ERROR,
"Could not establish table column types.");
return;
}
col = table->col;
/* start with 0 except for fixed-width columns */
for (i = 0; i != table->columns; i++) {
if (col[i].type == COLUMN_WIDTH_FIXED)
col[i].min = col[i].max = col[i].width;
else
col[i].min = col[i].max = 0;
}
/* border-spacing is used in the separated borders model */
if (css_computed_border_collapse(table->style) ==
CSS_BORDER_COLLAPSE_SEPARATE) {
css_fixed h = 0, v = 0;
css_unit hu = CSS_UNIT_PX, vu = CSS_UNIT_PX;
css_computed_border_spacing(table->style, &h, &hu, &v, &vu);
border_spacing_h = FIXTOINT(css_unit_len2device_px(
table->style,
&content->unit_len_ctx,
h, hu));
}
/* 1st pass: consider cells with colspan 1 only */
for (row_group = table->children; row_group; row_group =row_group->next)
for (row = row_group->children; row; row = row->next)
for (cell = row->children; cell; cell = cell->next) {
assert(cell->type == BOX_TABLE_CELL);
assert(cell->style);
/** TODO: Handle colspan="0" correctly.
* It's currently converted to 1 in box normaisation */
assert(cell->columns != 0);
if (cell->columns != 1)
continue;
layout_minmax_block(cell, font_func, content);
i = cell->start_column;
if (col[i].positioned)
continue;
/* update column min, max widths using cell widths */
if (col[i].min < cell->min_width)
col[i].min = cell->min_width;
if (col[i].max < cell->max_width)
col[i].max = cell->max_width;
}
/* 2nd pass: cells which span multiple columns */
for (row_group = table->children; row_group; row_group =row_group->next)
for (row = row_group->children; row; row = row->next)
for (cell = row->children; cell; cell = cell->next) {
unsigned int flexible_columns = 0;
int min = 0, max = 0, fixed_width = 0, extra;
if (cell->columns == 1)
continue;
layout_minmax_block(cell, font_func, content);
i = cell->start_column;
/* find min width so far of spanned columns, and count
* number of non-fixed spanned columns and total fixed width */
for (j = 0; j != cell->columns; j++) {
min += col[i + j].min;
if (col[i + j].type == COLUMN_WIDTH_FIXED)
fixed_width += col[i + j].width;
else
flexible_columns++;
}
min += (cell->columns - 1) * border_spacing_h;
/* distribute extra min to spanned columns */
if (min < cell->min_width) {
if (flexible_columns == 0) {
extra = 1 + (cell->min_width - min) /
cell->columns;
for (j = 0; j != cell->columns; j++) {
col[i + j].min += extra;
if (col[i + j].max < col[i + j].min)
col[i + j].max = col[i + j].min;
}
} else {
extra = 1 + (cell->min_width - min) /
flexible_columns;
for (j = 0; j != cell->columns; j++) {
if (col[i + j].type !=
COLUMN_WIDTH_FIXED) {
col[i + j].min += extra;
if (col[i + j].max <
col[i + j].min)
col[i + j].max =
col[i + j].min;
}
}
}
}
/* find max width so far of spanned columns */
for (j = 0; j != cell->columns; j++)
max += col[i + j].max;
max += (cell->columns - 1) * border_spacing_h;
/* distribute extra max to spanned columns */
if (max < cell->max_width && flexible_columns) {
extra = 1 + (cell->max_width - max) / flexible_columns;
for (j = 0; j != cell->columns; j++)
if (col[i + j].type != COLUMN_WIDTH_FIXED)
col[i + j].max += extra;
}
}
for (i = 0; i != table->columns; i++) {
if (col[i].max < col[i].min) {
box_dump(stderr, table, 0, true);
assert(0);
}
table_min += col[i].min;
table_max += col[i].max;
}
/* fixed width takes priority, unless it is too narrow */
wtype = css_computed_width(table->style, &value, &unit);
if (wtype == CSS_WIDTH_SET && unit != CSS_UNIT_PCT) {
int width = FIXTOINT(css_unit_len2device_px(
table->style,
&content->unit_len_ctx,
value, unit));
if (table_min < width)
table_min = width;
if (table_max < width)
table_max = width;
}
/* add margins, border, padding to min, max widths */
calculate_mbp_width(&content->unit_len_ctx,
table->style, LEFT, true, true, true,
&extra_fixed, &extra_frac);
calculate_mbp_width(&content->unit_len_ctx,
table->style, RIGHT, true, true, true,
&extra_fixed, &extra_frac);
if (extra_fixed < 0)
extra_fixed = 0;
if (extra_frac < 0)
extra_frac = 0;
if (1.0 <= extra_frac)
extra_frac = 0.9;
table->min_width = (table_min + extra_fixed) / (1.0 - extra_frac);
table->max_width = (table_max + extra_fixed) / (1.0 - extra_frac);
table->min_width += (table->columns + 1) * border_spacing_h;
table->max_width += (table->columns + 1) * border_spacing_h;
assert(0 <= table->min_width && table->min_width <= table->max_width);
}
/**
* Helper to check if a box has percentage max width.
*
* \param[in] b Box to check.
* \return true iff box has percnetage max width.
*/
static inline bool box_has_percentage_max_width(struct box *b)
{
css_unit unit = CSS_UNIT_PX;
enum css_max_width_e type;
css_fixed value = 0;
assert(b != NULL);
type = css_computed_max_width(b->style, &value, &unit);
return ((type == CSS_MAX_WIDTH_SET) && (unit == CSS_UNIT_PCT));
}
/**
* Calculate minimum and maximum width of a line.
*
* \param first a box in an inline container
* \param line_min updated to minimum width of line starting at first
* \param line_max updated to maximum width of line starting at first
* \param first_line true iff this is the first line in the inline container
* \param line_has_height updated to true or false, depending on line
* \param font_func Font functions.
* \return first box in next line, or 0 if no more lines
* \post 0 <= *line_min <= *line_max
*/
static struct box *
layout_minmax_line(struct box *first,
int *line_min,
int *line_max,
bool first_line,
bool *line_has_height,
const struct gui_layout_table *font_func,
const html_content *content)
{
int min = 0, max = 0, width, height, fixed;
float frac;
size_t i, j;
struct box *b;
struct box *block;
plot_font_style_t fstyle;
bool no_wrap;
assert(first->parent);
assert(first->parent->parent);
assert(first->parent->parent->style);
block = first->parent->parent;
no_wrap = (css_computed_white_space(block->style) ==
CSS_WHITE_SPACE_NOWRAP ||
css_computed_white_space(block->style) ==
CSS_WHITE_SPACE_PRE);
*line_has_height = false;
/* corresponds to the pass 1 loop in layout_line() */
for (b = first; b; b = b->next) {
enum css_width_e wtype;
enum css_height_e htype;
enum css_box_sizing_e bs;
css_fixed value = 0;
css_unit unit = CSS_UNIT_PX;
assert(lh__box_is_inline_content(b));
NSLOG(layout, DEBUG, "%p: min %i, max %i", b, min, max);
if (b->type == BOX_BR) {
b = b->next;
break;
}
if (lh__box_is_float_box(b)) {
assert(b->children);
if (b->children->type == BOX_TABLE)
layout_minmax_table(b->children, font_func,
content);
else
layout_minmax_block(b->children, font_func,
content);
b->min_width = b->children->min_width;
b->max_width = b->children->max_width;
if (min < b->min_width)
min = b->min_width;
max += b->max_width;
continue;
}
if (b->type == BOX_INLINE_BLOCK || b->type == BOX_INLINE_FLEX) {
layout_minmax_block(b, font_func, content);
if (min < b->min_width)
min = b->min_width;
max += b->max_width;
if (b->flags & HAS_HEIGHT)
*line_has_height = true;
continue;
}
assert(b->style);
font_plot_style_from_css(&content->unit_len_ctx, b->style, &fstyle);
if (b->type == BOX_INLINE && !b->object &&
!(b->flags & REPLACE_DIM) &&
!(b->flags & IFRAME)) {
fixed = frac = 0;
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT, true, true, true,
&fixed, &frac);
if (!b->inline_end)
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, true, true,
&fixed, &frac);
if (0 < fixed)
max += fixed;
*line_has_height = true;
/* \todo update min width, consider fractional extra */
} else if (b->type == BOX_INLINE_END) {
fixed = frac = 0;
calculate_mbp_width(&content->unit_len_ctx,
b->inline_end->style, RIGHT,
true, true, true,
&fixed, &frac);
if (0 < fixed)
max += fixed;
if (b->next) {
if (b->space == UNKNOWN_WIDTH) {
font_func->width(&fstyle, " ", 1,
&b->space);
}
max += b->space;
}
*line_has_height = true;
continue;
}
if (lh__box_is_replace(b) == false) {
/* inline non-replaced, 10.3.1 and 10.6.1 */
bool no_wrap_box;
if (!b->text)
continue;
no_wrap_box = (css_computed_white_space(b->style) ==
CSS_WHITE_SPACE_NOWRAP ||
css_computed_white_space(b->style) ==
CSS_WHITE_SPACE_PRE);
if (b->width == UNKNOWN_WIDTH) {
/** \todo handle errors */
/* 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;
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);
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;
}
}
max += b->width;
if (b->next) {
if (b->space == UNKNOWN_WIDTH) {
font_func->width(&fstyle, " ", 1,
&b->space);
}
max += b->space;
}
if (no_wrap) {
/* Don't wrap due to block style,
* so min is the same as max */
min = max;
} else if (no_wrap_box) {
/* This inline box can't be wrapped,
* for min, consider box's width */
if (min < b->width)
min = b->width;
} else if (b->parent->flags & NEED_MIN) {
/* If we care what the minimum width is,
* calculate it. (It's only needed if we're
* shrinking-to-fit.) */
/* min = widest single word */
i = 0;
do {
for (j = i; j != b->length &&
b->text[j] != ' '; j++)
;
font_func->width(&fstyle, b->text + i,
j - i, &width);
if (min < width)
min = width;
i = j + 1;
} while (j != b->length);
}
*line_has_height = true;
continue;
}
/* inline replaced, 10.3.2 and 10.6.2 */
assert(b->style);
/* calculate box width */
wtype = css_computed_width(b->style, &value, &unit);
bs = css_computed_box_sizing(block->style);
if (wtype == CSS_WIDTH_SET) {
if (unit == CSS_UNIT_PCT) {
width = AUTO;
} else {
width = FIXTOINT(css_unit_len2device_px(
b->style,
&content->unit_len_ctx,
value, unit));
if (bs == CSS_BOX_SIZING_BORDER_BOX) {
fixed = frac = 0;
calculate_mbp_width(&content->unit_len_ctx,
block->style, LEFT,
false, true, true,
&fixed, &frac);
calculate_mbp_width(&content->unit_len_ctx,
block->style, RIGHT,
false, true, true,
&fixed, &frac);
if (width < fixed) {
width = fixed;
}
}
if (width < 0)
width = 0;
}
} else {
width = AUTO;
}
/* height */
htype = css_computed_height(b->style, &value, &unit);
if (htype == CSS_HEIGHT_SET) {
height = FIXTOINT(css_unit_len2device_px(
b->style,
&content->unit_len_ctx,
value, unit));
} else {
height = AUTO;
}
if (b->object || (b->flags & REPLACE_DIM)) {
if (b->object) {
int temp_height = height;
layout_get_object_dimensions(b,
&width, &temp_height,
INT_MIN, INT_MAX,
INT_MIN, INT_MAX);
}
fixed = frac = 0;
if (bs == CSS_BOX_SIZING_BORDER_BOX) {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, false, false,
&fixed, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, false, false,
&fixed, &frac);
} else {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, true, true,
&fixed, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, true, true,
&fixed, &frac);
}
if (0 < width + fixed)
width += fixed;
} else if (b->flags & IFRAME) {
/* TODO: handle percentage widths properly */
if (width == AUTO)
width = 400;
fixed = frac = 0;
if (bs == CSS_BOX_SIZING_BORDER_BOX) {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, false, false,
&fixed, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, false, false,
&fixed, &frac);
} else {
calculate_mbp_width(&content->unit_len_ctx,
b->style, LEFT,
true, true, true,
&fixed, &frac);
calculate_mbp_width(&content->unit_len_ctx,
b->style, RIGHT,
true, true, true,
&fixed, &frac);
}
if (0 < width + fixed)
width += fixed;
} else {
/* form control with no object */
if (width == AUTO)
width = FIXTOINT(css_unit_len2device_px(
b->style,
&content->unit_len_ctx,
INTTOFIX(1), CSS_UNIT_EM));
}
if (min < width && !box_has_percentage_max_width(b))
min = width;
if (width > 0)
max += width;
*line_has_height = true;
}
if (first_line) {
/* todo: handle percentage values properly */
/* todo: handle text-indent interaction with floats */
int text_indent = layout_text_indent(&content->unit_len_ctx,
first->parent->parent->style, 100);
min = (min + text_indent < 0) ? 0 : min + text_indent;
max = (max + text_indent < 0) ? 0 : max + text_indent;
}
*line_min = min;
*line_max = max;
NSLOG(layout, DEBUG, "line_min %i, line_max %i", min, max);
assert(b != first);
assert(0 <= *line_min);
assert(*line_min <= *line_max);
return b;
}
/**
* Calculate minimum and maximum width of an inline container.
*
* \param inline_container box of type INLINE_CONTAINER
* \param[out] has_height set to true if container has height
* \param font_func Font functions.
* \post inline_container->min_width and inline_container->max_width filled in,
* 0 <= inline_container->min_width <= inline_container->max_width
*/
static void
layout_minmax_inline_container(struct box *inline_container,
bool *has_height,
const struct gui_layout_table *font_func,
const html_content *content)
{
struct box *child;
int line_min = 0, line_max = 0;
int min = 0, max = 0;
bool first_line = true;
bool line_has_height;
assert(inline_container->type == BOX_INLINE_CONTAINER);
/* check if the widths have already been calculated */
if (inline_container->max_width != UNKNOWN_MAX_WIDTH)
return;
*has_height = false;
for (child = inline_container->children; child; ) {
child = layout_minmax_line(child, &line_min, &line_max,
first_line, &line_has_height, font_func,
content);
if (min < line_min)
min = line_min;
if (max < line_max)
max = line_max;
first_line = false;
*has_height |= line_has_height;
}
inline_container->min_width = min;
inline_container->max_width = max;
assert(0 <= inline_container->min_width &&
inline_container->min_width <=
inline_container->max_width);
}
/**
* Calculate minimum and maximum width of a block.
*
* \param block box of type BLOCK, INLINE_BLOCK, or TABLE_CELL
* \param font_func font functions
* \param content The HTML content being layed out.
* \post block->min_width and block->max_width filled in,
* 0 <= block->min_width <= block->max_width
*/
static void layout_minmax_block(
struct box *block,
const struct gui_layout_table *font_func,
const html_content *content)
{
struct box *child;
int min = 0, max = 0;
int extra_fixed = 0;
float extra_frac = 0;
enum css_width_e wtype = CSS_WIDTH_AUTO;
css_fixed width = 0;
css_unit wunit = CSS_UNIT_PX;
enum css_height_e htype = CSS_HEIGHT_AUTO;
css_fixed height = 0;
css_unit hunit = CSS_UNIT_PX;
enum css_box_sizing_e bs = CSS_BOX_SIZING_CONTENT_BOX;
bool using_min_border_box = false;
bool using_max_border_box = false;
bool child_has_height = false;
assert(block->type == BOX_BLOCK ||
block->type == BOX_FLEX ||
block->type == BOX_INLINE_FLEX ||
block->type == BOX_INLINE_BLOCK ||
block->type == BOX_TABLE_CELL);
/* check if the widths have already been calculated */
if (block->max_width != UNKNOWN_MAX_WIDTH)
return;
if (block->style != NULL) {
wtype = css_computed_width(block->style, &width, &wunit);
htype = css_computed_height(block->style, &height, &hunit);
bs = css_computed_box_sizing(block->style);
}
/* set whether the minimum width is of any interest for this box */
if (((block->parent && lh__box_is_float_box(block->parent)) ||
block->type == BOX_INLINE_BLOCK ||
block->type == BOX_INLINE_FLEX) &&
wtype != CSS_WIDTH_SET) {
/* box shrinks to fit; need minimum width */
block->flags |= NEED_MIN;
} else if (block->type == BOX_TABLE_CELL) {
/* box shrinks to fit; need minimum width */
block->flags |= NEED_MIN;
} else if (block->parent && (block->parent->flags & NEED_MIN) &&
wtype != CSS_WIDTH_SET) {
/* box inside shrink-to-fit context; need minimum width */
block->flags |= NEED_MIN;
} else if (block->parent && (block->parent->type == BOX_FLEX)) {
/* box is flex item */
block->flags |= NEED_MIN;
}
if (block->gadget && (block->gadget->type == GADGET_TEXTBOX ||
block->gadget->type == GADGET_PASSWORD ||
block->gadget->type == GADGET_FILE ||
block->gadget->type == GADGET_TEXTAREA) &&
block->style && wtype == CSS_WIDTH_AUTO) {
css_fixed size = INTTOFIX(10);
css_unit unit = CSS_UNIT_EM;
min = max = FIXTOINT(css_unit_len2device_px(block->style,
&content->unit_len_ctx, size, unit));
block->flags |= HAS_HEIGHT;
}
if (block->gadget && (block->gadget->type == GADGET_RADIO ||
block->gadget->type == GADGET_CHECKBOX) &&
block->style && wtype == CSS_WIDTH_AUTO) {
css_fixed size = INTTOFIX(1);
css_unit unit = CSS_UNIT_EM;
/* form checkbox or radio button
* if width is AUTO, set it to 1em */
min = max = FIXTOINT(css_unit_len2device_px(block->style,
&content->unit_len_ctx, size, unit));
block->flags |= HAS_HEIGHT;
}
if (block->object) {
if (content_get_type(block->object) == CONTENT_HTML) {
layout_minmax_block(html_get_box_tree(block->object),
font_func, content);
min = html_get_box_tree(block->object)->min_width;
max = html_get_box_tree(block->object)->max_width;
} else {
min = max = content_get_width(block->object);
}
block->flags |= HAS_HEIGHT;
} else if (block->flags & IFRAME) {
/** \todo do we need to know the min/max width of the iframe's
* content? */
block->flags |= HAS_HEIGHT;
} else {
/* recurse through children */
for (child = block->children; child; child = child->next) {
switch (child->type) {
case BOX_FLEX:
case BOX_BLOCK:
layout_minmax_block(child, font_func,
content);
if (child->flags & HAS_HEIGHT)
child_has_height = true;
break;
case BOX_INLINE_CONTAINER:
if (block->flags & NEED_MIN)
child->flags |= NEED_MIN;
layout_minmax_inline_container(child,
&child_has_height, font_func,
content);
if (child_has_height &&
child ==
child->parent->children) {
block->flags |= MAKE_HEIGHT;
}
break;
case BOX_TABLE:
layout_minmax_table(child, font_func,
content);
/* todo: fix for zero height tables */
child_has_height = true;
child->flags |= MAKE_HEIGHT;
break;
default:
assert(0);
}
assert(child->max_width != UNKNOWN_MAX_WIDTH);
if (child->style &&
(css_computed_position(child->style) ==
CSS_POSITION_ABSOLUTE ||
css_computed_position(child->style) ==
CSS_POSITION_FIXED)) {
/* This child is positioned out of normal flow,
* so it will have no affect on width */
continue;
}
if (lh__box_is_flex_container(block) &&
lh__flex_main_is_horizontal(block)) {
if (block->style != NULL &&
css_computed_flex_wrap(block->style) ==
CSS_FLEX_WRAP_NOWRAP) {
min += child->min_width;
} else {
if (min < child->min_width)
min = child->min_width;
}
max += child->max_width;
} else {
if (min < child->min_width)
min = child->min_width;
if (max < child->max_width)
max = child->max_width;
}
if (child_has_height)
block->flags |= HAS_HEIGHT;
}
}
if (max < min) {
box_dump(stderr, block, 0, true);
assert(0);
}
/* fixed width takes priority */
if (block->type != BOX_TABLE_CELL && !lh__box_is_flex_item(block)) {
bool border_box = bs == CSS_BOX_SIZING_BORDER_BOX;
enum css_max_width_e max_type;
enum css_min_width_e min_type;
css_unit unit = CSS_UNIT_PX;
css_fixed value = 0;
if (wtype == CSS_WIDTH_SET && wunit != CSS_UNIT_PCT) {
min = max = FIXTOINT(
css_unit_len2device_px(block->style,
&content->unit_len_ctx, width, wunit));
using_max_border_box = border_box;
using_min_border_box = border_box;
}
min_type = css_computed_min_width(block->style, &value, &unit);
if (min_type == CSS_MIN_WIDTH_SET && unit != CSS_UNIT_PCT) {
int val = FIXTOINT(css_unit_len2device_px(block->style,
&content->unit_len_ctx, value, unit));
if (min < val) {
min = val;
using_min_border_box = border_box;
}
}
max_type = css_computed_max_width(block->style, &value, &unit);
if (max_type == CSS_MAX_WIDTH_SET && unit != CSS_UNIT_PCT) {
int val = FIXTOINT(css_unit_len2device_px(block->style,
&content->unit_len_ctx, value, unit));
if (val >= 0 && max > val) {
max = val;
using_max_border_box = border_box;
}
}
}
if (htype == CSS_HEIGHT_SET && hunit != CSS_UNIT_PCT &&
height > INTTOFIX(0)) {
block->flags |= MAKE_HEIGHT;
block->flags |= HAS_HEIGHT;
}
/* add margins, border, padding to min, max widths */
/* Note: we don't know available width here so percentage margin
* and paddings are wrong. */
calculate_mbp_width(&content->unit_len_ctx, block->style, LEFT,
false, true, true, &extra_fixed, &extra_frac);
calculate_mbp_width(&content->unit_len_ctx, block->style, RIGHT,
false, true, true, &extra_fixed, &extra_frac);
if (using_max_border_box) {
max -= extra_fixed;
max = max(max, 0);
}
if (using_min_border_box) {
min -= extra_fixed;
min = max(min, 0);
}
if (max < min) {
min = max;
}
calculate_mbp_width(&content->unit_len_ctx, block->style, LEFT,
true, false, false, &extra_fixed, &extra_frac);
calculate_mbp_width(&content->unit_len_ctx, block->style, RIGHT,
true, false, false, &extra_fixed, &extra_frac);
if (extra_fixed < 0)
extra_fixed = 0;
if (extra_frac < 0)
extra_frac = 0;
if (1.0 <= extra_frac)
extra_frac = 0.9;
if (block->style != NULL &&
(css_computed_float(block->style) == CSS_FLOAT_LEFT ||
css_computed_float(block->style) == CSS_FLOAT_RIGHT)) {
/* floated boxs */
block->min_width = min + extra_fixed;
block->max_width = max + extra_fixed;
} else {
/* not floated */
block->min_width = (min + extra_fixed) / (1.0 - extra_frac);
block->max_width = (max + extra_fixed) / (1.0 - extra_frac);
}
assert(0 <= block->min_width);
assert(block->min_width <= block->max_width);
}
/*
* Find y coordinate which clears all floats on left and/or right.
*
* \param fl first float in float list
* \param clear type of clear
* \return y coordinate relative to ancestor box for floats
*/
int layout_clear(struct box *fl, enum css_clear_e clear)
{
int y = 0;
for (; fl; fl = fl->next_float) {
if ((clear == CSS_CLEAR_LEFT || clear == CSS_CLEAR_BOTH) &&
fl->type == BOX_FLOAT_LEFT)
if (y < fl->y + fl->height)
y = fl->y + fl->height;
if ((clear == CSS_CLEAR_RIGHT || clear == CSS_CLEAR_BOTH) &&
fl->type == BOX_FLOAT_RIGHT)
if (y < fl->y + fl->height)
y = fl->y + fl->height;
}
return y;
}
/*
* Find left and right edges in a vertical range.
*
* \param fl first float in float list
* \param y0 start of y range to search
* \param y1 end of y range to search
* \param x0 start left edge, updated to available left edge
* \param x1 start right edge, updated to available right edge
* \param left returns float on left if present
* \param right returns float on right if present
*/
void
find_sides(struct box *fl,
int y0, int y1,
int *x0, int *x1,
struct box **left,
struct box **right)
{
int fy0, fy1, fx0, fx1;
NSLOG(layout, DEBUG, "y0 %i, y1 %i, x0 %i, x1 %i", y0, y1, *x0, *x1);
*left = *right = 0;
for (; fl; fl = fl->next_float) {
fy1 = fl->y + fl->height;
if (fy1 < y0) {
/* Floats are sorted in order of decreasing bottom pos.
* Past here, all floats will be too high to concern us.
*/
return;
}
fy0 = fl->y;
if (y0 < fy1 && fy0 <= y1) {
if (fl->type == BOX_FLOAT_LEFT) {
fx1 = fl->x + fl->width;
if (*x0 < fx1) {
*x0 = fx1;
*left = fl;
}
} else {
fx0 = fl->x;
if (fx0 < *x1) {
*x1 = fx0;
*right = fl;
}
}
}
}
NSLOG(layout, DEBUG, "x0 %i, x1 %i, left %p, right %p", *x0, *x1,
*left, *right);
}
/*
* Solve the width constraint as given in CSS 2.1 section 10.3.3.
*
* \param box Box to solve constraint for
* \param available_width Max width available in pixels
* \param width Current box width
* \param lm Min left margin required to avoid floats in px.
* zero if not applicable
* \param rm Min right margin required to avoid floats in px.
* zero if not applicable
* \param max_width Box max-width ( -ve means no max-width to apply)
* \param min_width Box min-width ( <=0 means no min-width to apply)
* \return New box width
*
* \post \a box's left/right margins will be updated.
*/
int
layout_solve_width(struct box *box,
int available_width,
int width,
int lm,
int rm,
int max_width,
int min_width)
{
bool auto_width = false;
/* Increase specified left/right margins */
if (box->margin[LEFT] != AUTO && box->margin[LEFT] < lm &&
box->margin[LEFT] >= 0)
box->margin[LEFT] = lm;
if (box->margin[RIGHT] != AUTO && box->margin[RIGHT] < rm &&
box->margin[RIGHT] >= 0)
box->margin[RIGHT] = rm;
/* Find width */
if (width == AUTO) {
int margin_left = box->margin[LEFT];
int margin_right = box->margin[RIGHT];
if (margin_left == AUTO) {
margin_left = lm;
}
if (margin_right == AUTO) {
margin_right = rm;
}
width = available_width -
(margin_left + box->border[LEFT].width +
box->padding[LEFT] + box->padding[RIGHT] +
box->border[RIGHT].width + margin_right);
width = width < 0 ? 0 : width;
auto_width = true;
}
if (max_width >= 0 && width > max_width) {
/* max-width is admissable and width exceeds max-width */
width = max_width;
auto_width = false;
}
if (min_width > 0 && width < min_width) {
/* min-width is admissable and width is less than max-width */
width = min_width;
auto_width = false;
}
/* Width was auto, and unconstrained by min/max width, so we're done */
if (auto_width) {
/* any other 'auto' become 0 or the minimum required values */
if (box->margin[LEFT] == AUTO) {
box->margin[LEFT] = lm;
}
if (box->margin[RIGHT] == AUTO) {
box->margin[RIGHT] = rm;
}
return width;
}
/* Width was not auto, or was constrained by min/max width
* Need to compute left/right margins */
/* HTML alignment (only applies to over-constrained boxes) */
if (box->margin[LEFT] != AUTO && box->margin[RIGHT] != AUTO &&
box->parent != NULL && box->parent->style != NULL) {
switch (css_computed_text_align(box->parent->style)) {
case CSS_TEXT_ALIGN_LIBCSS_RIGHT:
box->margin[LEFT] = AUTO;
box->margin[RIGHT] = 0;
break;
case CSS_TEXT_ALIGN_LIBCSS_CENTER:
box->margin[LEFT] = box->margin[RIGHT] = AUTO;
break;
case CSS_TEXT_ALIGN_LIBCSS_LEFT:
box->margin[LEFT] = 0;
box->margin[RIGHT] = AUTO;
break;
default:
/* Leave it alone; no HTML alignment */
break;
}
}
if (box->margin[LEFT] == AUTO && box->margin[RIGHT] == AUTO) {
/* make the margins equal, centering the element */
box->margin[LEFT] = box->margin[RIGHT] =
(available_width - lm - rm -
(box->border[LEFT].width + box->padding[LEFT] +
width + box->padding[RIGHT] +
box->border[RIGHT].width)) / 2;
if (box->margin[LEFT] < 0) {
box->margin[RIGHT] += box->margin[LEFT];
box->margin[LEFT] = 0;
}
box->margin[LEFT] += lm;
} else if (box->margin[LEFT] == AUTO) {
box->margin[LEFT] = available_width - lm -
(box->border[LEFT].width + box->padding[LEFT] +
width + box->padding[RIGHT] +
box->border[RIGHT].width + box->margin[RIGHT]);
box->margin[LEFT] = box->margin[LEFT] < lm
? lm : box->margin[LEFT];
} else {
/* margin-right auto or "over-constrained" */
box->margin[RIGHT] = available_width - rm -
(box->margin[LEFT] + box->border[LEFT].width +
box->padding[LEFT] + width +
box->padding[RIGHT] +
box->border[RIGHT].width);
}
return width;
}
/*
* Manimpulate box height according to CSS min-height and max-height properties
*
* \param unit_len_ctx CSS length conversion context for document.
* \param box block to modify with any min-height or max-height
* \param container containing block for absolutely positioned elements, or
* NULL for non absolutely positioned elements.
* \return whether the height has been changed
*/
bool layout_apply_minmax_height(
const css_unit_ctx *unit_len_ctx,
struct box *box,
struct box *container)
{
int h;
struct box *containing_block = NULL;
bool updated = false;
/* Find containing block for percentage heights */
if (box->style != NULL && css_computed_position(box->style) ==
CSS_POSITION_ABSOLUTE) {
/* Box is absolutely positioned */
assert(container);
containing_block = container;
} else if (box->float_container && box->style != NULL &&
(css_computed_float(box->style) == CSS_FLOAT_LEFT ||
css_computed_float(box->style) == CSS_FLOAT_RIGHT)) {
/* Box is a float */
assert(box->parent && box->parent->parent &&
box->parent->parent->parent);
containing_block = box->parent->parent->parent;
} else if (box->parent && box->parent->type != BOX_INLINE_CONTAINER) {
/* Box is a block level element */
containing_block = box->parent;
} else if (box->parent && box->parent->type == BOX_INLINE_CONTAINER) {
/* Box is an inline block */
assert(box->parent->parent);
containing_block = box->parent->parent;
}
if (box->style) {
enum css_height_e htype = CSS_HEIGHT_AUTO;
css_fixed value = 0;
css_unit unit = CSS_UNIT_PX;
if (containing_block) {
htype = css_computed_height(containing_block->style,
&value, &unit);
}
/* max-height */
if (css_computed_max_height(box->style, &value, &unit) ==
CSS_MAX_HEIGHT_SET) {
if (unit == CSS_UNIT_PCT) {
if (containing_block &&
containing_block->height != AUTO &&
(css_computed_position(box->style) ==
CSS_POSITION_ABSOLUTE ||
htype == CSS_HEIGHT_SET)) {
/* Box is absolutely positioned or its
* containing block has a valid
* specified height. (CSS 2.1
* Section 10.5) */
h = FPCT_OF_INT_TOINT(value,
containing_block->height);
if (h < box->height) {
box->height = h;
updated = true;
}
}
} else {
h = FIXTOINT(css_unit_len2device_px(
box->style, unit_len_ctx,
value, unit));
if (h < box->height) {
box->height = h;
updated = true;
}
}
}
/* min-height */
if (ns_computed_min_height(box->style, &value, &unit) ==
CSS_MIN_HEIGHT_SET) {
if (unit == CSS_UNIT_PCT) {
if (containing_block &&
containing_block->height != AUTO &&
(css_computed_position(box->style) ==
CSS_POSITION_ABSOLUTE ||
htype == CSS_HEIGHT_SET)) {
/* Box is absolutely positioned or its
* containing block has a valid
* specified height. (CSS 2.1
* Section 10.5) */
h = FPCT_OF_INT_TOINT(value,
containing_block->height);
if (h > box->height) {
box->height = h;
updated = true;
}
}
} else {
h = FIXTOINT(css_unit_len2device_px(
box->style, unit_len_ctx,
value, unit));
if (h > box->height) {
box->height = h;
updated = true;
}
}
}
}
return updated;
}
/**
* Get a dom node's element tag type.
*
* \param[in] node Node to get tag type of.
* \param[in] type Returns element tag type on success.
* \return true if on success, false otherwise.
*/
static bool
layout__get_element_tag(
const dom_node *node,
dom_html_element_type *type)
{
dom_html_element_type element_type;
dom_node_type node_type;
dom_exception exc;
exc = dom_node_get_node_type(node, &node_type);
if (exc != DOM_NO_ERR ||
node_type != DOM_ELEMENT_NODE) {
return false;
}
exc = dom_html_element_get_tag_type(node, &element_type);
if (exc != DOM_NO_ERR) {
return false;
}
*type = element_type;
return true;
}
/**
* Check a node's tag type.
*
* \param[in] node Node to check tag type of.
* \param[in] type Tag type to test for.
* \return true if if node has given type, false otherwise.
*/
static inline bool
layout__check_element_type(
const dom_node *node,
dom_html_element_type type)
{
dom_html_element_type element_type;
if (!layout__get_element_tag(node, &element_type)) {
return false;
}
return element_type == type;
}
/**
* Helper to get attribute value from a LI node.
*
* \param[in] li_node DOM node for the LI element;
* \param[out] value_out Returns the value on success.
* \return true if node has value, otherwise false.
*/
static bool
layout__get_li_value(dom_node *li_node, dom_long *value_out)
{
dom_exception exc;
dom_long value;
bool has_value;
/** \todo
* dom_html_li_element_get_value() is rubbish and we can't tell
* a lack of value attribute or invalid value from a perfectly
* valid '-1'.
*
* This helps for the common case of no value. However we should
* fix libdom to have some kind of sane interface to get numerical
* attributes.
*/
exc = dom_element_has_attribute(li_node,
corestring_dom_value,
&has_value);
if (exc != DOM_NO_ERR || has_value == false) {
return false;
}
exc = dom_html_li_element_get_value(
(dom_html_li_element *)li_node,
&value);
if (exc != DOM_NO_ERR) {
return false;
}
*value_out = value;
return true;
}
/**
* Helper to get start attribute value from a OL node.
*
* \param[in] ol_node DOM node for the OL element;
* \param[out] start_out Returns the value on success.
* \return true if node has value, otherwise false.
*/
static bool
layout__get_ol_start(dom_node *ol_node, dom_long *start_out)
{
dom_exception exc;
dom_long start;
bool has_start;
/** \todo
* see layout__get_li_value().
*/
exc = dom_element_has_attribute(ol_node,
corestring_dom_start,
&has_start);
if (exc != DOM_NO_ERR || has_start == false) {
return false;
}
exc = dom_html_olist_element_get_start(
(dom_html_olist_element *)ol_node,
&start);
if (exc != DOM_NO_ERR) {
return false;
}
*start_out = start;
return true;
}
/**
* Helper to get reversed attribute value from a OL node.
*
* \param[in] ol_node DOM node for the OL element;
* \return true if node has reversed, otherwise false.
*/
static bool
layout__get_ol_reversed(dom_node *ol_node)
{
dom_exception exc;
bool has_reversed;
exc = dom_element_has_attribute(ol_node,
corestring_dom_reversed,
&has_reversed);
if (exc != DOM_NO_ERR) {
return false;
}
return has_reversed;
}
/**
* Get the number of list items for a list owner.
*
* \param[in] list_owner DOM node to count list items for.
* \param[in] count_out Returns list item count on success.
* \return true on success, otherwise false.
*/
static bool
layout__get_list_item_count(
dom_node *list_owner, dom_long *count_out)
{
dom_html_element_type tag_type;
dom_exception exc;
dom_node *child;
int count;
if (list_owner == NULL) {
return false;
}
if (!layout__get_element_tag(list_owner, &tag_type)) {
return false;
}
if (tag_type != DOM_HTML_ELEMENT_TYPE_OL &&
tag_type != DOM_HTML_ELEMENT_TYPE_UL) {
return false;
}
exc = dom_node_get_first_child(list_owner, &child);
if (exc != DOM_NO_ERR) {
return false;
}
count = 0;
while (child != NULL) {
dom_node *temp_node;
if (layout__check_element_type(child,
DOM_HTML_ELEMENT_TYPE_LI)) {
struct box *child_box;
if (dom_node_get_user_data(child,
corestring_dom___ns_key_box_node_data,
&child_box) != DOM_NO_ERR) {
dom_node_unref(child);
return false;
}
if (child_box != NULL &&
child_box->list_marker != NULL) {
count++;
}
}
exc = dom_node_get_next_sibling(child, &temp_node);
dom_node_unref(child);
if (exc != DOM_NO_ERR) {
return false;
}
child = temp_node;
}
*count_out = count;
return true;
}
/**
* Handle list item counting, if this is a list owner box.
*
* \param[in] box Box to do list item counting for.
*/
static void
layout__ordered_list_count(
struct box *box)
{
dom_html_element_type tag_type;
dom_exception exc;
dom_node *child;
int step = 1;
dom_long next;
if (box->node == NULL) {
return;
}
if (!layout__get_element_tag(box->node, &tag_type)) {
return;
}
if (tag_type != DOM_HTML_ELEMENT_TYPE_OL &&
tag_type != DOM_HTML_ELEMENT_TYPE_UL) {
return;
}
next = 1;
if (tag_type == DOM_HTML_ELEMENT_TYPE_OL) {
bool have_start = layout__get_ol_start(box->node, &next);
bool have_reversed = layout__get_ol_reversed(box->node);
if (have_reversed) {
step = -1;
}
if (!have_start && have_reversed) {
layout__get_list_item_count(box->node, &next);
}
}
exc = dom_node_get_first_child(box->node, &child);
if (exc != DOM_NO_ERR) {
return;
}
while (child != NULL) {
dom_node *temp_node;
if (layout__check_element_type(child,
DOM_HTML_ELEMENT_TYPE_LI)) {
struct box *child_box;
if (dom_node_get_user_data(child,
corestring_dom___ns_key_box_node_data,
&child_box) != DOM_NO_ERR) {
dom_node_unref(child);
return;
}
if (child_box != NULL &&
child_box->list_marker != NULL) {
dom_long value;
struct box *marker = child_box->list_marker;
if (layout__get_li_value(child, &value)) {
marker->list_value = value;
next = marker->list_value;
} else {
marker->list_value = next;
}
next += step;
}
}
exc = dom_node_get_next_sibling(child, &temp_node);
dom_node_unref(child);
if (exc != DOM_NO_ERR) {
return;
}
child = temp_node;
}
}
/**
* Set up the marker text for a numerical list item.
*
* \param[in] content The HTML content.
* \param[in] box The list item's main box.
*/
static void
layout__set_numerical_marker_text(
const html_content *content,
struct box *box)
{
struct box *marker = box->list_marker;
size_t counter_len;
css_error css_res;
enum {
/**
* initial length of a list marker buffer
*
* enough for 9,999,999,999,999,999,999 in decimal
* or five characters for 4-byte UTF-8.
*/
LIST_MARKER_SIZE = 20,
};
marker->text = talloc_array(content->bctx, char, LIST_MARKER_SIZE);
if (marker->text == NULL) {
return;
}
css_res = css_computed_format_list_style(box->style, marker->list_value,
marker->text, LIST_MARKER_SIZE, &counter_len);
if (css_res == CSS_OK) {
if (counter_len > LIST_MARKER_SIZE) {
/* Use computed size as marker did not fit in
* default allocation. */
marker->text = talloc_realloc(content->bctx,
marker->text,
char,
counter_len);
if (marker->text == NULL) {
return;
}
css_computed_format_list_style(box->style,
marker->list_value, marker->text,
counter_len, &counter_len);
}
marker->length = counter_len;
}
}
/**
* Find out if box's style represents a numerical list style type.
*
* \param[in] b Box with style to test.
* \return true if box has numerical list style type, false otherwise.
*/
static bool
layout__list_item_is_numerical(
const struct box *b)
{
enum css_list_style_type_e t = css_computed_list_style_type(b->style);
switch (t) {
case CSS_LIST_STYLE_TYPE_DISC: /* Fall through. */
case CSS_LIST_STYLE_TYPE_CIRCLE: /* Fall through. */
case CSS_LIST_STYLE_TYPE_SQUARE: /* Fall through. */
case CSS_LIST_STYLE_TYPE_NONE:
return false;
default:
return true;
}
}
/**
* Layout list markers.
*/
static void
layout_lists(const html_content *content, struct box *box)
{
struct box *child;
layout__ordered_list_count(box);
for (child = box->children; child; child = child->next) {
if (child->list_marker) {
struct box *marker = child->list_marker;
if (layout__list_item_is_numerical(child)) {
if (marker->text == NULL) {
layout__set_numerical_marker_text(
content, child);
}
}
if (marker->object) {
marker->width =
content_get_width(marker->object);
marker->x = -marker->width;
marker->height =
content_get_height(marker->object);
marker->y = (layout_line_height(
&content->unit_len_ctx,
marker->style) -
marker->height) / 2;
} else if (marker->text) {
if (marker->width == UNKNOWN_WIDTH) {
plot_font_style_t fstyle;
font_plot_style_from_css(
&content->unit_len_ctx,
marker->style,
&fstyle);
content->font_func->width(&fstyle,
marker->text,
marker->length,
&marker->width);
marker->flags |= MEASURED;
}
marker->x = -marker->width;
marker->y = 0;
marker->height = layout_line_height(
&content->unit_len_ctx,
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(content, child);
}
}
/**
* Compute box offsets for a relatively or absolutely positioned box with
* respect to a box.
*
* \param unit_len_ctx Length conversion context
* \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(const css_unit_ctx *unit_len_ctx,
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);
assert(containing_block->width != AUTO);
assert(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 {
*left = FIXTOINT(css_unit_len2device_px(
box->style, unit_len_ctx,
value, unit));
}
} else {
*left = AUTO;
}
/* 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(css_unit_len2device_px(
box->style, unit_len_ctx,
value, unit));
}
} else {
*right = AUTO;
}
/* 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(css_unit_len2device_px(
box->style, unit_len_ctx,
value, unit));
}
} else {
*top = AUTO;
}
/* 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(css_unit_len2device_px(
box->style, unit_len_ctx,
value, unit));
}
} else {
*bottom = AUTO;
}
}
/**
* 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;
assert(box->type == BOX_BLOCK || box->type == BOX_TABLE ||
box->type == BOX_INLINE_BLOCK ||
box->type == BOX_FLEX ||
box->type == BOX_INLINE_FLEX);
/* 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];
}
layout_compute_offsets(&content->unit_len_ctx, 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(&content->unit_len_ctx, 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;
}
} 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;
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];
}
}
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);
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 (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);
} else if (box->type == BOX_FLEX || box->type == BOX_INLINE_FLEX) {
/* layout_table also expects the containing block to be
* stored in the float_container field */
box->float_container = containing_block;
if (!layout_flex(box, width, content))
return false;
box->float_container = NULL;
}
/* 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];
}
}
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);
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(&content->unit_len_ctx, box, containing_block);
return true;
}
/**
* 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 ||
c->type == BOX_FLEX ||
c->type == BOX_INLINE_FLEX) &&
(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 {
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;
}
}
return true;
}
/**
* Compute a box's relative offset as per CSS 2.1 9.4.3
*
* \param unit_len_ctx Length conversion context
* \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(
const css_unit_ctx *unit_len_ctx,
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;
}
layout_compute_offsets(unit_len_ctx, box, containing_block,
&top, &right, &bottom, &left);
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;
}
}
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;
}
NSLOG(layout, DEBUG, "left %i, right %i, top %i, bottom %i", left,
right, top, bottom);
*x = left;
*y = top;
}
/**
* Adjust positions of relatively positioned boxes.
*
* \param unit_len_ctx Length conversion context
* \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(
const css_unit_ctx *unit_len_ctx,
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" */
/**\todo ensure containing box is large enough after moving boxes */
assert(root);
/* Normal children */
for (box = root->children; box; box = box->next) {
if (box->type == BOX_TEXT)
continue;
/* If relatively positioned, get offsets */
if (box->style && css_computed_position(box->style) ==
CSS_POSITION_RELATIVE)
layout_compute_relative_offset(
unit_len_ctx, box, &x, &y);
else
x = y = 0;
/* 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;
}
}
}
if (box->float_children) {
fn = box;
fnx = fny = 0;
} else {
fn = fp;
fnx = fx + x;
fny = fy + y;
}
/* recurse first */
layout_position_relative(unit_len_ctx, 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;
}
}
}
}
/**
* Find a box's bounding box relative to itself, i.e. the box's border edge box
*
* \param unit_len_ctx Length conversion context
* \param box box find bounding box of
* \param desc_x0 updated to left of box's bbox
* \param desc_y0 updated to top of box's bbox
* \param desc_x1 updated to right of box's bbox
* \param desc_y1 updated to bottom of box's bbox
*/
static void
layout_get_box_bbox(
const css_unit_ctx *unit_len_ctx,
struct box *box,
int *desc_x0, int *desc_y0,
int *desc_x1, int *desc_y1)
{
*desc_x0 = -box->border[LEFT].width;
*desc_y0 = -box->border[TOP].width;
*desc_x1 = box->padding[LEFT] + box->width + box->padding[RIGHT] +
box->border[RIGHT].width;
*desc_y1 = box->padding[TOP] + box->height + box->padding[BOTTOM] +
box->border[BOTTOM].width;
/* To stop the top of text getting clipped when css line-height is
* reduced, we increase the top of the descendant bbox. */
if (box->type == BOX_BLOCK && box->style != NULL &&
css_computed_overflow_y(box->style) ==
CSS_OVERFLOW_VISIBLE &&
box->object == NULL) {
css_fixed font_size = 0;
css_unit font_unit = CSS_UNIT_PT;
int text_height;
css_computed_font_size(box->style, &font_size, &font_unit);
text_height = css_unit_len2device_px(box->style, unit_len_ctx,
font_size, font_unit);
text_height = FIXTOINT(text_height * 3 / 4);
*desc_y0 = (*desc_y0 < -text_height) ? *desc_y0 : -text_height;
}
}
/**
* Apply changes to box descendant_[xy][01] values due to given child.
*
* \param unit_len_ctx Length conversion context
* \param box box to update
* \param child a box, which may affect box's descendant bbox
* \param off_x offset to apply to child->x coord to treat as child of box
* \param off_y offset to apply to child->y coord to treat as child of box
*/
static void
layout_update_descendant_bbox(
const css_unit_ctx *unit_len_ctx,
struct box *box,
struct box *child,
int off_x,
int off_y)
{
int child_desc_x0, child_desc_y0, child_desc_x1, child_desc_y1;
/* get coordinates of child relative to box */
int child_x = child->x - off_x;
int child_y = child->y - off_y;
bool html_object = (child->object &&
content_get_type(child->object) == CONTENT_HTML);
enum css_overflow_e overflow_x = CSS_OVERFLOW_VISIBLE;
enum css_overflow_e overflow_y = CSS_OVERFLOW_VISIBLE;
if (child->style != NULL) {
overflow_x = css_computed_overflow_x(child->style);
overflow_y = css_computed_overflow_y(child->style);
}
/* Get child's border edge */
layout_get_box_bbox(unit_len_ctx, child,
&child_desc_x0, &child_desc_y0,
&child_desc_x1, &child_desc_y1);
if (overflow_x == CSS_OVERFLOW_VISIBLE &&
html_object == false) {
/* get child's descendant bbox relative to box */
child_desc_x0 = child->descendant_x0;
child_desc_x1 = child->descendant_x1;
}
if (overflow_y == CSS_OVERFLOW_VISIBLE &&
html_object == false) {
/* get child's descendant bbox relative to box */
child_desc_y0 = child->descendant_y0;
child_desc_y1 = child->descendant_y1;
}
child_desc_x0 += child_x;
child_desc_y0 += child_y;
child_desc_x1 += child_x;
child_desc_y1 += child_y;
/* increase box's descendant bbox to contain descendants */
if (child_desc_x0 < box->descendant_x0)
box->descendant_x0 = child_desc_x0;
if (child_desc_y0 < box->descendant_y0)
box->descendant_y0 = child_desc_y0;
if (box->descendant_x1 < child_desc_x1)
box->descendant_x1 = child_desc_x1;
if (box->descendant_y1 < child_desc_y1)
box->descendant_y1 = child_desc_y1;
}
/**
* Recursively calculate the descendant_[xy][01] values for a laid-out box tree
* and inform iframe browser windows of their size and position.
*
* \param unit_len_ctx Length conversion context
* \param box tree of boxes to update
*/
static void layout_calculate_descendant_bboxes(
const css_unit_ctx *unit_len_ctx,
struct box *box)
{
struct box *child;
assert(box->width != UNKNOWN_WIDTH);
assert(box->height != AUTO);
/* assert((box->width >= 0) && (box->height >= 0)); */
/* Initialise box's descendant box to border edge box */
layout_get_box_bbox(unit_len_ctx, box,
&box->descendant_x0, &box->descendant_y0,
&box->descendant_x1, &box->descendant_y1);
/* Extend it to contain HTML contents if box is replaced */
if (box->object && content_get_type(box->object) == CONTENT_HTML) {
if (box->descendant_x1 < content_get_width(box->object))
box->descendant_x1 = content_get_width(box->object);
if (box->descendant_y1 < content_get_height(box->object))
box->descendant_y1 = content_get_height(box->object);
}
if (box->iframe != NULL) {
int x, y;
box_coords(box, &x, &y);
browser_window_set_position(box->iframe, x, y);
browser_window_set_dimensions(box->iframe,
box->width, box->height);
browser_window_reformat(box->iframe, true,
box->width, box->height);
}
if (box->type == BOX_INLINE || box->type == BOX_TEXT)
return;
if (box->type == BOX_INLINE_END) {
box = box->inline_end;
for (child = box->next; child;
child = child->next) {
if (child->type == BOX_FLOAT_LEFT ||
child->type == BOX_FLOAT_RIGHT)
continue;
layout_update_descendant_bbox(unit_len_ctx, box, child,
box->x, box->y);
if (child == box->inline_end)
break;
}
return;
}
if (box->flags & REPLACE_DIM)
/* Box's children aren't displayed if the box is replaced */
return;
for (child = box->children; child; child = child->next) {
if (child->type == BOX_FLOAT_LEFT ||
child->type == BOX_FLOAT_RIGHT)
continue;
layout_calculate_descendant_bboxes(unit_len_ctx, child);
if (box->style && css_computed_overflow_x(box->style) ==
CSS_OVERFLOW_HIDDEN &&
css_computed_overflow_y(box->style) ==
CSS_OVERFLOW_HIDDEN)
continue;
layout_update_descendant_bbox(unit_len_ctx, box, child, 0, 0);
}
for (child = box->float_children; child; child = child->next_float) {
assert(child->type == BOX_FLOAT_LEFT ||
child->type == BOX_FLOAT_RIGHT);
layout_calculate_descendant_bboxes(unit_len_ctx, child);
layout_update_descendant_bbox(unit_len_ctx, box, child, 0, 0);
}
if (box->list_marker) {
child = box->list_marker;
layout_calculate_descendant_bboxes(unit_len_ctx, child);
layout_update_descendant_bbox(unit_len_ctx, box, child, 0, 0);
}
}
/* exported function documented in html/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;
NSLOG(layout, DEBUG, "Doing layout to %ix%i of %s",
width, height, nsurl_access(content_get_url(
&content->base)));
layout_minmax_block(doc, font_func, content);
layout_block_find_dimensions(&content->unit_len_ctx,
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;
ret = layout_block_context(doc, height, content);
/* make <html> and <body> 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(content, doc);
layout_position_absolute(doc, doc, 0, 0, content);
layout_position_relative(&content->unit_len_ctx, doc, doc, 0, 0);
layout_calculate_descendant_bboxes(&content->unit_len_ctx, doc);
return ret;
}