Fanning Software Consulting

Correct PostScript Landscape Orientation

QUESTION: Every time I create IDL PostScript landscape oriented output, it appears in my PostScript viewer upside-down. Has no one told ITTVIS that this is seascape output? That landscape output should be rotated 180 degrees from this?

ANSWER: Yes, I think it has been mentioned on several occasions, but it hasn't seemed to do any good. This is a long-standing annoyance with landscape output in IDL. I have begged for some time now for a SEASCAPE keyword that would produce real landscape output, if, in fact, ITTVIS has grave reservations about correcting the LANDSCAPE keyword. As weird as that would be, I could get used to it, I think.

But, in any case, help may be on the way. One of my colleagues, Scott Lewis, has found a web page that has given us the clue we needed to know how to manually edit these upside down PostScript files and get them oriented the way most people expect them to be oriented.

The first thing you need to do is locate the %%Page: line in the file. Normally, in an IDL file this will be just after the PostScript prolog in the file (bounded by %%BeginProlog and %%EndProlog in the file). This line is often labelled something like %%Page: 0 1. What you do next depends on what kind of PostScript page you are using.

For an American 8.5 x 11 inch sheet of paper, add the following line after the %%Page: line in the file:

    180 rotate -612 -792 translate 

If you are using an A4 European sized sheet of paper, use this line instead.

    180 rotate -595.28 -841.89 translate 

The two numbers after the "rotate" in the above lines are the page size in PostScript units, minus the unwriteable quarter inch border around the PostScript page.

Next, read a couple of lines down from where you just inserted the above line, and find the bounding box line, which is indicated by the %%BoundingBox: tag at the start of the line. You will want to modify the four values you find there. These values represent the X and Y coordinates at the lower-left corner of the bounding box, and the X and Y coordinates of the upper-right corner of the bounding box, in order from left to right. In pseudo code, the line will read like this.

    %%BoundingBox: ll_x, ll_y, ur_x, ur_y 

You want to change these values, using the PostScript page size values from above. So again, this will depend on the size of your PostScript page. Here is how you will make the change.

    new_ll_x --> 612 (or 595.28) - ur_x
   new_ll_y --> 792 (or 841.89) - ur_y
   new_ur_x --> Round(612 (or 595.28) - new_ll_x)
   new_ur_y --> Round(792 (or 841.89) - new_ll_y) 

To give you this in numbers, if the bounding box line looks like this (assuming an 8.5 x 11 sheet of American paper):

   %%BoundingBox: 30 51 580 739 

After correction, it will look like this.

  %%BoundingBox: 32 53 580 739 


Note that if your file contains more than one page of PostScript output, you will have to locate each %%Page line and add the appropriate rotate and translate line (the first line you inserted above) after it.

Also note that these fixes to not apply to any PostScript page that has embedded PostScript files (such as a PostScript figure) inside it. Normally embedded files are identified by matching %%BeginDocument and %%EndDocument tags.

Program to Fix IDL PostScript Landscape Files

As an aid to fixing PostScript landscape files created in IDL, I have written a program, named cgFixPS, that will modify the PostScript file in the manner outlined above. Both an input and output filename can be given, if you prefer to save a copy of the changed file, like this:

    IDL> cgFixPS, '', '' 

If you are using an A4 size page, set the A4 keyword like this.

    IDL> cgFixPS, '', '', /A4 

If you prefer to overwrite the input file, simply provide no output filename.

    IDL> cgFixPS, '' 

If no input filename is provided, the user will be asked to select one, and that input file will be overwritten.

    IDL> cgFixPS 

This program fixes single and multiple page PostScript output, and it will check to see if a file is truly in landscape orientation before modifying the file. Files that have been previously modified are also not modified a second time.

The cgFixPS program is compatible with other PostScript utilities in the Coyote Library, which means that if you set-up and close your PostScript device with the cgPS_Open and cgPS_Close utilities, your landscape PostScript files will be fixed automatically. For example, this code will automatically create landscape output in the proper right-side-up orientation.

   IDL> Histoplot, Loaddata(7), /FILL    IDL> cgPS_Close 

Version of IDL used to prepare this article: IDL 7.0.3.

Web Coyote's Guide to IDL Programming