about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-17 23:20:55 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-17 23:20:55 +0100
commitf8116499bb450928ecf737fa179ec371ccbb5dae (patch)
tree2c0013593c054e5e18f6b96cac22fe6da40443da /src
parentef4604ba8f494a2997a744a0b04732dc686c46ea (diff)
downloadchawan-f8116499bb450928ecf737fa179ec371ccbb5dae.tar.gz
Move... things
Diffstat (limited to 'src')
-rw-r--r--src/io/buffer.nim7
-rw-r--r--src/layout/box.nim11
-rw-r--r--src/layout/engine.nim45
3 files changed, 34 insertions, 29 deletions
diff --git a/src/io/buffer.nim b/src/io/buffer.nim
index 4f1f4960..d7a2b092 100644
--- a/src/io/buffer.nim
+++ b/src/io/buffer.nim
@@ -743,11 +743,8 @@ proc renderDocument*(buffer: Buffer) =
   stack.add(buffer.rootbox)
   while stack.len > 0:
     let box = stack.pop()
-    if box of CSSInlineBox:
-      let inline = CSSInlineBox(box)
-      #eprint "NEW BOX", inline.context.conty
-      for line in inline.content:
-        #eprint line
+    if box of CSSBlockBox:
+      for line in box.icontext.rows:
         buffer.setRowBox(line)
 
     var i = box.children.len - 1
diff --git a/src/layout/box.nim b/src/layout/box.nim
index 85e194af..b3cb972a 100644
--- a/src/layout/box.nim
+++ b/src/layout/box.nim
@@ -5,14 +5,17 @@ import html/dom
 import io/term
 
 type
+  BoxType* = enum
+    INLINE, BLOCK, INLINE_BLOCK
+
   CSSRect* = object
     x1*: int
     y1*: int
     x2*: int
     y2*: int
 
-  CSSBox* = ref CSSBoxObj
-  CSSBoxObj = object of RootObj
+  CSSBox* = ref object of RootObj
+    t*: BoxType
     x*: int
     y*: int
     children*: seq[CSSBox]
@@ -33,6 +36,7 @@ type
     whitespace*: bool
     ws_initial*: bool
     conts*: seq[RowContainer]
+    rows*: seq[CSSRowBox]
 
   BlockContext* = ref object
     fromy*: int
@@ -58,8 +62,5 @@ type
     nodes*: seq[Node]
 
   CSSInlineBox* = ref object of CSSBox
-    content*: seq[CSSRowBox]
-
   CSSBlockBox* = ref object of CSSBox
