| 1 |
/* fontmap.c: read files for additional font names. |
| 2 |
|
| 3 |
Copyright 1993, 1994, 1995, 1996, 1997, 2008, 2011-2013 Karl Berry. |
| 4 |
Copyright 2001, 2002, 2005 Olaf Weber. |
| 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/tex-file.h> |
| 29 |
|
| 30 |
/* We have one and only one fontmap, so may as well make it static |
| 31 |
instead of passing it around. */ |
| 32 |
|
| 33 |
#ifndef MAP_NAME |
| 34 |
#define MAP_NAME "texfonts.map" |
| 35 |
#endif |
| 36 |
#ifndef MAP_HASH_SIZE |
| 37 |
#define MAP_HASH_SIZE 4001 |
| 38 |
#endif |
| 39 |
|
| 40 |
|
| 41 |
/* Return next whitespace-delimited token in STR or NULL if none. */ |
| 42 |
|
| 43 |
static string |
| 44 |
token (const_string str) |
| 45 |
{ |
| 46 |
unsigned len; |
| 47 |
const_string start; |
| 48 |
string ret; |
| 49 |
|
| 50 |
while (*str && ISSPACE (*str)) |
| 51 |
str++; |
| 52 |
|
| 53 |
start = str; |
| 54 |
while (*str && !ISSPACE (*str)) |
| 55 |
str++; |
| 56 |
|
| 57 |
len = str - start; |
| 58 |
ret = (string)xmalloc (len + 1); |
| 59 |
strncpy (ret, start, len); |
| 60 |
ret[len] = 0; |
| 61 |
|
| 62 |
return ret; |
| 63 |
} |
| 64 |
|
| 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. */ |
| 73 |
|
| 74 |
static void |
| 75 |
map_file_parse (kpathsea kpse, const_string map_filename) |
| 76 |
{ |
| 77 |
char *orig_l; |
| 78 |
unsigned map_lineno = 0; |
| 79 |
FILE *f = xfopen (map_filename, FOPEN_R_MODE); |
| 80 |
|
| 81 |
if (kpse->record_input) |
| 82 |
kpse->record_input (map_filename); |
| 83 |
|
| 84 |
while ((orig_l = read_line (f)) != NULL) { |
| 85 |
string filename; |
| 86 |
string l = orig_l; /* save for free() */ |
| 87 |
string comment_loc = strrchr (l, '%'); |
| 88 |
if (!comment_loc) { |
| 89 |
comment_loc = strstr (l, "@c"); |
| 90 |
} |
| 91 |
|
| 92 |
/* Ignore anything after a % or @c. */ |
| 93 |
if (comment_loc) |
| 94 |
*comment_loc = 0; |
| 95 |
|
| 96 |
map_lineno++; |
| 97 |
|
| 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)) |
| 101 |
l++; |
| 102 |
|
| 103 |
/* If we don't have any filename, that's ok, the line is blank. */ |
| 104 |
filename = token (l); |
| 105 |
if (filename) { |
| 106 |
string alias = token (l + strlen (filename)); |
| 107 |
|
| 108 |
if (STREQ (filename, "include")) { |
| 109 |
if (alias == NULL) { |
| 110 |
WARNING2 ("kpathsea: %s:%u: Filename argument for include directive missing", |
| 111 |
map_filename, map_lineno); |
| 112 |
} else { |
| 113 |
string include_fname = kpathsea_path_search (kpse, |
| 114 |
kpse->map_path, alias, false); |
| 115 |
if (include_fname) { |
| 116 |
map_file_parse (kpse, include_fname); |
| 117 |
if (include_fname != alias) |
| 118 |
free (include_fname); |
| 119 |
} else { |
| 120 |
WARNING3 ("kpathsea: %s:%u: Can't find fontname include file `%s'", |
| 121 |
map_filename, map_lineno, alias); |
| 122 |
} |
| 123 |
free (alias); |
| 124 |
free (filename); |
| 125 |
} |
| 126 |
|
| 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); |
| 131 |
free (filename); |
| 132 |
|
| 133 |
} else { |
| 134 |
/* We've got everything. Insert the new entry. They were |
| 135 |
already dynamically allocated by token(), so don't bother |
| 136 |
with xstrdup. */ |
| 137 |
hash_insert_normalized (&(kpse->map), alias, filename); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
free (orig_l); |
| 142 |
} |
| 143 |
|
| 144 |
xfclose (f, map_filename); |
| 145 |
} |
| 146 |
|
| 147 |
/* Parse the file MAP_NAME in each of the directories in PATH and |
| 148 |
return the resulting structure. Entries in earlier files override |
| 149 |
later files. */ |
| 150 |
|
| 151 |
static void |
| 152 |
read_all_maps (kpathsea kpse) |
| 153 |
{ |
| 154 |
string *filenames; |
| 155 |
|
| 156 |
kpse->map_path = kpathsea_init_format (kpse, kpse_fontmap_format); |
| 157 |
filenames = kpathsea_all_path_search (kpse, kpse->map_path, MAP_NAME); |
| 158 |
|
| 159 |
kpse->map = hash_create (MAP_HASH_SIZE); |
| 160 |
|
| 161 |
while (*filenames) { |
| 162 |
map_file_parse (kpse, *filenames); |
| 163 |
filenames++; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 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. */ |
| 169 |
|
| 170 |
const_string * |
| 171 |
kpathsea_fontmap_lookup (kpathsea kpse, const_string key) |
| 172 |
{ |
| 173 |
const_string *ret; |
| 174 |
const_string suffix = find_suffix (key); |
| 175 |
|
| 176 |
if (kpse->map.size == 0) { |
| 177 |
read_all_maps (kpse); |
| 178 |
} |
| 179 |
|
| 180 |
ret = hash_lookup (kpse->map, key); |
| 181 |
if (!ret) { |
| 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 |
| 184 |
defines `foobar'. */ |
| 185 |
if (suffix) { |
| 186 |
string base_key = remove_suffix (key); |
| 187 |
ret = hash_lookup (kpse->map, base_key); |
| 188 |
free (base_key); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/* Append any original suffix. */ |
| 193 |
if (ret && suffix) { |
| 194 |
const_string *elt; |
| 195 |
for (elt = ret; *elt; elt++) { |
| 196 |
*elt = extend_filename (*elt, suffix); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return ret; |
| 201 |
} |