[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: inserting one postscript file into another



>>>>> "Rogerio" == Rogerio Brito <rbrito@iname.com> writes:

    Rogerio> On Nov 09 2000, Shao Zhang wrote:
    >> Could someone give me some hints? I looked the man page for
    >> psselect and psmerge and still cannot figure it out.

    Rogerio> 	Theoretically, it would be possible if psmerge worked
    Rogerio> correctly (I unfortunately haven't got it to work as I'd
    Rogerio> like).

    Rogerio> 	Anyway, to split the first file in two files with the
    Rogerio> pages you'd like, you might try to use gv, select the
    Rogerio> pages accordingly and then save them to different files.

    Rogerio> 	Then after that, you'd have three files and you'd use
    Rogerio> psmerge to glue them together. But, as I've stated
    Rogerio> before, I couldn't get it to work.

It works OK for me...

Here is me (hacked?) script designed only to print pages that have
changed since the last time I printed the document. I will include it
here, because it uses psselect to split up the document, removes
variable stuff that may not be relevant to the comparison (eg.
filenames), compares each page one by one, and then psmerge to merge
it back together again.

#!/bin/sh -e

# (C)opyright 2000 Brian May
# May be distributed under the terms and conditions of the GPLv2.
# Debian users: See /usr/share/common-licenses/GPL.

# known bug: script gets confused if no changes exist. This is
# because expressions like *.ps don't expand to a null list.

usage () {
  echo "diffps, find the differences between two PS files." >&2
  echo "   usage: diffps [-l|--log][-d|--doubleside] old.ps new.ps output.ps" >&2
}

######################
# PARSE COMMAND LINE #
######################

# stolen from /usr/bin/fakeroot, Debian potato version.
GETOPTEST=`getopt --version`
case $GETOPTEST in
getopt*) # GNU getopt
    TEMP=`getopt -n diffps -l log,doubleside,help -- +ldh "$@"`
    ;;
*) # POSIX getopt ?
    TEMP=`getopt dh "$@"`
    ;;
esac

if test "$?" != "0"; then
  echo Terminating...
  exit 1
fi

eval set -- "$TEMP"
while test "$1" != "--"; do
  case $1 in
    -l|--log)
      log=1;
      ;;
    -d|--doublepage)
      double=1;
      ;;
    -h|--help)
      usage;
      exit 0
      ;;
  esac
  shift
done

shift