-
   CSSInlineBlockBox* = ref object of CSSBox
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 1e9fc94a..18012e88 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -99,27 +99,33 @@ proc applyBlockStart(state: LayoutState, box, parent: CSSBox, vals: CSSComputedV
     if pheight.unit != UNIT_PERC or parent.bcontext.height.issome:
       box.bcontext.height = pheight.cells_h(state, parent.bcontext.height).some
 
-  box.icontext = newInlineContext()
-  box.icontext.fromy = box.y
-  box.icontext.fromx = box.x
   box.cssvalues = vals
 
 func newBlockBox(state: var LayoutState, parent: CSSBox, vals: CSSComputedValues): CSSBlockBox =
   new(result)
+  result.t = BLOCK
   if parent.icontext.conty:
     parent.flushConty()
   result.x = parent.x
 
   state.applyBlockStart(result, parent, vals)
+  result.icontext = newInlineContext()
+  result.icontext.fromy = result.y
+  result.icontext.fromx = result.x
 
 func newInlineBlockBox*(state: LayoutState, parent: CSSBox, vals: CSSComputedValues): CSSInlineBlockBox =
   new(result)
+  result.t = INLINE_BLOCK
   result.x = parent.icontext.fromx
 
   state.applyBlockStart(result, parent, vals)
+  result.icontext = parent.icontext
+  result.icontext.fromy = result.y
+  result.icontext.fromx = result.x
 
 func newInlineBox*(state: LayoutState, parent: CSSBox, vals: CSSComputedValues): CSSInlineBox =
   new(result)
+  result.t = INLINE
   result.x = parent.x
   result.y = parent.icontext.fromy
 
@@ -156,8 +162,11 @@ proc newRowBox(state: var InlineState) =
   state.rowbox.textdecoration = cssvalues[PROPERTY_TEXT_DECORATION].textdecoration
   state.rowbox.nodes = state.nodes
 
-proc inlineWrap(state: var InlineState) =
+proc addRowBox(state: var InlineState) =
   state.rowboxes.add(state.rowbox)
+
+proc inlineWrap(state: var InlineState) =
+  state.addRowBox()
   inc state.rowi
   state.icontext.fromx = state.x
   if state.word.len == 0:
@@ -295,7 +304,7 @@ proc processInlineText(str: string, icontext: InlineContext,
   state.addWord()
 
   if state.rowbox.str.len > 0:
-    state.rowboxes.add(state.rowbox)
+    state.addRowBox()
     state.icontext.fromx += state.rowbox.width
     state.icontext.conty = true
 
@@ -308,9 +317,9 @@ proc processInlineText(str: string, icontext: InlineContext,
 
 proc processInlineContext(ibox: CSSInlineBox, str: string, nodes: seq[Node]) =
   let rows = processInlineText(str, ibox.icontext, ibox.bcontext, ibox.cssvalues, ibox.x, nodes)
-  ibox.content.add(rows)
+  ibox.icontext.rows.add(rows)
 
-proc processInlineBox(state: var LayoutState, parent: CSSBox, str: string): CSSBox =
+proc processInlineBox(state: var LayoutState, parent: CSSBox, str: string): CSSInlineBox =
   if str.len == 0:
     return nil
 
@@ -340,7 +349,7 @@ proc applyBlockEnd(state: var LayoutState, parent, box: CSSBox) =
   else:
     parent.icontext.fromy += box.bcontext.height.get
 
-proc addBlock(state: var LayoutState, parent, box: CSSBox) =
+proc add(state: var LayoutState, parent: CSSBox, box: CSSBlockBox) =
   parent.icontext.fromx = parent.x
   if box.icontext.conty:
     box.flushConty()
@@ -348,13 +357,13 @@ proc addBlock(state: var LayoutState, parent, box: CSSBox) =
   state.applyBlockEnd(parent, box)
   parent.children.add(box)
 
-proc addInline(state: var LayoutState, parent, box: CSSBox) =
+proc add(state: var LayoutState, parent: CSSBox, box: CSSInlineBox) =
   parent.icontext.fromx += box.cssvalues[PROPERTY_MARGIN_RIGHT].length.cells_w(state, parent.bcontext.width)
   parent.icontext.fromy = box.icontext.fromy
 
   parent.children.add(box)
 
-proc addInlineBlock(state: var LayoutState, parent, box: CSSBox) =
+proc add(state: var LayoutState, parent: CSSBox, box: CSSInlineBlockBox) =
   parent.icontext.fromx = box.icontext.fromx
   parent.icontext.fromx += box.cssvalues[PROPERTY_MARGIN_RIGHT].length.cells_w(state, parent.bcontext.width)
   parent.icontext.conty = box.icontext.conty
@@ -363,12 +372,10 @@ proc addInlineBlock(state: var LayoutState, parent, box: CSSBox) =
   parent.children.add(box)
 
 proc add(state: var LayoutState, parent: CSSBox, box: CSSBox) =
-  if box of CSSBlockBox:
-    state.addBlock(parent, box)
-  elif box of CSSInlineBox:
-    state.addInline(parent, box)
-  elif box of CSSInlineBlockBox:
-    state.addInlineBlock(parent, box)
+  case box.t
+  of BLOCK: state.add(parent, CSSBlockBox(box))
+  of INLINE: state.add(parent, CSSInlineBox(box))
+  of INLINE_BLOCK: state.add(parent, CSSInlineBlockBox(box))
 
 proc processComputedValueBox(state: var LayoutState, parent: CSSBox, values: CSSComputedValues): CSSBox =
   case values[PROPERTY_DISPLAY].display
@@ -430,7 +437,7 @@ proc processBeforePseudoElem(state: var LayoutState, parent: CSSBox, elem: Eleme
     var inline = state.processInlineBox(box, $text)
     if inline != nil:
       inline.node = elem
-      state.addInline(box, inline)
+      state.add(box, inline)
 
     state.add(parent, box)
 
@@ -444,7 +451,7 @@ proc processAfterPseudoElem(state: var LayoutState, parent: CSSBox, elem: Elemen
     var inline = state.processInlineBox(box, $text)
     if inline != nil:
       inline.node = elem
-      state.addInline(box, inline)
+      state.add(box, inline)
 
     state.add(parent, box)
 
@@ -459,7 +466,7 @@ proc processMarker(state: var LayoutState, parent: CSSBox, elem: Element) =
     parent.icontext.fromx -= tlen
     let marker = state.processInlineBox(parent, text)
     if marker != nil:
-      state.addInline(parent, marker)
+      state.add(parent, marker)
 
 proc processNodes(state: var LayoutState, parent: CSSBox, nodes: seq[Node]) =
   for node in nodes: