#!/bin/bash #NOTE, because bash arrays are just string delimited by space, we will not be #using space as our empty character but . rawbuff=() function load_raw () { while read -r line; do if [ "$line" = "END_RAW" ]; then return else rawbuff=("${rawbuff[@]}" "$line") fi done } function load_point() { read -r x read -r y read -r c line=${rawbuff[$y]} #construct a sed prompt that replaces the xth character prompt="s/^\(.\{$x\}\).\(.*\)$/\1$c\2/" rawbuff[$y]=$(echo "$line" | sed "$prompt") } function print_raw () { for line in "${rawbuff[@]}"; do echo "$line" done } function canvas() { read -r w read -r h ln="" i=0 while [ $i -lt $w ];do ln="$ln." i=$(($i + 1)) done j=0 while [ $j -lt $h ]; do rawbuff=("${rawbuff[@]}" "$ln") j=$(($j+1)) done } #prints a single braille at $1=x $2=y function print_sin_braille() { #NOTE, for optimization purposes, this only works if the buffer consits of 1s #and 0s #bit to braille # #0 3 #1 4 #2 5 #6 7 #reverse #(1,3)(0,3)(1,2)(1,1)(1,0)(0,2)(0,1)(0,0) b0=${rawbuff[((0+4*$2))]:((0+2*$1)):1} b1=${rawbuff[((1+4*$2))]:((0+2*$1)):1} b2=${rawbuff[((2+4*$2))]:((0+2*$1)):1} b3=${rawbuff[((0+4*$2))]:((1+2*$1)):1} b4=${rawbuff[((1+4*$2))]:((1+2*$1)):1} b5=${rawbuff[((2+4*$2))]:((1+2*$1)):1} b6=${rawbuff[((3+4*$2))]:((0+2*$1)):1} b7=${rawbuff[((3+4*$2))]:((1+2*$1)):1} #ors it with the utf-8 hex value of the empty braille character #the two spaces assume aht the encoding is utf-8, convoluted bitwise OR #and coversion between character and number stuff echo "obase=16;$(( 2#$b7${b6}00$b5$b4$b3$b2$b1$b0 | 0xE2A080 ))"|bc | xxd --reverse -ps } function print_braille() { #I do the lazy thing and just test the first line w=${#rawbuff[0]} h=${#rawbuff[@]} #conver to braille coords, we do not want to try to print incomplete #characters because they will break with our optimization, so rounding #down is good w=$((w/2)) h=$((h/4)) i=0 j=0 while [ $i -lt $h ]; do j=0 while [ $j -lt $w ]; do print_sin_braille $j $i j=$(($j+1)) done #just want a new line echo i=$(($i+1)) done } while read -r line; do if [ "$line" = "LOAD_RAW" ]; then load_raw elif [ "$line" = "LOAD_POINT" ]; then load_point elif [ "$line" = "PRINT_RAW" ]; then print_raw elif [ "$line" = "CANVAS" ]; then canvas elif [ "$line" = "PRINT_BRAILLE" ]; then print_braille elif [ "$line" = "END" ]; then exit fi done