[project @ 2005-01-10 21:35:33 by rjw]

Vertical alignment for table cells

svn path=/import/netsurf/; revision=1437
This commit is contained in:
Richard Wilson 2005-01-10 21:35:34 +00:00
parent 0890fd790d
commit 7b1c6c0ea8
2 changed files with 81 additions and 10 deletions

View File

@ -8,7 +8,7 @@ html { display: block; }
head { display: none; }
body { display: block; margin: 5px; }
body { display: block; padding: 5px; }
div { display: block; }
div[align=left] > * { margin-right: auto; }
@ -71,16 +71,16 @@ table[align=right] { float: right; text-align: left; }
caption { display: table-caption; }
thead { display: table-header-group; }
tfoot { display: table-footer-group; }
tbody { display: table-row-group; }
thead { display: table-header-group; vertical-align: middle; }
tfoot { display: table-footer-group; vertical-align: middle; }
tbody { display: table-row-group; vertical-align: middle; }
colgroup { display: table-column-group; }
col { display: table-column; }
tr { display: table-row; }
th { display: table-cell; font-weight: bold; }
td { display: table-cell; text-align: left; }
tr { display: table-row; vertical-align: middle; }
th { display: table-cell; font-weight: bold; vertical-align: middle;}
td { display: table-cell; text-align: left; vertical-align: middle;}
th[nowrap], td[nowrap] { white-space: nowrap; }
a:link { color: #00f; text-decoration: underline; }
@ -164,3 +164,15 @@ fieldset { display: block; border: thin solid #888; }
[align=left] { text-align: left; }
[align=center] { text-align: center; }
[align=right] { text-align: right; }
col[valign=top], colgroup[valign=top], tbody[valign=top], td[valign=top], tfoot[valign=top], th[valign=top], thead[valign=top], tr[valign=top] { vertical-align: top; }
col[valign=middle], colgroup[valign=middle], tbody[valign=middle], td[valign=middle], tfoot[valign=middle], th[valign=middle], thead[valign=middle], tr[valign=middle] { vertical-align: middle; }
col[valign=bottom], colgroup[valign=bottom], tbody[valign=bottom], td[valign=bottom], tfoot[valign=bottom], th[valign=bottom], thead[valign=bottom], tr[valign=bottom] { vertical-align: bottom; }
tr[align=left] > td, th { text-align: left }
tr[align=center] > td, th { text-align: center }
tr[align=right] > td, th { text-align: right }
tr[valign=top] > td, th { vertical-align: top; }
tr[valign=middle] > td, th { vertical-align: middle; }
tr[valign=bottom] > td, th { vertical-align: bottom; }

View File

@ -2,6 +2,7 @@
* This file is part of NetSurf, http://netsurf.sourceforge.net/
* Licensed under the GNU General Public License,
* http://www.opensource.org/licenses/gpl-license
* Copyright 2005 Richard Wilson <info@tinct.net>
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
*/
@ -59,6 +60,7 @@ static bool layout_float(struct box *b, int width, pool box_pool);
static void place_float_below(struct box *c, int width, int cx, int y,
struct box *cont);
static bool layout_table(struct box *box, int available_width, pool box_pool);
static void layout_move_children(struct box *box, int x, int y);
static bool calculate_widths(struct box *box);
static bool calculate_block_widths(struct box *box, int *min, int *max,
int *max_sum);
@ -1204,6 +1206,7 @@ bool layout_table(struct box *table, int available_width,
int spare_width;
int relative_sum = 0;
int border_spacing_h = 0, border_spacing_v = 0;
int spare_height;
struct box *c;
struct box *row;
struct box *row_group;
@ -1448,6 +1451,9 @@ bool layout_table(struct box *table, int available_width,
free(xs);
return false;
}
/* preserve c->padding[BOTTOM] in c->descendant_y1 which is not used at this
* point in time. */
c->descendant_y1 = c->padding[BOTTOM];
if (c->style->height.height ==
CSS_HEIGHT_LENGTH) {
/* some sites use height="1" or similar
@ -1471,10 +1477,10 @@ bool layout_table(struct box *table, int available_width,
row_span_cell[c->start_column + i] = 0;
}
row_span_cell[c->start_column] = c;
c->height = -border_spacing_v -
c->padding[BOTTOM] = -border_spacing_v -
c->border[TOP] -
c->padding[TOP] -
c->padding[BOTTOM] -
c->height -
c->border[BOTTOM];
}
for (i = 0; i != columns; i++)
@ -1501,7 +1507,7 @@ bool layout_table(struct box *table, int available_width,
else
excess_y[i] = 0;
if (row_span_cell[i] != 0)
row_span_cell[i]->height += row_height +
row_span_cell[i]->padding[BOTTOM] += row_height +
border_spacing_v;
}
@ -1518,6 +1524,41 @@ bool layout_table(struct box *table, int available_width,
table_height += row_group_height;
}
/* perform vertical alignment */
for (row_group = table->children; row_group; row_group = row_group->next) {
for (row = row_group->children; row; row = row->next) {
for (c = row->children; c; c = c->next) {
/* unextended bottom padding is in c->descendant_y1 */
spare_height = c->padding[BOTTOM] - c->descendant_y1;
switch (c->style->vertical_align.type) {
case CSS_VERTICAL_ALIGN_SUB:
case CSS_VERTICAL_ALIGN_SUPER:
case CSS_VERTICAL_ALIGN_TEXT_TOP:
case CSS_VERTICAL_ALIGN_TEXT_BOTTOM:
case CSS_VERTICAL_ALIGN_LENGTH:
case CSS_VERTICAL_ALIGN_PERCENT:
case CSS_VERTICAL_ALIGN_BASELINE:
/* todo: baseline alignment, for now just use ALIGN_TOP */
case CSS_VERTICAL_ALIGN_TOP:
break;
case CSS_VERTICAL_ALIGN_MIDDLE:
c->padding[TOP] += spare_height / 2;
c->padding[BOTTOM] -= spare_height / 2;
layout_move_children(c, 0, spare_height / 2);
break;
case CSS_VERTICAL_ALIGN_BOTTOM:
c->padding[TOP] += spare_height;
c->padding[BOTTOM] -= spare_height;
layout_move_children(c, 0, spare_height);
break;
case CSS_VERTICAL_ALIGN_INHERIT:
assert(0);
break;
}
}
}
}
free(col);
free(excess_y);
free(row_span);
@ -1531,6 +1572,24 @@ bool layout_table(struct box *table, int available_width,
}
/**
* Moves the children of a box by a specified amount
*
* \param box top of tree of boxes
* \param x the amount to move children by horizontally
* \param y the amount to move children by vertically
*/
void layout_move_children(struct box *box, int x, int y) {
assert(box);
for (box = box->children; box; box = box->next) {
box->x += x;
box->y += y;
}
}
/**
* Find min, max widths required by boxes.
*