#!/bin/sh
# Part of latex2rtf by Scott Prahl (Nov 2002)
# Adapted for using pstopnm by Enrico Forestieri (Nov 2005)
#
# This version uses latex and dvips
#              and ghostscript (through pstopnm)
#              and various netpbm utilities
#
# This script file will convert a latex file into a PNG image.
# Because latex2rtf also needs to convert EPS files, these are 
# also converted.  By default latex2png assumes that the input
# file is a latex file, however, if the extension is .eps, then
# the file is treated an EPS file. 
#
# When the latex file contains the tag 'INLINE_DOT_ON_BASELINE' then
# a dot is assumed to have been placed at the beginning of a latex
# This dot is used to locate the baseline of the equation for 
# alignment in the RTF file.  This shell script will create a PGN
# (portable gray map) file that can be used to determine the 
# height of the equation baseline
#
#set -x  # uncomment for debugging
#

DVIPS="dvips -q -E -R"
LATEX="latex --interaction batchmode"
PSTOPNM="pstopnm"
PNMCROP="pnmcrop"
PNMCUT="pnmcut"
PNMTOPNG="pnmtopng"
PPMTOPGM="ppmtopgm"
PGMTOPBM="pgmtopbm"
TEXINPSEP=":"
GREP="grep"
VERSION="1.5"

help()
{
cat <<HELP
latex2png -- convert latex file to PNG image

USAGE: latex2png [-d density] [-o offset] [-h] [-k] [-c] [-g] [-m] [-H home dir] file[.tex|.eps]

OPTIONS: 
         -c             color image (default)
         -g             gray image  
         -m             monochrome

         -d density     (default 144 dpi)
         -o offset      = density/72+2
         -h             help
         -H /home/dir	directory to be included in search path (default '.')
         -k         	keep intermediate files (for debugging)

EXAMPLES: 
         latex2png -d 144 /tmp/file   #create /tmp/file.png at 144 dpi
         latex2png file.eps           #create file.png
         latex2png file.tex           #create file.png via latex
         latex2png -H . /tmp/file     #search the cwd for image files

HELP
exit 0
}

version()
{
echo "latex2png $VERSION"
exit 0
}

opt_d=144		# default to 144 dpi
opt_o=4			# default for 144 dpi
opt_k=0			# default to killing intermediate files
home_dir="."		# default to current directory
ext="tex"		# default to latex input
out="ppm"		# default to color output
inline=0		# default to no special equation processing

while [ -n "$1" ]; do
case $1 in
    -g) out="pgm";   shift 1;; #gray
    -c) out="ppm";   shift 1;; #color
    -m) out="pbm";   shift 1;; #monochrome
    -h) help;        shift 1;;
    -k) opt_k=1;     shift 1;; 
    -d) opt_d=$2;    shift 2;; #shift by 2 due to argument
    -o) opt_o=$2;    shift 2;;
    -H) home_dir=$2; shift 2;;
    -v) version;     shift 1;;
    --) break;;
    -*) echo "error:no such option $1"; exit 1;;
    *)  break;;
esac
done

# is there an input file?
if [ -z "$1" ] ; then
    echo "no input file specified"
    exit 1
fi

# is it an eps file?
if [ `echo $1 | ${GREP} -c eps$` -eq 1 ] ; then
    ext="eps"
fi

#process 'latex2png file.tex' and 'latex2png file' equivalently
name=`basename $1 ".$ext"`
dir=`dirname $1`

#add $home_dir to latex/dvips search path
cd $home_dir
TEXINPUTS=`pwd`${TEXINPSEP}
export TEXINPUTS
cd $dir

# does the input file exist?
if [ ! -f "$name.$ext" ] ; then
    echo "error: input file $name.$ext does not exist in directory $dir"
    exit 1
fi

rm -f $name.dvi $name.tmp.$out

if [ $ext = "tex" ] ; then

    inline=`${GREP} -c INLINE_DOT_ON_BASELINE $name.$ext`

    $LATEX $name > /dev/null
    
    if [ ! -e "$name.dvi" ] ; then
	echo "error: latex failed to translate $name.tex to $name.dvi"
	exit 1
    fi
    
    $DVIPS -o $name.eps $name.dvi
    
    if [ ! -e "$name.eps" ] ; then
	echo "error: dvips failed to translate $name.dvi to $name.eps"
	exit 1
    fi
fi

bb=`grep "%%BoundingBox" $name.eps`

if [ -z "$bb" ]; then
    echo "warning: BoundingBox not found in $name.eps"
    psargs=""
else
    llx=`echo $bb | cut -d' ' -f2`
    urx=`echo $bb | cut -d' ' -f4`
    sx=`(echo "scale=4";echo "$urx - $llx")|bc -l`
    xsize=`(echo "scale=0";echo "$sx * $opt_d / 72")|bc -l`
    psargs="-xborder 0 -yborder 0 -xsize $xsize"
fi

# pstopnm prefers the .ps extension
rm -f $name-.ps
ln -s $name.eps $name-.ps
$PSTOPNM -$out -portrait $psargs $name-.ps 2> /dev/null
rm -f $name-.ps

if [ ! -e "$name-001.$out" ] ; then
    echo "error: $PSTOPNM failed to translate $name.eps to $name-001.$out"
    exit 1
fi

$PNMCROP $name-001.$out > $name.$out 2> /dev/null
rm -f $name-001.$out

if [ ! -e "$name.$out" ] ; then
    echo "error: $PNMCROP failed to crop $name-001.$out to $name.$out"
    exit 1
fi

rm -f $name.png

# $inline is for images of equations that include an extra dot on the baseline.
#    To create the final image $name.png the dot in $name.$out is cropped out
#    and extra white space is removed.
#    The height can be determined from $name.pbm which contains the second
#    column of the image.
# 
if [ $inline -eq 1 ] ; then
    rm -f $name.pbm

    $PNMCUT -left $opt_o $name.$out | $PNMCROP -left | $PNMTOPNG > $name.png 2> /dev/null

    $PNMCUT -left 1 -width 1 $name.$out | $PPMTOPGM | $PGMTOPBM > $name.tmp.pbm
    mv $name.tmp.pbm $name.pbm

else
    $PNMTOPNG $name.$out > $name.png 2> /dev/null
fi

if [ ! -e "$name.png" ] ; then
    echo "error: $PNMTOPNG failed to translate $name.$out to $name.png"
    exit 1
fi

if [ $opt_k -eq 0 ] ; then
    rm -f $name.dvi $name.aux $name.log
    if [ $inline -eq 0 -o ! "$out" = "pbm" ]; then
	rm -f $name.$out
    fi
    if [ $ext = "tex" ] ; then
	rm -f $name.eps
    fi
fi

exit 0

