2002-05-04 23:57:18 +04:00
|
|
|
/**
|
2002-09-19 23:54:43 +04:00
|
|
|
* $Id: box.c,v 1.16 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-09-12 01:19:24 +04:00
|
|
|
#include "utf-8.h"
|
2002-08-18 20:46:45 +04:00
|
|
|
#include "netsurf/render/css.h"
|
|
|
|
#include "netsurf/render/font.h"
|
|
|
|
#include "netsurf/render/box.h"
|
|
|
|
#include "netsurf/render/utils.h"
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* internal functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
void box_add_child(struct box * parent, struct box * child);
|
2002-08-12 03:01:02 +04:00
|
|
|
struct box * box_create(xmlNode * node, box_type type, struct css_style * style,
|
|
|
|
const char *href);
|
2002-08-18 20:46:45 +04:00
|
|
|
struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style,
|
|
|
|
struct css_stylesheet * stylesheet,
|
|
|
|
struct css_selector ** selector, unsigned int depth,
|
|
|
|
struct box * parent, struct box * inline_container,
|
|
|
|
const char *href);
|
2002-05-22 01:32:35 +04:00
|
|
|
struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_style * parent_style,
|
|
|
|
xmlNode * n, struct css_selector * selector, unsigned int depth);
|
2002-08-18 20:46:45 +04:00
|
|
|
void box_normalise_block(struct box *block);
|
|
|
|
void box_normalise_table(struct box *table);
|
|
|
|
void box_normalise_table_row_group(struct box *row_group);
|
|
|
|
void box_normalise_table_row(struct box *row);
|
|
|
|
void box_normalise_inline_container(struct box *cont);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add a child to a box tree node
|
|
|
|
*/
|
|
|
|
|
|
|
|
void box_add_child(struct box * parent, struct box * child)
|
|
|
|
{
|
2002-08-18 20:46:45 +04:00
|
|
|
if (parent->children != 0) /* has children already */
|
2002-05-04 23:57:18 +04:00
|
|
|
parent->last->next = child;
|
|
|
|
else /* this is the first child */
|
|
|
|
parent->children = child;
|
|
|
|
|
|
|
|
parent->last = child;
|
|
|
|
child->parent = parent;
|
|
|
|
}
|
|
|
|
|
2002-06-27 03:27:30 +04:00
|
|
|
/**
|
|
|
|
* create a box tree node
|
|
|
|
*/
|
|
|
|
|
2002-08-12 03:01:02 +04:00
|
|
|
struct box * box_create(xmlNode * node, box_type type, struct css_style * style,
|
|
|
|
const char *href)
|
2002-06-27 03:27:30 +04:00
|
|
|
{
|
|
|
|
struct box * box = xcalloc(1, sizeof(struct box));
|
|
|
|
box->type = type;
|
2002-08-18 20:46:45 +04:00
|
|
|
box->node = node;
|
2002-06-27 03:27:30 +04:00
|
|
|
box->style = style;
|
2002-09-19 23:54:43 +04:00
|
|
|
box->width = UNKNOWN_WIDTH;
|
2002-09-18 23:36:28 +04:00
|
|
|
box->max_width = UNKNOWN_MAX_WIDTH;
|
2002-08-12 03:01:02 +04:00
|
|
|
box->text = 0;
|
|
|
|
box->href = href;
|
2002-08-18 20:46:45 +04:00
|
|
|
box->length = 0;
|
2002-09-18 23:36:28 +04:00
|
|
|
box->columns = 1;
|
2002-08-18 20:46:45 +04:00
|
|
|
box->next = 0;
|
|
|
|
box->children = 0;
|
|
|
|
box->last = 0;
|
|
|
|
box->parent = 0;
|
|
|
|
box->float_children = 0;
|
|
|
|
box->next_float = 0;
|
2002-09-18 23:36:28 +04:00
|
|
|
box->col = 0;
|
2002-06-27 03:27:30 +04:00
|
|
|
return box;
|
|
|
|
}
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2002-09-12 01:19:24 +04:00
|
|
|
|
|
|
|
char * tolat1(const xmlChar * s)
|
|
|
|
{
|
|
|
|
char *d = xcalloc(strlen(s) + 1, sizeof(char));
|
|
|
|
char *d0 = d;
|
|
|
|
unsigned int u, chars;
|
|
|
|
|
|
|
|
while (*s != 0) {
|
|
|
|
u = sgetu8(s, &chars);
|
|
|
|
s += chars;
|
2002-09-18 23:36:28 +04:00
|
|
|
if (u == 0x09 || u == 0x0a || u == 0x0d)
|
|
|
|
*d = ' ';
|
|
|
|
else if ((0x20 <= u && u <= 0x7f) || (0xa0 <= u && u <= 0xff))
|
|
|
|
*d = u;
|
|
|
|
else
|
|
|
|
*d = '?';
|
2002-09-12 01:19:24 +04:00
|
|
|
d++;
|
|
|
|
}
|
|
|
|
*d = 0;
|
|
|
|
|
|
|
|
return d0;
|
|
|
|
}
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
/**
|
|
|
|
* make a box tree with style data from an xml tree
|
|
|
|
*
|
|
|
|
* arguments:
|
|
|
|
* n xml tree
|
|
|
|
* parent_style style at this point in xml tree
|
|
|
|
* stylesheet stylesheet to use
|
|
|
|
* selector element selector hierachy to this point
|
|
|
|
* depth depth in xml tree
|
|
|
|
* parent parent in box tree
|
|
|
|
* inline_container current inline container box, or 0
|
|
|
|
*
|
|
|
|
* returns:
|
|
|
|
* updated current inline container
|
|
|
|
*/
|
|
|
|
|
2002-08-18 20:46:45 +04:00
|
|
|
void xml_to_box(xmlNode * n, struct css_style * parent_style,
|
|
|
|
struct css_stylesheet * stylesheet,
|
|
|
|
struct css_selector ** selector, unsigned int depth,
|
|
|
|
struct box * parent, struct box * inline_container,
|
|
|
|
const char *href)
|
|
|
|
{
|
|
|
|
convert_xml_to_box(n, parent_style, stylesheet,
|
|
|
|
selector, depth, parent, inline_container, href);
|
|
|
|
box_normalise_block(parent->children);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct box * convert_xml_to_box(xmlNode * n, struct css_style * parent_style,
|
|
|
|
struct css_stylesheet * stylesheet,
|
2002-05-04 23:57:18 +04:00
|
|
|
struct css_selector ** selector, unsigned int depth,
|
2002-08-12 03:01:02 +04:00
|
|
|
struct box * parent, struct box * inline_container,
|
|
|
|
const char *href)
|
2002-05-04 23:57:18 +04:00
|
|
|
{
|
|
|
|
struct box * box;
|
|
|
|
struct box * inline_container_c;
|
2002-06-27 03:27:30 +04:00
|
|
|
struct css_style * style, * style2;
|
2002-05-04 23:57:18 +04:00
|
|
|
xmlNode * c;
|
2002-06-27 03:27:30 +04:00
|
|
|
char * s;
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2002-09-11 18:24:02 +04:00
|
|
|
gui_multitask();
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
if (n->type == XML_ELEMENT_NODE) {
|
|
|
|
/* work out the style for this element */
|
|
|
|
*selector = xrealloc(*selector, (depth + 1) * sizeof(struct css_selector));
|
2002-06-26 16:19:24 +04:00
|
|
|
(*selector)[depth].element = (const char *) n->name;
|
2002-05-04 23:57:18 +04:00
|
|
|
(*selector)[depth].class = (*selector)[depth].id = 0;
|
2002-08-12 03:01:02 +04:00
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "class"))) {
|
2002-06-27 03:27:30 +04:00
|
|
|
(*selector)[depth].class = s;
|
2002-08-12 03:01:02 +04:00
|
|
|
free(s);
|
|
|
|
}
|
2002-05-22 01:32:35 +04:00
|
|
|
style = box_get_style(stylesheet, parent_style, n, *selector, depth + 1);
|
2002-06-29 00:14:04 +04:00
|
|
|
if (style->display == CSS_DISPLAY_NONE)
|
|
|
|
return inline_container;
|
2002-08-12 03:01:02 +04:00
|
|
|
|
|
|
|
if (strcmp((const char *) n->name, "a") == 0) {
|
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "href"))) {
|
|
|
|
href = strdup(s);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
2002-05-22 01:32:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n->type == XML_TEXT_NODE ||
|
|
|
|
(n->type == XML_ELEMENT_NODE && (style->float_ == CSS_FLOAT_LEFT ||
|
|
|
|
style->float_ == CSS_FLOAT_RIGHT))) {
|
|
|
|
/* text nodes are converted to inline boxes, wrapped in an inline container block */
|
|
|
|
if (inline_container == 0) { /* this is the first inline node: make a container */
|
|
|
|
inline_container = xcalloc(1, sizeof(struct box));
|
|
|
|
inline_container->type = BOX_INLINE_CONTAINER;
|
|
|
|
box_add_child(parent, inline_container);
|
|
|
|
}
|
|
|
|
if (n->type == XML_TEXT_NODE) {
|
2002-08-12 03:01:02 +04:00
|
|
|
box = box_create(n, BOX_INLINE, parent_style, href);
|
2002-09-12 01:19:24 +04:00
|
|
|
box->text = squash_whitespace(tolat1(n->content));
|
2002-06-19 01:24:21 +04:00
|
|
|
box->length = strlen(box->text);
|
2002-06-29 00:14:04 +04:00
|
|
|
box_add_child(inline_container, box);
|
2002-05-22 01:32:35 +04:00
|
|
|
} else {
|
2002-08-12 03:01:02 +04:00
|
|
|
box = box_create(0, BOX_FLOAT_LEFT, 0, href);
|
2002-06-29 00:14:04 +04:00
|
|
|
if (style->float_ == CSS_FLOAT_RIGHT) box->type = BOX_FLOAT_RIGHT;
|
|
|
|
box_add_child(inline_container, box);
|
|
|
|
style->float_ = CSS_FLOAT_NONE;
|
|
|
|
parent = box;
|
|
|
|
if (style->display == CSS_DISPLAY_INLINE)
|
|
|
|
style->display = CSS_DISPLAY_BLOCK;
|
2002-05-05 01:17:06 +04:00
|
|
|
}
|
2002-06-29 00:14:04 +04:00
|
|
|
}
|
2002-05-05 01:17:06 +04:00
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
if (n->type == XML_ELEMENT_NODE) {
|
2002-05-04 23:57:18 +04:00
|
|
|
switch (style->display) {
|
|
|
|
case CSS_DISPLAY_BLOCK: /* blocks get a node in the box tree */
|
2002-08-12 03:01:02 +04:00
|
|
|
box = box_create(n, BOX_BLOCK, style, href);
|
2002-05-04 23:57:18 +04:00
|
|
|
box_add_child(parent, box);
|
|
|
|
inline_container_c = 0;
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
2002-08-18 20:46:45 +04:00
|
|
|
inline_container_c = convert_xml_to_box(c, style, stylesheet,
|
2002-08-12 03:01:02 +04:00
|
|
|
selector, depth + 1, box, inline_container_c,
|
|
|
|
href);
|
2002-05-04 23:57:18 +04:00
|
|
|
inline_container = 0;
|
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_INLINE: /* inline elements get no box, but their children do */
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
2002-08-18 20:46:45 +04:00
|
|
|
inline_container = convert_xml_to_box(c, style, stylesheet,
|
2002-08-12 03:01:02 +04:00
|
|
|
selector, depth + 1, parent, inline_container,
|
|
|
|
href);
|
2002-05-04 23:57:18 +04:00
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_TABLE:
|
2002-08-12 03:01:02 +04:00
|
|
|
box = box_create(n, BOX_TABLE, style, href);
|
2002-05-04 23:57:18 +04:00
|
|
|
box_add_child(parent, box);
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
2002-08-18 20:46:45 +04:00
|
|
|
convert_xml_to_box(c, style, stylesheet,
|
2002-08-12 03:01:02 +04:00
|
|
|
selector, depth + 1, box, 0,
|
|
|
|
href);
|
2002-05-04 23:57:18 +04:00
|
|
|
inline_container = 0;
|
|
|
|
break;
|
2002-08-18 20:46:45 +04:00
|
|
|
case CSS_DISPLAY_TABLE_ROW_GROUP:
|
|
|
|
case CSS_DISPLAY_TABLE_HEADER_GROUP:
|
|
|
|
case CSS_DISPLAY_TABLE_FOOTER_GROUP:
|
|
|
|
box = box_create(n, BOX_TABLE_ROW_GROUP, style, href);
|
|
|
|
box_add_child(parent, box);
|
|
|
|
inline_container_c = 0;
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
|
|
|
inline_container_c = convert_xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, box, inline_container_c,
|
|
|
|
href);
|
|
|
|
inline_container = 0;
|
|
|
|
break;
|
2002-05-04 23:57:18 +04:00
|
|
|
case CSS_DISPLAY_TABLE_ROW:
|
2002-08-12 03:01:02 +04:00
|
|
|
box = box_create(n, BOX_TABLE_ROW, style, href);
|
2002-05-04 23:57:18 +04:00
|
|
|
box_add_child(parent, box);
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
2002-08-18 20:46:45 +04:00
|
|
|
convert_xml_to_box(c, style, stylesheet,
|
2002-08-12 03:01:02 +04:00
|
|
|
selector, depth + 1, box, 0,
|
|
|
|
href);
|
2002-05-04 23:57:18 +04:00
|
|
|
inline_container = 0;
|
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_TABLE_CELL:
|
2002-08-12 03:01:02 +04:00
|
|
|
box = box_create(n, BOX_TABLE_CELL, style, href);
|
2002-06-27 03:27:30 +04:00
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "colspan"))) {
|
2002-09-18 23:36:28 +04:00
|
|
|
if ((box->columns = strtol(s, 0, 10)) == 0)
|
|
|
|
box->columns = 1;
|
2002-06-27 03:27:30 +04:00
|
|
|
} else
|
2002-09-18 23:36:28 +04:00
|
|
|
box->columns = 1;
|
2002-05-04 23:57:18 +04:00
|
|
|
box_add_child(parent, box);
|
|
|
|
inline_container_c = 0;
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
2002-08-18 20:46:45 +04:00
|
|
|
inline_container_c = convert_xml_to_box(c, style, stylesheet,
|
2002-08-12 03:01:02 +04:00
|
|
|
selector, depth + 1, box, inline_container_c,
|
|
|
|
href);
|
2002-05-04 23:57:18 +04:00
|
|
|
inline_container = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2002-06-26 16:19:24 +04:00
|
|
|
break;
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inline_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-08 22:11:56 +04:00
|
|
|
/**
|
2002-05-22 01:32:35 +04:00
|
|
|
* get the style for an element
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_style * parent_style,
|
|
|
|
xmlNode * n, struct css_selector * selector, unsigned int depth)
|
|
|
|
{
|
|
|
|
struct css_style * style = xcalloc(1, sizeof(struct css_style));
|
2002-06-26 16:19:24 +04:00
|
|
|
char * s;
|
2002-05-22 01:32:35 +04:00
|
|
|
|
|
|
|
memcpy(style, parent_style, sizeof(struct css_style));
|
|
|
|
css_get_style(stylesheet, selector, depth, style);
|
|
|
|
|
2002-06-29 00:14:04 +04:00
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "align"))) {
|
|
|
|
if (strcmp((const char *) n->name, "table") == 0 ||
|
|
|
|
strcmp((const char *) n->name, "img") == 0) {
|
|
|
|
if (strcmp(s, "left") == 0) style->float_ = CSS_FLOAT_LEFT;
|
|
|
|
else if (strcmp(s, "right") == 0) style->float_ = CSS_FLOAT_RIGHT;
|
|
|
|
} else {
|
|
|
|
if (strcmp(s, "left") == 0) style->text_align = CSS_TEXT_ALIGN_LEFT;
|
|
|
|
else if (strcmp(s, "center") == 0) style->text_align = CSS_TEXT_ALIGN_CENTER;
|
|
|
|
else if (strcmp(s, "right") == 0) style->text_align = CSS_TEXT_ALIGN_RIGHT;
|
|
|
|
}
|
2002-08-12 03:01:02 +04:00
|
|
|
free(s);
|
2002-06-29 00:14:04 +04:00
|
|
|
}
|
|
|
|
|
2002-06-26 16:19:24 +04:00
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "clear"))) {
|
2002-06-21 22:16:24 +04:00
|
|
|
if (strcmp(s, "all") == 0) style->clear = CSS_CLEAR_BOTH;
|
|
|
|
else if (strcmp(s, "left") == 0) style->clear = CSS_CLEAR_LEFT;
|
|
|
|
else if (strcmp(s, "right") == 0) style->clear = CSS_CLEAR_RIGHT;
|
|
|
|
}
|
|
|
|
|
2002-06-26 16:19:24 +04:00
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "width"))) {
|
2002-08-12 03:01:02 +04:00
|
|
|
if (strrchr(s, '%')) {
|
|
|
|
style->width.width = CSS_WIDTH_PERCENT;
|
2002-05-22 01:32:35 +04:00
|
|
|
style->width.value.percent = atof(s);
|
2002-08-12 03:01:02 +04:00
|
|
|
} else {
|
|
|
|
style->width.width = CSS_WIDTH_LENGTH;
|
|
|
|
style->width.value.length.unit = CSS_UNIT_PX;
|
2002-05-22 01:32:35 +04:00
|
|
|
style->width.value.length.value = atof(s);
|
2002-08-12 03:01:02 +04:00
|
|
|
}
|
|
|
|
free(s);
|
2002-05-22 01:32:35 +04:00
|
|
|
}
|
|
|
|
|
2002-06-26 16:19:24 +04:00
|
|
|
if ((s = (char *) xmlGetProp(n, (xmlChar *) "style"))) {
|
2002-05-22 01:32:35 +04:00
|
|
|
struct css_style * astyle = xcalloc(1, sizeof(struct css_style));
|
|
|
|
memcpy(astyle, &css_empty_style, sizeof(struct css_style));
|
|
|
|
css_parse_property_list(astyle, s);
|
|
|
|
css_cascade(style, astyle);
|
|
|
|
free(astyle);
|
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
return style;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-08 22:11:56 +04:00
|
|
|
/**
|
2002-05-04 23:57:18 +04:00
|
|
|
* print a box tree to standard output
|
|
|
|
*/
|
|
|
|
|
|
|
|
void box_dump(struct box * box, unsigned int depth)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
struct box * c;
|
2002-06-19 01:24:21 +04:00
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
for (i = 0; i < depth; i++)
|
2002-06-19 01:24:21 +04:00
|
|
|
fprintf(stderr, " ");
|
2002-05-04 23:57:18 +04:00
|
|
|
|
2002-06-19 01:24:21 +04:00
|
|
|
fprintf(stderr, "x%li y%li w%li h%li ", box->x, box->y, box->width, box->height);
|
2002-09-18 23:36:28 +04:00
|
|
|
if (box->max_width != UNKNOWN_MAX_WIDTH)
|
|
|
|
fprintf(stderr, "min%lu max%lu ", box->min_width, box->max_width);
|
2002-05-04 23:57:18 +04:00
|
|
|
|
|
|
|
switch (box->type) {
|
2002-06-27 03:27:30 +04:00
|
|
|
case BOX_BLOCK: fprintf(stderr, "BOX_BLOCK "); break;
|
2002-06-19 01:24:21 +04:00
|
|
|
case BOX_INLINE_CONTAINER: fprintf(stderr, "BOX_INLINE_CONTAINER "); break;
|
2002-06-21 22:16:24 +04:00
|
|
|
case BOX_INLINE: fprintf(stderr, "BOX_INLINE '%.*s' ",
|
|
|
|
(int) box->length, box->text); break;
|
2002-06-27 03:27:30 +04:00
|
|
|
case BOX_TABLE: fprintf(stderr, "BOX_TABLE "); break;
|
|
|
|
case BOX_TABLE_ROW: fprintf(stderr, "BOX_TABLE_ROW "); break;
|
2002-09-18 23:36:28 +04:00
|
|
|
case BOX_TABLE_CELL: fprintf(stderr, "BOX_TABLE_CELL [columns %i] ",
|
|
|
|
box->columns); break;
|
2002-08-18 20:46:45 +04:00
|
|
|
case BOX_TABLE_ROW_GROUP: fprintf(stderr, "BOX_TABLE_ROW_GROUP "); break;
|
2002-06-29 00:14:04 +04:00
|
|
|
case BOX_FLOAT_LEFT: fprintf(stderr, "BOX_FLOAT_LEFT "); break;
|
|
|
|
case BOX_FLOAT_RIGHT: fprintf(stderr, "BOX_FLOAT_RIGHT "); break;
|
2002-06-19 01:24:21 +04:00
|
|
|
default: fprintf(stderr, "Unknown box type ");
|
2002-05-04 23:57:18 +04:00
|
|
|
}
|
2002-06-27 03:27:30 +04:00
|
|
|
if (box->node)
|
|
|
|
fprintf(stderr, "<%s> ", box->node->name);
|
2002-06-19 01:24:21 +04:00
|
|
|
if (box->style)
|
|
|
|
css_dump_style(box->style);
|
2002-08-12 03:01:02 +04:00
|
|
|
if (box->href != 0)
|
|
|
|
fprintf(stderr, " -> '%s'", box->href);
|
2002-06-19 01:24:21 +04:00
|
|
|
fprintf(stderr, "\n");
|
|
|
|
|
2002-05-04 23:57:18 +04:00
|
|
|
for (c = box->children; c != 0; c = c->next)
|
|
|
|
box_dump(c, depth + 1);
|
|
|
|
}
|
|
|
|
|
2002-08-18 20:46:45 +04:00
|
|
|
|
2002-09-08 22:11:56 +04:00
|
|
|
/**
|
|
|
|
* ensure the box tree is correctly nested
|
2002-08-18 20:46:45 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
void box_normalise_block(struct box *block)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
struct box *prev_child = 0;
|
|
|
|
struct box *table;
|
|
|
|
struct css_style *style;
|
|
|
|
|
|
|
|
assert(block->type == BOX_BLOCK || block->type == BOX_TABLE_CELL);
|
|
|
|
|
|
|
|
for (child = block->children; child != 0; prev_child = child, child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_BLOCK:
|
|
|
|
/* ok */
|
|
|
|
box_normalise_block(child);
|
|
|
|
break;
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
box_normalise_inline_container(child);
|
|
|
|
break;
|
|
|
|
case BOX_TABLE:
|
|
|
|
box_normalise_table(child);
|
|
|
|
break;
|
|
|
|
case BOX_INLINE:
|
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
|
|
|
/* should have been wrapped in inline
|
|
|
|
container by convert_xml_to_box() */
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
case BOX_TABLE_ROW_GROUP:
|
|
|
|
case BOX_TABLE_ROW:
|
|
|
|
case BOX_TABLE_CELL:
|
|
|
|
/* insert implied table */
|
|
|
|
style = xcalloc(1, sizeof(struct css_style));
|
|
|
|
memcpy(style, block->style, sizeof(struct css_style));
|
|
|
|
css_cascade(style, &css_blank_style);
|
|
|
|
table = box_create(0, BOX_TABLE, style, block->href);
|
|
|
|
if (prev_child == 0)
|
|
|
|
block->children = table;
|
|
|
|
else
|
|
|
|
prev_child->next = table;
|
|
|
|
while (child != 0 && (
|
|
|
|
child->type == BOX_TABLE_ROW_GROUP ||
|
|
|
|
child->type == BOX_TABLE_ROW ||
|
|
|
|
child->type == BOX_TABLE_CELL)) {
|
|
|
|
box_add_child(table, child);
|
|
|
|
prev_child = child;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
prev_child->next = 0;
|
|
|
|
table->next = child;
|
2002-09-18 23:36:28 +04:00
|
|
|
table->parent = block;
|
2002-08-18 20:46:45 +04:00
|
|
|
box_normalise_table(table);
|
|
|
|
child = table;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void box_normalise_table(struct box *table)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
struct box *prev_child = 0;
|
|
|
|
struct box *row_group;
|
|
|
|
struct css_style *style;
|
|
|
|
|
|
|
|
assert(table->type == BOX_TABLE);
|
|
|
|
|
|
|
|
for (child = table->children; child != 0; prev_child = child, child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_TABLE_ROW_GROUP:
|
|
|
|
/* ok */
|
|
|
|
box_normalise_table_row_group(child);
|
|
|
|
break;
|
|
|
|
case BOX_BLOCK:
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
case BOX_TABLE:
|
|
|
|
case BOX_TABLE_ROW:
|
|
|
|
case BOX_TABLE_CELL:
|
|
|
|
/* insert implied table row group */
|
|
|
|
fprintf(stderr, "inserting implied table row group\n");
|
|
|
|
style = xcalloc(1, sizeof(struct css_style));
|
|
|
|
memcpy(style, table->style, sizeof(struct css_style));
|
|
|
|
css_cascade(style, &css_blank_style);
|
|
|
|
row_group = box_create(0, BOX_TABLE_ROW_GROUP, style, table->href);
|
|
|
|
if (prev_child == 0)
|
|
|
|
table->children = row_group;
|
|
|
|
else
|
|
|
|
prev_child->next = row_group;
|
|
|
|
while (child != 0 && (
|
|
|
|
child->type == BOX_BLOCK ||
|
|
|
|
child->type == BOX_INLINE_CONTAINER ||
|
|
|
|
child->type == BOX_TABLE ||
|
|
|
|
child->type == BOX_TABLE_ROW ||
|
|
|
|
child->type == BOX_TABLE_CELL)) {
|
|
|
|
box_add_child(row_group, child);
|
|
|
|
prev_child = child;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
prev_child->next = 0;
|
|
|
|
row_group->next = child;
|
2002-09-18 23:36:28 +04:00
|
|
|
row_group->parent = table;
|
2002-08-18 20:46:45 +04:00
|
|
|
box_normalise_table_row_group(row_group);
|
|
|
|
child = row_group;
|
|
|
|
break;
|
|
|
|
case BOX_INLINE:
|
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
|
|
|
/* should have been wrapped in inline
|
|
|
|
container by convert_xml_to_box() */
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "%i\n", child->type);
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void box_normalise_table_row_group(struct box *row_group)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
struct box *prev_child = 0;
|
|
|
|
struct box *row;
|
|
|
|
struct css_style *style;
|
|
|
|
|
|
|
|
assert(row_group->type == BOX_TABLE_ROW_GROUP);
|
|
|
|
|
|
|
|
for (child = row_group->children; child != 0; prev_child = child, child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_TABLE_ROW:
|
|
|
|
/* ok */
|
|
|
|
box_normalise_table_row(child);
|
|
|
|
break;
|
|
|
|
case BOX_BLOCK:
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
case BOX_TABLE:
|
|
|
|
case BOX_TABLE_ROW_GROUP:
|
|
|
|
case BOX_TABLE_CELL:
|
|
|
|
/* insert implied table row */
|
|
|
|
style = xcalloc(1, sizeof(struct css_style));
|
|
|
|
memcpy(style, row_group->style, sizeof(struct css_style));
|
|
|
|
css_cascade(style, &css_blank_style);
|
|
|
|
row = box_create(0, BOX_TABLE_ROW, style, row_group->href);
|
|
|
|
if (prev_child == 0)
|
|
|
|
row_group->children = row;
|
|
|
|
else
|
|
|
|
prev_child->next = row;
|
|
|
|
while (child != 0 && (
|
|
|
|
child->type == BOX_BLOCK ||
|
|
|
|
child->type == BOX_INLINE_CONTAINER ||
|
|
|
|
child->type == BOX_TABLE ||
|
|
|
|
child->type == BOX_TABLE_ROW_GROUP ||
|
|
|
|
child->type == BOX_TABLE_CELL)) {
|
|
|
|
box_add_child(row, child);
|
|
|
|
prev_child = child;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
prev_child->next = 0;
|
|
|
|
row->next = child;
|
2002-09-18 23:36:28 +04:00
|
|
|
row->parent = row_group;
|
2002-08-18 20:46:45 +04:00
|
|
|
box_normalise_table_row(row);
|
|
|
|
child = row;
|
|
|
|
break;
|
|
|
|
case BOX_INLINE:
|
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
|
|
|
/* should have been wrapped in inline
|
|
|
|
container by convert_xml_to_box() */
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void box_normalise_table_row(struct box *row)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
struct box *prev_child = 0;
|
|
|
|
struct box *cell;
|
|
|
|
struct css_style *style;
|
2002-09-18 23:36:28 +04:00
|
|
|
unsigned int columns = 0;
|
2002-08-18 20:46:45 +04:00
|
|
|
|
|
|
|
assert(row->type == BOX_TABLE_ROW);
|
|
|
|
|
|
|
|
for (child = row->children; child != 0; prev_child = child, child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_TABLE_CELL:
|
|
|
|
/* ok */
|
|
|
|
box_normalise_block(child);
|
2002-09-18 23:36:28 +04:00
|
|
|
columns += child->columns;
|
2002-08-18 20:46:45 +04:00
|
|
|
break;
|
|
|
|
case BOX_BLOCK:
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
case BOX_TABLE:
|
|
|
|
case BOX_TABLE_ROW_GROUP:
|
|
|
|
case BOX_TABLE_ROW:
|
|
|
|
/* insert implied table cell */
|
|
|
|
style = xcalloc(1, sizeof(struct css_style));
|
|
|
|
memcpy(style, row->style, sizeof(struct css_style));
|
|
|
|
css_cascade(style, &css_blank_style);
|
|
|
|
cell = box_create(0, BOX_TABLE_CELL, style, row->href);
|
|
|
|
if (prev_child == 0)
|
|
|
|
row->children = cell;
|
|
|
|
else
|
|
|
|
prev_child->next = cell;
|
|
|
|
while (child != 0 && (
|
|
|
|
child->type == BOX_BLOCK ||
|
|
|
|
child->type == BOX_INLINE_CONTAINER ||
|
|
|
|
child->type == BOX_TABLE ||
|
|
|
|
child->type == BOX_TABLE_ROW_GROUP ||
|
|
|
|
child->type == BOX_TABLE_ROW)) {
|
|
|
|
box_add_child(cell, child);
|
|
|
|
prev_child = child;
|
|
|
|
child = child->next;
|
|
|
|
}
|
|
|
|
prev_child->next = 0;
|
|
|
|
cell->next = child;
|
2002-09-18 23:36:28 +04:00
|
|
|
cell->parent = row;
|
2002-08-18 20:46:45 +04:00
|
|
|
box_normalise_block(cell);
|
|
|
|
child = cell;
|
2002-09-18 23:36:28 +04:00
|
|
|
columns++;
|
2002-08-18 20:46:45 +04:00
|
|
|
break;
|
|
|
|
case BOX_INLINE:
|
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
|
|
|
/* should have been wrapped in inline
|
|
|
|
container by convert_xml_to_box() */
|
|
|
|
assert(0);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
2002-09-18 23:36:28 +04:00
|
|
|
if (row->parent->parent->columns < columns)
|
|
|
|
row->parent->parent->columns = columns;
|
2002-08-18 20:46:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void box_normalise_inline_container(struct box *cont)
|
|
|
|
{
|
|
|
|
struct box *child;
|
|
|
|
struct box *prev_child = 0;
|
|
|
|
|
|
|
|
assert(cont->type == BOX_INLINE_CONTAINER);
|
|
|
|
|
|
|
|
for (child = cont->children; child != 0; prev_child = child, child = child->next) {
|
|
|
|
switch (child->type) {
|
|
|
|
case BOX_INLINE:
|
|
|
|
/* ok */
|
|
|
|
break;
|
|
|
|
case BOX_FLOAT_LEFT:
|
|
|
|
case BOX_FLOAT_RIGHT:
|
|
|
|
/* ok */
|
|
|
|
assert(child->children != 0);
|
|
|
|
switch (child->children->type) {
|
|
|
|
case BOX_BLOCK:
|
|
|
|
box_normalise_block(child->children);
|
|
|
|
break;
|
|
|
|
case BOX_TABLE:
|
|
|
|
box_normalise_table(child->children);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case BOX_BLOCK:
|
|
|
|
case BOX_INLINE_CONTAINER:
|
|
|
|
case BOX_TABLE:
|
|
|
|
case BOX_TABLE_ROW_GROUP:
|
|
|
|
case BOX_TABLE_ROW:
|
|
|
|
case BOX_TABLE_CELL:
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-08 22:11:56 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* free a box tree recursively
|
|
|
|
*/
|
|
|
|
|
|
|
|
void box_free(struct box *box)
|
|
|
|
{
|
|
|
|
/* free children first */
|
|
|
|
if (box->children != 0)
|
|
|
|
box_free(box->children);
|
|
|
|
|
|
|
|
/* then siblings */
|
|
|
|
if (box->next != 0)
|
|
|
|
box_free(box->next);
|
|
|
|
|
|
|
|
/* last this box */
|
|
|
|
if (box->style != 0)
|
|
|
|
free(box->style);
|
|
|
|
if (box->text != 0)
|
|
|
|
free(box->text);
|
|
|
|
/* only free href if we're the top most user */
|
|
|
|
if (box->href != 0 && (box->parent == 0 || box->parent->href != box->href))
|
|
|
|
free(box->href);
|
|
|
|
}
|