2 * fontconfig/src/fcdir.c
4 * Copyright © 2000 Keith Packard
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.
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.
29 FcFileIsDir (const FcChar8 *file)
33 if (FcStat (file, &statb) != 0)
35 return S_ISDIR(statb.st_mode);
39 FcFileScanFontConfig (FcFontSet *set,
54 * Nothing in the cache, scan the file
56 if (FcDebug () & FC_DBG_SCAN)
58 printf ("\tScanning file %s...", file);
61 font = FcFreeTypeQuery (file, id, blanks, &count);
62 if (FcDebug () & FC_DBG_SCAN)
66 * Edit pattern with user-defined rules
68 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
70 FcPatternDestroy (font);
80 if (FcDebug() & FC_DBG_SCANV)
82 printf ("Final font pattern:\n");
83 FcPatternPrint (font);
85 if (!FcFontSetAdd (set, font))
87 FcPatternDestroy (font);
93 FcPatternDestroy (font);
95 } while (font && ret && id < count);
100 FcFileScanConfig (FcFontSet *set,
106 if (FcFileIsDir (file))
107 return FcStrSetAdd (dirs, file);
109 return FcFileScanFontConfig (set, blanks, file, config);
113 FcFileScan (FcFontSet *set,
115 FcFileCache *cache, /* XXX unused */
120 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
124 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
127 cmpstringp(const void *p1, const void *p2)
129 return strcmp(* (char **) p1, * (char **) p2);
133 FcDirScanConfig (FcFontSet *set,
137 FcBool force, /* XXX unused */
155 blanks = FcConfigGetBlanks (config);
158 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
164 strcpy ((char *) file, (char *) dir);
165 strcat ((char *) file, "/");
166 base = file + strlen ((char *) file);
168 if (FcDebug () & FC_DBG_SCAN)
169 printf ("\tScanning dir %s\n", dir);
171 d = opendir ((char *) dir);
174 /* Don't complain about missing directories */
180 files = FcStrSetCreate ();
186 while ((e = readdir (d)))
188 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
190 strcpy ((char *) base, (char *) e->d_name);
191 if (!FcStrSetAdd (files, file)) {
199 * Sort files to make things prettier
201 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
204 * Scan file files to build font patterns
206 for (i = 0; i < files->num; i++)
207 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
210 FcStrSetDestroy (files);
221 FcDirScan (FcFontSet *set,
223 FcFileCache *cache, /* XXX unused */
226 FcBool force /* XXX unused */)
231 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ());
235 * Scan the specified directory and construct a cache of its contents
238 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
242 FcCache *cache = NULL;
243 struct stat dir_stat;
245 if (FcDebug () & FC_DBG_FONTSET)
246 printf ("cache scan dir %s\n", dir);
248 if (FcStatChecksum (dir, &dir_stat) < 0)
251 set = FcFontSetCreate();
255 dirs = FcStrSetCreate ();
262 if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config))
266 * Build the cache object
268 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
273 * Write out the cache file, ignoring any troubles
275 FcDirCacheWrite (cache, config);
278 FcStrSetDestroy (dirs);
280 FcFontSetDestroy (set);
286 * Read (or construct) the cache for a directory
289 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
291 FcCache *cache = NULL;
293 /* Try to use existing cache file */
295 cache = FcDirCacheLoad (dir, config, NULL);
297 /* Not using existing cache file, construct new cache */
299 cache = FcDirCacheScan (dir, config);
305 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
307 return FcFalse; /* XXX deprecated */
310 #include "fcaliastail.h"