[comp.lang.postscript] PostScript page reverser?

bob@MorningStar.Com (Bob Sutterfield) (10/30/90)

Some PostScript printers place their output face-down in the collector
bin, and some place it face-up.  Some producers of PostScript
documents arrange "page 1" at the beginning of the file, and some put
it at the end.  The selection is really pretty arbitrary, because
there's no way to know what sort of printer (face-up or face-down) the
recipient will have.  So it's often decided by what sort of printer
the author uses.  About half the time, this leads to wasting the
recipient's secretary's time recollating the document.  It's
mind-numbingly dull work and they have better things to do.

Adobe's TranScript package includes a program called "psrev" that can
take any format-conformant PostScript document and (re-)reverse it, or
even select page ranges for printing.  I've looked through the sources
of psroff, psf, a2ps, and groff and found nothing that looks
promising.  It's probably a quick hack (likely a Perl one-liner :-)
and one of those wheels that shouldn't be reinvented.

Has anyone written a freely available PostScript page reverser?

clewis@ecicrl.UUCP (Chris Lewis) (10/31/90)

In article <BOB.90Oct30100901@volitans.MorningStar.Com> bob@MorningStar.Com (Bob Sutterfield) writes:
|Some PostScript printers place their output face-down in the collector
|bin, and some place it face-up.

|Adobe's TranScript package includes a program called "psrev" that can
|take any format-conformant PostScript document and (re-)reverse it, or
|even select page ranges for printing.  I've looked through the sources
|of psroff, psf, a2ps, and groff and found nothing that looks
|promising.  It's probably a quick hack (likely a Perl one-liner :-)
|and one of those wheels that shouldn't be reinvented.

But you don't want to use the quick hack unless you can help it (often
an extreme memory hog).

|Has anyone written a freely available PostScript page reverser?

Yup.  Psroff of course - why did you miss it?

The psxlate utility will reverse pages provided that the document uses
the document formatting conventions at level 1 or better.  Psxlate
is intended to be sort of a start to part of a generalized spooler for
postscript.  There are stubs in it for different kinds of collating.
Psxlate has the advantage of not limiting the size of your document
(well, disk limited not memory limited).
-- 
Chris Lewis, Phone: TBA
UUCP: uunet!utai!lsuc!ecicrl!clewis
Moderator of the Ferret Mailing List (ferret-request@eci386)
Psroff mailing list (psroff-request@eci386)

aas@boeygen.nr.no (Gisle Aas) (11/01/90)

In article <BOB.90Oct30100901@volitans.MorningStar.Com> bob@MorningStar.Com (Bob Sutterfield) writes:
>   Adobe's TranScript package includes a program called "psrev" that can
>   take any format-conformant PostScript document and (re-)reverse it, or
>   even select page ranges for printing.  I've looked through the sources
>   of psroff, psf, a2ps, and groff and found nothing that looks
>   promising.  It's probably a quick hack (likely a Perl one-liner :-)
>   and one of those wheels that shouldn't be reinvented.
>
>   Has anyone written a freely available PostScript page reverser?
>

I once wrote the following perl program. (Not quite a one-line :-)

#!/usr/bin/perl
# simple psrev-clone in perl. Written by Gisle Aas, NCC, Oslo, 1990

@psfile = <>;   # slurp in the whole file

unless ($psfile[0] =~ /^%!PS-Adobe-(.*)/) {
    printf STDERR "This is not a conforming PostScript file\n";
    exit(1);
}
$version = $1;

# Locate pages
for ($i=0; $psfile[$i]; $i++) {
    if ($psfile[$i] =~ /^%%Page:(.*)(\d+)$/) {
        push(@pages,$i);
        push(@labels,$1);
    }
}
if ($#pages == 0) {
    print STDERR "This file contains no PostScript %%Page comments\n";
    exit;
}

# Locate trailer
$trailer = $#psfile;
for ($i = $#psfile; $i > $pages[$#pages]; $i--) {
    if ($psfile[$i] =~ /^%%Trailer/) {
       $trailer = $i;
       last;
    }
}

# Output prolog
for ($i=0; $i < $pages[0]; $i++) {
    print $psfile[$i];
}

# Output pages in reverse order
$pageno = 1;
$prev_lineno = $trailer;
while ($lineno = pop(@pages)) {
    printf "%%%%Page:%s%d\n",pop(@labels),$pageno++;
    for ($i = $lineno + 1; $i < $prev_lineno; $i++) {
        print $psfile[$i];
    }
    $prev_lineno = $lineno;
}

# Output trailer
for ($i = $trailer; $i <= $#psfile; $i++) {
    print $psfile[$i];
}
--
Gisle Aas               |  snail: Boks 114 Blindern, N-0314 Oslo, Norway
Norsk Regnesentral      |  X.400: G=Gisle;S=Aas;O=nr;P=uninett;C=no
voice: +47-2-453561     |  inet:  Gisle.Aas@nr.no

aas@boeygen.nr.no (Gisle Aas) (11/01/90)

In article <AAS.90Oct31191354@boeygen.nr.no> aas@boeygen.nr.no (Gisle Aas) writes:

>   I once wrote the following perl program. (Not quite a one-line :-)

