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.h < prev    next >
Text File  |  1993-08-06  |  11KB  |  333 lines

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