update makebook

This commit is contained in:
Sebastian Hugentobler 2019-09-27 18:10:36 +02:00
parent affb7e9935
commit 5a77c03eaf
Signed by: shu
GPG Key ID: BB32CF3CA052C2F0

View File

@ -30,9 +30,11 @@
#**********************************************************# #**********************************************************#
# begin the code # # begin the code #
#**********************************************************# #**********************************************************#
# make script more portable and secure
PATH=/bin:/usr/bin:/usr/local/bin ; export PATH
umask 033 umask 033
# define our revision number variable for rcs # define our revision number variable for rcs
REVISION="2.0" REVISION="2.1"
# define error codes # define error codes
E_WRONG_ARGS=64 # too many or few args E_WRONG_ARGS=64 # too many or few args
E_BAD_SIG_TYPE=65 # invalid type of signature E_BAD_SIG_TYPE=65 # invalid type of signature
@ -74,6 +76,14 @@ command -v pdftk >/dev/null 2>&1 ||
{ echo >&2 "makebook: error: requires pdftk, but it's not installed"; { echo >&2 "makebook: error: requires pdftk, but it's not installed";
exit $E_NO_PROG; exit $E_NO_PROG;
} }
command -v dc >/dev/null 2>&1 ||
{ echo >&2 "makebook: error: requires dc, but it's not installed";
exit $E_NO_PROG;
}
command -v bc >/dev/null 2>&1 ||
{ echo >&2 "makebook: error: requires bc, but it's not installed";
exit $E_NO_PROG;
}
# print the version information and exit successfully # print the version information and exit successfully
versionfunc () versionfunc ()
@ -122,7 +132,7 @@ End-of-help
# define a for dealing with units; convert to bp () # define a for dealing with units; convert to bp ()
unitarg () unitarg ()
{ {
if [ ! `echo "$1" | grep '^[\+\|-]*[0-9\.][0-9\.]*\(in\|cm\|pt\|bp\|pc\|$\)'` ] if [ ! `echo "$1" | grep '^[\+\|-]*[0-9\.][0-9\.]*\(in\|cm\|pt\|mm\|bp\|pc\|$\)'` ]
then then
echo "ERROR: dimension \"$1\" not valid." >&2 echo "ERROR: dimension \"$1\" not valid." >&2
exit $E_BAD_UNIT exit $E_BAD_UNIT
@ -135,6 +145,10 @@ unitarg ()
then then
OPTARG=`echo "scale=0; ${1%cm} * 28.3464567" | bc` OPTARG=`echo "scale=0; ${1%cm} * 28.3464567" | bc`
OPTARG=${OPTARG%.*} OPTARG=${OPTARG%.*}
elif [ `expr match "$1" '.*\(mm\)'` ]
then
OPTARG=`echo "scale=0; ${1%mm} * 2.83464567" | bc`
OPTARG=${OPTARG%.*}
elif [ `expr match "$1" '.*\(pt\)'` ] elif [ `expr match "$1" '.*\(pt\)'` ]
then then
OPTARG=`echo "scale=0; ${1%pt} / 1.00375" | bc` OPTARG=`echo "scale=0; ${1%pt} / 1.00375" | bc`
@ -276,7 +290,7 @@ NUM_PAGES=`pdfinfo "$NEW_FILE_NAME" | awk '/Pages:/ {print $2}'`;
SRC_PAGE_WIDTH=`pdfinfo "$NEW_FILE_NAME" | awk '/Page\ size:/ {print $3}'`; SRC_PAGE_WIDTH=`pdfinfo "$NEW_FILE_NAME" | awk '/Page\ size:/ {print $3}'`;
SRC_PAGE_HEIGHT=`pdfinfo "$NEW_FILE_NAME" | awk '/Page\ size:/ {print $5}'`; SRC_PAGE_HEIGHT=`pdfinfo "$NEW_FILE_NAME" | awk '/Page\ size:/ {print $5}'`;
# find the number of pages we'll have per signature # find the number of pages we'll have per signature
PAGES_PER_SIG=$(($PAGES_PER_SIG * $SECT_TYPE)) PAGES_PER_SIG=$(dc -e "$PAGES_PER_SIG $SECT_TYPE * p")
# determine if extra pages will be necessary # determine if extra pages will be necessary
NUM_BLANKS=`expr $NUM_PAGES % $PAGES_PER_SIG` NUM_BLANKS=`expr $NUM_PAGES % $PAGES_PER_SIG`
if [ $NUM_BLANKS -ne 0 ] if [ $NUM_BLANKS -ne 0 ]
@ -293,13 +307,12 @@ then
echo '\\thispagestyle{empty}' >> blank.tex echo '\\thispagestyle{empty}' >> blank.tex
echo "\usepackage[paperwidth=${SRC_PAGE_WIDTH}bp,paperheight="${SRC_PAGE_HEIGHT}bp"]{geometry}" >> blank.tex echo "\usepackage[paperwidth=${SRC_PAGE_WIDTH}bp,paperheight="${SRC_PAGE_HEIGHT}bp"]{geometry}" >> blank.tex
echo '\\begin{document}' >> blank.tex echo '\\begin{document}' >> blank.tex
echo "\quad \end{document}" >> blank.tex echo "\quad \\\\end{document}" >> blank.tex
if [ $VERBOSE -eq 1 ] if [ $VERBOSE -eq 1 ]
then then
echo "CREATING blank page to fill sections..."; echo "CREATING blank page to fill sections...";
fi fi
pdflatex blank.tex > /dev/null 2&>1 pdflatex blank.tex > /dev/null 2&>1
rm blank.aux blank.log blank.tex
fi fi
# now insert blank pages as needed, preferring the back for # now insert blank pages as needed, preferring the back for
# odd numbers by default, front if stated # odd numbers by default, front if stated
@ -362,7 +375,7 @@ tmp=`expr $NUM_PAGES + $NUM_BLANKS`
if [ $VERBOSE -eq 1 ] if [ $VERBOSE -eq 1 ]
then then
echo "IMPOSING $NUM_PAGES pages ($tmp with blanks) on echo "IMPOSING $NUM_PAGES pages ($tmp with blanks) on
$(($NUM_SIGS * $SECT_TYPE)) signature(s) gathered in $(dc -e "$NUM_SIGS $SECT_TYPE * p") signature(s) gathered in
sections of $SECT_TYPE signature(s) each..."; sections of $SECT_TYPE signature(s) each...";
fi fi
# arrange the pages for impression on signatures # arrange the pages for impression on signatures
@ -377,16 +390,16 @@ do
if [ "$SIG_TYPE" = "quarto" ] if [ "$SIG_TYPE" = "quarto" ]
then then
# determine size of target page # determine size of target page
if [ $(echo "$TGT_PAGE_HEIGHT == 0" | bc) -eq 1 ] if [ $TGT_PAGE_HEIGHT -eq 0 ]
then then
TGT_PAGE_HEIGHT=$(echo "$SRC_PAGE_HEIGHT * 2" | bc) TGT_PAGE_HEIGHT=$(dc -e "$SRC_PAGE_HEIGHT 2 * p")
fi fi
if [ $(echo "$TGT_PAGE_WIDTH == 0" | bc) -eq 1 ] if [ $TGT_PAGE_WIDTH -eq 0 ]
then then
TGT_PAGE_WIDTH=$(echo "$SRC_PAGE_WIDTH * 2" | bc) TGT_PAGE_WIDTH=$(dc -e "$SRC_PAGE_WIDTH 2 * p")
fi fi
# do the rearranging # do the rearranging
k=$i; n=1; total=$(($SECT_TYPE * 8)); m=$total k=$i; n=1; total=$(dc -e "$SECT_TYPE 8 * p"); m=$total
while [ $n -le $SECT_TYPE ] while [ $n -le $SECT_TYPE ]
do do
pdftk "$NEW_FILE_NAME" cat `expr $k + $m - 1` $k \ pdftk "$NEW_FILE_NAME" cat `expr $k + $m - 1` $k \
@ -418,16 +431,16 @@ do
elif [ "$SIG_TYPE" = "folio" ] elif [ "$SIG_TYPE" = "folio" ]
then then
# determine size of target page # determine size of target page
if [ $(echo "$TGT_PAGE_HEIGHT == 0" | bc) -eq 1 ] if [ 1 -eq "$(echo "${TGT_PAGE_HEIGHT} == 0" | bc)" ]
then then
TGT_PAGE_HEIGHT=$(echo "$SRC_PAGE_HEIGHT * 1" | bc) TGT_PAGE_HEIGHT=$(dc -e "$SRC_PAGE_HEIGHT 1 * p")
fi fi
if [ $(echo "$TGT_PAGE_WIDTH == 0" | bc) -eq 1 ] if [ 1 -eq "$(echo "${TGT_PAGE_WIDTH} == 0" | bc)" ]
then then
TGT_PAGE_WIDTH=$(echo "$SRC_PAGE_WIDTH * 2" | bc) TGT_PAGE_WIDTH=$(dc -e "$SRC_PAGE_WIDTH 2 * p")
fi fi
# do the rearranging # do the rearranging
k=$i; n=1; total=$(($SECT_TYPE * 4)); m=$total k=$i; n=1; total=$(dc -e "$SECT_TYPE 4 * p"); m=$total
while [ $n -le $SECT_TYPE ] while [ $n -le $SECT_TYPE ]
do do
pdftk "$NEW_FILE_NAME" cat `expr $k + $m - 1` \ pdftk "$NEW_FILE_NAME" cat `expr $k + $m - 1` \
@ -458,16 +471,16 @@ do
elif [ "$SIG_TYPE" = "octavo" ] elif [ "$SIG_TYPE" = "octavo" ]
then then
# determine target page dimensions # determine target page dimensions
if [ $(echo "$TGT_PAGE_HEIGHT == 0" | bc) -eq 1 ] if [ 1 -eq "$(echo "${TGT_PAGE_HEIGHT} == 0" | bc)" ]
then then
TGT_PAGE_HEIGHT=$(echo "$SRC_PAGE_HEIGHT * 2" | bc) TGT_PAGE_HEIGHT=$(dc -e "$SRC_PAGE_HEIGHT 2 * p")
fi fi
if [ $(echo "$TGT_PAGE_WIDTH == 0" | bc) -eq 1 ] if [ 1 -eq "$(echo "${TGT_PAGE_WIDTH} == 0" | bc)" ]
then then
TGT_PAGE_WIDTH=$(echo "$SRC_PAGE_WIDTH * 4" | bc) TGT_PAGE_WIDTH=$(dc -e "$SRC_PAGE_WIDTH 4 * p")
fi fi
# do the rearranging # do the rearranging
k=$i; n=1; total=$(($SECT_TYPE * 16)); m=$total k=$i; n=1; total=$(dc -e "$SECT_TYPE 16 * p"); m=$total
while [ $n -le $SECT_TYPE ] while [ $n -le $SECT_TYPE ]
do do
pdftk "$NEW_FILE_NAME" cat `expr $k + 3` \ pdftk "$NEW_FILE_NAME" cat `expr $k + 3` \
@ -503,16 +516,16 @@ do
elif [ "$SIG_TYPE" = "sexto" ] elif [ "$SIG_TYPE" = "sexto" ]
then then
# determine target page dimensions # determine target page dimensions
if [ $(echo "$TGT_PAGE_HEIGHT == 0" | bc) -eq 1 ] if [ $TGT_PAGE_HEIGHT -eq 0 ]
then then
TGT_PAGE_HEIGHT=$(echo "$SRC_PAGE_HEIGHT * 3" | bc) TGT_PAGE_HEIGHT=$(dc -e "$SRC_PAGE_HEIGHT 3 * p")
fi fi
if [ $(echo "$TGT_PAGE_WIDTH == 0" | bc) -eq 1 ] if [ $TGT_PAGE_WIDTH -eq 0 ]
then then
TGT_PAGE_WIDTH=$(echo "$SRC_PAGE_WIDTH * 2" | bc) TGT_PAGE_WIDTH=$(dc -e "$SRC_PAGE_WIDTH 2 * p")
fi fi
# do the rearranging # do the rearranging
k=$i; n=1; total=$(($SECT_TYPE * 12)); m=$total k=$i; n=1; total=$(dc -e "$SECT_TYPE 12 * p"); m=$total
while [ $n -le $SECT_TYPE ] while [ $n -le $SECT_TYPE ]
do do
pdftk "$NEW_FILE_NAME" cat `expr $k + 5` \ pdftk "$NEW_FILE_NAME" cat `expr $k + 5` \
@ -547,16 +560,16 @@ do
elif [ "$SIG_TYPE" = "duodecimo" ] elif [ "$SIG_TYPE" = "duodecimo" ]
then then
# determine target page dimensions # determine target page dimensions
if [ $(echo "$TGT_PAGE_HEIGHT == 0" | bc) -eq 1 ] if [ $TGT_PAGE_HEIGHT -eq 0 ]
then then
TGT_PAGE_HEIGHT=$(($SRC_PAGE_HEIGHT * 3)) TGT_PAGE_HEIGHT=$(dc -e "$SRC_PAGE_HEIGHT 3 * p")
fi fi
if [ $(echo "$TGT_PAGE_WIDTH == 0" | bc) -eq 1 ] if [ $TGT_PAGE_WIDTH -eq 0 ]
then then
TGT_PAGE_WIDTH=$(($SRC_PAGE_WIDTH * 4)) TGT_PAGE_WIDTH=$(dc -e "$SRC_PAGE_WIDTH 4 * p")
fi fi
# do the rearranging # do the rearranging
k=$i; n=1; total=$(($SECT_TYPE * 24)); m=$total k=$i; n=1; total=$(dc -e "$SECT_TYPE 24 * p"); m=$total
while [ $n -le $SECT_TYPE ] while [ $n -le $SECT_TYPE ]
do do
pdftk "$NEW_FILE_NAME" cat `expr $k + 8` \ pdftk "$NEW_FILE_NAME" cat `expr $k + 8` \
@ -605,22 +618,32 @@ echo '\\begin{document}' >> ${OUTFILE_NAME%.pdf}.tex
echo "\includepdf[nup=$NUP,pages=-,turn=false,columnstrict, echo "\includepdf[nup=$NUP,pages=-,turn=false,columnstrict,
noautoscale,delta=${HORIZ_DELTA}bp ${VERT_DELTA}bp, noautoscale,delta=${HORIZ_DELTA}bp ${VERT_DELTA}bp,
offset=${HORIZ_OFFSET}bp ${VERT_OFFSET}bp,scale=${SCALE}] offset=${HORIZ_OFFSET}bp ${VERT_OFFSET}bp,scale=${SCALE}]
{./"$NEW_FILE_NAME"}\end{document}" >> ${OUTFILE_NAME%.pdf}.tex {./"$NEW_FILE_NAME"}" >> ${OUTFILE_NAME%.pdf}.tex
echo "\\\\end{document}" >> ${OUTFILE_NAME%.pdf}.tex
if [ $VERBOSE -eq 1 ] if [ $VERBOSE -eq 1 ]
then then
echo "IMPOSING sections..." echo "IMPOSING sections..."
fi fi
pdflatex ${OUTFILE_NAME%.pdf}.tex #> /dev/null 2>&1 pdflatex ${OUTFILE_NAME%.pdf}.tex > /dev/null 2>&1
if [ $VERBOSE -eq 1 ] if [ $VERBOSE -eq 1 ]
then then
echo "CLEANING up..." echo "CLEANING up..."
fi fi
rm ${OUTFILE_NAME%.pdf}.tex ${OUTFILE_NAME%.pdf}.aux \ rm ${OUTFILE_NAME%.pdf}.tex ${OUTFILE_NAME%.pdf}.aux \
${OUTFILE_NAME%.pdf}.log "$NEW_FILE_NAME" ${OUTFILE_NAME%.pdf}.log "$NEW_FILE_NAME"
if [ -e "./blank.tex" ]; then
rm ./blank.tex
fi
if [ -e "./blank.aux" ]; then
rm ./blank.aux
fi
if [ -e "./blank.log" ]; then
rm ./blank.log
fi
if [ $VERBOSE -eq 1 ] if [ $VERBOSE -eq 1 ]
then then
echo "`expr $NUM_PAGES + $NUM_BLANKS` pages imposed in echo "`expr $NUM_PAGES + $NUM_BLANKS` pages imposed in
$(($NUM_SIGS * $SECT_TYPE)) $SIG_TYPE signatures gathered $(dc -e "$NUM_SIGS $SECT_TYPE * p") $SIG_TYPE signatures gathered
in sections of $SECT_TYPE signature(s) each and output to in sections of $SECT_TYPE signature(s) each and output to
${OUTFILE_NAME%.pdf}.pdf." ${OUTFILE_NAME%.pdf}.pdf."
fi fi