2002-05-04 23:57:18 +04:00
|
|
|
/**
|
2002-05-22 01:32:35 +04:00
|
|
|
* $Id: box.c,v 1.4 2002/05/21 21:32:35 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
void box_add_child(struct box * parent, struct box * child);
|
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-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)
|
|
|
|
{
|
|
|
|
if (parent->children) /* has children already */
|
|
|
|
parent->last->next = child;
|
|
|
|
else /* this is the first child */
|
|
|
|
parent->children = child;
|
|
|
|
|
|
|
|
parent->last = child;
|
|
|
|
child->parent = parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct box * 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)
|
|
|
|
{
|
|
|
|
struct box * box;
|
|
|
|
struct box * inline_container_c;
|
|
|
|
struct css_style * style;
|
|
|
|
xmlNode * c;
|
|
|
|
|
|
|
|
if (n->type == XML_ELEMENT_NODE) {
|
|
|
|
/* work out the style for this element */
|
|
|
|
*selector = xrealloc(*selector, (depth + 1) * sizeof(struct css_selector));
|
|
|
|
(*selector)[depth].element = n->name;
|
|
|
|
(*selector)[depth].class = (*selector)[depth].id = 0;
|
|
|
|
|
2002-05-22 01:32:35 +04:00
|
|
|
style = box_get_style(stylesheet, parent_style, n, *selector, depth + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
box = calloc(1, sizeof(struct box));
|
|
|
|
box->node = n;
|
|
|
|
box_add_child(inline_container, box);
|
|
|
|
if (n->type == XML_TEXT_NODE) {
|
|
|
|
box->type = BOX_INLINE;
|
|
|
|
box->text = squash_whitespace(n->content);
|
|
|
|
} else {
|
|
|
|
box->type = BOX_FLOAT;
|
|
|
|
box->style = style;
|
|
|
|
inline_container_c = 0;
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
|
|
|
inline_container_c = xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, box, inline_container_c);
|
2002-05-05 01:17:06 +04:00
|
|
|
}
|
|
|
|
|
2002-05-22 01:32:35 +04:00
|
|
|
} else 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 */
|
|
|
|
box = xcalloc(1, sizeof(struct box));
|
|
|
|
box->node = n;
|
|
|
|
box->type = BOX_BLOCK;
|
|
|
|
box->style = style;
|
|
|
|
box_add_child(parent, box);
|
|
|
|
inline_container_c = 0;
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
|
|
|
inline_container_c = xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, box, inline_container_c);
|
|
|
|
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)
|
|
|
|
inline_container = xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, parent, inline_container);
|
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_TABLE:
|
|
|
|
box = xcalloc(1, sizeof(struct box));
|
|
|
|
box->node = n;
|
|
|
|
box->type = BOX_TABLE;
|
|
|
|
box->style = style;
|
|
|
|
box_add_child(parent, box);
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
|
|
|
xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, box, 0);
|
|
|
|
inline_container = 0;
|
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_TABLE_ROW:
|
2002-05-22 01:32:35 +04:00
|
|
|
assert(parent->type == BOX_TABLE);
|
2002-05-04 23:57:18 +04:00
|
|
|
box = xcalloc(1, sizeof(struct box));
|
|
|
|
box->node = n;
|
|
|
|
box->type = BOX_TABLE_ROW;
|
|
|
|
box->style = style;
|
|
|
|
box_add_child(parent, box);
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
|
|
|
xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, box, 0);
|
|
|
|
inline_container = 0;
|
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_TABLE_CELL:
|
2002-05-22 01:32:35 +04:00
|
|
|
assert(parent->type == BOX_TABLE_ROW);
|
2002-05-04 23:57:18 +04:00
|
|
|
box = xcalloc(1, sizeof(struct box));
|
|
|
|
box->node = n;
|
|
|
|
box->type = BOX_TABLE_CELL;
|
|
|
|
box->style = style;
|
|
|
|
box_add_child(parent, box);
|
|
|
|
inline_container_c = 0;
|
|
|
|
for (c = n->children; c != 0; c = c->next)
|
|
|
|
inline_container_c = xml_to_box(c, style, stylesheet,
|
|
|
|
selector, depth + 1, box, inline_container_c);
|
|
|
|
inline_container = 0;
|
|
|
|
break;
|
|
|
|
case CSS_DISPLAY_NONE:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inline_container;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
xmlChar * s;
|
|
|
|
|
|
|
|
memcpy(style, parent_style, sizeof(struct css_style));
|
|
|
|
css_get_style(stylesheet, selector, depth, style);
|
|
|
|
|
|
|
|
if ((s = xmlGetProp(n, "width"))) {
|
|
|
|
if (strrchr(s, '%'))
|
|
|
|
style->width.width = CSS_WIDTH_PERCENT,
|
|
|
|
style->width.value.percent = atof(s);
|
|
|
|
else
|
|
|
|
style->width.width = CSS_WIDTH_LENGTH,
|
|
|
|
style->width.value.length.unit = CSS_UNIT_PX,
|
|
|
|
style->width.value.length.value = atof(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((s = xmlGetProp(n, "style"))) {
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
printf(" ");
|
|
|
|
|
|
|
|
printf("x%li y%li w%li h%li ", box->x, box->y, box->width, box->height);
|
|
|
|
|
|
|
|
switch (box->type) {
|
|
|
|
case BOX_BLOCK: printf("BOX_BLOCK <%s>\n", box->node->name); break;
|
|
|
|
case BOX_INLINE_CONTAINER: printf("BOX_INLINE_CONTAINER\n"); break;
|
|
|
|
case BOX_INLINE: printf("BOX_INLINE '%s'\n", box->text); break;
|
|
|
|
case BOX_TABLE: printf("BOX_TABLE <%s>\n", box->node->name); break;
|
|
|
|
case BOX_TABLE_ROW: printf("BOX_TABLE_ROW <%s>\n", box->node->name); break;
|
|
|
|
case BOX_TABLE_CELL: printf("BOX_TABLE_CELL <%s>\n", box->node->name); break;
|
2002-05-22 01:32:35 +04:00
|
|
|
case BOX_FLOAT: printf("BOX_FLOAT <%s>\n", box->node->name); break;
|
2002-05-04 23:57:18 +04:00
|
|
|
default: printf("Unknown box type\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c = box->children; c != 0; c = c->next)
|
|
|
|
box_dump(c, depth + 1);
|
|
|
|
}
|
|
|
|
|