dvitomp fix from Akira
[mplib] / src / texk / kpathsea / fontmap.c
1 /* fontmap.c: read files for additional font names.
2
3    Copyright 2001, 2002, 2005 Olaf Weber.
4    Copyright 1993, 1994, 1995, 1996, 1997, 2008 Karl Berry.
5
6    This library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    This library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public License
17    along with this library; if not, see <http://www.gnu.org/licenses/>.  */
18
19 #include <kpathsea/config.h>
20
21 #include <kpathsea/c-ctype.h>
22 #include <kpathsea/c-fopen.h>
23 #include <kpathsea/fontmap.h>
24 #include <kpathsea/hash.h>
25 #include <kpathsea/line.h>
26 #include <kpathsea/pathsearch.h>
27 #include <kpathsea/str-list.h>
28 #include <kpathsea/recorder.h>
29 #include <kpathsea/tex-file.h>
30
31 /* We have one and only one fontmap, so may as well make it static
32    instead of passing it around.  */
33 static hash_table_type map;
34 #ifndef MAP_NAME
35 #define MAP_NAME "texfonts.map"
36 #endif
37 #ifndef MAP_HASH_SIZE
38 #define MAP_HASH_SIZE 4001
39 #endif
40
41 static const_string map_path; /* Only want to create this once. */
42 \f
43 /* Return next whitespace-delimited token in STR or NULL if none.  */
44
45 static string
46 token P1C(const_string, str)
47 {
48   unsigned len;
49   const_string start;
50   string ret;
51   
52   while (*str && ISSPACE (*str))
53     str++;
54   
55   start = str;
56   while (*str && !ISSPACE (*str))
57     str++;
58   
59   len = str - start;
60   ret = (string)xmalloc (len + 1);
61   strncpy (ret, start, len);
62   ret[len] = 0;
63   
64   return ret;
65 }
66
67 /* Open and read the mapping file MAP_FILENAME, putting its entries into
68    MAP. Comments begin with % and continue to the end of the line.  Each
69    line of the file defines an entry: the first word is the real
70    filename (e.g., `ptmr'), the second word is the alias (e.g.,
71    `Times-Roman'), and any subsequent words are ignored.  .tfm is added
72    if either the filename or the alias have no extension.  This is the
73    same order as in Dvips' psfonts.map.  Perhaps someday the programs
74    will both read the same file.  */
75
76 static void
77 map_file_parse P1C(const_string, map_filename)
78 {
79   char *orig_l;
80   unsigned map_lineno = 0;
81   FILE *f = xfopen (map_filename, FOPEN_R_MODE);
82
83   if (kpse_record_input)
84     kpse_record_input (map_filename);
85
86   while ((orig_l = read_line (f)) != NULL) {
87     string filename;
88     string l = orig_l; /* save for free() */
89     string comment_loc = strrchr (l, '%');
90     if (!comment_loc) {
91       comment_loc = strstr (l, "@c");
92     }
93     
94     /* Ignore anything after a % or @c.  */
95     if (comment_loc)
96       *comment_loc = 0;
97
98     map_lineno++;
99
100     /* Skip leading whitespace so we can use strlen below.  Can't use
101        strtok since this routine is recursive.  */
102     while (*l && ISSPACE (*l))
103       l++;
104       
105     /* If we don't have any filename, that's ok, the line is blank.  */
106     filename = token (l);
107     if (filename) {
108       string alias = token (l + strlen (filename));
109
110       if (STREQ (filename, "include")) {
111         if (alias == NULL) {
112           WARNING2 ("%s:%u: Filename argument for include directive missing",
113                     map_filename, map_lineno);
114         } else {
115           string include_fname = kpse_path_search (map_path, alias, false);
116           if (include_fname) {
117             map_file_parse (include_fname);
118             if (include_fname != alias)
119               free (include_fname);
120           } else {
121             WARNING3 ("%s:%u: Can't find fontname include file `%s'",
122                       map_filename, map_lineno, alias);
123           }
124           free (alias);
125           free (filename);
126         }
127
128       /* But if we have a filename and no alias, something's wrong.  */
129       } else if (alias == NULL) {
130         WARNING3 ("%s:%u: Fontname alias missing for filename `%s'",
131                   map_filename, map_lineno, filename);
132         free (filename);
133
134       } else {
135         /* We've got everything.  Insert the new entry.  They were
136            already dynamically allocated by token(), so don't bother
137            with xstrdup.  */
138         hash_insert_normalized (&map, alias, filename);
139       }
140     }
141
142     free (orig_l);
143   }
144   
145   xfclose (f, map_filename);
146 }
147 \f
148 /* Parse the file MAP_NAME in each of the directories in PATH and
149    return the resulting structure.  Entries in earlier files override
150    later files.  */
151
152 static void
153 read_all_maps P1H(void)
154 {
155   string *filenames;
156   
157   map_path = kpse_init_format (kpse_fontmap_format);
158   filenames = kpse_all_path_search (map_path, MAP_NAME);
159   
160   map = hash_create (MAP_HASH_SIZE);
161
162   while (*filenames) {
163     map_file_parse (*filenames);
164     filenames++;
165   }
166 }
167 \f
168 /* Look up KEY in texfonts.map's; if it's not found, remove any suffix
169    from KEY and try again.  Create the map if necessary.  */
170
171 string *
172 kpse_fontmap_lookup P1C(const_string, key)
173 {
174   string *ret;
175   string suffix = find_suffix (key);
176   
177   if (map.size == 0) {
178     read_all_maps ();
179   }
180
181   ret = hash_lookup (map, key);
182   if (!ret) {
183     /* OK, the original KEY didn't work.  Let's check for the KEY without
184        an extension -- perhaps they gave foobar.tfm, but the mapping only
185        defines `foobar'.  */
186     if (suffix) {
187       string base_key = remove_suffix (key);
188       ret = hash_lookup (map, base_key);
189       free (base_key);
190     }
191   }
192
193   /* Append any original suffix.  */
194   if (ret && suffix) {
195     string *elt;
196     for (elt = ret; *elt; elt++) {
197       *elt = extend_filename (*elt, suffix);
198     }
199   }
200
201   return ret;
202 }