#!/bin/csh
#
# vnlpr -- Print vietnamese text in either 7 bit or 8 bit VISCII encoding.
#	   Required vn7to8 to translate 7 bit to 8 bit code and vnpstext
#	   to produce postscript file.
#
# Written by Phu`ng Gia Cu+ (phung@spin.chem.utah.edu)
#
# Version: 2.0
#
# Usage: vnlpr [-am|-co|-cb|-hm|-hy|-mq|-pt|-th|-uh|-f fontname] [-s fontsize]
#		[-i|-O|-b] [-m|-v] [-7|-8] [-o outfile|-P printer] [-h] filename
#
#	-am : A'nh-Minh font (Optima)
#	-co : Courier-VN font
#	-cb : Courier-VN-Bold font
#	-hm : Heo-May font (Helvetica)
#	-hy : Hoa`ng-Ye^'n font (Present-script)
#	-mq : Minh-Qua^n font (Courier)
#	-pt : Phu+o+ng-Tha?o font (Brush script)
#	-th : Tha-Hu+o+ng font (Chancery)
#	-uh : U-Hoa`i font (Utopia)
#	-f  : specify fontname to use (only nine available)
#		AnhMinh
#		Courier-VN
#               Courier-VN-Bold
#		HeoMay
#		HoangYen
#		MinhQuan
#		PhuongThao
#		ThaHuong
#		UHoai
#	  -i : slanted font (Italic)
#	  -O : outline font (Ghost)
#         -b : pseudo bold font, scaled from the outline font
#         -s : specify fontsize
#         -m : language flag for vn7to8
#         -v : language flag for vn7to8 (default)
#         -7 : input is a 7-bit file
#         -8 : input is an 8-bit file
#         -o : output to outfile instead of printer
#         -P : specify printer name
#         -h : this help message
#
# Default parameters (Make necessary changes for your environments)
#
# set VNLIB to the directory where the vnpstext initialization files and the
# .gsf files are stored
set VNLIB=/usr/local/share/vn-fonts
#
# set PRINTER to your default postscript printer
#set PRINTER="postscript"
#
# set TMPDIR to a scratch directory
set TMPDIR=/tmp
#
# set FONT to your favor font and be sure to set INIT to the coresponding font

# Uncomment the next four lines for use of AnhMinh font as default font
#set FONT="AnhMinh11"
#set INIT="am_11.gsf"
#set LINES="47"
#set SPACE="-1.440"

# Uncomment the next four lines for use of Courier font as default font
#set FONT="Courier-VN"
#set INIT="initcour.ps"
#set LINES="61"
#set SPACE="-1.038"

# Uncomment the next four lines for use of Courier-Bold font as default font
#set FONT="Courier-VN-Bold"
#set INIT="initcourb.ps"
#set LINES="60"
#set SPACE="-1.138"

# Uncomment the next four lines for use of HeoMay font as default font
#set FONT="HeoMay11"
#set INIT="hm_11.gsf"
#set LINES="47"
#set SPACE="-1.4"

# Uncomment the next four lines for use of HoangYen font as default font
#set FONT="HoangYen11"
#set INIT="hy_11.gsf"
#set LINES="50"
#set SPACE="-1.370"

# Uncomment the next four lines for use of MinhQuan font as default font
set FONT="MinhQuan11"
set INIT="mq_11.gsf"
set LINES="60"
set SPACE="-1.150"

# Uncomment the next four lines for use of PhuongThao font as default font
#set FONT="PhuongThao11"
#set INIT="pt_11.gsf"
#set LINES="51"
#set SPACE="-1.345"

# Uncomment the next four lines for use of ThaHuong font as default font
#set FONT="ThaHuong11"
#set INIT="th_11.gsf"
#set LINES="54"
#set SPACE="-1.270"

# Uncomment the next four lines for use of UHoai font as default font
#set FONT="UHoai11"
#set INIT="uh_11.gsf"
#set LINES="47"
#set SPACE="-1.445"

