#!/bin/sh
#3x3 v0.10 ==============================
#Display block text using NiLuJe's fbink using a 3x3 font
# ʇɟǝןʎdoƆ (ↄ) 2019-04-25 PoP under Creative Commons Attribution-ShareAlike 3.0 Unported License
#Usage:
#3x3 [ALT] [-x NUM] -[y NUM] [-S NUM] [other] [-T "TEXT"] where
#           -x is the starting column number (fbink -x)
#           -y is the starting row number (fbink -y)
#           -S is the font scaling factor (fbink -S)
#             for example S1 8x8 is upscaled to 24x24
#           -T "TEXT" (a subset of ASCII) to print in 3x3 style using the single atomic █ character U+2588
#                  0123456789ABCDEFGHIJKLMNOPQRTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
#                  are supported, lower case is rendered as upper case, out of range bytes are skipped
#           ALT to alternate shading making text more... readable.
#           other parameters to pass to fbink
#dependency: fbink 1.15.0 at time of writing

x=0;y=0;S=0;T="";other="";ALT=0;COLOR="BLACK"

while [ "$1" != '' ]; do #parse parameters, no validation!
  case "$1" in
        -x) shift;X=$1;shift;;
        -y) shift;Y=$1;shift;;
        -S) shift;S=$1;shift;;
        ALT) ALT=1;shift;;
        -T) shift;T="$1";shift;;
         *) other="$other $1";shift;;
  esac
done

#keep the Kindle quiet
lipc-set-prop com.lab126.powerd preventScreenSaver 1
killall -stop cvm # pause framework

#keep the    Scaling factor within good limits
MC=1;MR=1 #to iterate at least once
while [ $MC -lt 3 ] || [ $MR -lt 3 ]; do

#Using the query parameter, parse fbink 1.15.0 info to get the maximum number of Rows and Columns
#viewWidth=1072;viewHeight=1448;screenWidth=1072;screenHeight=1448;viewHoriOrigin=0;viewVertOrigin=4;viewVertOffset=4;DPI=300;BPP=8;FONTW=24;FONTH=24;FONTSIZE_MULT=3;FONTNAME='IBM';glyphWidth=8;glyphHeight=8;MAXCOLS=44;MAXROWS=60;isPerfectFit=0;FBID=mxc_epdc_fb;USER_HZ=100;penFGColor=0;penBGColor=255;deviceName=PaperWhite 3;deviceId=513;isKoboNonMT=0;ntxBootRota=0;ntxRotaQuirk=0;canRotate=0
i="$(fbink -S $S -qe)"
MC=$(echo $i  | sed 's/.*MAXCOLS=\(.*\)*/\1/')
MC=${MC%;MAXROWS=*}  #max columns
MR=$(echo $i  | sed 's/.*MAXROWS=\(.*\)/\1/')
MR=${MR%;isPerfectFit=*}  #max rows

  #Clamp the    Scaling factor to allow a minimum of 1x1 in the new 4x4    Style
  if [ $MC -lt 3 ] || [ $MR -lt 3 ]; then S=$((S-1)); fi
done

MC=$((($MC/3)*3)) #rezize to 3x3 atoms with no column fraction
MR=$((($MR/3)*3)) #rezize to 3x3 atoms with no row fraction

C=$X; R=$Y
i=0
while :; do
  i=$((i+1))
  if [ $i -gt ${#T} ]
  then 
    killall -cont cvm;
    lipc-set-prop com.lab126.powerd preventScreenSaver 0
    exit
  fi

U=$(expr substr "$T" $i 1)  #next byte of T (Warning: letters,digits,ASCII subset only. Other Unicodes might be several bytes)
                          
case $U in

' ')
    S1="   "
    S2="   "
    S3="   "
  ;;
'0')
    S1="███"
    S2="█ █"
    S3="███"
  ;;
'1')
    S1="██ "
    S2=" █ "
    S3="███"
  ;;
'2')
    S1="██ "
    S2=" █ "
    S3=" ██"
  ;;
'3')
    S1="██ "
    S2=" ██"
    S3="██ "
  ;;
'4')
    S1="█ █"
    S2="███"
    S3="  █"
  ;;
'5')
    S1=" ██"
    S2=" █ "
    S3="██ "
  ;;
'6')
    S1="█  "
    S2="███"
    S3="███"
  ;;
