From e78ea408f3fee74beae600d1edeaa22c863953b9 Mon Sep 17 00:00:00 2001 From: James Bursa Date: Fri, 28 Jun 2002 20:14:04 +0000 Subject: [project @ 2002-06-28 20:14:04 by bursa] Changed float representation in box tree to implement floated tables. svn path=/import/netsurf/; revision=25 --- render/box.c | 48 ++++++++++++++++++++++----------- render/box.h | 5 ++-- render/css.c | 4 +-- render/layout.c | 75 ++++++++++++++++++++++++++------------------------- render/render.c | 7 ++--- render/test/test1.css | 7 ++--- 6 files changed, 84 insertions(+), 62 deletions(-) diff --git a/render/box.c b/render/box.c index e94f3909c..16e979bdf 100644 --- a/render/box.c +++ b/render/box.c @@ -1,5 +1,5 @@ /** - * $Id: box.c,v 1.8 2002/06/26 23:27:30 bursa Exp $ + * $Id: box.c,v 1.9 2002/06/28 20:14:04 bursa Exp $ */ #include @@ -85,6 +85,8 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css if ((s = (char *) xmlGetProp(n, (xmlChar *) "class"))) (*selector)[depth].class = s; style = box_get_style(stylesheet, parent_style, n, *selector, depth + 1); + if (style->display == CSS_DISPLAY_NONE) + return inline_container; } if (n->type == XML_TEXT_NODE || @@ -116,30 +118,31 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css break; case BOX_BLOCK: case BOX_TABLE_CELL: - case BOX_FLOAT: + case BOX_FLOAT_LEFT: + case BOX_FLOAT_RIGHT: break; default: assert(0); } 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 = box_create(n, BOX_INLINE, 0); box->text = squash_whitespace((char *) n->content); box->length = strlen(box->text); + box_add_child(inline_container, box); } 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); + box = box_create(0, BOX_FLOAT_LEFT, 0); + 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; } + } - } else if (n->type == XML_ELEMENT_NODE) { + if (n->type == XML_ELEMENT_NODE) { switch (style->display) { case CSS_DISPLAY_BLOCK: /* blocks get a node in the box tree */ switch (parent->type) { @@ -164,7 +167,8 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css break; case BOX_BLOCK: case BOX_TABLE_CELL: - case BOX_FLOAT: + case BOX_FLOAT_LEFT: + case BOX_FLOAT_RIGHT: break; default: assert(0); @@ -223,7 +227,6 @@ struct box * xml_to_box(xmlNode * n, struct css_style * parent_style, struct css selector, depth + 1, box, inline_container_c); inline_container = 0; break; - case CSS_DISPLAY_NONE: default: break; } @@ -246,6 +249,18 @@ struct css_style * box_get_style(struct css_stylesheet * stylesheet, struct css_ memcpy(style, parent_style, sizeof(struct css_style)); css_get_style(stylesheet, selector, depth, style); + 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; + } + } + if ((s = (char *) xmlGetProp(n, (xmlChar *) "clear"))) { if (strcmp(s, "all") == 0) style->clear = CSS_CLEAR_BOTH; else if (strcmp(s, "left") == 0) style->clear = CSS_CLEAR_LEFT; @@ -298,7 +313,8 @@ void box_dump(struct box * box, unsigned int depth) case BOX_TABLE_ROW: fprintf(stderr, "BOX_TABLE_ROW "); break; case BOX_TABLE_CELL: fprintf(stderr, "BOX_TABLE_CELL [colspan %i] ", box->colspan); break; - case BOX_FLOAT: fprintf(stderr, "BOX_FLOAT "); break; + case BOX_FLOAT_LEFT: fprintf(stderr, "BOX_FLOAT_LEFT "); break; + case BOX_FLOAT_RIGHT: fprintf(stderr, "BOX_FLOAT_RIGHT "); break; default: fprintf(stderr, "Unknown box type "); } if (box->node) diff --git a/render/box.h b/render/box.h index 76a3e69ac..1ee923ddf 100644 --- a/render/box.h +++ b/render/box.h @@ -1,5 +1,5 @@ /** - * $Id: box.h,v 1.4 2002/06/26 23:27:30 bursa Exp $ + * $Id: box.h,v 1.5 2002/06/28 20:14:04 bursa Exp $ */ /** @@ -8,7 +8,8 @@ typedef enum { BOX_BLOCK, BOX_INLINE_CONTAINER, BOX_INLINE, - BOX_TABLE, BOX_TABLE_ROW, BOX_TABLE_CELL, BOX_FLOAT + BOX_TABLE, BOX_TABLE_ROW, BOX_TABLE_CELL, + BOX_FLOAT_LEFT, BOX_FLOAT_RIGHT } box_type; struct box { diff --git a/render/css.c b/render/css.c index 574273737..af0cd5958 100644 --- a/render/css.c +++ b/render/css.c @@ -1,5 +1,5 @@ /** - * $Id: css.c,v 1.7 2002/06/26 12:19:24 bursa Exp $ + * $Id: css.c,v 1.8 2002/06/28 20:14:04 bursa Exp $ */ #include @@ -75,7 +75,7 @@ const struct css_style css_empty_style = { const struct css_style css_blank_style = { CSS_CLEAR_NONE, - CSS_DISPLAY_BLOCK, + CSS_DISPLAY_INLINE, CSS_FLOAT_NONE, { CSS_FONT_SIZE_INHERIT, { { 1, CSS_UNIT_EM } } }, { CSS_HEIGHT_AUTO, { 1, CSS_UNIT_EM } }, diff --git a/render/layout.c b/render/layout.c index 5b7de47ce..bffa26675 100644 --- a/render/layout.c +++ b/render/layout.c @@ -1,5 +1,5 @@ /** - * $Id: layout.c,v 1.10 2002/06/26 23:27:30 bursa Exp $ + * $Id: layout.c,v 1.11 2002/06/28 20:14:04 bursa Exp $ */ #include @@ -70,10 +70,28 @@ signed long len(struct css_length * length, struct css_style * style) void layout_document(struct box * doc, unsigned long width) { doc->float_children = 0; - layout_block(doc, width, doc, 0, 0); + layout_node(doc, width, doc, 0, 0); } +void layout_node(struct box * box, unsigned long width, struct box * cont, + unsigned long cx, unsigned long cy) +{ + 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); + } +} + /** * layout_block -- position block and recursively layout children * @@ -88,7 +106,7 @@ void layout_block(struct box * box, unsigned long width, struct box * cont, { struct css_style * style = box->style; - assert(box->type == BOX_BLOCK || box->type == BOX_FLOAT); + assert(box->type == BOX_BLOCK); switch (style->width.width) { case CSS_WIDTH_LENGTH: @@ -127,7 +145,8 @@ unsigned long layout_block_children(struct box * box, unsigned long width, struc struct box * c; unsigned long y = 0; - assert(box->type == BOX_BLOCK || box->type == BOX_FLOAT || box->type == BOX_TABLE_CELL); + assert(box->type == BOX_BLOCK || box->type == BOX_FLOAT_LEFT || + box->type == BOX_FLOAT_RIGHT || box->type == BOX_TABLE_CELL); for (c = box->children; c != 0; c = c->next) { if (c->style && c->style->clear != CSS_CLEAR_NONE) { @@ -149,31 +168,11 @@ unsigned long layout_block_children(struct box * box, unsigned long width, struc (c->style->clear == CSS_CLEAR_RIGHT && right != 0) || (c->style->clear == CSS_CLEAR_BOTH && (left != 0 || right != 0))); } - switch (c->type) { - case BOX_BLOCK: - layout_block(c, width, cont, cx, cy + y); - c->x = 0; - c->y = y; - y += c->height; - break; - case BOX_INLINE_CONTAINER: - layout_inline_container(c, width, cont, cx, cy + y); - c->x = 0; - c->y = y; - y += c->height; - break; - case BOX_TABLE: - layout_table(c, width, cont, cx, cy + y); - c->x = 0; - c->y = y; - y += c->height; - break; - default: - fprintf(stderr, "%s -> %s\n", - box->node ? (const char *) box->node->name : "()", - c->node ? (const char *) c->node->name : "()"); - die("block child not block, table, or inline container"); - } + + layout_node(c, width, cont, cx, cy + y); + c->x = 0; + c->y = y; + y += c->height; } return y; } @@ -196,10 +195,10 @@ void find_sides(struct box * fl, unsigned long y0, unsigned long y1, *left = *right = 0; for (; fl; fl = fl->next_float) { if (y0 <= fl->y + fl->height && fl->y <= y1) { - if (fl->style->float_ == CSS_FLOAT_LEFT && *x0 < fl->x + fl->width) { + if (fl->type == BOX_FLOAT_LEFT && *x0 < fl->x + fl->width) { *x0 = fl->x + fl->width; *left = fl; - } else if (fl->style->float_ == CSS_FLOAT_RIGHT && fl->x < *x1) { + } else if (fl->type == BOX_FLOAT_RIGHT && fl->x < *x1) { *x1 = fl->x; *right = fl; } @@ -271,7 +270,7 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long /* 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) { - assert(b->type == BOX_INLINE || b->type == BOX_FLOAT); + assert(b->type == BOX_INLINE || b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT); if (b->type == BOX_INLINE) { h = line_height(b->style ? b->style : b->parent->parent->style); b->height = h; @@ -296,12 +295,16 @@ struct box * layout_line(struct box * first, unsigned long width, unsigned long move_y = 1; /* fprintf(stderr, "layout_line: '%.*s' %li %li\n", b->length, b->text, xp, x); */ } else { - b->float_children = 0; + d = b->children; + d->float_children = 0; /* css_dump_style(b->style); */ - layout_block(b, width, b, 0, 0); + layout_node(d, width, d, 0, 0); + d->x = d->y = 0; + b->width = d->width; + b->height = d->height; 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 */ - if (b->style->float_ == CSS_FLOAT_LEFT) { + if (b->type == BOX_FLOAT_LEFT) { b->x = x0; x0 += b->width; left = b; @@ -417,7 +420,7 @@ void place_float_below(struct box * c, unsigned long width, unsigned long y, str } } while (!((left == 0 && right == 0) || (c->width < x1 - x0))); - if (c->style->float_ == CSS_FLOAT_LEFT) { + if (c->type == BOX_FLOAT_LEFT) { c->x = x0; } else { c->x = x1 - c->width; diff --git a/render/render.c b/render/render.c index e4d5b1b76..6105593a2 100644 --- a/render/render.c +++ b/render/render.c @@ -1,5 +1,5 @@ /** - * $Id: render.c,v 1.16 2002/06/26 23:27:30 bursa Exp $ + * $Id: render.c,v 1.17 2002/06/28 20:14:04 bursa Exp $ */ #include @@ -101,7 +101,8 @@ void render_dump(struct box * box, unsigned long x, unsigned long y) case BOX_TABLE: case BOX_TABLE_ROW: case BOX_TABLE_CELL: - case BOX_FLOAT: + case BOX_FLOAT_LEFT: + case BOX_FLOAT_RIGHT: case BOX_BLOCK: if (box->node) name = (const char *) box->node->name; break; case BOX_INLINE: @@ -133,7 +134,7 @@ void render_dump(struct box * box, unsigned long x, unsigned long y) fflush(stdout); for (c = box->children; c != 0; c = c->next) - if (c->type != BOX_FLOAT) + if (c->type != BOX_FLOAT_LEFT && c->type != BOX_FLOAT_RIGHT) render_dump(c, x + box->x, y + box->y); for (c = box->float_children; c != 0; c = c->next_float) diff --git a/render/test/test1.css b/render/test/test1.css index faf1354b8..a399bbadf 100644 --- a/render/test/test1.css +++ b/render/test/test1.css @@ -5,10 +5,10 @@ dl, dt, fieldset, form, h1, h2, h3, h4, h5, h6, html, object, ol, p, ul, hr, menu, pre { display: block } -a, abbr, acronym, b, code, +/* a, abbr, acronym, b, code, em, i, q, s, strong, u, font, span, -var { display: inline } -li { display: list-item } +var { display: inline } */ +li { display: /* list-item */ block } head { display: none } table { display: table } tr { display: table-row } @@ -19,5 +19,6 @@ col { display: table-column } colgroup { display: table-column-group } td, th { display: table-cell } caption { display: table-caption } +img { display:none} h1 { font-size: xx-large; text-align: center } -- cgit v1.2.3