Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

setpagedevice based on file contents

Status
Not open for further replies.

tgreer

Programmer
Oct 4, 2002
1,781
US
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.

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
 
Attempt Two, addresses multiple pages by triggering on "showpage". Takes into account that showpage may not be on a line by itself.

Code:
%!PS

/storageArray 120 array def
/indexer 0 def
/bufferString 256 string def
/preProcess
{ 
  { currentfile bufferString readline exch
    dup length string copy
    dup % copy for showpage
    
    ((keyword)) search
    { pop pop pop
      <</PageSize [306 792]>> setpagedevice
    }
    { pop 
    } ifelse
    
    (showpage) search
    { 
      storageArray
      { cvx exec
      } forall

      3 {cvx exec} repeat
      
      <</PageSize [612 792]>> setpagedevice
      /indexer 0 def
      /storageArray 120 array def
    }
    { storageArray indexer 3 -1 roll put
      /indexer indexer 1 add def
    } ifelse

    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

/Courier 24 selectfont
10 600 moveto (This is a test for Page Two) show showpage
%original_program_END
%********************

This is still just academic. With "real" PostScript programs, you still have the problem of operator redefinition. You may not, in other words, be able to search on "showpage".



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Modified the forall loop. No need to process all the null entries:

Code:
  storageArray
  { dup type (nulltype) eq {pop exit} if
    cvx exec
  } forall

This might be useful code, in fact. Since all strings are stored, one could process comments instead of triggering on strings or operators. Then, make setpagedevice calls (or any other code) based upon comments in the file.



Thomas D. Greer
Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top