#!/bin/bash
#
#	$Id: text2image 1046 2007-07-15 08:47:27Z gromeck $
#
#	Copyright (c) 2004 by Christian Lorenz
#

#
#	load the common functions
#
for DIR in . $( dirname $0 )/../lib/image2mpeg /usr/lib/image2mpeg /usr/local/lib/image2mpeg; do
	if [ -e $DIR/commonfuncs.sh ]; then
		. $DIR/commonfuncs.sh
		break
	fi
done

VERBOSE=0
GREY=20
RGB=$(( 255 * (100 - $GREY) / 100 ))
FGCOLOR=$( printf "#%02x%02x%02x" $RGB $RGB $RGB )
BGCOLOR="#000000"
MODE=""
NORM="p"
FPS=0
BITRATE=0
BITRATE_VIDEO=0
BITRATE_AUDIO=192
SAMPLERATE=0
VIDEO_BUFFER=0
WIDTH=0
HEIGHT=0
FONTSIZE=24
LINESPACING=0
DRAW=""


################################################################################
#
#	show the usage
#
show_usage()
{
	[ "$*" != "" ] && echo "$*"
cat <<-EOM
Usage: $0 [options] [<text file>]
Options are:
    -h
    --help                     Show this message and exit.
    -V
    --version                  Show version and exit.
    -v <level>
    --verbose <level>          Increase verosity.
    -g <num>
    --grey <num>               Grey value to use as the foreground color
    -f <color>
    --fgcolor <color>          Foreground color
    -b <color>
    --bgcolor <color>          Background color
    -m <str>
    --mode <str>               Type of output (VCD, SVCD, DVD)
    -n <str>
    --norm <str>               Video norm (p=pal, n=ntsc)
    -w <n>
    --width <width>            Width of output (to override the width
                               according to the selected mode)
    -h <n>
    --height <width>           Height of output (to override the height
                               according to the selected mode)
    -o <str>
    --output <str>             Output file
EOM
	[ "$*" != "" ] && exit 1
	exit 0
}


################################################################################
#
#	check the arguments
#
while [ "$1" != "" ]; do
	if [ "$( echo "#$1" | cut -c2 )" = '-' ]; then
		case "$1" in
			-h|--help)
				#
				#	show the usage
				#
				show_usage
				exit 0
				;;
			-V|--version)
				#
				#	show the version
				#
				echo '$Id: text2image 1046 2007-07-15 08:47:27Z gromeck $'
				exit 0
				;;
			-v|--verbose)
				#
				#	verbosity level
				#
				shift
				VERBOSE=$1
				;;
			-g|--grey|--gray)
				#
				#	grey value vor the foreground color
				#
				shift
				GREY="$1"
				RGB=$(( 255 * (100 - $GREY) / 100 ))
				FGCOLOR=$( printf "#%02x%02x%02x" $RGB $RGB $RGB )
				;;
			-f|--fgcolor)
				#
				#	foreground color
				#
				shift
				FGCOLOR="$1"
				;;
			-b|--bgcolor)
				#
				#	foreground color
				#
				shift
				BGCOLOR="$1"
				;;
			-m|--mode)
				#
				#	mode of output
				#
				shift
				MODE="$1"
				;;
			-n|--norm|--video-norm)
				#
				#	video norm
				#
				shift
				case "$1" in
					p|P|pal|PAL)
						#
						#	PAL
						#
						NORM=p
						;;
					n|N|ntsc|NTSC)
						#
						#	NTSC
						#
						NORM=n
						;;
					*)
						show_usage "Unknown video norm '$1'"
				esac
				;;
			-w|--width)
				#
				#	width
				#
				shift
				WIDTH=$1
				;;
			-h|--height)
				#
				#	height
				#
				shift
				HEIGHT=$1
				;;
			-o|--output)
				#
				#	output file
				#
				shift
				OUTPUT=$1
				;;
			*)
				#
				#	unknown option
				#
				echo "$0: unknown option '$1'"
				show_usage
				exit 1
				;;
		esac
	else
		#
		#	check the given file
		#
		[ "$TEXTFILE" != "" ] && die "Sorry, multiple text files are not supported"
		TEXTFILE="$1"
	fi
	shift
done
[ "$OUTPUT" = "" ] && OUTPUT="$( basename $TEXTFILE ).png"
read FORMAT FPS WIDTH HEIGHT BITRATE BITRATE_AUDIO SAMPLERATE VIDEO_BUFFER <<EOM
	$( getvideoformat "$MODE" "$NORM" "$FPS" "$WIDTH" "$HEIGHT" "$BITRATE" "$BITRATE_AUDIO" "$SAMPLERATE" "$VIDEO_BUFFER" )
EOM
#echo VERBOSE=$VERBOSE
#echo GREY=$GREY
#echo MODE=$MODE
#echo NORM=$NORM
#echo FPS=$FPS
#echo WIDTH=$WIDTH
#echo HEIGHT=$HEIGHT
#echo FGCOLOR=$FGCOLOR
#echo BGCOLOR=$BGCOLOR
#echo TEXTFILE=$TEXTFILE
#echo BITRATE=$BITRATE
#echo BITRATE_VIDEO=$BITRATE_VIDEO
#echo BITRATE_AUDIO=$BITRATE_AUDIO
#echo OUTPUT=$OUTPUT
#echo SAMPLERATE=$SAMPLERATE
#echo VIDEO_BUFFER=$VIDEO_BUFFER
#exit

