Skip to content

Commit 6d1073b

Browse files
committed
Changing logic beyond the positive boundaries of canvas
1 parent d7be213 commit 6d1073b

File tree

2 files changed

+313
-92
lines changed
  • mosaic-runtime/src

2 files changed

+313
-92
lines changed

mosaic-runtime/src/commonMain/kotlin/com/jakewharton/mosaic/canvas.kt

+4-31
Original file line numberDiff line numberDiff line change
@@ -20,51 +20,24 @@ internal interface TextCanvas {
2020
}
2121

2222
internal class TextSurface(
23-
initialWidth: Int,
24-
initialHeight: Int,
23+
override val width: Int,
24+
override val height: Int,
2525
) : TextCanvas {
26-
private var realWidth: Int = initialWidth
27-
private var realHeight: Int = initialHeight
28-
29-
override val width: Int get() = realWidth
30-
override val height: Int get() = realHeight
3126

3227
override var translationX = 0
3328
override var translationY = 0
3429

35-
private val rows = MutableList(height) { createBlankRow(width) }
30+
private val rows = Array(height) { Array(width) { newBlankPixel } }
3631

3732
override operator fun get(row: Int, column: Int): TextPixel {
3833
val x = translationX + column
3934
val y = translationY + row
40-
if (x < 0 || y < 0) {
35+
if (x >= width || y >= height || x < 0 || y < 0) {
4136
return reusableDirtyPixel
4237
}
43-
val widthDiff = x - width + 1
44-
if (widthDiff > 0) {
45-
if (widthDiff == 1) {
46-
rows.forEach { it.add(newBlankPixel) }
47-
} else {
48-
rows.forEach { it.addAll(createBlankRow(widthDiff)) }
49-
}
50-
realWidth += widthDiff
51-
}
52-
val heightDiff = y - height + 1
53-
if (heightDiff > 0) {
54-
if (heightDiff == 1) {
55-
rows.add(createBlankRow(width))
56-
} else {
57-
rows.addAll(MutableList(heightDiff) { createBlankRow(width) })
58-
}
59-
realHeight += heightDiff
60-
}
6138
return rows[y][x]
6239
}
6340

64-
private inline fun createBlankRow(size: Int): MutableList<TextPixel> {
65-
return MutableList(size) { newBlankPixel }
66-
}
67-
6841
fun appendRowTo(appendable: Appendable, row: Int) {
6942
// Reused heap allocation for building ANSI attributes inside the loop.
7043
val attributes = mutableListOf<Int>()

0 commit comments

Comments
 (0)