home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / s1 / sss / !SSS / c / dr next >
Text File  |  1991-07-30  |  5KB  |  240 lines

  1. /* Draw file construction */
  2. /* Tue,30 Jul 1991 */
  3. /* Copyright C.T.Stretch 1991 */
  4.  
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <limits.h>
  9.  
  10. #include "werr.h"
  11. #include "flex.h"
  12. #include "drhdr.h"
  13. #include "os.h"
  14. #include "font.h"
  15.  
  16. #define DF (*(int**)dfp)
  17. #define CP(n) ((char*)(*dfp)+4*n)
  18.  
  19. BOOL dr_ok=FALSE;
  20. int dr_page=3;
  21. double dr_xinch,dr_yinch;
  22.  
  23. static flex_ptr dfp=0;
  24. static int ds,ps,dt;
  25. static font trinity=0;
  26. static int paperx=0,papery,lmargin,bmargin,rmargin,tmargin;
  27. static double mx,my;
  28. static int cx,cy,psize;
  29. static int wimpcols[]={0XFFFFFF00,0XDFDFDF00,0XBFBFBF00,0X9F9F9F00,
  30.                        0X7F7F7F00,0X5F5F5F00,0X3F3F3F00,0X00000000,
  31.                        0X80300000,0X00E0E000,0X00B00000,0X0000A000,
  32.                        0XA0E0E000,0X00704000,0X00B0F000,0XF0A00000};
  33.  
  34. static void push(int n)
  35. { if(!dr_ok) return;
  36.   if(ds>=dt)
  37.   { dt+=1024;
  38.     if(!flex_extend(dfp,4*dt))
  39.     { werr(0,"No room");
  40.       dr_ok=FALSE;
  41.       return;
  42.     }
  43.   }
  44.   DF[ds++]=n;
  45. }
  46.  
  47. static void pushstr(char *s)
  48. { int n=(strlen(s)+4)/4;
  49.   if(!dr_ok) return;
  50.   if(ds+4*n>=dt)
  51.   { dt+=4*n+512;
  52.     if(!flex_extend(dfp,4*dt))
  53.     { werr(0,"No room");
  54.       dr_ok=FALSE;
  55.       return;
  56.     }
  57.   }
  58.   DF[ds+n-1]=0;
  59.   strcpy((char*)(DF+ds),s);
  60.   ds+=n;
  61. }
  62.  
  63. static void bbox()
  64. { push(INT_MAX);push(INT_MAX);push(INT_MIN);push(INT_MIN);
  65. }
  66.  
  67. static void adjust(int *b,int x,int y)
  68. { if(b[0]>x) b[0]=x;
  69.   if(b[1]>y) b[1]=y;
  70.   if(b[2]<x) b[2]=x;
  71.   if(b[3]<y) b[3]=y;
  72. }
  73.  
  74. static void resize(int *a,int *b)
  75. { if(a[0]>b[0]) a[0]=b[0];
  76.   if(a[1]>b[1]) a[1]=b[1];
  77.   if(a[2]<b[2]) a[2]=b[2];
  78.   if(a[3]<b[3]) a[3]=b[3];
  79. }
  80.  
  81. static void setpagesize()
  82. #define PS(n) (regs.r[n]*16)/25
  83. { os_regset regs;
  84.   switch(dr_page)
  85.   { default:if(!os_swix(0x80143+os_X,®s))
  86.     { paperx=PS(1);
  87.       papery=PS(2);
  88.       lmargin=PS(3);
  89.       bmargin=PS(4);
  90.       rmargin=PS(5);
  91.       tmargin=PS(6);
  92.       return;
  93.     }
  94.     case 1:paperx=8;papery=10;break;
  95.     case 2:paperx=10;papery=8;break;
  96.     case 3:paperx=8;papery=5;break;
  97.   }
  98.   paperx=paperx*180*256;
  99.   papery=papery*180*256;
  100.   lmargin=90*256;
  101.   bmargin=90*256;
  102.   rmargin=paperx-lmargin;
  103.   tmargin=papery-bmargin;
  104. }
  105.  
  106.  
  107. void dr_start(flex_ptr fp)
  108. { dfp=fp;dt=1024;
  109.   setpagesize();
  110.   if(*(int**)dfp)
  111.   { if(!flex_extend(dfp,4*dt))
  112.     { werr(0,"No room");
  113.       dr_ok=FALSE;
  114.       return;
  115.     }
  116.   }
  117.   else
  118.   { if(!flex_alloc(dfp,4*dt))
  119.     { werr(0,"No room");
  120.       dr_ok=FALSE;
  121.       return;
  122.     }
  123.   }
  124.   dr_ok=TRUE;
  125.   DF[0]=0x77617244;
  126.   DF[1]=201;
  127.   DF[2]=0;
  128.   memcpy(CP(3),"SSS         ",12);
  129.   ds=6;
  130.   bbox();
  131.   push(0);;push(24);pushstr("\x01Trinity.Medium");
  132. }
  133.  
  134. void dr_path()
  135. { if(!dr_ok) return;
  136.   ps=ds;
  137.   push(2);push(0);bbox();push(-1);push(0);push(0);push(0);
  138. }
  139.  
  140. void dr_style(int fc,int lc,int w)
  141. { DF[ps+6]=(fc==-1)?-1:wimpcols[fc];
  142.   DF[ps+7]=(lc==-1)?-1:wimpcols[lc];
  143.   DF[ps+8]=w;
  144. }
  145.  
  146. void dr_move(double x,double y)
  147. { int dx=(int)(mx*x)+cx,dy=(int)(my*y)+cy;
  148.   push(2);
  149.   push(dx);push(dy);
  150.   adjust(DF+ps+2,dx,dy);
  151. }
  152.  
  153.  
  154. void dr_draw(double x,double y)
  155. { int dx=(int)(mx*x)+cx,dy=(int)(my*y)+cy;
  156.   push(8);
  157.   push(dx);push(dy);
  158.   adjust(DF+ps+2,dx,dy);
  159. }
  160.  
  161. void dr_close()
  162. { push(5);
  163. }
  164.  
  165. void dr_end()
  166. { push(0);
  167.   DF[ps+1]=4*(ds-ps);
  168.   resize(DF+6,DF+ps+2);
  169. }
  170.  
  171. void dr_printl(double x,double y,char *s)
  172. { int ts=ds;
  173.   int n=strlen(s);
  174.   int x0=(int)(mx*x)+cx;
  175.   int y0=(int)(my*y)+cy-psize/2;
  176.   font_info fi;
  177.   push(1);
  178.   push(0);
  179.   font_stringbbox(s,&fi);
  180.   x0-=(fi.maxx-fi.minx)*16/25;
  181.   push(x0-psize);push(y0-psize);push(x0+(n+2)*psize);push(y0+2*psize);
  182.   push(0);push(0xFFFFFF00);push(1);
  183.   push(psize);push(psize);
  184.   push(x0);push(y0);
  185.   pushstr(s);
  186.   DF[ts+1]=4*(ds-ts);
  187.   resize(DF+6,DF+ts+2);
  188. }
  189.  
  190.  
  191. void dr_printu(double x,double y,char *s)
  192. { int ts=ds;
  193.   int n=strlen(s);
  194.   int x0=(int)(mx*x)+cx;
  195.   int y0=(int)(my*y)+cy-psize*3/2;
  196.   font_info fi;
  197.   push(1);
  198.   push(0);
  199.   font_stringbbox(s,&fi);
  200.   x0-=(fi.maxx-fi.minx)*8/25;
  201.   push(x0-psize);push(y0-psize);push(x0+(n+2)*psize);push(y0+2*psize);
  202.   push(0);push(0xFFFFFF00);push(1);
  203.   push(psize);push(psize);
  204.   push(x0);push(y0);
  205.   pushstr(s);
  206.   DF[ts+1]=4*(ds-ts);
  207.   resize(DF+6,DF+ts+2);
  208. }
  209.  
  210. BOOL dr_save(char *fn)
  211. { char buf[512];
  212.   FILE *out=fopen(fn,"w");
  213.   if(!out) return FALSE;
  214.   fwrite(DF,4,ds,out);
  215.   fclose(out);
  216.   sprintf(buf,"settype %s %X",fn,0xAFF);
  217.   system(buf);
  218.   return TRUE;
  219. }
  220.  
  221. void dr_psize(double x)
  222. { psize=(int)(x*mx);
  223.   if(psize<3200) psize=3200;
  224.   if(psize>15360) psize=15360;
  225.   psize=(psize/640)*640;
  226.   if(trinity) font_lose(trinity);
  227.   if(font_find("Trinity.Medium",psize/40,psize/40,90,45,&trinity))
  228.   werr(0,"Can't find Trinity.Medium");
  229.   font_setfont(trinity);
  230. }
  231.  
  232. void dr_scale(double x0,double y0,double x1,double y1)
  233. { mx=(rmargin-lmargin)/(x1-x0);
  234.   cx=lmargin-(int)(x0*mx);
  235.   my=(tmargin-bmargin)/(y1-y0);
  236.   cy=bmargin-(int)(y0*my);
  237.   dr_xinch=180*256/mx;
  238.   dr_yinch=180*256/my;
  239. }
  240.