about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--src/render/renderdocument.nim47
-rw-r--r--src/server/buffer.nim17
-rw-r--r--src/types/cell.nim3
3 files changed, 28 insertions, 39 deletions
diff --git a/src/render/renderdocument.nim b/src/render/renderdocument.nim
index dbdb2a6e..319d0f08 100644
--- a/src/render/renderdocument.nim
+++ b/src/render/renderdocument.nim
@@ -1,12 +1,9 @@
 import strutils
 import unicode
 
-import css/cascade
-import css/sheet
 import css/stylednode
 import css/values
 import display/winattrs
-import html/dom
 import layout/box
 import layout/engine
 import layout/layoutunit
@@ -14,8 +11,6 @@ import types/cell
 import types/color
 import utils/twtstr
 
-import chame/tags
-
 func formatFromWord(computed: ComputedFormat): Format =
   result.fgcolor = computed.color.cellColor()
   if computed.bgcolor.a != 0:
@@ -39,8 +34,8 @@ proc setText(lines: var FlexibleGrid, linestr: string, cformat: ComputedFormat,
   var i = 0
   var r: Rune
   # make sure we have line y
-  while lines.len <= y:
-    lines.addLine()
+  if lines.high < y:
+    lines.addLines(y - lines.high)
 
   var cx = 0 # first x of new string (before padding)
   while cx < x and i < lines[y].str.len:
@@ -230,8 +225,8 @@ proc paintBackground(lines: var FlexibleGrid, color: RGBAColor, startx,
   if startx == endx: return # width is 0, no need to paint
 
   # make sure we have line y
-  while lines.len <= endy:
-    lines.addLine()
+  if lines.high < endy:
+    lines.addLines(endy - lines.high)
 
   for y in starty..<endy:
     # Make sure line.width() >= endx
@@ -305,11 +300,9 @@ proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext,
   for line in ctx.lines:
     let y0 = y + line.offsety
     let y = y0 - erry * i
-
-    let r = y div window.ppl
-    while grid.len <= r:
-      grid.addLine()
-
+    let r = (y div window.ppl).toInt()
+    if grid.high < r:
+      grid.addLines(r - grid.high)
     for atom in line.atoms:
       case atom.t
       of INLINE_BLOCK:
@@ -377,27 +370,11 @@ proc renderBlockBox(grid: var FlexibleGrid, box: BlockBox, x, y: LayoutUnit,
       for i in countdown(box.nested.high, 0):
         stack.add((box.nested[i], x, y, posx, posy))
 
-const css = staticRead"res/ua.css"
-let uastyle = css.parseStylesheet()
-const quirk = css & staticRead"res/quirk.css"
-let quirkstyle = quirk.parseStylesheet()
-type RenderedDocument* = object
-  grid*: FlexibleGrid
-  styledRoot*: StyledNode
-  images*: seq[StyledNode]
-
-proc renderDocument*(document: Document, userstyle: CSSStylesheet,
-    layout: var Viewport, previousStyled: StyledNode): RenderedDocument =
+proc renderDocument*(styledRoot: StyledNode, viewport: Viewport,
+    attrs: WindowAttributes): FlexibleGrid =
   var grid: FlexibleGrid
-  var uastyle = uastyle
-  if document.mode == QUIRKS:
-    uastyle = quirkstyle
-  let styledRoot = document.applyStylesheets(uastyle, userstyle, previousStyled)
-  let rootBox = layout.renderLayout(styledRoot)
-  grid.renderBlockBox(rootBox, 0, 0, document.window.attrs)
+  let rootBox = viewport.renderLayout(styledRoot)
+  grid.renderBlockBox(rootBox, 0, 0, attrs)
   if grid.len == 0:
     grid.addLine()
-  return RenderedDocument(
-    grid: grid,
-    styledRoot: styledRoot
-  )
+  return grid
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 347fbed2..1a2b7e02 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -584,14 +584,23 @@ proc gotoAnchor*(buffer: Buffer): Opt[tuple[x, y: int]] {.proxy.} =
         return ok((format.pos, y))
   return err()
 
+const css = staticRead"res/ua.css"
+let uastyle = css.parseStylesheet()
+const quirk = css & staticRead"res/quirk.css"
+let quirkstyle = quirk.parseStylesheet()
+
 proc do_reshape(buffer: Buffer) =
   if buffer.ishtml:
     if buffer.viewport == nil:
       buffer.viewport = Viewport(window: buffer.attrs)
-    let ret = renderDocument(buffer.document, buffer.userstyle,
-      buffer.viewport, buffer.prevstyled)
-    buffer.lines = ret.grid
-    buffer.prevstyled = ret.styledRoot
+    let uastyle = if buffer.document.mode != QUIRKS:
+      uastyle
+    else:
+      quirkstyle
+    let styledRoot = buffer.document.applyStylesheets(uastyle,
+      buffer.userstyle, buffer.prevstyled)
+    buffer.lines = renderDocument(styledRoot, buffer.viewport, buffer.attrs)
+    buffer.prevstyled = styledRoot
   else:
     buffer.lines.renderStream(buffer.srenderer, buffer.available)
     buffer.available = 0
diff --git a/src/types/cell.nim b/src/types/cell.nim
index ff5eafdd..19a566cc 100644
--- a/src/types/cell.nim
+++ b/src/types/cell.nim
@@ -143,6 +143,9 @@ func findNextFormat*(line: SimpleFlexibleLine, pos: int): SimpleFormatCell =
 proc addLine*(grid: var FlexibleGrid) =
   grid.add(FlexibleLine())
 
+proc addLines*(grid: var FlexibleGrid, n: int) =
+  grid.setLen(grid.len + n)
+
 proc insertFormat*(line: var FlexibleLine, pos, i: int, format: Format, node: StyledNode = nil) =
   line.formats.insert(FormatCell(format: format, node: node, pos: pos), i)