]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
317b8492 | 2 | * fontconfig/src/fcdir.c |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2000 Keith Packard |
24330d27 KP |
5 | * |
6 | * Permission to use, copy, modify, distribute, and sell this software and its | |
7 | * documentation for any purpose is hereby granted without fee, provided that | |
8 | * the above copyright notice appear in all copies and that both that | |
9 | * copyright notice and this permission notice appear in supporting | |
5aaf466d | 10 | * documentation, and that the name of the author(s) not be used in |
24330d27 | 11 | * advertising or publicity pertaining to distribution of the software without |
5aaf466d | 12 | * specific, written prior permission. The authors make no |
24330d27 KP |
13 | * representations about the suitability of this software for any purpose. It |
14 | * is provided "as is" without express or implied warranty. | |
15 | * | |
3074a73b | 16 | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, |
24330d27 | 17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO |
3074a73b | 18 | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR |
24330d27 KP |
19 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, |
20 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
21 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
22 | * PERFORMANCE OF THIS SOFTWARE. | |
23 | */ | |
24 | ||
24330d27 | 25 | #include "fcint.h" |
179c3995 KP |
26 | #include <dirent.h> |
27 | ||
2d9c79c0 | 28 | FcBool |
179c3995 | 29 | FcFileIsDir (const FcChar8 *file) |
d0471dd2 MF |
30 | { |
31 | return FcFileIsDir2 (FcConfigGetCurrent (), file); | |
32 | } | |
33 | ||
34 | FcBool | |
35 | FcFileIsDir2 (FcConfig *config, const FcChar8 *file) | |
179c3995 KP |
36 | { |
37 | struct stat statb; | |
24330d27 | 38 | |
d0471dd2 | 39 | if (FcStat (config, file, &statb) != 0) |
179c3995 KP |
40 | return FcFalse; |
41 | return S_ISDIR(statb.st_mode); | |
42 | } | |
24330d27 | 43 | |
00f059e9 KP |
44 | static FcBool |
45 | FcFileScanFontConfig (FcFontSet *set, | |
46 | FcBlanks *blanks, | |
47 | const FcChar8 *file, | |
48 | FcConfig *config) | |
24330d27 | 49 | { |
00f059e9 KP |
50 | FcPattern *font; |
51 | FcBool ret = FcTrue; | |
52 | int id; | |
53 | int count = 0; | |
594dcef0 | 54 | |
24330d27 KP |
55 | id = 0; |
56 | do | |
57 | { | |
327a7fd4 KP |
58 | font = 0; |
59 | /* | |
eb0cf671 | 60 | * Nothing in the cache, scan the file |
327a7fd4 | 61 | */ |
eb0cf671 | 62 | if (FcDebug () & FC_DBG_SCAN) |
24330d27 | 63 | { |
eb0cf671 PL |
64 | printf ("\tScanning file %s...", file); |
65 | fflush (stdout); | |
24330d27 | 66 | } |
d0471dd2 | 67 | font = FcFreeTypeQuery2 (config, file, id, blanks, &count); |
eb0cf671 PL |
68 | if (FcDebug () & FC_DBG_SCAN) |
69 | printf ("done\n"); | |
c2c6976d KP |
70 | |
71 | /* | |
72 | * Edit pattern with user-defined rules | |
73 | */ | |
0fcf866d | 74 | if (font && config && !FcConfigSubstitute (config, font, FcMatchScan)) |
c2c6976d KP |
75 | { |
76 | FcPatternDestroy (font); | |
77 | font = NULL; | |
78 | ret = FcFalse; | |
79 | } | |
80 | ||
327a7fd4 KP |
81 | /* |
82 | * Add the font | |
83 | */ | |
0fcf866d | 84 | if (font) |
24330d27 | 85 | { |
c2c6976d KP |
86 | if (FcDebug() & FC_DBG_SCANV) |
87 | { | |
88 | printf ("Final font pattern:\n"); | |
89 | FcPatternPrint (font); | |
90 | } | |
24330d27 KP |
91 | if (!FcFontSetAdd (set, font)) |
92 | { | |
93 | FcPatternDestroy (font); | |
c2c6976d | 94 | font = NULL; |
24330d27 KP |
95 | ret = FcFalse; |
96 | } | |
97 | } | |
47b49bf1 KP |
98 | else if (font) |
99 | FcPatternDestroy (font); | |
24330d27 KP |
100 | id++; |
101 | } while (font && ret && id < count); | |
102 | return ret; | |
103 | } | |
104 | ||
00f059e9 KP |
105 | FcBool |
106 | FcFileScanConfig (FcFontSet *set, | |
107 | FcStrSet *dirs, | |
108 | FcBlanks *blanks, | |
109 | const FcChar8 *file, | |
00f059e9 KP |
110 | FcConfig *config) |
111 | { | |
d0471dd2 | 112 | if (FcFileIsDir2 (config, file)) |
00f059e9 KP |
113 | return FcStrSetAdd (dirs, file); |
114 | else | |
115 | return FcFileScanFontConfig (set, blanks, file, config); | |
116 | } | |
117 | ||
d47c9d6e KP |
118 | FcBool |
119 | FcFileScan (FcFontSet *set, | |
120 | FcStrSet *dirs, | |
00f059e9 | 121 | FcFileCache *cache, /* XXX unused */ |
d47c9d6e KP |
122 | FcBlanks *blanks, |
123 | const FcChar8 *file, | |
124 | FcBool force) | |
125 | { | |
5e544b32 | 126 | return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ()); |
d47c9d6e KP |
127 | } |
128 | ||
e3c6d336 PL |
129 | /* |
130 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
131 | */ | |
e3c6d336 PL |
132 | static int |
133 | cmpstringp(const void *p1, const void *p2) | |
134 | { | |
135 | return strcmp(* (char **) p1, * (char **) p2); | |
136 | } | |
137 | ||
4074fd25 BE |
138 | FcBool |
139 | FcDirScanConfig (FcFontSet *set, | |
140 | FcStrSet *dirs, | |
141 | FcBlanks *blanks, | |
142 | const FcChar8 *dir, | |
143 | FcBool force, /* XXX unused */ | |
144 | FcConfig *config) | |
24330d27 | 145 | { |
327a7fd4 KP |
146 | DIR *d; |
147 | struct dirent *e; | |
bc5e487f | 148 | FcStrSet *files; |
327a7fd4 KP |
149 | FcChar8 *file; |
150 | FcChar8 *base; | |
d0471dd2 MF |
151 | const FcChar8 *scanDir; |
152 | FcChar8 *fullDir; | |
327a7fd4 | 153 | FcBool ret = FcTrue; |
eb0cf671 | 154 | int i; |
24330d27 | 155 | |
4074fd25 BE |
156 | if (!force) |
157 | return FcFalse; | |
158 | ||
159 | if (!set && !dirs) | |
160 | return FcTrue; | |
161 | ||
162 | if (!blanks) | |
163 | blanks = FcConfigGetBlanks (config); | |
00f059e9 | 164 | |
9dac3c59 | 165 | /* freed below */ |
bc5e487f | 166 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
0d9e31c8 KP |
167 | if (!file) { |
168 | ret = FcFalse; | |
169 | goto bail; | |
170 | } | |
24330d27 | 171 | |
bc5e487f | 172 | strcpy ((char *) file, (char *) dir); |
ccb3e93b KP |
173 | strcat ((char *) file, "/"); |
174 | base = file + strlen ((char *) file); | |
594dcef0 | 175 | |
a8386abc | 176 | if (FcDebug () & FC_DBG_SCAN) |
bc5e487f | 177 | printf ("\tScanning dir %s\n", dir); |
d0471dd2 MF |
178 | |
179 | fullDir = FcConfigGetRootPlus (config, dir); | |
180 | if (fullDir) | |
181 | scanDir = fullDir; | |
182 | else | |
183 | scanDir = dir; | |
184 | d = opendir ((char *) scanDir); | |
185 | if (fullDir) | |
186 | FcStrFree (fullDir); | |
24330d27 KP |
187 | if (!d) |
188 | { | |
179c3995 | 189 | /* Don't complain about missing directories */ |
4074fd25 | 190 | if (errno != ENOENT) |
0d9e31c8 | 191 | ret = FcFalse; |
4074fd25 | 192 | goto bail; |
eb0cf671 PL |
193 | } |
194 | ||
bc5e487f KP |
195 | files = FcStrSetCreate (); |
196 | if (!files) | |
65448e8b PL |
197 | { |
198 | ret = FcFalse; | |
199 | goto bail1; | |
200 | } | |
e3c6d336 | 201 | while ((e = readdir (d))) |
24330d27 | 202 | { |
179c3995 | 203 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 204 | { |
00f059e9 | 205 | strcpy ((char *) base, (char *) e->d_name); |
bc5e487f KP |
206 | if (!FcStrSetAdd (files, file)) { |
207 | ret = FcFalse; | |
208 | goto bail2; | |
65448e8b | 209 | } |
24330d27 KP |
210 | } |
211 | } | |
bc5e487f | 212 | |
00f059e9 | 213 | /* |
bc5e487f | 214 | * Sort files to make things prettier |
00f059e9 | 215 | */ |
bc5e487f | 216 | qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp); |
4074fd25 | 217 | |
af180c40 | 218 | /* |
bc5e487f | 219 | * Scan file files to build font patterns |
af180c40 | 220 | */ |
bc5e487f KP |
221 | for (i = 0; i < files->num; i++) |
222 | FcFileScanConfig (set, dirs, blanks, files->strs[i], config); | |
594dcef0 | 223 | |
4074fd25 BE |
224 | bail2: |
225 | FcStrSetDestroy (files); | |
226 | bail1: | |
227 | closedir (d); | |
228 | bail: | |
229 | return ret; | |
230 | } | |
231 | ||
232 | FcBool | |
233 | FcDirScan (FcFontSet *set, | |
234 | FcStrSet *dirs, | |
235 | FcFileCache *cache, /* XXX unused */ | |
236 | FcBlanks *blanks, | |
237 | const FcChar8 *dir, | |
238 | FcBool force /* XXX unused */) | |
239 | { | |
240 | if (cache || !force) | |
241 | return FcFalse; | |
242 | ||
5e544b32 | 243 | return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ()); |
4074fd25 BE |
244 | } |
245 | ||
246 | /* | |
247 | * Scan the specified directory and construct a cache of its contents | |
248 | */ | |
249 | FcCache * | |
250 | FcDirCacheScan (const FcChar8 *dir, FcConfig *config) | |
251 | { | |
252 | FcStrSet *dirs; | |
4074fd25 BE |
253 | FcFontSet *set; |
254 | FcCache *cache = NULL; | |
255 | struct stat dir_stat; | |
256 | ||
257 | if (FcDebug () & FC_DBG_FONTSET) | |
258 | printf ("cache scan dir %s\n", dir); | |
259 | ||
d0471dd2 | 260 | if (FcStat (config, dir, &dir_stat) < 0) |
4074fd25 | 261 | goto bail; |
4074fd25 BE |
262 | |
263 | set = FcFontSetCreate(); | |
264 | if (!set) | |
4074fd25 | 265 | goto bail; |
4074fd25 BE |
266 | |
267 | dirs = FcStrSetCreate (); | |
268 | if (!dirs) | |
4074fd25 | 269 | goto bail1; |
4074fd25 BE |
270 | |
271 | /* | |
272 | * Scan the dir | |
273 | */ | |
274 | if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config)) | |
4074fd25 | 275 | goto bail2; |
4074fd25 | 276 | |
00f059e9 | 277 | /* |
bc5e487f | 278 | * Build the cache object |
00f059e9 | 279 | */ |
db6f19f1 | 280 | cache = FcDirCacheBuild (set, dir, &dir_stat, dirs); |
bc5e487f | 281 | if (!cache) |
4074fd25 | 282 | goto bail2; |
594dcef0 | 283 | |
00f059e9 | 284 | /* |
bc5e487f | 285 | * Write out the cache file, ignoring any troubles |
00f059e9 | 286 | */ |
bc5e487f | 287 | FcDirCacheWrite (cache, config); |
594dcef0 | 288 | |
65448e8b | 289 | bail2: |
4074fd25 | 290 | FcStrSetDestroy (dirs); |
65448e8b | 291 | bail1: |
bc5e487f | 292 | FcFontSetDestroy (set); |
0d9e31c8 | 293 | bail: |
bc5e487f KP |
294 | return cache; |
295 | } | |
296 | ||
297 | /* | |
298 | * Read (or construct) the cache for a directory | |
299 | */ | |
300 | FcCache * | |
301 | FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config) | |
302 | { | |
303 | FcCache *cache = NULL; | |
bc5e487f | 304 | |
bc5e487f KP |
305 | /* Try to use existing cache file */ |
306 | if (!force) | |
9b511b29 | 307 | cache = FcDirCacheLoad (dir, config, NULL); |
594dcef0 | 308 | |
bc5e487f KP |
309 | /* Not using existing cache file, construct new cache */ |
310 | if (!cache) | |
9b511b29 | 311 | cache = FcDirCacheScan (dir, config); |
594dcef0 | 312 | |
bc5e487f KP |
313 | return cache; |
314 | } | |
315 | ||
24330d27 | 316 | FcBool |
2304e38f | 317 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) |
24330d27 | 318 | { |
bc5e487f | 319 | return FcFalse; /* XXX deprecated */ |
24330d27 | 320 | } |
23816bf9 KP |
321 | #define __fcdir__ |
322 | #include "fcaliastail.h" | |
323 | #undef __fcdir__ |