]> git.wh0rd.org - fontconfig.git/blob - src/fcdir.c
fc-cache: add a --root option
[fontconfig.git] / src / fcdir.c
1 /*
2 * fontconfig/src/fcdir.c
3 *
4 * Copyright © 2000 Keith Packard
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
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
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
25 #include "fcint.h"
26 #include <dirent.h>
27
28 FcBool
29 FcFileIsDir (const FcChar8 *file)
30 {
31 return FcFileIsDir2 (FcConfigGetCurrent (), file);
32 }
33
34 FcBool
35 FcFileIsDir2 (FcConfig *config, const FcChar8 *file)
36 {
37 struct stat statb;
38
39 if (FcStat (config, file, &statb) != 0)
40 return FcFalse;
41 return S_ISDIR(statb.st_mode);
42 }
43
44 static FcBool
45 FcFileScanFontConfig (FcFontSet *set,
46 FcBlanks *blanks,
47 const FcChar8 *file,
48 FcConfig *config)
49 {
50 FcPattern *font;
51 FcBool ret = FcTrue;
52 int id;
53 int count = 0;
54
55 id = 0;
56 do
57 {
58 font = 0;
59 /*
60 * Nothing in the cache, scan the file
61 */
62 if (FcDebug () & FC_DBG_SCAN)
63 {
64 printf ("\tScanning file %s...", file);
65 fflush (stdout);
66 }
67 font = FcFreeTypeQuery2 (config, file, id, blanks, &count);
68 if (FcDebug () & FC_DBG_SCAN)
69 printf ("done\n");
70
71 /*
72 * Edit pattern with user-defined rules
73 */
74 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
75 {
76 FcPatternDestroy (font);
77 font = NULL;
78 ret = FcFalse;
79 }
80
81 /*
82 * Add the font
83 */
84 if (font)
85 {
86 if (FcDebug() & FC_DBG_SCANV)
87 {
88 printf ("Final font pattern:\n");
89 FcPatternPrint (font);
90 }
91 if (!FcFontSetAdd (set, font))
92 {
93 FcPatternDestroy (font);
94 font = NULL;
95 ret = FcFalse;
96 }
97 }
98 else if (font)
99 FcPatternDestroy (font);
100 id++;
101 } while (font && ret && id < count);
102 return ret;
103 }
104
105 FcBool
106 FcFileScanConfig (FcFontSet *set,
107 FcStrSet *dirs,
108 FcBlanks *blanks,
109 const FcChar8 *file,
110 FcConfig *config)
111 {
112 if (FcFileIsDir2 (config, file))
113 return FcStrSetAdd (dirs, file);
114 else
115 return FcFileScanFontConfig (set, blanks, file, config);
116 }
117
118 FcBool
119 FcFileScan (FcFontSet *set,
120 FcStrSet *dirs,
121 FcFileCache *cache, /* XXX unused */
122 FcBlanks *blanks,
123 const FcChar8 *file,
124 FcBool force)
125 {
126 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
127 }
128
129 /*
130 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
131 */
132 static int
133 cmpstringp(const void *p1, const void *p2)
134 {
135 return strcmp(* (char **) p1, * (char **) p2);
136 }
137
138 FcBool
139 FcDirScanConfig (FcFontSet *set,
140 FcStrSet *dirs,
141 FcBlanks *blanks,
142 const FcChar8 *dir,
143 FcBool force, /* XXX unused */
144 FcConfig *config)
145 {
146 DIR *d;
147 struct dirent *e;
148 FcStrSet *files;
149 FcChar8 *file;
150 FcChar8 *base;
151 const FcChar8 *scanDir;
152 FcChar8 *fullDir;
153 FcBool ret = FcTrue;
154 int i;
155
156 if (!force)
157 return FcFalse;
158
159 if (!set && !dirs)
160 return FcTrue;
161
162 if (!blanks)
163 blanks = FcConfigGetBlanks (config);
164
165 /* freed below */
166 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
167 if (!file) {
168 ret = FcFalse;
169 goto bail;
170 }
171
172 strcpy ((char *) file, (char *) dir);
173 strcat ((char *) file, "/");
174 base = file + strlen ((char *) file);
175
176 if (FcDebug () & FC_DBG_SCAN)
177 printf ("\tScanning dir %s\n", dir);
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);
187 if (!d)
188 {
189 /* Don't complain about missing directories */
190 if (errno != ENOENT)
191 ret = FcFalse;
192 goto bail;
193 }
194
195 files = FcStrSetCreate ();
196 if (!files)
197 {
198 ret = FcFalse;
199 goto bail1;
200 }
201 while ((e = readdir (d)))
202 {
203 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
204 {
205 strcpy ((char *) base, (char *) e->d_name);
206 if (!FcStrSetAdd (files, file)) {
207 ret = FcFalse;
208 goto bail2;
209 }
210 }
211 }
212
213 /*
214 * Sort files to make things prettier
215 */
216 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
217
218 /*
219 * Scan file files to build font patterns
220 */
221 for (i = 0; i < files->num; i++)
222 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
223
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
243 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ());
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;
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
260 if (FcStat (config, dir, &dir_stat) < 0)
261 goto bail;
262
263 set = FcFontSetCreate();
264 if (!set)
265 goto bail;
266
267 dirs = FcStrSetCreate ();
268 if (!dirs)
269 goto bail1;
270
271 /*
272 * Scan the dir
273 */
274 if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config))
275 goto bail2;
276
277 /*
278 * Build the cache object
279 */
280 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
281 if (!cache)
282 goto bail2;
283
284 /*
285 * Write out the cache file, ignoring any troubles
286 */
287 FcDirCacheWrite (cache, config);
288
289 bail2:
290 FcStrSetDestroy (dirs);
291 bail1:
292 FcFontSetDestroy (set);
293 bail:
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;
304
305 /* Try to use existing cache file */
306 if (!force)
307 cache = FcDirCacheLoad (dir, config, NULL);
308
309 /* Not using existing cache file, construct new cache */
310 if (!cache)
311 cache = FcDirCacheScan (dir, config);
312
313 return cache;
314 }
315
316 FcBool
317 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
318 {
319 return FcFalse; /* XXX deprecated */
320 }
321 #define __fcdir__
322 #include "fcaliastail.h"
323 #undef __fcdir__