about summary refs log tree commit diff stats
path: root/src/layout
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-25 20:25:04 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-25 20:25:04 +0100
commit92fbf479f8bf6db6ad0655e5b71a0b58a45a2213 (patch)
tree47b7f2c23ebabbf94e48476615014b52762542f3 /src/layout
parent4df71520f39ed7992f78484701467e513a34c5dc (diff)
downloadchawan-92fbf479f8bf6db6ad0655e5b71a0b58a45a2213.tar.gz
term: improve pixels-per-column/line detection
Some terminal emulators (AKA vte) refuse to set ws_xpixel and ws_ypixel
in the TIOCGWINSZ ioctl, so we now query for CSI 14 t as well. (Also CSI
18 t for good measure, just in case we can't ioctl for some reason.)

Also added some fallback (optionally forced) config values for width,
height, ppc, and ppl. (This is especially useful in dump mode.)
Diffstat (limited to 'src/layout')
-rw-r--r--src/layout/engine.nim2
-rw-r--r--src/layout/renderdocument.nim61
2 files changed, 60 insertions, 3 deletions
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 613cd9c3..93a324b3 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -5,7 +5,7 @@ import std/unicode
 
 import css/stylednode
 import css/values
-import display/winattrs
+import display/term
 import layout/box
 import layout/layoutunit
 import utils/luwrap
diff --git a/src/layout/renderdocument.nim b/src/layout/renderdocument.nim
index 7526b111..0313ca2b 100644
--- a/src/layout/renderdocument.nim
+++ b/src/layout/renderdocument.nim
@@ -3,7 +3,7 @@ import std/unicode
 
 import css/stylednode
 import css/values
-import display/winattrs
+import display/term
 import layout/box
 import layout/engine
 import layout/layoutunit
@@ -11,6 +11,63 @@ import types/cell
 import types/color
 import utils/strwidth
 
+type
+  # A FormatCell *starts* a new terminal formatting context.
+  # If no FormatCell exists before a given cell, the default formatting is used.
+  FormatCell* = object
+    format*: Format
+    pos*: int
+    node*: StyledNode
+
+  # Following properties should hold for `formats':
+  # * Position should be >= 0, <= str.width().
+  # * The position of every FormatCell should be greater than the position
+  #   of the previous FormatCell.
+  FlexibleLine* = object
+    str*: string
+    formats*: seq[FormatCell]
+
+  FlexibleGrid* = seq[FlexibleLine]
+
+func findFormatN*(line: FlexibleLine, pos: int): int =
+  var i = 0
+  while i < line.formats.len:
+    if line.formats[i].pos > pos:
+      break
+    inc i
+  return i
+
+func findFormat*(line: FlexibleLine, pos: int): FormatCell =
+  let i = line.findFormatN(pos) - 1
+  if i != -1:
+    result = line.formats[i]
+  else:
+    result.pos = -1
+
+func findNextFormat*(line: FlexibleLine, pos: int): FormatCell =
+  let i = line.findFormatN(pos)
+  if i < line.formats.len:
+    result = line.formats[i]
+  else:
+    result.pos = -1
+
+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, i: int, cell: FormatCell) =
+  line.formats.insert(cell, i)
+
+proc insertFormat*(line: var FlexibleLine, pos, i: int, format: Format,
+    node: StyledNode = nil) =
+  line.insertFormat(i, FormatCell(format: format, node: node, pos: pos))
+
+proc addFormat*(line: var FlexibleLine, pos: int, format: Format,
+    node: StyledNode = nil) =
+  line.formats.add(FormatCell(format: format, node: node, pos: pos))
+
 func toFormat(computed: CSSComputedValues): Format =
   if computed == nil:
     return Format()
@@ -233,7 +290,7 @@ proc paintBackground(grid: var FlexibleGrid; color: CellColor; startx,
 
   for y in starty..<endy:
     # Make sure line.width() >= endx
-    let linewidth = grid[y].width()
+    let linewidth = grid[y].str.width()
     if linewidth < endx:
       grid[y].str &= ' '.repeat(endx - linewidth)