@@ -23,8 +23,6 @@ internal interface TextCanvas {
23
23
operator fun get (row : Int , column : Int ): TextPixel
24
24
}
25
25
26
- private val blankPixel = TextPixel (' ' )
27
-
28
26
internal class TextSurface (
29
27
override val width : Int ,
30
28
override val height : Int ,
@@ -33,16 +31,35 @@ internal class TextSurface(
33
31
override var translationX = 0
34
32
override var translationY = 0
35
33
36
- private val rows = Array (height) { Array (width) { TextPixel (' ' ) } }
37
-
38
- override operator fun get (row : Int , column : Int ) = rows[translationY + row][translationX + column]
34
+ /* *
35
+ * It is used in places where the [TextPixel] state is not important
36
+ * and it can change.
37
+ */
38
+ private val reusableDirtyPixel: TextPixel = newBlankPixel
39
+
40
+ /* *
41
+ * It is used in places where it is important that the [TextPixel]
42
+ * has its original state and **will not change**.
43
+ */
44
+ private val reusableBlankPixel: TextPixel = newBlankPixel
45
+
46
+ private val rows = Array (height) { Array (width) { newBlankPixel } }
47
+
48
+ override operator fun get (row : Int , column : Int ): TextPixel {
49
+ val x = translationX + column
50
+ val y = translationY + row
51
+ if (x >= width || y >= height || x < 0 || y < 0 ) {
52
+ return reusableDirtyPixel
53
+ }
54
+ return rows[y][x]
55
+ }
39
56
40
57
fun appendRowTo (appendable : Appendable , row : Int ) {
41
58
// Reused heap allocation for building ANSI attributes inside the loop.
42
59
val attributes = mutableListOf<Int >()
43
60
44
61
val rowPixels = rows[row]
45
- var lastPixel = blankPixel
62
+ var lastPixel = reusableBlankPixel
46
63
for (columnIndex in 0 until width) {
47
64
val pixel = rowPixels[columnIndex]
48
65
@@ -153,6 +170,11 @@ internal class TextSurface(
153
170
}
154
171
}
155
172
173
+ /* *
174
+ * Returns always a new blank [TextPixel].
175
+ */
176
+ private inline val newBlankPixel: TextPixel get() = TextPixel (' ' )
177
+
156
178
internal class TextPixel (var codePoint : Int ) {
157
179
var background: Color = Color .Unspecified
158
180
var foreground: Color = Color .Unspecified
0 commit comments