#
set SIZE="12"		# Default 12 point font size
set LANG="-v"		# Default assuming VISCII format
set OUTFILE=""		# Default assuming output is sent to printer
set BIT7="FALSE"	# Default assuming an 8 bit input file
set SLANT="FALSE"
set OUTLINE="FALSE"
set BOLD="FALSE"
set FILE=""
#
# Check options
#
set OPTS=($*)
while ("$1" =~ -*)
	switch ($1)
	case "-b":
		set BOLD="TRUE"
		breaksw
	case "-i":
		set SLANT="TRUE"
		breaksw
	case "-O":
		set OUTLINE="TRUE"
		breaksw
	case "-co":
		set FONT="Courier-VN"
		set INIT="initcour.ps"
		set LINES="60"
		set SPACE="-1.138"
		breaksw
	case "-cb":
		set FONT="Courier-VN-Bold"
		set INIT="initcourb.ps"
		set LINES="60"
		set SPACE="-1.138"
		breaksw
	case "-am":
		set FONT="AnhMinh11"
		set INIT="am_11.gsf"
		set LINES="47"
		set SPACE="-1.440"
		breaksw
	case "-hm":
		set FONT="HeoMay11"
		set INIT="hm_11.gsf"
		set LINES="47"
		set SPACE="-1.430"
		breaksw
	case "-hy":
		set FONT="HoangYen11"
		set INIT="hy_11.gsf"
		set LINES="50"
		set SPACE="-1.370"
		breaksw
	case "-mq":
		set FONT="MinhQuan11"
		set INIT="mq_11.gsf"
		set LINES="60"
		set SPACE="-1.150"
		breaksw
	case "-pt":
		set FONT="PhuongThao11"
		set INIT="pt_11.gsf"
		set LINES="51"
		set SPACE="-1.345"
		breaksw
	case "-th":
		set FONT="ThaHuong11"
		set INIT="th_11.gsf"
		set LINES="54"
		set SPACE="-1.270"
		breaksw
	case "-uh":
		set FONT="UHoai11"
		set INIT="uh_11.gsf"
		set LINES="47"
		set SPACE="-1.445"
		breaksw
	case "-f":
		shift
		switch ($1)
		case "Courier":
			set INIT="initcour.ps"
			set LINES="60"
			set SPACE="-1.138"
			breaksw
		case "Courier-VN":
			set INIT="initcour.ps"
			set LINES="60"
			set SPACE="-1.138"
                        breaksw
		case "Courier-Bold":
			set INIT="initcourb.ps"
			set LINES="60"
			set SPACE="-1.138"
			breaksw
		case "Courier-VN-Bold":
			set INIT="initcourb.ps"
			set LINES="60"
			set SPACE="-1.138"
                        breaksw
		case "AnhMinh":
			set INIT="am_11.gsf"
			set LINES="47"
			set SPACE="-1.440"
			breaksw
		case "HeoMay":
			set INIT="hm_11.gsf"
			set LINES="47"
			set SPACE="-1.430"
                        breaksw
		case "HoangYen":
			set INIT="hy_11.gsf"
			set LINES="50"
			set SPACE="-1.370"
			breaksw
		case "MinhQuan":
			set INIT="mq_11.gsf"
			set LINES="60"
			set SPACE="-1.150"
                        breaksw
		case "PhuongThao":
			set INIT="pt_11.gsf"
			set LINES="51"
			set SPACE="-1.345"
			breaksw
		case "ThaHuong":
			set INIT="th_11.gsf"
			set LINES="54"
			set SPACE="-1.270"
                        breaksw
		case "UHoai":
			set INIT="uh_11.gsf"
			set LINES="47"
			set SPACE="-1.445"
                        breaksw
		case "AnhMinh11":
			set INIT="am_11.gsf"
			set LINES="47"
			set SPACE="-1.440"
			breaksw
		case "HeoMay11":
			set INIT="hm_11.gsf"
			set LINES="47"
			set SPACE="-1.430"
                        breaksw
		case "HoangYen11":
			set INIT="hy_11.gsf"
			set LINES="50"
			set SPACE="-1.370"
			breaksw
		case "MinhQuan11":
			set INIT="mq_11.gsf"
			set LINES="60"
			set SPACE="-1.150"
                        breaksw
		case "PhuongThao11":
			set INIT="pt_11.gsf"
			set LINES="51"
			set SPACE="-1.345"
			breaksw
		case "ThaHuong11":
			set INIT="th_11.gsf"
			set LINES="54"
			set SPACE="-1.270"
                        breaksw
		case "UHoai11":
			set INIT="uh_11.gsf"
			set LINES="47"
			set SPACE="-1.445"
                        breaksw
		endsw
		breaksw
	case "-m":
		set LANG="-m"
		breaksw
	case "-v":
		set LANG="-v"
		breaksw
	case "-s":
		shift
		set SIZE=$1
		breaksw
	case "-7":
		set BIT7="TRUE"
		breaksw
	case "-8":
		set BIT7="FALSE"
		breaksw
	case "-o":
		shift
		set OUTFILE=$1
		breaksw
	case "-P":
		shift
		set PRINTER=$1
		breaksw
	case "-h":
		echo "usage: vnlpr [-am|-co|-cb|-hm|-hy|-mq|-pt|-th|-uh|-f fontname] [-s fontsize]"
		echo '             [-i|-O|-b] [-m|-v] [-7|-8] [-o outfile|-P printer] [-h] filename'
		echo	' '
		echo	'	-am : Anh-Minh font'
		echo	'	-co : Courier-VN font'
		echo	'	-cb : Courier-VN-Bold font'
		echo    '	-hm : Heo-May font'
		echo    '	-hy : Hoang-Yen font'
		echo    '	-mq : Minh-Qua^n font'
		echo    '	-pt : Phuong-Thao font'
		echo    '	-th : Tha-Huong font'
		echo    '	-uh : U-Hoai font'
		echo	'	-f  : specify fontname to use (nine available)'
                echo    '		AnhMinh (Optima)'
		echo	'		Courier-VN'
		echo	'		Courier-VN-Bold'
		echo    '		HeoMay (Helvetica)'
		echo    '		HoangYen (Present script)'
		echo    '		MinhQuan (Courier)'
		echo    '		PhuongThao (Brush script)'
		echo    '		ThaHuong (Chancery)'
		echo    '		UHoai (Utopia)'
		echo	'	-s  : specify fontsize'
		echo	'	-i  : slanted font (Italic)'
		echo	'	-O  : outline font (Ghost)'
		echo	'	-b  : pseudo bold like, scaled from outline font'
		echo	'	-m  : language flag for vn7to8'
		echo	'	-v  : language flag for vn7to8 (default)'
		echo	'	-7  : input is a 7-bit file'
		echo	'	-8  : input is an 8-bit file'
		echo	'	-o  : output to outfile instead of printer'
		echo	'	-P  : specify printer name'
		echo	'	-h  : this help message'
		echo	' '
		exit 1
		breaksw
	case "-*":
		echo "usage: vnlpr [-am|-co|-cb|-hm|-hy|-mq|-pt|-th|-uh|-f fontname] [-s fontsize]"
                echo '             [-i|-O|-b] [-m|-v] [-7|-8] [-o outfile|-P printer] [-h] filename'
		exit 1
		breaksw
	endsw
	shift
