home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / PRINTING / DVIPS386.ZIP / FLIB.C < prev    next >
C/C++ Source or Header  |  1991-11-03  |  10KB  |  303 lines

  1. /*
  2.  *   Here's some code to handle font libraries.  Not needed unless you are
  3.  *   running on a system that can't handle files well.  Not endorsed by
  4.  *   Tomas Rokicki or Radical Eye Software; use at your own risk.
  5.  */
  6. #ifdef FONTLIB
  7. #include "structures.h"
  8. #include "paths.h"
  9. extern FILE *search() ;
  10. extern char *newstring() ;
  11. extern shalfword pkbyte() ;
  12. extern void badpk() ;
  13. /* extern char *malloc() ; */
  14. extern void* malloc(unsigned) ;
  15. extern integer pkquad() ;
  16. extern int debug_flag ;
  17. extern char errbuf[] ;
  18. extern char *flipath ;
  19. extern char *fliname ;
  20. extern FILE *pkfile ;
  21. /*
  22.  * font library structures
  23.  */
  24. struct fli_entry {
  25.    unsigned long  offset;
  26.    char          *name;
  27. };
  28.  
  29. struct fli_size {
  30.    unsigned long     size;
  31.    halfword          entries;
  32.    struct fli_entry *entry;
  33. };
  34.  
  35. struct fli_lib {
  36.    char            *name;
  37.    FILE            *fp;
  38.    halfword         sizes;
  39.    struct fli_size *size;
  40.    struct fli_lib  *next;
  41. };
  42.  
  43. struct fli_lib *firstlib = NULL;
  44.  
  45. Boolean flib = 0;  /* non zero if reading a font library */
  46.  
  47. halfword
  48. pkdouble()
  49. {
  50.    register halfword i ;
  51.    i = pkbyte() ;
  52.    i = i * 256 + pkbyte() ;
  53.    return(i) ;
  54. }
  55. extern char name[] ;
  56. /*
  57.  *   fliload opens each font library, then reads in its
  58.  *   directory for later use.  Font libraries are left
  59.  *   open for later use by flisearch().
  60.  *   loadfont() does not close pkfile if it is a font 
  61.  *   library (flib non zero).
  62.  */
  63. void
  64. fliload()
  65. {
  66.    int i ;
  67.    halfword version1, version2;
  68.    Boolean needext;
  69.    char fontname[50]; 
  70.    char name[50] ;
  71.    char *fli;
  72.    unsigned long dpi;
  73.    halfword len, numsizes, numfonts;
  74.    struct fli_lib *lib=NULL, *next_lib=NULL;
  75.    struct fli_size *size;
  76.    struct fli_entry *entry;
  77.  
  78.    fli = fliname;
  79.  
  80.    while (*fli) {
  81.       /* get next font library name from fliname */
  82.       needext=1;
  83.       for (i=0; *fli && *fli!=PATHSEP; i++)
  84.          if ( (name[i] = *fli++) == '.')
  85.             needext=0;
  86.       name[i] = '\0';
  87.       if (*fli)
  88.          fli++;  /* skip PATHSEP */
  89.       if (*name) { 
  90.          /* got fli name, now search for it */
  91.          if (needext)
  92.             strcat(name,".fli");
  93.  
  94.          if ( (pkfile=search(flipath,name,READBIN)) != (FILE *)NULL ) {
  95.             /* for each font library */
  96.             for (i=0; i<4; i++) {
  97.               fontname[i] = pkbyte();  /* read header */
  98.             }
  99.             version1 = pkbyte();
  100.             version2 = pkbyte();
  101.             if (strncmp(fontname,"FLIB",4)!=0 || version1 != 2 || version2 != 0)
  102.                badpk("incorrect font library format");
  103.  
  104.             (void) pkdouble();       /* ignore directory length */
  105.             numsizes = pkdouble();   /* number of sizes */
  106.             numfonts = pkdouble();   /* number of fonts */
  107.             len = pkdouble();        /* length of comment */
  108.             for (i=0; i<len; i++)
  109.                (void)pkbyte();       /* skip comment */
  110. #ifdef DEBUG
  111.    if (dd(D_FONTS))
  112.       (void)fprintf(stderr,"Font library %s has %d font size%s, %d font%s\n",
  113.          name, numsizes , numsizes !=1 ? "s" : "", 
  114.          numfonts, numfonts!=1 ? "s" : "");
  115. #endif /* DEBUG */
  116.  
  117.             next_lib =  (struct fli_lib *)malloc(sizeof(struct fli_lib)); 
  118.             if (next_lib == (struct fli_lib *)NULL)
  119.                error("! out of memory while reading font library") ;
  120.             if (firstlib == (struct fli_lib *)NULL)
  121.                firstlib = next_lib;
  122.             else
  123.                lib->next = next_lib;
  124.             lib = next_lib;
  125.             size = (struct fli_size *)
  126.                         malloc(numsizes * sizeof(struct fli_size)); 
  127.             if (size == (struct fli_size *)NULL)
  128.                error("! out of memory while reading font library") ;
  129.             entry = (struct fli_entry *)
  130.                         malloc(numfonts * sizeof(struct fli_entry)); 
  131.             if (entry == (struct fli_entry *)NULL)
  132.                error("! out of memory while reading font library") ;
  133.             lib->name = newstring(name);
  134.             lib->fp = pkfile;         /* keep file open */
  135.             lib->sizes = numsizes;
  136.             lib->size = size;
  137.             lib->next = (struct fli_lib *)NULL;
  138.  
  139.             for ( ;numsizes>0; numsizes--, size++) { 
  140.                /* for each font size in this library */
  141.                (void)pkdouble();      /* length of size entry - ignore */
  142.                numfonts = pkdouble(); /* number of fonts */
  143.                dpi = pkquad();        /* DPI (fixed point 16.16) */
  144.  
  145. #ifdef DEBUG
  146.    if (dd(D_FONTS))
  147.       (void)fprintf(stderr,"Font library %s size %.5gdpi has %d font%s\n", 
  148.                   name, dpi/65536.0, numfonts, numfonts!=1 ? "s" : "");
  149. #endif /* DEBUG */
  150.                size->size    = dpi ;
  151.                size->entries = numfonts ;
  152.                size->entry   = entry ;
  153.                   for ( ;numfonts > 0; numfonts--, entry++) {
  154.                      /* read each entry */
  155.                      (void)pkquad();            /* ignore length of font */
  156.                      entry->offset = pkquad();  /* offset to font */
  157.                      len = pkbyte();            /* length of name */
  158.                      for (i=0; i<len; i++)
  159.                         fontname[i] = pkbyte();
  160.                      fontname[len] = '\0';
  161.                      entry->name = newstring(fontname);
  162.                   } /* end for numfonts>0 */
  163.             }  /* end for numsizes>0 */
  164.             /* keep font library open */
  165.          }  /* end if opened library */
  166.       } /* end if (*name) */
  167.    }
  168. }
  169.    
  170.  
  171. /*
  172.  *   flisearch searches all the font libraries for a PK font.
  173.  *   returns FILE pointer positioned to PK font in font library
  174.  */
  175. FILE *
  176. flisearch(n, dpi)
  177. char *n;
  178. halfword dpi;
  179. {
  180.    halfword dpi1, numsizes, numfonts;
  181.    struct fli_lib *lib=NULL;
  182.    struct fli_size *size;
  183.    struct fli_entry *entry;
  184.    int del ;
  185.    
  186.    if (firstlib == (struct fli_lib *)NULL)
  187.       return((FILE *)NULL);  /* return if no font libraries */
  188.  
  189. #ifdef DEBUG
  190.       if (dd(D_FONTS))
  191.          (void)fprintf(stderr,"Trying %s at %ddpi\n", n, dpi);
  192. #endif /* DEBUG */
  193.    for (lib = firstlib; lib != (struct fli_lib *)NULL; lib = lib->next ) {
  194.       /* for each font library */
  195.       numsizes = lib->sizes ;
  196.       size = lib->size ;
  197. #ifdef DEBUG
  198.       if (dd(D_FONTS))
  199.          (void)fprintf(stderr,"  Searching %s\n", lib->name);
  200. #endif /* DEBUG */
  201.       for (; numsizes>0; numsizes--, size++) { 
  202.          /* for each font size in this library */
  203.          dpi1 = (halfword)((size->size+32768L)/65536) ;
  204.          if ( dpi1 == dpi ) {
  205.             /* if correct size then search for font */
  206. #ifdef DEBUG
  207.             if (dd(D_FONTS))
  208.                (void)fprintf(stderr, "    Checking size %ddpi\n",dpi1);
  209. #endif /* DEBUG */
  210.             entry = size->entry ;
  211.             for (numfonts=size->entries ;numfonts > 0; numfonts--, entry++) {
  212.                if (strcmp(entry->name,n)==0) {
  213.                   /* if correct font name then seek it */
  214.                      pkfile = lib->fp;
  215.                      flib = 1 ; /* this is a font library - don't close it */
  216.                      (void)sprintf(name,"%s %s %ddpi",lib->name, n, dpi1) ;
  217.                      if ( fseek(pkfile,entry->offset,0) )
  218.                            badpk("couldn't seek font");
  219.                         /* make sure it is a PK font */
  220.                         if (pkbyte()==247) /* pre byte */
  221.                            if (pkbyte()==89) {  /* id byte */
  222.                               if ( fseek(pkfile,entry->offset,0) )
  223.                                  badpk("couldn't seek font");
  224.                               return(pkfile); /* found it */
  225.                            }
  226.                         sprintf(errbuf,"%s %s %ddpi isn't PK format, ignoring",
  227.                               lib->name, n, dpi1);
  228.                         error(errbuf);
  229.                } /* end if name correct */
  230.             } /* end for numfonts>0 */
  231.          }
  232.          else {
  233.             /* if not correct size then skip */
  234. #ifdef DEBUG
  235.       if (dd(D_FONTS))
  236.          (void)fprintf(stderr, "    Skipping size %ddpi\n", dpi1);
  237. #endif /* DEBUG */
  238.          }
  239.       }  /* end for numsizes>0 */
  240.    }
  241.    return((FILE *)NULL);
  242. }
  243.  
  244. /* parse the font library path, putting all directory names in path, 
  245.  * and all font library names in name.  
  246.  * Directory names have a trailing DIRSEP.
  247.  */
  248. char *
  249. fliparse(path, name)
  250.         char *path, *name ;
  251. {
  252.    char *p, *prevp ;           /* pointers to path */
  253.    char *n, *prevn ;           /* pointers to name */
  254.    char *s ;
  255.  
  256.    p = path ;
  257.    n = name ;
  258.    s = path ;
  259.  
  260.    while (*s) {
  261.       prevp = p ;
  262.       prevn = n ;
  263.       while (*s && *s != PATHSEP) {
  264.          /* copy till PATHSEP */
  265.          *p++ = *s; 
  266.          *n++ = *s;
  267.          s++;
  268.       }  
  269.       *n = '\0' ;
  270.       if (*s)
  271.          s++;  /* skip PATHSEP */
  272.  
  273.       if ( *prevn=='\0' || prevn[strlen(prevn)-1] == DIRSEP ) {
  274.          n = prevn ; /* ignore name if it is dir */
  275.          if (*prevn)
  276.             p--;     /* backup over DIRSEP */
  277.          *p++ = PATHSEP;
  278.          prevp = p ;
  279.       }
  280.       else {
  281.          p = prevp ; /* ignore path if it is library name */
  282.          *n++ = PATHSEP;
  283.          prevn = n ;
  284.       }
  285.  
  286.    }
  287.    *p = '\0' ;
  288.    *n = '\0' ;
  289.    if (n!=name && *--n==PATHSEP)
  290.       *n = '\0'; /* remove trailing PATHSEP from name */
  291.    if (p!=path && *--p==PATHSEP)
  292.       *p = '\0'; /* remove trailing PATHSEP from path */
  293.    return(path);
  294. }               /* end fliparse */
  295. #else
  296. /*
  297.  *   Some systems don't like .o files that compile to nothing, so we
  298.  *   provide a stub routine.
  299.  */
  300. void fliload() {}
  301. #endif
  302.  
  303.