about summary refs log tree commit diff stats
path: root/src/layout
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-28 21:21:09 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-28 21:23:53 +0100
commit7eeef1700eee50a2e80910ba0b07138ea1d39d55 (patch)
tree8502cc824e1ba33df05dc83885ddf2aca900c390 /src/layout
parent11267020c1bb85d9c27548d13182f889e5444909 (diff)
downloadchawan-7eeef1700eee50a2e80910ba0b07138ea1d39d55.tar.gz
layout: round atom offsets too
it is useful to round them so that they don't get positioned somewhere
in the middle of a line (which is rounded to the same precision as well)
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/engine.nim20
-rw-r--r--src/layout/layoutunit.nim3
2 files changed, 13 insertions, 10 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index c76c4be5..0db56704 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -245,16 +245,16 @@ func whitespacepre(computed: CSSComputedValues): bool =
 func nowrap(computed: CSSComputedValues): bool =
   computed{"white-space"} in {WHITESPACE_NOWRAP, WHITESPACE_PRE}
 
-func cellwidth(lctx: LayoutState): LayoutUnit =
+func cellwidth(lctx: LayoutState): int =
   lctx.attrs.ppc
 
-func cellwidth(ictx: InlineContext): LayoutUnit =
+func cellwidth(ictx: InlineContext): int =
   ictx.lctx.cellwidth
 
-func cellheight(lctx: LayoutState): LayoutUnit =
+func cellheight(lctx: LayoutState): int =
   lctx.attrs.ppl
 
-func cellheight(ictx: InlineContext): LayoutUnit =
+func cellheight(ictx: InlineContext): int =
   ictx.lctx.attrs.ppl
 
 template atoms(state: LineBoxState): untyped =
@@ -287,7 +287,7 @@ proc applyLineHeight(ictx: InlineContext, state: var LineBoxState,
   let lctx = ictx.lctx
   #TODO this should be computed during cascading.
   let lineheight = if computed{"line-height"}.auto: # ergo normal
-    lctx.cellheight
+    lctx.cellheight.toLayoutUnit
   else:
     # Percentage: refers to the font size of the element itself.
     computed{"line-height"}.px(lctx, lctx.cellheight)
@@ -424,10 +424,11 @@ proc verticalAlignLine(ictx: var InlineContext) =
 
   # Finally, offset all atoms' y position by the largest top margin and the
   # line box's top padding.
+  let paddingTop = ictx.currentLine.paddingTop
+  let offsety = ictx.currentLine.offsety
+  let ch = ictx.cellheight
   for atom in ictx.currentLine.atoms:
-    atom.offset.y += marginTop
-    atom.offset.y += ictx.currentLine.paddingTop
-    atom.offset.y += ictx.currentLine.offsety
+    atom.offset.y = (atom.offset.y + marginTop + paddingTop + offsety).round(ch)
 
   # Set the line height to new top edge + old bottom edge, and set the
   # baseline.
@@ -528,8 +529,7 @@ proc finishLine(ictx: var InlineContext, state: var InlineState, wrap: bool,
     ictx.lines.add(ictx.currentLine.line)
     # round line height to real cell height, so that the next line will actually
     # come after ours.
-    let ch = ictx.cellheight
-    ictx.currentLine.size.h = toInt(ictx.currentLine.size.h div ch) * ch
+    ictx.currentLine.size.h = ictx.currentLine.size.h.round(ictx.cellheight)
     ictx.currentLine = LineBoxState(
       offsety: y + ictx.currentLine.size.h,
       line: LineBox()
diff --git a/src/layout/layoutunit.nim b/src/layout/layoutunit.nim
index 86f019f0..adf1e692 100644
--- a/src/layout/layoutunit.nim
+++ b/src/layout/layoutunit.nim
@@ -51,3 +51,6 @@ func `$`*(a: LayoutUnit): string =
 func min*(a, b: LayoutUnit): LayoutUnit {.borrow.}
 func max*(a, b: LayoutUnit): LayoutUnit {.borrow.}
 func clamp*(x, a, b: LayoutUnit): LayoutUnit {.borrow.}
+
+func round*(a: LayoutUnit; prec: int): LayoutUnit =
+  return (a div prec).toInt * prec