2 * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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 (stat ((const char *) 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 (config && !FcConfigSubstituteWithPat (config, font, NULL, FcMatchScan))
70 FcPatternDestroy (font);
78 if (font && (!config || FcConfigAcceptFont (config, 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, NULL);
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 * Scan the specified directory and construct a cache of its contents
136 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
147 FcBlanks *blanks = FcConfigGetBlanks (config);
148 FcCache *cache = NULL;
150 if (FcDebug () & FC_DBG_FONTSET)
151 printf ("cache scan dir %s\n", dir);
154 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
160 strcpy ((char *) file, (char *) dir);
161 strcat ((char *) file, "/");
162 base = file + strlen ((char *) file);
164 if (FcDebug () & FC_DBG_SCAN)
165 printf ("\tScanning dir %s\n", dir);
167 d = opendir ((char *) dir);
170 /* Don't complain about missing directories */
178 set = FcFontSetCreate();
185 files = FcStrSetCreate ();
191 while ((e = readdir (d)))
193 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
195 strcpy ((char *) base, (char *) e->d_name);
196 if (!FcStrSetAdd (files, file)) {
204 * Sort files to make things prettier
206 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
208 dirs = FcStrSetCreate ();
213 * Scan file files to build font patterns
215 for (i = 0; i < files->num; i++)
216 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
219 * Build the cache object
221 cache = FcDirCacheBuild (set, dir, dirs);
226 * Write out the cache file, ignoring any troubles
228 FcDirCacheWrite (cache, config);
231 FcStrSetDestroy (dirs);
233 FcStrSetDestroy (files);
235 FcFontSetDestroy (set);
247 * Read (or construct) the cache for a directory
250 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
252 FcCache *cache = NULL;
254 if (config && !FcConfigAcceptFilename (config, dir))
257 /* Try to use existing cache file */
259 cache = FcDirCacheLoad (dir, config, NULL);
261 /* Not using existing cache file, construct new cache */
263 cache = FcDirCacheScan (dir, config);
269 FcDirScanConfig (FcFontSet *set,
276 return FcFalse; /* XXX fixme */
280 FcDirScan (FcFontSet *set,
282 FcFileCache *cache, /* XXX unused */
287 return FcDirScanConfig (set, dirs, blanks, dir, force, NULL);
291 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
293 return FcFalse; /* XXX deprecated */