about summary refs log tree commit diff stats
path: root/src/render/renderdocument.nim
blob: 46d8a0032ea989f1dad34b29a4c3c08c351ef47d (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import strutils
import unicode

import buffer/cell
import css/cascade
import css/sheet
import css/stylednode
import css/values
import html/dom
import io/window
import layout/box
import layout/engine
import types/color
import utils/twtstr

func formatFromWord(computed: ComputedFormat): Format =
  result.fgcolor = computed.color.cellColor()
  if computed.fontstyle in { FONT_STYLE_ITALIC, FONT_STYLE_OBLIQUE }:
    result.italic = true
  if computed.fontweight > 500:
    result.bold = true
  if TEXT_DECORATION_UNDERLINE in computed.textdecoration:
    result.underline = true
  if TEXT_DECORATION_OVERLINE in computed.textdecoration:
    result.overline = true
  if TEXT_DECORATION_LINE_THROUGH in computed.textdecoration:
    result.strike = true
  if TEXT_DECORATION_BLINK in computed.textdecoration:
    result.blink = true
  else: discard

proc setText(lines: var FlexibleGrid, linestr: string, cformat: ComputedFormat, x, y: int) {.inline.} =
  var i = 0
  var r: Rune
  # make sure we have line y
  while lines.len <= y:
    lines.addLine()

  var cx = 0 # first x of new string (before padding)
  while cx < x and i < lines[y].str.len:
    let pi = i
    fastRuneAt(lines[y].str, i, r)
    let w = r.twidth(cx)
    # we must ensure x is max(cx, x), otherwise our assumption of cx <= x
    # breaks down
    if cx + w > x:
      i = pi
      break
    cx += w

  let ostr = lines[y].str.substr(i)
  lines[y].str.setLen(i)
  let padwidth = x - cx
  if padwidth > 0:
    lines[y].str &= ' '.repeat(padwidth)

  lines[y].str &= linestr
  let linestrwidth = linestr.twidth(x) - x

  i = 0
  var nx = x # last x of new string
  while nx < x + linestrwidth and i < ostr.len:
    fastRuneAt(ostr, i, r)
    nx += r.twidth(nx)

  if i < ostr.len:
    lines[y].str &= ostr.substr(i)

  # Skip unchanged formats before the new string
  var fi = lines[y].findFormatN(cx) - 1

  if padwidth > 0:
    # Replace formats for padding
    var padformat = newFormat()
    if fi == -1:
      # No formats
      inc fi # insert after first format (meaning fi = 0)
      lines[y].insertFormat(cx, fi, padformat)
    else:
      # First format's pos may be == cx here.
      if lines[y].formats[fi].pos == cx:
        padformat.bgcolor = lines[y].formats[fi].format.bgcolor
        lines[y].formats.delete(fi)
        lines[y].insertFormat(cx, fi, padformat)
      else:
        # First format < cx => split it up
        assert lines[y].formats[fi].pos < cx
        padformat.bgcolor = lines[y].formats[fi].format.bgcolor
        inc fi # insert after first format
        lines[y].insertFormat(cx, fi, padformat)
    inc fi # skip last format
    while fi < lines[y].formats.len and lines[y].formats[fi].pos < x:
      # Other formats must be > cx => replace them
      padformat.bgcolor = lines[y].formats[fi].format.bgcolor
      let px = lines[y].formats[fi].pos
      lines[y].formats.delete(fi)
      lines[y].insertFormat(px, fi, padformat)
      inc fi
    dec fi # go back to previous format, so that pos <= x
    assert lines[y].formats[fi].pos <= x

  # Now for the text's formats:
  var format = cformat.formatFromWord()
  var lformat: Format
  if fi == -1:
    # No formats => just insert a new format at 0
    inc fi
    lines[y].insertFormat(x, fi, format, cformat)
    lformat = newFormat()
  else:
    # First format's pos may be == x here.
    lformat = lines[y].formats[fi].format # save for later use
    if lines[y].formats[fi].pos == x:
      # Replace.
      format.bgcolor = lines[y].formats[fi].format.bgcolor
      lines[y].formats.delete(fi)
      lines[y].insertFormat(x, fi, format, cformat)
    else:
      # First format's pos < x => split it up.
      assert lines[y].formats[fi].pos < x
      format.bgcolor = lines[y].formats[fi].format.bgcolor
      inc fi # insert after first format
      lines[y].insertFormat(x, fi, format, cformat)
  inc fi # skip last format

  while fi < lines[y].formats.len and lines[y].formats[fi].pos < nx:
    # Other formats must be > x => replace them
    format.bgcolor = lines[y].formats[fi].format.bgcolor
    let px = lines[y].formats[fi].pos
    lformat = lines[y].formats[fi].format # save for later use
    lines[y].formats.delete(fi)
    lines[y].insertFormat(px, fi, format, cformat)
    inc fi

  if i < ostr.len and (fi >= lines[y].formats.len or lines[y].formats[fi].pos > nx):
    # nx < ostr.width, but we have removed all formatting in the range of our
    # string, and no formatting comes directly after it. So we insert the
    # continuation of the last format we replaced after our string.
    # (Default format when we haven't replaced anything.)
    lines[y].insertFormat(nx, fi, lformat)

  dec fi # go back to previous format, so that pos <= nx
  assert lines[y].formats[fi].pos <= nx
  # That's it!

proc setRowWord(lines: var FlexibleGrid, word: InlineWord, x, y: int, window: WindowAttributes) =
  var r: Rune

  var y = (y + word.offset.y) div window.ppl # y cell
  if y < 0: return # y is outside the canvas, no need to draw

  var x = (x + word.offset.x) div window.ppc # x cell
  var i = 0
  while x < 0 and i < word.str.len:
    fastRuneAt(word.str, i, r)
    x += r.twidth(x)
  if x < 0: return # highest x is outside the canvas, no need to draw
  let linestr = word.str.substr(i)

  lines.setText(linestr, word.format, x, y)

proc setSpacing(lines: var FlexibleGrid, spacing: InlineSpacing, x, y: int, window: WindowAttributes) =
  var y = (y + spacing.offset.y) div window.ppl # y cell
  if y < 0: return # y is outside the canvas, no need to draw

  var x = (x + spacing.offset.x) div window.ppc # x cell
  let width = spacing.width div window.ppc # cell width

  if x + width < 0: return # highest x is outside the canvas, no need to draw
  var i = 0
  if x < 0:
    i -= x
    x = 0
  let linestr = ' '.repeat(width - i)

  lines.setText(linestr, spacing.format, x, y)

proc paintBackground(lines: var FlexibleGrid, color: RGBAColor, startx, starty, endx, endy: int, window: WindowAttributes) =
  let color = color.cellColor()

  var starty = starty div window.ppl
  var endy = endy div window.ppl

  if starty > endy:
    swap(starty, endy)

  if endy <= 0: return # highest y is outside canvas, no need to paint
  if starty < 0: starty = 0
  if starty == endy: return # height is 0, no need to paint

  var startx = startx div window.ppc

  var endx = endx div window.ppc
  if endy < 0: endy = 0

  if startx > endx:
    swap(startx, endx)

  if endx <= 0: return # highest x is outside the canvas, no need to paint
  if startx < 0: startx = 0
  if startx == endx: return # width is 0, no need to paint

  # make sure we have line y
  while lines.len <= endy:
    lines.addLine()

  for y in starty..<endy:
    # Make sure line.width() >= endx
    let linewidth = lines[y].width()
    if linewidth < endx:
      lines[y].addFormat(linewidth, newFormat())
      lines[y].str &= ' '.repeat(endx - linewidth)

    # Process formatting around startx
    if lines[y].formats.len == 0:
      # No formats
      lines[y].addFormat(startx, newFormat())
    else:
      let fi = lines[y].findFormatN(startx) - 1
      if fi == -1:
        # No format <= startx
        lines[y].insertFormat(startx, 0, newFormat())
      elif lines[y].formats[fi].pos == startx:
        # Last format equals startx => next comes after, nothing to be done
        discard
      else:
        # Last format lower than startx => separate format from startx
        let copy = lines[y].formats[fi]
        lines[y].formats[fi].pos = startx
        lines[y].formats.insert(copy, fi)

    # Process formatting around endx
    assert lines[y].formats.len > 0
    let fi = lines[y].findFormatN(endx) - 1
    if fi == -1:
      # Last format > endx -> nothing to be done
      discard
    else:
      if lines[y].formats[fi].pos != endx:
        let copy = lines[y].formats[fi]
        if linewidth != endx:
          lines[y].formats[fi].pos = endx
          lines[y].formats.insert(copy, fi)
        else:
          lines[y].formats.delete(fi)
          lines[y].formats.insert(copy, fi)

    # Paint format backgrounds between startx and endx
    for fi in 0..lines[y].formats.high:
      if lines[y].formats[fi].pos >= endx:
        break
      if lines[y].formats[fi].pos >= startx:
        lines[y].formats[fi].format.bgcolor = color

func calculateErrorY(ctx: InlineContext, window: WindowAttributes): int =
  if ctx.lines.len <= 1: return 0
  var error = 0
  for i in 0 ..< ctx.lines.len:
    if i < ctx.lines.high:
      let dy = ctx.lines[i + 1].offset.y - ctx.lines[i].offset.y
      error += dy - (dy div window.ppl) * window.ppl
  return error div (ctx.lines.len - 1)

proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockBox, x, y: int, window: WindowAttributes)

proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext, x, y: int, window: WindowAttributes) =
  let x = x + ctx.offset.x
  let y = y + ctx.offset.y
  let erry = ctx.calculateErrorY(window)
  var i = 0
  for line in ctx.lines:
    let x = x + line.offset.x
    let y0 = y + line.offset.y
    let y = y0 - erry * i

    let r = y div window.ppl
    while grid.len <= r:
      grid.addLine()

    for atom in line.atoms:
      if atom of InlineBlockBox:
        let iblock = InlineBlockBox(atom)
        grid.renderBlockContext(iblock.innerbox, x + iblock.offset.x, y + iblock.offset.y, window)
      elif atom of InlineWord:
        let word = InlineWord(atom)
        grid.setRowWord(word, x, y, window)
      elif atom of InlineSpacing:
        let spacing = InlineSpacing(atom)
        grid.setSpacing(spacing, x, y, window)
    inc i

proc renderBlockContext(grid: var FlexibleGrid, ctx: BlockBox, x, y: int, window: WindowAttributes) =
  var stack = newSeqOfCap[(BlockBox, int, int)](100)
  stack.add((ctx, x, y))

  while stack.len > 0:
    var (ctx, x, y) = stack.pop()
    x += ctx.offset.x
    y += ctx.offset.y

    if ctx.computed{"background-color"}.a != 0: #TODO color blending
      grid.paintBackground(ctx.computed{"background-color"}, x, y, x + ctx.width, y + ctx.height, window)
    if ctx.computed{"background-image"}.t == CONTENT_IMAGE:
      # ugly hack for background-image display... TODO actually implement images
      let s = ctx.computed{"background-image"}.s # [img]
      let w = s.len * window.ppc
      var x = x
      if ctx.width < w:
        # text is larger than image; center it to minimize error
        x -= w div 2
        x += ctx.width div 2
      x = x div window.ppc
      y = y div window.ppl
      grid.setText(s, ComputedFormat(node: ctx.node), x, y)

    if ctx of ListItemBox:
      let ctx = ListItemBox(ctx)
      if ctx.marker != nil:
        grid.renderInlineContext(ctx.marker, x - ctx.marker.width, y, window)

    if ctx.inline != nil:
      assert ctx.nested.len == 0
      grid.renderInlineContext(ctx.inline, x, y, window)
    else:
      for i in countdown(ctx.nested.high, 0):
        stack.add((ctx.nested[i], x, y))

const css = staticRead"res/ua.css"
let uastyle = css.parseStylesheet()
const quirk = css & staticRead"res/quirk.css"
let quirkstyle = quirk.parseStylesheet()
proc renderDocument*(document: Document, window: WindowAttributes, userstyle: CSSStylesheet, layout: var Viewport, previousStyled: StyledNode): (FlexibleGrid, StyledNode) =
  var uastyle = uastyle
  if document.mode == QUIRKS:
    uastyle = quirkstyle
  let styledNode = document.applyStylesheets(uastyle, userstyle, previousStyled)
  result[1] = styledNode
  layout.renderLayout(styledNode)
  result[0].setLen(0)
  for root in layout.root:
    result[0].renderBlockContext(root, 0, 0, window)
  if result[0].len == 0:
    result[0].addLine()