aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-01-26 15:49:41 +0100
committerbptato <nincsnevem662@gmail.com>2022-01-26 15:49:41 +0100
commitef37b1119ec78801ab72a76dc750c0b8bfd52af5 (patch)
tree07e93271d3fdf27b945ea2368a3eb36423e99e59 /src
parent0cdcb6b2646e6753b99e66a974040fc6d71d7dfd (diff)
downloadchawan-ef37b1119ec78801ab72a76dc750c0b8bfd52af5.tar.gz
Fix whitespace styling
Diffstat (limited to 'src')
-rw-r--r--src/layout/box.nim3
-rw-r--r--src/layout/engine.nim8
-rw-r--r--src/render/renderdocument.nim50
3 files changed, 59 insertions, 2 deletions
diff --git a/src/layout/box.nim b/src/layout/box.nim
index d461f0f5..d7ae3400 100644
--- a/src/layout/box.nim
+++ b/src/layout/box.nim
@@ -23,6 +23,9 @@ type
width*: int
height*: int
+ InlineSpacing* = ref object of InlineAtom
+ word*: InlineWord
+
InlineWord* = ref object of InlineAtom
str*: string
fontstyle*: CSSFontStyle
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 9b1e9363..45aef6de 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -80,9 +80,13 @@ proc addAtom(ictx: InlineContext, atom: InlineAtom, maxwidth: int, specified: CS
shift = ictx.computeShift(specified)
ictx.whitespace = false
- ictx.thisrow.width += shift
+ if atom.width > 0 and atom.height > 0:
+ if shift > 0:
+ let spacing = InlineSpacing(width: shift, height: atom.height)
+ spacing.relx = ictx.thisrow.width
+ ictx.thisrow.width += spacing.width
+ ictx.thisrow.atoms.add(spacing)
- if atom.width > 0:
atom.relx += ictx.thisrow.width
ictx.thisrow.width += atom.width
ictx.thisrow.height = max(ictx.thisrow.height, atom.height)
diff --git a/src/render/renderdocument.nim b/src/render/renderdocument.nim
index 6765853b..89ce4ae6 100644
--- a/src/render/renderdocument.nim
+++ b/src/render/renderdocument.nim
@@ -67,6 +67,53 @@ proc setRowWord(lines: var FlexibleGrid, word: InlineWord, x, y: int, term: Term
let oline = FlexibleLine(str: ostr.substr(i), formats: oformats.subformats(i))
lines[y].add(oline)
+proc setSpacing(lines: var FlexibleGrid, spacing: InlineSpacing, x, y: int, term: TermAttributes) =
+ var r: Rune
+
+ let y = y div term.ppl
+ var x = x div term.ppc
+ let width = spacing.width div term.ppc
+
+ var i = 0
+ if x < 0:
+ i -= x
+ x = 0
+
+ let linestr = ' '.repeat(width - i)
+ i = 0
+
+ while lines.len <= y:
+ lines.addLine()
+
+ var cx = 0
+ while cx < x and i < lines[y].str.len:
+ fastRuneAt(lines[y].str, i, r)
+ cx += r.width()
+
+ let ostr = lines[y].str.substr(i)
+ let oformats = lines[y].formats.subformats(i)
+ lines[y].setLen(i)
+
+ if spacing.word != nil:
+ lines.addFormat(y, i, spacing.word.formatFromWord(), spacing.word.node)
+
+ var nx = cx
+ if nx < x:
+ lines[y].str &= ' '.repeat(x - nx)
+ nx = x
+
+ lines[y].str &= linestr
+ nx += linestr.len
+
+ i = 0
+ while cx < nx and i < ostr.len:
+ fastRuneAt(ostr, i, r)
+ cx += r.width()
+
+ if i < ostr.len:
+ let oline = FlexibleLine(str: ostr.substr(i), formats: oformats.subformats(i))
+ lines[y].add(oline)
+
proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockContext, x, y: int, term: TermAttributes)
proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext, x, y: int, term: TermAttributes) =
@@ -83,6 +130,9 @@ proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext, x, y: int,
elif atom of InlineWord:
let word = InlineWord(atom)
grid.setRowWord(word, x + word.relx, y, term)
+ elif atom of InlineSpacing:
+ let spacing = InlineSpacing(atom)
+ grid.setSpacing(spacing, x + spacing.relx, y, term)
proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockContext, x, y: int, term: TermAttributes) =
var x = x