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

  1. #include "all.h"
  2. #include "mygraph.h"
  3. #include <math.h>
  4. int text_gprint(long *in,int ilen);
  5. int fftext_block(uchar *s,double width,int justify);
  6. char *fontdir(char *s);
  7. char *gledir(char *fname);
  8. int font_load(void);
  9. int font_init(void);
  10. int g_get_font(int *i);
  11. int do_prim(uchar **in,long *out,int *lout);  /*  \frac{text}{text} */
  12.  
  13. #define true (!false)
  14. #define false 0
  15. #define dbg if ((gle_debug & 32)>0)
  16. extern int gle_debug;
  17. /* typedef char unsigned uchar; */
  18.  
  19. /*  FONT.DAT contains  names of fonts, and indexes for each font, and
  20.     names of metric, vector and bitmap files
  21.     This enables fonts to be referred to by number, as their index will
  22.     not change */
  23.  
  24. #include "font.h"
  25.  
  26. char *font_getname(int i)
  27. {
  28.     return fnt[i].name;
  29. }
  30. char_bbox(int ff,int cc,double *xx1, double *yy1, double *xx2, double *yy2)
  31. {
  32.     if (nfnt==0) font_load();
  33.     if (fnt[ff].chr==NULL) font_load_metric(ff);
  34.     if ( (fnt[ff].chr) != NULL) {
  35.         *xx1 = ((*(fnt[ff].chr))[cc].x1);
  36.         *yy1 = ((*(fnt[ff].chr))[cc].y1);
  37.         *xx2 = ((*(fnt[ff].chr))[cc].x2);
  38.         *yy2 = ((*(fnt[ff].chr))[cc].y2);
  39.     } else {
  40.         *xx1 = 0;
  41.         *xx2 = 0;
  42.         *yy1 = 0;
  43.         *yy2 = 0;
  44.     }
  45.  
  46. }
  47. font_get_chardata(struct char_data *cd,int ff,int cc)
  48. {
  49.     if ( (fnt[ff].chr) != NULL) {
  50.         cd->x1 = ((*(fnt[ff].chr))[cc].x1);
  51.         cd->y1 = ((*(fnt[ff].chr))[cc].y1);
  52.         cd->x2 = ((*(fnt[ff].chr))[cc].x2);
  53.         cd->y2 = ((*(fnt[ff].chr))[cc].y2);
  54.     } else {
  55.         cd->x1 = 0;
  56.         cd->y1 = 0;
  57.         cd->x2 = 0;
  58.         cd->y2 = 0;
  59.     }
  60.  
  61. }
  62. int smallf = false;
  63. font_simple()
  64. {
  65.     smallf = true;
  66. }
  67. font_load()
  68. {
  69.     FILE *fptr;
  70.     uchar inbuff[200];
  71.     char fname[80];
  72.     char nbuf[80];
  73.     int n;
  74.     char *s;
  75.     char *cp;
  76.     char *ns;
  77.  
  78.     strcpy(fname,fontdir("font.dat"));
  79.     fptr = fopen(fname,"r");
  80.     if (fptr==0) {
  81.         gprint("Unable to open font.dat file {%s} \n",fname);
  82.         perror("Reason ");
  83.     }
  84. /* font_load() */
  85.     for (fgets(inbuff,200,fptr);!feof(fptr);fgets(inbuff,200,fptr)) {
  86.         s = strchr(inbuff,'!');
  87.         if (s!=NULL) *s=0;
  88.         s = strtok(inbuff,"     ,");
  89.         if (s!=NULL) if (*s!='\n') {
  90.          ncpy(nbuf,s,30);
  91.          ns = strtok(0,"     ,");
  92.          n = atoi(ns);
  93.          mystrcpy(&fnt[n].name,nbuf);
  94.          for (cp = fnt[n].name; *cp!=0; cp++)
  95.              *cp = toupper(*cp);
  96.          s = strtok(0,"     ,");
  97.          if (smallf) mystrcpy(&fnt[n].file_metric,"pstr.fmt");
  98.          if (s!=0) mystrcpy(&fnt[n].file_metric,s);
  99.          s = strtok(0,"     ,");
  100.          if (smallf) mystrcpy(&fnt[n].file_vector,"plsr.fve");
  101.          else if (s!=0) mystrcpy(&fnt[n].file_vector,s);
  102.          s = strtok(0,"     ,");
  103.          if (s!=0) mystrcpy(&fnt[n].file_bitmap,s);
  104.          s = strtok(0,"\n");
  105.          if (s!=0) mystrcpy(&fnt[n].file_bitmap,s);
  106.          if (nfnt<n) nfnt = n;
  107.         }
  108.     }
  109.     fclose(fptr);
  110.  
  111.     if (fnt[n].scale==0) fnt[n].scale = 1000.0;
  112. }
  113.  
  114. FILE *fmt;
  115.  
  116. font_set(char *s)
  117. {}
  118.  
  119. font_file_vector(int ff,char *s)
  120. {
  121.     if (nfnt==0) font_load();
  122.     strcpy(s,fnt[ff].file_vector);
  123. }
  124. plotter_fonts()
  125. {
  126.     int i;
  127.     if (nfnt==0) font_load();
  128.     for (i=1;i<9;i++) {
  129.         freefont(i);
  130.         fnt[i].file_vector = "plsr.fve";
  131.         fnt[i].file_metric = "plsr.fmt";
  132.     }
  133.     for (i=82;i<86;i++) {
  134.         fnt[i].file_vector = "plsr.fve";
  135.         fnt[i].file_metric = "plsr.fmt";
  136.     }
  137. }
  138. font_replace_vector(int ff) /* replace vector file with substitue font */
  139. {
  140.     if (nfnt==0) font_load();
  141.     myfree(fnt[ff].file_vector);
  142.     fnt[ff].file_vector = sdup(fnt[1].file_vector);
  143. }
  144. font_get_encoding(int ff)
  145. {
  146.     if (nfnt==0) font_load();
  147.     if (fnt[ff].chr==NULL) font_load_metric(ff);
  148.     return fnt[ff].encoding;
  149. }
  150. show_font(void)
  151. {
  152.     printf("Font name 1 {%s} 2  {%s} \n",fnt[1].name,fnt[2].name);
  153. }
  154. /* Picks a font at random and frees it, checks to see if enough freemem */
  155. freeafont()
  156. {
  157.     int i,j;
  158.     struct char_datax (*cdx)[256];
  159.     if (coreleft()>75000l) return;
  160.     freeavec();
  161.     if (coreleft()>60000l) return;
  162.     for (i=99;i>0;i--) if (fnt[i].chr != NULL) {
  163.         freefont(i);
  164.         if (coreleft()>60000l) break;
  165.     }
  166. }
  167. freefont(int i)
  168. {
  169.     int j;
  170.     struct char_datax (*cdx)[256];
  171.     if ( (fnt[i].chr != NULL) && (fnt[i].chrx != NULL) ) {
  172.         myfree(fnt[i].chr);
  173.         fnt[i].chr = NULL;
  174.         cdx = fnt[i].chrx;
  175.         for (j=1;j<255;j++) {
  176.           if ((*cdx)[j].kern != NULL) myfree((*cdx)[j].kern);
  177.           if ((*cdx)[j].lig != NULL) myfree((*cdx)[j].lig);
  178.           if ((*cdx)[j].lrep != NULL) myfree((*cdx)[j].lrep);
  179.           if ((*cdx)[j].krep != NULL) myfree((*cdx)[j].krep);
  180.         }
  181.         myfree(fnt[i].chrx);
  182.         fnt[i].chrx = NULL;
  183.     }
  184. }
  185. font_load_metric(int ff)
  186. {
  187.     struct char_data (*cd)[256];
  188.     struct char_datax (*cdx)[256];
  189.     int ll,c,i;
  190.     double f;
  191.  
  192.     /* return; */
  193.     if (ff==0) {
  194.         gprint("There is no zero font, errorloading font \n");
  195.         return;
  196.     }
  197.     if (fnt[ff].chr!=NULL) return;
  198.     if (nfnt==0) font_load();
  199.     dbg printf("Load font number %d \n",ff);
  200.     dbg gprint("Have loaded font.dat \n");
  201.     if (fnt[ff].chr==NULL) {
  202.         if (coreleft()<60000l) freeafont();
  203.         dbg gprint("Loading metrics now \n");
  204.         fnt[ff].chr = myallocz(sizeof(*fnt[1].chr));
  205.         fnt[ff].chrx = myallocz(sizeof(*fnt[1].chrx));
  206.         cd = fnt[ff].chr;
  207.         cdx = fnt[ff].chrx;
  208.  
  209. #ifdef unix
  210.         fmt = fopen(fontdir(fnt[ff].file_metric),"r");
  211. #else
  212.         fmt = fopen(fontdir(fnt[ff].file_metric),"rb");
  213. #endif
  214.         if (fmt==NULL) {
  215.             gprint("(Warning) Font Metric not found [%d] (%s), Spacing wrong \n"
  216.                 ,ff,fnt[ff].file_metric);
  217.             myfree(fnt[ff].file_metric);
  218.             fnt[ff].file_metric = sdup(fnt[1].file_metric);
  219. #ifdef unix
  220.             fmt = fopen(fontdir(fnt[1].file_metric),"r");
  221. #else
  222.             fmt = fopen(fontdir(fnt[1].file_metric),"rb");
  223. #endif
  224.             if (fmt==NULL) {
  225.                 printf("Couldn't open {%s} \n",fontdir(fnt[1].file_metric));
  226.                 gle_abort("Can't open font file \n");
  227.             }
  228.         }
  229.  
  230.         /* reads in font data */
  231.         fread(&fnt[ff].encoding,4,16,fmt);
  232.  
  233.         dbg gprint("Encoding %d  slant %f,  box %f %f %f %f \n"
  234.             ,fnt[ff].encoding,fnt[ff].slant,fnt[ff].fx1
  235.             ,fnt[ff].fy1,fnt[ff].fx2,fnt[ff].fy2);
  236.  
  237.         fread(cd,sizeof(*cd),1,fmt);
  238.  
  239.  
  240.         while (c=fgetc(fmt)) {
  241.             ll = fgetc(fmt);
  242.             get_str((char **) &(*cdx)[c].lig,ll,fmt);
  243.             get_str((char **) &(*cdx)[c].lrep,ll,fmt);
  244.             ll = fgetc(fmt);
  245.             get_str((char **) &(*cdx)[c].kern,ll,fmt);
  246.             get_str((char **) &(*cdx)[c].krep,8*ll,fmt);
  247.             /* f = (*((*cdx)[c].krep+(0))).x;
  248.              gprint("kern char %d,  %f \n",c,f);
  249.             */
  250.         }
  251.         fclose(fmt);
  252.  
  253.     }
  254. }
  255.  
  256. char_lig(int p_fnt,uchar *c1, int c2)
  257. {
  258.     static struct char_datax (*cdx)[256];
  259.     static uchar *r,*l;
  260.     int a;
  261.  
  262.     cdx = fnt[p_fnt].chrx;
  263.     l = (*cdx)[*c1].lig;
  264.     if (l==NULL) {
  265.         return 0;
  266.     }
  267.     r = strchr(l,c2);
  268.     if (r!=NULL) {
  269.         dbg gprint("font p_fnt %d Lig pos %d \n ",p_fnt,r-l);
  270.         *c1 = * (((*cdx)[*c1].lrep ) + (r-l)) ;
  271.         return *c1;
  272.     } else return 0;
  273. }
  274. char_kern(int p_fnt,int c1, int c2, float *w)
  275. {
  276.     static struct char_datax (*cdx)[256];
  277.     static uchar *r,*k;
  278.  
  279.     cdx = fnt[p_fnt].chrx;
  280.     k = (*cdx)[c1].kern;
  281.     if (k==NULL) {
  282.         *w = 0;
  283.         return;
  284.     }
  285.     r = strchr(k,c2);
  286.     if (r!=NULL) {
  287.         *w = (*((*cdx)[c1].krep+(r-k))).x;
  288.     } else
  289.         *w = 0;
  290. }
  291. get_str(char **s,int l,FILE *fmt)
  292. {
  293.     if (l==0) return;
  294.     if (l+1==0) gprint("error -1 in get_str \n");
  295.     *s = myallocn(1,l+1);
  296.     fread(*s,1,l,fmt);
  297. }
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.