if [ $# -lt 3 ]
then
  usage
  exit 
fi

A=$1; shift
B=$1; shift
OUT=$1; shift

####################################
# Extract all pages from a PS file #
####################################
extract() {
CASE=$1
FILE=$2
SECT=1
PAGE=1
LPAGE=0
APAGE=illegal
echo -n "Extracting $FILE to $TMPDIR/$CASE-... " >&2
while [ $APAGE ]
do
  if [ $double ]
  then
    # if double sided, try to extract two pages
    psselect $FILE -p$PAGE,$[PAGE+1] -q > $TMPDIR/new.ps
  else
    # otherwise, extract one page
    psselect $FILE -p$PAGE -q > $TMPDIR/new.ps
  fi
 
  # get the internal page number
  APAGE=`awk '/%%Page: / {print $2; exit}' < $TMPDIR/new.ps`

  if [ -z $APAGE ]
  then
    # if no internal page number exists, then stop
    rm $TMPDIR/new.ps
  else
    # otherwise save the page

    if [ $APAGE -le $LPAGE ]
    then
      # if numbers overlap, then increment section number
      SECT=$[$SECT + 1]
    fi
    LPAGE=$APAGE

    # only save the PS file if double sided and this is an odd page...
    ODDPAGE=$[PAGE % 2]
    if [ "$double" -a $ODDPAGE -eq 1 ]
    then
      # remove variables and save
      grep -v -E '^%(DVIPSSource|%Creator|%CreationDate|%Title|DVIPSCommandLine):' < $TMPDIR/new.ps \
      | sed 's/^\(TeXDict begin .*\) (.*\.dvi)/\1/' \
      | awk '/^%DVIPSBitmapFont/ { x=1; } {if (!x) print } /%EndDVIPSBitmapFont/ { x=0; }' > $TMPDIR/$CASE-$PAGE.ps
    fi
    # ...or if not doubled sided.
    if [ -z $double ]
    then
      # remove variables and save
      grep -v -E '^%(DVIPSSource|%Creator|%CreationDate|%Title|DVIPSCommandLine):' < $TMPDIR/new.ps \
      | sed 's/^\(TeXDict begin .*\) (.*\.dvi)/\1/' \
      | awk '/^%DVIPSBitmapFont/ { x=1; } {if (!x) print } /%EndDVIPSBitmapFont/ { x=0; }' > $TMPDIR/$CASE-$PAGE.ps
    fi

    # try an get date from the PS file
    awk '/%DVIPSSource: / {split($4,A,/:/); printf("%s",A[1]); exit}' < $TMPDIR/new.ps > $TMPDIR/$CASE-$PAGE.ps.date

    # save some other details
    echo -n "$PAGE" > $TMPDIR/$CASE-$PAGE.ps.ppage
    echo -n "$SECT-$APAGE" > $TMPDIR/$CASE-$PAGE.ps.apage

    # delete original
    rm $TMPDIR/new.ps
  fi

  #increment page count
  PAGE=$[$PAGE + 1]
done
echo "done." >&2
}

#########################
# create temp directory #
#########################
TMPDIR=/tmp/$$
trap "rm -rf $TMPDIR" EXIT
mkdir $TMPDIR

# extract both files.
extract A $A
extract B $B

############################################
# delete pages in B that are the same in A #
############################################
echo -n "Finding similar pages... " >&2
for bname in $TMPDIR/B-*.ps
do
  aname=`echo $bname | sed 's/\/B-/\/A-/'`

  if [ ! -f $aname ];
  then
  	echo -n "" >&2
  elif diff -q $aname $bname > /dev/null;
  then
	# files are the same, delete
	echo -n "" >&2
	rm $bname
  else
	#debug
	#echo $aname $bname
	#diff $aname $bname || true
  	echo -n "" >&2
  fi
done
echo "done." >&2

########################################
# get list of pages that are different #
########################################
echo -n "Getting list of pages to extract... " >&2
for bname in $TMPDIR/B-*.ps
do
  PPAGE=`cat $bname.ppage`
  echo $PPAGE >> $TMPDIR/list
done

##################################
# sort list of pages, and record #
##################################
for PPAGE in `sort -n < $TMPDIR/list`
do
  # save current page
  bname="$TMPDIR/B-$PPAGE.ps"
  LIST="$LIST$PPAGE,"
  if [ $log ]
  then
    cat $bname.apage
    echo -n " "
    cat $bname.date
    echo ""
  fi
  rm $bname
  rm $bname.apage
  rm $bname.ppage
  rm $bname.date

  # if double sided, also do the next page
  PPAGE=$[$PPAGE + 1]
  bname="$TMPDIR/B-$PPAGE.ps"
  # ... but only if this page exists
  if [ "$double" -a -f $bname.apage ]
  then
    LIST="$LIST$PPAGE,"
    if [ "$log" ]
    then
        cat $bname.apage
	echo -n " "
	cat $bname.date
	echo ""
    fi
    rm $bname.apage
    rm $bname.ppage
    rm $bname.date
  fi
done	
echo "done." >&2

##########################
# extract required pages #
##########################
echo -n "Extracting pages from $B to $OUT... " >&2
psselect -p${LIST}999 -q $B $OUT
echo "done." >&2
Sometimes, believe it or not, it works!!!!!!

I use this script with another script that attempts to compile the
LaTeX document with change-bars (actually this is somewhat broken,
because LaTeX wasn't designed with change-bars in mind), and keeps a
record of when the most up-to-date page was printed.

Improvements welcome ;-).
-- 
Brian May <bam@debian.org>

Reply to: