about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-10-14 12:04:51 +0200
committerbptato <nincsnevem662@gmail.com>2023-10-14 12:04:51 +0200
commit95c5438a619783230b26d0ebf18a56f425c3dbf5 (patch)
tree0378071843081b5d0efa51ec7a9865e94c5dbf4f
parent4145bb7097ad748ded0a58fc502141a9c4e433eb (diff)
downloadchawan-95c5438a619783230b26d0ebf18a56f425c3dbf5.tar.gz
layout: get rid of baseline, firstBaseline functions
-rw-r--r--src/layout/box.nim10
-rw-r--r--src/layout/engine.nim37
2 files changed, 22 insertions, 25 deletions
diff --git a/src/layout/box.nim b/src/layout/box.nim
index 28dc951c..ff99a298 100644
--- a/src/layout/box.nim
+++ b/src/layout/box.nim
@@ -112,6 +112,11 @@ type
     width*: LayoutUnit
     lines*: seq[LineBox]
 
+    # baseline of the first line box
+    firstBaseline*: LayoutUnit
+    # baseline of the last line box
+    baseline*: LayoutUnit
+
     # this is actually xminwidth.
     minwidth*: LayoutUnit
 
@@ -152,6 +157,11 @@ type
     # in table cells.
     xminwidth*: LayoutUnit
 
+    # baseline of the first line box of all descendants
+    firstBaseline*: LayoutUnit
+    # baseline of the last line box of all descendants
+    baseline*: LayoutUnit
+
   ListItemBox* = ref object of BlockBox
     marker*: InlineContext
 
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 6ca54e46..a0a64ef1 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -319,8 +319,11 @@ proc finish(state: var InlineState, computed: CSSComputedValues) =
     for i in 0 ..< state.ictx.lines.len - 1:
       let line = state.ictx.lines[i]
       state.horizontalAlignLine(line, computed, last = false)
-    let line = state.ictx.lines[^1]
-    state.horizontalAlignLine(line, computed, last = true)
+    let lastLine = state.ictx.lines[^1]
+    state.horizontalAlignLine(lastLine, computed, last = true)
+    state.ictx.baseline = lastLine.offset.y + lastLine.baseline
+    let firstLine = state.ictx.lines[0]
+    state.ictx.firstBaseline = firstLine.offset.y + firstLine.baseline
 
 func minwidth(atom: InlineAtom): LayoutUnit =
   if atom of InlineBlockBox:
@@ -851,6 +854,8 @@ proc applyInlineDimensions(box: BlockBox) =
   box.applyWidth(box.inline.width)
   box.width += box.padding_left
   box.width += box.padding_right
+  box.baseline = box.inline.offset.y + box.inline.baseline
+  box.firstBaseline = box.inline.offset.y + box.inline.firstBaseline
 
 # Builder only contains inline boxes.
 proc buildInlineLayout(parent: BlockBox, children: seq[BoxBuilder]) =
@@ -866,29 +871,6 @@ proc buildBlockLayout(box: BlockBox, children: seq[BoxBuilder], node: StyledNode
   if positioned:
     discard box.viewport.positioned.pop()
 
-#TODO this is horribly inefficient, and should be inherited like xminwidth
-func firstBaseline(box: BlockBox): LayoutUnit =
-  if box.inline != nil:
-    if box.inline.lines.len > 0:
-      return box.offset.y + box.inline.lines[0].baseline
-    return box.offset.y
-  if box.nested.len > 0:
-    return box.offset.y + box.nested[^1].firstBaseline
-  box.offset.y
-
-#TODO ditto
-func baseline(box: BlockBox): LayoutUnit =
-  if box.inline != nil:
-    var y: LayoutUnit = 0
-    for line in box.inline.lines:
-      if line == box.inline.lines[^1]:
-        return box.offset.y + y + line.baseline
-      y += line.height
-    return box.offset.y + box.height
-  if box.nested.len > 0:
-    return box.offset.y + box.nested[^1].baseline
-  box.offset.y
-
 proc buildLayout(box: BlockBox, builder: BlockBoxBuilder) =
   if builder.inlinelayout:
     box.buildInlineLayout(builder.children)
@@ -1111,6 +1093,7 @@ proc positionBlocks(box: BlockBox) =
     box.margin_top = margin_todo.sum()
     applyChildPosition(box, child, x, y, margin_todo, maxChildWidth,
       childHeight)
+    box.firstBaseline = child.offset.y + child.firstBaseline
     inc i
 
   while i < box.nested.len:
@@ -1123,6 +1106,10 @@ proc positionBlocks(box: BlockBox) =
       childHeight)
     inc i
 
+  if box.nested.len > 0:
+    let lastNested = box.nested[^1]
+    box.baseline = lastNested.offset.y + lastNested.baseline
+
   margin_todo.append(box.margin_bottom)
   box.margin_bottom = margin_todo.sum()