about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-07-29 18:42:20 +0200
committerbptato <nincsnevem662@gmail.com>2024-07-29 18:45:22 +0200
commit1e719997eda085e998d38a90134f26489e50188e (patch)
treeb645f55129bb7749ae4a830a57699794d120a3f1
parent9653c35fb9a4398942ecb305835a95fbd87c433a (diff)
downloadchawan-1e719997eda085e998d38a90134f26489e50188e.tar.gz
buffer, layout: small refactoring
-rw-r--r--src/layout/engine.nim31
-rw-r--r--src/server/buffer.nim46
2 files changed, 37 insertions, 40 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index b92676dc..0a92c185 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -250,7 +250,6 @@ type
     whitespaceIsLF: bool
     whitespaceFragment: InlineFragment
     word: InlineAtom
-    wordstate: InlineAtomState
     wrappos: int # position of last wrapping opportunity, or -1
     textFragmentSeen: bool
     lastTextFragment: InlineFragment
@@ -325,15 +324,11 @@ proc applyLineHeight(ictx: InlineContext; state: var LineBoxState;
   state.paddingBottom = max(paddingBottom, state.paddingBottom)
   state.lineHeight = max(lineHeight, state.lineHeight)
 
-proc newWord(ictx: var InlineContext; state: var InlineState) =
+proc newWord(ictx: var InlineContext) =
   ictx.word = InlineAtom(
     t: iatWord,
     size: size(w = 0, h = ictx.cellHeight)
   )
-  ictx.wordstate = InlineAtomState(
-    vertalign: state.fragment.computed{"vertical-align"},
-    baseline: ictx.cellHeight
-  )
   ictx.wrappos = -1
   ictx.hasshy = false
 
@@ -528,16 +523,16 @@ proc putAtom(state: var LineBoxState; atom: InlineAtom;
   state.atoms.add(atom)
   fragment.state.atoms.add(atom)
 
-proc addSpacing(ictx: var InlineContext; width, height: LayoutUnit;
-    state: InlineState; hang = false) =
+proc addSpacing(ictx: var InlineContext; width: LayoutUnit; state: InlineState;
+    hang = false) =
   let fragment = ictx.whitespaceFragment
   if fragment.state.atoms.len == 0 or fragment.state.atoms[^1].t != iatWord:
     let atom = InlineAtom(
       t: iatWord,
-      size: size(w = 0, h = height),
-      offset: offset(x = ictx.lbstate.size.w, y = height)
+      size: size(w = 0, h = ictx.cellHeight),
+      offset: offset(x = ictx.lbstate.size.w, y = ictx.cellHeight)
     )
-    let iastate = InlineAtomState(baseline: height)
+    let iastate = InlineAtomState(baseline: atom.size.h)
     ictx.lbstate.putAtom(atom, iastate, fragment)
   let atom = fragment.state.atoms[^1]
   let n = (width div ictx.cellWidth).toInt #TODO
@@ -555,7 +550,7 @@ proc flushWhitespace(ictx: var InlineContext; state: InlineState;
   ictx.lbstate.charwidth += ictx.whitespacenum
   ictx.whitespacenum = 0
   if shift > 0:
-    ictx.addSpacing(shift, ictx.cellHeight, state, hang)
+    ictx.addSpacing(shift, state, hang)
 
 # Prepare the next line's initial width and available width.
 # (If space on the left is excluded by floats, set the initial width to
@@ -669,7 +664,7 @@ proc addAtom(ictx: var InlineContext; state: var InlineState;
       shift = ictx.computeShift(state)
   if atom.size.w > 0 and atom.size.h > 0:
     if shift > 0:
-      ictx.addSpacing(shift, ictx.cellHeight, state)
+      ictx.addSpacing(shift, state)
     ictx.root.state.xminwidth = max(ictx.root.state.xminwidth, atom.xminwidth)
     ictx.applyLineHeight(ictx.lbstate, state.fragment.computed)
     if atom.t == iatWord:
@@ -703,8 +698,12 @@ proc addWord(ictx: var InlineContext; state: var InlineState): bool =
   result = false
   if ictx.word.str != "":
     ictx.word.str.mnormalize() #TODO this may break on EOL.
-    result = ictx.addAtom(state, ictx.wordstate, ictx.word)
-    ictx.newWord(state)
+    let iastate = InlineAtomState(
+      vertalign: state.fragment.computed{"vertical-align"},
+      baseline: ictx.word.size.h
+    )
+    result = ictx.addAtom(state, iastate, ictx.word)
+    ictx.newWord()
 
 proc addWordEOL(ictx: var InlineContext; state: var InlineState): bool =
   result = false
@@ -841,7 +840,7 @@ proc layoutTextLoop(ictx: var InlineContext; state: var InlineState;
 
 proc layoutText(ictx: var InlineContext; state: var InlineState; s: string) =
   ictx.flushWhitespace(state)
-  ictx.newWord(state)
+  ictx.newWord()
   let transform = state.fragment.computed{"text-transform"}
   if transform == TextTransformNone:
     ictx.layoutTextLoop(state, s)
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index e27e42e0..3d466fa8 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -752,7 +752,7 @@ proc checkRefresh*(buffer: Buffer): CheckRefreshResult {.proxy.} =
     return CheckRefreshResult(n: -1)
   return CheckRefreshResult(n: n, url: url.get)
 
-proc do_reshape(buffer: Buffer) =
+proc reshape(buffer: Buffer) =
   if buffer.document == nil:
     return # not parsed yet, nothing to render
   let uastyle = if buffer.document.mode != QUIRKS:
@@ -769,7 +769,7 @@ proc do_reshape(buffer: Buffer) =
 
 proc maybeReshape(buffer: Buffer) =
   if buffer.document != nil and buffer.document.invalid:
-    buffer.do_reshape()
+    buffer.reshape()
     buffer.document.invalid = false
 
 proc processData0(buffer: Buffer; data: UnsafeSlice): bool =
@@ -843,9 +843,8 @@ proc processData(buffer: Buffer; iq: openArray[uint8]): bool =
 proc windowChange*(buffer: Buffer; attrs: WindowAttributes) {.proxy.} =
   buffer.attrs = attrs
   buffer.prevStyled = nil
-  if buffer.window != nil:
-    buffer.window.attrs = attrs
-  buffer.do_reshape()
+  buffer.window.attrs = attrs
+  buffer.reshape()
 
 type UpdateHoverResult* = object
   hover*: seq[tuple[t: HoverType, s: string]]
@@ -887,7 +886,7 @@ proc updateHover*(buffer: Buffer; cursorx, cursory: int): UpdateHoverResult
           elem.setHover(false)
           repaint = true
   if repaint:
-    buffer.do_reshape()
+    buffer.reshape()
   buffer.prevnode = thisnode
   return UpdateHoverResult(repaint: repaint, hover: hover)
 
@@ -1086,7 +1085,7 @@ proc load*(buffer: Buffer): int {.proxy, task.} =
   if buffer.state == bsLoaded:
     return -1
   elif buffer.bytesRead > buffer.reportedBytesRead:
-    buffer.do_reshape()
+    buffer.reshape()
     buffer.reportedBytesRead = buffer.bytesRead
     return buffer.bytesRead
   else:
@@ -1134,7 +1133,7 @@ proc onload(buffer: Buffer) =
       reprocess = false
     else: # EOF
       buffer.finishLoad().then(proc() =
-        buffer.do_reshape()
+        buffer.reshape()
         buffer.state = bsLoaded
         buffer.document.readyState = rsComplete
         if buffer.config.scripting:
@@ -1149,7 +1148,7 @@ proc onload(buffer: Buffer) =
   # pass
   if not buffer.config.isdump and buffer.tasks[bcLoad] != 0:
     # only makes sense when not in dump mode (and the user has requested a load)
-    buffer.do_reshape()
+    buffer.reshape()
     buffer.reportedBytesRead = buffer.bytesRead
     if buffer.hasTask(bcGetTitle):
       buffer.resolveTask(bcGetTitle, buffer.document.title)
@@ -1168,7 +1167,7 @@ proc getTitle*(buffer: Buffer): string {.proxy, task.} =
 
 proc forceRender*(buffer: Buffer) {.proxy.} =
   buffer.prevStyled = nil
-  buffer.do_reshape()
+  buffer.reshape()
 
 proc cancel*(buffer: Buffer) {.proxy.} =
   if buffer.state == bsLoaded:
@@ -1195,7 +1194,7 @@ proc cancel*(buffer: Buffer) {.proxy.} =
     buffer.htmlParser.finish()
   buffer.document.readyState = rsInteractive
   buffer.state = bsLoaded
-  buffer.do_reshape()
+  buffer.reshape()
 
 #https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#multipart/form-data-encoding-algorithm
 proc serializeMultipart(entries: seq[FormDataEntry]): FormData =
@@ -1331,13 +1330,13 @@ proc submitForm(buffer: Buffer; form: HTMLFormElement; submitter: Element): Requ
 proc setFocus(buffer: Buffer; e: Element): bool =
   if buffer.document.focus != e:
     buffer.document.setFocus(e)
-    buffer.do_reshape()
+    buffer.reshape()
     return true
 
 proc restoreFocus(buffer: Buffer): bool =
   if buffer.document.focus != nil:
     buffer.document.setFocus(nil)
-    buffer.do_reshape()
+    buffer.reshape()
     return true
 
 type ReadSuccessResult* = object
@@ -1372,20 +1371,20 @@ proc readSuccess*(buffer: Buffer; s: string; hasFd: bool): ReadSuccessResult
       of itFile:
         input.file = newWebFile(s, fd)
         input.setInvalid()
-        buffer.do_reshape()
+        buffer.reshape()
         res.repaint = true
         res.open = buffer.implicitSubmit(input)
       else:
         input.value = s
         input.setInvalid()
-        buffer.do_reshape()
+        buffer.reshape()
         res.repaint = true
         res.open = buffer.implicitSubmit(input)
     of TAG_TEXTAREA:
       let textarea = HTMLTextAreaElement(buffer.document.focus)
       textarea.value = s
       textarea.setInvalid()
-      buffer.do_reshape()
+      buffer.reshape()
       res.repaint = true
     else: discard
     let r = buffer.restoreFocus()
@@ -1445,7 +1444,7 @@ proc click(buffer: Buffer; anchor: HTMLAnchorElement): ClickResult =
       if not buffer.config.scripting:
         return ClickResult(repaint: repaint)
       let s = buffer.evalJSURL(url)
-      buffer.do_reshape()
+      buffer.reshape()
       repaint = true
       if s.isNone:
         return ClickResult(repaint: repaint)
@@ -1470,7 +1469,7 @@ proc click(buffer: Buffer; button: HTMLButtonElement): ClickResult =
       open = buffer.submitForm(button.form, button)
     of btReset:
       button.form.reset()
-      buffer.do_reshape()
+      buffer.reshape()
       return ClickResult(repaint: true)
     of btButton: discard
     let repaint = buffer.setFocus(button)
@@ -1525,7 +1524,7 @@ proc click(buffer: Buffer; input: HTMLInputElement): ClickResult =
   of itCheckbox:
     input.setChecked(not input.checked)
     input.setInvalid()
-    buffer.do_reshape()
+    buffer.reshape()
     return ClickResult(repaint: true)
   of itRadio:
     for radio in input.radiogroup:
@@ -1533,12 +1532,12 @@ proc click(buffer: Buffer; input: HTMLInputElement): ClickResult =
       radio.setInvalid()
     input.setChecked(true)
     input.setInvalid()
-    buffer.do_reshape()
+    buffer.reshape()
     return ClickResult(repaint: true)
   of itReset:
     if input.form != nil:
       input.form.reset()
-      buffer.do_reshape()
+      buffer.reshape()
       return ClickResult(repaint: true)
     return ClickResult(repaint: false)
   of itSubmit, itButton:
@@ -1593,7 +1592,7 @@ proc click*(buffer: Buffer; cursorx, cursory: int): ClickResult {.proxy.} =
       let event = newEvent(window.toAtom(satClick), element)
       canceled = window.jsctx.dispatch(element, event)
       if buffer.document.invalid:
-        buffer.do_reshape()
+        buffer.reshape()
         buffer.document.invalid = false
         repaint = true
   if not canceled:
@@ -1721,7 +1720,7 @@ proc markURL*(buffer: Buffer; schemes: seq[string]) {.proxy.} =
         let element = HTMLElement(node)
         if element.tagType notin {TAG_HEAD, TAG_SCRIPT, TAG_STYLE, TAG_A}:
           stack.add(element)
-  buffer.do_reshape()
+  buffer.reshape()
 
 proc toggleImages*(buffer: Buffer) {.proxy.} =
   buffer.config.images = not buffer.config.images
@@ -1845,7 +1844,6 @@ proc runBuffer(buffer: Buffer) =
           alive = false
           break
       if selectors.Event.Timer in event.events:
-        assert buffer.window != nil
         let r = buffer.window.timeouts.runTimeoutFd(event.fd)
         assert r
         buffer.window.runJSJobs()