home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / octave-1.1.1p1-base.tgz / octave-1.1.1p1-base.tar / fsf / octave / kpathsea / fontmap.c < prev    next >
C/C++ Source or Header  |  1994-03-03  |  4KB  |  134 lines

  1. /* fontmap.c: read a file for additional font names.
  2.  
  3. Copyright (C) 1993, 94 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include <kpathsea/config.h>
  20.  
  21. #include <kpathsea/c-fopen.h>
  22. #include <kpathsea/fontmap.h>
  23. #include <kpathsea/hash.h>
  24. #include <kpathsea/line.h>
  25. #include <kpathsea/pathsearch.h>
  26. #include <kpathsea/str-list.h>
  27.  
  28.  
  29. /* Look up KEY in MAP; if it's not found, remove any suffix from KEY and
  30.    try again.  */
  31.  
  32. string *
  33. map_lookup P2C(hash_table_type, map,  const_string, key)
  34. {
  35.   string suffix = find_suffix (key);
  36.   string *ret = hash_lookup (map, key);
  37.   
  38.   if (!ret)
  39.     {
  40.       /* OK, the original KEY didn't work.  Let's check for the KEY without
  41.          an extension -- perhaps they gave foobar.tfm, but the mapping only
  42.          defines `foobar'.  */
  43.       if (suffix)
  44.         {
  45.           string base_key = remove_suffix (key);
  46.           
  47.           ret = hash_lookup (map, base_key);
  48.  
  49.           free (base_key);
  50.         }
  51.     }
  52.  
  53.   /* Append the original suffix, if we had one.  */
  54.   if (ret && suffix)
  55.     while (*ret)
  56.       {
  57.        *ret = extend_filename (*ret, suffix);
  58.        ret++;
  59.       }
  60.  
  61.   return ret;
  62. }
  63.  
  64. /* Open and read the mapping file MAP_FILENAME, putting its entries into
  65.    MAP. Comments begin with % and continue to the end of the line.  Each
  66.    line of the file defines an entry: the first word is the real
  67.    filename (e.g., `ptmr'), the second word is the alias (e.g.,
  68.    `Times-Roman'), and any subsequent words are ignored.  .tfm is added
  69.    if either the filename or the alias have no extension.  This is the
  70.    same order as in Dvips' psfonts.map; unfortunately, we can't have TeX
  71.    read that same file, since most of the real filenames start with an
  72.    `r', because of the virtual fonts Dvips uses.  */
  73.  
  74. static void
  75. map_file_parse P2C(hash_table_type *, map,  const_string, map_filename)
  76. {
  77.   extern FILE *xfopen ();    /* In xfopen.c.  */
  78.   char *l;
  79.   unsigned map_lineno = 0;
  80.   FILE *f = xfopen (map_filename, FOPEN_R_MODE);
  81.   
  82.   while ((l = read_line (f)) != NULL)
  83.     {
  84.       string filename;
  85.       string comment_loc = strchr (l, '%');
  86.       
  87.       map_lineno++;
  88.       
  89.       /* Ignore anything after a %.  */
  90.       if (comment_loc)
  91.         *comment_loc = 0;
  92.       
  93.       /* If we don't have any filename, that's ok, the line is blank.  */
  94.       filename = strtok (l, " \t");
  95.       if (filename)
  96.         {
  97.           string alias = strtok (NULL, " \t");
  98.           
  99.           /* But if we have a filename and no alias, something's wrong.  */
  100.           if (alias == NULL || *alias == 0)
  101.             fprintf (stderr, "%s:%u: Alias missing for filename `%s'.\n",
  102.                      map_filename, map_lineno, filename);
  103.           else
  104.             { /* We've got everything.  Insert the new entry.  */
  105.               hash_insert (map, xstrdup (alias), xstrdup (filename));
  106.             }
  107.         }
  108.       
  109.       free (l);
  110.     }
  111.   
  112.   xfclose (f, map_filename);
  113. }
  114.  
  115. /* Search for all the MAP_NAME's in PATH.  */
  116.  
  117. #define MAP_NAME "texfonts.map"
  118.  
  119. hash_table_type
  120. map_create P1C(const_string, path)
  121. {
  122.   string *filenames = kpse_all_path_search (path, MAP_NAME);
  123.   hash_table_type map; /* some old compilers ... */
  124.   map = hash_create (751);
  125.   
  126.   while (*filenames)
  127.     {
  128.       map_file_parse (&map, *filenames);
  129.       filenames++;
  130.     }
  131.  
  132.   return map;
  133. }
  134.