1 /* fontmap.c: read files for additional font names.
3 Copyright 1993, 1994, 1995, 1996, 1997, 2008, 2011-2013 Karl Berry.
4 Copyright 2001, 2002, 2005 Olaf Weber.
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/tex-file.h>
30 /* We have one and only one fontmap, so may as well make it static
31 instead of passing it around. */
34 #define MAP_NAME "texfonts.map"
37 #define MAP_HASH_SIZE 4001
41 /* Return next whitespace-delimited token in STR or NULL if none. */
44 token (const_string str
)
50 while (*str
&& ISSPACE (*str
))
54 while (*str
&& !ISSPACE (*str
))
58 ret
= (string
)xmalloc (len
+ 1);
59 strncpy (ret
, start
, len
);
65 /* Open and read the mapping file MAP_FILENAME, putting its entries into
66 MAP. Comments begin with % and continue to the end of the line. Each
67 line of the file defines an entry: the first word is the real
68 filename (e.g., `ptmr'), the second word is the alias (e.g.,
69 `Times-Roman'), and any subsequent words are ignored. .tfm is added
70 if either the filename or the alias have no extension. This is the
71 same order as in Dvips' psfonts.map. Perhaps someday the programs
72 will both read the same file. */
75 map_file_parse (kpathsea kpse
, const_string map_filename
)
78 unsigned map_lineno
= 0;
79 FILE *f
= xfopen (map_filename
, FOPEN_R_MODE
);
81 if (kpse
->record_input
)
82 kpse
->record_input (map_filename
);
84 while ((orig_l
= read_line (f
)) != NULL
) {
86 string l
= orig_l
; /* save for free() */
87 string comment_loc
= strrchr (l
, '%');
89 comment_loc
= strstr (l
, "@c");
92 /* Ignore anything after a % or @c. */
98 /* Skip leading whitespace so we can use strlen below. Can't use
99 strtok since this routine is recursive. */
100 while (*l
&& ISSPACE (*l
))
103 /* If we don't have any filename, that's ok, the line is blank. */
104 filename
= token (l
);
106 string alias
= token (l
+ strlen (filename
));
108 if (STREQ (filename
, "include")) {
110 WARNING2 ("kpathsea: %s:%u: Filename argument for include directive missing",
111 map_filename
, map_lineno
);
113 string include_fname
= kpathsea_path_search (kpse
,
114 kpse
->map_path
, alias
, false);
116 map_file_parse (kpse
, include_fname
);
117 if (include_fname
!= alias
)
118 free (include_fname
);
120 WARNING3 ("kpathsea: %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 ("kpathsea: %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 by token(), so don't bother
137 hash_insert_normalized (&(kpse
->map
), alias
, filename
);
144 xfclose (f
, map_filename
);
147 /* Parse the file MAP_NAME in each of the directories in PATH and
148 return the resulting structure. Entries in earlier files override
152 read_all_maps (kpathsea kpse
)
156 kpse
->map_path
= kpathsea_init_format (kpse
, kpse_fontmap_format
);
157 filenames
= kpathsea_all_path_search (kpse
, kpse
->map_path
, MAP_NAME
);
159 kpse
->map
= hash_create (MAP_HASH_SIZE
);
162 map_file_parse (kpse
, *filenames
);
167 /* Look up KEY in texfonts.map's; if it's not found, remove any suffix
168 from KEY and try again. Create the map if necessary. */
171 kpathsea_fontmap_lookup (kpathsea kpse
, const_string key
)
174 const_string suffix
= find_suffix (key
);
176 if (kpse
->map
.size
== 0) {
177 read_all_maps (kpse
);
180 ret
= hash_lookup (kpse
->map
, key
);
182 /* OK, the original KEY didn't work. Let's check for the KEY without
183 an extension -- perhaps they gave foobar.tfm, but the mapping only
186 string base_key
= remove_suffix (key
);
187 ret
= hash_lookup (kpse
->map
, base_key
);
192 /* Append any original suffix. */
195 for (elt
= ret
; *elt
; elt
++) {
196 *elt
= extend_filename (*elt
, suffix
);