Since then I have learned not to write C for loops in perl. So here
you have a rewrite of my psrev program, done the perl way.

#!/usr/local/bin/perl
# Simple psrev clone in perl. Reverses pages of conforming postscript files.
# Author: Gisle Aas, NCC, Oslo.      email: <Gisle.Aas@nr.no>

@psfile = <>;                              # Slurp in the whole file
die "This is not a conforming PostScript file"
    unless $psfile[0] =~ /^%!PS-Adobe-(.*)/;

$line = $[;                                # Locate pages
for (@psfile) {
    if (/^%%Page:(.*)(\d+)$/) {
        push(@pages,$line);
        push(@labels,$1);
    }
    $line++;
}
die "This file contains no PostScript %%Page comments\n" if $#pages < $[;

$trailer = $#psfile;                       # Locate trailer
for (reverse(@psfile)) {
    last if /^%%Trailer/;
    $trailer--;
}

print @psfile[$[..$pages[0] - 1];          # Output prolog

$pageno = 1;                               # Output pages in reverse order
$to_line = $trailer - 1;
while ($from_line = pop(@pages)) {
    printf "%%%%Page:%s%d\n", pop(@labels), $pageno++;
    print @psfile[$from_line + 1 .. $to_line];
    $to_line = $from_line - 1;
}

print @psfile[$trailer .. $#psfile];       # Output trailer
__END__



--
Gisle Aas               |  snail: Boks 114 Blindern, N-0314 Oslo, Norway
Norsk Regnesentral      |  X.400: G=Gisle;S=Aas;O=nr;P=uninett;C=no
voice: +47-2-453561     |  inet:  Gisle.Aas@nr.no

cramer@optilink.UUCP (Clayton Cramer) (11/01/90)

In article <BOB.90Oct30100901@volitans.MorningStar.Com>, bob@MorningStar.Com (Bob Sutterfield) writes:
> Adobe's TranScript package includes a program called "psrev" that can
> take any format-conformant PostScript document and (re-)reverse it, or
> even select page ranges for printing.  I've looked through the sources
> of psroff, psf, a2ps, and groff and found nothing that looks
> promising.  It's probably a quick hack (likely a Perl one-liner :-)
> and one of those wheels that shouldn't be reinvented.
> 
> Has anyone written a freely available PostScript page reverser?

Frustrating, isn't it?  I wrote a page reverser specifically
for Microsoft Word -- and there's the rub.  Unless you are capable
of interpreting PostScript, you can't write a PostScript page
reverse which is generally useful -- you end up building one for
a particular application.  Building one for a particular application
is enough work that no one does it for free -- and no one is 
going to build a program that works for any arbitrary PostScript
program and give it away.



-- 
Clayton E. Cramer {pyramid,pixar,tekbspa}!optilink!cramer
Alcohol prohibition didn't work; drug prohibition doesn't work; gun
prohibition won't work.
You must be kidding!  No company would hold opinions like mine!

bob@MorningStar.Com (Bob Sutterfield) (11/02/90)

In article <4772@optilink.UUCP> cramer@optilink.UUCP (Clayton Cramer) writes:
   Unless you are capable of interpreting PostScript, you can't write
   a PostScript page reverse which is generally useful -- you end up
   building one for a particular application.

If the PS document is conformant enough with Appendix C of the Red
Book, then it is page-reversible.  If an application produces a PS
program that is so non-conformant as not to be page-reversible, then
that application can be considered broken.

   Building one for a particular application is enough work that no
   one does it for free -- and no one is going to build a program that
   works for any arbitrary PostScript program and give it away.

I have been generously provided with several PS page reversers (thanks
to all for the pointers!) that work with conformant documents.  If an
application produces non-conformant PS documents, then its author has
the choice of either fixing the application or writing a custom
reverser and all the other necessary PS handling tools.  If the author
fixes the application, then {s}he won't have to write a custom
reverser and decide whether to give it away.

rcd@ico.isc.com (Dick Dunn) (11/02/90)

cramer@optilink.UUCP (Clayton Cramer) writes:
> bob@MorningStar.Com (Bob Sutterfield) writes:
> > Adobe's TranScript package includes a program called "psrev"...
> >...Has anyone written a freely available PostScript page reverser?

> Frustrating, isn't it?  I wrote a page reverser specifically
> for Microsoft Word -- and there's the rub.  Unless you are capable
> of interpreting PostScript, you can't write a PostScript page
> reverse which is generally useful ...

Not true.  You can write a page-reverser that will work for the output of
any application that generates output conforming to the Document Struc-
turing Conventions.  (Bob's article alluded to "conformant" apps.)  In
fact, page-reversal is one of the reasons those conventions exist! More-
over, the conventions contain constraints which guarantee that you don't
have to interpret the PostScript to figure out how to reverse it.  It's
nothing more than a simple copying-about of chunks of text...a simple one
could be written in an hour or so, if you didn't mind some wasted motion.

This is just one more reason to insist that the PostScript-generating
apps you buy (or write) conform to the conventions.
-- 
Dick Dunn     rcd@ico.isc.com -or- ico!rcd       Boulder, CO   (303)449-2870
   ...but Meatball doesn't work that way!