summaryrefslogtreecommitdiff
path: root/bbrll
blob: 13fd5f7367b580f5f997da6729eeb8a0bd3fb7bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/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)
	#the braille empty char in decimal base
	num=14852224
	if [ "${rawbuff[((0+4*$2))]:((0+2*$1)):1}" == "1" ]; then
		num=$(($num + 1))
	fi
	if [ "${rawbuff[((1+4*$2))]:((0+2*$1)):1}" == "1" ];then
		num=$(($num + 2))
	fi
	if [ "${rawbuff[((2+4*$2))]:((0+2*$1)):1}" == "1" ];then
		num=$(($num + 4))
	fi
	if [ "${rawbuff[((0+4*$2))]:((1+2*$1)):1}" == "1" ];then
		num=$(($num + 8))
	fi
	if [ "${rawbuff[((1+4*$2))]:((1+2*$1)):1}" == "1" ];then
		num=$(($num + 16))
	fi
	if [ "${rawbuff[((2+4*$2))]:((1+2*$1)):1}" == "1" ];then
		num=$(($num + 32))
	fi
	if [ "${rawbuff[((3+4*$2))]:((0+2*$1)):1}" == "1" ];then
		num=$(($num + 256))
	fi
	if [ "${rawbuff[((3+4*$2))]:((1+2*$1)):1}" == "1" ];then
		num=$(($num + 512))
	fi
	#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
	printf %x $num | 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, since bash rounds down, account for when
	#there a braille character that will not be completely filled(it can
	#handle this fine because of how the sed pattern was constructed in
	#print_sin_braille)
	if [ $((w % 2)) -gt 0 ]; then
		w=$((w/2+1))
	else
		w=$((w/2))
	fi
	if [ $((h % 4)) -gt 0 ]; then
		h=$((h/4+1))
	else
		h=$((h/4))
	fi

	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