/*
 * $Id: corel.trm,v 1.7 1995/12/20 21:47:42 drd Exp $
 */

/*
   corel.trm

   A modified ai.trm for CorelDraw import filters
   by Chris Parks, parks@physics.purdue.edu
   Import from CorelDraw with the CorelTrace filter

   syntax: set terminal default
           set terminal mode "fontname" fontsize,xsize,ysize,linewidth

           mode= color or monochrome             (default=mono)
           "fontname"= postscript font name      (default="SwitzerlandLight")
           fontsize  = size of font in points    (default=22pt)
           xsize     = width of page in inches   (default=8.2in)
           ysize     = height of page in inches  (default=10in)
           linewidth = width of lines in points  (default=1.2pt)

*/

/* 
 * adapted to the new terminal layout by Stefan Bodewig (Dec. 1995)
 */

#ifndef GOT_DRIVER_H
#include "driver.h"
#endif

#ifdef TERM_REGISTER
register_term(corel)
#endif

#ifdef TERM_PROTO
TERM_PUBLIC void COREL_options __P((void));
TERM_PUBLIC void COREL_init __P((void));
TERM_PUBLIC void COREL_graphics __P((void));
TERM_PUBLIC void COREL_text __P((void));
TERM_PUBLIC void COREL_reset __P((void));
TERM_PUBLIC void COREL_linetype __P((int linetype));
TERM_PUBLIC void COREL_move __P((unsigned int x, unsigned int y));
TERM_PUBLIC void COREL_vector __P((unsigned int x, unsigned int y));
TERM_PUBLIC void COREL_put_text __P((unsigned int x, unsigned int y, char *str));
TERM_PUBLIC int COREL_text_angle __P((int ang));
TERM_PUBLIC int COREL_justify_text __P((enum JUSTIFY mode));
#define CORELD_XMAX  5960            /* 8.2 inches wide */
#define CORELD_YMAX  7200            /* 10 inches high  */
#define CORELD_VTIC  (CORELD_YMAX/80)
#define CORELD_HTIC  (CORELD_YMAX/80)
#define CORELD_VCHAR (22*COREL_SC)      /* default is 22 point characters */
#define CORELD_HCHAR (22*COREL_SC*6/10)
#endif

#ifndef TERM_PROTO_ONLY
#ifdef TERM_BODY

/* plots for publication should be sans-serif (don't use TimesRoman) */
char corel_font[MAX_ID_LEN+1] = "SwitzerlandLight" ; /* name of font */
int corel_fontsize = 22;                            /* size of font in pts */
TBOOLEAN corel_color = FALSE;
TBOOLEAN corel_stroke = FALSE;
int corel_path_count=0;            /* count of lines in path */
int corel_ang=0;                   /* text angle */
enum JUSTIFY corel_justify=LEFT;   /* text is flush left */


/* default mode constants */
#define CORELD_XOFF  0               /* page offset in pts */
#define CORELD_YOFF  0
#define COREL_SC     (10.0)           /* scale is 1pt = 10 units */
#define CORELD_LW    (1.2*COREL_SC)  /* linewidth = 1.2 pts */

unsigned int corel_xmax=CORELD_XMAX;
unsigned int corel_ymax=CORELD_YMAX;
float corel_lw=CORELD_LW;

