This commit is contained in:
Felipe Pena 2025-08-06 11:51:02 -03:00
parent 69623a2a22
commit ca2d020d0c

View file

@ -1,7 +1,9 @@
import arrays
pub fn Canvas.new(width int, height int) &Canvas {
return &Canvas { layers: create_buffer(width, height) }
return &Canvas{
layers: create_buffer(width, height)
}
}
pub fn (canvas Canvas) size() (int, int) {
@ -10,7 +12,7 @@ pub fn (canvas Canvas) size() (int, int) {
pub fn (canvas Canvas) string() string {
output := buffer_to_braille(canvas.layers, dots_to_braille_rune_map)
return arrays.join_to_string(output, "\n", |row| arrays.join_to_string(row, "", |character| "${character}"))
return arrays.join_to_string(output, '\n', |row| arrays.join_to_string(row, '', |character| '${character}'))
}
pub fn (mut canvas Canvas) set(x int, y int) {
@ -26,40 +28,14 @@ pub fn (mut canvas Canvas) clear() {
canvas.layers = create_buffer(width, height)
}
/* private code */
struct Canvas {
mut:
layers [][]bool
}
/**
* Maps each bit number in a dots byte (western reading order) to it's
* corresponding bit number in the unicode Braille mapping.
*/
const dots_to_braille = [u8(0), 3, 1, 4, 2, 5, 6, 7]
/**
* Maps each possible dots byte to an unicode rune.
*/
const dots_to_braille_rune_map = memoize_dots_to_braille_rune()
/**
* dots_to_braille_rune returns the Unicode Braille character for a given 8-dot
* mask. Dots are numbered in Western reading order (leftright, topbottom):
* 1 2
* 3 4
* 5 6
* 7 8
* The input bytes most significant bit represents dot 1, and the least
* significant bit dot 8.
* See: https://en.wikipedia.org/wiki/Braille_Patterns
*
* Parameters:
* dots uint8 - 8-bit mask where each bit corresponds to a Braille dot.
*
* Returns:
* rune - Unicode code point in the Braille block U+2800U+28FF.
*/
fn dots_to_braille_rune(dots u8) rune {
mut mask := 0
for index in 0 .. 8 {
@ -81,22 +57,6 @@ fn memoize_dots_to_braille_rune() [256]rune {
return memo
}
/**
* Converts a 2D boolean buffer into a 2D grid of Braille runes.
*
* Each group of 2x4 pixels from the boolean buffer is mapped into a single
* Braille character. The function iterates through the buffer, calculates which
* Braille dots should be active, and converts them into runes using
* `dots_to_braille_rune`.
*
* Parameters:
* buffer [][]bool - A 2D array of booleans representing pixels (true =
* filled, false = empty). Dimensions are expected to be
* multiples of 4 rows and 2 columns.
* Returns:
* [][]rune - A 2D array of runes where each rune corresponds to one Braille
* character.
*/
fn buffer_to_braille(buffer [][]bool, braille_mapping [256]rune) [][]rune {
out_rows := buffer.len / 4
out_cols := buffer[0].len / 2