home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume31 / jgraph / part06 / printline.c < prev    next >
C/C++ Source or Header  |  1992-07-14  |  6KB  |  309 lines

  1. /* 
  2.  * $Source: /n/fs/vd/jsp/src/jgraph/RCS/printline.c,v $
  3.  * $Revision: 8.0 $
  4.  * $Date: 92/07/03 14:16:05 $
  5.  * $Author: jsp $
  6.  */
  7.  
  8. #include "jgraph.h"
  9. #include <stdio.h>
  10.  
  11. #define LINEWIDTHFACTOR 0.700
  12. #define MAX(a, b) ((a > b) ? (a) : (b))
  13.  
  14. typedef struct fontlist {
  15.   struct fontlist *flink;
  16.   struct fontlist *blink;
  17.   int level;
  18.   float s;
  19.   char *f;
  20. } *Fontlist;
  21.  
  22. static Fontlist Jgraph_fonts;
  23. static int Jgraph_gsave_level = -100;
  24. static int Jgraph_comment;
  25.  
  26. gsave()
  27. {
  28.   if (Jgraph_gsave_level == -100) {
  29.     Jgraph_gsave_level = 0;
  30.     Jgraph_fonts = (Fontlist) make_list(sizeof(struct fontlist));
  31.   } 
  32.   Jgraph_gsave_level++;
  33.   printf(" gsave ");
  34. }
  35.  
  36. grestore()
  37. {
  38.   Fontlist l;
  39.  
  40.   if (last(Jgraph_fonts) != nil(Jgraph_fonts)) {
  41.     l = last(Jgraph_fonts);
  42.     if (l->level == Jgraph_gsave_level) {
  43.       delete_item(l);
  44.       free_node(l, Jgraph_fonts);
  45.     }
  46.   }
  47.   Jgraph_gsave_level--;
  48.   printf(" grestore ");
  49. }
  50.  
  51. setfont(f, s)
  52. char *f;
  53. float s;
  54. {
  55.   Fontlist l;
  56.   int ins;
  57.  
  58.   if (last(Jgraph_fonts) != nil(Jgraph_fonts)) {
  59.     l = last(Jgraph_fonts);
  60.     ins = (strcmp(l->f, f) != 0 || s != l->s);
  61.     if (ins) {
  62.       delete_item(l);
  63.       free_node(l, Jgraph_fonts);
  64.     }
  65.   } else {
  66.     ins = 1;
  67.   }
  68.   if (ins) {
  69.     l = (Fontlist) get_node(Jgraph_fonts);
  70.     l->level = Jgraph_gsave_level;
  71.     l->s = s;
  72.     l->f = f;
  73.     insert(l, Jgraph_fonts);
  74.     printf("/%s findfont %f scalefont setfont\n", f, s);
  75.   }
  76. }
  77.   
  78. setfill( t, f)
  79. char t ;
  80. float f[] ;
  81. {
  82.     if ( t == 'g' )  {
  83.        if( f[0] >= 0.0 ) printf("gsave %f setgray fill grestore ", f[0] );
  84.     } else if ( t == 'c' )  {
  85.        printf("gsave %f %f %f setrgbcolor fill grestore ", f[0], f[1], f[2] );
  86.     } else {
  87.        printf("fill ");
  88.     }
  89. }
  90.  
  91. setgray( t, f)
  92. char t ;
  93. float f[] ;
  94. {
  95.     if ( t == 'g' )  {
  96.        if( f[0] >= 0.0 ) printf("%f setgray\n", f[0] );
  97.     } else if ( t == 'c' )  {
  98.        printf("%f %f %f setrgbcolor\n", f[0], f[1], f[2] );
  99.     }
  100. }
  101.  
  102. printline(x1, y1,x2, y2, orientation)
  103. float x1, y1, x2, y2;
  104. char orientation;
  105. {
  106.   if (orientation == 'x') 
  107.     printf("newpath %f %f moveto %f %f lineto stroke\n",
  108.           x1, y1, x2, y2);
  109.   else
  110.     printf("newpath %f %f moveto %f %f lineto stroke\n",
  111.           y1, x1, y2, x2);
  112.  
  113. print_ebar(x1, y1, x2, ms, orientation)
  114. float x1, y1, x2, ms;
  115. char orientation;
  116. {
  117.   printline(x1, y1, x2, y1, orientation);
  118.   printline(x2, y1-ms, x2, y1+ms, orientation);
  119. }
  120.  
  121. start_line(x1, y1, c)
  122. float x1, y1;
  123. Curve c;
  124. {
  125.   setlinewidth(c->linethick);
  126.   setlinestyle(c->linetype, c->gen_linetype);
  127.   printf("%f %f moveto ", x1, y1);
  128. }
  129.  
  130. cont_line(x1, y1)
  131. float x1, y1;
  132. {
  133.   printf("  %f %f lineto\n", x1, y1);
  134. }
  135.  
  136. end_line()
  137. {
  138.   printf("stroke\n");
  139.   setlinewidth(1.0);
  140.   setlinestyle('s', (Flist) 0);
  141.  
  142. }
  143.  
  144. bezier_control(x1, y1)
  145. float x1, y1;
  146. {
  147.   printf("  %f %f ", x1, y1);
  148. }
  149.  
  150. bezier_end(x1, y1)
  151. float x1, y1;
  152. {
  153.   printf("  %f %f curveto\n", x1, y1);
  154. }
  155.  
  156.  
  157. start_poly(x1, y1)
  158. float x1, y1;
  159. {
  160.   printf("newpath %f %f moveto ", x1, y1);
  161. }
  162.  
  163. cont_poly(x1, y1)
  164. float x1, y1;
  165. {
  166.   printf("  %f %f lineto\n", x1, y1);
  167. }
  168.  
  169. end_poly(ftype, fill)
  170. char  ftype ;
  171. float fill[];
  172. {
  173.   printf("closepath ");
  174.   setfill( ftype, fill );
  175.   printf("stroke\n");
  176. }
  177.  
  178. printellipse(x, y, radius1, radius2, ftype, fill)
  179. char ftype ;
  180. float x, y, radius1, radius2, fill[];
  181. {
  182.   printf("newpath %f %f %f %f 0 360 DrawEllipse ", x, y, radius1, radius2);
  183.   setfill( ftype, fill );
  184.   printf("stroke\n");
  185. }
  186.  
  187. set_comment(c)
  188. int c;
  189. {
  190.   Jgraph_comment = c;
  191. }
  192.  
  193. comment(s)
  194. char *s;
  195. {
  196.   if (Jgraph_comment) printf("%% %s\n", s);
  197. }
  198.  
  199. printline_c(x1, y1, x2, y2, g)
  200. float x1, y1, x2, y2;
  201. Graph g;
  202. {
  203.   printline(ctop(x1, g->x_axis), ctop(y1, g->y_axis),
  204.             ctop(x2, g->x_axis), ctop(y2, g->y_axis), 'x');
  205. }
  206.  
  207. print_label(l)
  208. Label l;
  209. {
  210.   int f, i, nlines;
  211.   float fnl;
  212.   char *s;
  213.  
  214.   if (l->label == CNULL) return;
  215.  
  216.   nlines = 0;
  217.   for (i = 0; l->label[i] != '\0'; i++) {
  218.     if (l->label[i] == '\n') {
  219.       l->label[i] = '\0';
  220.       nlines++;
  221.     }
  222.   }
  223.   fnl = (float) nlines;
  224.  
  225.   setfont(l->font, l->fontsize);
  226.   printf("gsave %f %f translate %f rotate\n", l->x, l->y, l->rotate);
  227.   if (l->graytype == 'g') {
  228.     printf("  %f setgray\n", l->gray[0]);
  229.   } else if (l->graytype == 'c') {
  230.     printf("  %f %f %f setrgbcolor\n", l->gray[0], l->gray[1], 
  231.            l->gray[2]);
  232.   }
  233.  
  234.   if (l->vj == 'b') {
  235.     printf("0 %f translate ", fnl * (l->fontsize + l->linesep) * FCPI / FPPI);
  236.   } else if (l->vj == 'c') {
  237.     if (nlines % 2 == 0) {
  238.       printf("0 %f translate ", 
  239.              (fnl/2.0*(l->fontsize + l->linesep) - l->fontsize/2.0)
  240.               * FCPI / FPPI);
  241.     } else {
  242.       printf("0 %f translate ", 
  243.              ((fnl-1.0)/2.0*(l->fontsize + l->linesep) + l->linesep/2.0)
  244.               * FCPI / FPPI);
  245.     }
  246.   } else {
  247.     printf("0 %f translate ", -l->fontsize * FCPI / FPPI);
  248.   }
  249.  
  250.   s = l->label;
  251.   for (i = 0; i <= nlines; i++) {
  252.     printf("(%s) dup stringwidth pop ", s);
  253.     if (l->hj == 'c') {
  254.       printf("2 div neg 0 moveto\n");
  255.     } else if (l->hj == 'r') {
  256.       printf("neg 0 moveto\n");
  257.     } else {
  258.       printf("pop 0 0 moveto\n");
  259.     }
  260.     /* I would put string blanking in here if I had the time... */
  261.  
  262.     if (i != nlines) {
  263.       f = strlen(s);
  264.       s[f] = '\n';
  265.       s = &(s[f+1]);
  266.       printf("show 0 %f translate\n", 
  267.               - (l->fontsize + l->linesep) * FCPI / FPPI);
  268.     } else {
  269.       printf("show\n");
  270.     }
  271.   }
  272.   printf("grestore\n");
  273. }
  274.  
  275. setlinewidth(size)
  276. float size;
  277. {
  278.   printf("%f setlinewidth\n", size * LINEWIDTHFACTOR);
  279. }
  280.  
  281. setlinestyle(style, glist)
  282. char style;
  283. Flist glist;
  284. {
  285.   Flist fl;
  286.  
  287.   switch(style) {
  288.     case '0': printf("\t[0 2] setdash\n"); break;
  289.     case 's': printf("\t[] 0 setdash\n"); break;
  290.     case '.': printf("\t [1 3.200000] 0 setdash\n"); break;
  291.     case '-': printf("\t [4.00000] 0 setdash\n"); break;
  292.     case 'l': printf("\t [7 2] 0 setdash\n"); break;
  293.     case 'd': printf("\t [5 3 1 3] 0 setdash\n"); break;
  294.     case 'D': printf("\t [5 3 1 2 1 3] 0 setdash\n"); break;
  295.     case '2': printf("\t [5 3 5 3 1 2 1 3] 0 setdash\n"); break;
  296.     case 'g': 
  297.       printf("\t [");
  298.       for (fl = first(glist); fl != nil(glist); fl = next(fl))
  299.         printf("%f ", fl->f);
  300.       printf("] 0 setdash\n");
  301.       break;
  302.     default: fprintf(stderr, "Error: Unknown line type: %c\n", style);
  303.              exit(1);
  304.              break;
  305.   }
  306. }
  307.  
  308.