2002-05-04 23:57:18 +04:00
|
|
|
/**
|
2002-05-11 19:22:24 +04:00
|
|
|
* $Id: layout.c,v 1.2 2002/05/11 15:22:24 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"
|
|
|
|
#include "css.h"
|
|
|
|
#include "font.h"
|
|
|
|
#include "box.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* internal functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
signed long len(struct css_length * length, unsigned long em);
|
|
|
|
|
|
|
|
unsigned long layout_block_children(struct box * box, unsigned long width);
|
|
|
|
void layout_inline_container(struct box * box, unsigned long width);
|
|
|
|
void layout_table(struct box * box, unsigned long width);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* convert a struct css_length to pixels
|
|
|
|
*/
|
|
|
|
|
|
|
|
signed long len(struct css_length * length, unsigned long em)
|
|
|
|
{
|
|
|
|
switch (length->unit) {
|
|
|
|
case CSS_UNIT_EM: return length->value * em;
|
|
|
|
case CSS_UNIT_EX: return length->value * em * 0.6;
|
|
|
|
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;
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* layout algorithm
|
|
|
|
*/
|
|
|
|
|
|
|
|
void layout_block(struct box * box, unsigned long width)
|
|
|
|
{
|
|
|
|
struct css_style * style = box->style;
|
|
|
|
switch (style->width.width) {
|
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
box->width = width;
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_LENGTH:
|
2002-05-11 19:22:24 +04:00
|
|
|
box->width = len(&style->width.value.length, 20);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
|
|
|
box->width = width * style->width.value.percent / 100;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
box->height = layout_block_children(box, box->width);
|
|
|
|
switch (style->height.height) {
|
|
|
|
case CSS_HEIGHT_AUTO:
|
|
|
|
break;
|
|
|
|
case CSS_HEIGHT_LENGTH:
|
2002-05-11 19:22:24 +04:00
|
|
|
box->height = len(&style->height.length, 20);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long layout_block_children(struct box * box, unsigned long width)
|
|
|
|
{
|
|
|
|
struct box * c;
|
2002-05-11 19:22:24 +04:00
|
|
|
unsigned long y = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
for (c = box->children; c != 0; c = c->next) {
|
|
|
|
switch (c->type) {
|
|
|
|
case BOX_BLOCK:
|
2002-05-11 19:22:24 +04:00
|
|
|
layout_block(c, width);
|
|
|
|
c->x = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
c->y = y;
|
2002-05-11 19:22:24 +04:00
|
|
|
y += c->height;
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case BOX_INLINE_CONTAINER:
|
2002-05-11 19:22:24 +04:00
|
|
|
layout_inline_container(c, width);
|
|
|
|
c->x = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
c->y = y;
|
2002-05-11 19:22:24 +04:00
|
|
|
y += c->height;
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case BOX_TABLE:
|
2002-05-11 19:22:24 +04:00
|
|
|
layout_table(c, width);
|
|
|
|
c->x = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
c->y = y;
|
2002-05-11 19:22:24 +04:00
|
|
|
y += c->height;
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
die("block child not block, table, or inline container");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void layout_inline_container(struct box * box, unsigned long width)
|
|
|
|
{
|
|
|
|
/* TODO: write this */
|
|
|
|
struct box * c;
|
2002-05-11 19:22:24 +04:00
|
|
|
unsigned long y = 0;
|
|
|
|
unsigned long x = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
const char * end;
|
2002-05-11 19:22:24 +04:00
|
|
|
struct box * c2;
|
|
|
|
struct font_split split;
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
for (c = box->children; c != 0; ) {
|
2002-05-11 19:22:24 +04:00
|
|
|
split = font_split(0, c->font, c->text, width - x, x == 0);
|
|
|
|
if (*(split.end) == 0) {
|
2002-05-04 23:57:18 +04:00
|
|
|
/* fits into this line */
|
|
|
|
c->x = x;
|
|
|
|
c->y = y;
|
2002-05-11 19:22:24 +04:00
|
|
|
c->width = split.width;
|
|
|
|
c->height = split.height;
|
|
|
|
c->length = split.end - c->text;
|
2002-05-04 23:57:18 +04:00
|
|
|
x += c->width;
|
|
|
|
c = c->next;
|
2002-05-11 19:22:24 +04:00
|
|
|
} else if (split.end == c->text) {
|
2002-05-04 23:57:18 +04:00
|
|
|
/* doesn't fit at all: move down a line */
|
2002-05-11 19:22:24 +04:00
|
|
|
x = 0;
|
|
|
|
y += 30;
|
|
|
|
/*c->width = font_split(0, c->font, c->text, (width-x)/20+1, &end)*20;
|
2002-05-04 23:57:18 +04:00
|
|
|
if (end == c->text) end = strchr(c->text, ' ');
|
2002-05-11 19:22:24 +04:00
|
|
|
if (end == 0) end = c->text + 1;*/
|
|
|
|
} else {
|
|
|
|
/* split into two lines */
|
|
|
|
c->x = x;
|
|
|
|
c->y = y;
|
|
|
|
c->width = split.width;
|
|
|
|
c->height = split.height;
|
|
|
|
c->length = split.end - c->text;
|
|
|
|
x = 0;
|
|
|
|
y += 30;
|
|
|
|
c2 = memcpy(xcalloc(1, sizeof(struct box)), c, sizeof(struct box));
|
|
|
|
c2->text = split.end;
|
|
|
|
c2->next = c->next;
|
|
|
|
c->next = c2;
|
|
|
|
c = c2;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
box->width = width;
|
2002-05-11 19:22:24 +04:00
|
|
|
box->height = y + 30;
|
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>
|
|
|
|
*/
|
|
|
|
|
|
|
|
void layout_table(struct box * table, unsigned long width)
|
|
|
|
{
|
|
|
|
unsigned int columns; /* total columns */
|
|
|
|
unsigned int auto_columns; /* number of columns with auto width */
|
|
|
|
unsigned long table_width;
|
|
|
|
unsigned long used_width = 0; /* width used by fixed or percent columns */
|
|
|
|
unsigned long auto_width; /* width of each auto column (all equal) */
|
|
|
|
unsigned long extra_width = 0; /* extra width for each column if table is wider than columns */
|
|
|
|
unsigned long x;
|
2002-05-11 19:22:24 +04:00
|
|
|
unsigned long y = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
unsigned long * xs;
|
|
|
|
unsigned int i;
|
|
|
|
struct box * c;
|
|
|
|
struct box * r;
|
|
|
|
|
|
|
|
assert(table->type == BOX_TABLE);
|
|
|
|
|
|
|
|
/* find table width */
|
|
|
|
switch (table->style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
2002-05-11 19:22:24 +04:00
|
|
|
table_width = len(&table->style->width.value.length, 20);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate column statistics */
|
|
|
|
for (columns = 0, auto_columns = 0, c = table->children->children;
|
|
|
|
c != 0; c = c->next) {
|
|
|
|
assert(c->type == BOX_TABLE_CELL);
|
|
|
|
switch (c->style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
2002-05-11 19:22:24 +04:00
|
|
|
used_width += len(&c->style->width.value.length, 20);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
|
|
|
used_width += table_width * c->style->width.value.percent / 100;
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
auto_columns++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
columns++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (auto_columns == 0 && table->style->width.width != CSS_WIDTH_AUTO)
|
|
|
|
extra_width = (table_width - used_width) / columns;
|
|
|
|
else if (auto_columns != 0)
|
|
|
|
auto_width = (table_width - used_width) / auto_columns;
|
|
|
|
|
|
|
|
/*printf("%i %i %i %i %i\n", table_width, columns, auto_columns, used_width, auto_width);*/
|
|
|
|
|
|
|
|
/* find column widths */
|
|
|
|
xs = xcalloc(columns + 1, sizeof(*xs));
|
|
|
|
xs[0] = x = 0;
|
|
|
|
for (i = 1, c = table->children->children; c != 0; i++, c = c->next) {
|
|
|
|
switch (c->style->width.width) {
|
|
|
|
case CSS_WIDTH_LENGTH:
|
|
|
|
x += len(&c->style->width.value.length, 10) + extra_width;
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_PERCENT:
|
|
|
|
x += table_width * c->style->width.value.percent / 100 + extra_width;
|
|
|
|
break;
|
|
|
|
case CSS_WIDTH_AUTO:
|
|
|
|
x += auto_width;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
xs[i] = x;
|
2002-05-11 19:22:24 +04:00
|
|
|
/*printf("%i ", x);*/
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-05-11 19:22:24 +04:00
|
|
|
/*printf("\n");*/
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
if (auto_columns == 0 && table->style->width.width == CSS_WIDTH_AUTO)
|
|
|
|
table_width = used_width;
|
|
|
|
|
|
|
|
/* position cells */
|
|
|
|
for (r = table->children; r != 0; r = r->next) {
|
|
|
|
unsigned long height = 0;
|
|
|
|
for (i = 0, c = r->children; c != 0; i++, c = c->next) {
|
|
|
|
c->width = xs[i+1] - xs[i];
|
|
|
|
c->height = layout_block_children(c, c->width);
|
|
|
|
switch (c->style->height.height) {
|
|
|
|
case CSS_HEIGHT_AUTO:
|
|
|
|
break;
|
|
|
|
case CSS_HEIGHT_LENGTH:
|
|
|
|
c->height = len(&c->style->height.length, 10);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c->x = xs[i];
|
2002-05-11 19:22:24 +04:00
|
|
|
c->y = 0;
|
2002-05-04 23:57:18 +04:00
|
|
|
if (c->height > height) height = c->height;
|
|
|
|
}
|
|
|
|
r->x = 0;
|
|
|
|
r->y = y;
|
|
|
|
r->width = table_width;
|
2002-05-11 19:22:24 +04:00
|
|
|
r->height = height;
|
|
|
|
y += height;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
free(xs);
|
|
|
|
|
|
|
|
table->width = table_width;
|
|
|
|
table->height = y;
|
|
|
|
}
|
|
|
|
|