about summary refs log tree commit diff stats
path: root/src/layout
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-01-01 03:48:53 +0100
committerbptato <nincsnevem662@gmail.com>2023-01-01 04:10:45 +0100
commitea3331690928fd61fa9d0e7457ba017a4fffc363 (patch)
treeb0193bd91dc6976625f0a7fc1f3f52d10e82e5d7 /src/layout
parent2f8ab98958d17677298e285cffd2c72d5c42eaf7 (diff)
downloadchawan-ea3331690928fd61fa9d0e7457ba017a4fffc363.tar.gz
layout/engine: fix text-align: cha-* for boxes with shrink
In shrink, we can only take up as much space as our content, so we first
have to determine our content width and only then align the children.
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/engine.nim37
1 files changed, 22 insertions, 15 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 6cb12510..f1b27d94 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -794,24 +794,12 @@ proc positionBlocks(box: BlockBox) =
   box.height += box.padding_top
 
   x += box.padding_left
-  case box.computed{"text-align"}
-  of TEXT_ALIGN_CHA_CENTER:
-    x += box.contentWidth div 2
-  of TEXT_ALIGN_CHA_LEFT: discard
-  of TEXT_ALIGN_CHA_RIGHT:
-    x += box.contentWidth
-  else: discard
+  let needsMove = box.computed{"text-align"} in {TEXT_ALIGN_CHA_LEFT, TEXT_ALIGN_CHA_CENTER, TEXT_ALIGN_CHA_RIGHT}
 
   template apply_child(child: BlockBox) =
     child.offset.y = y
     child.offset.x = x
-    case box.computed{"text-align"}
-    of TEXT_ALIGN_CHA_CENTER:
-      child.offset.x -= child.width div 2
-    of TEXT_ALIGN_CHA_LEFT: discard
-    of TEXT_ALIGN_CHA_RIGHT:
-      child.offset.x -= child.width
-    elif spec and child.contentWidth < box.contentWidth:
+    if not needsMove and spec and child.contentWidth < box.contentWidth:
       let margin_left = child.computed{"margin-left"}
       let margin_right = child.computed{"margin-right"}
       if margin_left.auto and margin_right.auto:
@@ -826,7 +814,6 @@ proc positionBlocks(box: BlockBox) =
         child.margin_right += box.contentWidth
         child.margin_right -= child.contentWidth
     child.offset.x += child.margin_left
-
     if box.computed{"position"} == POSITION_RELATIVE:
       box.positionRelative(child)
     y += child.height
@@ -884,6 +871,26 @@ proc positionBlocks(box: BlockBox) =
   margin_todo.append(box.margin_bottom)
   box.margin_bottom = margin_todo.sum()
 
+  if needsMove:
+    # Re-position the children.
+    # The x offset for values in shrink mode depends on the parent box's
+    # width, so we can not just do this in the first pass.
+    let width = if box.shrink:
+      min(box.width, box.contentWidth)
+    else:
+      max(box.width, box.contentWidth)
+    case box.computed{"text-align"}
+    of TEXT_ALIGN_CHA_CENTER:
+      for child in box.nested:
+        child.offset.x += width div 2
+        child.offset.x -= child.width div 2
+    of TEXT_ALIGN_CHA_LEFT: discard
+    of TEXT_ALIGN_CHA_RIGHT:
+      for child in box.nested:
+        child.offset.x += width
+        child.offset.x -= child.width
+    else: discard
+
   box.height += box.padding_bottom
 
   if box.contentHeight.isSome: