about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-17 18:20:21 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-17 18:20:21 +0100
commitd430a83ab3e971b816581839fbdf860a293242e8 (patch)
treea027d1d8fbd45310e59f1791b7399c2ae1c1aef8
parent6a5cc2383c3fc3b23cc9b98d44bcf0e64b811083 (diff)
downloadchawan-d430a83ab3e971b816581839fbdf860a293242e8.tar.gz
More deduplication
-rw-r--r--src/layout/box.nim6
-rw-r--r--src/layout/engine.nim95
2 files changed, 65 insertions, 36 deletions
diff --git a/src/layout/box.nim b/src/layout/box.nim
index 7974e4b6..85e194af 100644
--- a/src/layout/box.nim
+++ b/src/layout/box.nim
@@ -21,12 +21,18 @@ type
     cssvalues*: CSSComputedValues
     node*: Node
 
+  RowContainer* = object
+    case leaf*: bool
+    of true: row*: CSSRowBox
+    of false: ctx*: InlineContext
+
   InlineContext* = ref object
     fromx*: int
     fromy*: int
     conty*: bool
     whitespace*: bool
     ws_initial*: bool
+    conts*: seq[RowContainer]
 
   BlockContext* = ref object
     fromy*: int
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index baa8fcc1..6568c400 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -28,6 +28,40 @@ func newInlineContext*(): InlineContext =
 func newBlockContext(): BlockContext =
   new(result)
 
+#proc calcConts(conts: var seq[RowContainer]) =
+#  if conts.len == 0: return
+#  var y = 0
+#  var re = false
+#  if conts[0].leaf:
+#    y = conts[0].row.y
+#  else:
+#    y = conts[0].ctx.fromy
+#  var i = 1
+#  while i < conts.len:
+#    let cont = conts[i]
+#    if cont.leaf:
+#      if y != cont.row.y:
+#        re = true
+#        if y < cont.row.y:
+#          y = cont.row.y
+#    else:
+#      if y != cont.ctx.fromy:
+#        re = true
+#        if y < cont.ctx.fromy:
+#          y = cont.ctx.fromy
+#    inc i
+#
+#  if re:
+#    i = 0
+#    while i < conts.len:
+#      if conts[i].leaf:
+#        conts[i].row.y = y
+#      else:
+#        let diff = y - conts[i].ctx.fromy
+#        #TODO
+#        conts[i].row.y += diff
+#      inc i
+  
 proc flushConty(box: CSSBox) =
   inc box.icontext.fromy
   inc box.bcontext.fromy
@@ -40,12 +74,8 @@ proc flushMargins(box: CSSBox) =
   box.bcontext.margin_done += box.bcontext.margin_todo
   box.bcontext.margin_todo = 0
 
-proc flushLines(box: CSSBox) =
-  if box.icontext.conty:
-    box.flushConty()
-  box.flushMargins()
-
-proc applyBlockProperties(state: LayoutState, box, parent: CSSBox, vals: CSSComputedValues) =
+proc applyBlockStart(state: LayoutState, box, parent: CSSBox, vals: CSSComputedValues) =
+  parent.flushMargins()
   box.bcontext = newBlockContext()
   box.x += vals[PROPERTY_MARGIN_LEFT].length.cells_w(state, parent.bcontext.width)
 
@@ -76,14 +106,17 @@ proc applyBlockProperties(state: LayoutState, box, parent: CSSBox, vals: CSSComp
 
 func newBlockBox(state: var LayoutState, parent: CSSBox, vals: CSSComputedValues): CSSBlockBox =
   new(result)
-  parent.flushLines()
+  if parent.icontext.conty:
+    parent.flushConty()
   result.x = parent.x
-  state.applyBlockProperties(result, parent, vals)
+
+  state.applyBlockStart(result, parent, vals)
 
 func newInlineBlockBox*(state: LayoutState, parent: CSSBox, vals: CSSComputedValues): CSSInlineBlockBox =
   new(result)
   result.x = parent.icontext.fromx
-  state.applyBlockProperties(result, parent, vals)
+
+  state.applyBlockStart(result, parent, vals)
 
 func newInlineBox*(state: LayoutState, parent: CSSBox, vals: CSSComputedValues): CSSInlineBox =
   new(result)
@@ -293,12 +326,8 @@ proc processInlineBox(state: var LayoutState, parent: CSSBox, str: string): CSSB
   ibox.processInlineContext(str, state.nodes)
   return ibox
 
-proc addBlock(state: var LayoutState, parent: CSSBox, box: CSSBox) =
-  parent.icontext.fromx = parent.x
-  parent.icontext.whitespace = true
-  parent.icontext.ws_initial = true
-
-  box.flushLines()
+proc applyBlockEnd(state: var LayoutState, parent, box: CSSBox) =
+  box.flushMargins()
 
   let mbot = box.cssvalues[PROPERTY_MARGIN_BOTTOM].length.cells_h(state, parent.bcontext.width)
   parent.bcontext.margin_todo += mbot
@@ -311,6 +340,12 @@ proc addBlock(state: var LayoutState, parent: CSSBox, box: CSSBox) =
   else:
     parent.icontext.fromy += box.bcontext.height.get
 
+proc addBlock(state: var LayoutState, parent: CSSBox, box: CSSBox) =
+  parent.icontext.fromx = parent.x
+  if box.icontext.conty:
+    box.flushConty()
+
+  state.applyBlockEnd(parent, box)
   parent.children.add(box)
 
 proc addInline(state: var LayoutState, parent: CSSBox, box: CSSBox) =
@@ -321,27 +356,13 @@ proc addInline(state: var LayoutState, parent: CSSBox, box: CSSBox) =
 
 proc addInlineBlock(state: var LayoutState, parent: CSSBox, box: CSSBox) =
   parent.icontext.fromx = box.icontext.fromx
-
   parent.icontext.fromx += box.cssvalues[PROPERTY_MARGIN_RIGHT].length.cells_w(state, parent.bcontext.width)
-
-  let mbot = box.cssvalues[PROPERTY_MARGIN_BOTTOM].length.cells_h(state, parent.bcontext.width)
-  parent.bcontext.margin_todo += mbot
-
-  parent.bcontext.margin_done = box.bcontext.margin_done
-  parent.bcontext.margin_todo = max(parent.bcontext.margin_todo - box.bcontext.margin_done, 0)
-
-  if box.bcontext.height.isnone:
-    parent.icontext.fromy = box.icontext.fromy
-  else:
-    parent.icontext.fromy += box.bcontext.height.get
-
   parent.icontext.conty = box.icontext.conty
 
+  state.applyBlockEnd(parent, box)
   parent.children.add(box)
 
 proc add(state: var LayoutState, parent: CSSBox, box: CSSBox) =
-  if box == nil:
-    return
   if box of CSSBlockBox:
     state.addBlock(parent, box)
   elif box of CSSInlineBox:
@@ -402,8 +423,8 @@ proc processNode(state: var LayoutState, parent: CSSBox, node: Node): CSSBox =
 proc processBeforePseudoElem(state: var LayoutState, parent: CSSBox, elem: Element) =
   if elem.cssvalues_before != nil:
     let box = state.processComputedValueBox(parent, elem.cssvalues_before)
-    if box != nil:
-      box.node = elem
+    if box == nil: return
+    box.node = elem
 
     let text = elem.cssvalues_before[PROPERTY_CONTENT].content
     var inline = state.processInlineBox(box, $text)
@@ -416,8 +437,8 @@ proc processBeforePseudoElem(state: var LayoutState, parent: CSSBox, elem: Eleme
 proc processAfterPseudoElem(state: var LayoutState, parent: CSSBox, elem: Element) =
   if elem.cssvalues_after != nil:
     let box = state.processComputedValueBox(parent, elem.cssvalues_after)
-    if box != nil:
-      box.node = elem
+    if box == nil: return
+    box.node = elem
 
     let text = elem.cssvalues_after[PROPERTY_CONTENT].content
     var inline = state.processInlineBox(box, $text)
@@ -437,12 +458,14 @@ proc processMarker(state: var LayoutState, parent: CSSBox, elem: Element) =
     let tlen = text.width()
     parent.icontext.fromx -= tlen
     let marker = state.processInlineBox(parent, text)
-    state.addInline(parent, marker)
+    if marker != nil:
+      state.addInline(parent, marker)
 
 proc processNodes(state: var LayoutState, parent: CSSBox, nodes: seq[Node]) =
   for node in nodes:
     let box = state.processNode(parent, node)
-    state.add(parent, box)
+    if box != nil:
+      state.add(parent, box)
 
 proc processElemChildren(state: var LayoutState, parent: CSSBox, elem: Element) =
   state.nodes.add(elem)
'n430' href='#n430'>430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674