home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gle / gle / mychar.c < prev    next >
C/C++ Source or Header  |  1992-11-29  |  6KB  |  274 lines

  1. /*----------------------------------------------------------------------*/
  2. /* This routine prints a char from one of my fonts, it has cacheing     */
  3. /* for the char data, which could be extended to bitmap cacheing     */
  4. /*----------------------------------------------------------------------*/
  5. #include "all.h"
  6. #include "core.h"
  7. #include "color.h"
  8. extern struct gmodel g;
  9. #include "mygraph.h"
  10. #define true (!false)
  11. #define false 0
  12. #ifdef BIGINDIAN
  13. #define BYTE0 1
  14. #define BYTE1 0
  15. #else
  16. #define BYTE0 0
  17. #define BYTE1 1
  18. #endif
  19.  
  20. int font_file_vector(int ff,char *filename);
  21. int draw_char_pcode(char *s);
  22. char *fontdir(char *s);
  23. int my_load_font(int ff);
  24. double frx(char **s);
  25. unsigned char my_name[80];
  26. char *my_code[80];
  27. int my_ref[80];
  28. char *my_buff;
  29. int my_font[80];
  30. int my_pnt[257];
  31. int my_curfont;
  32. extern double font_lwidth;
  33. int char_plen(char *s);
  34. int get_char_pcode(int ff, int cc, char **pp);
  35. my_char(int ff, int cc)
  36. {
  37.     char *pp;
  38.     get_char_pcode(ff,cc,&pp);
  39.     draw_char_pcode(pp);
  40. }
  41. get_char_pcode(int ff, int cc, char **pp)
  42. {
  43.     int i,plen,mi,minref;
  44. /* y:    Is char in font cache, if so draw it, inc ref */
  45.     /*  should use *memchr(s,c,n)  */
  46.     for (i=1;i<80;i++) {
  47.         if (my_name[i]==cc) {
  48.             if (my_font[i]==ff) {
  49.                 my_ref[i]++;
  50.                 *pp = my_code[i];
  51.                 return;
  52.             }
  53.         }
  54.     }
  55.  
  56. /* x:    Is that font currently loaded, if so put in cache at least used, goto y */
  57.     minref = 30000;
  58.     mi = 0;
  59.     if (my_curfont!=ff) my_load_font(ff);
  60.     /* Find least used cache character */
  61.     for (i=1;i<80;i++)  {
  62.         if (my_ref[i]<minref) {minref=my_ref[i]; mi = i;}
  63.     }
  64.     if (mi==0) mi=1;
  65.     plen = char_plen(my_buff+my_pnt[cc]);
  66.     if (my_code[mi]==0) my_code[mi] = myallocz(plen+1);
  67.     else        {
  68.     /*    my_code[mi] = realloc(my_code[mi],plen+1); */
  69.         myfree(my_code[mi]);
  70.         my_code[mi] = myalloc((plen+1));
  71.     }
  72.     if (my_code[mi]==0) gprint("Memory allocation failure, in myfont.c \n");
  73.     memcpy(my_code[mi],my_buff+my_pnt[cc],plen+1);
  74.     *pp = my_code[mi];
  75.     my_name[mi] = cc;
  76.     my_ref[mi] = 1;
  77.     my_font[mi] = ff;
  78. }
  79. /* free up memory used by vector font */
  80. freeavec()
  81. {
  82.     if (my_buff == NULL) return;
  83.     myfree(my_buff);
  84.     my_buff = 0;
  85.     my_curfont = 0;
  86. }
  87. char *gledir(char *s);
  88. my_load_font(int ff)
  89. {
  90.     char vector_file[60];
  91.     int i;
  92.     FILE *fin;
  93.     font_file_vector(ff,vector_file);
  94.     fin = fopen(fontdir(vector_file),READ_BIN);
  95.     if (fin==0) {
  96.         gprint("(Warning) can't find vector file {%s} replacing it.\n",fontdir(vector_file));
  97.         font_replace_vector(ff);
  98.         font_file_vector(ff,vector_file);
  99.         fin = fopen(fontdir(vector_file),READ_BIN);
  100.         if (fin==NULL) gle_abort("Font vector texcmr.fve not found\n");
  101.     }
  102.     fread(my_pnt,sizeof(i),256,fin);
  103.     /* gprint("Size of rest of font %d \n",my_pnt[0]); */
  104.     if (my_buff==0) my_buff = myallocz(10 + my_pnt[0]);
  105.     else         {
  106.         myfree(my_buff);
  107.         my_buff = myallocz(10 + my_pnt[0]);
  108.     }
  109.     if (my_buff==0) gprint("Memory allocation failure MY_BUFF , in myfont.c \n");
  110.     fread(my_buff,1,my_pnt[0],fin);
  111.     fclose(fin);
  112.     my_curfont = ff;
  113. }
  114. int frxi(char **s);
  115. char_plen(char *s)
  116. {
  117.     char *savelen;
  118.     savelen = s;
  119.     while (*s!=15) {
  120.       switch (*s++) {
  121.         case 1:
  122.         case 2:
  123.         frxi(&s); frxi(&s);
  124.         break;
  125.         case 3:
  126.         frxi(&s); frxi(&s);
  127.         frxi(&s); frxi(&s);
  128.         frxi(&s); frxi(&s);
  129.         break;
  130.         case 4:
  131.         case 5:
  132.         case 6:
  133.         case 7:
  134.         case 8:
  135.         break;
  136.         case 0: /* char does not exist */
  137.         goto abort;
  138.         default:
  139.         gprint("Error in mychar pcode %d \n",*s++);
  140.         goto abort;
  141.       }
  142.  
  143.     }
  144. abort:
  145.     return s-savelen;
  146. }
  147. int draw_char_pcode(char *s)
  148. {
  149.     static double cx,cy,ox,oy,x1,y1,x2,y2;
  150.     char *savelen;
  151.     double old_lwidth;
  152.     int old_path,old_join;
  153.     long old_color;
  154.  
  155.     g_get_path(&old_path);
  156.     g_get_color(&old_color);
  157.     g_set_fill(old_color);
  158.     g_get_line_width(&old_lwidth);
  159.     g_set_line_width(font_lwidth);
  160.     g_get_line_join(&old_join);
  161.     g_set_line_join(1);    /* use rounded lines to avoid ucky peeks */
  162.  
  163.     g_get_xy(&ox,&oy);
  164.     savelen = s;
  165.     if (!old_path) {
  166.         g_set_path(true);
  167.         g_newpath();
  168.     }
  169.     while (*s!=15) {
  170.       switch (*s++) {
  171.         case 1:
  172.         cx = ox + frx(&s); cy = oy + frx(&s);
  173.         g_move(cx,cy);
  174.         break;
  175.         case 2:
  176.         cx = cx + frx(&s); cy = cy + frx(&s);
  177.         g_line(cx,cy);
  178.         break;
  179.         case 3:
  180.         cx = cx + frx(&s); cy = cy + frx(&s);
  181.         x1 = cx; y1 = cy;
  182.         cx = cx + frx(&s); cy = cy + frx(&s);
  183.         x2 = cx; y2 = cy;
  184.         cx = cx + frx(&s); cy = cy + frx(&s);
  185.         g_bezier(x1,y1,x2,y2,cx,cy);
  186.         break;
  187.         case 4:
  188.         g_closepath();
  189.         break;
  190.         case 5:
  191.         if (!old_path)     g_fill();
  192.         break;
  193.         case 6:
  194.         g_stroke();
  195.         break;
  196.         case 7:
  197.         g_gsave();
  198.         g_set_fill(COLOR_WHITE);
  199.         g_fill();
  200.         g_grestore();
  201.         break;
  202.         case 8:
  203.         g_set_line_width(frx(&s));
  204.         break;
  205.         case 0:         /* no such char in this font */
  206.         goto abort;
  207.         default:
  208.         gprint("Error in mychar pcode %d \n",*s++);
  209.         goto abort;
  210.       }
  211.  
  212.     }
  213. abort:    if (!old_path) g_set_path(old_path);
  214.     g_set_line_join(old_join);
  215.     g_set_line_width(old_lwidth);
  216.     g_set_color(old_color);
  217.     return s-savelen;
  218. }
  219. double frx(char **s)
  220. {
  221.     static union {char a[2];short b;} both;
  222.     static int i;
  223.  
  224.     if (g.fontsz==0) {
  225.         gprint("Font size is zero ***\n");
  226.         g.fontsz = 1;
  227.     }
  228.     i = *(*s)++;
  229.     if (i==127) {
  230.         both.a[0] = (*(*s)++);
  231.         both.a[1] = (*(*s)++);
  232.         if (1==2) printf("bothb %d \n",both.b);
  233.         return (g.fontsz*both.b)/1000.0;
  234.     } else {
  235.             if (i>127) i = -(256-i);
  236.         if (1==2) printf("ii %d \n",i);
  237.         return (g.fontsz*i)/1000.0;
  238.     }
  239. }
  240. frxi(char **s)
  241. {
  242.     static union {char a[2];short b;} both;
  243.     static int i;
  244.  
  245.     i = *(*s)++;
  246.     if (i==127) {
  247.         both.a[0] = (*(*s)++);
  248.         both.a[1] = (*(*s)++);
  249.         if (1==2) printf("both %d \n",both.b);
  250.         return (both.b);
  251.     } else {
  252.             if (i>127) i = -(256-i);
  253.         if (1==2) printf("i %d \n",i);
  254.         return i;
  255.     }
  256. }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.