1
/* fontmap.c: read files for additional font names.
3
Copyright 2001, 2002, 2005 Olaf Weber.
4
Copyright 1993, 94, 95, 96, 97 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
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include <kpathsea/config.h>
24
#include <kpathsea/c-ctype.h>
25
#include <kpathsea/c-fopen.h>
26
#include <kpathsea/fontmap.h>
27
#include <kpathsea/hash.h>
28
#include <kpathsea/line.h>
29
#include <kpathsea/pathsearch.h>
30
#include <kpathsea/str-list.h>
31
#include <kpathsea/tex-file.h>
33
/* We have one and only one fontmap, so may as well make it static
34
instead of passing it around. */
35
static hash_table_type map;
37
#define MAP_NAME "texfonts.map"
40
#define MAP_HASH_SIZE 4001
43
static const_string map_path; /* Only want to create this once. */
45
/* Return next whitespace-delimited token in STR or NULL if none. */
48
token P1C(const_string, str)
54
while (*str && ISSPACE (*str))
58
while (*str && !ISSPACE (*str))
62
ret = (string)xmalloc (len + 1);
63
strncpy (ret, start, len);
69
/* Open and read the mapping file MAP_FILENAME, putting its entries into
70
MAP. Comments begin with % and continue to the end of the line. Each
71
line of the file defines an entry: the first word is the real
72
filename (e.g., `ptmr'), the second word is the alias (e.g.,
73
`Times-Roman'), and any subsequent words are ignored. .tfm is added
74
if either the filename or the alias have no extension. This is the
75
same order as in Dvips' psfonts.map. Perhaps someday the programs
76
will both read the same file. */
79
map_file_parse P1C(const_string, map_filename)
82
unsigned map_lineno = 0;
83
FILE *f = xfopen (map_filename, FOPEN_R_MODE);
85
while ((orig_l = read_line (f)) != NULL) {
88
string comment_loc = strrchr (l, '%');
90
comment_loc = strstr (l, "@c");
93
/* Ignore anything after a % or @c. */
99
/* Skip leading whitespace so we can use strlen below. Can't use
100
strtok since this routine is recursive. */
101
while (*l && ISSPACE (*l))
104
/* If we don't have any filename, that's ok, the line is blank. */
105
filename = token (l);
107
string alias = token (l + strlen (filename));
109
if (STREQ (filename, "include")) {
111
WARNING2 ("%s:%u: Filename argument for include directive missing",
112
map_filename, map_lineno);
114
string include_fname = kpse_path_search (map_path, alias, false);
116
map_file_parse (include_fname);
117
if (include_fname != alias)
118
free (include_fname);
120
WARNING3 ("%s:%u: Can't find fontname include file `%s'",
121
map_filename, map_lineno, alias);
127
/* But if we have a filename and no alias, something's wrong. */
128
} else if (alias == NULL) {
129
WARNING3 ("%s:%u: Fontname alias missing for filename `%s'",
130
map_filename, map_lineno, filename);
134
/* We've got everything. Insert the new entry. They were
135
already dynamically allocated, so don't bother with xstrdup. */
136
hash_insert_normalized (&map, alias, filename);
143
xfclose (f, map_filename);
146
/* Parse the file MAP_NAME in each of the directories in PATH and
147
return the resulting structure. Entries in earlier files override
151
read_all_maps P1H(void)
155
map_path = kpse_init_format (kpse_fontmap_format);
156
filenames = kpse_all_path_search (map_path, MAP_NAME);
158
map = hash_create (MAP_HASH_SIZE);
161
map_file_parse (*filenames);
166
/* Look up KEY in texfonts.map's; if it's not found, remove any suffix
167
from KEY and try again. Create the map if necessary. */
170
kpse_fontmap_lookup P1C(const_string, key)
173
string suffix = find_suffix (key);
179
ret = hash_lookup (map, key);
181
/* OK, the original KEY didn't work. Let's check for the KEY without
182
an extension -- perhaps they gave foobar.tfm, but the mapping only
185
string base_key = remove_suffix (key);
186
ret = hash_lookup (map, base_key);
191
/* Append any original suffix. */
194
for (elt = ret; *elt; elt++) {
195
*elt = extend_filename (*elt, suffix);