X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffcdir.c;h=c15b7277b10216a9959847f60215150e91546620;hb=af180c40376690b7ced5262156fbe13c9ebba1e2;hp=4f69f228bc0688fb98fb65df81b13d3a597f3fb1;hpb=4262e0b3853bc2153270eb33d09a063f852f3f90;p=fontconfig.git diff --git a/src/fcdir.c b/src/fcdir.c index 4f69f22..c15b727 100644 --- a/src/fcdir.c +++ b/src/fcdir.c @@ -35,115 +35,32 @@ FcFileIsDir (const FcChar8 *file) return S_ISDIR(statb.st_mode); } -FcBool -FcFileScanConfig (FcFontSet *set, - FcStrSet *dirs, - FcGlobalCache *cache, - FcBlanks *blanks, - const FcChar8 *file, - FcBool force, - FcConfig *config) +static FcBool +FcFileScanFontConfig (FcFontSet *set, + FcBlanks *blanks, + const FcChar8 *file, + FcConfig *config) { - int id; -#if 0 - FcChar8 *name; - FcGlobalCacheFile *cache_file; - FcGlobalCacheDir *cache_dir; -#endif - FcPattern *font; - FcBool ret = FcTrue; - FcBool isDir; - int count = 0; - FcBool need_scan; + FcPattern *font; + FcBool ret = FcTrue; + int id; + int count = 0; - if (config && !FcConfigAcceptFilename (config, file)) - return FcTrue; - - if (force) - cache = 0; id = 0; do { - need_scan = FcTrue; font = 0; -#if 0 - /* - * Check the cache - */ - if (cache) - { - if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count))) - { - /* - * Found a cache entry for the file - */ - if (FcGlobalCacheCheckTime (file, &cache_file->info)) - { - name = cache_file->name; - need_scan = FcFalse; - FcGlobalCacheReferenced (cache, &cache_file->info); - /* "." means the file doesn't contain a font */ - if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0) - { - font = FcNameParse (name); - if (font) - if (!FcPatternAddString (font, FC_FILE, file)) - ret = FcFalse; - } - } - } - else if ((cache_dir = FcGlobalCacheDirGet (cache, file, - strlen ((const char *) file), - FcFalse))) - { - if (FcGlobalCacheCheckTime (cache_dir->info.file, - &cache_dir->info)) - { - font = 0; - need_scan = FcFalse; - FcGlobalCacheReferenced (cache, &cache_dir->info); - if (!FcStrSetAdd (dirs, file)) - ret = FcFalse; - } - } - } -#endif /* * Nothing in the cache, scan the file */ - if (need_scan) + if (FcDebug () & FC_DBG_SCAN) { - if (FcDebug () & FC_DBG_SCAN) - { - printf ("\tScanning file %s...", file); - fflush (stdout); - } - font = FcFreeTypeQuery (file, id, blanks, &count); - if (FcDebug () & FC_DBG_SCAN) - printf ("done\n"); - isDir = FcFalse; - if (!font && FcFileIsDir (file)) - { - isDir = FcTrue; - ret = FcStrSetAdd (dirs, file); - } -#if 0 - /* - * Update the cache - */ - if (cache && font) - { - FcChar8 *unparse; - - unparse = FcNameUnparse (font); - if (unparse) - { - (void) FcGlobalCacheUpdate (cache, file, id, unparse); - FcStrFree (unparse); - } - } -#endif + printf ("\tScanning file %s...", file); + fflush (stdout); } + font = FcFreeTypeQuery (file, id, blanks, &count); + if (FcDebug () & FC_DBG_SCAN) + printf ("done\n"); /* * Add the font */ @@ -163,15 +80,42 @@ FcFileScanConfig (FcFontSet *set, return ret; } +FcBool +FcFileScanConfig (FcFontSet *set, + FcStrSet *dirs, + FcBlanks *blanks, + const FcChar8 *file, + FcBool force, + FcConfig *config) +{ + if (config && !FcConfigAcceptFilename (config, file)) + return FcTrue; + + if (FcFileIsDir (file)) + return FcStrSetAdd (dirs, file); + else + return FcFileScanFontConfig (set, blanks, file, config); +} + FcBool FcFileScan (FcFontSet *set, FcStrSet *dirs, - FcGlobalCache *cache, + FcFileCache *cache, /* XXX unused */ FcBlanks *blanks, const FcChar8 *file, FcBool force) { - return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); + return FcFileScanConfig (set, dirs, blanks, file, force, 0); +} + +/* + * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage + */ + +static int +cmpstringp(const void *p1, const void *p2) +{ + return strcmp(* (char **) p1, * (char **) p2); } /* @@ -182,7 +126,6 @@ FcFileScan (FcFontSet *set, FcBool FcDirScanConfig (FcFontSet *set, FcStrSet *dirs, - FcGlobalCache *cache, FcBlanks *blanks, const FcChar8 *dir, FcBool force, @@ -190,34 +133,25 @@ FcDirScanConfig (FcFontSet *set, { DIR *d; struct dirent *e; + FcStrSet *dirlist, *filelist; FcChar8 *file; FcChar8 *base; FcBool ret = FcTrue; + FcFontSet *tmpSet; + int i; if (config && !FcConfigAcceptFilename (config, dir)) return FcTrue; if (!force) { -#if 0 - /* - * Check fonts.cache- file - */ - if (FcDirCacheReadDir (set, dirs, dir, config)) - { - if (cache) - FcGlobalCacheReferenceSubdir (cache, dir); - return FcTrue; - } - - /* - * Check ~/.fonts.cache- file - */ - if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config)) + if (FcDirCacheRead (set, dirs, dir, config)) return FcTrue; -#endif } + if (FcDebug () & FC_DBG_FONTSET) + printf ("cache scan dir %s\n", dir); + /* freed below */ file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); if (!file) @@ -231,7 +165,6 @@ FcDirScanConfig (FcFontSet *set, printf ("\tScanning dir %s\n", dir); d = opendir ((char *) dir); - if (!d) { free (file); @@ -240,51 +173,106 @@ FcDirScanConfig (FcFontSet *set, return FcTrue; return FcFalse; } - while (ret && (e = readdir (d))) + + tmpSet = FcFontSetCreate(); + if (!tmpSet) + { + ret = FcFalse; + goto bail0; + } + + dirlist = FcStrSetCreate (); + if (!dirlist) + { + ret = FcFalse; + goto bail1; + } + filelist = FcStrSetCreate (); + if (!filelist) + { + ret = FcFalse; + goto bail2; + } + while ((e = readdir (d))) { if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) { strcpy ((char *) base, (char *) e->d_name); - ret = FcFileScanConfig (set, dirs, cache, blanks, file, force, config); + if (FcFileIsDir (file)) { + if (!FcStrSetAdd (dirlist, file)) { + ret = FcFalse; + goto bail3; + } + } else { + if (!FcStrSetAdd (filelist, file)) { + ret = FcFalse; + goto bail3; + } + } } } - free (file); - closedir (d); + /* + * Sort files and dirs to make things prettier + */ + qsort(dirlist->strs, dirlist->num, sizeof(FcChar8 *), cmpstringp); + qsort(filelist->strs, filelist->num, sizeof(FcChar8 *), cmpstringp); + + for (i = 0; i < filelist->num; i++) + FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config); + /* * Now that the directory has been scanned, - * add the cache entry + * write out the cache file */ -#if 0 - if (ret && cache) - FcGlobalCacheUpdate (cache, dir, 0, 0); -#endif - + FcDirCacheWrite (tmpSet, dirlist, dir, config); + + /* + * Add the discovered fonts to our internal non-cache list + */ + for (i = 0; i < tmpSet->nfont; i++) + FcFontSetAdd (set, tmpSet->fonts[i]); + + /* + * the patterns in tmpset now belong to set; don't free them + */ + tmpSet->nfont = 0; + + /* + * Add the discovered directories to the list to be scanned + */ + for (i = 0; i < dirlist->num; i++) + if (!FcStrSetAdd (dirs, dirlist->strs[i])) { + ret = FcFalse; + goto bail3; + } + + bail3: + FcStrSetDestroy (filelist); + bail2: + FcStrSetDestroy (dirlist); + bail1: + FcFontSetDestroy (tmpSet); + + bail0: + closedir (d); + + free (file); return ret; } FcBool FcDirScan (FcFontSet *set, FcStrSet *dirs, - FcGlobalCache *cache, + FcFileCache *cache, /* XXX unused */ FcBlanks *blanks, const FcChar8 *dir, FcBool force) { - return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); + return FcDirScanConfig (set, dirs, blanks, dir, force, 0); } FcBool -FcDirSave (FcFontSet *set, const FcChar8 *dir) +FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) { - static int rand_state = 0; - int bank; - - if (!rand_state) - rand_state = time(0L); - bank = rand_r(&rand_state); - - while (FcCacheHaveBank(bank)) - bank = rand_r(&rand_state); - - return FcDirCacheWrite (bank, set, dir); + return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ()); }