TERM_PUBLIC void COREL_options()
{
        struct value a;

                if (!END_OF_COMMAND && almost_equals(c_token,"def$ault")) {
                        corel_color=FALSE;
                        strcpy(corel_font,"SwitzerlandLight");
                        corel_fontsize = 22;
                        corel_lw=CORELD_LW;
        		corel_xmax=CORELD_XMAX;
		        corel_ymax=CORELD_YMAX;
			c_token++;
                }
                if (!END_OF_COMMAND && almost_equals(c_token,"mono$chrome")) {
                        corel_color=FALSE;
			c_token++;
                } else
                if (!END_OF_COMMAND && almost_equals(c_token,"color$")) {
                        corel_color=TRUE;
			c_token++;
                }
                if (!END_OF_COMMAND && isstring(c_token)) {
                  quote_str(corel_font,c_token, MAX_ID_LEN);
                  c_token++;
                }
                if (!END_OF_COMMAND) {
                  /* We have font size specified */
                  corel_fontsize = (int)real(const_express(&a));
                  c_token++;
                  term->v_char = (unsigned int)(corel_fontsize*COREL_SC);
                  term->h_char = (unsigned int)(corel_fontsize*COREL_SC*6/10);
                }
                if (!END_OF_COMMAND) {
                  corel_xmax=(unsigned int)(real(const_express(&a))*720);
                  c_token++;
                  if (!END_OF_COMMAND) {
                    corel_ymax=(unsigned int)(real(const_express(&a))*720);
                    c_token++;
                  }
                  term->xmax=corel_xmax;
                  term->ymax=corel_ymax;
                  term->v_tic=corel_ymax/80;
                  term->h_tic=corel_ymax/80;
                }
                if (!END_OF_COMMAND) {
                  corel_lw= real(const_express(&a))*COREL_SC;
                  c_token++;
                }

        sprintf(term_options,"%s \"%s\" %d,%0.1f,%0.1f,%0.1f",
                corel_color ? "color" : "monochrome",corel_font,
                corel_fontsize,corel_xmax/720.0,corel_ymax/720.0,
                corel_lw/COREL_SC);
}

TERM_PUBLIC void COREL_init()
{
	fprintf(outfile,"%%!PS-Adobe-2.0 EPSF-1.2\n");
        fprintf(outfile,"%%%%BoundingBox: %d %d %d %d\n", CORELD_XOFF,CORELD_YOFF,
                (int)((corel_xmax)/COREL_SC+0.5+CORELD_XOFF),
                (int)((corel_ymax)/COREL_SC+0.5+CORELD_YOFF) );
        fprintf(outfile,"%%%%TemplateBox: %d %d %d %d\n", CORELD_XOFF,CORELD_YOFF,
                (int)((corel_xmax)/COREL_SC+0.5+CORELD_XOFF),
                (int)((corel_ymax)/COREL_SC+0.5+CORELD_YOFF) );
	fprintf(outfile,"%%%%EndComments\n");
	fprintf(outfile,"%%%%EndProlog\n");
        fprintf(outfile,"%%%%BeginSetup\n%%%%EndSetup\n");
}


TERM_PUBLIC void COREL_graphics()
{
        corel_path_count = 0;
        corel_stroke = FALSE;
}


TERM_PUBLIC void COREL_text()
{
        if (corel_stroke) {
		fprintf(outfile,"S\n");
                corel_stroke = FALSE;
	}
        corel_path_count = 0;
}


TERM_PUBLIC void COREL_reset()
{
	fprintf(outfile,"%%%%Trailer\n");
}