################################################################################
#
#	check the given arguments
#
[ "$MODE" = "" ] && die "Sorry, 'mode' is not set!"
[ "$TEXTFILE" != "" ] && [ ! -f "$TEXTFILE" ] && die "Couldn't open text file"
[ "$WIDTH" = 0 ] && die "Sorry, 'width' is not set!"
[ "$HEIGHT" = 0 ] && die "Sorry, 'height' is not set!"
[ "$OUTPUT" = "" ] && die "Sorry, 'output' is not set!"


################################################################################
#
#	setup temporary files
#
TMP="/tmp"
LOGFILE="$OUTPUT.log"
rm -f $LOGFILE


################################################################################
#
#	read the credits from file and add the draw statements
#
X=0
Y=$HEIGHT
LINENR=0
POINTSIZE=$FONTSIZE
while read LINE ; do
	LINENR=$(( $LINENR + 1 ))
	case "$( echo "$LINE" | cut -c1 )" in
		"#"|";")	#
				#	comment do nothing
				#
				;;
		"!")	#
				#	append this to converts command line
				#
				VAR="$( echo "$LINE" | cut -c2- | cut -f1 -d= )"
				VAL="$( echo "$LINE" | cut -c2- | cut -f2 -d= )"
				case "$VAR" in
					"font"|"FONT")
						#
						#	set a font
						#
						[ ! -f $VAL ] && die "Couldn't find font $VAL"
						DRAW="$DRAW -font @$VAL"
						;;
					"FONTSIZE"|"FONTSIZE")
						#
						#	set a fontsize
						#
						POINTSIZE=$(( $HEIGHT * $VAL / 100 ))
						DRAW="$DRAW -pointsize $POINTSIZE"
						;;
					"fgcolor"|"FGCOLOR")
						#
						#	set the foreground color
						#
						DRAW="$DRAW -fill '$VAL'"
						;;
					"justify"|"JUSTIFY")
						#
						#	set a justification
						#
						case "$( echo $VAL | tr "[:lower:]" "[:upper:]" )" in
							"left"|"LEFT")
								DRAW="$DRAW -gravity NorthWest"
								;;
							"center"|"CENTER")
								DRAW="$DRAW -gravity North"
								;;
							"right"|"RIGHT")
								DRAW="$DRAW -gravity NorthEast"
								;;
							*)
								echo "Unknown justification '$VAL' in line $LINENR"
								exit 1
						esac
						;;
					"linespacing"|"LINESPACING")
						#
						#	set a linespacing
						#
						LINESPACING=$(( $HEIGHT * $VAL / 100 ))
						;;
					"graphic"|"GRAPHIC")
						#
						#	insert a graphic
						#
						W=$( echo $VAL | cut -f1 -d, )
						FILE=$( echo $VAL | cut -f2 -d, )
						[ ! -f $FILE ] && die "Couldn't find graphic $FILE"
						IW=$( image_width $FILE )
						IH=$( image_height $FILE )
						if [ "$( echo $W | grep "%" )" != "" ]; then
							W=$( echo $W | tr -d "%" )
							W=$(( $W * $WIDTH / 100 ))
						fi
						IH=$(( $IH * $W / $IW ))
						IW=$(( $W ))
						DRAW="$DRAW -draw 'image over 0,$Y $IW,$IH \"$FILE\"'"
						Y=$(( $Y + $IH - $POINTSIZE + $LINESPACING ))
						;;
					*)
						echo "Unknown command '$VAR' in line $LINENR"
						exit 1
				esac
				;;
		*)		#
				#	append this line to the output
				#
				LINE="$( echo "$LINE" | sed -e "s/\"/\\\\\"/g" )"
				[ "$LINE" != "" ] && DRAW="$DRAW -draw 'text $X,$Y \"$LINE\"'"
				Y=$(( $Y + $POINTSIZE + $LINESPACING )) 
	esac
done <"$TEXTFILE"


################################################################################
#
#	complete the draw statement
#
Y=$(( $Y + $POINTSIZE + $LINESPACING )) 
OVERALL_HEIGHT=$(( $Y + $HEIGHT ))
echo "Generating image $OUTPUT with size of ${WIDTH}x${OVERALL_HEIGHT}px"
DRAW="convert \
		NULL: -geometry ${WIDTH}x${OVERALL_HEIGHT}! \
        -fill '$BGCOLOR' \
        -draw 'rectangle 0,0 $WIDTH,$OVERALL_HEIGHT' \
        -fill '$FGCOLOR' \
		-gravity North \
		-pointsize $FONTSIZE \
		$DRAW \"$OUTPUT\""
echo $DRAW >>$LOGFILE
nicerun "$DRAW"


#
#	EOF
#
################################################################################
