This is a public experiment, inspired by a question in this forum: thread280-869949.
The issue with setpagedevice is that it implicitly performs an "initgraphics" and an "erasepage". This makes sense, seeing that you are about to pass instructions to the device specific to a page, the device can't be in the middle of marking that page.
This presents a challenge if you want to perform a setpagedevice conditionally. Your condition may not arise until late in the page, when it's too late to use setpagedevice.
Here is my first attempt at a "pure PostScript" solution.
The code at the top of the program defines a procedure. The procedure is called just prior to the start of the original program.
The procedure reads the currentfile, one line at a time, into a string. The strings are stored in an array. Each string is examined to see if it contains the keyword. If so, the procedure performs a setpagedevice. This is safe, because we haven't performed any path or marking operations yet.
When the currentfile is exhausted, the procedure loops through the array, executing each string.
Problems:
1) How large do we make our array?
2) How large do we make our buffer string?
3) What happens with binary data?
4) How do we expand this to work with multiple pages?
a. we'll need to add a search for "showpage", perhaps. At each showpage, execute the array, reset things, then call the procedure recursively?
b. can we split the file, maybe with SubFileDecode? I don't like that, we want something that works by prepending code only. If we could insert code wherever we want, then we really don't have a problem in the first place, do we?
c. could we use /BeginPage /EndPage procedures somehow?
I'll post this with an invitation to comment, add code, make objections, etc.
Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
The issue with setpagedevice is that it implicitly performs an "initgraphics" and an "erasepage". This makes sense, seeing that you are about to pass instructions to the device specific to a page, the device can't be in the middle of marking that page.
This presents a challenge if you want to perform a setpagedevice conditionally. Your condition may not arise until late in the page, when it's too late to use setpagedevice.
Here is my first attempt at a "pure PostScript" solution.
Code:
%!PS
/storageArray 120 array def
/indexer 0 def
/bufferString 256 string def
/preProcess
{
{ currentfile bufferString readline exch
dup length string copy
dup ((keyword)) search
{ pop pop pop
<</PageSize [306 792]>> setpagedevice
}
{ pop
} ifelse
storageArray indexer 3 -1 roll put
/indexer indexer 1 add def
not {exit} if
} loop
storageArray
{ cvx exec
} forall
} bind def
preProcess
%********************
%original_program_BEG
%!PS
/Courier 24 selectfont
10 600 moveto (This is a test) show
10 500 moveto (keyword) show
showpage
%original_program_END
%********************
The code at the top of the program defines a procedure. The procedure is called just prior to the start of the original program.
The procedure reads the currentfile, one line at a time, into a string. The strings are stored in an array. Each string is examined to see if it contains the keyword. If so, the procedure performs a setpagedevice. This is safe, because we haven't performed any path or marking operations yet.
When the currentfile is exhausted, the procedure loops through the array, executing each string.
Problems:
1) How large do we make our array?
2) How large do we make our buffer string?
3) What happens with binary data?
4) How do we expand this to work with multiple pages?
a. we'll need to add a search for "showpage", perhaps. At each showpage, execute the array, reset things, then call the procedure recursively?
b. can we split the file, maybe with SubFileDecode? I don't like that, we want something that works by prepending code only. If we could insert code wherever we want, then we really don't have a problem in the first place, do we?
c. could we use /BeginPage /EndPage procedures somehow?
I'll post this with an invitation to comment, add code, make objections, etc.
Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting