/*
- * $XFree86: xc/lib/fontconfig/src/fcdir.c,v 1.5 2002/05/21 17:06:22 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
#include "fcint.h"
#include <dirent.h>
-static FcBool
+FcBool
FcFileIsDir (const FcChar8 *file)
{
struct stat statb;
}
FcBool
-FcFileScan (FcFontSet *set,
- FcStrSet *dirs,
- FcFileCache *cache,
- FcBlanks *blanks,
- const FcChar8 *file,
- FcBool force)
+FcFileScanConfig (FcFontSet *set,
+ FcStrSet *dirs,
+ FcGlobalCache *cache,
+ FcBlanks *blanks,
+ const FcChar8 *file,
+ FcBool force,
+ FcConfig *config)
{
- int id;
- FcChar8 *name;
- FcPattern *font;
- FcBool ret = FcTrue;
- FcBool isDir;
- int count;
+ int id;
+ FcPattern *font;
+ FcBool ret = FcTrue;
+ int count = 0;
+ if (config && !FcConfigAcceptFilename (config, file))
+ return FcTrue;
+
+ if (FcFileIsDir (file))
+ return FcStrSetAdd (dirs, file);
+
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 (FcStrCmp (name, FC_FONT_FILE_INVALID) == 0)
- font = 0;
- else if (FcStrCmp (name, FC_FONT_FILE_DIR) == 0)
- {
- ret = FcStrSetAdd (dirs, file);
- font = 0;
- }
- else
- {
- font = FcNameParse (name);
- if (font)
- if (!FcPatternAddString (font, FC_FILE, file))
- ret = FcFalse;
- }
- }
- 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");
- isDir = FcFalse;
- if (!font && FcFileIsDir (file))
- {
- isDir = FcTrue;
- ret = FcStrSetAdd (dirs, file);
- }
- if (!force && cache)
- {
- if (font)
- {
- FcChar8 *unparse;
-
- unparse = FcNameUnparse (font);
- if (unparse)
- {
- (void) FcFileCacheUpdate (cache, file, id, unparse);
- free (unparse);
- }
- }
- else
- {
- if (isDir)
- {
- FcFileCacheUpdate (cache, file, id, (FcChar8 *)
- FC_FONT_FILE_DIR);
- }
- else
- {
- /* negative cache files not containing fonts */
- FcFileCacheUpdate (cache, file, id, (FcChar8 *)
- FC_FONT_FILE_INVALID);
- }
- }
- }
+ printf ("\tScanning file %s...", file);
+ fflush (stdout);
}
- 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))
{
ret = FcFalse;
}
}
+ else if (font)
+ FcPatternDestroy (font);
id++;
} while (font && ret && id < count);
return ret;
}
FcBool
-FcDirCacheValid (const FcChar8 *dir)
+FcFileScan (FcFontSet *set,
+ FcStrSet *dirs,
+ FcGlobalCache *cache,
+ FcBlanks *blanks,
+ const FcChar8 *file,
+ FcBool force)
{
- FcChar8 *path;
- FcBool ret;
+ return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
+}
- path = (FcChar8 *) malloc (strlen ((const char *) dir) + 1 +
- strlen ((const char *) FC_DIR_CACHE_FILE) + 1);
- if (!path)
- return FcFalse;
- strcpy ((char *) path, (const char *) dir);
- strcat ((char *) path, (const char *) "/");
- strcat ((char *) path, (const char *) FC_DIR_CACHE_FILE);
- ret = FcFileCacheValid (path);
- free (path);
- return ret;
+/*
+ * 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);
}
-#define FC_MAX_FILE_LEN 4096
+/*
+ * Scan 'dir', adding font files to 'set' and
+ * subdirectories to 'dirs'
+ */
FcBool
-FcDirScan (FcFontSet *set,
- FcStrSet *dirs,
- FcFileCache *cache,
- FcBlanks *blanks,
- const FcChar8 *dir,
- FcBool force)
+FcDirScanConfig (FcFontSet *set,
+ FcStrSet *dirs,
+ FcGlobalCache *cache,
+ FcBlanks *blanks,
+ const FcChar8 *dir,
+ FcBool force,
+ FcConfig *config)
{
- DIR *d;
- struct dirent *e;
- FcChar8 *file;
- FcChar8 *base;
- FcBool ret = FcTrue;
+ DIR *d;
+ struct dirent *e;
+ FcChar8 **dirlist;
+ int dirlistlen, dirlistalloc;
+ FcChar8 *file;
+ FcChar8 *base;
+ FcBool ret = FcTrue;
+ FcFontSet *tmpSet;
+ int i;
+
+ if (config && !FcConfigAcceptFilename (config, dir))
+ return FcTrue;
+ if (!force)
+ {
+ /*
+ * Check ~/.fonts.cache-<version> file
+ */
+ if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
+ return FcTrue;
+
+ if (FcDirCacheValid (dir, config) &&
+ FcDirCacheHasCurrentArch (dir, config) &&
+ FcDirCacheRead (set, dirs, dir, config))
+ return FcTrue;
+ }
+
+ /* freed below */
file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
if (!file)
return FcFalse;
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, dirs, file))
- {
- free (file);
- return FcTrue;
- }
- }
+ if (FcDebug () & FC_DBG_SCAN)
+ printf ("\tScanning dir %s\n", dir);
+
d = opendir ((char *) dir);
if (!d)
{
return FcTrue;
return FcFalse;
}
- while (ret && (e = readdir (d)))
+
+ tmpSet = FcFontSetCreate();
+ if (!tmpSet)
+ {
+ ret = FcFalse;
+ goto bail0;
+ }
+
+ dirlistlen = 0;
+ dirlistalloc = 8;
+ dirlist = malloc(dirlistalloc * sizeof(FcChar8 *));
+ if (!dirlist)
+ {
+ ret = FcFalse;
+ goto bail1;
+ }
+ 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, dirs, cache, blanks, file, force);
+ if (dirlistlen == dirlistalloc)
+ {
+ FcChar8 **tmp_dirlist;
+
+ dirlistalloc *= 2;
+ tmp_dirlist = realloc(dirlist,
+ dirlistalloc * sizeof(FcChar8 *));
+ if (!tmp_dirlist)
+ {
+ ret = FcFalse;
+ goto bail2;
+ }
+ dirlist = tmp_dirlist;
+ }
+ dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1);
+ if (!dirlist[dirlistlen])
+ {
+ ret = FcFalse;
+ goto bail2;
+ }
+ strcpy((char *)dirlist[dirlistlen], e->d_name);
+ dirlistlen++;
}
}
- free (file);
+ qsort(dirlist, dirlistlen, sizeof(FcChar8 *), cmpstringp);
+ i = 0;
+ while (ret && i < dirlistlen)
+ {
+ strcpy ((char *) base, (char *) dirlist[i]);
+ ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
+ i++;
+ }
+ /*
+ * Now that the directory has been scanned,
+ * add the cache entry
+ */
+ if (ret && cache)
+ FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
+
+ for (i = 0; i < tmpSet->nfont; i++)
+ FcFontSetAdd (set, tmpSet->fonts[i]);
+
+ if (tmpSet->fonts)
+ {
+ FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
+ free (tmpSet->fonts);
+ }
+ FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
+
+ bail2:
+ for (i = 0; i < dirlistlen; i++)
+ free(dirlist[i]);
+
+ free (dirlist);
+
+ bail1:
+ free (tmpSet);
+
+ bail0:
closedir (d);
+
+ free (file);
return ret;
}
FcBool
-FcDirSave (FcFontSet *set, FcStrSet *dirs, const FcChar8 *dir)
+FcDirScan (FcFontSet *set,
+ FcStrSet *dirs,
+ FcGlobalCache *cache,
+ 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;
+ return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
+}
- strcpy ((char *) file, (char *) dir);
- strcat ((char *) file, "/");
- base = file + strlen ((char *) file);
- strcpy ((char *) base, FC_DIR_CACHE_FILE);
- ret = FcFileCacheWriteDir (set, dirs, file);
- free (file);
- return ret;
+FcBool
+FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
+{
+ return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ());
}