2002-05-04 23:57:18 +04:00
|
|
|
/**
|
2002-09-19 23:54:43 +04:00
|
|
|
* $Id: layout.c,v 1.18 2002/09/19 19:54:43 bursa Exp $
|
2002-05-04 23:57:18 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "libxml/HTMLparser.h"
|
2002-08-06 00:34:45 +04:00
|
|
|
#include "netsurf/render/css.h"
|
|
|
|
#include "netsurf/render/font.h"
|
|
|
|
#include "netsurf/render/box.h"
|
|
|
|
#include "netsurf/render/utils.h"
|
|
|
|
#include "netsurf/render/layout.h"
|
|
|
|
|
2002-08-18 20:46:45 +04:00
|
|
|
/* #define DEBUG_LAYOUT */
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* internal functions
|
|
|
|
*/
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
signed long len(struct css_length * length, struct css_style * style);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2002-08-06 00:34:45 +04:00
|
|
|
void layout_node(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy);
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_block(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy);
|
|
|
|
unsigned long layout_block_children(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy);
|
|
|
|
void find_sides(struct box * fl, unsigned long y0, unsigned long y1,
|
2002-06-19 01:24:21 +04:00
|
|
|
unsigned long * x0, unsigned long * x1, struct box ** left, struct box ** right);
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_inline_container(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy);
|
2002-06-19 01:24:21 +04:00
|
|
|
signed long line_height(struct css_style * style);
|
|
|
|
struct box * layout_line(struct box * first, unsigned long width, unsigned long * y,
|
|
|
|
unsigned long cy, struct box * cont);
|
|
|
|
void place_float_below(struct box * c, unsigned long width, unsigned long y, struct box * cont);
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_table(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy);
|
2002-09-18 23:36:28 +04:00
|
|
|
void calculate_widths(struct box *box);
|
|
|
|
void calculate_inline_container_widths(struct box *box);
|
|
|
|
void calculate_table_widths(struct box *table);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* convert a struct css_length to pixels
|
|
|
|
*/
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
signed long len(struct css_length * length, struct css_style * style)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
2002-06-19 01:24:21 +04:00
|
|
|
assert(!((length->unit == CSS_UNIT_EM || length->unit == CSS_UNIT_EX) && style == 0));
|
2002-05-04 23:57:18 +04:00
|
|
|
switch (length->unit) {
|
2002-06-19 01:24:21 +04:00
|
|
|
case CSS_UNIT_EM: return length->value * len(&style->font_size.value.length, 0);
|
|
|
|
case CSS_UNIT_EX: return length->value * len(&style->font_size.value.length, 0) * 0.6;
|
2002-05-04 23:57:18 +04:00
|
|
|
case CSS_UNIT_PX: return length->value;
|
|
|
|
case CSS_UNIT_IN: return length->value * 90.0;
|
|
|
|
case CSS_UNIT_CM: return length->value * 35.0;
|
|
|
|
case CSS_UNIT_MM: return length->value * 3.5;
|
|
|
|
case CSS_UNIT_PT: return length->value * 90.0 / 72.0;
|
|
|
|
case CSS_UNIT_PC: return length->value * 90.0 / 6.0;
|
2002-06-26 16:19:24 +04:00
|
|
|
default: break;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* layout algorithm
|
|
|
|
*/
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/**
|
|
|
|
* layout_document -- calculate positions of boxes in a document
|
|
|
|
*
|
|
|
|
* doc root of document box tree
|
|
|
|
* width page width
|
|
|
|
*/
|
|
|
|
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_document(struct box * doc, unsigned long width)
|
|
|
|
{
|
|
|
|
doc->float_children = 0;
|
2002-06-29 00:14:04 +04:00
|
|
|
layout_node(doc, width, doc, 0, 0);
|
2002-05-28 03:21:11 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
void layout_node(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy)
|
|
|
|
{
|
2002-08-06 00:34:45 +04:00
|
|
|
#ifdef DEBUG_LAYOUT
|
|
|
|
fprintf(stderr, "layout_node(%p, %lu, %p, %lu, %lu)\n",
|
|
|
|
box, width, cont, cx, cy);
|
|
|
|
#endif
|
|
|
|
|
2002-09-11 18:24:02 +04:00
|
|
|
gui_multitask();
|
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
switch (box->type) {
|
|
|
|
case BOX_BLOCK:
|
|
|
|
layout_block(box, width, cont, cx, cy);
|
|
|
|
break;
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
layout_inline_container(box, width, cont, cx, cy);
|
|
|
|
break;
|
|
|
|
case BOX_TABLE:
|
|
|
|
layout_table(box, width, cont, cx, cy);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/**
|
|
|
|
* layout_block -- position block and recursively layout children
|
|
|
|
*
|
|
|
|
* box block box to layout
|
|
|
|
* width horizontal space available
|
|
|
|
* cont ancestor box which defines horizontal space, for inlines
|
|
|
|
* cx, cy box position relative to cont
|
|
|
|
*/
|
|
|
|
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_block(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
|
|
|
struct css_style * style = box->style;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
assert(box->type == BOX_BLOCK);
|
2002-08-18 20:46:45 +04:00
|
|
|
assert(style != 0);
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-08-06 00:34:45 +04:00
|
|
|
#ifdef DEBUG_LAYOUT
|
|
|
|
fprintf(stderr, "layout_block(%p, %lu, %p, %lu, %lu)\n",
|
|
|
|
box, width, cont, cx, cy);
|
|
|
|
#endif
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
switch (style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
2002-06-19 01:24:21 +04:00
|
|
|
box->width = len(&style->width.value.length, box->style);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
|
|
|
box->width = width * style->width.value.percent / 100;
|
|
|
|
break;
|
2002-06-21 22:16:24 +04:00
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
default:
|
|
|
|
/* take all available width */
|
|
|
|
box->width = width;
|
|
|
|
break;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-05-28 03:21:11 +04:00
|
|
|
box->height = layout_block_children(box, box->width, cont, cx, cy);
|
2002-05-04 23:57:18 +04:00
|
|
|
switch (style->height.height) {
|
|
|
|
case CSS_HEIGHT_LENGTH:
|
2002-06-19 01:24:21 +04:00
|
|
|
box->height = len(&style->height.length, box->style);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
2002-09-18 23:36:28 +04:00
|
|
|
case CSS_HEIGHT_AUTO:
|
|
|
|
default:
|
|
|
|
/* use the computed height */
|
|
|
|
break;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* layout_block_children -- recursively layout block children
|
|
|
|
*
|
|
|
|
* (as above)
|
|
|
|
*/
|
|
|
|
|
2002-05-28 03:21:11 +04:00
|
|
|
unsigned long layout_block_children(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
|
|
|
struct box * c;
|
2002-05-11 19:22:24 +04:00
|
|
|
unsigned long y = 0;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
assert(box->type == BOX_BLOCK || box->type == BOX_FLOAT_LEFT ||
|
|
|
|
box->type == BOX_FLOAT_RIGHT || box->type == BOX_TABLE_CELL);
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-08-06 00:34:45 +04:00
|
|
|
#ifdef DEBUG_LAYOUT
|
|
|
|
fprintf(stderr, "layout_block_children(%p, %lu, %p, %lu, %lu)\n",
|
|
|
|
box, width, cont, cx, cy);
|
|
|
|
#endif
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
for (c = box->children; c != 0; c = c->next) {
|
2002-08-18 20:46:45 +04:00
|
|
|
if (c->style != 0 && c->style->clear != CSS_CLEAR_NONE) {
|
2002-06-21 22:16:24 +04:00
|
|
|
unsigned long x0, x1;
|
|
|
|
struct box * left, * right;
|
|
|
|
do {
|
|
|
|
x0 = cx;
|
|
|
|
x1 = cx + width;
|
|
|
|
find_sides(cont->float_children, cy + y, cy + y,
|
|
|
|
&x0, &x1, &left, &right);
|
|
|
|
if ((c->style->clear == CSS_CLEAR_LEFT || c->style->clear == CSS_CLEAR_BOTH)
|
|
|
|
&& left != 0)
|
|
|
|
y = left->y + left->height - cy + 1;
|
|
|
|
if ((c->style->clear == CSS_CLEAR_RIGHT || c->style->clear == CSS_CLEAR_BOTH)
|
|
|
|
&& right != 0)
|
|
|
|
if (cy + y < right->y + right->height + 1)
|
|
|
|
y = right->y + right->height - cy + 1;
|
|
|
|
} while ((c->style->clear == CSS_CLEAR_LEFT && left != 0) ||
|
|
|
|
(c->style->clear == CSS_CLEAR_RIGHT && right != 0) ||
|
|
|
|
(c->style->clear == CSS_CLEAR_BOTH && (left != 0 || right != 0)));
|
|
|
|
}
|
2002-06-29 00:14:04 +04:00
|
|
|
|
|
|
|
layout_node(c, width, cont, cx, cy + y);
|
|
|
|
c->x = 0;
|
|
|
|
c->y = y;
|
|
|
|
y += c->height;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* find_sides -- find left and right margins
|
|
|
|
*
|
|
|
|
* fl first float in float list
|
|
|
|
* y0, y1 y range to search
|
|
|
|
* x1, x1 margins updated
|
|
|
|
* left float on left if present
|
|
|
|
* right float on right if present
|
|
|
|
*/
|
|
|
|
|
2002-05-28 03:21:11 +04:00
|
|
|
void find_sides(struct box * fl, unsigned long y0, unsigned long y1,
|
2002-06-19 01:24:21 +04:00
|
|
|
unsigned long * x0, unsigned long * x1, struct box ** left, struct box ** right)
|
2002-05-28 03:21:11 +04:00
|
|
|
{
|
2002-06-21 22:16:24 +04:00
|
|
|
/* fprintf(stderr, "find_sides: y0 %li y1 %li x0 %li x1 %li => ", 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) {
|
|
|
|
if (y0 <= fl->y + fl->height && fl->y <= y1) {
|
2002-06-29 00:14:04 +04:00
|
|
|
if (fl->type == BOX_FLOAT_LEFT && *x0 < fl->x + fl->width) {
|
2002-05-28 03:21:11 +04:00
|
|
|
*x0 = fl->x + fl->width;
|
2002-06-19 01:24:21 +04:00
|
|
|
*left = fl;
|
2002-06-29 00:14:04 +04:00
|
|
|
} else if (fl->type == BOX_FLOAT_RIGHT && fl->x < *x1) {
|
2002-05-28 03:21:11 +04:00
|
|
|
*x1 = fl->x;
|
2002-06-19 01:24:21 +04:00
|
|
|
*right = fl;
|
|
|
|
}
|
2002-05-28 03:21:11 +04:00
|
|
|
}
|
|
|
|
}
|
2002-06-21 22:16:24 +04:00
|
|
|
/* fprintf(stderr, "x0 %li x1 %li left 0x%x right 0x%x\n", *x0, *x1, *left, *right); */
|
2002-05-28 03:21:11 +04:00
|
|
|
}
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* layout_inline_container -- layout lines of text or inline boxes with floats
|
|
|
|
*
|
|
|
|
* box inline container
|
|
|
|
* width horizontal space available
|
|
|
|
* cont ancestor box which defines horizontal space, for inlines
|
|
|
|
* cx, cy box position relative to cont
|
|
|
|
*/
|
|
|
|
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_inline_container(struct box * box, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
|
|
|
struct box * c;
|
2002-05-11 19:22:24 +04:00
|
|
|
unsigned long 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
|
|
|
|
2002-08-06 00:34:45 +04:00
|
|
|
#ifdef DEBUG_LAYOUT
|
|
|
|
fprintf(stderr, "layout_inline_container(%p, %lu, %p, %lu, %lu)\n",
|
|
|
|
box, width, cont, cx, cy);
|
|
|
|
#endif
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
for (c = box->children; c != 0; ) {
|
2002-06-19 01:24:21 +04:00
|
|
|
c = layout_line(c, width, &y, cy + y, cont);
|
|
|
|
}
|
|
|
|
|
|
|
|
box->width = width;
|
|
|
|
box->height = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
signed long line_height(struct css_style * style)
|
|
|
|
{
|
2002-08-18 20:46:45 +04:00
|
|
|
assert(style != 0);
|
2002-06-19 01:24:21 +04:00
|
|
|
assert(style->line_height.size == CSS_LINE_HEIGHT_LENGTH ||
|
|
|
|
style->line_height.size == CSS_LINE_HEIGHT_ABSOLUTE);
|
|
|
|
|
|
|
|
if (style->line_height.size == CSS_LINE_HEIGHT_LENGTH)
|
|
|
|
return len(&style->line_height.value.length, style);
|
|
|
|
else
|
|
|
|
return style->line_height.value.absolute * len(&style->font_size.value.length, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct box * layout_line(struct box * first, unsigned long width, unsigned long * y,
|
|
|
|
unsigned long cy, struct box * cont)
|
|
|
|
{
|
|
|
|
unsigned long height;
|
|
|
|
unsigned long x0 = 0;
|
|
|
|
unsigned long x1 = width;
|
|
|
|
unsigned long x, h, xp;
|
|
|
|
struct box * left;
|
|
|
|
struct box * right;
|
|
|
|
struct box * b;
|
|
|
|
struct box * c;
|
|
|
|
struct box * d;
|
2002-06-21 22:16:24 +04:00
|
|
|
int move_y = 0;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: '%.*s' %li %li %li\n", first->length, first->text, width, *y, cy); */
|
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
/* find sides at top of line */
|
|
|
|
find_sides(cont->float_children, cy, cy, &x0, &x1, &left, &right);
|
|
|
|
|
|
|
|
/* get minimum line height from containing block */
|
|
|
|
height = line_height(first->parent->parent->style);
|
|
|
|
|
|
|
|
/* 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) {
|
2002-06-29 00:14:04 +04:00
|
|
|
assert(b->type == BOX_INLINE || b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT);
|
2002-06-19 01:24:21 +04:00
|
|
|
if (b->type == BOX_INLINE) {
|
|
|
|
h = line_height(b->style ? b->style : b->parent->parent->style);
|
|
|
|
b->height = h;
|
|
|
|
if (h > height) height = h;
|
2002-09-19 23:54:43 +04:00
|
|
|
if (b->width == UNKNOWN_WIDTH)
|
|
|
|
b->width = font_width(b->style, b->text, b->length);
|
|
|
|
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 */
|
2002-06-21 22:16:24 +04:00
|
|
|
x0 = 0;
|
|
|
|
x1 = width;
|
2002-06-19 01:24:21 +04:00
|
|
|
find_sides(cont->float_children, cy, cy + height, &x0, &x1, &left, &right);
|
|
|
|
|
|
|
|
/* pass 2: place boxes in line */
|
2002-06-21 22:16:24 +04:00
|
|
|
for (x = xp = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) {
|
2002-06-19 01:24:21 +04:00
|
|
|
if (b->type == BOX_INLINE) {
|
|
|
|
b->x = x;
|
|
|
|
xp = x;
|
|
|
|
x += b->width;
|
|
|
|
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 {
|
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); */
|
2002-06-29 00:14:04 +04:00
|
|
|
layout_node(d, width, d, 0, 0);
|
|
|
|
d->x = d->y = 0;
|
|
|
|
b->width = d->width;
|
|
|
|
b->height = d->height;
|
2002-06-19 01:24:21 +04:00
|
|
|
if (b->width < (x1 - x0) - x || (left == 0 && right == 0 && x == 0)) {
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
b->next_float = cont->float_children;
|
|
|
|
cont->float_children = b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x1 - x0 < x) {
|
|
|
|
/* the last box went over the end */
|
|
|
|
char * space = strchr(c->text, ' ');
|
|
|
|
char * space2 = space;
|
2002-06-27 03:27:30 +04:00
|
|
|
unsigned long w, wp;
|
2002-06-19 01:24:21 +04:00
|
|
|
struct box * c2;
|
|
|
|
|
2002-06-21 22:16:24 +04:00
|
|
|
if (space == 0)
|
2002-09-19 23:54:43 +04:00
|
|
|
wp = w = c->width;
|
2002-06-21 22:16:24 +04:00
|
|
|
else
|
2002-06-27 03:27:30 +04:00
|
|
|
wp = w = font_width(c->style, c->text, space - c->text);
|
2002-06-21 22:16:24 +04:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
if (x1 - x0 < xp + w && left == 0 && right == 0 && c == first) {
|
|
|
|
/* 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) {
|
|
|
|
b = c->next;
|
|
|
|
} else {
|
|
|
|
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;
|
2002-06-21 22:16:24 +04:00
|
|
|
c->length = space - c->text;
|
|
|
|
c2->next = c->next;
|
|
|
|
c->next = c2;
|
|
|
|
b = c2;
|
|
|
|
}
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: overflow, forcing\n"); */
|
2002-06-19 01:24:21 +04:00
|
|
|
} else if (x1 - x0 < xp + w) {
|
|
|
|
/* first word doesn't fit, but full width not available so leave for later */
|
|
|
|
b = c;
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "layout_line: overflow, leaving\n"); */
|
2002-05-11 19:22:24 +04: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-06-19 01:24:21 +04:00
|
|
|
while (xp + w < x1 - x0) {
|
2002-06-19 19:17:45 +04:00
|
|
|
/* fprintf(stderr, "%li + %li = %li < %li = %li - %li\n", */
|
|
|
|
/* xp, w, xp + w, x1 - x0, x1, x0); */
|
2002-06-19 01:24:21 +04:00
|
|
|
space = space2;
|
|
|
|
wp = w;
|
|
|
|
space2 = strchr(space + 1, ' ');
|
|
|
|
w = font_width(c->style, c->text, space2 - c->text);
|
|
|
|
}
|
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;
|
2002-06-19 01:24:21 +04:00
|
|
|
c->length = space - c->text;
|
2002-05-11 19:22:24 +04:00
|
|
|
c2->next = c->next;
|
|
|
|
c->next = c2;
|
2002-06-19 01:24:21 +04:00
|
|
|
b = c2;
|
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-19 01:24:21 +04:00
|
|
|
c->width = wp;
|
2002-06-19 19:17:45 +04:00
|
|
|
x = xp + wp;
|
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
|
|
|
}
|
2002-06-19 01:24:21 +04:00
|
|
|
for (d = first; d != b; d = d->next) {
|
|
|
|
if (d->type == BOX_INLINE) {
|
|
|
|
d->x += x0;
|
|
|
|
d->y = *y;
|
|
|
|
}
|
|
|
|
}
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-06-21 22:16:24 +04:00
|
|
|
if (move_y) *y += 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
|
|
|
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
void place_float_below(struct box * c, unsigned long width, unsigned long y, struct box * cont)
|
|
|
|
{
|
|
|
|
unsigned long x0, x1, yy = y;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} 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
|
|
|
|
*
|
|
|
|
* this is the fixed table layout algorithm,
|
|
|
|
* <http://www.w3.org/TR/REC-CSS2/tables.html#fixed-table-layout>
|
|
|
|
*/
|
|
|
|
|
2002-05-28 03:21:11 +04:00
|
|
|
void layout_table(struct box * table, unsigned long width, struct box * cont,
|
|
|
|
unsigned long cx, unsigned long cy)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
2002-09-18 23:36:28 +04:00
|
|
|
unsigned int columns = table->columns; /* total columns */
|
2002-09-19 23:54:43 +04:00
|
|
|
unsigned long table_width, max_width = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
unsigned long x;
|
2002-08-18 20:46:45 +04:00
|
|
|
unsigned long table_height = 0;
|
|
|
|
unsigned long *xs; /* array of column x positions */
|
2002-05-04 23:57:18 +04:00
|
|
|
unsigned int i;
|
2002-08-18 20:46:45 +04:00
|
|
|
struct box *c;
|
|
|
|
struct box *row;
|
|
|
|
struct box *row_group;
|
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);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2002-08-06 00:34:45 +04:00
|
|
|
#ifdef DEBUG_LAYOUT
|
|
|
|
fprintf(stderr, "layout_table(%p, %lu, %p, %lu, %lu)\n",
|
|
|
|
table, width, cont, cx, cy);
|
|
|
|
#endif
|
|
|
|
|
2002-09-18 23:36:28 +04:00
|
|
|
calculate_table_widths(table);
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
/* find table width */
|
|
|
|
switch (table->style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
2002-06-19 01:24:21 +04:00
|
|
|
table_width = len(&table->style->width.value.length, table->style);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
|
|
|
table_width = width * table->style->width.value.percent / 100;
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
default:
|
|
|
|
table_width = width;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2002-09-18 23:36:28 +04:00
|
|
|
fprintf(stderr, "table width %lu, min %lu, max %lu\n", table_width, table->min_width, table->max_width);
|
2002-08-06 00:34:45 +04:00
|
|
|
|
2002-09-19 23:54:43 +04:00
|
|
|
/* percentage width columns give an upper bound if possible */
|
|
|
|
for (i = 0; i < table->columns; i++) {
|
|
|
|
if (table->col[i].type == COLUMN_WIDTH_PERCENT) {
|
|
|
|
table->col[i].max = width * table->col[i].width / 100;
|
|
|
|
if (table->col[i].max < table->col[i].min)
|
|
|
|
table->col[i].max = table->col[i].min;
|
|
|
|
}
|
|
|
|
max_width += table->col[i].max;
|
|
|
|
}
|
|
|
|
|
2002-09-18 23:36:28 +04:00
|
|
|
if (table_width <= table->min_width) {
|
|
|
|
/* not enough space: minimise column widths */
|
|
|
|
for (i = 0; i < table->columns; i++) {
|
|
|
|
table->col[i].width = table->col[i].min;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-09-18 23:36:28 +04:00
|
|
|
table_width = table->min_width;
|
2002-09-19 23:54:43 +04:00
|
|
|
} else if (max_width <= table_width) {
|
2002-09-18 23:36:28 +04:00
|
|
|
/* more space than maximum width: maximise widths */
|
|
|
|
for (i = 0; i < table->columns; i++) {
|
|
|
|
table->col[i].width = table->col[i].max;
|
|
|
|
}
|
2002-09-19 23:54:43 +04:00
|
|
|
table_width = max_width;
|
2002-09-18 23:36:28 +04:00
|
|
|
} else {
|
|
|
|
/* space between min and max: fill it exactly */
|
|
|
|
float scale = (float) (table_width - table->min_width) /
|
2002-09-19 23:54:43 +04:00
|
|
|
(float) (max_width - table->min_width);
|
2002-09-18 23:36:28 +04:00
|
|
|
fprintf(stderr, "filling, scale %f\n", scale);
|
|
|
|
for (i = 0; i < table->columns; i++) {
|
|
|
|
table->col[i].width = table->col[i].min +
|
|
|
|
(table->col[i].max - table->col[i].min) * scale;
|
|
|
|
}
|
|
|
|
}
|
2002-06-19 19:17:45 +04:00
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
xs = xcalloc(columns + 1, sizeof(*xs));
|
|
|
|
xs[0] = x = 0;
|
2002-09-18 23:36:28 +04:00
|
|
|
for (i = 0; i < table->columns; i++) {
|
|
|
|
x += table->col[i].width;
|
|
|
|
xs[i + 1] = x;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-08-06 00:34:45 +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) {
|
|
|
|
unsigned long row_group_height = 0;
|
|
|
|
for (row = row_group->children; row != 0; row = row->next) {
|
|
|
|
unsigned long row_height = 0;
|
2002-09-18 23:36:28 +04:00
|
|
|
for (i = 0, c = row->children; c != 0; i += c->columns, c = c->next) {
|
2002-08-18 20:46:45 +04:00
|
|
|
assert(c->style != 0);
|
2002-09-18 23:36:28 +04:00
|
|
|
c->width = xs[i + c->columns] - xs[i];
|
2002-08-18 20:46:45 +04:00
|
|
|
c->float_children = 0;
|
|
|
|
c->height = layout_block_children(c, c->width, c, 0, 0);
|
|
|
|
if (c->style->height.height == CSS_HEIGHT_LENGTH)
|
2002-06-19 01:24:21 +04:00
|
|
|
c->height = len(&c->style->height.length, c->style);
|
2002-08-18 20:46:45 +04:00
|
|
|
c->x = xs[i];
|
|
|
|
c->y = 0;
|
|
|
|
if (c->height > row_height) row_height = c->height;
|
2002-05-04 23:57:18 +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
|
|
|
}
|
|
|
|
|
|
|
|
free(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
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find min, max widths required by boxes
|
|
|
|
*/
|
|
|
|
|
|
|
|
void calculate_widths(struct box *box)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
unsigned long min = 0, max = 0, width;
|
|
|
|
|
|
|
|
assert(box->type == BOX_TABLE_CELL ||
|
|
|
|
box->type == BOX_BLOCK ||
|
|
|
|
box->type == BOX_FLOAT_LEFT || box->type == BOX_FLOAT_RIGHT);
|
|
|
|
|
|
|
|
/* check if the widths have already been calculated */
|
|
|
|
if (box->max_width != UNKNOWN_MAX_WIDTH)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (child = box->children; child != 0; child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_BLOCK:
|
|
|
|
case BOX_TABLE:
|
|
|
|
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 {
|
|
|
|
if (child->type == BOX_TABLE)
|
|
|
|
calculate_table_widths(child);
|
|
|
|
else
|
|
|
|
calculate_widths(child);
|
|
|
|
if (min < child->min_width) min = child->min_width;
|
|
|
|
if (max < child->max_width) max = child->max_width;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
calculate_inline_container_widths(child);
|
|
|
|
if (min < child->min_width) min = child->min_width;
|
|
|
|
if (max < child->max_width) max = child->max_width;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box->min_width = min;
|
|
|
|
box->max_width = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void calculate_inline_container_widths(struct box *box)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
unsigned long min = 0, max = 0, width;
|
|
|
|
char *word, *space;
|
|
|
|
|
|
|
|
for (child = box->children; child != 0; child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_INLINE:
|
|
|
|
/* max = all one line */
|
2002-09-19 23:54:43 +04:00
|
|
|
child->width = font_width(child->style,
|
|
|
|
child->text, child->length);
|
|
|
|
max += child->width;
|
2002-09-18 23:36:28 +04:00
|
|
|
|
|
|
|
/* min = widest word */
|
|
|
|
for (word = child->text, space = strchr(child->text, ' ');
|
|
|
|
space != 0;
|
|
|
|
word = space + 1, space = strchr(word, ' ')) {
|
|
|
|
width = font_width(child->style, word, space - word);
|
|
|
|
if (min < width) min = width;
|
|
|
|
}
|
|
|
|
width = font_width(child->style, word, strlen(word));
|
|
|
|
if (min < width) min = width;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
|
|
|
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 {
|
|
|
|
calculate_widths(child);
|
|
|
|
if (min < child->min_width) min = child->min_width;
|
|
|
|
if (max < child->max_width) max = child->max_width;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box->min_width = min;
|
|
|
|
box->max_width = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void calculate_table_widths(struct box *table)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct box *row_group, *row, *cell;
|
|
|
|
unsigned long width, min_width = 0, max_width = 0;
|
|
|
|
struct column *col = xcalloc(table->columns, sizeof(*col));
|
|
|
|
|
|
|
|
#define WIDTH_FIXED ULONG_MAX
|
|
|
|
|
|
|
|
assert(table->children != 0 && table->children->children != 0);
|
|
|
|
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);
|
|
|
|
for (i = 0, cell = row->children; cell != 0;
|
|
|
|
i += cell->columns, cell = cell->next) {
|
|
|
|
assert(cell->type == BOX_TABLE_CELL);
|
|
|
|
assert(cell->style != 0);
|
|
|
|
if (col[i].type == COLUMN_WIDTH_FIXED)
|
|
|
|
continue;
|
|
|
|
/* ignore specified width if colspan > 1 */
|
|
|
|
if (cell->style->width.width == CSS_WIDTH_LENGTH &&
|
|
|
|
cell->columns == 1) {
|
|
|
|
width = len(&cell->style->width.value.length,
|
|
|
|
cell->style);
|
|
|
|
col[i].type = COLUMN_WIDTH_FIXED;
|
|
|
|
col[i].min = col[i].max = col[i].width = width;
|
|
|
|
} else {
|
|
|
|
calculate_widths(cell);
|
|
|
|
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;
|
|
|
|
if (col[i].type != COLUMN_WIDTH_UNKNOWN)
|
|
|
|
continue;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < table->columns; i++) {
|
|
|
|
min_width += col[i].min;
|
|
|
|
max_width += col[i].max;
|
|
|
|
fprintf(stderr, "col %u, min %lu, max %lu\n", i, col[i].min, col[i].max);
|
|
|
|
}
|
|
|
|
table->min_width = min_width;
|
|
|
|
table->max_width = max_width;
|
|
|
|
table->col = col;
|
|
|
|
}
|