@@ -18,6 +18,9 @@ var lineWidth = 0
18
18
var textRun = false
19
19
var words: seq [string ] = @ []
20
20
var level = 3
21
+ var scrollY = 0
22
+ var controlHeight = 500
23
+ var firstPass = true
21
24
22
25
proc parse* (rst:string ): PRstNode =
23
26
var opts = {roSupportSmilies}
@@ -42,6 +45,12 @@ proc splitLines(canvas: Canvas) : seq[string] =
42
45
lineWidth = textWidth
43
46
return lines
44
47
48
+ proc inView():bool =
49
+ let startY = scrollY
50
+ let endY = scrollY + controlHeight
51
+ # echo startY, ",", endY, ", ", scrollY
52
+ result = (y >= startY and y <= endY)
53
+
45
54
proc renderLeaf(canvas: Canvas) =
46
55
if x > pageWidth:
47
56
x = spaceWidth
@@ -50,8 +59,9 @@ proc renderLeaf(canvas: Canvas) =
50
59
var lines = splitLines(canvas)
51
60
for line in lines:
52
61
canvas.fontSize = size[level- 1 ]
53
- canvas.drawText(line, x, y)
54
- drawTextCalls += 1
62
+ if inView():
63
+ canvas.drawText(line, x, y)
64
+ drawTextCalls += 1
55
65
if len(lines) != 1 :
56
66
y += textHeight
57
67
# x = floor(textWidth.toFloat * wScale).toInt
@@ -91,6 +101,7 @@ proc renderUnknown(canvas: Canvas, node: PRstNode) =
91
101
lastUnknown = true
92
102
93
103
proc renderNode(canvas: Canvas, node: PRstNode) =
104
+ if firstPass and not inView(): return
94
105
if node == nil : return
95
106
# echo node.kind
96
107
var dummy = 0
@@ -116,25 +127,25 @@ proc renderNode(canvas: Canvas, node: PRstNode) =
116
127
for son in node.sons:
117
128
renderNode(canvas, son)
118
129
119
- proc render* (canvas: Canvas, root: PRstNode) =
130
+ proc render* (canvas: Canvas, root: PRstNode, scrollPos: int ) =
120
131
x = 0
121
132
y = 0
133
+ scrollY = scrollPos
122
134
var n = 0
123
135
textRun = false
124
136
words = newSeq[string ]()
125
137
level = 3
126
138
127
139
drawTextCalls = 0
128
- canvas.textColor = rgb(0 , 0 , 0 )
129
- canvas.fontSize = 20
130
- canvas.fontFamily = " Arial"
131
-
140
+
132
141
let time = cpuTime()
133
142
for son in root.sons:
134
143
n += 1
135
144
renderNode(canvas, son)
136
145
137
- if pageHeight != y:
146
+ if pageHeight < y:
138
147
pageHeight = y + 100
139
148
echo " render in " ,(cpuTime()- time)* 1000.0
140
149
echo " total drawText calls: " ,drawTextCalls
150
+ firstPass = false
151
+
0 commit comments