1 /* fontmap.c: read files for additional font names.
3 Copyright 2001, 2002, 2005 Olaf Weber.
4 Copyright 1993, 1994, 1995, 1996, 1997, 2008 Karl Berry.
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.
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.
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/>. */
19 #include <kpathsea/config.h>
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>
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;
35 #define MAP_NAME "texfonts.map"
38 #define MAP_HASH_SIZE 4001
41 static const_string map_path; /* Only want to create this once. */
43 /* Return next whitespace-delimited token in STR or NULL if none. */
46 token P1C(const_string, str)
52 while (*str && ISSPACE (*str))
56 while (*str && !ISSPACE (*str))
60 ret = (string)xmalloc (len + 1);
61 strncpy (ret, start, len);
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. */
77 map_file_parse P1C(const_string, map_filename)
80 unsigned map_lineno = 0;
81 FILE *f = xfopen (map_filename, FOPEN_R_MODE);
83 if (kpse_record_input)
84 kpse_record_input (map_filename);
86 while ((orig_l = read_line (f)) != NULL) {
88 string l = orig_l; /* save for free() */
89 string comment_loc = strrchr (l, '%');
91 comment_loc = strstr (l, "@c");
94 /* Ignore anything after a % or @c. */
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))
105 /* If we don't have any filename, that's ok, the line is blank. */
106 filename = token (l);
108 string alias = token (l + strlen (filename));
110 if (STREQ (filename, "include")) {
112 WARNING2 ("%s:%u: Filename argument for include directive missing",
113 map_filename, map_lineno);
115 string include_fname = kpse_path_search (map_path, alias, false);
117 map_file_parse (include_fname);
118 if (include_fname != alias)
119 free (include_fname);
121 WARNING3 ("%s:%u: Can't find fontname include file `%s'",
122 map_filename, map_lineno, alias);
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);
135 /* We've got everything. Insert the new entry. They were
136 already dynamically allocated by token(), so don't bother
138 hash_insert_normalized (&map, alias, filename);
145 xfclose (f, map_filename);
148 /* Parse the file MAP_NAME in each of the directories in PATH and
149 return the resulting structure. Entries in earlier files override
153 read_all_maps P1H(void)
157 map_path = kpse_init_format (kpse_fontmap_format);
158 filenames = kpse_all_path_search (map_path, MAP_NAME);
160 map = hash_create (MAP_HASH_SIZE);
163 map_file_parse (*filenames);
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. */
172 kpse_fontmap_lookup P1C(const_string, key)
175 string suffix = find_suffix (key);
181 ret = hash_lookup (map, key);
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
187 string base_key = remove_suffix (key);
188 ret = hash_lookup (map, base_key);
193 /* Append any original suffix. */
196 for (elt = ret; *elt; elt++) {
197 *elt = extend_filename (*elt, suffix);