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.
28 #include FT_FREETYPE_H
32 FcFileIsDir (const FcChar8 *file)
36 if (FcStat (file, &statb) != 0)
38 return S_ISDIR(statb.st_mode);
42 FcFileIsLink (const FcChar8 *file)
47 if (lstat ((const char *)file, &statb) != 0)
49 return S_ISLNK (statb.st_mode);
56 FcFileIsFile (const FcChar8 *file)
60 if (FcStat (file, &statb) != 0)
62 return S_ISREG (statb.st_mode);
66 FcFileScanFontConfig (FcFontSet *set,
76 int num_instances = 0;
80 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
82 if (FT_Init_FreeType (&ftLibrary))
89 * Nothing in the cache, scan the file
91 if (FcDebug () & FC_DBG_SCAN)
93 printf ("\tScanning file %s...", file);
97 id = ((instance_num << 16) + face_num);
98 if (FT_New_Face (ftLibrary, (char *) file, id, &face))
100 num_faces = face->num_faces;
101 num_instances = face->style_flags >> 16;
102 font = FcFreeTypeQueryFace (face, file, id, blanks);
105 if (FcDebug () & FC_DBG_SCAN)
108 * Get rid of sysroot here so that targeting scan rule may contains FC_FILE pattern
109 * and they should usually expect without sysroot.
113 size_t len = strlen ((const char *)sysroot);
116 if (FcPatternObjectGetString (font, FC_FILE_OBJECT, 0, &f) == FcResultMatch &&
117 strncmp ((const char *)f, (const char *)sysroot, len) == 0)
119 FcChar8 *s = FcStrdup (f);
120 FcPatternObjectDel (font, FC_FILE_OBJECT);
123 else if (s[len+1] == '/')
125 FcPatternObjectAddString (font, FC_FILE_OBJECT, &s[len]);
131 * Edit pattern with user-defined rules
133 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
135 FcPatternDestroy (font);
145 if (FcDebug() & FC_DBG_SCANV)
147 printf ("Final font pattern:\n");
148 FcPatternPrint (font);
150 if (!FcFontSetAdd (set, font))
152 FcPatternDestroy (font);
160 if (instance_num < num_instances)
167 } while (font && ret && face_num < num_faces);
169 FT_Done_FreeType (ftLibrary);
175 FcFileScanConfig (FcFontSet *set,
181 if (FcFileIsDir (file))
183 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
184 const FcChar8 *d = file;
189 len = strlen ((const char *)sysroot);
190 if (strncmp ((const char *)file, (const char *)sysroot, len) == 0)
192 if (file[len] != '/')
194 else if (file[len+1] == '/')
199 return FcStrSetAdd (dirs, d);
204 return FcFileScanFontConfig (set, blanks, file, config);
211 FcFileScan (FcFontSet *set,
213 FcFileCache *cache FC_UNUSED,
216 FcBool force FC_UNUSED)
218 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
222 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
225 cmpstringp(const void *p1, const void *p2)
227 return strcmp(* (char **) p1, * (char **) p2);
231 FcDirScanConfig (FcFontSet *set,
235 FcBool force, /* XXX unused */
253 blanks = FcConfigGetBlanks (config);
256 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
262 strcpy ((char *) file, (char *) dir);
263 strcat ((char *) file, "/");
264 base = file + strlen ((char *) file);
266 if (FcDebug () & FC_DBG_SCAN)
267 printf ("\tScanning dir %s\n", dir);
269 d = opendir ((char *) dir);
272 /* Don't complain about missing directories */
278 files = FcStrSetCreateEx (FCSS_ALLOW_DUPLICATES | FCSS_GROW_BY_64);
284 while ((e = readdir (d)))
286 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
288 strcpy ((char *) base, (char *) e->d_name);
289 if (!FcStrSetAdd (files, file)) {
297 * Sort files to make things prettier
299 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
302 * Scan file files to build font patterns
304 for (i = 0; i < files->num; i++)
305 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
308 FcStrSetDestroy (files);
319 FcDirScan (FcFontSet *set,
321 FcFileCache *cache, /* XXX unused */
324 FcBool force /* XXX unused */)
329 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ());
333 * Scan the specified directory and construct a cache of its contents
336 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
340 FcCache *cache = NULL;
341 struct stat dir_stat;
342 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
347 d = FcStrBuildFilename (sysroot, dir, NULL);
351 if (FcDebug () & FC_DBG_FONTSET)
352 printf ("cache scan dir %s\n", d);
354 if (FcStatChecksum (d, &dir_stat) < 0)
357 set = FcFontSetCreate();
361 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
365 fd = FcDirCacheLock (dir, config);
369 if (!FcDirScanConfig (set, dirs, NULL, d, FcTrue, config))
373 * Build the cache object
375 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
380 * Write out the cache file, ignoring any troubles
382 FcDirCacheWrite (cache, config);
385 FcDirCacheUnlock (fd);
386 FcStrSetDestroy (dirs);
388 FcFontSetDestroy (set);
396 FcDirCacheRescan (const FcChar8 *dir, FcConfig *config)
400 struct stat dir_stat;
402 const FcChar8 *sysroot = FcConfigGetSysRoot (config);
406 cache = FcDirCacheLoad (dir, config, NULL);
411 d = FcStrBuildFilename (sysroot, dir, NULL);
414 if (FcStatChecksum (d, &dir_stat) < 0)
416 dirs = FcStrSetCreateEx (FCSS_GROW_BY_64);
420 fd = FcDirCacheLock (dir, config);
424 if (!FcDirScanConfig (NULL, dirs, NULL, d, FcTrue, config))
427 * Rebuild the cache object
429 new = FcDirCacheRebuild (cache, &dir_stat, dirs);
432 FcDirCacheUnload (cache);
434 * Write out the cache file, ignoring any troubles
436 FcDirCacheWrite (new, config);
439 FcDirCacheUnlock (fd);
440 FcStrSetDestroy (dirs);
449 * Read (or construct) the cache for a directory
452 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
454 FcCache *cache = NULL;
456 /* Try to use existing cache file */
458 cache = FcDirCacheLoad (dir, config, NULL);
460 /* Not using existing cache file, construct new cache */
462 cache = FcDirCacheScan (dir, config);
468 FcDirSave (FcFontSet *set FC_UNUSED, FcStrSet * dirs FC_UNUSED, const FcChar8 *dir FC_UNUSED)
470 return FcFalse; /* XXX deprecated */
473 #include "fcaliastail.h"