#!/bin/sh
# text_to_ascii_graphics.sh
# (c) zimon@iki.fi Mon Oct 30 00:26:26 EET 2000
# Example1: ./text_to_ascii_graphics.sh "E X A M P L E 1"
#    .___    .  .      .     .  .    .__.    .       .___     .,
#    |__     `\,'     |\     |\.&    |__|    |       |__       |
#    |___    ,'`\    .T"L    |`/|    |       |__.    |___      |
#
# Example2: ./text_to_ascii_graphics.sh -i "EXAMPLE 2"     # messy
# Example3: ./text_to_ascii_graphics.sh -i -b "EXAMPLE 3"  # ok

VERSION="0.05"

# Good fonts are hard to find. This script requires xfs to make the fonts.
# The idea is to have some "clean" fonts converted to BDF. See pbmtext(1).
# You can use xlsfonts(1) and xfontsel(1) to help select the fonts.
# i.e. 'lucidasans-14' is a good one too.
#FONTSERVER='unix/:-1'
FONTSERVER='127.0.0.1:7100'
FONT='-schumacher-clean-medium-r-normal--16-160-75-75-c-80-iso8859-1'
FONTDIR='/tmp'	    # Where to write the BDF font file.

############################################################################
INVERSE=0
if [ "$1" = "-i" ]; then
  shift 1
  INVERSE=1
fi

SIZE="-2x4"
if [ "$1" = "-b" ]; then
  shift 1
  SIZE="-1x2"
fi

if [ "$1" = "-s" ]; then
  shift 1
  FONTSERVER=$1
  shift 1
fi

if [ "$1" = "-fn" ]; then
  shift 1
  FONT=$1
  shift 1
fi

if [ "$#" -gt "1" -o "$1" = "-h" -o "$1" = "--help" ]; then
  echo "Usage: $0 [-i] [-b] [-s <font_server>] [-fn <fonts>] <text_to_be_converted>"
  exit 1
fi

# Get the fonts and convert them to BDF file.
if [ ! -f "$FONTDIR/$FONT.bdf" -o ! -s "$FONTDIR/$FONT.bdf" ]; then
  echo "Tries to extract fonts from the font server $FONTSERVER"
  fstobdf -s $FONTSERVER -fn "$FONT" > "$FONTDIR/$FONT.bdf"
  if [ ! -f "$FONTDIR/$FONT.bdf" -o ! -s "$FONTDIR/$FONT.bdf" ]; then
    echo "Failed to retrieve or convert fonts from the font server."
    exit 1
  fi  
  echo "Fonts were converted to BDF."
  echo "File $FONTDIR/$FONT.bdf was created."
fi

# Do the rap.
if [ "$INVERSE" = "0" ]; then
  pbmtext -font "$FONTDIR/$FONT.bdf" $1 | pbmtoascii $SIZE
else
  pbmtext -font "$FONTDIR/$FONT.bdf" $1 | pnminvert | pbmtoascii $SIZE
fi
  
#	Creates a pbm picture from the text string using the "clean" font.
#	Then converts the pbm-pic to ascii graphics using one ascii-char
#	per 2x4 or 1x2 (-b == bigger) pixels. Graphics in inverse with -i