TERM_PUBLIC void COREL_linetype(linetype)
int linetype;
{
        if (corel_stroke) {
		fprintf(outfile,"S\n");
                corel_stroke = FALSE;
	}
	switch(linetype) {
                case -2 : fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 0 0 1 K\n");
				}
				else {				
                                        fprintf(outfile,"[] 0 d\n");
                                        fprintf(outfile,"0 j\n0 G\n");
				}
				break;

                case -1 : fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 0 0 1 K\n");
				}
				else {				
					fprintf(outfile,"[1 2] 0 d\n");
                                        fprintf(outfile,"0 j\n0 G\n");
                                }
				break;
				
                case 0 :  fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"1 0 1 0 K\n");
				}
				else {				
					fprintf(outfile,"[] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 1 :  fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"1 1 0 0 K\n");
				}
				else {				
					fprintf(outfile,"[4 2] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 2 :  fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 1 1 0 K\n");
				}
				else {				
                                        fprintf(outfile,"[2 3] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 3 :  fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 1 0 0 K\n");
				}
				else {				
					fprintf(outfile,"[1 1.5] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 4 :  fprintf(outfile,"%f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"1 0 0 0 K\n");
                                }
				else {				
					fprintf(outfile,"[5 2 1 2] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 5 : fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 0 1 0 K\n");
				}
				else {				
					fprintf(outfile,"[4 3 1 3] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 6 : fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 0 0 1 K\n");
				}
				else {				
					fprintf(outfile,"[2 2 2 4] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 7 : fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0 0.7 1 0 K\n");
				}
				else {				
					fprintf(outfile,"[2 2 2 2 2 4] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
				
                case 8 : fprintf(outfile,"%.2f w\n",corel_lw/COREL_SC);
                                if (corel_color) {
					fprintf(outfile,"0.5 0.5 0.5 0 K\n");
				}
				else {				
					fprintf(outfile,"[2 2 2 2 2 2 2 4] 0 d\n");
                                        fprintf(outfile,"2 j\n0 G\n");
                                }
				break;
		}
        corel_path_count = 0;
}


TERM_PUBLIC void COREL_move(x,y)
unsigned int x,y;
{
        if (corel_stroke) fprintf(outfile,"S\n");
        fprintf(outfile,"%0.2f %0.2f m\n", x/COREL_SC, y/COREL_SC);
        corel_path_count += 1;
        corel_stroke = TRUE;
}


TERM_PUBLIC void COREL_vector(x,y)
unsigned int x,y;
{
        fprintf(outfile,"%.2f %.2f l\n", x/COREL_SC, y/COREL_SC);
        corel_path_count += 1;
        corel_stroke = TRUE;
        if (corel_path_count >= 400) {
                fprintf(outfile,"S\n%.2f %.2f m\n",x/COREL_SC,y/COREL_SC);
                corel_path_count = 0;
	}
}


TERM_PUBLIC void COREL_put_text(x,y,str)
unsigned int x, y;
char *str;
{
char ch;
        if (corel_stroke) {
		fprintf(outfile,"S\n");
                corel_stroke = FALSE;
	}
        switch(corel_justify) {
                case LEFT :   fprintf(outfile,"/_%s %d %d 0 0 z\n",
                                      corel_font,corel_fontsize,corel_fontsize);
			break;
                case CENTRE : fprintf(outfile,"/_%s %d %d 0 1 z\n",
                                      corel_font,corel_fontsize,corel_fontsize);
			break;
                case RIGHT :  fprintf(outfile,"/_%s %d %d 0 2 z\n",
                                      corel_font,corel_fontsize,corel_fontsize);
			break;
	}
        if (corel_ang==0) {
                fprintf(outfile,"[1 0 0 1 %.2f %.2f]e\n0 g\n",
                        x/COREL_SC,y/COREL_SC - corel_fontsize/3.0);
	}
	else {
                fprintf(outfile,"[0 1 -1 0 %.2f %.2f]e\n0 g\n",
                        x/COREL_SC - corel_fontsize/3.0,y/COREL_SC);
	}
		
	putc('(',outfile);
	ch = *str++;
	while(ch!='\0') {
		if ( (ch=='(') || (ch==')') || (ch=='\\') )
			putc('\\',outfile);
		putc(ch,outfile);
		ch = *str++;
	}
        fprintf(outfile,")t\nT\n");
        corel_path_count = 0;
}

TERM_PUBLIC int COREL_text_angle(ang)
int ang;
{
        corel_ang=ang;
	return TRUE;
}

TERM_PUBLIC int COREL_justify_text(mode)
enum JUSTIFY mode;
{
        corel_justify=mode;
	return TRUE;
}

#endif /* TERM_BODY */

#ifdef TERM_TABLE

TERM_TABLE_START(corel_driver)
    "corel","EPS format for CorelDRAW",
     CORELD_XMAX, CORELD_YMAX, CORELD_VCHAR, CORELD_HCHAR,
     CORELD_VTIC, CORELD_HTIC, COREL_options, COREL_init, COREL_reset,
     COREL_text, null_scale, COREL_graphics, COREL_move, COREL_vector,
     COREL_linetype, COREL_put_text, COREL_text_angle,
     COREL_justify_text, do_point, do_arrow, set_font_null
TERM_TABLE_END(corel_driver)

#undef LAST_TERM
#define LAST_TERM corel_driver

#endif /* TERM_TABLE */
#endif /* TERM_PROTO_ONLY */

/*
 * NAME: corel 
 *
 * OPTIONS: mode "fontname" fontsize,xsize,ysize,linewidth
 *        mode= color or monochrome             (default=mono)
 *        "fontname"= postscript font name      (default="SwitzerlandLight")
 *        fontsize  = size of font in points    (default=22pt)
 *        xsize     = width of page in inches   (default=8.2in)
 *        ysize     = height of page in inches  (default=10in)
 *        linewidth = width of lines in points  (default=1.2pt)
 *
 * SUPPORTS: CorelDraw EPS format
 *
 * Further Info: Import from CorelDraw with the CorelTrace filter.
 *
 */