home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / DVI_MGR / dvimgr_s.lzh / dvimgr / openfont.beebe < prev    next >
Text File  |  1993-08-06  |  11KB  |  328 lines

  1. /* -*-C-*- openfont.h */
  2. /* 2. correction --> MIN(maxopen,MAXFONTS), 12.2.90,GF
  3. /*-->openfont*/
  4. /**********************************************************************/
  5. /****************************** openfont ******************************/
  6. /**********************************************************************/
  7.  
  8. void
  9. openfont(fontname)
  10. char *fontname;
  11.  
  12. /***********************************************************************
  13.     The original version of this DVI driver reopened the font file  each
  14.     time the font changed, resulting in an enormous number of relatively
  15.     expensive file  openings.    This version  keeps  a cache  of  up  to
  16.     MAXOPEN open files,  so that when  a font change  is made, the  file
  17.     pointer, fontfp, can  usually be  updated from the  cache.  When the
  18.     file is not found in  the cache, it must  be opened.  In this  case,
  19.     the next empty slot  in the cache  is assigned, or  if the cache  is
  20.     full, the least used font file is closed and its slot reassigned for
  21.     the new file.  Identification of the least used file is based on the
  22.     counts of the number  of times each file  has been "opened" by  this
  23.     routine.  On return, the file pointer is always repositioned to  the
  24.     beginning of the file.
  25.  
  26.     If the first open attempt  fails, an attempt will  be made to use  a
  27.     substitute font, then neighboring magnifications (with the same font
  28.     name), or substitutes for them.
  29. ***********************************************************************/
  30.  
  31. {
  32.     register INT16 i,j,k;    /* loop indices */
  33.     INT16 current;
  34.     INT16 least_used;
  35.     INT16 maxopen = MAXOPEN;
  36.  
  37. #if    OS_VAXVMS
  38.     long *jpi;
  39. #endif
  40.  
  41.     struct font_entry *tfontptr;
  42.     char subfont[MAXFNAME];
  43.     int submag;
  44.     char* filelist[MAXFORMATS];    /* pointers to templist[][] */
  45.     char templist[MAXFORMATS][MAXFNAME];
  46.  
  47. #if    VIRTUAL_FONTS
  48.     struct stat statbuf;        /* so fstat() can get file size */
  49.     char *p;
  50. #endif
  51.  
  52.     if ((pfontptr != (struct font_entry *)NULL) && (pfontptr == fontptr))
  53.     return;            /* we need not have been called */
  54.  
  55.     for (j = 0; j < MAXFORMATS; ++j)    /* initialize fontlist pointers */
  56.     filelist[j] = &templist[j][0];
  57.  
  58. #if    IBM_PC_MICROSOFT
  59.     /* This code required rewriting to get around a fatal compiler
  60.     assertion error occasioned by the code in the #else part */
  61.     for (current = 1; current <= nopen; ++current)
  62.     {
  63.         FILE* pfp;
  64.  
  65.     pfp = font_files[current].font_id;
  66.     if (pfp == fontptr->font_file_id)
  67.         break;
  68.     }
  69. #else
  70.     for (current = 1;
  71.     (current <= nopen) &&
  72.         (font_files[current].font_id != fontptr->font_file_id);
  73.     ++current)
  74.     ;            /* try to find file in open list */
  75. #endif
  76.  
  77.     if (current <= nopen)    /* file already open, lookup its id */
  78.     fontfp = font_files[current].font_id;
  79.     else
  80.     {
  81.     /***************************************************************
  82.     The file was  not in list  of open  files.  If the  list is  not
  83.     full, add it to  the list; otherwise  close the least-used  file
  84.     and remove it from the font_entry containing it.  Finally,  open
  85.     the file, or its closest  neighbor in the magnification  family.
  86.     A warning is issued  if no file can  be opened.  The caller  can
  87.     then proceed with zero font metrics if desired.
  88.     ***************************************************************/
  89.  
  90. #if    OS_VAXVMS
  91.         /***************************************************************
  92.     VAX VMS has many user quotas, one of which is the maximum number
  93.     of files  that can be open, which  need have no relation  to the
  94.     number  that C  permits.  If  we do  not determine the  limit at
  95.     run-time, the drivers may attempt to open too many files, and in
  96.     such a case, will fail.   There  are two relevant  quotas, FILLM
  97.     (open file limit), and  FILCNT (remaining open file quota).   We
  98.     use the latter,  and leave  one available file  for a   possible
  99.     error log.
  100.     ***************************************************************/
  101.  
  102.     jpi = (long*)getjpi(JPI$_FILCNT);
  103.     if (jpi == (long*)NULL)
  104.         maxopen = MAXOPEN;        /* should never happen */
  105.     else
  106.         maxopen = nopen - 2 + *jpi; /* temorary: 2 (gf) */
  107. #endif /* OS_VAXVMS */
  108.  
  109.     maxopen = MIN(maxopen,MAXFONTS);/* we have arrays of size MAXFONT */
  110.                     /* so do not exceed that limit */
  111.     maxopen = MIN(maxopen,MAXOPEN);
  112.  
  113.     if (nopen < maxopen)    /* just add it to list */
  114.         current = ++nopen;
  115.     else            /* list full -- find least used file, */
  116.     {            /* close it, and reuse slot for new file */
  117.         least_used = 1;
  118.         for (i = 2; i <= maxopen; ++i)
  119.         if (font_files[least_used].use_count >
  120.             font_files[i].use_count)
  121.             least_used = i;
  122.  
  123.         fontfp = font_files[least_used].font_id;
  124.         tfontptr = hfontptr;
  125.         while (tfontptr != (struct font_entry*)NULL)
  126.         {            /* remove file pointer from its font_entry */
  127.         if (tfontptr->font_file_id == fontfp)
  128.         {
  129.             tfontptr->font_file_id = (FILE*)NULL;
  130.             break;
  131.         }
  132.         tfontptr = tfontptr->next;
  133.         }
  134.  
  135. #if    VIRTUAL_FONTS
  136.         if (virt_font && (fontfp != (FILE*)NULL))
  137.             (void)virtfree(fontfp);
  138. #endif
  139.  
  140.         (void)fclose(fontfp);
  141.         fontfp = (FILE*)NULL;
  142.         current = least_used;
  143.     }
  144.     (void)actfact(fontptr->font_mag);    /* Get global mag_index */
  145.  
  146.     fontfp = (FILE*)NULL;
  147.  
  148.     /***************************************************************
  149.     Try the requested font, then any substitute font, then for  each
  150.     neighboring magnification  from  nearest to  furthest,  try  the
  151.     requested font, and then any substitute font.
  152.     ***************************************************************/
  153.  
  154.     for (k = 0; (fontfp == (FILE*)NULL) && (k < MAGTABSIZE); ++k)
  155.     {                /* loop over mag family */
  156.         for (i = -k; (fontfp == (FILE*)NULL) && (i <= k); i += MAX(1,k+k))
  157.         {                /* try smaller, then larger */
  158.         if (IN(0,mag_index+i,MAGTABSIZE-1))
  159.         {
  160.             (void)fontfile(filelist, ((fontptr->a==0)?fontpath:""),
  161.             fontname, (int)MAGSIZE(mag_table[mag_index+i]));
  162.             for (j = 0; (j < MAXFORMATS) && *filelist[j]; ++j)
  163.             {
  164.             fontfp = FOPEN(filelist[j],RB_OPEN);
  165.             DEBUG_OPEN(fontfp,filelist[j],RB_OPEN);
  166.             if (fontfp != (FILE *)NULL)
  167.             {
  168.                 strcpy(fontptr->name,filelist[j]);
  169.                 break;
  170.             }
  171.             }
  172.             if ((k > 0) && (fontfp != (FILE*)NULL))
  173.             {
  174.             (void)sprintf(message,
  175.                 "Font file [%s [mag %d]] could not be opened.\n\
  176. ---using nearest neighbor [%s [mag %d]] instead.",
  177.                 fontname,(int)MAGSIZE(mag_table[mag_index]),
  178.                 fontptr->name,
  179.                 (int)MAGSIZE(mag_table[mag_index+i]));
  180.             (void)warning(message);
  181.             }
  182.  
  183.             if ((fontfp == (FILE*)NULL) && fontsub(subfont,&submag,
  184.             fontname,(int)MAGSIZE(mag_table[mag_index+i])))
  185.             {
  186.             (void)fontfile(filelist,((fontptr->a==0)?fontpath:""),
  187.                 subfont,(submag ? submag :
  188.                 (int)MAGSIZE(mag_table[mag_index+i])));
  189.             for (j = 0; (j < MAXFORMATS) && *filelist[j]; ++j)
  190.             {
  191.                 fontfp = FOPEN(filelist[j],RB_OPEN);
  192.                 DEBUG_OPEN(fontfp,filelist[j],RB_OPEN);
  193.                 if (fontfp != (FILE *)NULL)
  194.                 {
  195.                 strcpy(fontptr->name,filelist[j]);
  196.                 break;
  197.                 }
  198.             }
  199.  
  200.             if (fontfp != (FILE*)NULL)
  201.             {
  202.                 (void)sprintf(message,
  203.                 "Substituting font file [%s [mag %d]] \
  204. by [%s [mag %d]]",
  205.                 fontname,(int)MAGSIZE(mag_table[mag_index]),
  206.                 fontptr->name,
  207.                 (int)MAGSIZE(mag_table[mag_index+i]));
  208.                 (void)warning(message);
  209.             }
  210.             }
  211.         }
  212.         } /* end for (i) -- loop over smaller and larger neighbors  */
  213.     } /* end for (k) -- loop over mag family */
  214.  
  215.     if (fontfp == (FILE*)NULL)
  216.     {
  217.         --nopen;            /* don't count this failed open */
  218.         (void)sprintf(message,"Font file [%s [mag %d]] could not be \
  219. opened; %d font files are open\n\
  220. Proceeding with zero size characters for this font",
  221.         fontname,(int)MAGSIZE(mag_table[mag_index]),nopen);
  222.         (void)warning(message);
  223.     }
  224.  
  225.     font_files[current].font_id = fontfp;
  226.     font_files[current].use_count = 0;
  227.  
  228. #if    VIRTUAL_FONTS
  229.     /*
  230.     ****************************************************************
  231.     This code  is implementation-dependent.   On many  C  compilers,
  232.     FILE points to a struct of the form
  233.  
  234.     struct    _iobuf {
  235.         char    *_ptr;    // pointer to next available char
  236.         int    _cnt;    // number of chars left in buffer
  237.         char    *_base;    // pointer to start of buffer
  238.         int    _flag;    // assorted flags
  239.         int    _file;    // file number handle
  240.         }
  241.  
  242.     To implement virtual fonts,  we save the  pointers in a  private
  243.     global array, get the file size from fstat(), malloc() a  buffer
  244.     for it, read the entire file in, and replace the pointers in the
  245.     FILE variable with ones  for  the  new buffer.  Just before  the
  246.     file is  closed, the space is  freed  and the   old pointers are
  247.     restored.   Although many C implementations use  malloc() to get
  248.     the buffer  space  in the  first place, which  would  make  this
  249.     saving unnecessary, not all do; see  the implementation given in
  250.     Kernighan and Ritchie "The C Programming Language", p. 168.
  251.  
  252.     In  implementing   this   code   on   any   new   machine,   the
  253.     implementations of fread(), read(), fseek(), and rewind() should
  254.     be checked to  choose the  most efficient one  (e.g. one  system
  255.     call for entire file  read, and no  double buffering), and  make
  256.     sure that  fseek() and  rewind()  do not  unnecessarily  discard
  257.     input buffer  contents.  fseek()  and  rewind() are  defined  in
  258.     terms of macros FSEEK() and REWIND() in machdefs.h to facilitate
  259.     replacement.
  260.     ****************************************************************
  261.     */
  262.     if (virt_font)
  263.     {
  264.         virt_save[fileno(fontfp)].base = (char *)NULL;
  265.         (void)fstat(fileno(fontfp),&statbuf);    /* get file size */
  266.         p = (char *)MALLOC((int)statbuf.st_size);    /* get file buffer */
  267.         if (p != (char *)NULL)
  268.         {
  269.         if (READ(fileno(fontfp),p,(int)statbuf.st_size)
  270.              == (int)statbuf.st_size)
  271.         {            /* read successful */
  272.             virt_save[fileno(fontfp)].ptr = FILE_PTR(fontfp);
  273.             virt_save[fileno(fontfp)].cnt = FILE_CNT(fontfp);
  274.             virt_save[fileno(fontfp)].base = FILE_BASE(fontfp);
  275.             FILE_PTR(fontfp) = p;
  276.             FILE_BASE(fontfp) = p;
  277.             FILE_CNT(fontfp) = (int)statbuf.st_size;
  278.         }
  279.         else            /* failure */
  280.         {
  281.             (void)REWIND(fontfp);
  282.             (void)free(p);    /* free dynamic buffer */
  283.         }
  284.         }
  285.         if (DBGOPT(DBG_FONT_CACHE))
  286.         {
  287.             (void)fprintf(stderr,"\nopenfont(): Font file %d [%s] ",
  288.             (int)fileno(fontfp),fontptr->name);
  289.             if (p == (char *)NULL)
  290.             (void)fprintf(stderr,
  291.             "cannot callocate buffer for entire file\n");
  292.         else
  293.             (void)fprintf(stderr,
  294.             "buffer length 0x%x\told buffer at 0x%lx\t\
  295. new buffer at 0x%lx\n",
  296.             (int)statbuf.st_size,
  297.             (long)virt_save[fileno(fontfp)].base,
  298.             (long)FILE_BASE(fontfp));
  299.         }
  300.     }
  301. #endif
  302.  
  303.     } /* end if (file is in open list) */
  304.  
  305.     pfontptr = fontptr;            /* make previous = current font */
  306.     fontptr->font_file_id = fontfp;    /* set file identifier */
  307.     font_files[current].use_count++;    /* update reference count */
  308. }
  309.  
  310. #if    VIRTUAL_FONTS
  311. void
  312. virtfree(fp)                /* free buffer space before close */
  313. FILE *fp;
  314. {
  315.     if (virt_font && (fp != (FILE*)NULL) &&
  316.     (virt_save[fileno(fp)].base != (char *)NULL))
  317.     {
  318.     (void)fflush(fp);
  319.     (void)free(FILE_BASE(fp));
  320.     FILE_PTR(fp) = virt_save[fileno(fp)].ptr;
  321.     FILE_CNT(fp) = virt_save[fileno(fp)].cnt;
  322.     FILE_BASE(fp) = virt_save[fileno(fp)].base;
  323.     virt_save[fileno(fp)].base = (char *)NULL;
  324.     }
  325. }
  326. #endif
  327.  
  328.