2

In a sense, I'm asking how psnup works.

Say I have an existing document that follows the document structuring conventions. I can scan it, pull out the prolog, trailer, and individual pages. I want to then embed selected pages into the output of another document, subject to scaling and translation. I've tried following the conventions for encapsulated post script, but those conventions assume the source material follows the right rules.

I've tried doing this:

Define these in the prolog:

/BeginEPSF {
  /b4_inc_state save def            % save state for cleanup
  /dict_count countdictstack def    % count objects on dict stack
  /op_count count 1 sub def         % count objects on operand stack
  userdict begin                    % push userdict on dict stack
  /showpage { } def                 % redefined showpage
  0 setgray 0 setlinecap
  1 setlinewidth 0 setlinejoin
  10 setmiterlimit [] 0 setdash newpath
  /languagelevel where
  {pop languagelevel
  1 ne
    {false setstrokeadjust false setoverprint
    } if
  } if
} bind def


/EndEPSF {
  count op_count sub {pop} repeat   % clean up stacks
  countdictstack dict_count sub {end} repeat
  b4_inc_state restore
} bind def

Then in one of my output pages I do something like this:

%%Page: "1" 1
BeginEPSF
gsave
0.00000 49.76471 translate
0.64706 0.64706 scale

(copy one page from source document, discarding its leading %%Page)

grestore
EndEPSF
showpage

and yet the embedded page renders unchanged in the output document. Something in the embedded page overrides my BeginEPSF function.

I've compared and confirmed that both my code and psnup copy the page contents unchanged (except for removing the %%Page comment), so it's something in the wrapping code.

Rather than spending several days trying to understand the psnup code, I was wondering if there was a document, similar to the EPS specification, that says how this should be done.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112
  • Its not really possible to tell without seeing both the original program and your 'converted to EPS' program. Remember PostScript is a programming language so what happens when you insert a chunk of program inside another program isn't always obvious. Comments, however, are ignored so it won't be those causing the problem. Most likely the 'EPS' program uses operators directly from systemdict rather than your redefined ones in userdict but there could be many reasons. – KenS Aug 30 '23 at 10:39
  • Well, that's sort of the problem. Given _any_ postscript file that follows the DSC conventions (which nominally allow you to extract individual pages from the document), I want to embed those pages inside a new postscript document that I'm generating. The `psnup` program is an example of this, as it can supposedly take any input postscript file and generate an output file that contains several input pages in one output page. – Edward Falk Sep 01 '23 at 02:01
  • I cobbled together a quick-n-dirty program that does what I want, if you want to take a look: https://github.com/efalk/Tools/blob/master/ps2book.py – Edward Falk Sep 01 '23 at 02:03
  • (Sigh. Looking at the code I wrote, it needs some work. Please don't judge me by something I threw together in a hurry. ) – Edward Falk Sep 01 '23 at 02:13
  • Sorry I'm currently away from my desk on vacation and can't look at this, I promise not to judge but it'll be next week before I can comment further. – KenS Sep 01 '23 at 17:02

1 Answers1

2

Not the final answer, but I solved it by copying the prolog code from psnup and modifying as I needed.

Leaving the question open for now because I was hoping to find an official "canonical" answer, rather than copy-pasting code I don't really understand.

Edward Falk
  • 9,991
  • 11
  • 77
  • 112