Skip to content

Commit

Permalink
Change the handling of out-of-bounds canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
EpicDima committed May 29, 2024
1 parent cdc86fe commit 3be531a
Show file tree
Hide file tree
Showing 2 changed files with 336 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ internal interface TextCanvas {
operator fun get(row: Int, column: Int): TextPixel
}

private val blankPixel = TextPixel(' ')

internal class TextSurface(
override val width: Int,
override val height: Int,
Expand All @@ -33,16 +31,23 @@ internal class TextSurface(
override var translationX = 0
override var translationY = 0

private val rows = Array(height) { Array(width) { TextPixel(' ') } }
private val rows = Array(height) { Array(width) { newBlankPixel } }

override operator fun get(row: Int, column: Int) = rows[translationY + row][translationX + column]
override operator fun get(row: Int, column: Int): TextPixel {
val x = translationX + column
val y = translationY + row
if (x >= width || y >= height || x < 0 || y < 0) {
return reusableDirtyPixel
}
return rows[y][x]
}

fun appendRowTo(appendable: Appendable, row: Int) {
// Reused heap allocation for building ANSI attributes inside the loop.
val attributes = mutableListOf<Int>()

val rowPixels = rows[row]
var lastPixel = blankPixel
var lastPixel = reusableBlankPixel
for (columnIndex in 0 until width) {
val pixel = rowPixels[columnIndex]

Expand Down Expand Up @@ -153,6 +158,23 @@ internal class TextSurface(
}
}

/**
* Returns always a new blank [TextPixel].
*/
private val newBlankPixel: TextPixel get() = TextPixel(' ')

/**
* It is used in places where it is important that the [TextPixel]
* has its original state and **will not change**.
*/
private val reusableBlankPixel: TextPixel = newBlankPixel

/**
* It is used in places where the [TextPixel] state is not important
* and it can change.
*/
private val reusableDirtyPixel: TextPixel = newBlankPixel

internal class TextPixel(var codePoint: Int) {
var background: Color = Color.Unspecified
var foreground: Color = Color.Unspecified
Expand Down

0 comments on commit 3be531a

Please sign in to comment.