X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=src%2Ffcdir.c;h=86d391b5b4ab38156e9cc6d21c4a15a696379dc7;hb=0d9e31c810a36cddadff7572fdbb5a1b505e495e;hp=c8284368243dfb0b3dd279c4a039c0ecd8f84bb5;hpb=5faa099cd5fb8d9d9f868362233929ff36cd0ac8;p=fontconfig.git diff --git a/src/fcdir.c b/src/fcdir.c index c828436..86d391b 100644 --- a/src/fcdir.c +++ b/src/fcdir.c @@ -1,7 +1,7 @@ /* - * $XFree86: xc/lib/fontconfig/src/fcdir.c,v 1.2 2002/02/15 06:01:28 keithp Exp $ + * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $ * - * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc. + * Copyright © 2000 Keith Packard * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -22,77 +22,49 @@ * PERFORMANCE OF THIS SOFTWARE. */ -#include -#include -#include -#include #include "fcint.h" - -#define FC_INVALID_FONT_FILE "." +#include FcBool -FcFileScan (FcFontSet *set, - FcFileCache *cache, - FcBlanks *blanks, - const FcChar8 *file, - FcBool force) +FcFileIsDir (const FcChar8 *file) { - int id; - FcChar8 *name; - FcPattern *font; - FcBool ret = FcTrue; - int count; + struct stat statb; + + if (stat ((const char *) file, &statb) != 0) + return FcFalse; + return S_ISDIR(statb.st_mode); +} + +static FcBool +FcFileScanFontConfig (FcFontSet *set, + FcBlanks *blanks, + const FcChar8 *file, + FcConfig *config) +{ + FcPattern *font; + FcBool ret = FcTrue; + int id; + int count = 0; id = 0; do { - if (!force && cache) - name = FcFileCacheFind (cache, file, id, &count); - else - name = 0; - if (name) + font = 0; + /* + * Nothing in the cache, scan the file + */ + if (FcDebug () & FC_DBG_SCAN) { - /* "." means the file doesn't contain a font */ - if (strcmp ((const char *) name, FC_INVALID_FONT_FILE) != 0) - { - font = FcNameParse (name); - if (font) - FcPatternAddString (font, FC_FILE, file); - } - else - font = 0; + printf ("\tScanning file %s...", file); + fflush (stdout); } - else - { - 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"); - if (!force && cache) - { - if (font) - { - FcChar8 *unparse; - - unparse = FcNameUnparse (font); - if (unparse) - { - (void) FcFileCacheUpdate (cache, file, id, unparse); - free (unparse); - } - } - else - { - /* negative cache files not containing fonts */ - FcFileCacheUpdate (cache, file, id, (FcChar8 *) FC_INVALID_FONT_FILE); - } - } - } - if (font) + font = FcFreeTypeQuery (file, id, blanks, &count); + if (FcDebug () & FC_DBG_SCAN) + printf ("done\n"); + /* + * Add the font + */ + if (font && (!config || FcConfigAcceptFont (config, font))) { if (!FcFontSetAdd (set, font)) { @@ -101,78 +73,221 @@ FcFileScan (FcFontSet *set, ret = FcFalse; } } + else if (font) + FcPatternDestroy (font); id++; } while (font && ret && id < count); return ret; } FcBool -FcDirScan (FcFontSet *set, - FcFileCache *cache, - FcBlanks *blanks, - const FcChar8 *dir, - FcBool force) +FcFileScanConfig (FcFontSet *set, + FcStrSet *dirs, + FcBlanks *blanks, + const FcChar8 *file, + FcBool force, + FcConfig *config) { - DIR *d; - struct dirent *e; - FcChar8 *file; - FcChar8 *base; - FcBool ret = FcTrue; - - file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + 256 + 1); - if (!file) - return FcFalse; + 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, + FcFileCache *cache, /* XXX unused */ + FcBlanks *blanks, + const FcChar8 *file, + FcBool force) +{ + 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); +} + +/* + * Scan 'dir', adding font files to 'set' and + * subdirectories to 'dirs' + */ + +FcBool +FcDirScanConfig (FcFontSet *set, + FcStrSet *dirs, + FcBlanks *blanks, + const FcChar8 *dir, + FcBool force, + FcConfig *config) +{ + DIR *d; + FcChar8 *canon_dir; + struct dirent *e; + FcStrSet *dirlist, *filelist; + FcChar8 *file; + FcChar8 *base; + FcBool ret = FcTrue; + FcFontSet *tmpSet; + int i; + + canon_dir = FcStrCanonFilename (dir); + if (!canon_dir) canon_dir = (FcChar8 *) dir; + printf ("dir %s canon %s\n", dir, canon_dir); + + if (config && !FcConfigAcceptFilename (config, canon_dir)) { + ret = FcTrue; + goto bail; + } - strcpy ((char *) file, (char *) dir); - strcat ((char *) file, "/"); - base = file + strlen ((char *) file); if (!force) { - strcpy ((char *) base, FC_DIR_CACHE_FILE); - - if (FcFileCacheReadDir (set, file)) - { - free (file); - return FcTrue; + if (FcDirCacheRead (set, dirs, canon_dir, config)) { + ret = FcTrue; + goto bail; } } - d = opendir ((char *) dir); + if (FcDebug () & FC_DBG_FONTSET) + printf ("cache scan dir %s\n", canon_dir); + + /* freed below */ + file = (FcChar8 *) malloc (strlen ((char *) canon_dir) + 1 + FC_MAX_FILE_LEN + 1); + if (!file) { + ret = FcFalse; + goto bail; + } + + strcpy ((char *) file, (char *) canon_dir); + strcat ((char *) file, "/"); + base = file + strlen ((char *) file); + + if (FcDebug () & FC_DBG_SCAN) + printf ("\tScanning dir %s\n", canon_dir); + + d = opendir ((char *) canon_dir); if (!d) { - free (file); - return FcFalse; + /* Don't complain about missing directories */ + if (errno == ENOENT) + ret = FcTrue; + else + ret = FcFalse; + goto bail_1; } - while (ret && (e = readdir (d))) + + tmpSet = FcFontSetCreate(); + if (!tmpSet) + { + ret = FcFalse; + goto bail0; + } + + dirlist = FcStrSetCreate (); + if (!dirlist) { - if (e->d_name[0] != '.') + 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 = FcFileScan (set, cache, blanks, file, force); + if (FcFileIsDir (file)) { + if (!FcStrSetAdd (dirlist, file)) { + ret = FcFalse; + goto bail3; + } + } else { + if (!FcStrSetAdd (filelist, file)) { + ret = FcFalse; + goto bail3; + } + } } } - free (file); + /* + * 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, + * write out the cache file + */ + FcDirCacheWrite (tmpSet, dirlist, canon_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); + + bail_1: + free (file); + bail: + if (canon_dir != dir) free (canon_dir); return ret; } FcBool -FcDirSave (FcFontSet *set, const FcChar8 *dir) +FcDirScan (FcFontSet *set, + FcStrSet *dirs, + FcFileCache *cache, /* XXX unused */ + FcBlanks *blanks, + const FcChar8 *dir, + FcBool force) { - FcChar8 *file; - FcChar8 *base; - FcBool ret; - - file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + 256 + 1); - if (!file) - return FcFalse; - - strcpy ((char *) file, (char *) dir); - strcat ((char *) file, "/"); - base = file + strlen ((char *) file); - strcpy ((char *) base, FC_DIR_CACHE_FILE); - ret = FcFileCacheWriteDir (set, file); - free (file); - return ret; + return FcDirScanConfig (set, dirs, blanks, dir, force, 0); } +FcBool +FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) +{ + return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ()); +}