end
set FILE=$1
if ( "$FILE" == "") then
  echo 'Input file is required.'
  echo "usage: vnlpr [-am|-co|-cb|-hm|-hy|-mq|-pt|-th|-uh|-f fontname] [-s fontsize]"
  echo '             [-i|-O|-b] [-m|-v] [-7|-8] [-o outfile|-P printer] [-h] filename'
  exit 1
endif

#
# Create postscript prolog file
#
set PROLOG=$TMPDIR/$$.pro
cat $VNLIB/$INIT > $PROLOG
echo "%! $SIZE-point $FONT prolog file" >> $PROLOG
echo "/Font{/$FONT findfont} def" >> $PROLOG
echo "/size $SIZE def" >> $PROLOG
echo "/bsize $SIZE 98 mul 100 div def" >> $PROLOG
echo "/slant size 3 mul 10 div def" >> $PROLOG
echo "/lines $LINES 10 mul $SIZE div def" >> $PROLOG
echo "/FMatrix{[size 0 0 size 0 0]} def" >> $PROLOG
echo "/BMatrix{[bsize 0 0 size 0 0]} def" >> $PROLOG
echo "/SMatrix{[size 0 slant size 0 0]} def" >> $PROLOG
echo "Font size scalefont setfont" >> $PROLOG
echo "/outline{/type exch def Font FMatrix makefont setfont size 1 mul">> $PROLOG
echo "         1000 div setlinewidth type false charpath stroke} def" >> $PROLOG
echo "/bold{/type exch def Font BMatrix makefont setfont size 5 mul">> $PROLOG
echo "         100 div setlinewidth type false charpath stroke} def" >> $PROLOG
echo "/slanted{/type exch def Font SMatrix makefont setfont" >> $PROLOG
echo "         type show stroke} def" >> $PROLOG
echo "/StartDoc{/sv save def 58 720 moveto} def" >> $PROLOG
echo "/EndDoc{sv restore} def" >> $PROLOG
echo "/Curline 0 def" >> $PROLOG
echo "/Doit {EndPage StartPage} def" >> $PROLOG
echo "/Lcheck { /Curline Curline 1 add def" >> $PROLOG
echo "Curline lines ge { /Curline 0 def showpage 58 720 moveto } if" >> $PROLOG
echo "} def" >> $PROLOG
echo "/ld size $SPACE mul def" >> $PROLOG
if ("$SLANT" == "TRUE") then
  echo "/S{Lcheck count{gsave slanted grestore}repeat 0 ld rmoveto} def" >> $PROLOG
  else if ("$OUTLINE" == "TRUE") then
    echo "/S{Lcheck count{gsave outline grestore}repeat 0 ld rmoveto} def" >> $PROLOG
  else if ("$BOLD" == "TRUE") then
    echo "/S{Lcheck count{gsave bold grestore}repeat 0 ld rmoveto} def" >> $PROLOG
  else
    echo "/S{Lcheck count{gsave show grestore}repeat 0 ld rmoveto} def">> $PROLOG
endif
echo "/L{Lcheck ld mul 0 exch rmoveto}def" >> $PROLOG
echo "/B{Lcheck 0 ld rmoveto}def" >> $PROLOG
#
switch ($BIT7)
	case "TRUE":
		if ("$OUTFILE" != "")  then
		    echo "Output file is $OUTFILE"
		    vn7to8 $LANG $FILE | vnpstext -i $PROLOG >! $OUTFILE
		else
		  echo "Printing $FILE at $PRINTER"
		  vn7to8 $LANG $FILE | vnpstext -i $PROLOG | lpr -P$PRINTER
		endif
	breaksw
	case "FALSE":
		if ("$OUTFILE" != "") then
		    echo "Output file is $OUTFILE"
		    vnpstext -i $PROLOG < $FILE >! $OUTFILE
		else
		  echo "Printing $FILE at $PRINTER"
		  vnpstext -i $PROLOG < $FILE | lpr -P$PRINTER
		endif
	breaksw
endsw
rm -f $PROLOG
exit 0
