about summary refs log tree commit diff stats
path: root/src/render/renderdocument.nim
blob: 9d5e0dcd02bbf42968ca24c14277b8f352f91b54 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import strutils
import unicode

import css/cascade
import css/sheet
import css/values
import html/dom
import io/cell
import io/term
import layout/box
import layout/engine
import utils/twtstr

func formatFromLine(line: RowBox): Formatting =
  result.fgcolor = line.color.cellColor()
  if line.fontstyle in { FONT_STYLE_ITALIC, FONT_STYLE_OBLIQUE }:
    result.italic = true
  if line.fontweight > 500:
    result.bold = true
  if line.textdecoration == TEXT_DECORATION_UNDERLINE:
    result.underline = true
  if line.textdecoration == TEXT_DECORATION_OVERLINE:
    result.overline = true
  if line.textdecoration == TEXT_DECORATION_LINE_THROUGH:
    result.strike = true

proc setRowBox(lines: var FlexibleGrid, line: RowBox) =
  var r: Rune

  var x = line.x
  var i = 0
  while x < 0:
    fastRuneAt(line.str, i, r)
    x += r.width()
  let linestr = line.str.substr(i)
  i = 0

  let y = line.y

  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, line.formatFromLine(), line.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)

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 + row.height
    for atom in row.atoms:
      # This aligns atoms with the baseline.
      # (other alignment types in progress)
      let y = y - atom.height
      if atom of BlockContext:
        let ctx = BlockContext(atom)
        grid.renderBlockContext(ctx, x + ctx.relx, y + ctx.rely)
      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.inline != nil:
    assert ctx.nested.len == 0
    grid.renderInlineContext(ctx.inline, x + ctx.inline.relx, y)
  else:
    for ctx in ctx.nested:
      grid.renderBlockContext(ctx, x + ctx.relx, y + ctx.rely)

const css = staticRead"res/ua.css"
let uastyle = css.parseStylesheet()
proc renderDocument*(document: Document, attrs: TermAttributes, userstyle: CSSStylesheet, layout: var Viewport): FlexibleGrid =
  document.applyStylesheets(uastyle, userstyle)
  layout.renderLayout(document)
  result.setLen(0)
  result.renderBlockContext(layout.root.bctx, 0, 0)
  if result.len == 0:
    result.addLine()