home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fonts 1 / freshfonts1.bin / bbs / programs / amiga / pastex13.lha / DVIPS / dvips5519.lha / dvips / emspecial.c < prev    next >
C/C++ Source or Header  |  1993-02-12  |  35KB  |  1,479 lines

  1. /*
  2.  *   emspecial.c
  3.  *   This routine handles the emTeX special commands.
  4.  */
  5. #include "dvips.h" /* The copyright notice in that file is included too!*/
  6.  
  7. #include <ctype.h>
  8. extern int atoi();
  9. extern FILE *search();
  10. extern char *getenv();
  11. /*
  12.  *   These are the external routines called:
  13.  */
  14. /**/
  15. extern void hvpos() ;
  16. extern void cmdout() ;
  17. extern void mhexout() ;
  18. extern void nlcmdout() ;
  19. extern void newline() ;
  20. extern void floatout() ;
  21. extern void numout() ;
  22. extern void error() ;
  23. extern void specerror() ;
  24. extern char errbuf[] ;
  25. extern shalfword linepos;
  26. extern FILE *bitfile;
  27. extern int actualdpi ;
  28. extern int vactualdpi ;
  29. extern integer hh, vv;
  30. extern char *figpath ;
  31. extern int prettycolumn ;
  32. extern int quiet;
  33. extern Boolean disablecomments ;
  34.  
  35. #ifdef DEBUG
  36. extern integer debug_flag;
  37. #endif
  38.  
  39.  
  40. #ifdef EMTEX
  41. /* emtex specials, added by rjl */
  42.  
  43. #define EMMAX 1613 /* maximum number of emtex special points */
  44. #define TRUE 1
  45. #define FALSE 0
  46.  
  47. /*
  48.  *   We define these seek constants if they don't have their
  49.  *   values already defined.
  50.  */
  51. #ifndef SEEK_SET
  52. #define SEEK_SET (0)
  53. #endif
  54. #ifndef SEEK_END
  55. #define SEEK_END (2)
  56. #endif
  57.  
  58. struct empt {
  59.    shalfword point;
  60.    integer x, y;
  61. };
  62.  
  63. struct empt *empoints = NULL;
  64. boolean emused = FALSE;  /* true if em points used on this page */
  65. integer emx, emy;
  66.  
  67. struct emunit {
  68.    char *unit;
  69.    float factor;
  70. };
  71. struct emunit emtable[] = {
  72.   {"pt",72.27},
  73.   {"pc",72.27/12},
  74.   {"in",1.0},
  75.   {"bp",72.0},
  76.   {"cm",2.54},
  77.   {"mm",25.4},
  78.   {"dd",72.27/(1238/1157)},
  79.   {"cc",72.27/12/(1238/1157)},
  80.   {"sp",72.27*65536},
  81.   {"",0.0}
  82. };
  83.  
  84.  
  85. /* clear the empoints array if necessary */
  86. void
  87. emclear()
  88. {
  89. int i;
  90.    if (emused && empoints)
  91.       for (i=0; i<EMMAX; i++)
  92.          empoints[i].point = 0;
  93.    emused = FALSE ;
  94. }
  95.  
  96. /* put an empoint into the empoints array */
  97. struct empt *emptput(point, x, y)
  98. shalfword point;
  99. integer x, y;
  100. {
  101. int i, start;
  102.  
  103.    emused = TRUE;
  104.    start = point % EMMAX;
  105.    i = start;
  106.    while ( empoints[i].point != 0 ) {
  107.       if ( empoints[i].point == point )
  108.          break;
  109.       i++;
  110.       if (i >= EMMAX)
  111.          i = 0;
  112.       if (i == start) {
  113.      sprintf(errbuf,"!Too many em: special points");
  114.      specerror(errbuf);
  115.       }
  116.    }
  117.  
  118.    empoints[i].point = point;
  119.    empoints[i].x = x;
  120.    empoints[i].y = y;
  121.    return(&empoints[i]);
  122. }
  123.  
  124. /* get an empoint from the empoints array */
  125. struct empt *emptget(point)
  126. shalfword point;
  127. {
  128. int i, start;
  129.  
  130.    start = point % EMMAX;
  131.    i = start;
  132.    if (emused == TRUE)
  133.       while ( empoints[i].point != 0 ) {
  134.          if (empoints[i].point == point)
  135.             return(&empoints[i]);
  136.          i++;
  137.          if (i >= EMMAX)
  138.             i = 0;
  139.          if (i == start)
  140.             break;
  141.       }
  142.    sprintf(errbuf,"!em: point %d not defined",point);
  143.    specerror(errbuf);
  144.    return(NULL); /* never returns due to error */
  145. }
  146.  
  147.  
  148. /* convert width into dpi units */
  149. float emunits(width,unit)
  150. float width;
  151. char *unit;
  152. {
  153. struct emunit *p;
  154.     for (p=emtable; *(p->unit)!='\0'; p++) {
  155.        if (strcmp(p->unit,unit)==0)
  156.         return( width * actualdpi / p->factor );
  157.     }
  158.     return (-1.0); /* invalid unit */
  159. }
  160.  
  161. /* The main routine for \special{em:graph ...} called from dospecial.c */
  162. /* the line cut parameter is not supported (and is ignored) */
  163.  
  164. void emspecial(p)
  165. char *p ;
  166. {
  167. float emwidth, emheight;
  168. shalfword empoint1, empoint2;
  169. struct empt *empoint;
  170. char emunit[3];
  171. char emstr[80];
  172. char *emp;
  173. void emgraph();
  174.  
  175.         hvpos() ;
  176.     for (emp = p+3; *emp && isspace(*emp); emp++); /* skip blanks */
  177.     if (strncmp(emp, "linewidth", 9) == 0) {
  178.        /* code for linewidth */
  179.        for (emp = emp+9; *emp && isspace(*emp); emp++); /* skip blanks */
  180.        sscanf(emp, "%f%2s", &emwidth, emunit);
  181.        emwidth = emunits(emwidth,emunit);
  182.        if (emwidth!=-1.0) {
  183.           sprintf(emstr,"%.1f setlinewidth", emwidth);
  184.           cmdout(emstr);
  185. #ifdef DEBUG
  186.    if (dd(D_SPECIAL))
  187.       (void)fprintf(stderr, "em special: Linewidth set to %.1f dots\n", 
  188.         emwidth) ;
  189. #endif
  190.        } else {
  191.           sprintf(errbuf,"Unknown em: special width");
  192.           specerror(errbuf);
  193.        }
  194.     }
  195.         else if (strncmp(emp, "moveto", 6) == 0) {
  196. #ifdef DEBUG
  197.    if (dd(D_SPECIAL))
  198. #ifdef SHORTINT
  199.       (void)fprintf(stderr, "em special: moveto %ld,%ld\n", hh, vv);
  200. #else
  201.       (void)fprintf(stderr, "em special: moveto %d,%d\n", hh, vv);
  202. #endif
  203. #endif
  204.            emx = hh;
  205.            emy = vv;
  206.         }
  207.         else if (strncmp(emp, "lineto", 6) == 0) {
  208. #ifdef DEBUG
  209.    if (dd(D_SPECIAL))
  210. #ifdef SHORTINT
  211.       (void)fprintf(stderr, "em special: lineto %ld,%ld\n", hh, vv);
  212. #else
  213.       (void)fprintf(stderr, "em special: lineto %d,%d\n", hh, vv);
  214. #endif
  215. #endif
  216.        cmdout("np");
  217.        numout(emx);
  218.        numout(emy);
  219.        cmdout("a");
  220.        numout(hh);
  221.        numout(vv);
  222.        cmdout("li");
  223.        cmdout("st");
  224.            emx = hh;
  225.            emy = vv;
  226.         }
  227.     else if (strncmp(emp, "point", 5) == 0) {
  228.            if (empoints == NULL) {
  229.               empoints = 
  230.               (struct empt *)mymalloc((integer)EMMAX * sizeof(struct empt)) ;
  231.               emused = TRUE;
  232.               emclear();
  233.            }
  234.        for (emp = emp+5; *emp && isspace(*emp); emp++); /* skip blanks */
  235.            empoint1 = (shalfword)atoi(emp);
  236.            empoint = emptput(empoint1,hh,vv);
  237. #ifdef DEBUG
  238.    if (dd(D_SPECIAL))
  239. #ifdef SHORTINT
  240.       (void)fprintf(stderr, "em special: Point %d is %ld,%ld\n",
  241. #else
  242.       (void)fprintf(stderr, "em special: Point %d is %d,%d\n",
  243. #endif
  244.         empoint->point, empoint->x, empoint->y) ;
  245. #endif
  246.     }
  247.     else if (strncmp(emp, "line", 4) == 0) {
  248.        for (emp = emp+4; *emp && isspace(*emp); emp++); /* skip blanks */
  249.            empoint1 = (shalfword)atoi(emp);
  250.        for (; *emp && isdigit(*emp); emp++); /* skip point 1 */
  251.        if ( *emp && strchr("hvp",*emp)!=0 )
  252.           emp++;  /* skip line cut */
  253.        for (; *emp && isspace(*emp); emp++); /* skip blanks */
  254.        if ( *emp && (*emp==',') )
  255.           emp++; /*  skip comma separator */
  256.        for (; *emp && isspace(*emp); emp++); /* skip blanks */
  257.            empoint2 = (shalfword)atoi(emp);
  258.        for (; *emp && isdigit(*emp); emp++); /* skip point 2 */
  259.        if ( *emp && strchr("hvp",*emp)!=0 )
  260.           emp++;  /* skip line cut */
  261.        for (; *emp && isspace(*emp); emp++); /* skip blanks */
  262.        if ( *emp && (*emp==',') )
  263.           emp++; /*  skip comma separator */
  264.        emwidth = -1.0;
  265.        emunit[0]='\0';
  266.        sscanf(emp, "%f%2s", &emwidth, emunit);
  267.        emwidth = emunits(emwidth,emunit);
  268. #ifdef DEBUG
  269.    if (dd(D_SPECIAL))
  270.       (void)fprintf(stderr, "em special: Line from point %d to point %d\n",
  271.         empoint1, empoint2) ;
  272. #endif
  273.        cmdout("np");
  274.        if (emwidth!=-1.0) {
  275. #ifdef DEBUG
  276.    if (dd(D_SPECIAL))
  277.    (void)fprintf(stderr,"em special: Linewidth temporarily set to %.1f dots\n", 
  278.         emwidth) ;
  279. #endif
  280.            strcpy(emstr,"currentlinewidth");
  281.            cmdout(emstr);
  282.             sprintf(emstr,"%.1f setlinewidth", emwidth);
  283.             cmdout(emstr);
  284.        }
  285.            empoint = emptget(empoint1);
  286.        numout(empoint->x);
  287.        numout(empoint->y);
  288.        cmdout("a");
  289.            empoint = emptget(empoint2);
  290.        numout(empoint->x);
  291.        numout(empoint->y);
  292.        cmdout("li");
  293.        cmdout("st");
  294.        if (emwidth!=-1.0) {
  295.            strcpy(emstr,"setlinewidth");
  296.            cmdout(emstr);
  297.        }
  298.     }
  299.     else if (strncmp(emp, "message", 7) == 0) {
  300.            (void)fprintf(stderr, "em message: %s\n", emp+7) ;
  301.     }
  302.     else if (strncmp(emp, "graph", 5) == 0) {
  303.        int i;
  304.        for (emp = emp+5; *emp && isspace(*emp); emp++); /* skip blanks */
  305.        for (i=0; *emp && !isspace(*emp) && !(*emp==',') ; emp++)
  306.           emstr[i++] = *emp; /* copy filename */
  307.        emstr[i] = '\0';
  308.        /* now get optional width and height */
  309.        emwidth = emheight = -1.0;    /* no dimension is <= 0 */
  310.        for (; *emp && ( isspace(*emp) || (*emp==',') ); emp++)
  311.           ;  /* skip blanks and comma */
  312.        if (*emp) {
  313.           sscanf(emp, "%f%2s", &emwidth, emunit); /* read width */
  314.           emwidth = emunits(emwidth,emunit); /* convert to pixels */
  315.           for (; *emp && ( isdigit(*emp) || isalpha(*emp) ); emp++)
  316.              ; /* skip width dimension */
  317.           for (; *emp && ( isspace(*emp) || (*emp==',') ); emp++)
  318.              ;  /* skip blanks and comma */
  319.           if (*emp) {
  320.              sscanf(emp, "%f%2s", &emheight, emunit); /* read height */
  321.              emheight = emunits(emheight,emun