]>
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 KP |
29 | FcFileIsDir (const FcChar8 *file) |
30 | { | |
31 | struct stat statb; | |
24330d27 | 32 | |
c21fb9ac | 33 | if (FcStat (file, &statb) != 0) |
179c3995 KP |
34 | return FcFalse; |
35 | return S_ISDIR(statb.st_mode); | |
36 | } | |
24330d27 | 37 | |
00f059e9 KP |
38 | static FcBool |
39 | FcFileScanFontConfig (FcFontSet *set, | |
40 | FcBlanks *blanks, | |
41 | const FcChar8 *file, | |
42 | FcConfig *config) | |
24330d27 | 43 | { |
00f059e9 KP |
44 | FcPattern *font; |
45 | FcBool ret = FcTrue; | |
46 | int id; | |
47 | int count = 0; | |
594dcef0 | 48 | |
24330d27 KP |
49 | id = 0; |
50 | do | |
51 | { | |
327a7fd4 KP |
52 | font = 0; |
53 | /* | |
eb0cf671 | 54 | * Nothing in the cache, scan the file |
327a7fd4 | 55 | */ |
eb0cf671 | 56 | if (FcDebug () & FC_DBG_SCAN) |
24330d27 | 57 | { |
eb0cf671 PL |
58 | printf ("\tScanning file %s...", file); |
59 | fflush (stdout); | |
24330d27 | 60 | } |
eb0cf671 PL |
61 | font = FcFreeTypeQuery (file, id, blanks, &count); |
62 | if (FcDebug () & FC_DBG_SCAN) | |
63 | printf ("done\n"); | |
c2c6976d KP |
64 | |
65 | /* | |
66 | * Edit pattern with user-defined rules | |
67 | */ | |
0fcf866d | 68 | if (font && config && !FcConfigSubstitute (config, font, FcMatchScan)) |
c2c6976d KP |
69 | { |
70 | FcPatternDestroy (font); | |
71 | font = NULL; | |
72 | ret = FcFalse; | |
73 | } | |
74 | ||
327a7fd4 KP |
75 | /* |
76 | * Add the font | |
77 | */ | |
0fcf866d | 78 | if (font) |
24330d27 | 79 | { |
c2c6976d KP |
80 | if (FcDebug() & FC_DBG_SCANV) |
81 | { | |
82 | printf ("Final font pattern:\n"); | |
83 | FcPatternPrint (font); | |
84 | } | |
24330d27 KP |
85 | if (!FcFontSetAdd (set, font)) |
86 | { | |
87 | FcPatternDestroy (font); | |
c2c6976d | 88 | font = NULL; |
24330d27 KP |
89 | ret = FcFalse; |
90 | } | |
91 | } | |
47b49bf1 KP |
92 | else if (font) |
93 | FcPatternDestroy (font); | |
24330d27 KP |
94 | id++; |
95 | } while (font && ret && id < count); | |
96 | return ret; | |
97 | } | |
98 | ||
00f059e9 KP |
99 | FcBool |
100 | FcFileScanConfig (FcFontSet *set, | |
101 | FcStrSet *dirs, | |
102 | FcBlanks *blanks, | |
103 | const FcChar8 *file, | |
00f059e9 KP |
104 | FcConfig *config) |
105 | { | |
00f059e9 KP |
106 | if (FcFileIsDir (file)) |
107 | return FcStrSetAdd (dirs, file); | |
108 | else | |
109 | return FcFileScanFontConfig (set, blanks, file, config); | |
110 | } | |
111 | ||
d47c9d6e KP |
112 | FcBool |
113 | FcFileScan (FcFontSet *set, | |
114 | FcStrSet *dirs, | |
00f059e9 | 115 | FcFileCache *cache, /* XXX unused */ |
d47c9d6e KP |
116 | FcBlanks *blanks, |
117 | const FcChar8 *file, | |
118 | FcBool force) | |
119 | { | |
5e544b32 | 120 | return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ()); |
d47c9d6e KP |
121 | } |
122 | ||
e3c6d336 PL |
123 | /* |
124 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
125 | */ | |
e3c6d336 PL |
126 | static int |
127 | cmpstringp(const void *p1, const void *p2) | |
128 | { | |
129 | return strcmp(* (char **) p1, * (char **) p2); | |
130 | } | |
131 | ||
4074fd25 BE |
132 | FcBool |
133 | FcDirScanConfig (FcFontSet *set, | |
134 | FcStrSet *dirs, | |
135 | FcBlanks *blanks, | |
136 | const FcChar8 *dir, | |
137 | FcBool force, /* XXX unused */ | |
138 | FcConfig *config) | |
24330d27 | 139 | { |
327a7fd4 KP |
140 | DIR *d; |
141 | struct dirent *e; | |
bc5e487f | 142 | FcStrSet *files; |
327a7fd4 KP |
143 | FcChar8 *file; |
144 | FcChar8 *base; | |
145 | FcBool ret = FcTrue; | |
eb0cf671 | 146 | int i; |
24330d27 | 147 | |
4074fd25 BE |
148 | if (!force) |
149 | return FcFalse; | |
150 | ||
151 | if (!set && !dirs) | |
152 | return FcTrue; | |
153 | ||
154 | if (!blanks) | |
155 | blanks = FcConfigGetBlanks (config); | |
00f059e9 | 156 | |
9dac3c59 | 157 | /* freed below */ |
bc5e487f | 158 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
0d9e31c8 KP |
159 | if (!file) { |
160 | ret = FcFalse; | |
161 | goto bail; | |
162 | } | |
24330d27 | 163 | |
bc5e487f | 164 | strcpy ((char *) file, (char *) dir); |
ccb3e93b KP |
165 | strcat ((char *) file, "/"); |
166 | base = file + strlen ((char *) file); | |
594dcef0 | 167 | |
a8386abc | 168 | if (FcDebug () & FC_DBG_SCAN) |
bc5e487f | 169 | printf ("\tScanning dir %s\n", dir); |
a8386abc | 170 | |
bc5e487f | 171 | d = opendir ((char *) dir); |
24330d27 KP |
172 | if (!d) |
173 | { | |
179c3995 | 174 | /* Don't complain about missing directories */ |
4074fd25 | 175 | if (errno != ENOENT) |
0d9e31c8 | 176 | ret = FcFalse; |
4074fd25 | 177 | goto bail; |
eb0cf671 PL |
178 | } |
179 | ||
bc5e487f KP |
180 | files = FcStrSetCreate (); |
181 | if (!files) | |
65448e8b PL |
182 | { |
183 | ret = FcFalse; | |
184 | goto bail1; | |
185 | } | |
e3c6d336 | 186 | while ((e = readdir (d))) |
24330d27 | 187 | { |
179c3995 | 188 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 189 | { |
00f059e9 | 190 | strcpy ((char *) base, (char *) e->d_name); |
bc5e487f KP |
191 | if (!FcStrSetAdd (files, file)) { |
192 | ret = FcFalse; | |
193 | goto bail2; | |
65448e8b | 194 | } |
24330d27 KP |
195 | } |
196 | } | |
bc5e487f | 197 | |
00f059e9 | 198 | /* |
bc5e487f | 199 | * Sort files to make things prettier |
00f059e9 | 200 | */ |
bc5e487f | 201 | qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp); |
4074fd25 | 202 | |
af180c40 | 203 | /* |
bc5e487f | 204 | * Scan file files to build font patterns |
af180c40 | 205 | */ |
bc5e487f KP |
206 | for (i = 0; i < files->num; i++) |
207 | FcFileScanConfig (set, dirs, blanks, files->strs[i], config); | |
594dcef0 | 208 | |
4074fd25 BE |
209 | bail2: |
210 | FcStrSetDestroy (files); | |
211 | bail1: | |
212 | closedir (d); | |
213 | bail: | |
214 | return ret; | |
215 | } | |
216 | ||
217 | FcBool | |
218 | FcDirScan (FcFontSet *set, | |
219 | FcStrSet *dirs, | |
220 | FcFileCache *cache, /* XXX unused */ | |
221 | FcBlanks *blanks, | |
222 | const FcChar8 *dir, | |
223 | FcBool force /* XXX unused */) | |
224 | { | |
225 | if (cache || !force) | |
226 | return FcFalse; | |
227 | ||
5e544b32 | 228 | return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ()); |
4074fd25 BE |
229 | } |
230 | ||
231 | /* | |
232 | * Scan the specified directory and construct a cache of its contents | |
233 | */ | |
234 | FcCache * | |
235 | FcDirCacheScan (const FcChar8 *dir, FcConfig *config) | |
236 | { | |
237 | FcStrSet *dirs; | |
4074fd25 BE |
238 | FcFontSet *set; |
239 | FcCache *cache = NULL; | |
240 | struct stat dir_stat; | |
241 | ||
242 | if (FcDebug () & FC_DBG_FONTSET) | |
243 | printf ("cache scan dir %s\n", dir); | |
244 | ||
c21fb9ac | 245 | if (FcStat (dir, &dir_stat) < 0) |
4074fd25 | 246 | goto bail; |
4074fd25 BE |
247 | |
248 | set = FcFontSetCreate(); | |
249 | if (!set) | |
4074fd25 | 250 | goto bail; |
4074fd25 BE |
251 | |
252 | dirs = FcStrSetCreate (); | |
253 | if (!dirs) | |
4074fd25 | 254 | goto bail1; |
4074fd25 BE |
255 | |
256 | /* | |
257 | * Scan the dir | |
258 | */ | |
259 | if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config)) | |
4074fd25 | 260 | goto bail2; |
4074fd25 | 261 | |
00f059e9 | 262 | /* |
bc5e487f | 263 | * Build the cache object |
00f059e9 | 264 | */ |
db6f19f1 | 265 | cache = FcDirCacheBuild (set, dir, &dir_stat, dirs); |
bc5e487f | 266 | if (!cache) |
4074fd25 | 267 | goto bail2; |
594dcef0 | 268 | |
00f059e9 | 269 | /* |
bc5e487f | 270 | * Write out the cache file, ignoring any troubles |
00f059e9 | 271 | */ |
bc5e487f | 272 | FcDirCacheWrite (cache, config); |
594dcef0 | 273 | |
65448e8b | 274 | bail2: |
4074fd25 | 275 | FcStrSetDestroy (dirs); |
65448e8b | 276 | bail1: |
bc5e487f | 277 | FcFontSetDestroy (set); |
0d9e31c8 | 278 | bail: |
bc5e487f KP |
279 | return cache; |
280 | } | |
281 | ||
282 | /* | |
283 | * Read (or construct) the cache for a directory | |
284 | */ | |
285 | FcCache * | |
286 | FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config) | |
287 | { | |
288 | FcCache *cache = NULL; | |
bc5e487f | 289 | |
bc5e487f KP |
290 | /* Try to use existing cache file */ |
291 | if (!force) | |
9b511b29 | 292 | cache = FcDirCacheLoad (dir, config, NULL); |
594dcef0 | 293 | |
bc5e487f KP |
294 | /* Not using existing cache file, construct new cache */ |
295 | if (!cache) | |
9b511b29 | 296 | cache = FcDirCacheScan (dir, config); |
594dcef0 | 297 | |
bc5e487f KP |
298 | return cache; |
299 | } | |
300 | ||
24330d27 | 301 | FcBool |
2304e38f | 302 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) |
24330d27 | 303 | { |
bc5e487f | 304 | return FcFalse; /* XXX deprecated */ |
24330d27 | 305 | } |
23816bf9 KP |
306 | #define __fcdir__ |
307 | #include "fcaliastail.h" | |
308 | #undef __fcdir__ |