aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-09-09 19:58:49 +0200
committerbptato <nincsnevem662@gmail.com>2023-09-09 19:58:49 +0200
commitedf4321c7a2b206ff8f8fa3e69174e05c4c414bd (patch)
tree11d4ac29d90f9bbe41514c0d14ffd609ae03a0b2
parentef43cb35d55370690f674b2234124c73cfdc9f15 (diff)
downloadchawan-edf4321c7a2b206ff8f8fa3e69174e05c4c414bd.tar.gz
layout: add table rowspan
-rw-r--r--src/layout/box.nim1
-rw-r--r--src/layout/engine.nim67
-rw-r--r--todo2
3 files changed, 57 insertions, 13 deletions
diff --git a/src/layout/box.nim b/src/layout/box.nim
index 190313b9..33817294 100644
--- a/src/layout/box.nim
+++ b/src/layout/box.nim
@@ -165,7 +165,6 @@ type
CellWrapper* = ref object
builder*: TableCellBoxBuilder
box*: BlockBox
- rowi*: int
coli*: int
colspan*: int
rowspan*: int
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 6236825d..bdc51122 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -1,3 +1,4 @@
+import algorithm
import math
import options
import unicode
@@ -1135,12 +1136,53 @@ proc buildTableCell(viewport: Viewport, builder: TableCellBoxBuilder,
tableCell.buildLayout(builder)
return tableCell
+# Sort growing cells, and filter out cells that have grown to their intended
+# rowspan.
+proc sortGrowing(pctx: var TableContext) =
+ var i = 0
+ for j in 0 ..< pctx.growing.len:
+ if pctx.growing[i].grown == 0:
+ continue
+ if j != i:
+ pctx.growing[i] = pctx.growing[j]
+ inc i
+ pctx.growing.setLen(i)
+ pctx.growing.sort(proc(a, b: CellWrapper): int =
+ cmp(a.coli, b.coli))
+
+# Grow cells with a rowspan > 1 (to occupy their place in a new row).
+proc growRowspan(pctx: var TableContext, ctx: var RowContext,
+ growi, i, n: var int, growlen: int) =
+ while growi < growlen:
+ let cellw = pctx.growing[growi]
+ if cellw.coli > n:
+ break
+ dec cellw.grown
+ let rowspanFiller = CellWrapper(
+ colspan: cellw.colspan,
+ rowspan: cellw.rowspan,
+ coli: n
+ )
+ ctx.cells.add(nil)
+ ctx.cells[i] = rowspanFiller
+ for i in n ..< n + cellw.colspan:
+ ctx.width += pctx.cols[i].width
+ ctx.width += pctx.inlinespacing * 2
+ n += cellw.colspan
+ inc i
+ inc growi
+
proc preBuildTableRow(pctx: var TableContext, box: TableRowBoxBuilder,
- parent: BlockBox, i: int): RowContext =
+ parent: BlockBox): RowContext =
var ctx = RowContext(builder: box, cells: newSeq[CellWrapper](box.children.len))
var n = 0
var i = 0
+ var growi = 0
+ # this increases in the loop, but we only want to check growing cells that
+ # were added by previous rows.
+ let growlen = pctx.growing.len
for child in box.children:
+ pctx.growRowspan(ctx, growi, i, n, growlen)
assert child.computed{"display"} == DISPLAY_TABLE_CELL
let cellbuilder = TableCellBoxBuilder(child)
let colspan = cellbuilder.computed{"-cha-colspan"}
@@ -1157,11 +1199,10 @@ proc preBuildTableRow(pctx: var TableContext, box: TableRowBoxBuilder,
builder: cellbuilder,
colspan: colspan,
rowspan: rowspan,
- rowi: i,
coli: n
)
ctx.cells[i] = wrapper
- if rowspan != 1:
+ if rowspan > 1:
pctx.growing.add(wrapper)
wrapper.grown = rowspan - 1
if pctx.cols.len < n + colspan:
@@ -1200,6 +1241,10 @@ proc preBuildTableRow(pctx: var TableContext, box: TableRowBoxBuilder,
ctx.width += pctx.inlinespacing
n += colspan
inc i
+ pctx.growRowspan(ctx, growi, i, n, growlen)
+ pctx.sortGrowing()
+ for i in 0 ..< ctx.cells.len:
+ doAssert ctx.cells[i] != nil, $i
ctx.ncols = n
return ctx
@@ -1216,7 +1261,7 @@ proc buildTableRow(pctx: TableContext, ctx: RowContext, parent: BlockBox,
w += pctx.cols[i].width
# Add inline spacing for merged columns.
w += pctx.inlinespacing * (cellw.colspan - 1) * 2
- if cellw.reflow:
+ if cellw.reflow and cellw.builder != nil:
#TODO TODO TODO this is a hack, and it doesn't even work properly
#TODO 2: maybe we can remove this now?
#let ocomputed = cellw.builder.computed
@@ -1231,17 +1276,19 @@ proc buildTableRow(pctx: TableContext, ctx: RowContext, parent: BlockBox,
#cellw.builder.computed = ocomputed
w = max(w, cell.width)
x += pctx.inlinespacing
- cell.offset.x += x
+ if cell != nil:
+ cell.offset.x += x
x += pctx.inlinespacing
x += w
n += cellw.colspan
const HasNoBaseline = {
VERTICAL_ALIGN_TOP, VERTICAL_ALIGN_MIDDLE, VERTICAL_ALIGN_BOTTOM
}
- if cell.computed{"vertical-align"}.keyword notin HasNoBaseline: # baseline
- baseline = max(cell.firstBaseline, baseline)
- row.nested.add(cell)
- row.height = max(row.height, cell.height)
+ if cell != nil:
+ if cell.computed{"vertical-align"}.keyword notin HasNoBaseline: # baseline
+ baseline = max(cell.firstBaseline, baseline)
+ row.nested.add(cell)
+ row.height = max(row.height, cell.height)
for cell in row.nested:
cell.height = min(cell.height, row.height)
for cell in row.nested:
@@ -1300,7 +1347,7 @@ proc preBuildTableRows(ctx: var TableContext, builder: TableBoxBuilder,
ctx.caption = TableCaptionBoxBuilder(row)
else:
let row = TableRowBoxBuilder(row)
- let rctx = ctx.preBuildTableRow(row, table, i)
+ let rctx = ctx.preBuildTableRow(row, table)
ctx.rows.add(rctx)
ctx.maxwidth = max(rctx.width, ctx.maxwidth)
inc i
diff --git a/todo b/todo
index 2b9a7162..72d3c57d 100644
--- a/todo
+++ b/todo
@@ -78,8 +78,6 @@ layout engine:
- important: floats (way too many websites look very ugly without them)
- make background-color a property of inline boxes, not words. (this would
require implementing actual inline boxes)
-- table rowspan
- * there's already a `growing' list in table context, just use that...
- overflow
- incremental layout & layout caching
* first for tree generation, then for layout.