home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / d / dots151.zip / GRAPHSRC.ZIP / GLI.C < prev    next >
C/C++ Source or Header  |  1990-07-15  |  16KB  |  558 lines

  1. /*    
  2.     gli -    graphics interface routine for
  3.             CIE Laser Image Printing System, or LIPS 10
  4.  
  5.     history...
  6.         15 Jul 90    ver 1.30: can get parameters from configuration file
  7.         25 Apr 89    dummy routines for flipping lines and setting background
  8.                     color.
  9.         22 Jul 88    Deleting output file if it already exists.
  10.         9 Jul 87    Creating output file if it doesn't already exist
  11.         21 May 87    Stronger warning msg about using default output port
  12.         13 May 87    Default margins, width, and height set for landscape
  13.                     orientation.
  14.         1 May 87    Adapted from Hewlett Packard plotter routine
  15.         23 Jun 86    Adapted from Houstin Instruments plotter routine
  16.         27 Oct 87    Environment variable "plot_setup" can contain an
  17.                     alternate setup string.
  18.  
  19.     bugs...
  20.  
  21. bench needs to check for "erasing"
  22. g3x needs to call set_height to set character size.
  23.  
  24. */
  25.  
  26. #include <stdio.h>
  27. #include <math.h>
  28. #include <string.h>
  29. #include "g.h"
  30. #include "config.h"
  31.  
  32. #define ESC 27
  33. #define UNIT (1./300.)
  34. #define BUFSIZE 80
  35.  
  36.                 /* default margins, defined assuming portrait orientation */
  37. #define LMARGIN 1.125
  38. #define RMARGIN 1.125
  39. #define TMARGIN 1.5
  40. #define BMARGIN 1.5
  41.  
  42. /*    imported variables    */
  43.  
  44. extern char *default_script_file;
  45.  
  46. /*    exported graphics variables    */
  47.  
  48. char *machine        =    "CIE LIPS 10";
  49. char *interface_version    =    "1.30";
  50. char *config_file    =    NULL;
  51. static char default_config_file[] = "GRAPHLI.CFG";
  52. int plotting_device    =    1;
  53. int erasing            =    0;
  54. int flipping        =    0;
  55.  
  56. int max_color        =    1;
  57.  
  58. int current_color    =    0;
  59. int pixels_wide        =    (72*85)/2;    /* width of screen in pixels */
  60. int pixels_high        =    72*11;        /* height of screen in pixels */
  61. double best_width    =    1.;
  62. double best_height    =    .720;        /* relative height/width parameters */
  63. int pen_diameter    =    3;
  64.  
  65. int char_rows        =    66;            /* text parameters */
  66. int    char_columns    =    133;
  67. int    char_height        =    108;
  68. int    char_width        =    10;
  69. int    x_offset        =    0;
  70. int    y_offset        =    0;
  71. int    char_v_adjusted    =    1;
  72. int    char_h_adjusted    =    1;
  73.  
  74. int has_cursor_keys    =    0;
  75. int    up_arrow        =    0x48;        /* cursor keys */
  76. int    down_arrow        =    0x50;
  77. int    left_arrow        =    0x4b;
  78. int    right_arrow        =    0x4d;
  79. int    escaped_arrows    =    1;            /* cursor keys are preceded by 0 */
  80. int    escape_char        =    0;
  81.  
  82. /* an 'A' on row r, column c  has lower left corner on raster 
  83.                 r*char_height + y_offset 
  84.     at pixel
  85.                 c*char_width + x_offset            */
  86.  
  87.  
  88. struct PRIM_ATTR prim_attr=
  89.     {2,     /* # colors                          */
  90.     1,      /* # intensities                     */
  91.     0,      /* nonzero if supported in hardware  */
  92.     1,      /* # linestyles in hardware          */
  93.     1,      /* # linestyles in software          */
  94.     1,      /* # linewidths                      */
  95.     0,      /* nonzero if supported in hardware  */
  96.     1,      /* minimum linewidth                 */
  97.     14,     /* maximum linewidth (.2 in)         */
  98.     1,      /* # pens in hardware                */
  99.     0,      /* # pens in software                */
  100.     1,      /* # fonts                           */
  101.     1,      /* # character sizes                 */
  102.     0,      /* nonzero if supported in hardware  */
  103.     10,     /* minimum character height (pixels) */
  104.     10,     /* maximum character height (pixels) */
  105.     0,      /* # markers in hardware             */
  106.     0,      /* # markers in software             */
  107.     0       /* # pick IDs in hardware            */
  108.     };
  109.  
  110.  
  111. static draw();
  112. static erase();
  113. static text();
  114. static character();
  115. char *getenv();
  116.  
  117. static font=0;
  118. static int plotter_type;
  119. FILE *plot_file;
  120. static file_open = 0;
  121. static int style_code=1;
  122. static int color_code=0;
  123. static char styles[]=" 0123456789:";
  124. static cx=0, cy=0;    /*    actual position of pen  */
  125. static something_printed=0;    /* nonzero if something printed since last
  126.                                 PAGE command (graphics_clear())  */
  127. static wait()    /* wait for plotter buffer space */   
  128. {    something_printed=1;
  129. }
  130.  
  131.  
  132. static set_width(width) int width; 
  133. {    width *= 3;
  134.     if(width<3) width=3;
  135.     else if(width>64) width=64;
  136.     fprintf(plot_file,"SPD %d;\n",width);
  137. }
  138.  
  139. set_color(color) int color; {}
  140. set_intensity(intensity) double intensity; {}
  141. set_background_color(color) int color; {}
  142. set_background_intensity(intensity) double intensity; {}
  143.  
  144. inquire_color() {return 1;}
  145. double inquire_intensity() {return 1.;}
  146.  
  147. /*    draw - draw a straight line     */
  148.  
  149. static int draw(x1,y1,x2,y2) int x1,y1,x2,y2;
  150. {    int t,d1,d2;
  151. /*    y1= pixels_high-1-y1; y2= pixels_high-1-y2; */
  152.     d1=maximum(abs(cx-x1),abs(cy-y1));
  153.     d2=maximum(abs(cx-x2),abs(cy-y2));
  154. #ifdef DEBUG
  155.     printf("\nat (%d,%d)  drawing (%d,%d)(%d away) to (%d,%d)(%d away)",
  156.      cx,cy,x1,y1,d1,x2,y2,d2);
  157. #endif
  158.     if(d2<d1)
  159.         {t=x1; x1=x2; x2=t; t=y1; y1=y2; y2=t; t=d1; d1=d2; d2=t;
  160. #ifdef DEBUG
  161.         puts("swapping");
  162. #endif
  163.         }
  164. #ifdef DEBUG
  165.     putchar('\n');
  166. #endif
  167.     wait();
  168.     if(d1)
  169.         {fprintf(plot_file,"MAP %d,%d;",x1,y1);
  170.         }
  171.     fprintf(plot_file,"DAP %d,%d;\n",x2,y2);
  172.     cx=x2; cy=y2;
  173. }
  174.  
  175. /*    gotoxy - move pen to new position (used before text display) */
  176.  
  177. gotoxy(x,y) int x,y;
  178. {/*    y= pixels_high-1-y; */
  179.     if (x<0) x=0; else if (x>=pixels_wide) x=pixels_wide-1;
  180.     if (y<0) y=0; else if (y>=pixels_high) y=pixels_high-1;
  181.     if(x==cx && y==cy) return;
  182.     wait();
  183.     fprintf(plot_file,"MAP %d,%d;",x,y);
  184.     cx=x; cy=y;
  185. }
  186.  
  187. static nil() {}
  188.  
  189. /* exported function pointers */
  190. int (*draw_line)()    =    draw;
  191. int (*erase_line)()    =    nil;
  192. int (*draw_text)()    =    text;
  193. int (*draw_char)()    =    character;
  194. int (*flip_line)()    =    draw;
  195.  
  196. /*    find a character in a string (case insensitive) */
  197. static int strichr(s, c) char *s, c;
  198. {
  199.     for (c=tolower(c); *s; s++)
  200.         if(tolower(*s) == c)
  201.             return 1;
  202.     return 0;
  203. }
  204.  
  205. /*        init - initialize the graphics system    */
  206. init_graphics()
  207. {    int d1, d2, c, i, vel;
  208.     double left, top, w, h, wmax, hmax;
  209.     char buf[BUFSIZE], inbuf[BUFSIZE], *s, *t;
  210.     char *setup_string;
  211.  
  212.     static int portrait = 0, landscape = 1;
  213.     static int offset_ask = 1,
  214.         size_ask = 1,
  215.         orientation_ask = 1,
  216. /*        plotter_ask = 1, */
  217. /*        velocity_ask = 1, */
  218.         file_ask = 0;
  219.     static double offset[2] = {BMARGIN, LMARGIN};
  220. /*    static double velocity = {38.}; */
  221.     static double size[2] = {11. - BMARGIN - TMARGIN, 8.5 - LMARGIN - RMARGIN};
  222.     static char *port_name = NULL;
  223.  
  224.     static PARAM parmv[] = {
  225.                 {'o', REAL, &offset_ask, offset, 2},
  226.                 {'l', REAL, &offset_ask, &offset[0]},
  227.                 {'t', REAL, &offset_ask, &offset[1]},
  228.                 {'s', REAL, &size_ask, size, 2},
  229.                 {'w', REAL, &size_ask, &size[0]},
  230.                 {'h', REAL, &size_ask, &size[1]},
  231.                 {'p', BOOLEAN, &orientation_ask, &portrait, 0}, 
  232.                 {'l', BOOLEAN, &orientation_ask, &landscape, 0}, 
  233.                 {'f', STRING, &file_ask, &port_name, 1},
  234. /*                {'n', STRING, &plotter_ask, &plotter_name, 1}, */
  235. /*                {'v', REAL, &velocity_ask, &velocity, 1}, */
  236.                 {'\0'}};
  237.  
  238.     if(config_file == NULL) config_file = default_config_file;
  239.     config(config_file, NULL, parmv, buf, BUFSIZE);
  240.  
  241.     if(port_name == NULL && version() >= 0x200)
  242.         port_name = getenv("PLOT_PORT");
  243.     if(port_name == NULL) 
  244.         {port_name = strncpy(buf, default_script_file, BUFSIZE-4);
  245.         s = strchr(port_name, '.');
  246.         if(s != NULL) *s = 0;
  247.         strcat(port_name, ".li");
  248.         }
  249.  
  250.     if(file_ask)
  251.         {port_name = strncpy(buf, port_name, BUFSIZE);
  252.         printf("Enter output file                        (default %s): ",buf);
  253.         gets(inbuf);
  254.         if(inbuf[0]) strcpy(buf,inbuf);
  255.         }
  256.     else
  257.         printf("\noutput written to %s\n", port_name);
  258.  
  259.     unlink(port_name);                /* delete the file if it exists */
  260.     plot_file=fopen(port_name,"w");        /* this will open a file or device */
  261.     if(plot_file==NULL) {printf("can\'t open output file %s", port_name); exit();}
  262.     file_open = 1;
  263.  
  264.     setup_string = NULL;
  265.     if(version()>=0x200) setup_string = getenv("PLOT_SETUP");
  266.     if(setup_string == NULL) 
  267.         setup_string = strcpy(buf,"DAF; DAM; UNIT D; SCF; FONT 2;");
  268.     /*    
  269.             LIPS command language mode on
  270.              delete all fonts
  271.              delete all macros
  272.              unit = points (1/300 of inch)
  273.              save current font
  274.              Prestige Elite, 10-pt, 12 CPI portrait-style font
  275.     */
  276.     fixup_escapes(setup_string);
  277.     fprintf(plot_file,"(O] %s\n", setup_string);
  278.  
  279.     if(orientation_ask)
  280.         {printf("Portrait or landscape orientation? ('p' or 'l', default %c): ",
  281.             portrait?'p':'l');
  282.         gets(buf); 
  283.         portrait |= strichr(buf, 'p');
  284.         landscape = !portrait;
  285.         }
  286.  
  287.     left=offset[0];
  288.     top=offset[1];
  289.     while(1)
  290.         {if(offset_ask)
  291.             {printf("\nEnter left and top margin in inches \n");
  292.             printf("                                 (default %5.3f and %5.3f): ",left,top);
  293.             gets(buf); if(buf[0]) sscanf(buf,"%lf %lf",&left,&top);
  294.             }
  295.         if(left<0. || left>10.75)
  296.             printf("left margin outside valid range 0-10.75\n");
  297.         else if(top<0. || top>10.75)
  298.             printf("top margin outside valid range 0-10.75\n");
  299.         else if((portrait && left > 8.25) || (landscape && top > 8.25) || 
  300.                                                 (left > 8.25 && top > 8.25))
  301.             printf("no room left for plot!\n");
  302.         else break;
  303.         offset_ask = 1;
  304.         left=BMARGIN;
  305.         top=LMARGIN;
  306.         }
  307.                                  /* set origin */
  308.     fprintf(plot_file,"SLM %f; STM %f;\n", left/UNIT, top/UNIT);
  309.     
  310.     w = size[0];
  311.     h = size[1];
  312.     
  313.     while(1)
  314.         {if(size_ask)
  315.             {
  316.             printf("\nEnter width and height of plot area in inches \n");
  317.             if(!portrait)
  318.                 printf("maximum for landscape orientation, %5.3f by %5.3f\n", 
  319.                                                     10.75 - left, 8.15 - top);
  320.             if(!landscape)
  321.                 printf("maximum for portrait orientation,  %5.3f by %5.3f\n", 
  322.                                                     8.15 - left, 10.75 - top);
  323.             printf("                                 (default, %5.3f by %5.3f): ", w, h);
  324.             gets(buf); if(buf[0]) sscanf(buf,"%lf %lf",&w,&h);
  325.             }
  326.  
  327.         if(landscape) hmax = 8.15 - top; else hmax = 10.75 - top;
  328.         if(portrait) wmax = 8.15 - left; else wmax = 10.75 - left;
  329.                                 /* round to nearest .01" */
  330.         hmax = floor(hmax*100. + .5)/100.;
  331.         wmax = floor(wmax*100. + .5)/100.;
  332.         h = floor(h*100. + .5)/100.;
  333.         w = floor(w*100. + .5)/100.;
  334.  
  335.         if(w < .1 || w > wmax)
  336.             printf("width outside valid range 0.1 to %2.3f\n", wmax);
  337.         else if(h < .1 || h > hmax)
  338.             printf("height outside valid range 0.1 to %2.3f\n", hmax);
  339.         else if(w + left > 8.15 && h + top > 8.15)
  340.             printf("either width or height must be reduced\n");
  341.         else break;
  342.                             /* find suitable defaults for next time */
  343.         if(!portrait && top < 8.5 - LMARGIN)/* assume landscape orientation */
  344.             {h = 8.5 - top - LMARGIN;
  345.             if(left < 11. - TMARGIN) w=11. - left - TMARGIN;
  346.             else w = 11 - left;
  347.             }
  348.         else if(!landscape && left < 8.5 - RMARGIN)
  349.             {                            /* else assume portrait orientation */
  350.             w = 8.5 - RMARGIN - left;
  351.             if(top < 11. - BMARGIN) h = 11. - top - BMARGIN;
  352.             else h = 11. - top;
  353.             }
  354.         else if(!portrait && top<8.15) {h=8.15-top; w=10.75-left;}
  355.         else {h=10.75-top; w=8.15-left;}  /* maximize area */
  356.         size_ask = 1;
  357.         }
  358.     if(h<.1) h=.1;
  359.      if(w<.1) w=.1; 
  360.      else if(w+left>10.75) w=10.75-left;
  361.     if(landscape) 
  362.             /* landscape-style with Prestige Elite, 10-pt, 12 CPI font */
  363.         {fprintf(plot_file,"SPO L; FONT 3;");
  364.         if(h+top>9.75+1.) h=9.75+1.-top;
  365.         }
  366.     else
  367.         {if(h+top>7.25+1.) h=7.25+1.-top;
  368.         }
  369.  
  370.     pixels_wide=w/UNIT;
  371.     pixels_high=h/UNIT;
  372.     char_height=1./6./UNIT;                /* 6 lines/in */
  373.     char_rows=h*6.;
  374.     char_width=1./12./UNIT;                /* 12 char/in */
  375.     char_columns=w*12.;
  376.     if(pixels_wide>=pixels_high)         /* landscape style */
  377.         {best_width=1.;
  378.         best_height=(double)pixels_high/pixels_wide;
  379.         }
  380.     else                                 /* portrait style */
  381.         {best_height=1.;
  382.         best_width=(double)pixels_wide/pixels_high;
  383.         }
  384. }
  385.  
  386. /* character sizes in units of .005 inch */
  387.      /* interpret escape sequences in setup string...
  388.                          "$e" -> escape char, 
  389.                          "\n" -> line feed 
  390.                          "\r" -> carriage return
  391.                          "\t" -> tab
  392.                          "\c" -> c (escape an otherwise special character) */
  393. fixup_escapes(s) char *s;
  394. {    char *t;
  395.     for (t=s; *s; s++)
  396.         {if(*s=='$' && s[1]=='e') {*t++=0x1b; s++;}
  397.         else if(*s=='\\' && s[1]=='n') {*t++='\n'; s++;}
  398.         else if(*s=='\\' && s[1]=='t') {*t++='\t'; s++;}
  399.         else if(*s=='\\' && s[1]=='r') {*t++='\r'; s++;}
  400.         else if(*s=='\\') {*t++=s[1]; s++;}
  401.         else *t++=*s;
  402.         }
  403.     *t=0;
  404. }
  405.  
  406. static text(s) char *s;
  407. {    char *t, c, buf[80];
  408.     int i;
  409.     while(*s)
  410.         {t=s;
  411.         c= *t&127;
  412.         if(c=='\'') {fprintf(plot_file,"TEXT \"'\", E;"); s++; continue;}
  413.         if(c=='\n') {fprintf(plot_file,"TEXT '', N;"); s++; continue;}
  414.         i=0;
  415.         do
  416.             {if(c<32 || c>126) c=' ';
  417.             buf[i++]=c;
  418.             c= *++t&127;
  419.             } while(c && (i<79) && c!='\n' && c!='\'');
  420.         buf[i]=0;
  421.         wait();
  422.         fprintf(plot_file,"TEXT '%s', E;\n", buf);
  423.         s=t;
  424.         }
  425.     cx=cy=-2000;
  426. }
  427.  
  428. static character(c) char c;
  429. {    char buf[2];
  430.     buf[0]=c; buf[1]=0;
  431.     text(buf);
  432. }
  433.  
  434.  
  435. version()    /* return MS-DOS version number. Version 2.01 returned as 0x201 */
  436. {
  437. #ifdef __DESMET__
  438.     extern unsigned _rax;
  439.     _rax=0x3000;
  440.     _doint(0x21);
  441.     return ( (_rax&0xff)<<8 | (_rax&0xff00)>>8 );
  442. #else
  443. #include <dos.h>
  444.     return ( (_version&0xff)<<8 | (_version&0xff00)>>8 );
  445. #endif
  446. }
  447.  
  448. #ifdef __DESMET__
  449. /*    search environment for given string */
  450.  
  451. getenv(target) char *target;
  452. {    char buf[256],*s,t[25],*env, *malloc();
  453.     int nt,offset;
  454.  
  455.     s=t;
  456.     while(*target) *s++=toupper(*target++);
  457.     *s++= '='; *s=0;
  458.     nt = strlen(t);
  459.     offset=0;
  460.     _lmove(2,44,_showcs()-0x10,&env,_showds());
  461.     while(1)
  462.         {_lmove(256,offset,env,buf,_showds());
  463.         s=buf;
  464.         if(*s)
  465.             {/* printf("examining entry: %s \n",s); getchar(); */
  466.             if (strncmp(t,s,nt)==0) 
  467.                 {env = malloc(strlen(s+nt)+1);
  468.                 if(env == NULL) return NULL;
  469.                 return strcpy(env, s+nt);
  470.                 }
  471.             }
  472.         else return NULL;
  473.         offset+=strlen(buf)+1;
  474.         }
  475. }
  476. #endif /* DESMET */
  477.  
  478. /*        finish - close down the graphics system     */
  479.  
  480. finish_graphics()
  481. {    if(file_open)
  482.         {clear_graphics();
  483.  
  484.         fprintf(plot_file,"SPO P; UNIT I; RPF; EXIT;");
  485.             /*     portrait orientation,
  486.                 units are inches,
  487.                 return to previous font,
  488.                 command language mode off */
  489.  
  490.         fclose(plot_file);
  491.         file_open = 0;
  492.         }
  493. }
  494.  
  495. clear_graphics()
  496. {    if(something_printed) fprintf(plot_file,"PAGE;\n");
  497.     something_printed=0;
  498. }
  499.  
  500.  
  501. /*    pointers to optional functions (NULL if not implemented)    */
  502.  
  503. int    (*new_linestyle)()=0;    /* (*new_linestyle)(style) int style; */
  504. int    (*new_linewidth)()=set_width;    /* (*new_linewidth)(width) int width; */
  505. int    (*new_charsize)()=0;    /* (*new_charsize)(w,h) int w,h; */
  506. int    (*draw_marker)()=0;    /* (*draw_marker)(n) int n; */
  507.  
  508. #ifdef MAIN
  509.  
  510. main()
  511. {    char buf[100];
  512.     printf("Interface %s for the %s\n",interface_version,machine);
  513.     init_graphics();
  514.     printf("screen width %d pixels\nheight %d pixels\n",
  515.         pixels_wide,pixels_high);
  516.     printf("height:width ratio %f:%f\n",best_height,best_width);
  517.     printf("%d colors\n",max_color+1);
  518.     printf("primitive attributes...\n");
  519.     printf("color_count=               %d \n",prim_attr.color_count);
  520.     printf("intensity_count=           %d \n",prim_attr.intensity_count);
  521.     printf("intensities_in_hardware=   %d \n",prim_attr.intensities_in_hardware);
  522.     printf("hardware_linestyles=       %d \n",prim_attr.hardware_linestyles);
  523.     printf("software_linestyles=       %d \n",prim_attr.software_linestyles);
  524.     printf("linewidth_count=           %d \n",prim_attr.linewidth_count);
  525.     printf("linewidths_in_hardware=    %d \n",prim_attr.linewidths_in_hardware);
  526.     printf("linewidth_minimum=         %d \n",prim_attr.linewidth_minimum);
  527.     printf("linewidth_maximum=         %d \n",prim_attr.linewidth_maximum);
  528.     printf("hardware_pens=             %d \n",prim_attr.hardware_pens);
  529.     printf("software_pens=             %d \n",prim_attr.software_pens);
  530.     printf("charfont_count=            %d \n",prim_attr.charfont_count);
  531.     printf("charsize_count=            %d \n",prim_attr.charsize_count);
  532.     printf("charsize_in_hardware=      %d \n",prim_attr.charsize_in_hardware);
  533.     printf("charsize_minimum=          %d \n",prim_attr.charsize_minimum);
  534.     printf("charsize_maximum=          %d \n",prim_attr.charsize_maximum);
  535.     printf("hardware_markers=          %d \n",prim_attr.hardware_markers);
  536.     printf("software_markers=          %d \n",prim_attr.software_markers);
  537.     printf("pick_id_count=             %d \n",prim_attr.pick_id_count);
  538.     (*draw_line)(2,4,200,-400);
  539.     (*draw_line)(20,40,100,-200);
  540.     (*erase_line)(2,4,200,-400);
  541.     (*draw_line)(10,30,100,300);
  542.     (*draw_line)(10,40,500,890);
  543.     (*draw_line)(10,40,500,32);
  544.     (*draw_line)(10,1000,78,8900);
  545.     gotoxy(10,-20);
  546.     (*draw_text)(" ello\nDolly ");
  547.     (*draw_char)('H');
  548.     finish_graphics();
  549. }
  550.  
  551. #endif
  552.  
  553. maximum(a,b) int a,b;
  554. {    if (a>b) return a;
  555.     return b;
  556. }
  557.  
  558.