@@ -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,23 @@ 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 ( ' ' ) } }
34
+ private val rows = Array (height) { Array (width) { newBlankPixel } }
37
35
38
- override operator fun get (row : Int , column : Int ) = rows[translationY + row][translationX + column]
36
+ override operator fun get (row : Int , column : Int ): TextPixel {
37
+ val x = translationX + column
38
+ val y = translationY + row
39
+ if (x >= width || y >= height || x < 0 || y < 0 ) {
40
+ return reusableDirtyPixel
41
+ }
42
+ return rows[y][x]
43
+ }
39
44
40
45
fun appendRowTo (appendable : Appendable , row : Int ) {
41
46
// Reused heap allocation for building ANSI attributes inside the loop.
42
47
val attributes = mutableListOf<Int >()
43
48
44
49
val rowPixels = rows[row]
45
- var lastPixel = blankPixel
50
+ var lastPixel = reusableBlankPixel
46
51
for (columnIndex in 0 until width) {
47
52
val pixel = rowPixels[columnIndex]
48
53
@@ -153,6 +158,23 @@ internal class TextSurface(
153
158
}
154
159
}
155
160
161
+ /* *
162
+ * Returns always a new blank [TextPixel].
163
+ */
164
+ private inline val newBlankPixel: TextPixel get() = TextPixel (' ' )
165
+
166
+ /* *
167
+ * It is used in places where it is important that the [TextPixel]
168
+ * has its original state and **will not change**.
169
+ */
170
+ private val reusableBlankPixel: TextPixel = newBlankPixel
171
+
172
+ /* *
173
+ * It is used in places where the [TextPixel] state is not important
174
+ * and it can change.
175
+ */
176
+ private val reusableDirtyPixel: TextPixel = newBlankPixel
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