"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "extra/opt-pdf.in" between
littleutils-1.2.3.tar.lz and littleutils-1.2.4.tar.lz

About: littleutils are a collection of small and simple utilities (rename files, search for duplicate files, ...).

opt-pdf.in  (littleutils-1.2.3.tar.lz):opt-pdf.in  (littleutils-1.2.4.tar.lz)
#! PROGDASH #! PROGBASH
# vim: set filetype=sh: # vim: set filetype=bash:
# opt-pdf: Recompresses .pdf files using Ghostscript or Poppler # opt-pdf: Recompresses .pdf files using Ghostscript or Poppler
# Copyright (C) 2013-2020 by Brian Lindholm. This file is part of the # Copyright (C) 2013-2021 by Brian Lindholm. This file is part of the
# littleutils utility set. # littleutils utility set.
# #
# The opt-pdf utility is free software; you can redistribute it and/or modify # The opt-pdf utility is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free # it under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later version. # Software Foundation; either version 3, or (at your option) any later version.
# #
# The opt-pdf utility is distributed in the hope that it will be useful, but # The opt-pdf utility is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. # more details.
# #
# You should have received a copy of the GNU General Public License along with # You should have received a copy of the GNU General Public License along with
# the littleutils. If not, see <https://www.gnu.org/licenses/>. # the littleutils. If not, see <https://www.gnu.org/licenses/>.
# get a valid temporary directory and set up traps
TMPWILD=`tempname -w opt-pdf_$$` || exit 99
trap 'rm -f ${TMPWILD} ; exit 1' 1 2 3 13 15
trap 'rm -f ${TMPWILD} ; exit 0' 0
# get command-line options # get command-line options
BACKUP='n' BACKUP='n'
COLORDPI='' COLORDPI=''
FORCE='n' FORCE='n'
GRAYDPI='' GRAYDPI=''
MONODPI='' MONODPI=''
QUIETFLAG='-q' QUIETFLAG='-q'
TOUCH='n' TOUCH='n'
VERBOSE='y' VERBOSE='y'
WEBOPT='n' WEBOPT='n'
skipping to change at line 60 skipping to change at line 55
q) VERBOSE='n' ;; q) VERBOSE='n' ;;
t) TOUCH='y' ;; t) TOUCH='y' ;;
v) QUIETFLAG='' ;; v) QUIETFLAG='' ;;
*) echo 'opt-pdf LU_VERSION' *) echo 'opt-pdf LU_VERSION'
echo 'usage: opt-pdf [-b(ackup)] [-c(olor) DPI] [-f(orce_overwrite)]' echo 'usage: opt-pdf [-b(ackup)] [-c(olor) DPI] [-f(orce_overwrite)]'
echo ' [-g(rayscale) DPI] [-h(elp)] [-m(onochrome) DPI] [-t(ouch) ]' echo ' [-g(rayscale) DPI] [-h(elp)] [-m(onochrome) DPI] [-t(ouch) ]'
echo ' [-o(ptimize_for_web)] [-q(uiet)] [-v(erbose)] PDF_filename ...' echo ' [-o(ptimize_for_web)] [-q(uiet)] [-v(erbose)] PDF_filename ...'
exit 1 ;; exit 1 ;;
esac esac
done done
shift `expr ${OPTIND} - 1` shift $((${OPTIND}-1))
# set up traps
trap 'rm -f $TMPPDF0 $TMPPDF1 $TMPPDF2 ; exit 1' 1 2 3 13 15
# handle special PDF options # handle special PDF options
GSVER=`gs --version | sed -e 's/\.//' -e 's/\..*$//'` declare -i GSVER=$(gs --version | sed -e 's/\.//' -e 's/\..*$//')
if [ "$WEBOPT" = 'y' ]; then if [ "$WEBOPT" = 'y' ]; then
if [ "$GSVER" -ge 950 ]; then if [ $GSVER -ge 950 ]; then
PDFOPT='-dFastWebView=true -c 30000000 setvmthreshold' PDFOPT='-dFastWebView=true -c 33550336 setvmthreshold'
elif [ "$GSVER" -ge 907 ]; then elif [ $GSVER -ge 907 ]; then
PDFOPT='-dFastWebView=true -c .setpdfwrite' PDFOPT='-dFastWebView=true -c .setpdfwrite'
else else
PDFOPT='-c .setpdfwrite -f pdfopt.ps' PDFOPT='-c .setpdfwrite -f pdfopt.ps'
fi fi
else else
if [ "$GSVER" -ge 950 ]; then if [ $GSVER -ge 950 ]; then
PDFOPT='-c 30000000 setvmthreshold' PDFOPT='-c 33550336 setvmthreshold'
else else
PDFOPT='-c .setpdfwrite' PDFOPT='-c .setpdfwrite'
fi fi
fi fi
# run through files # run through files
declare -i S0=0 S1=0 S2=0
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
# make sure we can read and modify file # make sure we can read and modify file
if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then if [ ! -f "$1" -o ! -r "$1" -o ! -w "$1" ]; then
echo "opt-pdf warning: $1 is not a writeable non-directory file" echo "opt-pdf warning: $1 is not a writeable non-directory file"
shift; continue shift; continue
fi fi
# make sure it's a PDF # make sure it's a PDF
file "$1" | grep -q 'PDF document' file "$1" | grep -q 'PDF document'
OUT="$?" if [ $? -ne 0 ]; then
if [ "$OUT" -ne 0 ]; then
echo "opt-pdf warning: $1 is not a PDF" echo "opt-pdf warning: $1 is not a PDF"
shift; continue shift; continue
fi fi
# skip already-processed files # skip already-processed files
if [ "$FORCE" = 'n' ]; then if [ "$FORCE" = 'n' ]; then
command -v pdfinfo > /dev/null command -v pdfinfo > /dev/null
if [ "$?" = '0' ]; then if [ $? -eq 0 ]; then
TMPPDF0=`tempname opt-pdf_$$` || exit 99 TMPPDF0=$(tempname opt-pdf_$$) || exit 99
pdfinfo "$1" > "$TMPPDF0" pdfinfo "$1" > $TMPPDF0
grep Producer "$TMPPDF0" | grep -q Ghostscript grep Producer $TMPPDF0 | grep -q Ghostscript
if [ "$?" = '0' ]; then if [ $? -eq 0 ]; then
echo "opt-pdf message: skipping ghostscript-processed $1" echo "opt-pdf message: skipping ghostscript-processed $1"
rm -f ${TMPPDF0} rm -f $TMPPDF0
shift; continue shift; continue
fi fi
grep Producer "$TMPPDF0" | grep -q cairo grep Producer $TMPPDF0 | grep -q cairo
if [ "$?" = '0' ]; then if [ "$?" = '0' ]; then
echo "opt-pdf message: skipping poppler-processed $1" echo "opt-pdf message: skipping poppler-processed $1"
rm -f ${TMPPDF0} rm -f $TMPPDF0
shift; continue shift; continue
fi fi
fi fi
fi fi
# run through ghostscript # run through ghostscript
TMPPDF1=`tempname -s .pdf opt-pdf_$$` || exit 99 TMPPDF1=$(tempname -s .pdf opt-pdf_$$) || exit 99
gs ${QUIETFLAG} -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dCompatibilityLev el=1.4 \ gs ${QUIETFLAG} -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dCompatibilityLev el=1.4 \
-dDetectDuplicateImages=true -sBandListStorage=memory -dSubsetFonts=true \ -dDetectDuplicateImages=true -sBandListStorage=memory -dSubsetFonts=true \
-dCompressFonts=true ${COLORDPI} ${GRAYDPI} ${MONODPI} \ -dCompressFonts=true $COLORDPI $GRAYDPI $MONODPI -sOutputFile=$TMPPDF1 $PDFO
-sOutputFile="$TMPPDF1" ${PDFOPT} -f "$1" PT -f "$1"
OUT="$?" if [ $? -eq 0 ]; then
if [ "$OUT" -eq 0 ]; then chmod --reference="$1" $TMPPDF1
chmod --reference="$1" "$TMPPDF1"
if [ "$TOUCH" = 'y' ]; then if [ "$TOUCH" = 'y' ]; then
touch -r "$1" ${TMPPDF1} touch -r "$1" $TMPPDF1
fi fi
S1=`filesize "$TMPPDF1"` S1=$(filesize $TMPPDF1)
GHOSTGOOD='y' GHOSTGOOD='y'
else else
echo "opt-pdf warning: ghostscript failed to process $1: gs rc = ${OUT}" echo "opt-pdf warning: ghostscript failed to process $1: gs rc = $?"
rm -f ${TMPPDF1} rm -f $TMPPDF1
S1="$(((1<<63)-1))" # largest integer S1=$(((1<<63)-1)) # largest integer
GHOSTGOOD='n' GHOSTGOOD='n'
fi fi
# run through poppler # run through poppler
TMPPDF2=`tempname -s .pdf opt-pdf_$$` || exit 99 TMPPDF2=$(tempname -s .pdf opt-pdf_$$) || exit 99
pdftocairo ${QUIETFLAG} -pdf "$1" "$TMPPDF2" pdftocairo ${QUIETFLAG} -pdf "$1" $TMPPDF2
OUT="$?" if [ $? -eq 0 ]; then
if [ "$OUT" -eq 0 ]; then chmod --reference="$1" $TMPPDF2
chmod --reference="$1" "$TMPPDF2"
if [ "$TOUCH" = 'y' ]; then if [ "$TOUCH" = 'y' ]; then
touch -r "$1" ${TMPPDF2} touch -r "$1" $TMPPDF2
fi fi
S2=`filesize "$TMPPDF2"` S2=$(filesize $TMPPDF2)
POPPLERGOOD='y' POPPLERGOOD='y'
else else
echo "opt-pdf warning: poppler failed to process $1: pdftocairo rc = ${OUT}" echo "opt-pdf warning: poppler failed to process $1: pdftocairo rc = $?"
rm -f ${TMPPDF2} rm -f $TMPPDF2
POPPLERGOOD='n' POPPLERGOOD='n'
S2="$(((1<<63)-1))" # largest integer S2=$(((1<<63)-1)) # largest integer
fi fi
# see if we have any results to use # see if we have any results to use
if [ "$GHOSTGOOD" = 'n' -a "$POPPLERGOOD" = 'n' ]; then if [ "$GHOSTGOOD" = 'n' -a "$POPPLERGOOD" = 'n' ]; then
echo "opt-pdf error: both ghostscript and poppler failed; skipping..." echo "opt-pdf error: both ghostscript and poppler failed; skipping..."
shift; continue shift; continue
fi fi
# ensure that new file is smaller # ensure that new file is smaller
S0=`filesize "$1"` S0=$(filesize "$1")
if [ "$GHOSTGOOD" = 'n' -o "$S2" -lt "$S1" ]; then if [ "$GHOSTGOOD" = 'n' -o $S2 -lt $S1 ]; then
if [ "$S2" -lt "$S0" -o "$FORCE" = 'y' ]; then if [ $S2 -lt $S0 -o "$FORCE" = 'y' ]; then
if [ "$BACKUP" = 'y' ]; then if [ "$BACKUP" = 'y' ]; then
mv "$1" "${1}.bak" mv "$1" "${1}.bak"
fi fi
cp --preserve=timestamps ${TMPPDF2} "$1" cp --preserve=timestamps $TMPPDF2 "$1"
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: using poppler - ${S0} vs. ${S2}" echo "$1: using poppler - $S0 vs. $S2"
fi fi
elif [ "$VERBOSE" = 'y' ]; then elif [ "$VERBOSE" = 'y' ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
else else
if [ "$S1" -lt "$S0" -o "$FORCE" = 'y' ]; then if [ $S1 -lt $S0 -o "$FORCE" = 'y' ]; then
if [ "$BACKUP" = 'y' ]; then if [ "$BACKUP" = 'y' ]; then
mv "$1" "${1}.bak" mv "$1" "${1}.bak"
fi fi
cp --preserve=timestamps ${TMPPDF1} "$1" cp --preserve=timestamps $TMPPDF1 "$1"
if [ "$VERBOSE" = 'y' ]; then if [ "$VERBOSE" = 'y' ]; then
echo "$1: using ghostscript - ${S0} vs. ${S1}" echo "$1: using ghostscript - $S0 vs. $S1"
fi fi
elif [ "$VERBOSE" = 'y' ]; then elif [ "$VERBOSE" = 'y' ]; then
echo "$1: unchanged" echo "$1: unchanged"
fi fi
fi fi
# clean up afterwards # clean up afterwards
rm -f ${TMPPDF1} ${TMPPDF2} rm -f $TMPPDF1 $TMPPDF2
shift shift
done done
rm -f ${TMPWILD}
 End of changes. 31 change blocks. 
55 lines changed or deleted 51 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)