aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2021-12-29 21:08:23 +0100
committerbptato <nincsnevem662@gmail.com>2021-12-29 21:08:23 +0100
commit270628aa3e3849a9fe1a666e687ead67056efda4 (patch)
tree4c20311a03081dc9c5ec7b3382d7d289f7728f33 /src
parentb7f19942e3d0a4cf25981c1686ffdb200ec4fd6f (diff)
downloadchawan-270628aa3e3849a9fe1a666e687ead67056efda4.tar.gz
Cascade optimizations, start layout engine rewrite
Diffstat (limited to 'src')
-rw-r--r--src/css/cascade.nim24
-rw-r--r--src/css/select.nim113
-rw-r--r--src/css/sheet.nim2
-rw-r--r--src/html/dom.nim2
-rw-r--r--src/layout/box.nim88
-rw-r--r--src/layout/engine.nim1017
-rw-r--r--src/render/renderdocument.nim108
7 files changed, 787 insertions, 567 deletions
diff --git a/src/css/cascade.nim b/src/css/cascade.nim
index 50dbd139..1b246821 100644
--- a/src/css/cascade.nim
+++ b/src/css/cascade.nim
@@ -16,7 +16,7 @@ type
ApplyResult = object
normal: seq[CSSDeclaration]
important: seq[CSSDeclaration]
- RuleList* = array[low(PseudoElem)..high(PseudoElem), seq[CSSSimpleBlock]]
+ RuleList* = array[PseudoElem, seq[CSSSimpleBlock]]
proc applyProperty(elem: Element, d: CSSDeclaration, pseudo: PseudoElem) =
var parent: CSSSpecifiedValues
@@ -88,7 +88,7 @@ func calcRules(elem: Element, rules: CSSStylesheet): RuleList =
for rule in rules:
tosorts.calcRule(elem, rule)
- for i in low(PseudoElem)..high(PseudoElem):
+ for i in PseudoElem:
tosorts[i].sort((x, y) => cmp(x.s,y.s))
result[i] = tosorts[i].map((x) => x.b)
@@ -131,6 +131,15 @@ proc applyRules(element: Element, ua, user, author: RuleList, pseudo: PseudoElem
for rule in ares.important:
element.applyProperty(rule, pseudo)
+func applyMediaQuery(ss: CSSStylesheet): CSSStylesheet =
+ for rule in ss:
+ if rule of CSSRuleDef:
+ result.add(rule)
+ elif rule of CSSMediaQueryDef:
+ let rule = CSSMediaQueryDef(rule)
+ if rule.query.applies():
+ result.add(rule.children.applyMediaQuery())
+
proc applyRules*(document: Document, ua, user: CSSStylesheet) =
var stack: seq[Element]
@@ -176,19 +185,18 @@ proc applyRules*(document: Document, ua, user: CSSStylesheet) =
let this_rules = embedded_rules.concat()
let authorrules = calcRules(elem, this_rules)
- for pseudo in low(PseudoElem)..high(PseudoElem):
+ for pseudo in PseudoElem:
elem.applyRules(uarules, userrules, authorrules, pseudo)
- var i = elem.children.len - 1
- while i >= 0:
- let child = elem.children[i]
- stack.add(child)
- dec i
+ for i in countdown(elem.children.high, 0):
+ stack.add(elem.children[i])
if rules_local.len > 0:
discard embedded_rules.pop()
proc applyStylesheets*(document: Document, uass, userss: CSSStylesheet) =
+ let uass = uass.applyMediaQuery()
+ let userss = userss.applyMediaQuery()
document.applyRules(uass, userss)
proc refreshStyle*(elem: Element) =
diff --git a/src/css/select.nim b/src/css/select.nim
index 9da20d4f..ec285432 100644
--- a/src/css/select.nim
+++ b/src/css/select.nim
@@ -65,6 +65,63 @@ func funcSelectorMatches(elem: Element, sel: Selector): SelectResult =
return selectres(false)
else: discard
+func combinatorSelectorMatches(elem: Element, sel: Selector): SelectResult =
+ #combinator without at least two members makes no sense
+ assert sel.csels.len > 1
+ let match = elem.selectorsMatch(sel.csels[^1])
+ if match.success:
+ var i = sel.csels.len - 2
+ case sel.ct
+ of DESCENDANT_COMBINATOR:
+ var e = elem.parentElement
+ while e != nil and i >= 0:
+ let res = e.selectorsMatch(sel.csels[i])
+
+ if res.pseudo != PSEUDO_NONE:
+ return selectres(false)
+
+ if res.success:
+ dec i
+ e = e.parentElement
+ of CHILD_COMBINATOR:
+ var e = elem.parentElement
+ while e != nil and i >= 0:
+ let res = e.selectorsMatch(sel.csels[i])
+
+ if res.pseudo != PSEUDO_NONE:
+ return selectres(false)
+
+ if not res.success:
+ return selectres(false)
+ dec i
+ e = e.parentElement
+ of NEXT_SIBLING_COMBINATOR:
+ var e = elem.previousElementSibling
+ while e != nil and i >= 0:
+ let res = e.selectorsMatch(sel.csels[i])
+
+ if res.pseudo != PSEUDO_NONE:
+ return selectres(false)
+
+ if not res.success:
+ return selectres(false)
+ dec i
+ e = e.previousElementSibling
+ of SUBSEQ_SIBLING_COMBINATOR:
+ var e = elem.previousElementSibling
+ while e != nil and i >= 0:
+ let res = e.selectorsMatch(sel.csels[i])
+
+ if res.pseudo != PSEUDO_NONE:
+ return selectres(false)
+
+ if res.success:
+ dec i
+ e = e.previousElementSibling
+ return selectres(i == -1, match.pseudo)
+ else:
+ return selectres(false)
+
func selectorMatches(elem: Element, sel: Selector): SelectResult =
case sel.t
of TYPE_SELECTOR:
@@ -84,61 +141,7 @@ func selectorMatches(elem: Element, sel: Selector): SelectResult =
of FUNC_SELECTOR:
return funcSelectorMatches(elem, sel)
of COMBINATOR_SELECTOR:
- #combinator without at least two members makes no sense
- assert sel.csels.len > 1
- let match = elem.selectorsMatch(sel.csels[^1])
- if match.success:
- var i = sel.csels.len - 2
- case sel.ct
- of DESCENDANT_COMBINATOR:
- var e = elem.parentElement
- while e != nil and i >= 0:
- let res = e.selectorsMatch(sel.csels[i])
-
- if res.pseudo != PSEUDO_NONE:
- return selectres(false)
-
- if res.success:
- dec i
- e = e.parentElement
- of CHILD_COMBINATOR:
- var e = elem.parentElement
- while e != nil and i >= 0:
- let res = e.selectorsMatch(sel.csels[i])
-
- if res.pseudo != PSEUDO_NONE:
- return selectres(false)
-
- if not res.success:
- return selectres(false)
- dec i
- e = e.parentElement
- of NEXT_SIBLING_COMBINATOR:
- var e = elem.previousElementSibling
- while e != nil and i >= 0:
- let res = e.selectorsMatch(sel.csels[i])
-
- if res.pseudo != PSEUDO_NONE:
- return selectres(false)
-
- if not res.success:
- return selectres(false)
- dec i
- e = e.previousElementSibling
- of SUBSEQ_SIBLING_COMBINATOR:
- var e = elem.previousElementSibling
- while e != nil and i >= 0:
- let res = e.selectorsMatch(sel.csels[i])
-
- if res.pseudo != PSEUDO_NONE:
- return selectres(false)
-
- if res.success:
- dec i
- e = e.previousElementSibling
- return selectres(i == -1, match.pseudo)
- else:
- return selectres(false)
+ return combinatorSelectorMatches(elem, sel)
func selectorsMatch*(elem: Element, selectors: SelectorList): SelectResult =
for sel in selectors.sels:
diff --git a/src/css/sheet.nim b/src/css/sheet.nim
index 468585fa..44e923cb 100644
--- a/src/css/sheet.nim
+++ b/src/css/sheet.nim
@@ -13,7 +13,7 @@ type
oblock*: CSSSimpleBlock
CSSConditionalDef* = ref object of CSSRuleBase
- children*: seq[CSSRuleBase]
+ children*: CSSStylesheet
CSSMediaQueryDef* = ref object of CSSConditionalDef
query*: MediaQueryList
diff --git a/src/html/dom.nim b/src/html/dom.nim
index f336c111..064682e7 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -67,7 +67,7 @@ type
classList*: seq[string]
attributes*: Table[string, string]
css*: CSSSpecifiedValues
- pseudo*: array[low(PseudoElem)..high(PseudoElem), CSSSpecifiedValues]
+ pseudo*: array[PseudoElem, CSSSpecifiedValues]
hover*: bool
cssapplied*: bool
rendered*: bool
diff --git a/src/layout/box.nim b/src/layout/box.nim
index a165c729..a18faf33 100644
--- a/src/layout/box.nim
+++ b/src/layout/box.nim
@@ -8,71 +8,65 @@ type
BoxType* = enum
BOX_INLINE, BOX_BLOCK, BOX_INLINE_BLOCK
- Rectangle* = object
- x1*: int
- x2*: int
- y1*: int
- y2*: int
-
CSSBox* = ref object of RootObj
t*: BoxType
x*: int
y*: int
width*: int
children*: seq[CSSBox]
- icontext*: InlineContext
- bcontext*: BlockContext
+ ictx*: InlineContext
+ bctx*: BlockContext
cssvalues*: CSSSpecifiedValues
node*: Node
+ viewport*: Viewport
- CSSInlineAtom* = ref object of CSSBox
- dimensions*: Rectangle
+ InlineAtom* = ref object of RootObj
+ relx*: int
+ width*: int
- CSSInlineRowPart* = ref object of CSSInlineAtom
+ InlineWord* = ref object of InlineAtom
str*: string
- rwidth*: int
+ fontstyle*: CSSFontStyle
+ fontweight*: int
+ textdecoration*: CSSTextDecoration
+ color*: CSSColor
+ nodes*: seq[Node]
- CSSInlineRow* = ref object
- content*: seq[CSSInlineAtom]
+ InlineRow* = ref object
+ atoms*: seq[InlineAtom]
+ relx*: int
rely*: int
width*: int
+ height*: int
InlineContext* = ref object
- fromx*: int
- fromy*: int
- xhigh*: int
+ relx*: int
+ width*: int
+ height*: int
+ rows*: seq[InlineRow]
+ thisrow*: InlineRow
+ specified*: CSSSpecifiedValues
+
whitespace*: bool
ws_initial*: bool
- rows*: seq[CSSRowBox]
- thisrow*: seq[CSSRowBox]
-
- #dimensions*: Rectangle
- #content*: seq[CSSInlineRow]
- #rcontent*: CSSInlineRow
- #color*: CSSColor
- #fontstyle*: CSSFontStyle
- #fontweight*: int
- #textdecoration*: CSSTextDecoration
- #nodes*: seq[Node]
-
- #maxwidth*: int
+ maxwidth*: int
- BlockContext* = ref object
- fromy*: int
- dimensions*: Rectangle
+ BlockContext* = ref object of InlineAtom
+ height*: int
margin_done*: int
margin_todo*: int
- width*: int
- height*: Option[int]
- content*: seq[CSSBox]
+ inlines*: seq[InlineContext]
+ nested*: seq[BlockContext]
+ specified*: CSSSpecifiedValues
- LayoutState* = object
- nodes*: seq[Node]
+ compwidth*: int
+ compheight*: Option[int]
+
+ Viewport* = ref object
term*: TermAttributes
- fromy*: int
- fromx*: int
+ nodes*: seq[Node]
- CSSRowBox* = object
+ RowBox* = object
x*: int
y*: int
width*: int
@@ -85,12 +79,6 @@ type
nodes*: seq[Node]
bottom*: int
- CSSInlineBox* = ref object of CSSBox
- CSSBlockBox* = ref object of CSSBox
- CSSInlineBlockBox* = ref object of CSSBlockBox
-
-func width*(rectangle: Rectangle): int =
- return rectangle.x2 - rectangle.x1
-
-func height*(rectangle: Rectangle): int =
- return rectangle.y2 - rectangle.y1
+ InlineBox* = ref object of CSSBox
+ BlockBox* = ref object of CSSBox
+ InlineBlockBox* = ref object of CSSBox
diff --git a/src/layout/engine.nim b/src/layout/engine.nim
index 97c24d01..27f4f91e 100644
--- a/src/layout/engine.nim
+++ b/src/layout/engine.nim
@@ -8,250 +8,571 @@ import css/values
import utils/twtstr
import io/term
-func cells_in(l: CSSLength, state: LayoutState, d: int, p: Option[int], o: bool): int =
+func cells_in(l: CSSLength, state: Viewport, d: int, p: Option[int], o: bool): int =
return cells(l, d, state.term.width_px, state.term.height_px, p, o)
-func cells_w(l: CSSLength, state: LayoutState, p: int): int =
+func cells_w(l: CSSLength, state: Viewport, p: int): int =
return l.cells_in(state, state.term.ppc, p.some, true)
-func cells_h(l: CSSLength, state: LayoutState, p: Option[int]): int =
+func cells_h(l: CSSLength, state: Viewport, p: Option[int]): int =
return l.cells_in(state, state.term.ppl, p, false)
-func cells_h(l: CSSLength, state: LayoutState, p: int): int =
+func cells_h(l: CSSLength, state: Viewport, p: int): int =
return l.cells_in(state, state.term.ppl, p.some, false)
-func newInlineContext*(): InlineContext =
- new(result)
- result.whitespace = true
- result.ws_initial = true
-
-func newBlockContext(): BlockContext =
- new(result)
-
-proc putRows(icontext: InlineContext) =
- var i = 0
- while i < icontext.rows.len:
- icontext.rows[i].bottom = icontext.fromy
- inc i
-
-proc flushRows(icontext: InlineContext) =
- if icontext.thisrow.len == 0: return
- icontext.putRows()
- var y = 0
- var re = false
- y = icontext.thisrow[0].bottom
- var i = 1
- while i < icontext.thisrow.len:
- let ry = icontext.thisrow[i].bottom
- if y != ry:
- re = true
- if y < ry:
- y = ry
- inc i
-
- if re:
- i = 0
- while i < icontext.thisrow.len:
- icontext.thisrow[i].y = y + icontext.thisrow[i].y - icontext.thisrow[i].bottom
- inc i
- icontext.rows.add(icontext.thisrow)
- icontext.thisrow.setLen(0)
-
-func conty(icontext: InlineContext): bool =
- icontext.thisrow.len > 0
-
-proc flushConty(box: CSSBox) =
- box.icontext.flushRows()
- box.icontext.fromx = box.x
- inc box.icontext.fromy
- inc box.bcontext.fromy
- box.icontext.whitespace = true
- box.icontext.ws_initial = true
-
-proc flushMargins(box: CSSBox) =
- box.icontext.fromy += box.bcontext.margin_todo
- box.bcontext.margin_done += box.bcontext.margin_todo
- box.bcontext.margin_todo = 0
-
-proc applyBlockStart(state: LayoutState, box, parent: CSSBox, vals: CSSSpecifiedValues) =
- parent.flushMargins()
- box.bcontext = newBlockContext()
- box.x += vals{"margin-left"}.cells_w(state, parent.bcontext.width)
-
- let mtop = vals{"margin-top"}.cells_h(state, parent.bcontext.width)
- if mtop > parent.bcontext.margin_done or mtop < 0:
- let diff = mtop - parent.bcontext.margin_done
- parent.icontext.fromy += diff
- parent.bcontext.margin_done += diff
-
- box.y = parent.icontext.fromy
- box.bcontext.margin_done = parent.bcontext.margin_done
-
- let pwidth = vals{"width"}
- if pwidth.auto:
- box.bcontext.width = parent.bcontext.width
- else:
- box.bcontext.width = pwidth.cells_w(state, parent.bcontext.width)
-
- let pheight = vals{"height"}
- if not pheight.auto:
- if pheight.unit != UNIT_PERC or parent.bcontext.height.issome:
- box.bcontext.height = pheight.cells_h(state, parent.bcontext.height).some
-
- box.cssvalues = vals
-
-func newBlockBox(state: var LayoutState, parent: CSSBox, vals: CSSSpecifiedValues): CSSBlockBox =
- new(result)
- result.t = BOX_BLOCK
- if parent.icontext.conty:
- parent.flushConty()
- result.x = parent.x
-
- state.applyBlockStart(result, parent, vals)
- result.icontext = newInlineContext()
- result.icontext.fromy = result.y
- result.icontext.fromx = result.x
-
-func newInlineBlockBox*(state: LayoutState, parent: CSSBox, vals: CSSSpecifiedValues): CSSInlineBlockBox =
- new(result)
- result.t = BOX_INLINE_BLOCK
- result.x = parent.icontext.fromx
-
- state.applyBlockStart(result, parent, vals)
- result.icontext = newInlineContext()
- result.icontext.fromy = result.y
- result.icontext.fromx = result.x
-
-func newInlineBox*(state: LayoutState, parent: CSSBox, vals: CSSSpecifiedValues): CSSInlineBox =
- new(result)
- result.t = BOX_INLINE
- result.x = parent.x
- result.y = parent.icontext.fromy
-
- result.icontext = parent.icontext
- result.bcontext = parent.bcontext
- result.cssvalues = vals
- result.icontext.fromx += vals{"margin-left"}.cells_w(state, parent.bcontext.width)
+#func newInlineContext*(): InlineContext =
+# new(result)
+# result.whitespace = true
+# result.ws_initial = true
+#
+#func newBlockContext(): BlockContext =
+# new(result)
+#
+#proc putRows(icontext: InlineContext) =
+# var i = 0
+# while i < icontext.rows.len:
+# icontext.rows[i].bottom = icontext.fromy
+# inc i
+#
+#proc flushRows(icontext: InlineContext) =
+# if icontext.thisrow.len == 0: return
+# icontext.putRows()
+# var y = 0
+# var re = false
+# y = icontext.thisrow[0].bottom
+# var i = 1
+# while i < icontext.thisrow.len:
+# let ry = icontext.thisrow[i].bottom
+# if y != ry:
+# re = true
+# if y < ry:
+# y = ry
+# inc i
+#
+# if re:
+# i = 0
+# while i < icontext.thisrow.len:
+# icontext.thisrow[i].y = y + icontext.thisrow[i].y - icontext.thisrow[i].bottom
+# inc i
+# icontext.rows.add(icontext.thisrow)
+# icontext.thisrow.setLen(0)
+#
+#func conty(icontext: InlineContext): bool =
+# icontext.thisrow.len > 0
+#
+#proc flushConty(box: CSSBox) =
+# box.icontext.flushRows()
+# box.icontext.fromx = box.x
+# inc box.icontext.fromy
+# inc box.bcontext.fromy
+# box.icontext.whitespace = true
+# box.icontext.ws_initial = true
+#
+#proc flushMargins(box: CSSBox) =
+# box.icontext.fromy += box.bcontext.margin_todo
+# box.bcontext.margin_done += box.bcontext.margin_todo
+# box.bcontext.margin_todo = 0
+#
+#proc applyBlockStart(state: LayoutState, box, parent: CSSBox, vals: CSSSpecifiedValues) =
+# parent.flushMargins()
+# box.bcontext = newBlockContext()
+# box.x += vals{"margin-left"}.cells_w(state, parent.bcontext.width)
+#
+# let mtop = vals{"margin-top"}.cells_h(state, parent.bcontext.width)
+# if mtop > parent.bcontext.margin_done or mtop < 0:
+# let diff = mtop - parent.bcontext.margin_done
+# parent.icontext.fromy += diff
+# parent.bcontext.margin_done += diff
+#
+# box.y = parent.icontext.fromy
+# box.bcontext.margin_done = parent.bcontext.margin_done
+#
+# let pwidth = vals{"width"}
+# if pwidth.auto:
+# box.bcontext.width = parent.bcontext.width
+# else:
+# box.bcontext.width = pwidth.cells_w(state, parent.bcontext.width)
+#
+# let pheight = vals{"height"}
+# if not pheight.auto:
+# if pheight.unit != UNIT_PERC or parent.bcontext.height.issome:
+# box.bcontext.height = pheight.cells_h(state, parent.bcontext.height).some
+#
+# box.cssvalues = vals
+#
+#func newBlockBox(state: var LayoutState, parent: CSSBox, vals: CSSSpecifiedValues): CSSBlockBox =
+# new(result)
+# result.t = BOX_BLOCK
+# if parent.icontext.conty:
+# parent.flushConty()
+# result.x = parent.x
+#
+# state.applyBlockStart(result, parent, vals)
+# result.icontext = newInlineContext()
+# result.icontext.fromy = result.y
+# result.icontext.fromx = result.x
+#
+#func newInlineBlockBox*(state: LayoutState, parent: CSSBox, vals: CSSSpecifiedValues): CSSInlineBlockBox =
+# new(result)
+# result.t = BOX_INLINE_BLOCK
+# result.x = parent.icontext.fromx
+#
+# state.applyBlockStart(result, parent, vals)
+# result.icontext = newInlineContext()
+# result.icontext.fromy = result.y
+# result.icontext.fromx = result.x
+#
+#func newInlineBox*(state: LayoutState, parent: CSSBox, vals: CSSSpecifiedValues): CSSInlineBox =
+# new(result)
+# result.t = BOX_INLINE
+# result.x = parent.x
+# result.y = parent.icontext.fromy
+#
+# result.icontext = parent.icontext
+# result.bcontext = parent.bcontext
+# result.cssvalues = vals
+# result.icontext.fromx += vals{"margin-left"}.cells_w(state, parent.bcontext.width)
+#
+#type InlineState = object
+# icontext: InlineContext
+# bcontext: BlockContext
+# ibox: CSSInlineBox
+# rowbox: CSSRowBox
+# word: seq[Rune]
+# ww: int
+# skip: bool
+# nodes: seq[Node]
+# cssvalues: CSSSpecifiedValues
+# x: int
+#
+#func maxwidth(state: InlineState): int = state.bcontext.width
+#
+#proc newRowBox(state: var InlineState) =
+# state.rowbox = CSSRowBox()
+# state.rowbox.x = state.icontext.fromx
+# state.rowbox.y = state.icontext.fromy
+# state.rowbox.bottom = state.rowbox.y
+#
+# let cssvalues = state.cssvalues
+# state.rowbox.color = cssvalues{"color"}
+# state.rowbox.fontstyle = cssvalues{"font-style"}
+# state.rowbox.fontweight = cssvalues{"font-weight"}
+# state.rowbox.textdecoration = cssvalues{"text-decoration"}
+# state.rowbox.nodes = state.nodes
+#
+#proc addRowBox(state: var InlineState) =
+# state.icontext.thisrow.add(state.rowbox)
+#
+#proc inlineWrap(state: var InlineState) =
+# state.addRowBox()
+# state.icontext.flushRows()
+# inc state.icontext.fromy
+# state.icontext.fromx = state.x
+# if state.word.len == 0:
+# state.icontext.whitespace = true
+# state.icontext.ws_initial = true
+# else:
+# if state.word[^1] == Rune(' '):
+# state.icontext.whitespace = true
+# state.icontext.ws_initial = false
+# state.newRowBox()
+#
+#proc addWord(state: var InlineState) =
+# state.rowbox.str &= $state.word
+# state.rowbox.width += state.ww
+# state.ibox.width += state.ww
+# state.ibox.width = min(state.maxwidth, state.ibox.width)
+# state.word.setLen(0)
+# state.ww = 0
+#
+#proc wrapNormal(state: var InlineState, r: Rune) =
+# if state.icontext.fromx + state.rowbox.width + state.ww == state.maxwidth and r == Rune(' '):
+# state.addWord()
+# if state.word.len == 0:
+# if r == Rune(' '):
+# state.skip = true
+# elif state.word[0] == Rune(' '):
+# state.word = state.word.substr(1)
+# dec state.ww
+# state.inlineWrap()
+# if not state.skip and r == Rune(' '):
+# state.icontext.whitespace = true
+# state.icontext.ws_initial = false
+#
+#proc checkWrap(state: var InlineState, r: Rune) =
+# if state.cssvalues{"white-space"} in {WHITESPACE_NOWRAP, WHITESPACE_PRE}:
+# return
+# case state.cssvalues{"word-break"}
+# of WORD_BREAK_NORMAL:
+# if state.icontext.fromx + state.rowbox.width > state.x and
+# state.icontext.fromx + state.rowbox.width + state.ww + r.width() > state.maxwidth:
+# state.wrapNormal(r)
+# of WORD_BREAK_BREAK_ALL:
+# if state.icontext.fromx + state.rowbox.width + state.ww + r.width() > state.maxwidth:
+# var pl: seq[Rune]
+# var i = 0
+# var w = 0
+# while i < state.word.len and
+# state.icontext.fromx + state.rowbox.width + w <
+# state.bcontext.width:
+# pl &= state.word[i]
+# w += state.word[i].width()
+# inc i
+#
+# if pl.len > 0:
+# state.rowbox.str &= $pl
+# state.rowbox.width += w
+# state.word = state.word.substr(pl.len)
+# state.ww = state.word.width()
+# if r == Rune(' '):
+# state.skip = true
+# state.inlineWrap()
+# of WORD_BREAK_KEEP_ALL:
+# if state.icontext.fromx + state.rowbox.width > state.x and
+# state.icontext.fromx + state.rowbox.width + state.ww + r.width() > state.maxwidth:
+# state.wrapNormal(r)
+#
+#proc preWrap(state: var InlineState) =
+# state.inlineWrap()
+# state.icontext.whitespace = false
+# state.icontext.ws_initial = true
+# state.skip = true
+#
+#proc processInlineText(str: string, ibox: CSSInlineBox, nodes: seq[Node]) =
+# var state: InlineState
+# state.ibox = ibox
+# state.icontext = ibox.icontext
+# state.bcontext = ibox.bcontext
+# state.cssvalues = ibox.cssvalues
+# state.x = ibox.x
+# state.nodes = nodes
+#
+# var i = 0
+# state.newRowBox()
+#
+# var r: Rune
+# while i < str.len:
+# var rw = 0
+# case str[i]
+# of ' ', '\n', '\t':
+# rw = 1
+# r = Rune(str[i])
+# inc i
+# state.addWord()
+#
+# case state.cssvalues{"white-space"}
+# of WHITESPACE_NORMAL, WHITESPACE_NOWRAP:
+# if state.icontext.whitespace:
+# if state.icontext.ws_initial:
+# state.icontext.ws_initial = false
+# state.skip = true
+# else:
+# state.skip = true
+# state.icontext.whitespace = true
+# of WHITESPACE_PRE_LINE:
+# if state.icontext.whitespace:
+# state.skip = true
+# state.icontext.ws_initial = false
+# if r == Rune('\n'):
+# state.preWrap()
+# of WHITESPACE_PRE, WHITESPACE_PRE_WRAP:
+# state.icontext.ws_initial = false
+# if r == Rune('\n'):
+# state.preWrap()
+# r = Rune(' ')
+# else:
+# state.icontext.whitespace = false
+# fastRuneAt(str, i, r)
+# rw = r.width()
+#
+# # TODO a better line wrapping algorithm would be nice... especially because
+# # this one doesn't even work
+# if rw > 1 or state.cssvalues{"word-break"} == WORD_BREAK_BREAK_ALL:
+# state.addWord()
+#
+# state.checkWrap(r)
+#
+# if state.skip:
+# state.skip = false
+# continue
+#
+# state.word &= r
+# state.ww += rw
+#
+# state.addWord()
+#
+# if state.rowbox.str.len > 0:
+# state.addRowBox()
+# state.icontext.fromx += state.rowbox.width
+#
+# state.bcontext.margin_todo = 0
+# state.bcontext.margin_done = 0
+#
+#proc processInlineContext(ibox: CSSInlineBox, str: string, nodes: seq[Node]) =
+# processInlineText(str, ibox, nodes)
+#
+#proc processInlineBox(state: var LayoutState, parent: CSSBox, str: string): CSSInlineBox =
+# if str.len == 0:
+# return nil
+#
+# #TODO this doesn't really belong in here
+# parent.flushMargins()
+#
+# if parent of CSSInlineBox:
+# let ibox = CSSInlineBox(parent)
+# ibox.processInlineContext(str, state.nodes)
+# return nil
+#
+# let ibox = state.newInlineBox(parent, parent.cssvalues.inheritProperties())
+# ibox.processInlineContext(str, state.nodes)
+# return ibox
+#
+#proc applyBlockEnd(state: var LayoutState, parent, box: CSSBox) =
+# box.flushMargins()
+#
+# let mbot = box.cssvalues{"margin-bottom"}.cells_h(state, parent.bcontext.width)
+# parent.bcontext.margin_todo += mbot
+#
+# parent.bcontext.margin_done = box.bcontext.margin_done
+# parent.bcontext.margin_todo = max(parent.bcontext.margin_todo - box.bcontext.margin_done, 0)
+#
+# if box.bcontext.height.isnone:
+# parent.icontext.fromy = box.icontext.fromy
+# else:
+# parent.icontext.fromy += box.bcontext.height.get
+#
+#proc add(state: var LayoutState, parent: CSSBox, box: CSSBlockBox) =
+# parent.icontext.fromx = parent.x
+# if box.icontext.conty:
+# box.flushConty()
+#
+# state.applyBlockEnd(parent, box)
+# parent.children.add(box)
+#
+#proc add(state: var LayoutState, parent: CSSBox, box: CSSInlineBox) =
+# parent.width = max(parent.width, box.width)
+# parent.icontext.fromx += box.cssvalues{"margin-right"}.cells_w(state, parent.bcontext.width)
+# parent.icontext.fromy = box.icontext.fromy
+#
+# parent.children.add(box)
+#
+#proc add(state: var LayoutState, parent: CSSBox, box: CSSInlineBlockBox) =
+# parent.width = max(parent.width, box.width)
+# parent.icontext.fromx = max(box.icontext.fromx, box.x + box.width)
+# parent.icontext.fromx += box.cssvalues{"margin-right"}.cells_w(state, parent.bcontext.width)
+# parent.icontext.whitespace = box.icontext.whitespace
+# parent.icontext.ws_initial = box.icontext.ws_initial
+#
+# box.icontext.putRows()
+# parent.icontext.thisrow.add(box.icontext.rows)
+# parent.icontext.thisrow.add(box.icontext.thisrow)
+# box.icontext.rows.setLen(0)
+# box.icontext.thisrow.setLen(0)
+#
+# state.applyBlockEnd(parent, box)
+# parent.children.add(box)
+#
+#proc add(state: var LayoutState, parent: CSSBox, box: CSSBox) =
+# case box.t
+# of BOX_BLOCK: state.add(parent, CSSBlockBox(box))
+# of BOX_INLINE: state.add(parent, CSSInlineBox(box))
+# of BOX_INLINE_BLOCK: state.add(parent, CSSInlineBlockBox(box))
+#
+#proc processComputedValueBox(state: var LayoutState, parent: CSSBox, values: CSSSpecifiedValues): CSSBox =
+# case values{"display"}
+# of DISPLAY_BLOCK:
+# result = state.newBlockBox(parent, values)
+# of DISPLAY_INLINE_BLOCK:
+# result = state.newInlineBlockBox(parent, values)
+# of DISPLAY_INLINE:
+# result = state.newInlineBox(parent, values)
+# of DISPLAY_LIST_ITEM:
+# result = state.newBlockBox(parent, values)
+# of DISPLAY_NONE:
+# return nil
+# else:
+# return nil
+#
+#proc processBr(state: var LayoutState, parent: CSSBox, vals: CSSSpecifiedValues) =
+# if vals{"display"} == DISPLAY_INLINE:
+# if parent.icontext.conty:
+# parent.flushConty()
+# else:
+# inc parent.bcontext.fromy
+# inc parent.icontext.fromy
+# parent.icontext.fromx = parent.x
+#
+#proc processElemBox(state: var LayoutState, parent: CSSBox, elem: Element): CSSBox =
+# if elem.tagType == TAG_BR:
+# state.processBr(parent, elem.css)
+#
+# result = state.processComputedValueBox(parent, elem.css)
+# if result != nil:
+# result.node = elem
+#
+#proc processElemChildren(state: var LayoutState, parent: CSSBox, elem: Element)
+#
+#proc processNode(state: var LayoutState, parent: CSSBox, node: Node): CSSBox =
+# case node.nodeType
+# of ELEMENT_NODE:
+# let elem = Element(node)
+# result = state.processElemBox(parent, Element(node))
+# if result == nil:
+# return
+#
+# state.processElemChildren(result, elem)
+# of TEXT_NODE:
+# let text = Text(node)
+# result = state.processInlineBox(parent, text.data)
+# if result != nil:
+# result.node = node
+# else: discard
+#
+#proc processBeforePseudoElem(state: var LayoutState, parent: CSSBox, elem: Element) =
+# if elem.pseudo[PSEUDO_BEFORE] != nil:
+# let box = state.processComputedValueBox(parent, elem.pseudo[PSEUDO_BEFORE])
+# if box == nil: return
+# box.node = elem
+#
+# let text = elem.pseudo[PSEUDO_BEFORE]{"content"}
+# var inline = state.processInlineBox(box, $text)
+# if inline != nil:
+# inline.node = elem
+# state.add(box, inline)
+#
+# state.add(parent, box)
+#
+#proc processAfterPseudoElem(state: var LayoutState, parent: CSSBox, elem: Element) =
+# if elem.pseudo[PSEUDO_AFTER] != nil:
+# let box = state.processComputedValueBox(parent, elem.pseudo[PSEUDO_AFTER])
+# if box == nil: return
+# box.node = elem
+#
+# let text = elem.pseudo[PSEUDO_AFTER]{"content"}
+# var inline = state.processInlineBox(box, $text)
+# if inline != nil:
+# inline.node = elem
+# state.add(box, inline)
+#
+# state.add(parent, box)
+#
+#proc processMarker(state: var LayoutState, parent: CSSBox, elem: Element) =
+# if elem.css{"display"} == DISPLAY_LIST_ITEM:
+# var ordinalvalue = 1
+# if elem.tagType == TAG_LI:
+# ordinalvalue = HTMLLIElement(elem).ordinalvalue
+#
+# let text = elem.css{"list-style-type"}.listMarker(ordinalvalue)
+# let tlen = text.width()
+# parent.icontext.fromx -= tlen
+# let marker = state.processInlineBox(parent, text)
+# if marker != nil:
+# state.add(parent, marker)
+#
+#proc processNodes(state: var LayoutState, parent: CSSBox, nodes: seq[Node]) =
+# for node in nodes:
+# let box = state.processNode(parent, node)
+# if box != nil:
+# state.add(parent, box)
+#
+#proc processElemChildren(state: var LayoutState, parent: CSSBox, elem: Element) =
+# state.nodes.add(elem)
+#
+# state.processBeforePseudoElem(parent, elem)
+# state.processMarker(parent, elem)
+# state.processNodes(parent, elem.childNodes)
+# state.processAfterPseudoElem(parent, elem)
+#
+# discard state.nodes.pop()
+#
+#proc alignBoxes*(document: Document, term: TermAttributes): CSSBox =
+# var state: LayoutState
+# state.term = term
+# var rootbox = CSSBlockBox(x: 0, y: 0)
+# rootbox.cssvalues = rootProperties()
+# rootbox.bcontext = newBlockContext()
+# rootbox.icontext = newInlineContext()
+# rootbox.bcontext.width = term.width
+# state.nodes.add(document.root)
+# state.processElemChildren(rootbox, document.root)
+# return rootbox
type InlineState = object
- icontext: InlineContext
- bcontext: BlockContext
- ibox: CSSInlineBox
- rowbox: CSSRowBox
- word: seq[Rune]
- ww: int
+ ictx: InlineContext
skip: bool
nodes: seq[Node]
- cssvalues: CSSSpecifiedValues
- x: int
-
-func maxwidth(state: InlineState): int = state.bcontext.width
-
-proc newRowBox(state: var InlineState) =
- state.rowbox = CSSRowBox()
- state.rowbox.x = state.icontext.fromx
- state.rowbox.y = state.icontext.fromy
- state.rowbox.bottom = state.rowbox.y
+ word: InlineWord
+ maxwidth: int
+
+proc newWord(state: var InlineState) =
+ let word = InlineWord()
+ let specified = state.ictx.specified
+ word.color = specified{"color"}
+ word.fontstyle = specified{"font-style"}
+ word.fontweight = specified{"font-weight"}
+ word.textdecoration = specified{"text-decoration"}
+ word.nodes = state.nodes
+ word.relx = state.ictx.thisrow.width
+ state.word = word
- let cssvalues = state.cssvalues
- state.rowbox.color = cssvalues{"color"}
- state.rowbox.fontstyle = cssvalues{"font-style"}
- state.rowbox.fontweight = cssvalues{"font-weight"}
- state.rowbox.textdecoration = cssvalues{"text-decoration"}
- state.rowbox.nodes = state.nodes
+proc addWord(state: var InlineState) =
+ let row = state.ictx.thisrow
+ let word = state.word
+ row.width += word.width
+ if row.height == 0: #first word => higher than 0
+ row.height = 1
+ row.atoms.add(word)
+ state.newWord()
-proc addRowBox(state: var InlineState) =
- state.icontext.thisrow.add(state.rowbox)
+func wordLen(state: InlineState): int =
+ state.word.str.len
proc inlineWrap(state: var InlineState) =
- state.addRowBox()
- state.icontext.flushRows()
- inc state.icontext.fromy
- state.icontext.fromx = state.x
- if state.word.len == 0:
- state.icontext.whitespace = true
- state.icontext.ws_initial = true
- else:
- if state.word[^1] == Rune(' '):
- state.icontext.whitespace = true
- state.icontext.ws_initial = false
- state.newRowBox()
-
-proc addWord(state: var InlineState) =
- state.rowbox.str &= $state.word
- state.rowbox.width += state.ww
- state.ibox.width += state.ww
- state.ibox.width = min(state.maxwidth, state.ibox.width)
- state.word.setLen(0)
- state.ww = 0
+ state.addWord()
+ state.ictx.rows.add(state.ictx.thisrow)
+ state.ictx.thisrow = nil
-proc wrapNormal(state: var InlineState, r: Rune) =
- if state.icontext.fromx + state.rowbox.width + state.ww == state.maxwidth and r == Rune(' '):
+proc wrap(state: var InlineState, r: Rune) =
+ if state.ictx.thisrow.width + state.word.width == state.maxwidth and r == Rune(' '):
state.addWord()
- if state.word.len == 0:
+ if state.wordLen == 0:
if r == Rune(' '):
state.skip = true
- elif state.word[0] == Rune(' '):
- state.word = state.word.substr(1)
- dec state.ww
+ elif state.word.str[0] == ' ':
+ state.word.str = state.word.str.substr(1)
+ dec state.word.width
state.inlineWrap()
if not state.skip and r == Rune(' '):
- state.icontext.whitespace = true
- state.icontext.ws_initial = false
+ state.ictx.whitespace = true
+ state.ictx.ws_initial = false
proc checkWrap(state: var InlineState, r: Rune) =
- if state.cssvalues{"white-space"} in {WHITESPACE_NOWRAP, WHITESPACE_PRE}:
- return
- case state.cssvalues{"word-break"}
- of WORD_BREAK_NORMAL:
- if state.icontext.fromx + state.rowbox.width > state.x and
- state.icontext.fromx + state.rowbox.width + state.ww + r.width() > state.maxwidth:
- state.wrapNormal(r)
- of WORD_BREAK_BREAK_ALL:
- if state.icontext.fromx + state.rowbox.width + state.ww + r.width() > state.maxwidth:
- var pl: seq[Rune]
- var i = 0
- var w = 0
- while i < state.word.len and
- state.icontext.fromx + state.rowbox.width + w <
- state.bcontext.width:
- pl &= state.word[i]
- w += state.word[i].width()
- inc i
+ if state.ictx.thisrow.width > state.maxwidth:
+ state.wrap(r)
- if pl.len > 0:
- state.rowbox.str &= $pl
- state.rowbox.width += w
- state.word = state.word.substr(pl.len)
- state.ww = state.word.width()
- if r == Rune(' '):
- state.skip = true
- state.inlineWrap()
- of WORD_BREAK_KEEP_ALL:
- if state.icontext.fromx + state.rowbox.width > state.x and
- state.icontext.fromx + state.rowbox.width + state.ww + r.width() > state.maxwidth:
- state.wrapNormal(r)
+proc checkRow(ictx: InlineContext) =
+ if ictx.thisrow == nil:
+ ictx.thisrow = InlineRow()
-proc preWrap(state: var InlineState) =
- state.inlineWrap()
- state.icontext.whitespace = false
- state.icontext.ws_initial = true
- state.skip = true
+proc flushLine(ictx: InlineContext) =
+ let oldrow = ictx.thisrow
+ ictx.rows.add(ictx.thisrow)
+ ictx.thisrow = InlineRow()
+ ictx.thisrow.rely = oldrow.rely + oldrow.height
-proc processInlineText(str: string, ibox: CSSInlineBox, nodes: seq[Node]) =
+proc renderText*(ictx: InlineContext, str: string, maxwidth: int) =
var state: InlineState
- state.ibox = ibox
- state.icontext = ibox.icontext
- state.bcontext = ibox.bcontext
- state.cssvalues = ibox.cssvalues
- state.x = ibox.x
- state.nodes = nodes
+ state.ictx = ictx
+ state.maxwidth = maxwidth
+ state.ictx.checkRow()
+ state.newWord()
var i = 0
- state.newRowBox()
var r: Rune
while i < str.len:
+ state.ictx.checkRow()
+
var rw = 0
case str[i]
of ' ', '\n', '\t':
@@ -259,245 +580,63 @@ proc processInlineText(str: string, ibox: CSSInlineBox, nodes: seq[Node]) =
r = Rune(str[i])
inc i
state.addWord()
-
- case state.cssvalues{"white-space"}
- of WHITESPACE_NORMAL, WHITESPACE_NOWRAP:
- if state.icontext.whitespace:
- if state.icontext.ws_initial:
- state.icontext.ws_initial = false
- state.skip = true
- else:
- state.skip = true
- state.icontext.whitespace = true
- of WHITESPACE_PRE_LINE:
- if state.icontext.whitespace:
- state.skip = true
- state.icontext.ws_initial = false
- if r == Rune('\n'):
- state.preWrap()
- of WHITESPACE_PRE, WHITESPACE_PRE_WRAP:
- state.icontext.ws_initial = false
- if r == Rune('\n'):
- state.preWrap()
- r = Rune(' ')
else:
- state.icontext.whitespace = false
+ ictx.whitespace = false
fastRuneAt(str, i, r)
rw = r.width()
- # TODO a better line wrapping algorithm would be nice... especially because
- # this one doesn't even work
- if rw > 1 or state.cssvalues{"word-break"} == WORD_BREAK_BREAK_ALL:
- state.addWord()
-
state.checkWrap(r)
if state.skip:
state.skip = false
continue
- state.word &= r
- state.ww += rw
-
- state.addWord()
-
- if state.rowbox.str.len > 0:
- state.addRowBox()
- state.icontext.fromx += state.rowbox.width
-
- state.bcontext.margin_todo = 0
- state.bcontext.margin_done = 0
-
-proc processInlineContext(ibox: CSSInlineBox, str: string, nodes: seq[Node]) =
- processInlineText(str, ibox, nodes)
-
-proc processInlineBox(state: var LayoutState, parent: CSSBox, str: string): CSSInlineBox =
- if str.len == 0:
- return nil
-
- #TODO this doesn't really belong in here
- parent.flushMargins()
-
- if parent of CSSInlineBox:
- let ibox = CSSInlineBox(parent)
- ibox.processInlineContext(str, state.nodes)
- return nil
+ state.word.str &= r
+ state.word.width += rw
- let ibox = state.newInlineBox(parent, parent.cssvalues.inheritProperties())
- ibox.processInlineContext(str, state.nodes)
- return ibox
-
-proc applyBlockEnd(state: var LayoutState, parent, box: CSSBox) =
- box.flushMargins()
-
- let mbot = box.cssvalues{"margin-bottom"}.cells_h(state, parent.bcontext.width)
- parent.bcontext.margin_todo += mbot
-
- parent.bcontext.margin_done = box.bcontext.margin_done
- parent.bcontext.margin_todo = max(parent.bcontext.margin_todo - box.bcontext.margin_done, 0)
-
- if box.bcontext.height.isnone:
- parent.icontext.fromy = box.icontext.fromy
- else:
- parent.icontext.fromy += box.bcontext.height.get
-
-proc add(state: var LayoutState, parent: CSSBox, box: CSSBlockBox) =
- parent.icontext.fromx = parent.x
- if box.icontext.conty:
- box.flushConty()
-
- state.applyBlockEnd(parent, box)
- parent.children.add(box)
-
-proc add(state: var LayoutState, parent: CSSBox, box: CSSInlineBox) =
- parent.width = max(parent.width, box.width)
- parent.icontext.fromx += box.cssvalues{"margin-right"}.cells_w(state, parent.bcontext.width)
- parent.icontext.fromy = box.icontext.fromy
-
- parent.children.add(box)
-
-proc add(state: var LayoutState, parent: CSSBox, box: CSSInlineBlockBox) =
- parent.width = max(parent.width, box.width)
- parent.icontext.fromx = max(box.icontext.fromx, box.x + box.width)
- parent.icontext.fromx += box.cssvalues{"margin-right"}.cells_w(state, parent.bcontext.width)
- parent.icontext.whitespace = box.icontext.whitespace
- parent.icontext.ws_initial = box.icontext.ws_initial
-
- box.icontext.putRows()
- parent.icontext.thisrow.add(box.icontext.rows)
- parent.icontext.thisrow.add(box.icontext.thisrow)
- box.icontext.rows.setLen(0)
- box.icontext.thisrow.setLen(0)
-
- state.applyBlockEnd(parent, box)
- parent.children.add(box)
+ if state.word.str.len > 0:
+ state.addWord()
-proc add(state: var LayoutState, parent: CSSBox, box: CSSBox) =
- case box.t
- of BOX_BLOCK: state.add(parent, CSSBlockBox(box))
- of BOX_INLINE: state.add(parent, CSSInlineBox(box))
- of BOX_INLINE_BLOCK: state.add(parent, CSSInlineBlockBox(box))
+proc finish(ictx: InlineContext) =
+ if ictx.thisrow != nil and ictx.thisrow.atoms.len > 0:
+ ictx.rows.add(ictx.thisrow)
+ ictx.thisrow = nil
-proc processComputedValueBox(state: var LayoutState, parent: CSSBox, values: CSSSpecifiedValues): CSSBox =
- case values{"display"}
- of DISPLAY_BLOCK:
- result = state.newBlockBox(parent, values)
- of DISPLAY_INLINE_BLOCK:
- result = state.newInlineBlockBox(parent, values)
- of DISPLAY_INLINE:
- result = state.newInlineBox(parent, values)
- of DISPLAY_LIST_ITEM:
- result = state.newBlockBox(parent, values)
- of DISPLAY_NONE:
- return nil
+proc newBlockContext(parent: BlockBox, specified: CSSSpecifiedValues): BlockContext =
+ new(result)
+ let pwidth = specified{"width"}
+ if pwidth.auto:
+ result.compwidth = parent.bctx.compwidth
else:
- return nil
-
-proc processBr(state: var LayoutState, parent: CSSBox, vals: CSSSpecifiedValues) =
- if vals{"display"} == DISPLAY_INLINE:
- if parent.icontext.conty:
- parent.flushConty()
- else:
- inc parent.bcontext.fromy
- inc parent.icontext.fromy
- parent.icontext.fromx = parent.x
-
-proc processElemBox(state: var LayoutState, parent: CSSBox, elem: Element): CSSBox =
- if elem.tagType == TAG_BR:
- state.processBr(parent, elem.css)
-
- result = state.processComputedValueBox(parent, elem.css)
- if result != nil:
- result.node = elem
-
-proc processElemChildren(state: var LayoutState, parent: CSSBox, elem: Element)
-
-proc processNode(state: var LayoutState, parent: CSSBox, node: Node): CSSBox =
- case node.nodeType
- of ELEMENT_NODE:
- let elem = Element(node)
- result = state.processElemBox(parent, Element(node))
- if result == nil:
- return
-
- state.processElemChildren(result, elem)
- of TEXT_NODE:
- let text = Text(node)
- result = state.processInlineBox(parent, text.data)
- if result != nil:
- result.node = node
- else: discard
-
-proc processBeforePseudoElem(state: var LayoutState, parent: CSSBox, elem: Element) =
- if elem.pseudo[PSEUDO_BEFORE] != nil:
- let box = state.processComputedValueBox(parent, elem.pseudo[PSEUDO_BEFORE])
- if box == nil: return
- box.node = elem
-
- let text = elem.pseudo[PSEUDO_BEFORE]{"content"}
- var inline = state.processInlineBox(box, $text)
- if inline != nil:
- inline.node = elem
- state.add(box, inline)
-
- state.add(parent, box)
-
-proc processAfterPseudoElem(state: var LayoutState, parent: CSSBox, elem: Element) =
- if elem.pseudo[PSEUDO_AFTER] != nil:
- let box = state.processComputedValueBox(parent, elem.pseudo[PSEUDO_AFTER])
- if box == nil: return
- box.node = elem
-
- let text = elem.pseudo[PSEUDO_AFTER]{"content"}
- var inline = state.processInlineBox(box, $text)
- if inline != nil:
- inline.node = elem
- state.add(box, inline)
-
- state.add(parent, box)
-
-proc processMarker(state: var LayoutState, parent: CSSBox, elem: Element) =
- if elem.css{"display"} == DISPLAY_LIST_ITEM:
- var ordinalvalue = 1
- if elem.tagType == TAG_LI:
- ordinalvalue = HTMLLIElement(elem).ordinalvalue
-
- let text = elem.css{"list-style-type"}.listMarker(ordinalvalue)
- let tlen = text.width()
- parent.icontext.fromx -= tlen
- let marker = state.processInlineBox(parent, text)
- if marker != nil:
- state.add(parent, marker)
-
-proc processNodes(state: var LayoutState, parent: CSSBox, nodes: seq[Node]) =
- for node in nodes:
- let box = state.processNode(parent, node)
- if box != nil:
- state.add(parent, box)
-
-proc processElemChildren(state: var LayoutState, parent: CSSBox, elem: Element) =
- state.nodes.add(elem)
-
- state.processBeforePseudoElem(parent, elem)
- state.processMarker(parent, elem)
- state.processNodes(parent, elem.childNodes)
- state.processAfterPseudoElem(parent, elem)
-
- discard state.nodes.pop()
-
-proc alignBoxes*(document: Document, term: TermAttributes): CSSBox =
- var state: LayoutState
- state.term = term
- var rootbox = CSSBlockBox(x: 0, y: 0)
- rootbox.cssvalues = rootProperties()
- rootbox.bcontext = newBlockContext()
- rootbox.icontext = newInlineContext()
- rootbox.bcontext.width = term.width
- state.nodes.add(document.root)
- state.processElemChildren(rootbox, document.root)
- return rootbox
-
-#proc alignBoxes2*(document: Document, term: TermAttributes): CSSBlockBox =
-# result = CSSBlockBox()
-# result.bcontext = BlockContext()
-# result.bcontext.content.add(CSSInlineBox())
+ result.compwidth = pwidth.cells_w(parent.viewport, parent.bctx.compwidth)
+ result.specified = specified
+
+proc newBlockBox(parent: BlockBox, specified: CSSSpecifiedValues): BlockBox =
+ result = BlockBox()
+ result.bctx = parent.newBlockContext(specified)
+
+proc newBlockBox(viewport: Viewport, specified: CSSSpecifiedValues): BlockBox =
+ result = BlockBox()
+ result.bctx = BlockContext(compwidth: viewport.term.width)
+ result.bctx.specified = specified
+
+proc newInlineContext(box: BlockBox): InlineContext =
+ result = InlineContext()
+ result.specified = box.bctx.specified
+ box.bctx.inlines.add(result)
+
+proc processElement(parent: CSSBox, element: Element) =
+ if parent of BlockBox:
+ let parent = BlockBox(parent)
+ let inline = InlineBox()
+ inline.ictx = parent.newInlineContext()
+ inline.ictx.renderText("Hello, world!", parent.viewport.term.width)
+ inline.ictx.flushLine()
+ inline.ictx.renderText("Hello, world!", parent.viewport.term.width)
+ inline.ictx.finish()
+
+proc alignBoxes*(document: Document, term: TermAttributes): BlockBox =
+ var viewport = Viewport(term: term)
+ result = newBlockBox(viewport, rootProperties())
+ result.viewport = viewport
+ result.processElement(document.root)
diff --git a/src/render/renderdocument.nim b/src/render/renderdocument.nim
index 21ae0945..cb89c965 100644
--- a/src/render/renderdocument.nim
+++ b/src/render/renderdocument.nim
@@ -11,7 +11,7 @@ import layout/box
import layout/engine
import utils/twtstr
-func formatFromLine(line: CSSRowBox): Formatting =
+func formatFromLine(line: RowBox): Formatting =
result.fgcolor = line.color.cellColor()
if line.fontstyle in { FONT_STYLE_ITALIC, FONT_STYLE_OBLIQUE }:
result.italic = true
@@ -24,7 +24,7 @@ func formatFromLine(line: CSSRowBox): Formatting =
if line.textdecoration == TEXT_DECORATION_LINE_THROUGH:
result.strike = true
-proc setRowBox(lines: var FlexibleGrid, line: CSSRowBox) =
+proc setRowBox(lines: var FlexibleGrid, line: RowBox) =
var r: Rune
var x = line.x
@@ -68,20 +68,102 @@ proc setRowBox(lines: var FlexibleGrid, line: CSSRowBox) =
let oline = FlexibleLine(str: ostr.substr(i), formats: oformats.subformats(i))
lines[y].add(oline)
+func formatFromWord(word: InlineWord): Formatting =
+ result.fgcolor = word.color.cellColor()
+ if word.fontstyle in { FONT_STYLE_ITALIC, FONT_STYLE_OBLIQUE }:
+ result.italic = true
+ if word.fontweight > 500:
+ result.bold = true
+ if word.textdecoration == TEXT_DECORATION_UNDERLINE:
+ result.underline = true
+ if word.textdecoration == TEXT_DECORATION_OVERLINE:
+ result.overline = true
+ if word.textdecoration == TEXT_DECORATION_LINE_THROUGH:
+ result.strike = true
+
+proc setRowWord(lines: var FlexibleGrid, word: InlineWord, x, y: int) =
+ var r: Rune
+
+ var x = x
+ var i = 0
+ while x < 0:
+ fastRuneAt(word.str, i, r)
+ x += r.width()
+ let linestr = word.str.substr(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)
+
+ lines.addFormat(y, i, word.formatFromWord(), word.nodes)
+
+ var nx = cx
+ if nx < x:
+ lines[y].str &= ' '.repeat(x - nx)
+ nx = x
+
+ lines[y].str &= linestr
+ nx += linestr.width()
+
+ 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)
+
+proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext, x, y: int) =
+ for row in ctx.rows:
+ let x = x + row.relx
+ let y = y + row.rely
+ for atom in row.atoms:
+ if atom of BlockContext:
+ let ctx = BlockContext(atom)
+ grid.renderBlockContext(ctx, x + ctx.relx, y)
+ elif atom of InlineWord:
+ let word = InlineWord(atom)
+ grid.setRowWord(word, x + word.relx, y)
+
+proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockContext, x, y: int) =
+ var x = x
+ var y = y
+ if ctx.nested.len == 0:
+ for ictx in ctx.inlines:
+ grid.renderInlineContext(ictx, x + ictx.relx, y)
+ else:
+ for ctx in ctx.nested:
+ grid.renderBlockContext(ctx, x + ctx.relx, y)
+
const css = staticRead"res/ua.css"
let uastyle = css.parseStylesheet()
proc renderDocument*(document: Document, attrs: TermAttributes, userstyle: CSSStylesheet): FlexibleGrid =
document.applyStylesheets(uastyle, userstyle)
let rootbox = document.alignBoxes(attrs)
- var stack: seq[CSSBox]
- stack.add(rootbox)
+ var stack: seq[BlockContext]
+ stack.add(rootbox.bctx)
+ var x = 0
+ var y = 0
while stack.len > 0:
- let box = stack.pop()
- if box of CSSBlockBox:
- for line in box.icontext.rows:
- result.setRowBox(line)
-
- var i = box.children.len - 1
- while i >= 0:
- stack.add(box.children[i])
- dec i
+ let ctx = stack.pop()
+ if ctx.nested.len == 0:
+ for ictx in ctx.inlines:
+ result.renderInlineContext(ictx, x, y)
+ y += ictx.height
+ else:
+ for i in countdown(ctx.nested.high, 0):
+ stack.add(ctx.nested[i])
+ if result.len == 0:
+ result.addLine()