'7')
    S1="███"
    S2="  █"
    S3="  █"
  ;;
'8')
    S1="███"
    S2="███"
    S3="███"
  ;;
'9')
    S1="███"
    S2="███"
    S3="  █"
  ;;
'a' | 'A')
    S1=" █ "
    S2="███"
    S3="█ █"
  ;;
'b' | 'B')
    S1="██ "
    S2="███"
    S3="██ "
  ;;
'c' | 'C')
    S1=" ██"
    S2="█  "
    S3=" ██"
  ;;
'd' | 'D')
    S1="██ "
    S2="█ █"
    S3="██ "
  ;;
'e' | 'E')
    S1="███"
    S2="██ "
    S3="███"
  ;;
'f' | 'F')
    S1="███"
    S2="██ "
    S3="█  "
  ;;
'g' | 'G')
    S1="██"
    S2="█ █"
    S3="███"
  ;;
'h' | 'H')
    S1="█ █"
    S2="███"
    S3="█ █"
  ;;
'i' | 'I')
    S1="███"
    S2=" █ "
    S3="███"
  ;;
'j' | 'J')
    S1="  █"
    S2="  █"
    S3="██ "
  ;;
'k' | 'K')
    S1="█ █"
    S2="██ "
    S3="█ █"
  ;;
'l' | 'L')
    S1="█  "
    S2="█  "
    S3="███"
  ;;
'm' | 'M')
    S1="███"
    S2="███"
    S3="█ █"
  ;;
'n' | 'N')
    S1="██ "
    S2="█ █"
    S3="█ █"
  ;;
'o' | 'O')
    S1="███"
    S2="█ █"
    S3="███"
  ;;
'p' | 'P')
    S1="███"
    S2="███"
    S3="█  "
  ;;
'q' | 'Q')
    S1="███"
    S2="█ █"
    S3="██ "
  ;;
'r' | 'R')
    S1="██ "
    S2="███"
    S3="█ █"
  ;;
's' | 'S')
    S1=" ██"
    S2=" █ "
    S3="██ "
  ;;
't' | 'T')
    S1="███"
    S2=" █ "
    S3=" █ "
  ;;
'u' | 'U')
    S1="█ █"
    S2="█ █"
    S3="███"
  ;;
'v' | 'V')
    S1="█ █"
    S2="█ █"
    S3=" ██"
  ;;
'w' | 'W')
    S1="█ █"
    S2="███"
    S3="███"
  ;;
'x' | 'X')
    S1="█ █"
    S2=" █ "
    S3="█ █"
  ;;
'y' | 'Y')
    S1="█ █"
    S2=" █ "
    S3=" █ "
  ;;
'z' | 'Z')
    S1="██ "
    S2=" █ "
    S3=" ██"
  ;;
'.')
    S1="   "
    S2="   "
    S3="█  "
  ;;
',')
    S1="   "
    S2=" █ "
    S3="█  "
  ;;
';')
    S1=" █ "
    S2=" █ "
    S3="█  "
  ;;
':')
    S1=" █ "
    S2="   "
    S3=" █ "
  ;;
'(')
    S1=" █ "
    S2="█  "
    S3=" █ "
  ;;
')')
    S1=" █ "
    S2="  █"
    S3=" █ "
  ;;
*) # out of range
    S1=""
    S2=""
    S3=""
  ;;

esac

if [ "$S1" != "" ]
then #character is defined in the font, print it
  if [ $ALT -eq 1 ]; then COLOR="BLACK"; fi
  if [ $ALT -eq -1 ]; then COLOR="GRAY7"; fi
  ALT=$((ALT*-1))

  fbink -x $C -y $((R+0)) -S $S -C $COLOR $other -F unscii "$S1"
  fbink -x $C -y $((R+1)) -S $S -C $COLOR $other -F unscii "$S2"
  fbink -x $C -y $((R+2)) -S $S -C $COLOR $other -F unscii "$S3"

  C=$((C+3)); 
  if [ $C -ge $MC ] 
    then 
     C=$X; R=$((R+3))
     if [ $((($MC/2)*2)) -eq $MC ]; then ALT=$((ALT*-1)); fi
  fi

  if [ $R -ge $MR ]; then C=$X; R=$Y; fbink -c; fi #clear screen and wrap to initial position

fi

done