usage
text2xpm "your text here" >/tmp/tmp.xpm
It takes ~0.01s to generate a Hello World xpm image
(yes I know it would have been simpler if I used bash arrays, but I want it to be compatible with other shells)
Code: Select all
#!/bin/ash
#Given string input $1, go char by char & create 7x13 xpm representation
CHARS=" 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$%^&()_+-=<>,./?;':\"{}[]\|"
ROWA=" # ## # # # # # # ## ## #### #### # # "
ROWB=" ## # #### ###### # ###### ### ###### #### #### ## #### #### #### ###### ###### #### # # ##### ### # # # # # # # #### ##### #### ##### #### ######## # # # # # # # # # ###### # # ## # # # # ## # # # # # ### # # # ## # # # ## # # # #### # # # # # # # # # "
ROWC=" # # ## # # # ## # # # # # # # # # # # # # # # # # # # # # # # # # # ## ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #### # # # # # # # # # # # # # # # # # # # # # # # "
ROWD=" # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ## ## ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### # # # # # # ## # # # # # # # # # # # # # # # # # # # # "
ROWE=" # # # # # # # ##### # # # # # # # # # # # # # # # # # # # # # # # # ## # ## # # # # # # # # # # # # # # # # # # # # # # #### # ### #### ### # #### # ### # # ### ## ## # # # ## # # ### #### # ### ### # # ### #### ##### # # # # # # # # # # ###### # # # # ##### # # # ### # # # # # ###### # # # # ## ### # # # # # # "
ROWF=" # # # # ### # # # # # ### # ## # ## # # #### # # # #### #### # ###### # # ## # # ## # # # # # # # # # # # # ## # # # # # # # ## # # # # # ## # # # # ## # # ##### # # ## # # # # # # # # # ## # # # ## # # ## ## # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # ## # # # # # # # "
ROWG=" # # # # # # # # ## # # # # ### # ###### # # # # # # # # ### # # # # # # # # # # # # # # ##### # # ##### ## # # # # # # # ## # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ### # ## # # # ##### ##### # # # # # # # # # # "
ROWH=" # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # ### # # # # # # # # # # # # # # # # ##### # # # # # ###### # # # # # # # ### # # # # # # # # # # # # # ## # # # # # # # # ## # # # # # # # ##### # # ### # # # # # # ###### # # # # # # # # # # "
ROWI=" # # # # # # ###### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # ### # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # ## # # ### # # # # # ## # # # # # # # # # ## # # # # # # # "
ROWJ=" # # # # # # # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # # # # # # # ## # # # # # # # ## # # # # # # # # # ## # # # # ## # # # # # # # # # # # # # # # # # # ## # # ## # # # # # # ## # # # # # # ### # # # # # # # # # # # # # ## # # # # # ## # # # # ### # # # # # # "
ROWK=" ## ##### ###### #### # #### #### # #### ### # # #### #### #### ###### # ### # # # ##### ### # # ###### # # # # #### # #### # # #### # #### ## # # # # # ###### ##### # ### #### ### # #### # # ### # # ##### # # # # ##### # # # # #### # ### ### # # #### ## ### # # # # # # # ###### # #### # # #### # ## ## # # # # # # ### # # # # # # # # # # "
ROWL=" # # # # # # # # # # # # # # # # # # # # # # "
ROWM=" # #### ### # # #### # # ###### # # ## ## #### #### # # "
#echo $CHARS
j=0
while ([ "${j}" -lt "${#1}" ]) do
i=${CHARS%${1:$j:1}*} #cut the string after current char, length = position
i=${#i} #note each ROW{A-M}
RA=${RA}${ROWA:$(( $i*7 )):7}
RB=${RB}${ROWB:$(( $i*7 )):7}
RC=${RC}${ROWC:$(( $i*7 )):7}
RD=${RD}${ROWD:$(( $i*7 )):7}
RE=${RE}${ROWE:$(( $i*7 )):7}
RF=${RF}${ROWF:$(( $i*7 )):7}
RG=${RG}${ROWG:$(( $i*7 )):7}
RH=${RH}${ROWH:$(( $i*7 )):7}
RI=${RI}${ROWI:$(( $i*7 )):7}
RJ=${RJ}${ROWJ:$(( $i*7 )):7}
RK=${RK}${ROWK:$(( $i*7 )):7}
RL=${RL}${ROWL:$(( $i*7 )):7}
RM=${RM}${ROWM:$(( $i*7 )):7}
j=$(($j+1))
done
IMAGE='/* XPM */
static char *tmp_xpm[] = {
"'${#RA}' 13 2 1",
" c #000000",
"# c #FFFFFF",
"'$RA'",
"'$RB'",
"'$RC'",
"'$RD'",
"'$RE'",
"'$RF'",
"'$RG'",
"'$RH'",
"'$RI'",
"'$RJ'",
"'$RK'",
"'$RL'",
"'$RM'"
};'
echo "$IMAGE"