@@ -20,8 +20,6 @@ internal interface TextCanvas {
20
20
operator fun get (row : Int , column : Int ): TextPixel
21
21
}
22
22
23
- private val blankPixel = TextPixel (' ' )
24
-
25
23
internal class TextSurface (
26
24
override val width : Int ,
27
25
override val height : Int ,
@@ -30,16 +28,23 @@ internal class TextSurface(
30
28
override var translationX = 0
31
29
override var translationY = 0
32
30
33
- private val rows = Array (height) { Array (width) { TextPixel ( ' ' ) } }
31
+ private val rows = Array (height) { Array (width) { newBlankPixel } }
34
32
35
- override operator fun get (row : Int , column : Int ) = rows[translationY + row][translationX + column]
33
+ override operator fun get (row : Int , column : Int ): TextPixel {
34
+ val x = translationX + column
35
+ val y = translationY + row
36
+ if (x >= width || y >= height || x < 0 || y < 0 ) {
37
+ return reusableDirtyPixel
38
+ }
39
+ return rows[y][x]
40
+ }
36
41
37
42
fun appendRowTo (appendable : Appendable , row : Int ) {
38
43
// Reused heap allocation for building ANSI attributes inside the loop.
39
44
val attributes = mutableListOf<Int >()
40
45
41
46
val rowPixels = rows[row]
42
- var lastPixel = blankPixel
47
+ var lastPixel = reusableBlankPixel
43
48
for (columnIndex in 0 until width) {
44
49
val pixel = rowPixels[columnIndex]
45
50
@@ -150,6 +155,23 @@ internal class TextSurface(
150
155
}
151
156
}
152
157
158
+ /* *
159
+ * Returns always a new blank [TextPixel].
160
+ */
161
+ private val newBlankPixel: TextPixel get() = TextPixel (' ' )
162
+
163
+ /* *
164
+ * It is used in places where it is important that the [TextPixel]
165
+ * has its original state and **will not change**.
166
+ */
167
+ private val reusableBlankPixel: TextPixel = newBlankPixel
168
+
169
+ /* *
170
+ * It is used in places where the [TextPixel] state is not important
171
+ * and it can change.
172
+ */
173
+ private val reusableDirtyPixel: TextPixel = newBlankPixel
174
+
153
175
internal class TextPixel (var value : String ) {
154
176
var background: Color ? = null
155
177
var foreground: Color ? = null
0 commit comments