iGREAT Code
Status: Beta
Brought to you by:
jgonzamol
#!/bin/bash
PROG=${0##*/}
TMP=/tmp/$PROG.$$
trap "rm $TMP* 2>/dev/null" EXIT
DEST=papiro
FRAME=2
LANDSCAPE="yes"
MARGIN=0.2
PAGES=1
PRINT="no"
###############################################################################
function usage
{
cat <<-EOF >&2
usage: $PROG [<options>] [<file.ps[.gz]>]
-1 1 page per sheet (default)
-2 2 normal pages per sheet
-4 4 normal pages per sheet
-d <dest> choose dest as the printer to do printing ($DEST)
-frame <width> $FRAME points by default
-help this message
-landscape set landscape orientation (default)
-margin <length> minimum margin length in cm.
-Print print output
-portrait set portrait orientation
-view send output to the gv viewer (default)
EOF
exit
}
###############################################################################
function landscape1
{
if [ $PRINT = "no" ]; then gv -spartan -seascape $TMP-aux
else cat $TMP-aux | rsh remero lp -d$DEST -odpi600
fi
}
###############################################################################
function portrait1
{
if [ $PRINT = "no" ]; then gv -spartan $TMP-aux
else cat $TMP-aux | rsh remero lp -d$DEST -odpi600
fi
}
###############################################################################
function landscape2
{
# gv -portrait
# y
# ^ -------------------
# | ---------------- |
# | | | |
# | | 1 | |
# | | | |
# | ----------------o |
# | ---------------- |
# | | | |
# | | 2 | |
# | | | |
# | ----------------o |
# o--------------------> x
# x scale: $xs * 29.7 + 2 * $MARGIN = 21
xs=`echo "(21-2*$MARGIN)/29.7" | bc -l`
# y scale: 2 * $ys * 21 + 3 * $MARGIN = 29.7
ys=`echo "(29.7-3*$MARGIN)/42" | bc -l`
# scale and margins
s=`echo "if ($xs<$ys) $xs else $ys" | bc -l`
xm=`echo "if ($xs<$ys) $MARGIN else (21-$s*29.7)/2" | bc -l`
ym=`echo "if ($xs<$ys) (29.7-2*$s*21)/3 else $MARGIN" | bc -l`
# shifts
s1="(`echo "$s*29.7+$xm" | bc -l`cm,`echo "$s*21+2*$ym" | bc -l`cm)"
s2="(`echo "$s*29.7+$xm" | bc -l`cm,${ym}cm)"
pstops -pa4 -d$FRAME "2:0L@$s$s1+1L@$s$s2" $TMP-aux $TMP
if [ $PRINT = "no" ]; then gv -spartan -portrait $TMP
else cat $TMP | rsh remero lp -d$DEST -odpi600
fi
}
###############################################################################
function portrait2
{
# gv -seascape x
# ------------------------------^
# | ----------- ------------ |
# | | | | | |
# | | | | | |
# | | | | | |
# | | 1 | | 2 | |
# | | | | | |
# | | | | | |
# | | | | | |
# | | | | | |
# | o----------- o------------ |
# y< -----------------------------o
# x scale: $xs * 29.7 + 2 * $MARGIN = 21
xs=`echo "(21-2*$MARGIN)/29.7" | bc -l`
# y scale: 2 * $ys * 21 + 3 * $MARGIN = 29.7
ys=`echo "(29.7-3*$MARGIN)/42" | bc -l`
# scale and margins
s=`echo "if ($xs<$ys) $xs else $ys" | bc -l`
xm=`echo "if ($xs<$ys) $MARGIN else (21-$s*29.7)/2" | bc -l`
ym=`echo "if ($xs<$ys) (29.7-2*$s*21)/3 else $MARGIN" | bc -l`
# shifts
s1="(${xm}cm,`echo "2*$s*21+2*$ym" | bc -l`cm)"
s2="(${xm}cm,`echo "$s*21+$ym" | bc -l`cm)"
pstops -pa4 -d$FRAME "2:0R@$s$s1+1R@$s$s2" $TMP-aux $TMP
if [ $PRINT = "no" ]; then gv -spartan -seascape $TMP
else cat $TMP | rsh remero lp -d$DEST -odpi600 -ohd
fi
}
###############################################################################
function landscape4
{
# gv -seascape x
# ------------------------------^
# | ----------- ------------ |
# | | | | | |
# | | 1 | | 2 | |
# | | | | | |
# | -----------o ------------o |
# | ----------- ------------ |
# | | | | | |
# | | 3 | | 4 | |
# | | | | | |
# | -----------o ------------o |
# y<------------------------------o
# scale: 2 * $s * 21 + 3 * $MARGIN = 21
s=`echo "(21-3*$MARGIN)/(2*21)" | bc -l`
# y margin: 2 * $s * 29.7 + 3 * $ym = 29.7
ym=`echo "(29.7-2*$s*29.7)/3" | bc -l`
# shifts
s1="(`echo "$s*21+2*$MARGIN" | bc -l`cm,`echo "$s*29.7+2*$ym" | bc -l`cm)"
s2="(`echo "$s*21+2*$MARGIN" | bc -l`cm,${ym}cm)"
s3="(${MARGIN}cm,`echo "$s*29.7+2*$ym" | bc -l`cm)"
s4="(${MARGIN}cm,${ym}cm)"
pstops -pa4 -d$FRAME "4:0@$s$s1+1@$s$s2+2@$s$s3+3@$s$s4" $TMP-aux $TMP
if [ $PRINT = "no" ]; then gv -spartan -seascape $TMP
else cat $TMP | rsh remero lp -d$DEST -odpi600 -ohd
fi
}
###############################################################################
function portrait4
{
# y
# ^ ------------------
# | ------ ------ |
# | | | | | |
# | | 1 | | 2 | |
# | | | | | |
# | o------ o------ |
# | ------ ------ |
# | | | | | |
# | | 3 | | 4 | |
# | | | | | |
# | o------ o------ |
# o--------------------> x
# scale: 2 * $s * 21 + 3 * $MARGIN = 21
s=`echo "(21-3*$MARGIN)/(2*21)" | bc -l`
# y margin: 2 * $s * 29.7 + 3 * $ym = 29.7
ym=`echo "(29.7-2*$s*29.7)/3" | bc -l`
# shifts
s1="(${MARGIN}cm,`echo "$s*29.7+2*$ym" | bc -l`cm)"
s2="(`echo "$s*21+2*$MARGIN" | bc -l`cm,`echo "$s*29.7+2*$ym" | bc -l`cm)"
s3="(${MARGIN}cm,${ym}cm)"
s4="(`echo "$s*21+2*$MARGIN" | bc -l`cm,${ym}cm)"
pstops -pa4 -d$FRAME "4:0@$s$s1+1@$s$s2+2@$s$s3+3@$s$s4" $TMP-aux $TMP
if [ $PRINT = "no" ]; then gv -spartan $TMP
else cat $TMP | rsh remero lp -d$DEST -odpi600
fi
}
###############################################################################
while [ $# -ge 1 ]; do
if [ ${1:0:2} = "-1" ]; then PAGES=1
elif [ ${1:0:2} = "-2" ]; then PAGES=2
elif [ ${1:0:2} = "-4" ]; then PAGES=4
elif [ ${1:0:2} = "-d" ]; then shift; DEST=$1; PRINT="yes"
elif [ ${1:0:2} = "-f" ]; then shift; FRAME=$1
elif [ ${1:0:2} = "-h" ]; then usage;
elif [ ${1:0:2} = "-l" ]; then LANDSCAPE="yes"
elif [ ${1:0:2} = "-m" ]; then shift; MARGIN=$1
elif [ ${1:0:2} = "-P" ]; then PRINT="yes"
elif [ ${1:0:2} = "-p" ]; then LANDSCAPE="no"
elif [ ${1:0:2} = "-v" ]; then PRINT="no"
elif [ ${1:0:1} = "-" ]; then echo "$PROG: Unknown option: $1"; usage;
else break;
fi
shift
done
if [ $# -eq 0 ]; then gunzip -cf -; else gunzip -cf $1; fi >$TMP-aux
if [ $LANDSCAPE = "yes" ]; then
if [ $PAGES -eq 1 ]; then landscape1
elif [ $PAGES -eq 2 ]; then landscape2
else landscape4
fi
else
if [ $PAGES -eq 1 ]; then portrait1
elif [ $PAGES -eq 2 ]; then portrait2
else portrait4
fi
fi