2003-06-30 16:44:03 +04:00
|
|
|
/*
|
|
|
|
* This file is part of NetSurf, http://netsurf.sourceforge.net/
|
|
|
|
* Licensed under the GNU General Public License,
|
|
|
|
* http://www.opensource.org/licenses/gpl-license
|
2004-02-13 19:10:28 +03:00
|
|
|
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
|
2003-06-30 16:44:03 +04:00
|
|
|
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
|
2002-05-04 23:57:18 +04:00
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
/** \file
|
|
|
|
* HTML layout (implementation).
|
|
|
|
*
|
|
|
|
* Layout is carried out in a single pass through the box tree, except for
|
|
|
|
* precalculation of minimum / maximum box widths.
|
|
|
|
*/
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2003-07-18 21:08:39 +04:00
|
|
|
#include <limits.h>
|
2004-02-02 01:42:40 +03:00
|
|
|
#include <stdbool.h>
|
2002-05-04 23:57:18 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-07-18 03:01:02 +04:00
|
|
|
#include "netsurf/css/css.h"
|
2003-06-17 23:24:21 +04:00
|
|
|
#ifdef riscos
|
2003-02-09 15:58:15 +03:00
|
|
|
#include "netsurf/desktop/gui.h"
|
2003-06-17 23:24:21 +04:00
|
|
|
#endif
|
2002-08-06 00:34:45 +04:00
|
|
|
#include "netsurf/render/box.h"
|
2003-07-18 03:01:02 +04:00
|
|
|
#include "netsurf/render/font.h"
|
2002-08-06 00:34:45 +04:00
|
|
|
#include "netsurf/render/layout.h"
|
2003-03-26 00:51:29 +03:00
|
|
|
#define NDEBUG
|
2002-10-12 17:05:16 +04:00
|
|
|
#include "netsurf/utils/log.h"
|
2003-07-18 03:01:02 +04:00
|
|
|
#include "netsurf/utils/utils.h"
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2004-02-02 03:22:59 +03:00
|
|
|
|
|
|
|
#define AUTO INT_MIN
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
static void layout_node(struct box *box, int width, struct box *cont,
|
|
|
|
int cx, int cy);
|
|
|
|
static void layout_block_find_dimensions(int available_width,
|
|
|
|
struct css_style *style, struct box *box);
|
|
|
|
static void layout_float_find_dimensions(int available_width,
|
|
|
|
struct css_style *style, struct box *box);
|
|
|
|
static void layout_find_dimensions(int available_width,
|
2004-02-02 03:22:59 +03:00
|
|
|
struct css_style *style,
|
|
|
|
int margin[4], int padding[4], int border[4]);
|
|
|
|
static void layout_block_children(struct box *box, struct box *cont,
|
2004-02-11 20:15:36 +03:00
|
|
|
int cx, int cy);
|
2004-02-13 19:10:28 +03:00
|
|
|
static int layout_clear(struct box *fl, css_clear clear);
|
2004-02-11 20:15:36 +03:00
|
|
|
static void find_sides(struct box *fl, int y0, int y1,
|
|
|
|
int *x0, int *x1, struct box **left, struct box **right);
|
|
|
|
static void layout_inline_container(struct box *box, int width,
|
|
|
|
struct box *cont, int cx, int cy);
|
|
|
|
static int line_height(struct css_style *style);
|
|
|
|
static struct box * layout_line(struct box *first, int width, int *y,
|
|
|
|
int cx, int cy, struct box *cont, bool indent);
|
|
|
|
static void place_float_below(struct box *c, int width, int y,
|
|
|
|
struct box *cont);
|
|
|
|
static void layout_table(struct box *box);
|
2003-03-04 14:59:36 +03:00
|
|
|
static void calculate_widths(struct box *box);
|
|
|
|
static void calculate_inline_container_widths(struct box *box);
|
|
|
|
static void calculate_table_widths(struct box *table);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-02 03:22:59 +03:00
|
|
|
* Calculate positions of boxes in a document.
|
2002-06-19 01:24:21 +04:00
|
|
|
*
|
2004-02-02 03:22:59 +03:00
|
|
|
* \param doc root of document box tree
|
2004-02-11 20:15:36 +03:00
|
|
|
* \param width available page width
|
2002-06-19 01:24:21 +04:00
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void layout_document(struct box *doc, int width)
|
2002-05-28 03:21:11 +04:00
|
|
|
{
|
2003-04-15 21:53:00 +04:00
|
|
|
struct box *box;
|
2002-05-28 03:21:11 +04:00
|
|
|
doc->float_children = 0;
|
2004-02-11 20:15:36 +03:00
|
|
|
|
|
|
|
calculate_widths(doc);
|
|
|
|
|
|
|
|
layout_block_find_dimensions(width, doc->style, doc);
|
|
|
|
doc->x = doc->margin[LEFT] + doc->border[LEFT];
|
|
|
|
doc->y = doc->margin[TOP] + doc->border[TOP];
|
|
|
|
width -= doc->margin[LEFT] + doc->border[LEFT] +
|
|
|
|
doc->border[RIGHT] + doc->margin[RIGHT];
|
2002-06-29 00:14:04 +04:00
|
|
|
layout_node(doc, width, doc, 0, 0);
|
2004-02-11 20:15:36 +03:00
|
|
|
|
2003-07-18 21:08:39 +04:00
|
|
|
for (box = doc->float_children; box != 0; box = box->next_float)
|
2003-04-15 21:53:00 +04:00
|
|
|
if (doc->height < box->y + box->height)
|
|
|
|
doc->height = box->y + box->height;
|
2002-05-28 03:21:11 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-02 03:22:59 +03:00
|
|
|
/**
|
|
|
|
* Layout the children of a box.
|
|
|
|
*
|
|
|
|
* \param box box to layout
|
|
|
|
* \param width horizontal space available
|
2004-02-11 20:15:36 +03:00
|
|
|
* \param cont ancestor box which defines horizontal space, for floats
|
2004-02-02 03:22:59 +03:00
|
|
|
* \param cx box position relative to cont
|
|
|
|
* \param cy box position relative to cont
|
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void layout_node(struct box *box, int width, struct box *cont,
|
|
|
|
int cx, int cy)
|
2002-06-29 00:14:04 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
int cy1, x0, x1, table_width;
|
|
|
|
struct box *left, *right;
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("box %p, width %i, cont %p, cx %i, cy %i",
|
|
|
|
box, width, cont, cx, cy));
|
2002-09-11 18:24:02 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
gui_multitask();
|
2004-02-02 03:22:59 +03:00
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
switch (box->type) {
|
|
|
|
case BOX_BLOCK:
|
2003-09-22 02:47:08 +04:00
|
|
|
case BOX_INLINE_BLOCK:
|
2004-02-11 20:15:36 +03:00
|
|
|
layout_block(box, cont, cx, cy);
|
2002-06-29 00:14:04 +04:00
|
|
|
break;
|
|
|
|
case BOX_INLINE_CONTAINER:
|
2004-02-11 20:15:36 +03:00
|
|
|
layout_inline_container(box, box->width, cont, cx, cy);
|
2002-06-29 00:14:04 +04:00
|
|
|
break;
|
|
|
|
case BOX_TABLE:
|
2004-02-11 20:15:36 +03:00
|
|
|
layout_table(box);
|
|
|
|
/* find sides and move table down if it doesn't fit
|
|
|
|
in available width */
|
|
|
|
cy1 = cy;
|
|
|
|
table_width = box->width;
|
|
|
|
while (1) {
|
|
|
|
x0 = cx;
|
|
|
|
x1 = cx + width;
|
|
|
|
find_sides(cont->float_children, cy1, cy1 + box->height,
|
|
|
|
&x0, &x1, &left, &right);
|
|
|
|
if (table_width <= x1 - x0)
|
|
|
|
break;
|
|
|
|
if (left == 0 && right == 0)
|
|
|
|
break;
|
|
|
|
/* move down to the next place where the space may increase */
|
|
|
|
if (left == 0)
|
|
|
|
cy1 = right->y + right->height + 1;
|
|
|
|
else if (right == 0)
|
|
|
|
cy1 = left->y + left->height + 1;
|
|
|
|
else if (left->y + left->height < right->y + right->height)
|
|
|
|
cy1 = left->y + left->height + 1;
|
|
|
|
else
|
|
|
|
cy1 = right->y + right->height + 1;
|
|
|
|
}
|
|
|
|
box->x = x0;
|
|
|
|
box->y += cy1 - cy;
|
2002-06-29 00:14:04 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-30 01:27:35 +03:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/**
|
2004-02-02 03:22:59 +03:00
|
|
|
* Layout the children of a block box.
|
2002-06-19 01:24:21 +04:00
|
|
|
*
|
2004-02-02 03:22:59 +03:00
|
|
|
* \param box block box to layout
|
2004-02-11 20:15:36 +03:00
|
|
|
* \param cont ancestor box which defines horizontal space, for floats
|
2004-02-02 03:22:59 +03:00
|
|
|
* \param cx box position relative to cont
|
|
|
|
* \param cy box position relative to cont
|
2002-06-19 01:24:21 +04:00
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void layout_block(struct box *box, struct box *cont, int cx, int cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
struct css_style *style = box->style;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2003-09-22 02:47:08 +04:00
|
|
|
assert(box->type == BOX_BLOCK || box->type == BOX_INLINE_BLOCK);
|
2002-08-18 20:46:45 +04:00
|
|
|
assert(style != 0);
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("box %p, cont %p, cx %i, cy %i", box, cont, cx, cy));
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2004-02-02 03:22:59 +03:00
|
|
|
layout_block_children(box, cont, cx, cy);
|
|
|
|
switch (style->height.height) {
|
|
|
|
case CSS_HEIGHT_LENGTH:
|
|
|
|
box->height = len(&style->height.length, style);
|
|
|
|
break;
|
|
|
|
case CSS_HEIGHT_AUTO:
|
|
|
|
default:
|
|
|
|
/* use the computed height */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2004-02-11 20:15:36 +03:00
|
|
|
* Compute dimensions of box, margins, paddings, and borders for a block-level
|
|
|
|
* element.
|
2004-02-02 03:22:59 +03:00
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void layout_block_find_dimensions(int available_width,
|
|
|
|
struct css_style *style, struct box *box)
|
2004-02-02 03:22:59 +03:00
|
|
|
{
|
|
|
|
int width;
|
2004-02-11 20:15:36 +03:00
|
|
|
int *margin = box->margin;
|
|
|
|
int *padding = box->padding;
|
|
|
|
int *border = box->border;
|
2004-02-02 03:22:59 +03:00
|
|
|
|
|
|
|
/* calculate box width */
|
2002-05-04 23:57:18 +04:00
|
|
|
switch (style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
2004-02-02 03:22:59 +03:00
|
|
|
width = len(&style->width.value.length, style);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
2004-02-02 03:22:59 +03:00
|
|
|
width = available_width * style->width.value.percent / 100;
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
2002-06-21 22:16:24 +04:00
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
default:
|
2004-02-02 03:22:59 +03:00
|
|
|
width = AUTO;
|
2002-06-21 22:16:24 +04:00
|
|
|
break;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2004-02-02 03:22:59 +03:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
layout_find_dimensions(available_width, style, margin, padding, border);
|
|
|
|
|
|
|
|
/* solve the width constraint as given in CSS 2.1 section 10.3.3 */
|
|
|
|
if (width == AUTO) {
|
|
|
|
/* any other 'auto' become 0 */
|
|
|
|
if (margin[LEFT] == AUTO)
|
|
|
|
margin[LEFT] = 0;
|
|
|
|
if (margin[RIGHT] == AUTO)
|
|
|
|
margin[RIGHT] = 0;
|
|
|
|
width = available_width -
|
|
|
|
(margin[LEFT] + border[LEFT] + padding[LEFT] +
|
|
|
|
padding[RIGHT] + border[RIGHT] + margin[RIGHT]);
|
|
|
|
} else if (margin[LEFT] == AUTO && margin[RIGHT] == AUTO) {
|
|
|
|
/* make the margins equal, centering the element */
|
|
|
|
margin[LEFT] = margin[RIGHT] = (available_width -
|
|
|
|
(border[LEFT] + padding[LEFT] + width +
|
|
|
|
padding[RIGHT] + border[RIGHT])) / 2;
|
|
|
|
} else if (margin[LEFT] == AUTO) {
|
|
|
|
margin[LEFT] = available_width -
|
|
|
|
(border[LEFT] + padding[LEFT] + width +
|
|
|
|
padding[RIGHT] + border[RIGHT] + margin[RIGHT]);
|
|
|
|
} else {
|
|
|
|
/* margin-right auto or "over-constained" */
|
|
|
|
margin[RIGHT] = available_width -
|
|
|
|
(margin[LEFT] + border[LEFT] + padding[LEFT] +
|
|
|
|
width + padding[RIGHT] + border[RIGHT]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (margin[TOP] == AUTO)
|
|
|
|
margin[TOP] = 0;
|
|
|
|
if (margin[BOTTOM] == AUTO)
|
|
|
|
margin[BOTTOM] = 0;
|
|
|
|
|
|
|
|
box->width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute dimensions of box, margins, paddings, and borders for a floating
|
|
|
|
* element.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void layout_float_find_dimensions(int available_width,
|
|
|
|
struct css_style *style, struct box *box)
|
|
|
|
{
|
|
|
|
layout_find_dimensions(available_width, style,
|
|
|
|
box->margin, box->padding, box->border);
|
|
|
|
|
|
|
|
if (box->margin[LEFT] == AUTO)
|
|
|
|
box->margin[LEFT] = 0;
|
|
|
|
if (box->margin[RIGHT] == AUTO)
|
|
|
|
box->margin[RIGHT] = 0;
|
|
|
|
|
|
|
|
/* calculate box width */
|
|
|
|
switch (style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
|
|
|
box->width = len(&style->width.value.length, style);
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
|
|
|
box->width = available_width *
|
|
|
|
style->width.value.percent / 100;
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
default:
|
|
|
|
/* CSS 2.1 section 10.3.5 */
|
|
|
|
available_width -= box->margin[LEFT] + box->border[LEFT] +
|
|
|
|
box->padding[LEFT] + box->padding[RIGHT] +
|
|
|
|
box->border[RIGHT] + box->margin[RIGHT];
|
|
|
|
if (box->min_width < available_width)
|
|
|
|
box->width = available_width;
|
|
|
|
else
|
|
|
|
box->width = box->min_width;
|
|
|
|
if (box->max_width < box->width)
|
|
|
|
box->width = box->max_width;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate size of margins, paddings, and borders.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void layout_find_dimensions(int available_width,
|
|
|
|
struct css_style *style,
|
|
|
|
int margin[4], int padding[4], int border[4])
|
|
|
|
{
|
|
|
|
unsigned int i;
|
2004-02-02 03:22:59 +03:00
|
|
|
for (i = 0; i != 4; i++) {
|
|
|
|
switch (style->margin[i].margin) {
|
|
|
|
case CSS_MARGIN_LENGTH:
|
|
|
|
margin[i] = len(&style->margin[i].value.length, style);
|
|
|
|
break;
|
|
|
|
case CSS_MARGIN_PERCENT:
|
|
|
|
margin[i] = available_width *
|
|
|
|
style->margin[i].value.percent / 100;
|
|
|
|
break;
|
|
|
|
case CSS_MARGIN_AUTO:
|
|
|
|
default:
|
|
|
|
margin[i] = AUTO;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (style->padding[i].padding) {
|
|
|
|
case CSS_MARGIN_PERCENT:
|
|
|
|
padding[i] = available_width *
|
|
|
|
style->padding[i].value.percent / 100;
|
|
|
|
break;
|
|
|
|
case CSS_PADDING_LENGTH:
|
|
|
|
default:
|
|
|
|
padding[i] = len(&style->padding[i].value.length, style);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (style->border[i].style == CSS_BORDER_STYLE_NONE ||
|
|
|
|
style->border[i].style == CSS_BORDER_STYLE_HIDDEN)
|
|
|
|
/* spec unclear: following Mozilla */
|
|
|
|
border[i] = 0;
|
|
|
|
else
|
|
|
|
border[i] = len(&style->border[i].width.value, style);
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/**
|
2004-02-02 03:22:59 +03:00
|
|
|
* Recursively layout block children.
|
|
|
|
*
|
|
|
|
* \param box block box to layout
|
2004-02-11 20:15:36 +03:00
|
|
|
* \param cont ancestor box which defines horizontal space, for floats
|
2004-02-02 03:22:59 +03:00
|
|
|
* \param cx box position relative to cont
|
|
|
|
* \param cy box position relative to cont
|
2002-06-19 01:24:21 +04:00
|
|
|
*
|
2004-02-02 03:22:59 +03:00
|
|
|
* box->width, box->margin, box->padding and box->border must be valid.
|
|
|
|
* box->height is filled in.
|
2002-06-19 01:24:21 +04:00
|
|
|
*/
|
|
|
|
|
2004-02-02 03:22:59 +03:00
|
|
|
void layout_block_children(struct box *box, struct box *cont,
|
2004-02-11 20:15:36 +03:00
|
|
|
int cx, int cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
2004-02-02 03:22:59 +03:00
|
|
|
struct box *c;
|
|
|
|
int width = box->width;
|
|
|
|
int y = box->padding[TOP];
|
2004-02-13 19:10:28 +03:00
|
|
|
int y1;
|
2004-02-20 03:42:36 +03:00
|
|
|
int vert_margin = 0;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2003-09-22 02:47:08 +04:00
|
|
|
assert(box->type == BOX_BLOCK || box->type == BOX_INLINE_BLOCK ||
|
|
|
|
box->type == BOX_FLOAT_LEFT || box->type == BOX_FLOAT_RIGHT ||
|
|
|
|
box->type == BOX_TABLE_CELL);
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("box %p, width %i, cont %p, cx %i, cy %i",
|
|
|
|
box, width, cont, cx, cy));
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
for (c = box->children; c != 0; c = c->next) {
|
2004-02-13 19:10:28 +03:00
|
|
|
if (c->style && c->style->clear != CSS_CLEAR_NONE) {
|
|
|
|
y1 = layout_clear(cont->float_children,
|
|
|
|
c->style->clear) - cy;
|
|
|
|
if (y < y1)
|
|
|
|
y = y1;
|
2002-06-21 22:16:24 +04:00
|
|
|
}
|
2002-06-29 00:14:04 +04:00
|
|
|
|
2004-02-02 03:22:59 +03:00
|
|
|
c->x = box->padding[LEFT];
|
2002-06-29 00:14:04 +04:00
|
|
|
c->y = y;
|
2004-02-11 20:15:36 +03:00
|
|
|
|
|
|
|
if (c->style) {
|
|
|
|
layout_block_find_dimensions(width, c->style, c);
|
|
|
|
c->x += c->margin[LEFT] + c->border[LEFT];
|
2004-02-20 03:42:36 +03:00
|
|
|
if (vert_margin < c->margin[TOP])
|
|
|
|
vert_margin = c->margin[TOP];
|
|
|
|
c->y += vert_margin + c->border[TOP];
|
2004-02-11 20:15:36 +03:00
|
|
|
} else {
|
|
|
|
c->width = box->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
layout_node(c, width, cont, cx + c->x, cy + c->y);
|
2004-02-02 03:22:59 +03:00
|
|
|
y = c->y + c->height + c->padding[TOP] + c->padding[BOTTOM] +
|
2004-02-20 03:42:36 +03:00
|
|
|
c->border[BOTTOM];
|
2003-09-09 02:47:11 +04:00
|
|
|
if (box->width < c->width)
|
|
|
|
box->width = c->width;
|
2004-02-20 03:42:36 +03:00
|
|
|
|
|
|
|
vert_margin = c->margin[BOTTOM];
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2004-02-02 03:22:59 +03:00
|
|
|
box->height = y - box->padding[TOP];
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-13 19:10:28 +03:00
|
|
|
/**
|
|
|
|
* 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, css_clear 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 + 1)
|
|
|
|
y = fl->y + fl->height + 1;
|
|
|
|
if ((clear == CSS_CLEAR_RIGHT || clear == CSS_CLEAR_BOTH) &&
|
|
|
|
fl->type == BOX_FLOAT_RIGHT)
|
|
|
|
if (y < fl->y + fl->height + 1)
|
|
|
|
y = fl->y + fl->height + 1;
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/**
|
2004-02-11 20:15:36 +03:00
|
|
|
* Find left and right edges in a vertical range.
|
2002-06-19 01:24:21 +04:00
|
|
|
*
|
2004-02-11 20:15:36 +03:00
|
|
|
* \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
|
2002-06-19 01:24:21 +04:00
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void find_sides(struct box *fl, int y0, int y1,
|
|
|
|
int *x0, int *x1, struct box **left, struct box **right)
|
2002-05-28 03:21:11 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
int fy0, fy1, fx0, fx1;
|
|
|
|
LOG(("y0 %i, y1 %i, x0 %i, x1 %i", y0, y1, *x0, *x1));
|
2002-06-19 01:24:21 +04:00
|
|
|
*left = *right = 0;
|
2002-05-28 03:21:11 +04:00
|
|
|
for (; fl; fl = fl->next_float) {
|
2004-02-11 20:15:36 +03:00
|
|
|
fy0 = fl->y;
|
|
|
|
fy1 = fl->y + fl->height;
|
|
|
|
if (y0 <= fy1 && fy0 <= y1) {
|
|
|
|
if (fl->type == BOX_FLOAT_LEFT) {
|
|
|
|
fx1 = fl->x + fl->width;
|
|
|
|
if (*x0 < fx1) {
|
|
|
|
*x0 = fx1;
|
|
|
|
*left = fl;
|
|
|
|
}
|
|
|
|
} else if (fl->type == BOX_FLOAT_RIGHT) {
|
|
|
|
fx0 = fl->x;
|
|
|
|
if (fx0 < *x1) {
|
|
|
|
*x1 = fx0;
|
|
|
|
*right = fl;
|
|
|
|
}
|
2002-06-19 01:24:21 +04:00
|
|
|
}
|
2002-05-28 03:21:11 +04:00
|
|
|
}
|
|
|
|
}
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("x0 %i, x1 %i, left %p, right %p", *x0, *x1, *left, *right));
|
2002-05-28 03:21:11 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/**
|
2004-02-11 20:15:36 +03:00
|
|
|
* Layout lines of text or inline boxes with floats.
|
2002-06-19 01:24:21 +04:00
|
|
|
*
|
2004-02-11 20:15:36 +03:00
|
|
|
* \param box inline container
|
|
|
|
* \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
|
2002-06-19 01:24:21 +04:00
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void layout_inline_container(struct box *box, int width,
|
|
|
|
struct box *cont, int cx, int cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
bool first_line = true;
|
|
|
|
struct box *c;
|
|
|
|
int y = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
assert(box->type == BOX_INLINE_CONTAINER);
|
2002-05-28 03:21:11 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("box %p, width %i, cont %p, cx %i, cy %i",
|
|
|
|
box, width, cont, cx, cy));
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
for (c = box->children; c; ) {
|
|
|
|
c = layout_line(c, width, &y, cx, cy + y, cont, first_line);
|
|
|
|
first_line = false;
|
2002-06-19 01:24:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
box->width = width;
|
|
|
|
box->height = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
/**
|
|
|
|
* Calculate line height from a style.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int line_height(struct css_style *style)
|
2002-06-19 01:24:21 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
assert(style);
|
2002-06-19 01:24:21 +04:00
|
|
|
assert(style->line_height.size == CSS_LINE_HEIGHT_LENGTH ||
|
2003-07-20 17:58:12 +04:00
|
|
|
style->line_height.size == CSS_LINE_HEIGHT_ABSOLUTE ||
|
|
|
|
style->line_height.size == CSS_LINE_HEIGHT_PERCENT);
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
switch (style->line_height.size) {
|
|
|
|
case CSS_LINE_HEIGHT_LENGTH:
|
|
|
|
return len(&style->line_height.value.length, style);
|
|
|
|
|
|
|
|
case CSS_LINE_HEIGHT_ABSOLUTE:
|
|
|
|
return style->line_height.value.absolute *
|
|
|
|
len(&style->font_size.value.length, 0);
|
|
|
|
|
|
|
|
case CSS_LINE_HEIGHT_PERCENT:
|
|
|
|
default:
|
|
|
|
return style->line_height.value.percent *
|
|
|
|
len(&style->font_size.value.length, 0)
|
|
|
|
/ 100.0;
|
|
|
|
}
|
2002-06-19 01:24:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
/**
|
|
|
|
* Position a line of boxes in inline formatting context.
|
|
|
|
*
|
|
|
|
* \param first box at start of line
|
|
|
|
* \param width available width
|
|
|
|
* \param y coordinate of top of line, updated on exit to bottom
|
|
|
|
* \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
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct box * layout_line(struct box *first, int width, int *y,
|
|
|
|
int cx, int cy, struct box *cont, bool indent)
|
2002-06-19 01:24:21 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
int height, used_height;
|
|
|
|
int x0 = 0;
|
|
|
|
int x1 = width;
|
|
|
|
int x, h, x_previous;
|
2002-06-19 01:24:21 +04:00
|
|
|
struct box * left;
|
|
|
|
struct box * right;
|
|
|
|
struct box * b;
|
2004-02-11 23:51:34 +03:00
|
|
|
struct box * c;
|
2002-06-19 01:24:21 +04:00
|
|
|
struct box * d;
|
2003-07-18 21:08:39 +04:00
|
|
|
struct box * fl;
|
2002-06-21 22:16:24 +04:00
|
|
|
int move_y = 0;
|
2004-02-11 20:15:36 +03:00
|
|
|
int space_before = 0, space_after = 0;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("first->text '%.*s', width %i, y %i, cy %i",
|
|
|
|
first->length, first->text, width, *y, cy));
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/* find sides at top of line */
|
2004-02-11 20:15:36 +03:00
|
|
|
x0 += cx;
|
|
|
|
x1 += cx;
|
2002-06-19 01:24:21 +04:00
|
|
|
find_sides(cont->float_children, cy, cy, &x0, &x1, &left, &right);
|
2004-02-11 20:15:36 +03:00
|
|
|
x0 -= cx;
|
|
|
|
x1 -= cx;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/* get minimum line height from containing block */
|
2003-09-22 02:47:08 +04:00
|
|
|
used_height = height = line_height(first->parent->parent->style);
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/* pass 1: find height of line assuming sides at top of line */
|
|
|
|
for (x = 0, b = first; x < x1 - x0 && b != 0; b = b->next) {
|
2003-09-22 02:47:08 +04:00
|
|
|
assert(b->type == BOX_INLINE || b->type == BOX_INLINE_BLOCK ||
|
|
|
|
b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT);
|
2002-06-19 01:24:21 +04:00
|
|
|
if (b->type == BOX_INLINE) {
|
2003-09-20 03:36:17 +04:00
|
|
|
if ((b->object || b->gadget) && b->style && b->style->height.height == CSS_HEIGHT_LENGTH)
|
2003-09-10 01:43:44 +04:00
|
|
|
h = len(&b->style->height.length, b->style);
|
2003-04-15 21:53:00 +04:00
|
|
|
else
|
2003-09-22 02:47:08 +04:00
|
|
|
h = line_height(b->style ? b->style : b->parent->parent->style);
|
2002-06-19 01:24:21 +04:00
|
|
|
b->height = h;
|
2003-01-07 02:53:40 +03:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
if (h > height) height = h;
|
2003-01-07 02:53:40 +03:00
|
|
|
|
2003-09-20 03:36:17 +04:00
|
|
|
if ((b->object || b->gadget) && b->style && b->style->width.width == CSS_WIDTH_LENGTH)
|
2003-09-10 01:43:44 +04:00
|
|
|
b->width = len(&b->style->width.value.length, b->style);
|
2003-09-20 03:36:17 +04:00
|
|
|
else if ((b->object || b->gadget) && b->style && b->style->width.width == CSS_WIDTH_PERCENT)
|
2003-09-10 01:43:44 +04:00
|
|
|
b->width = width * b->style->width.value.percent / 100;
|
|
|
|
else if (b->text) {
|
2004-02-11 20:15:36 +03:00
|
|
|
if (b->width == UNKNOWN_WIDTH)
|
2003-01-07 02:53:40 +03:00
|
|
|
b->width = font_width(b->font, b->text, b->length);
|
2003-09-20 03:36:17 +04:00
|
|
|
} else
|
2003-09-10 01:43:44 +04:00
|
|
|
b->width = 0;
|
2003-01-07 02:53:40 +03:00
|
|
|
|
|
|
|
if (b->text != 0)
|
2002-12-30 01:27:35 +03:00
|
|
|
x += b->width + b->space ? b->font->space_width : 0;
|
2003-01-07 02:53:40 +03:00
|
|
|
else
|
2002-12-30 01:27:35 +03:00
|
|
|
x += b->width;
|
2002-05-22 01:32:35 +04:00
|
|
|
}
|
2002-06-19 01:24:21 +04:00
|
|
|
}
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/* find new sides using this height */
|
2004-02-11 20:15:36 +03:00
|
|
|
x0 = cx;
|
|
|
|
x1 = cx + width;
|
2002-06-19 01:24:21 +04:00
|
|
|
find_sides(cont->float_children, cy, cy + height, &x0, &x1, &left, &right);
|
2004-02-11 20:15:36 +03:00
|
|
|
x0 -= cx;
|
|
|
|
x1 -= cx;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-02 01:42:40 +03:00
|
|
|
/* text-indent */
|
|
|
|
/* TODO - fix <BR> related b0rkage */
|
|
|
|
if (indent) {
|
|
|
|
switch (first->parent->parent->style->text_indent.size) {
|
|
|
|
case CSS_TEXT_INDENT_LENGTH:
|
|
|
|
x0 += len(&first->parent->parent->style->text_indent.value.length, first->parent->parent->style);
|
|
|
|
if (x0 + x > x1)
|
|
|
|
x1 = x0 + x;
|
|
|
|
break;
|
|
|
|
case CSS_TEXT_INDENT_PERCENT:
|
|
|
|
x0 += ((x1-x0) * first->parent->parent->style->text_indent.value.percent) / 100;
|
|
|
|
if (x0 + x > x1)
|
|
|
|
x1 = x0 + x;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-11 23:51:34 +03:00
|
|
|
c = first;
|
2002-06-19 01:24:21 +04:00
|
|
|
/* pass 2: place boxes in line */
|
2002-12-27 20:28:19 +03:00
|
|
|
for (x = x_previous = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) {
|
2003-09-22 02:47:08 +04:00
|
|
|
if (b->type == BOX_INLINE || b->type == BOX_INLINE_BLOCK) {
|
2004-02-11 20:15:36 +03:00
|
|
|
x_previous = x;
|
|
|
|
x += space_after;
|
|
|
|
b->x = x;
|
|
|
|
|
2003-09-27 03:22:00 +04:00
|
|
|
if (b->type == BOX_INLINE_BLOCK) {
|
2004-02-11 20:15:36 +03:00
|
|
|
layout_float_find_dimensions(width, b->style, b);
|
|
|
|
b->x += b->margin[LEFT] + b->border[LEFT];
|
|
|
|
layout_node(b, b->width, b, 0, 0);
|
2003-09-27 03:22:00 +04:00
|
|
|
/* increase height to contain any floats inside */
|
|
|
|
for (fl = b->float_children; fl != 0; fl = fl->next_float)
|
|
|
|
if (b->height < fl->y + fl->height)
|
|
|
|
b->height = fl->y + fl->height;
|
2004-02-11 20:15:36 +03:00
|
|
|
x = b->x + b->padding[LEFT] + b->width + b->padding[RIGHT] +
|
|
|
|
b->border[RIGHT] + b->margin[RIGHT];
|
|
|
|
} else
|
|
|
|
x += b->width;
|
|
|
|
|
2002-12-27 20:28:19 +03:00
|
|
|
space_before = space_after;
|
2003-09-10 01:43:44 +04:00
|
|
|
if (b->object)
|
|
|
|
space_after = 0;
|
|
|
|
else if (b->text)
|
2002-12-30 01:27:35 +03:00
|
|
|
space_after = b->space ? b->font->space_width : 0;
|
|
|
|
else
|
|
|
|
space_after = 0;
|
2002-06-19 01:24:21 +04:00
|
|
|
c = b;
|
2002-06-21 22:16:24 +04:00
|
|
|
move_y = 1;
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: '%.*s' %li %li\n", b->length, b->text, xp, x); */
|
2002-06-19 01:24:21 +04:00
|
|
|
} else {
|
2003-04-13 16:50:10 +04:00
|
|
|
/* float */
|
2002-06-29 00:14:04 +04:00
|
|
|
d = b->children;
|
|
|
|
d->float_children = 0;
|
2002-06-21 22:16:24 +04:00
|
|
|
/* css_dump_style(b->style); */
|
2004-02-11 20:15:36 +03:00
|
|
|
|
|
|
|
layout_float_find_dimensions(width, d->style, d);
|
|
|
|
|
|
|
|
layout_node(d, d->width, d, 0, 0);
|
2003-07-18 21:08:39 +04:00
|
|
|
/* increase height to contain any floats inside */
|
|
|
|
for (fl = d->float_children; fl != 0; fl = fl->next_float)
|
|
|
|
if (d->height < fl->y + fl->height)
|
|
|
|
d->height = fl->y + fl->height;
|
2004-02-11 20:15:36 +03:00
|
|
|
d->x = d->margin[LEFT] + d->border[LEFT];
|
|
|
|
d->y = d->margin[TOP] + d->border[TOP];
|
|
|
|
b->width = d->margin[LEFT] + d->border[LEFT] +
|
|
|
|
d->padding[LEFT] + d->width +
|
|
|
|
d->padding[RIGHT] + d->border[RIGHT] +
|
|
|
|
d->margin[RIGHT];
|
|
|
|
b->height = d->margin[TOP] + d->border[TOP] +
|
|
|
|
d->padding[TOP] + d->height +
|
|
|
|
d->padding[BOTTOM] + d->border[BOTTOM] +
|
|
|
|
d->margin[BOTTOM];
|
|
|
|
if (b->width < (x1 - x0) - x || (left == 0 && right == 0 && x == 0)) {
|
2002-06-19 01:24:21 +04:00
|
|
|
/* fits next to this line, or this line is empty with no floats */
|
2002-06-29 00:14:04 +04:00
|
|
|
if (b->type == BOX_FLOAT_LEFT) {
|
2002-06-19 01:24:21 +04:00
|
|
|
b->x = x0;
|
|
|
|
x0 += b->width;
|
|
|
|
left = b;
|
|
|
|
} else {
|
|
|
|
b->x = x1 - b->width;
|
|
|
|
x1 -= b->width;
|
|
|
|
right = b;
|
|
|
|
}
|
|
|
|
b->y = cy;
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: float fits %li %li, edges %li %li\n", */
|
|
|
|
/* b->x, b->y, x0, x1); */
|
2002-06-19 01:24:21 +04:00
|
|
|
} else {
|
|
|
|
/* doesn't fit: place below */
|
|
|
|
place_float_below(b, width, cy + height + 1, cont);
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: float doesn't fit %li %li\n", b->x, b->y); */
|
2002-06-19 01:24:21 +04:00
|
|
|
}
|
2004-02-20 03:42:36 +03:00
|
|
|
assert(cont->float_children != b);
|
2002-06-19 01:24:21 +04:00
|
|
|
b->next_float = cont->float_children;
|
|
|
|
cont->float_children = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x1 - x0 < x) {
|
|
|
|
/* the last box went over the end */
|
2003-01-07 02:53:40 +03:00
|
|
|
char * space = 0;
|
2004-02-11 20:15:36 +03:00
|
|
|
int w;
|
2002-06-19 01:24:21 +04:00
|
|
|
struct box * c2;
|
|
|
|
|
2002-12-27 20:28:19 +03:00
|
|
|
x = x_previous;
|
|
|
|
|
2003-09-27 03:22:00 +04:00
|
|
|
if (!c->object && !c->gadget && c->text)
|
2003-01-07 02:53:40 +03:00
|
|
|
space = strchr(c->text, ' ');
|
2003-02-09 15:58:15 +03:00
|
|
|
if (space != 0 && c->length <= (unsigned int) (space - c->text))
|
2002-12-27 20:28:19 +03:00
|
|
|
/* space after end of string */
|
|
|
|
space = 0;
|
|
|
|
|
2003-01-07 02:53:40 +03:00
|
|
|
/* space != 0 implies c->text != 0 */
|
|
|
|
|
2002-06-21 22:16:24 +04:00
|
|
|
if (space == 0)
|
2002-12-27 20:28:19 +03:00
|
|
|
w = c->width;
|
2003-01-07 02:53:40 +03:00
|
|
|
else
|
2003-02-09 15:58:15 +03:00
|
|
|
w = font_width(c->font, c->text, (unsigned int) (space - c->text));
|
2002-06-21 22:16:24 +04:00
|
|
|
|
2003-07-16 03:11:03 +04:00
|
|
|
if (x1 - x0 <= x + space_before + w && left == 0 && right == 0 && c == first) {
|
2002-06-19 01:24:21 +04:00
|
|
|
/* first word doesn't fit, but no floats and first on line so force in */
|
2002-06-21 22:16:24 +04:00
|
|
|
if (space == 0) {
|
2003-01-07 02:53:40 +03:00
|
|
|
/* only one word in this box or not text */
|
2002-06-21 22:16:24 +04:00
|
|
|
b = c->next;
|
|
|
|
} else {
|
2002-12-27 20:28:19 +03:00
|
|
|
/* cut off first word for this line */
|
2002-06-21 22:16:24 +04:00
|
|
|
c2 = memcpy(xcalloc(1, sizeof(struct box)), c, sizeof(struct box));
|
2002-09-08 22:11:56 +04:00
|
|
|
c2->text = xstrdup(space + 1);
|
2002-09-12 01:18:18 +04:00
|
|
|
c2->length = c->length - ((space + 1) - c->text);
|
2002-09-19 23:54:43 +04:00
|
|
|
c2->width = UNKNOWN_WIDTH;
|
2003-09-10 01:43:44 +04:00
|
|
|
c2->clone = 1;
|
2002-12-27 20:38:47 +03:00
|
|
|
c->length = space - c->text;
|
2002-12-27 20:28:19 +03:00
|
|
|
c->width = w;
|
|
|
|
c->space = 1;
|
2002-06-21 22:16:24 +04:00
|
|
|
c2->next = c->next;
|
|
|
|
c->next = c2;
|
2003-09-23 22:35:44 +04:00
|
|
|
c2->prev = c;
|
2003-09-24 01:48:22 +04:00
|
|
|
if (c2->next)
|
|
|
|
c2->next->prev = c2;
|
|
|
|
else
|
2003-09-23 22:35:44 +04:00
|
|
|
c2->parent->last = c2;
|
2002-06-21 22:16:24 +04:00
|
|
|
b = c2;
|
|
|
|
}
|
2002-12-27 20:28:19 +03:00
|
|
|
x += space_before + w;
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: overflow, forcing\n"); */
|
2003-07-16 03:11:03 +04:00
|
|
|
} else if (x1 - x0 <= x + space_before + w) {
|
2002-06-19 01:24:21 +04:00
|
|
|
/* first word doesn't fit, but full width not available so leave for later */
|
|
|
|
b = c;
|
2004-02-11 20:15:36 +03:00
|
|
|
assert(used_height);
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: overflow, leaving\n"); */
|
2003-01-07 02:53:40 +03:00
|
|
|
} else {
|
2002-06-19 01:24:21 +04:00
|
|
|
/* fit as many words as possible */
|
2002-06-21 22:16:24 +04:00
|
|
|
assert(space != 0);
|
2002-12-27 20:28:19 +03:00
|
|
|
space = font_split(c->font, c->text, c->length,
|
|
|
|
x1 - x0 - x - space_before, &w);
|
2003-02-09 15:58:15 +03:00
|
|
|
LOG(("'%.*s' %lu %u (%c) %u", (int) c->length, c->text,
|
2004-02-11 20:15:36 +03:00
|
|
|
(x1 - x0), space - c->text, *space, w));
|
2003-11-04 21:35:32 +03:00
|
|
|
/* assert(space != c->text); */
|
|
|
|
if (space == c->text)
|
|
|
|
space = c->text + 1;
|
2002-05-11 19:22:24 +04:00
|
|
|
c2 = memcpy(xcalloc(1, sizeof(struct box)), c, sizeof(struct box));
|
2002-09-08 22:11:56 +04:00
|
|
|
c2->text = xstrdup(space + 1);
|
2002-09-12 01:18:18 +04:00
|
|
|
c2->length = c->length - ((space + 1) - c->text);
|
2002-09-19 23:54:43 +04:00
|
|
|
c2->width = UNKNOWN_WIDTH;
|
2003-09-10 01:43:44 +04:00
|
|
|
c2->clone = 1;
|
2002-12-27 20:38:47 +03:00
|
|
|
c->length = space - c->text;
|
2002-12-27 20:28:19 +03:00
|
|
|
c->width = w;
|
|
|
|
c->space = 1;
|
2002-05-11 19:22:24 +04:00
|
|
|
c2->next = c->next;
|
|
|
|
c->next = c2;
|
2003-09-23 22:35:44 +04:00
|
|
|
c2->prev = c;
|
2003-09-24 01:48:22 +04:00
|
|
|
if (c2->next)
|
|
|
|
c2->next->prev = c2;
|
|
|
|
else
|
2003-09-23 22:35:44 +04:00
|
|
|
c2->parent->last = c2;
|
2002-06-19 01:24:21 +04:00
|
|
|
b = c2;
|
2002-12-27 20:28:19 +03:00
|
|
|
x += space_before + w;
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: overflow, fit\n"); */
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-06-21 22:16:24 +04:00
|
|
|
move_y = 1;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/* set positions */
|
2002-06-19 19:17:45 +04:00
|
|
|
switch (first->parent->parent->style->text_align) {
|
|
|
|
case CSS_TEXT_ALIGN_RIGHT: x0 = x1 - x; break;
|
|
|
|
case CSS_TEXT_ALIGN_CENTER: x0 = (x0 + (x1 - x)) / 2; break;
|
2002-06-26 16:19:24 +04:00
|
|
|
default: break; /* leave on left */
|
2002-06-19 19:17:45 +04:00
|
|
|
}
|
2004-02-02 01:42:40 +03:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
for (d = first; d != b; d = d->next) {
|
2003-09-22 02:47:08 +04:00
|
|
|
if (d->type == BOX_INLINE || d->type == BOX_INLINE_BLOCK) {
|
2002-06-19 01:24:21 +04:00
|
|
|
d->x += x0;
|
|
|
|
d->y = *y;
|
2004-02-11 20:15:36 +03:00
|
|
|
if (used_height < d->height)
|
2003-09-22 02:47:08 +04:00
|
|
|
used_height = d->height;
|
2002-06-19 01:24:21 +04:00
|
|
|
}
|
|
|
|
}
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2003-09-22 02:47:08 +04:00
|
|
|
if (move_y) *y += used_height + 1;
|
2002-06-19 01:24:21 +04:00
|
|
|
return b;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
/**
|
|
|
|
* Position a float in the first available space.
|
|
|
|
*
|
|
|
|
* \param c float box to position
|
|
|
|
* \param width available width
|
|
|
|
* \param y y coordinate relative to cont to place float below
|
|
|
|
* \param cont ancestor box which defines horizontal space, for floats
|
|
|
|
*/
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void place_float_below(struct box *c, int width, int y,
|
|
|
|
struct box *cont)
|
2002-06-19 01:24:21 +04:00
|
|
|
{
|
2004-02-11 20:15:36 +03:00
|
|
|
int x0, x1, yy = y;
|
2002-06-19 01:24:21 +04:00
|
|
|
struct box * left;
|
|
|
|
struct box * right;
|
|
|
|
do {
|
|
|
|
y = yy;
|
|
|
|
x0 = 0;
|
|
|
|
x1 = width;
|
|
|
|
find_sides(cont->float_children, y, y, &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) + 1;
|
|
|
|
} else if (left == 0 && right != 0) {
|
|
|
|
yy = right->y + right->height + 1;
|
|
|
|
} else if (left != 0 && right == 0) {
|
|
|
|
yy = left->y + left->height + 1;
|
|
|
|
}
|
2004-02-11 20:15:36 +03:00
|
|
|
} while (!((left == 0 && right == 0) || (c->width < x1 - x0)));
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
if (c->type == BOX_FLOAT_LEFT) {
|
2002-06-19 01:24:21 +04:00
|
|
|
c->x = x0;
|
|
|
|
} else {
|
|
|
|
c->x = x1 - c->width;
|
|
|
|
}
|
|
|
|
c->y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
/**
|
|
|
|
* layout a table
|
|
|
|
*/
|
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
void layout_table(struct box *table)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
2002-09-18 23:36:28 +04:00
|
|
|
unsigned int columns = table->columns; /* total columns */
|
2003-09-12 22:30:44 +04:00
|
|
|
unsigned int i;
|
2004-02-11 20:15:36 +03:00
|
|
|
unsigned int *row_span;
|
|
|
|
int *excess_y;
|
|
|
|
int table_width, min_width = 0, max_width = 0;
|
|
|
|
int required_width = 0;
|
|
|
|
int x;
|
|
|
|
int table_height = 0;
|
|
|
|
int *xs; /* array of column x positions */
|
2002-08-18 20:46:45 +04:00
|
|
|
struct box *c;
|
|
|
|
struct box *row;
|
|
|
|
struct box *row_group;
|
2003-07-07 01:10:12 +04:00
|
|
|
struct box **row_span_cell;
|
2003-07-18 21:08:39 +04:00
|
|
|
struct box *fl;
|
2003-09-12 02:02:06 +04:00
|
|
|
struct column col[table->columns];
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
assert(table->type == BOX_TABLE);
|
2002-08-18 20:46:45 +04:00
|
|
|
assert(table->style != 0);
|
2003-01-04 01:19:39 +03:00
|
|
|
assert(table->children != 0 && table->children->children != 0);
|
2003-09-12 22:30:44 +04:00
|
|
|
assert(columns != 0);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2004-02-11 20:15:36 +03:00
|
|
|
LOG(("table %p", table));
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2003-09-12 22:30:44 +04:00
|
|
|
memcpy(col, table->col, sizeof(col[0]) * columns);
|
2002-09-18 23:36:28 +04:00
|
|
|
|
2004-02-02 03:22:59 +03:00
|
|
|
table_width = table->width;
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2003-01-04 01:19:39 +03:00
|
|
|
LOG(("width %lu, min %lu, max %lu", table_width, table->min_width, table->max_width));
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i != columns; i++) {
|
|
|
|
if (col[i].type == COLUMN_WIDTH_FIXED)
|
2003-09-15 00:32:05 +04:00
|
|
|
required_width += col[i].width;
|
|
|
|
else if (col[i].type == COLUMN_WIDTH_PERCENT) {
|
2004-02-11 20:15:36 +03:00
|
|
|
int width = col[i].width * table_width / 100;
|
2003-09-15 00:32:05 +04:00
|
|
|
required_width += col[i].min < width ? width : col[i].min;
|
|
|
|
} else
|
|
|
|
required_width += col[i].min;
|
2003-09-12 22:30:44 +04:00
|
|
|
}
|
|
|
|
|
2003-09-15 00:32:05 +04:00
|
|
|
if (table_width < required_width) {
|
2003-09-12 22:30:44 +04:00
|
|
|
/* table narrower than required width for columns:
|
|
|
|
* treat percentage widths as maximums */
|
|
|
|
for (i = 0; i != columns; i++) {
|
|
|
|
if (col[i].type == COLUMN_WIDTH_PERCENT) {
|
|
|
|
col[i].max = table_width * col[i].width / 100;
|
|
|
|
if (col[i].max < col[i].min)
|
|
|
|
col[i].max = col[i].min;
|
|
|
|
}
|
|
|
|
min_width += col[i].min;
|
|
|
|
max_width += col[i].max;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* take percentages exactly */
|
|
|
|
for (i = 0; i != columns; i++) {
|
|
|
|
if (col[i].type == COLUMN_WIDTH_PERCENT) {
|
2004-02-11 20:15:36 +03:00
|
|
|
int width = table_width * col[i].width / 100;
|
2003-09-12 22:30:44 +04:00
|
|
|
if (width < col[i].min)
|
|
|
|
width = col[i].min;
|
|
|
|
col[i].min = col[i].width = col[i].max = width;
|
|
|
|
col[i].type = COLUMN_WIDTH_FIXED;
|
|
|
|
}
|
|
|
|
min_width += col[i].min;
|
|
|
|
max_width += col[i].max;
|
2002-09-19 23:54:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-09-12 18:37:26 +04:00
|
|
|
if (table_width <= min_width) {
|
2002-09-18 23:36:28 +04:00
|
|
|
/* not enough space: minimise column widths */
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i < columns; i++) {
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].width = col[i].min;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2003-09-12 18:37:26 +04:00
|
|
|
table_width = min_width;
|
2002-09-19 23:54:43 +04:00
|
|
|
} else if (max_width <= table_width) {
|
2002-12-28 01:29:45 +03:00
|
|
|
/* more space than maximum width */
|
|
|
|
if (table->style->width.width == CSS_WIDTH_AUTO) {
|
|
|
|
/* for auto-width tables, make columns max width */
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i < columns; i++) {
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].width = col[i].max;
|
2002-12-28 01:29:45 +03:00
|
|
|
}
|
|
|
|
table_width = max_width;
|
|
|
|
} else {
|
|
|
|
/* for fixed-width tables, distribute the extra space too */
|
2003-07-09 00:54:19 +04:00
|
|
|
unsigned int flexible_columns = 0;
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i != columns; i++)
|
2003-09-12 02:02:06 +04:00
|
|
|
if (col[i].type != COLUMN_WIDTH_FIXED)
|
2003-07-09 00:54:19 +04:00
|
|
|
flexible_columns++;
|
|
|
|
if (flexible_columns == 0) {
|
2004-02-11 20:15:36 +03:00
|
|
|
int extra = (table_width - max_width) / columns;
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i != columns; i++)
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].width = col[i].max + extra;
|
2003-07-09 00:54:19 +04:00
|
|
|
} else {
|
2004-02-11 20:15:36 +03:00
|
|
|
int extra = (table_width - max_width) / flexible_columns;
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i != columns; i++)
|
2003-09-12 02:02:06 +04:00
|
|
|
if (col[i].type != COLUMN_WIDTH_FIXED)
|
|
|
|
col[i].width = col[i].max + extra;
|
2002-12-28 01:29:45 +03:00
|
|
|
}
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|
2003-01-04 01:19:39 +03:00
|
|
|
} else {
|
|
|
|
/* space between min and max: fill it exactly */
|
2003-09-12 18:37:26 +04:00
|
|
|
float scale = (float) (table_width - min_width) /
|
|
|
|
(float) (max_width - min_width);
|
2002-09-27 01:38:33 +04:00
|
|
|
/* fprintf(stderr, "filling, scale %f\n", scale); */
|
2003-09-12 22:30:44 +04:00
|
|
|
for (i = 0; i < columns; i++) {
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].width = col[i].min +
|
|
|
|
(col[i].max - col[i].min) * scale;
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|
2003-01-04 01:19:39 +03:00
|
|
|
}
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
xs = xcalloc(columns + 1, sizeof(*xs));
|
2003-07-07 01:10:12 +04:00
|
|
|
row_span = xcalloc(columns, sizeof(row_span[0]));
|
|
|
|
excess_y = xcalloc(columns, sizeof(excess_y[0]));
|
|
|
|
row_span_cell = xcalloc(columns, sizeof(row_span_cell[0]));
|
2002-05-04 23:57:18 +04:00
|
|
|
xs[0] = x = 0;
|
2003-07-07 01:10:12 +04:00
|
|
|
for (i = 0; i != columns; i++) {
|
2003-09-12 02:02:06 +04:00
|
|
|
x += col[i].width;
|
2002-09-18 23:36:28 +04:00
|
|
|
xs[i + 1] = x;
|
2003-07-07 01:10:12 +04:00
|
|
|
row_span[i] = 0;
|
|
|
|
excess_y[i] = 0;
|
|
|
|
row_span_cell[i] = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2003-07-08 00:30:51 +04:00
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
/* position cells */
|
2002-08-18 20:46:45 +04:00
|
|
|
for (row_group = table->children; row_group != 0; row_group = row_group->next) {
|
2004-02-11 20:15:36 +03:00
|
|
|
int row_group_height = 0;
|
2002-08-18 20:46:45 +04:00
|
|
|
for (row = row_group->children; row != 0; row = row->next) {
|
2004-02-11 20:15:36 +03:00
|
|
|
int row_height = 0;
|
2003-07-07 01:10:12 +04:00
|
|
|
for (c = row->children; c != 0; c = c->next) {
|
2002-08-18 20:46:45 +04:00
|
|
|
assert(c->style != 0);
|
2003-07-07 01:10:12 +04:00
|
|
|
c->width = xs[c->start_column + c->columns] - xs[c->start_column];
|
2002-08-18 20:46:45 +04:00
|
|
|
c->float_children = 0;
|
2004-02-02 03:22:59 +03:00
|
|
|
layout_block_children(c, c, 0, 0);
|
2003-03-09 00:25:56 +03:00
|
|
|
if (c->style->height.height == CSS_HEIGHT_LENGTH) {
|
|
|
|
/* some sites use height="1" or similar to attempt
|
|
|
|
* to make cells as small as possible, so treat
|
|
|
|
* it as a minimum */
|
2004-02-11 20:15:36 +03:00
|
|
|
int h = len(&c->style->height.length, c->style);
|
|
|
|
if (c->height < h)
|
2003-03-09 00:25:56 +03:00
|
|
|
c->height = h;
|
|
|
|
}
|
2003-07-18 21:08:39 +04:00
|
|
|
/* increase height to contain any floats inside */
|
|
|
|
for (fl = c->float_children; fl != 0; fl = fl->next_float)
|
|
|
|
if (c->height < fl->y + fl->height)
|
|
|
|
c->height = fl->y + fl->height;
|
2003-07-07 01:10:12 +04:00
|
|
|
c->x = xs[c->start_column];
|
2002-08-18 20:46:45 +04:00
|
|
|
c->y = 0;
|
2003-07-07 01:10:12 +04:00
|
|
|
for (i = 0; i != c->columns; i++) {
|
|
|
|
row_span[c->start_column + i] = c->rows;
|
|
|
|
excess_y[c->start_column + i] = c->height;
|
|
|
|
row_span_cell[c->start_column + i] = 0;
|
|
|
|
}
|
|
|
|
row_span_cell[c->start_column] = c;
|
|
|
|
c->height = 0;
|
|
|
|
}
|
|
|
|
for (i = 0; i != columns; i++)
|
2003-07-08 22:49:14 +04:00
|
|
|
if (row_span[i] != 0)
|
|
|
|
row_span[i]--;
|
|
|
|
else
|
|
|
|
row_span_cell[i] = 0;
|
2003-08-30 03:15:54 +04:00
|
|
|
if (row->next || row_group->next) {
|
|
|
|
/* row height is greatest excess of a cell which ends in this row */
|
|
|
|
for (i = 0; i != columns; i++)
|
|
|
|
if (row_span[i] == 0 && row_height < excess_y[i])
|
|
|
|
row_height = excess_y[i];
|
|
|
|
} else {
|
|
|
|
/* except in the last row */
|
|
|
|
for (i = 0; i != columns; i++)
|
|
|
|
if (row_height < excess_y[i])
|
|
|
|
row_height = excess_y[i];
|
|
|
|
}
|
2003-07-07 01:10:12 +04:00
|
|
|
for (i = 0; i != columns; i++) {
|
2003-07-08 00:30:51 +04:00
|
|
|
if (row_height < excess_y[i])
|
|
|
|
excess_y[i] -= row_height;
|
|
|
|
else
|
|
|
|
excess_y[i] = 0;
|
2003-07-07 01:10:12 +04:00
|
|
|
if (row_span_cell[i] != 0)
|
|
|
|
row_span_cell[i]->height += row_height;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2003-07-07 01:10:12 +04:00
|
|
|
|
2002-08-18 20:46:45 +04:00
|
|
|
row->x = 0;
|
|
|
|
row->y = row_group_height;
|
|
|
|
row->width = table_width;
|
|
|
|
row->height = row_height;
|
|
|
|
row_group_height += row_height;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-08-18 20:46:45 +04:00
|
|
|
row_group->x = 0;
|
|
|
|
row_group->y = table_height;
|
|
|
|
row_group->width = table_width;
|
|
|
|
row_group->height = row_group_height;
|
|
|
|
table_height += row_group_height;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
|
2003-07-07 01:10:12 +04:00
|
|
|
xfree(row_span_cell);
|
|
|
|
xfree(excess_y);
|
|
|
|
xfree(row_span);
|
|
|
|
xfree(xs);
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
table->width = table_width;
|
2002-08-18 20:46:45 +04:00
|
|
|
table->height = table_height;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
|
2002-09-18 23:36:28 +04:00
|
|
|
|
|
|
|
/**
|
2004-02-23 01:22:50 +03:00
|
|
|
* Find min, max widths required by boxes.
|
|
|
|
*
|
|
|
|
* \param box top of tree of boxes
|
|
|
|
*
|
|
|
|
* The min_width and max_width fields of each box in the tree are computed.
|
2002-09-18 23:36:28 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void calculate_widths(struct box *box)
|
|
|
|
{
|
|
|
|
struct box *child;
|
2004-02-23 01:22:50 +03:00
|
|
|
int min = 0, max = 0, width, extra_fixed = 0;
|
|
|
|
float extra_frac = 0;
|
|
|
|
unsigned int side;
|
|
|
|
struct css_style *style = box->style;
|
2002-09-18 23:36:28 +04:00
|
|
|
|
|
|
|
assert(box->type == BOX_TABLE_CELL ||
|
2003-09-22 02:47:08 +04:00
|
|
|
box->type == BOX_BLOCK || box->type == BOX_INLINE_BLOCK ||
|
2002-09-18 23:36:28 +04:00
|
|
|
box->type == BOX_FLOAT_LEFT || box->type == BOX_FLOAT_RIGHT);
|
2004-02-23 01:22:50 +03:00
|
|
|
assert(style);
|
2002-09-18 23:36:28 +04:00
|
|
|
|
|
|
|
/* check if the widths have already been calculated */
|
2004-02-11 20:15:36 +03:00
|
|
|
if (box->max_width != UNKNOWN_MAX_WIDTH)
|
2002-09-18 23:36:28 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
for (child = box->children; child != 0; child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_BLOCK:
|
|
|
|
case BOX_TABLE:
|
2004-02-11 20:15:36 +03:00
|
|
|
if (child->type == BOX_TABLE)
|
|
|
|
calculate_table_widths(child);
|
|
|
|
else
|
|
|
|
calculate_widths(child);
|
2002-09-18 23:36:28 +04:00
|
|
|
if (child->style->width.width == CSS_WIDTH_LENGTH) {
|
|
|
|
width = len(&child->style->width.value.length,
|
|
|
|
child->style);
|
|
|
|
if (min < width) min = width;
|
|
|
|
if (max < width) max = width;
|
|
|
|
} else {
|
2004-02-11 20:15:36 +03:00
|
|
|
if (min < child->min_width) min = child->min_width;
|
|
|
|
if (max < child->max_width) max = child->max_width;
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
calculate_inline_container_widths(child);
|
2004-02-11 20:15:36 +03:00
|
|
|
if (min < child->min_width) min = child->min_width;
|
|
|
|
if (max < child->max_width) max = child->max_width;
|
2002-09-18 23:36:28 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-23 01:22:50 +03:00
|
|
|
/* add margins, border, padding to min, max widths */
|
|
|
|
for (side = 1; side != 5; side += 2) { /* RIGHT, LEFT */
|
|
|
|
if (style->padding[side].padding == CSS_PADDING_LENGTH)
|
|
|
|
extra_fixed += len(&style->padding[side].value.length,
|
|
|
|
style);
|
|
|
|
else if (style->padding[side].padding == CSS_PADDING_PERCENT)
|
|
|
|
extra_frac += style->padding[side].value.percent * 0.01;
|
|
|
|
|
|
|
|
if (style->border[side].style != CSS_BORDER_STYLE_NONE)
|
|
|
|
extra_fixed += len(&style->border[side].width.value,
|
|
|
|
style);
|
|
|
|
|
|
|
|
if (style->margin[side].margin == CSS_MARGIN_LENGTH)
|
|
|
|
extra_fixed += len(&style->margin[side].value.length,
|
|
|
|
style);
|
|
|
|
else if (style->margin[side].margin == CSS_MARGIN_PERCENT)
|
|
|
|
extra_frac += style->margin[side].value.percent * 0.01;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (1.0 <= extra_frac)
|
|
|
|
extra_frac = 0.9;
|
|
|
|
|
|
|
|
box->min_width = (min + extra_fixed) / (1.0 - extra_frac);
|
|
|
|
box->max_width = (max + extra_fixed) / (1.0 - extra_frac);
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void calculate_inline_container_widths(struct box *box)
|
|
|
|
{
|
|
|
|
struct box *child;
|
2004-02-11 20:15:36 +03:00
|
|
|
int min = 0, max = 0, width;
|
|
|
|
unsigned int i, j;
|
2002-09-18 23:36:28 +04:00
|
|
|
|
|
|
|
for (child = box->children; child != 0; child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_INLINE:
|
2003-09-20 03:36:17 +04:00
|
|
|
if (child->object || child->gadget) {
|
2003-09-10 01:43:44 +04:00
|
|
|
if (child->style->width.width == CSS_WIDTH_LENGTH) {
|
|
|
|
child->width = len(&child->style->width.value.length,
|
|
|
|
child->style);
|
|
|
|
max += child->width;
|
2004-02-11 20:15:36 +03:00
|
|
|
if (min < child->width)
|
2003-09-10 01:43:44 +04:00
|
|
|
min = child->width;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (child->text) {
|
2003-01-07 02:53:40 +03:00
|
|
|
/* max = all one line */
|
|
|
|
child->width = font_width(child->font,
|
|
|
|
child->text, child->length);
|
|
|
|
max += child->width;
|
2003-09-27 03:22:00 +04:00
|
|
|
if (child->next && child->space)
|
|
|
|
max += child->font->space_width;
|
2003-01-07 02:53:40 +03:00
|
|
|
|
|
|
|
/* min = widest word */
|
2003-09-15 00:32:05 +04:00
|
|
|
i = 0;
|
|
|
|
do {
|
2004-02-11 20:15:36 +03:00
|
|
|
for (j = i; j != child->length && child->text[j] != ' '; j++)
|
2003-09-15 00:32:05 +04:00
|
|
|
;
|
2004-02-11 20:15:36 +03:00
|
|
|
width = font_width(child->font, child->text + i, (j - i));
|
2003-01-07 02:53:40 +03:00
|
|
|
if (min < width) min = width;
|
2003-09-15 00:32:05 +04:00
|
|
|
i = j + 1;
|
2004-02-11 20:15:36 +03:00
|
|
|
} while (j != child->length);
|
2002-12-30 05:06:03 +03:00
|
|
|
}
|
2002-09-18 23:36:28 +04:00
|
|
|
break;
|
|
|
|
|
2003-09-22 02:47:08 +04:00
|
|
|
case BOX_INLINE_BLOCK:
|
2004-02-11 20:15:36 +03:00
|
|
|
calculate_widths(child);
|
2003-10-02 02:48:39 +04:00
|
|
|
if (child->style != 0 &&
|
|
|
|
child->style->width.width == CSS_WIDTH_LENGTH) {
|
|
|
|
width = len(&child->style->width.value.length,
|
|
|
|
child->style);
|
|
|
|
if (min < width) min = width;
|
|
|
|
max += width;
|
|
|
|
} else {
|
2004-02-11 20:15:36 +03:00
|
|
|
if (min < child->min_width) min = child->min_width;
|
2003-10-02 02:48:39 +04:00
|
|
|
max += child->max_width;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2002-09-18 23:36:28 +04:00
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
2004-02-11 20:15:36 +03:00
|
|
|
calculate_widths(child);
|
2002-09-18 23:36:28 +04:00
|
|
|
if (child->style != 0 &&
|
|
|
|
child->style->width.width == CSS_WIDTH_LENGTH) {
|
|
|
|
width = len(&child->style->width.value.length,
|
|
|
|
child->style);
|
|
|
|
if (min < width) min = width;
|
|
|
|
if (max < width) max = width;
|
|
|
|
} else {
|
2004-02-11 20:15:36 +03:00
|
|
|
if (min < child->min_width) min = child->min_width;
|
|
|
|
if (max < child->max_width) max = child->max_width;
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-08 01:34:39 +04:00
|
|
|
if (box->parent && box->parent->style &&
|
|
|
|
(box->parent->style->white_space == CSS_WHITE_SPACE_PRE ||
|
|
|
|
box->parent->style->white_space == CSS_WHITE_SPACE_NOWRAP))
|
|
|
|
min = max;
|
|
|
|
|
2003-09-15 00:32:05 +04:00
|
|
|
assert(min <= max);
|
2002-09-18 23:36:28 +04:00
|
|
|
box->min_width = min;
|
|
|
|
box->max_width = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void calculate_table_widths(struct box *table)
|
|
|
|
{
|
2003-09-12 02:02:06 +04:00
|
|
|
unsigned int i, j;
|
2002-09-18 23:36:28 +04:00
|
|
|
struct box *row_group, *row, *cell;
|
2004-02-11 20:15:36 +03:00
|
|
|
int width, min_width = 0, max_width = 0;
|
2003-08-30 23:20:19 +04:00
|
|
|
struct column *col;
|
2002-09-18 23:36:28 +04:00
|
|
|
|
2003-01-04 01:19:39 +03:00
|
|
|
LOG(("table %p, columns %u", table, table->columns));
|
2002-09-18 23:36:28 +04:00
|
|
|
|
2003-08-30 23:20:19 +04:00
|
|
|
/* check if the widths have already been calculated */
|
2004-02-11 20:15:36 +03:00
|
|
|
if (table->max_width != UNKNOWN_MAX_WIDTH)
|
2003-08-30 23:20:19 +04:00
|
|
|
return;
|
|
|
|
|
2003-10-09 00:49:57 +04:00
|
|
|
free(table->col);
|
|
|
|
table->col = col = xcalloc(table->columns, sizeof(*col));
|
2003-09-09 23:25:28 +04:00
|
|
|
|
2002-09-18 23:36:28 +04:00
|
|
|
assert(table->children != 0 && table->children->children != 0);
|
2003-09-12 02:02:06 +04:00
|
|
|
|
|
|
|
/* 1st pass: consider cells with colspan 1 only */
|
2002-09-18 23:36:28 +04:00
|
|
|
for (row_group = table->children; row_group != 0; row_group = row_group->next) {
|
|
|
|
assert(row_group->type == BOX_TABLE_ROW_GROUP);
|
|
|
|
for (row = row_group->children; row != 0; row = row->next) {
|
|
|
|
assert(row->type == BOX_TABLE_ROW);
|
2003-07-07 01:10:12 +04:00
|
|
|
for (cell = row->children; cell != 0; cell = cell->next) {
|
2002-09-18 23:36:28 +04:00
|
|
|
assert(cell->type == BOX_TABLE_CELL);
|
|
|
|
assert(cell->style != 0);
|
2002-12-27 21:58:03 +03:00
|
|
|
|
2003-09-12 02:02:06 +04:00
|
|
|
if (cell->columns != 1)
|
|
|
|
continue;
|
|
|
|
|
2003-01-04 01:19:39 +03:00
|
|
|
calculate_widths(cell);
|
2003-07-07 01:10:12 +04:00
|
|
|
i = cell->start_column;
|
2002-12-27 21:58:03 +03:00
|
|
|
|
2003-09-12 02:02:06 +04:00
|
|
|
if (col[i].type == COLUMN_WIDTH_FIXED) {
|
2004-02-11 20:15:36 +03:00
|
|
|
if (col[i].width < cell->min_width)
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].min = col[i].width = col[i].max = cell->min_width;
|
|
|
|
continue;
|
2003-04-10 01:57:09 +04:00
|
|
|
}
|
2003-07-09 00:54:19 +04:00
|
|
|
|
2003-09-12 02:02:06 +04:00
|
|
|
/* update column min, max widths using cell widths */
|
2004-02-11 20:15:36 +03:00
|
|
|
if (col[i].min < cell->min_width)
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].min = cell->min_width;
|
2004-02-11 20:15:36 +03:00
|
|
|
if (col[i].max < cell->max_width)
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i].max = cell->max_width;
|
|
|
|
|
2003-04-10 01:57:09 +04:00
|
|
|
if (col[i].type != COLUMN_WIDTH_FIXED &&
|
2003-09-12 02:02:06 +04:00
|
|
|
cell->style->width.width == CSS_WIDTH_LENGTH) {
|
|
|
|
/* fixed width cell => fixed width column */
|
|
|
|
col[i].type = COLUMN_WIDTH_FIXED;
|
2003-04-10 01:57:09 +04:00
|
|
|
width = len(&cell->style->width.value.length,
|
|
|
|
cell->style);
|
2003-09-12 02:02:06 +04:00
|
|
|
if (width < col[i].min)
|
|
|
|
/* if the given width is too small, give
|
|
|
|
* the column its minimum width */
|
|
|
|
width = col[i].min;
|
|
|
|
col[i].min = col[i].width = col[i].max = width;
|
2003-04-10 01:57:09 +04:00
|
|
|
|
2003-09-12 02:02:06 +04:00
|
|
|
} else if (col[i].type == COLUMN_WIDTH_UNKNOWN) {
|
2003-04-10 01:57:09 +04:00
|
|
|
if (cell->style->width.width == CSS_WIDTH_PERCENT) {
|
|
|
|
col[i].type = COLUMN_WIDTH_PERCENT;
|
|
|
|
col[i].width = cell->style->width.value.percent;
|
|
|
|
} else if (cell->style->width.width == CSS_WIDTH_AUTO) {
|
|
|
|
col[i].type = COLUMN_WIDTH_AUTO;
|
|
|
|
}
|
|
|
|
}
|
2003-09-12 02:02:06 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-10 01:57:09 +04:00
|
|
|
|
2003-09-12 02:02:06 +04:00
|
|
|
/* 2nd pass: cells which span multiple columns */
|
|
|
|
for (row_group = table->children; row_group != 0; row_group = row_group->next) {
|
|
|
|
for (row = row_group->children; row != 0; row = row->next) {
|
|
|
|
for (cell = row->children; cell != 0; cell = cell->next) {
|
|
|
|
unsigned int flexible_columns = 0;
|
2004-02-11 20:15:36 +03:00
|
|
|
int min = 0, max = 0, fixed_width = 0;
|
2003-09-12 02:02:06 +04:00
|
|
|
signed long extra;
|
|
|
|
|
|
|
|
if (cell->columns == 1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
calculate_widths(cell);
|
|
|
|
i = cell->start_column;
|
|
|
|
|
|
|
|
/* find min, max width so far of spanned columns */
|
2003-04-10 01:57:09 +04:00
|
|
|
for (j = 0; j != cell->columns; j++) {
|
2003-09-12 02:02:06 +04:00
|
|
|
min += col[i + j].min;
|
|
|
|
max += col[i + j].max;
|
|
|
|
if (col[i + j].type == COLUMN_WIDTH_FIXED)
|
|
|
|
fixed_width += col[i + j].width;
|
|
|
|
else
|
2003-01-04 01:19:39 +03:00
|
|
|
flexible_columns++;
|
|
|
|
}
|
2003-04-10 01:57:09 +04:00
|
|
|
|
2003-09-12 02:02:06 +04:00
|
|
|
if (cell->style->width.width == CSS_WIDTH_LENGTH &&
|
|
|
|
flexible_columns) {
|
|
|
|
/* cell is fixed width, and not all the spanned columns
|
|
|
|
* are fixed width, so split difference between spanned
|
|
|
|
* columns which aren't fixed width yet */
|
|
|
|
width = len(&cell->style->width.value.length,
|
|
|
|
cell->style);
|
2004-02-11 20:15:36 +03:00
|
|
|
if (width < cell->min_width)
|
2003-09-12 02:02:06 +04:00
|
|
|
width = cell->min_width;
|
|
|
|
extra = width - fixed_width;
|
|
|
|
for (j = 0; j != cell->columns; j++)
|
|
|
|
if (col[i + j].type != COLUMN_WIDTH_FIXED)
|
|
|
|
extra -= col[i + j].min;
|
|
|
|
if (0 < extra)
|
|
|
|
extra = 1 + extra / flexible_columns;
|
|
|
|
else
|
|
|
|
extra = 0;
|
|
|
|
for (j = 0; j != cell->columns; j++) {
|
|
|
|
if (col[i + j].type != COLUMN_WIDTH_FIXED) {
|
2003-11-04 21:35:32 +03:00
|
|
|
col[i + j].width = col[i + j].max =
|
2003-09-12 02:02:06 +04:00
|
|
|
col[i + j].min += extra;
|
|
|
|
col[i + j].type = COLUMN_WIDTH_FIXED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* distribute extra min, max to spanned columns */
|
2004-02-11 20:15:36 +03:00
|
|
|
if (min < cell->min_width) {
|
2003-01-04 01:19:39 +03:00
|
|
|
if (flexible_columns == 0) {
|
2002-12-27 21:58:03 +03:00
|
|
|
extra = 1 + (cell->min_width - min)
|
|
|
|
/ cell->columns;
|
2003-09-12 02:02:06 +04:00
|
|
|
for (j = 0; j != cell->columns; j++)
|
|
|
|
col[i + j].min = col[i + j].width =
|
|
|
|
col[i + j].max += extra;
|
2003-01-04 01:19:39 +03:00
|
|
|
} else {
|
|
|
|
extra = 1 + (cell->min_width - min)
|
|
|
|
/ flexible_columns;
|
|
|
|
max = 0;
|
|
|
|
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;
|
|
|
|
max += col[i + j].max;
|
|
|
|
}
|
|
|
|
}
|
2002-12-27 21:58:03 +03:00
|
|
|
}
|
2003-01-04 01:19:39 +03:00
|
|
|
}
|
2004-02-11 20:15:36 +03:00
|
|
|
if (max < cell->max_width && flexible_columns != 0) {
|
2003-09-12 02:02:06 +04:00
|
|
|
extra = 1 + (cell->max_width - max)
|
|
|
|
/ flexible_columns;
|
|
|
|
for (j = 0; j != cell->columns; j++)
|
|
|
|
if (col[i + j].type != COLUMN_WIDTH_FIXED)
|
2003-01-04 01:19:39 +03:00
|
|
|
col[i + j].max += extra;
|
|
|
|
}
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < table->columns; i++) {
|
2003-04-10 01:57:09 +04:00
|
|
|
LOG(("col %u, type %i, min %lu, max %lu, width %lu",
|
|
|
|
i, col[i].type, col[i].min, col[i].max, col[i].width));
|
2003-01-04 01:19:39 +03:00
|
|
|
assert(col[i].min <= col[i].max);
|
2002-09-18 23:36:28 +04:00
|
|
|
min_width += col[i].min;
|
|
|
|
max_width += col[i].max;
|
|
|
|
}
|
|
|
|
table->min_width = min_width;
|
|
|
|
table->max_width = max_width;
|
2003-01-04 01:19:39 +03:00
|
|
|
|
|
|
|
LOG(("min_width %lu, max_width %lu", min_width, max_width));
|
2002-09-18 23:36:28 +04:00
|
|
|
}
|