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