#!/bin/sh
#piczat v0.10 ==="picture that"===========================
#Display text with transparent pictures of ASCII characters using NiLuJe's fbink
# ʇɟǝןʎdoƆ (ↄ) 2018-09-27 PoP under Creative Commons Attribution-ShareAlike 3.0 Unported License
# 2018-09-27 v0.10 initial release
# 2018-09-27 v0.20 fix h&v alignment
# 2018-10-02 v0.30 only align when text fits screen width, support -m and -M alignment synonyms, supports X and Y pixel ofsets
# 2018-10-03 v0.40 ignore non ASCII
#
#Original pictures generated from http://www.picturetopeople.org/p2p/text_effects_generator.p2p/transparent_text_effect
#using "Pants Patrol" font, Font size 250, Margin 3, Text layout style Normal, Text Rotation 0, Text color 000000, Text transparency 0
#Some manual minor size adjustment with IrfanView
#Resized pictures generated from IrfanView batch, in various sizes

#Usage:
#   piczat [-x NUM] [X NUM] [-y NUM] [Y NUM] [x= NUM] [y= NUM] 
#          [halign= {NONE or LEFT | CENTER or MIDDLE | EDGE or RIGHT}]  
#          [valign= {NONE or TOP | CENTER or MIDDLE | EDGE or BOTTOM}]
#          [-S NUM] [other_fbink_options] -T "TEXT" where
#
#           -x is the starting column number
#           -X is the number of pixels offsetting -x
#           x= is the horizontal pixel coordinate, ofsetted by -X
#           -y is the starting row number
#           -Y is the number of pixels offsetting -y
#           y= is the vertical pixel coordinate, ofsetted by -Y
#           halign= align coordinates horizontally, overrides -x, ofsetted by x=
#           valign= align coordinates vertically, overrides -y, ofsetted by y=
#           -S =1 for size 16x16
#              =2 for size 32x32 (default)
#              =3 for size 48x48
#              =4 for size 64x64
#              =5 for size 80x80
#              =6 for size 96x96
#           -T TEXT to print, wrapping at column 0 row 0
#               !"#$%&'()*+,-./0123456789:;<=>@ABCDEFGHIJKLMNOPQRowsTUVWXYZ[\]^_abcdefghijklmnopqRowstuvwxyz`{|}~
#              ASCII only is supported, out of range unicodes are ignored.
#
#dependency: fbink (currently 1.7.0) 

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

#Using the query parameter, parse fbink 1.7.0.1.0 info to get the screen dimensions in pixels 
#viewwidth=600;viewheight=800;screenwidth=600;screenheight=800;viewHoriOrigin=0;viewVertOrigin=0;viewVertOffset=0;BPP=4;FONTW=16;FONTH=16;FONTSIZE_MULT=2;FONTNAME='IBM';glyphwidth=8;glyphheight=8;MAXCOLS=37;MAXROWS=50;isPerfectFit=0;FBID=eink_fb;USER_HZ=100;penFGColor=255;penBGColor=0
i="$(fbink -qe)"
height=$(echo $i  | sed 's/.*screenHeight=\(.*\)*/\1/')
height=${height%;viewHoriOrigin=*}  #screen height
width=$(echo $i  | sed 's/.*screenWidth=\(.*\)/\1/')
width=${width%;screenHeight=*}  #screen width
x=0;col=0;X=0;y=0;row=0;Y=0;S=2;halign="NONE";valign="NONE"
other='-q' #quiet fbink calls
fname="zebra" #will anybody ever create another one :-)

while [ "$1" != '' ]; do #parse parameters, no validation!
  case "$1" in
        -x) shift;col=$1;shift;;
        -X) shift;X=$1;shift;;
        x=) shift;x=$1;shift;;
        -y) shift;row=$1;shift;;
        -Y) shift;Y=$1;shift;;
        y=) shift;y=$1;shift;;
   halign=) shift;halign=$1;shift;;
        -m) shift;halign="CENTER";; #synonym of centered text
   valign=) shift;valign=$1;shift;;
        -M) shift;valign="CENTER";; #synonym of halfway text
        -S) shift;S=$1;shift;;
        -T) shift;T="$1";shift;;
        -c) fbink -c -q '';shift;;
         *) other="$other $1";shift;;
  esac
done


S=$((S*16)) #font size
x=$((x+col*S+X));y=$((y+row*S+Y)) #pixel +col/row offset +-X|Y offset
LT=${#T} #Length of the text
MC=$((width/S)) #not fbink MAXCOLUMNS
MR=$((height/S)) #not fbink MAXROWS
NUMROWS=$(((LT-1)/MC+1))

if [ $NUMROWS -eq 1 ] #align on request (dont attempt to align multiple rows)
then
  case "$halign" in
    NONE | LEFT) ;; #left justify
    CENTER | MIDDLE) x=$((width/2-S/2-LT/2*S+x));; #center horizontally
    EDGE | RIGHT) x=$((width-LT*S+x));; #right justify
  esac

  case "$valign" in
    NONE | TOP) ;;#place on first line
    CENTER | MIDDLE) y=$((height/2-S/2+y));; #center vertically
    EDGE | BOTTOM) y=$((height-S+y));; #place on last line
  esac
fi

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

  t=$(expr substr "$T" $i 1)  #next byte of T (= next ASCII)
 xt="$(printf  "%x" "'$t")" #hex of $t
 dt="$(printf  "%d" "'$t")" #dec of $t

  if [ $dt -le 126 ] && [ $dt -ge 32 ]
  then #print the picture of the glyph 
    if [ $(($x+$S)) -gt $width ]; then x=0; y=$((y+$S));fi
    if [ $(($y+$S)) -gt $height ]; then y=0; fi
    fbink -g file="./fonts/$fname/$S/"00"$xt".png,x=$x,y=$y $other
    x=$((x+S));
    halign="NONE";valign="NONE" #reset for subsequent characters
  fi
done