/*
 *
 *                 linetype.c
 *
 *
 * Sets the line type and the scaling of the line pattern 
 *
 * Don McCormick CSIRO Division of Applied Physics National Measurements
 * Laboritory Bradfield Road Lindfield 2018 N.S.W. 
 *
 * NOTE: default length for a line pattern is approx 6.5 mm on A4 size paper. 
 *
 * Marek Krawus, PCC
 */
#include "defn.h"

float   line_len;
float   linescale;
int     linestyle;
int     parts;

linetype(line)
int     line;
{
    float   down1, down2, down3;
    float   up1, up2, up3;
    float   diagonal;

    end_draw();

    if (line == LINE_TYPE_SCALE)			/* HP-GL only */
    {
	if (test_token())
	{
	    linestyle = rintx(getval());
	    if ((linestyle < -6) || (linestyle > 6))
	    {
	      if (DEBUG)
	          fprintf(stderr,
		  "ERROR: pattern number parameter in LT instruction is \
		  out of range\n");
	      return;
            }
	    if (linestyle < 0)
	    {
	      if (DEBUG)
	          fprintf(stderr,
		  "WARNING: adaptive pattern length (pattern number less \
		  then 0 in LT instruction)\n         not implemented yet\n");
	      linestyle = -linestyle;
            }
	}

	else
	{
	    SETDOT = FALSE;
	    fprintf(outfile, "[] 0 setdash\n");
            LINE_STYLE = SOLID_LINE;
	    LINE_LENGTH = 4.0;
	    return;
	}

	if (test_token())
	{
	    linescale = getval();
	    if ((linescale < 0.0) || (linescale >= 128.0))
	    {
	     fprintf(stderr,"ERROR: pattern length parameter in LT instruction is out of range\n");
	     return;
            }
	    if (linescale == 0.0)
	     fprintf(stderr,"WARNING: pattern length parameter in LT instructionis equal zero\n");
         }
	 else
	    linescale = 4.0;    /* default length  */

     LINE_STYLE = linestyle;
     LINE_LENGTH = linescale;

    }  /* end of LINE_TYPE_SCALE  */

/************************* not HPGL code **********************************   
    else
    if (line == LINESCALE)
    {
	if (test_token())
	    linescale = getval() / 80 * SCALE;
	else
	    return;
    } else
    if (line == LINETYPE)	
    {
	if (test_token())
	{
	    if ((linestyle = rintx(getval())) > 5)
		linestyle = 5;
	    else if (linestyle < -5)
		linestyle = -5;
	}
	else
	    return;
    } else
    {
	fprintf(stderr, "Error: Unknown line flag in linetype.c\n");
	exit(1);
    }
**************************************************************************/
    /*
     * now prepare proper length
     */
     diagonal = pow(((P2x - P1x) * XSCALE_ABS),2.0) + pow(((P2y - P1y) * YSCALE_ABS),2.0);
     diagonal = pow(diagonal,0.5);
     line_len = diagonal * LINE_LENGTH * 0.01;

    /*
     * select a line style/type and scale/pitch 
     */
    switch (LINE_STYLE)
    {
    case 6:                /*    [----- - - ]     */ 
       { 
	down1 = 0.5;
	up1 = up2 = up3 = 0.1;
	down2 = down3 = 0.1;
	parts = 6;
       }
	break;

    case 5:               /*    [------- - ]     */
       {
	down1 = 0.7;
	up1 = up2 = 0.1;
	down2 = 0.1;
	parts = 4;
       }
	break;

    case 4:               /*   [-------- . ]     */
       { 
	down1 = 0.78;
	up1 = up2 = 0.1;
	down2 = 0.02;
	parts = 4;
       }
	break;

    case 3:               /*   [-------   ]     */
       { 
	down1 = 0.7; 
	up1 = 0.3;
	parts = 2;
       } 
	break;

    case 2:               /*   [-----     ]     */
       { 
	down1 = 0.5;
	up1 = 0.5;
	parts = 2;
       }
	break;

    case 1:               /*   [.         ]     */
       { 
	down1 = 0.02;
	up1 = 0.98;
	parts = 2;
       }
	break;
    }

    if (linestyle == 0)
    {
	SETDOT = TRUE;				/* For HP-GL only */
	fprintf(outfile, "[] 0 setdash\n");
    } else
    {
	SETDOT = FALSE;				/* For HP-GL only */

	down1 *= line_len;
	down2 *= line_len;
	down3 *= line_len;
	up1 *= line_len;
	up2 *= line_len;
	up3 *= line_len;

	switch (parts)
	{
	  case 2:
	    fprintf(outfile, "[%g mm %g mm] 0 setdash\n",
	            down1, up1);
            break;


	  case 4:
	    fprintf(outfile, "[%g mm %g mm %g mm %g mm] 0 setdash\n",
	            down1, up1, down2, up2);
            break;

	  case 6:
	    fprintf(outfile,"[%g mm %g mm %g mm %g mm %g mm %g mm] 0 setdash\n",
	            down1, up1, down2, up2, down3, up3);
            break;
        }

    }
}
