/*
 *
 *                        viewport.c
 *
 *
 * This procedure sets up the variables for the translation of plotter
 * coordinates to PostScript coordinates.
 *
 * Note: the procedure "defaults" may be incorporated here, however
 *	I have not had the time to work it out properly.
 *
 * Don McCormick,   Marek Krawus
 */
#include "defn.h"

#define POINT(x) ((int)((x)/25.4*72.0))

viewport()
{

    float psxmax, psymax;		/* Sizes scaled to the viewport */
    float ypsoffset;			/* bottom of EPS bounding box	*/

    if (LANDSCAPE)		/* Default mode */
    {
	if (A4_OUT)  psymax = MaxA4RangeY; 
	   else      psymax = MaxA3RangeY;  
	psxmax = psymax * (maxprX - minprX) / (maxprY - minprY);
        
	if (A4_OUT) 
	{
	  xoffset = xprzes + (A4Height - psxmax) / 2.0;
	  yoffset = yprzes - (A4Width + psymax) / 2.0;
	  ypsoffset = yprzes + (A4Width - psymax) / 2.0;
        }  else
	{
	  xoffset = xprzes + (A3Height - psxmax) / 2.0;
	  yoffset = yprzes - (A3Width + psymax) / 2.0;
	  ypsoffset = yprzes + (A3Width - psymax) / 2.0;
	}
	if (ONE_TIME) {
	    if (ENCAPSULATED)
		eps_macros(POINT(ypsoffset), POINT(xoffset),
			   POINT(psymax+ypsoffset-yprzes),
			   POINT(psxmax+xoffset-xprzes));
	    fprintf(outfile, "90 rotate\n");
	}
    } else
    {
	if (A4_OUT)  psxmax = MaxA4RangeY;
	   else      psxmax = MaxA3RangeY; 
	psymax = psxmax * (maxprY - minprY) / (maxprX - minprX);

	if (A4_OUT) 
	{
	  xoffset = xprzes + (A4Width - psxmax) / 2.0;
	  yoffset = yprzes + (A4Height - psymax) / 2.0;
        }   else
	{
	  xoffset = xprzes + (A3Width - psxmax) / 2.0;
	  yoffset = yprzes + (A3Height - psymax) / 2.0;
        }
	if (ONE_TIME && ENCAPSULATED)
	    eps_macros(POINT(xoffset), POINT(yoffset),
		       POINT(psxmax+xoffset-xprzes),
		       POINT(psymax+yoffset-yprzes+1));
    }

    if (ONE_TIME) 
	 { fprintf(outfile, "%g mm %g mm translate\n", xoffset, yoffset);
	   fprintf(outfile, "defaultmtx currentmatrix /defaultmtx exch def \n");
	   fprintf(outfile, "/fullpage %g def \n", FULLPAGE );
	   fprintf(outfile, "/clipX_lo %g def \n", FULLPAGE );
	   fprintf(outfile, "/clipY_lo %g def \n", FULLPAGE );
	   fprintf(outfile, "/clipX_up %g def \n", FULLPAGE );
	   fprintf(outfile, "/clipY_up %g def \n", FULLPAGE );
	   ONE_TIME = FALSE;
         }

    XSCALE_ABS = XSCALE = psxmax / maxprX * SCALE;
    YSCALE_ABS = YSCALE = psymax / maxprY * SCALE;

    Xshift = Yshift = 0.0;
}
