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