about summary refs log tree commit diff stats
path: root/src/render/renderdocument.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/render/renderdocument.nim')
-rw-r--r--src/render/renderdocument.nim47
1 files changed, 12 insertions, 35 deletions
diff --git a/src/render/renderdocument.nim b/src/render/renderdocument.nim
index dbdb2a6e..319d0f08 100644
--- a/src/render/renderdocument.nim
+++ b/src/render/renderdocument.nim
@@ -1,12 +1,9 @@
 import strutils
 import unicode
 
-import css/cascade
-import css/sheet
 import css/stylednode
 import css/values
 import display/winattrs
-import html/dom
 import layout/box
 import layout/engine
 import layout/layoutunit
@@ -14,8 +11,6 @@ import types/cell
 import types/color
 import utils/twtstr
 
-import chame/tags
-
 func formatFromWord(computed: ComputedFormat): Format =
   result.fgcolor = computed.color.cellColor()
   if computed.bgcolor.a != 0:
@@ -39,8 +34,8 @@ proc setText(lines: var FlexibleGrid, linestr: string, cformat: ComputedFormat,
   var i = 0
   var r: Rune
   # make sure we have line y
-  while lines.len <= y:
-    lines.addLine()
+  if lines.high < y:
+    lines.addLines(y - lines.high)
 
   var cx = 0 # first x of new string (before padding)
   while cx < x and i < lines[y].str.len:
@@ -230,8 +225,8 @@ proc paintBackground(lines: var FlexibleGrid, color: RGBAColor, startx,
   if startx == endx: return # width is 0, no need to paint
 
   # make sure we have line y
-  while lines.len <= endy:
-    lines.addLine()
+  if lines.high < endy:
+    lines.addLines(endy - lines.high)
 
   for y in starty..<endy:
     # Make sure line.width() >= endx
@@ -305,11 +300,9 @@ proc renderInlineContext(grid: var FlexibleGrid, ctx: InlineContext,
   for line in ctx.lines:
     let y0 = y + line.offsety
     let y = y0 - erry * i
-
-    let r = y div window.ppl
-    while grid.len <= r:
-      grid.addLine()
-
+    let r = (y div window.ppl).toInt()
+    if grid.high < r:
+      grid.addLines(r - grid.high)
     for atom in line.atoms:
       case atom.t
       of INLINE_BLOCK:
@@ -377,27 +370,11 @@ proc renderBlockBox(grid: var FlexibleGrid, box: BlockBox, x, y: LayoutUnit,
       for i in countdown(box.nested.high, 0):
         stack.add((box.nested[i], x, y, posx, posy))
 
-const css = staticRead"res/ua.css"
-let uastyle = css.parseStylesheet()
-const quirk = css & staticRead"res/quirk.css"
-let quirkstyle = quirk.parseStylesheet()
-type RenderedDocument* = object
-  grid*: FlexibleGrid
-  styledRoot*: StyledNode
-  images*: seq[StyledNode]
-
-proc renderDocument*(document: Document, userstyle: CSSStylesheet,
-    layout: var Viewport, previousStyled: StyledNode): RenderedDocument =
+proc renderDocument*(styledRoot: StyledNode, viewport: Viewport,
+    attrs: WindowAttributes): FlexibleGrid =
   var grid: FlexibleGrid
-  var uastyle = uastyle
-  if document.mode == QUIRKS:
-    uastyle = quirkstyle
-  let styledRoot = document.applyStylesheets(uastyle, userstyle, previousStyled)
-  let rootBox = layout.renderLayout(styledRoot)
-  grid.renderBlockBox(rootBox, 0, 0, document.window.attrs)
+  let rootBox = viewport.renderLayout(styledRoot)
+  grid.renderBlockBox(rootBox, 0, 0, attrs)
   if grid.len == 0:
     grid.addLine()
-  return RenderedDocument(
-    grid: grid,
-    styledRoot: styledRoot
-  )
+  return grid
mitter bptato <nincsnevem662@gmail.com> 2021-08-10 15:19:32 +0200 Some refactoring' href='/ahoang/chawan/commit/src/io/cell.nim?id=a5f7da0428fdb74d9691cd6d589047478f422898'>a5f7da04 ^
a6bbcd0d ^
2e408503 ^
7de898bd ^

a6bbcd0d ^

cd28af13 ^
a6bbcd0d ^




bbd416f5 ^


876f5e9c ^
e7b53775 ^
a6bbcd0d ^


99965868 ^

a5f7da04 ^
a6bbcd0d ^


ad5adef7 ^
a6bbcd0d ^
a5f7da04 ^
7d6c75e4 ^














9c688a75 ^





051d22b1 ^
9c688a75 ^

876f5e9c ^


46ed9f5a ^
876f5e9c ^





051d22b1 ^
46ed9f5a ^
cd28af13 ^
876f5e9c ^
a6bbcd0d ^
cd28af13 ^
a6099512 ^
7d6c75e4 ^
a6099512 ^

bbd416f5 ^
a6099512 ^
60b58351 ^
ad5adef7 ^
60b58351 ^
876f5e9c ^

a6099512 ^
7de898bd ^
a6bbcd0d ^
bbd416f5 ^




8f4b4977 ^

876f5e9c ^
8f4b4977 ^
bbd416f5 ^
cd28af13 ^
bbd416f5 ^
cd28af13 ^
bbd416f5 ^
a6bbcd0d ^






876f5e9c ^
8f4b4977 ^
cd28af13 ^



bbd416f5 ^
a6bbcd0d ^






d3d9139d ^
bbd416f5 ^

eda26331 ^

647089a9 ^
eda26331 ^

d3d9139d ^
a6099512 ^




876f5e9c ^
a6099512 ^


876f5e9c ^
a6099512 ^







876f5e9c ^



051d22b1 ^
876f5e9c ^



051d22b1 ^
876f5e9c ^

940325ab ^
a6099512 ^





e46f0a4c ^
a6099512 ^
e46f0a4c ^
a6099512 ^
e46f0a4c ^
940325ab ^
a6099512 ^






876f5e9c ^
a6099512 ^
940325ab ^
a6099512 ^





e46f0a4c ^
a6099512 ^
e46f0a4c ^
a6099512 ^
e46f0a4c ^
940325ab ^
a6099512 ^





876f5e9c ^


a6099512 ^




876f5e9c ^
647089a9 ^
















2b1dac71 ^
647089a9 ^







876f5e9c ^
647089a9 ^
a6099512 ^
c141a3ce ^
3a12afa7 ^











































876f5e9c ^
647089a9 ^

























876f5e9c ^
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
               

               
            
 
                     
                  
                   

    
                     
             
               
                  
                

                 
              
 
                  

                       
                            
 

                                                                                

                      
             




                            


                        
                             
 


                                   

                                   
 


                                               
                
                   
 














                                                                                           





                          
                      

 


                                                                      
 





                                                                                   
                                                                           
 
                                              
                                 
                  
 
                                                   
                                                                        

                                      
                         
 
                                   
                         
 

                                                             
 
                                              
                                                                         




                                 

          
                                                            
                                   
             
                            
       
                   
 






                                                                        
                                                                
                               



                            
 






                                                                            
                                       

                          

                                                                                                 
 

                                                                                           
 




                            
                                                                      


                       
                          







                                                



                                       
                                   



                                      
                                     

                                       
                                                               





                                   
                                
                        
                                
                        
                                
                                                           






                        
                                         
                    
                                                     





                                   
                                
                        
                                
                        
                                
                                                           





                        


                                              




                                
                                                                    
















                                                    
             







                                                    
                                        
 
          
 











































                                                                                    
                                                         

























                                          
                                        
import sequtils
import streams
import strutils
import sugar

import css/stylednode
import types/color
import utils/twtstr

type
  FormatFlags* = enum
    FLAG_BOLD
    FLAG_ITALIC
    FLAG_UNDERLINE
    FLAG_REVERSE
    FLAG_STRIKE
    FLAG_OVERLINE
    FLAG_BLINK

  Format* = object
    fgcolor*: CellColor
    bgcolor*: CellColor
    flags*: set[FormatFlags]

  # 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

  SimpleFormatCell* = object
    format*: Format
    pos*: int

  FlexibleLine* = object
    str*: string
    formats*: seq[FormatCell]

  SimpleFlexibleLine* = object
    str*: string
    formats*: seq[SimpleFormatCell]

  FlexibleGrid* = seq[FlexibleLine]

  SimpleFlexibleGrid* = seq[SimpleFlexibleLine]

  FixedCell* = object
    str*: string
    format*: Format

  FixedGrid* = object
    width*, height*: int
    cells*: seq[FixedCell]

proc `[]=`*(grid: var FixedGrid, i: int, cell: FixedCell) = grid.cells[i] = cell
proc `[]=`*(grid: var FixedGrid, i: BackwardsIndex, cell: FixedCell) = grid.cells[i] = cell
proc `[]`*(grid: var FixedGrid, i: int): var FixedCell = grid.cells[i]
proc `[]`*(grid: var FixedGrid, i: BackwardsIndex): var FixedCell = grid.cells[i]
proc `[]`*(grid: FixedGrid, i: int): FixedCell = grid.cells[i]
proc `[]`*(grid: FixedGrid, i: BackwardsIndex): FixedCell = grid.cells[i]
iterator items*(grid: FixedGrid): FixedCell {.inline.} =
  for cell in grid.cells: yield cell
proc len*(grid: FixedGrid): int = grid.cells.len

const FormatCodes*: array[FormatFlags, tuple[s: int, e: int]] = [
  FLAG_BOLD: (1, 22),
  FLAG_ITALIC: (3, 23),
  FLAG_UNDERLINE: (4, 24),
  FLAG_REVERSE: (7, 27),
  FLAG_STRIKE: (9, 29),
  FLAG_OVERLINE: (53, 55),
  FLAG_BLINK: (5, 25),
]

template flag_template(format: Format, val: bool, flag: FormatFlags) =
  if val: format.flags.incl(flag)
  else: format.flags.excl(flag)

template `italic=`*(f: var Format, b: bool) = flag_template f, b, FLAG_ITALIC
template `bold=`*(f: var Format, b: bool) = flag_template f, b, FLAG_BOLD
template `underline=`*(f: var Format, b: bool) = flag_template f, b, FLAG_UNDERLINE
template `reverse=`*(f: var Format, b: bool) = flag_template f, b, FLAG_REVERSE
template `strike=`*(f: var Format, b: bool) = flag_template f, b, FLAG_STRIKE
template `overline=`*(f: var Format, b: bool) = flag_template f, b, FLAG_OVERLINE
template `blink=`*(f: var Format, b: bool) = flag_template f, b, FLAG_BLINK

func `==`*(a: FixedCell, b: FixedCell): bool =
  return a.format == b.format and
    a.str == b.str

func newFixedGrid*(w: int, h: int = 1): FixedGrid =
  return FixedGrid(width: w, height: h, cells: newSeq[FixedCell](w * h))

func width*(line: FlexibleLine): int =
  return line.str.width()

func width*(cell: FixedCell): int =
  return cell.str.width()

func newFormat*(): Format =
  return Format(fgcolor: defaultColor, bgcolor: defaultColor)

# Get the first format cell after pos, if any.
func findFormatN*(line: FlexibleLine|SimpleFlexibleLine, 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 findFormat*(line: SimpleFlexibleLine, pos: int): SimpleFormatCell =
  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

func findNextFormat*(line: SimpleFlexibleLine, pos: int): SimpleFormatCell =
  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 insertFormat*(line: var FlexibleLine, pos, i: int, format: Format, node: StyledNode = nil) =
  line.formats.insert(FormatCell(format: format, node: node, pos: pos), i)

proc addFormat*(line: var FlexibleLine, pos: int, format: Format, node: StyledNode = nil) =
  line.formats.add(FormatCell(format: format, node: node, pos: pos))

template inc_check(i: int) =
  inc i
  if i >= buf.len:
    return i

proc handleAnsiCode(format: var Format, final: char, params: string) =
  case final
  of 'm':
    if params.len == 0:
      format = newFormat()
    else:
      let sparams = params.split(';')
      try:
        let ip = sparams.map((x) => parseInt(x))
        var pi = 0
        while pi < ip.len:
          case ip[pi]
          of 0:
            format = newFormat()
          of 1: format.bold = true
          of 3: format.italic = true
          of 4: format.underline = true
          of 5: format.blink = true
          of 7: format.reverse = true
          of 9: format.strike = true
          of 22: format.bold = false
          of 23: format.italic = false
          of 25: format.blink = false
          of 27: format.reverse = false
          of 29: format.strike = false
          of 30..37: format.fgcolor = uint8(ip[pi]).cellColor()
          of 38:
            inc pi
            if pi < ip.len:
              if ip[pi] == 2:
                inc pi
                if pi + 2 < ip.len:
                  let r = ip[pi]
                  inc pi
                  let g = ip[pi]
                  inc pi
                  let b = ip[pi]
                  format.fgcolor = rgb(r, g, b).cellColor()
              else:
                #TODO
                inc pi
                continue
            else:
              break
          of 39:
            format.fgcolor = defaultColor
          of 40..47:
            format.bgcolor = uint8(ip[0]).cellColor()
          of 48:
            inc pi
            if pi < ip.len:
              if ip[pi] == 2:
                inc pi
                if pi + 2 < ip.len:
                  let r = ip[pi]
                  inc pi
                  let g = ip[pi]
                  inc pi
                  let b = ip[pi]
                  format.bgcolor = rgb(r, g, b).cellColor()
              else:
                #TODO
                inc pi
                continue
            else:
              break
          of 49: format.bgcolor = defaultColor
          of 53: format.overline = true
          of 55: format.overline = false
          else: discard
          inc pi
      except ValueError: discard
  else: discard

proc parseAnsiCode*(format: var Format, buf: string, fi: int): int =
  var i = fi
  if buf[i] != '\e':
    return i

  inc_check i
  if 0x40 <= int(buf[i]) and int(buf[i]) <= 0x5F:
    if buf[i] != '[':
      #C1, TODO?
      return
    inc_check i

  let sp = i
  #parameter bytes
  while 0x30 <= int(buf[i]) and int(buf[i]) <= 0x3F:
    inc_check i
  let params = buf.substr(sp, i - 1)

  #let si = i
  #intermediate bytes
  while 0x20 <= int(buf[i]) and int(buf[i]) <= 0x2F:
    inc_check i
  #let interm = buf.substr(si, i)

  let final = buf[i]
  #final byte
  if 0x40 <= int(buf[i]) and int(buf[i]) <= 0x7E:
    format.handleAnsiCode(final, params)

  return i

type
  AnsiCodeParseState* = enum
    PARSE_START, PARSE_PARAMS, PARSE_INTERM, PARSE_FINAL, PARSE_DONE

  AnsiCodeParser* = object
    state*: AnsiCodeParseState
    params: string

proc reset*(parser: var AnsiCodeParser) =
  parser.state = PARSE_START
  parser.params = ""

proc parseAnsiCode*(parser: var AnsiCodeParser, format: var Format, c: char): bool =
  case parser.state
  of PARSE_START:
    if 0x40 <= int(c) and int(c) <= 0x5F:
      if c != '[':
        #C1, TODO?
        parser.state = PARSE_DONE
      else:
        parser.state = PARSE_PARAMS
    else:
      parser.state = PARSE_DONE
      return true
  of PARSE_PARAMS:
    if 0x30 <= int(c) and int(c) <= 0x3F:
      parser.params &= c
    else:
      parser.state = PARSE_INTERM
      return parser.parseAnsiCode(format, c)
  of PARSE_INTERM:
    if 0x20 <= int(c) and int(c) <= 0x2F:
      discard
    else:
      parser.state = PARSE_FINAL
      return parser.parseAnsiCode(format, c)
  of PARSE_FINAL:
    parser.state = PARSE_DONE
    if 0x40 <= int(c) and int(c) <= 0x7E:
      format.handleAnsiCode(c, parser.params)
    else:
      return true
  of PARSE_DONE: discard

proc parseAnsiCode*(format: var Format, stream: Stream) =
  if stream.atEnd(): return
  var c = stream.readChar()
  if 0x40 <= int(c) and int(c) <= 0x5F:
    if c != '[':
      #C1, TODO?
      return
    if stream.atEnd(): return
    c = stream.readChar()

  var params = $c
  #parameter bytes
  while 0x30 <= int(c) and int(c) <= 0x3F:
    params &= c
    if stream.atEnd(): return
    c = stream.readChar()

  #intermediate bytes
  #var interm = $c
  while 0x20 <= int(c) and int(c) <= 0x2F:
    #interm &= c
    if stream.atEnd(): return
    c = stream.readChar()

  #final byte
  if 0x40 <= int(c) and int(c) <= 0x7E:
    let final = c
    format.handleAnsiCode(final, params)