#!/bin/sh
#piczat2 v0.10 ==="picture that"===========================
#Display text with transparent pictures of ASCII characters using NiLuJe's fbink
# ʇɟǝןʎdoƆ (ↄ) 2019-04-21 PoP under Creative Commons Attribution-ShareAlike 3.0 Unported License
# 2019-04-21 v0.10 initial release (ported from piczat v5.0)
#
#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, 
#16x16 size only used by piczat2

#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] [w= NUM] [h= 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)
#              ...
#              =n for n*16x16
#            w= scale the font to that width, takes precedence over -S
#            h= scale the font to that height, takes precedence over -S
#           -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.15.0) using w= and h= image rezizing

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

#Using the query parameter, parse fbink 1.15.0 info to get the screen dimensions in pixels
#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 -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";w=0;h=0
other='-q' #quiet fbink calls
fname="PantsPatrol" #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;;
        w=) shift;w=$1;shift;;
        h=) shift;h=$1;shift;;
        -T) shift;T="$1";shift;;
        -c) fbink -c -q '';shift;;
         *) other="$other $1";shift;;
  esac
done

S=$((S*16)) #font size
if [ $w -eq 0 ] && [ $h -eq 0 ]; then w=$S; h=$S; fi #use S to scale when not specified
if [ $w -eq 0 ] && [ $h -ne 0 ]; then w=$S; fi  #only scale vertically, don't scale proportionally 
if [ $w -ne 0 ] && [ $h -eq 0 ]; then h=$S; fi #only scale horizontally, don't scale proportionally 
x=$((x+col*w+X));y=$((y+row*h+Y)) #pixel +col/row offset +-X|Y offset
LT=${#T} #Length of the text
MC=$((width/w)) #not fbink MAXCOLUMNS
MR=$((height/h)) #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-w/2-LT/2*w+x));; #center horizontally
    EDGE | RIGHT) x=$((width-LT*w+x));; #right justify
  esac

  case "$valign" in
    NONE | TOP) ;;#place on first line
    CENTER | MIDDLE) y=$((height/2-h/2+y));; #center vertically
    EDGE | BOTTOM) y=$((height-h+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  "%04x" "'$t")" #hex of character $t #"X	(a literal double-quote infront of a character): interpreted as number (underlying codeset) don't forget escaping
 dt="$(printf  "%d" "'$t")" #dec of character $t #'X	(a literal single-quote infront of a character): interpreted as number (underlying codeset) don't forget escaping

  if [ $dt -le 126 ] && [ $dt -ge 32 ]
  then #print the picture of the glyph 
    if [ $(($x+$w)) -gt $width ]; then x=0; y=$((y+$h));fi
    if [ $(($y+$h)) -gt $height ]; then y=0; fi

    fbink -g file="./fonts/$fname/$xt".png,x=$x,y=$y,w=$w,h=$h $other
    x=$((x+w));
    halign="NONE";valign="NONE" #reset for subsequent characters
  fi
done