Search code examples
ghostscriptpostscript

Postscript layout from top to bottom


Hi I have a postscript file that layouts an image from left to right.

%%% Temporary
/Fix_long 4.2 cm def
/Fix_short 3.2 cm def
%%% Set Image Scale
/SetFixScale { 2 copy gt { Fix_long Fix_short }{ Fix_short Fix_long  }ifelse scale } bind def

%%% Set put coordinate
/SetXAdjust { 2 copy gt 
{ X_Step Fix_long sub 2 div floor }
{ Fix_long Fix_short sub 2 div} ifelse /XAdjust exch def 
} bind def
 /YAdjust 1.0 cm def
%%% Temporary
/Row 4 def
/Column 5 def
/X_Step urx llx sub Row div floor def
/Y_Step ury lly sub Column div floor  def
/Row_pos 0 def
/Column_pos 1 def
/SetPutPosition { 
llx X_Step Row_pos mul add 
ury Y_Step Column_pos mul sub translate
DrawFrame
DrawFileName
XAdjust YAdjust translate
Row 1 sub Row_pos eq { /Row_pos 0 def /Column_pos Column_pos 1 add def }{ /Row_pos Row_pos 1 add def } ifelse  
Column_pos Column gt { /Column_pos 1 def } if
} bind def

I tried changing the postscript to layout the images from top to bottom. I can layout the image from top to bottom but I can only put it on the first column.

/SetPutPosition { 
llx X_Step Row_pos mul add 
ury Y_Step Column_pos mul sub translate
DrawFrame
DrawFileName
XAdjust YAdjust translate
Row 1 sub Row_pos eq { /Row_pos 0 def /Column_pos Column_pos 1 add def }{ /Column_pos Column_pos 1 add def } ifelse  
Column_pos Column gt { /Row_pos 1 def } if
} bind def

Solution

  • Not all the program is given, for example llx, lly, urx and ury are not defined. SO its impossible to reproduce what you are doing.....

    The definition of rows and columns seems odd to me too, since rows increment in the x direction and columns decrement in the y direction.

    I'd assume that the program operates in a loop (also not shown). On each iteration of the loop it sets the position on the page to:

    x = llx + (X_Step * Row_pos) y = ury - (Y_Step * Column_pos)

    Then program then subtracts 1 from Row and compares with Row_pos. If they are the same then we reset the row, otherwise we add 1 to Row_pos. In effect we increment Row_pos until we get to Row - 1.

    Now, if we reset the row, then we set Row_pos back to 0, and add 1 to column_pos.

    Finally, we compare Column_pos and Column, if Column_pos is greater than Column, then we reset Column_pos to 1. Since we would have also reset Row_pos in the preceding control block, this is effectively a complete page reset, and starts again from the initial values.

    Your code starts by checking Row_pos against Row again (when you should be checking Column_pos against Column). If Row_pos hasn't reached Row then you add 1 to Column_pos. Then you check Column_pos against Column and if its greater you reset Row_pos.

    Notice that the only way you can alter Row_pos is if Row_pos is equal to Row - 1

    If it is, then you reset Row_pos to 0. After that its impossible to increment Row_pos unless Row is 1.

    Basically your logic is broken.

    You want to compare Column_pos against Column - 1. When they are equal you want to set Column_pos to 1 and increment Row_pos, otherwise you want to increment Column_pos. Finally, if Row_pos is greater than Row, you want to reset Row_pos top 0.

    So, bearing in mind thaqt I can't test this, because not all the code is present, something like :

    /SetPutPosition { 
    llx X_Step Row_pos mul add 
    ury Y_Step Column_pos mul sub translate
    DrawFrame
    DrawFileName
    XAdjust YAdjust translate
    Column 1 sub Column_pos eq { /Column_pos 1 def /Row_pos Row_pos 1 add def }{ /Column_pos Column_pos 1 add def } ifelse  
    Row_pos Row gt { /Row_pos 0 def } if
    } bind def