Replace UUID file mechanism with per-directory 'map' attribute [v2]

The UUID files would be placed in each font directory to provide the
unique cache name, independent of path, for that directory. The UUID
files are undesireable for a couple of reasons:

 1) They must be placed in the font directories to be useful. This
    requires modifying the font directories themselves, introducing
    potential visible timestamp changes when running multiple
    applications, and makes the cache processing inconsistent between
    applications with permission to write to the font directories and
    applications without such permission.

 2) The UUID contents were generated randomly, which makes the font
    cache not reproducible across multiple runs.

One proposed fix for 2) is to make the UUID dependent on the font
directory path, but once we do that, we can simply use the font
directory path itself as the key as the original MD5-based font cache
naming mechanism did.

The goal of the UUID file mechanism was to fix startup time of
flatpaks; as the font path names inside the flatpak did not match the
font path names in the base system, the font cache would need to be
reconstructed the first time the flatpak was launched.

The new mechanism for doing this is to allow each '<dir>' element in
the configuration include a 'map' attribute. When looking for a cache
file for a particular directory, if the directory name starts with the
contents of the <dir> element, that portion of the name will be
replaced with the value of the 'map' attribute.

Outside of the flatpak, nothing need change -- fontconfig will build
cache files using real directory names.

Inside the flatpak, the custom fonts.conf file will now include
mappings such as this:

	<dir map="/usr/share/fonts">/run/host/fonts</dir>

When scanning the directory /run/host/fonts/ttf, fontconfig will
use the name /usr/share/fonts/ttf as the source for building the cache
file name.

The existing FC_FILE replacement code used for the UUID-based
implementation continues to correctly adapt font path names seen by
applications.

v2:
	Leave FcDirCacheCreateUUID stub around to avoid removing
	public API function.

	Document 'map' attribute of <dir> element in
	fontconfig-user.sgml

	Suggested-by: Akira TAGOH <akira@tagoh.org>

Signed-off-by: Keith Packard <keithp@keithp.com>
This commit is contained in:
Keith Packard 2018-10-29 16:39:05 -07:00 committed by Akira TAGOH
parent 4cde12bfda
commit c4324f54ee
17 changed files with 280 additions and 300 deletions

View File

@ -332,39 +332,6 @@ AM_CONDITIONAL(FREETYPE_PCF_LONG_FAMILY_NAMES, test "x$have_pcf_long_family_name
LIBS="$fontconfig_save_libs" LIBS="$fontconfig_save_libs"
CFLAGS="$fontconfig_save_cflags" CFLAGS="$fontconfig_save_cflags"
#
# Check for uuid
#
if test "$os_win32" != "yes"; then
use_pkgconfig_for_uuid=yes
if test -n "${UUID_LIBS}"; then
save_UUID_LIBS=$UUID_LIBS
fi
PKG_CHECK_MODULES([UUID], [uuid],
[use_pkgconfig_for_uuid=yes],
[use_pkgconfig_for_uuid=no])
if test "x$use_pkgconfig_for_uuid" = "xno"; then
AC_MSG_CHECKING([where uuid functions comes from])
AC_TRY_LINK([#include <uuid/uuid.h>],
[uuid_t a; uuid_generate(a);],
[AC_MSG_RESULT([builtin])
UUID_CFLAGS=""
UUID_LIBS=""
],[AC_MSG_ERROR([
*** uuid is required. install util-linux.
])])
else
if test -z "${save_UUID_LIBS+x}" && test "x${UUID_LIBS}" != "x"; then
PKGCONFIG_REQUIRES_PRIVATELY="$PKGCONFIG_REQUIRES_PRIVATELY uuid"
fi
fi
else
UUID_CFLAGS=""
UUID_LIBS=""
fi
AC_SUBST(UUID_CFLAGS)
AC_SUBST(UUID_LIBS)
# #
# Check expat configuration # Check expat configuration
# #

View File

@ -88,20 +88,6 @@ to <parameter>config</parameter>.
@SINCE@ 2.9.91 @SINCE@ 2.9.91
@@ @@
@RET@ FcBool
@FUNC@ FcDirCacheCreateUUID
@TYPE1@ FcChar8 * @ARG1@ dir
@TYPE2@ FcBool @ARG2@ force
@TYPE3@ FcConfig * @ARG3@ config
@PURPOSE@ Create .uuid file at a directory
@DESC@
This is to create .uuid file containing an UUID at a font directory of
<parameter>dir</parameter>.
The UUID will be used to identify the font directory and is used to determine
the cache filename if available.
@SINCE@ 2.12.92
@@
@RET@ FcBool @RET@ FcBool
@FUNC@ FcDirCacheDeleteUUID @FUNC@ FcDirCacheDeleteUUID
@TYPE1@ const FcChar8 * @ARG1@ dir @TYPE1@ const FcChar8 * @ARG1@ dir

View File

@ -310,9 +310,15 @@ following structure:
This is the top level element for a font configuration and can contain This is the top level element for a font configuration and can contain
<literal>&lt;dir&gt;</literal>, <literal>&lt;cachedir&gt;</literal>, <literal>&lt;include&gt;</literal>, <literal>&lt;match&gt;</literal> and <literal>&lt;alias&gt;</literal> elements in any order. <literal>&lt;dir&gt;</literal>, <literal>&lt;cachedir&gt;</literal>, <literal>&lt;include&gt;</literal>, <literal>&lt;match&gt;</literal> and <literal>&lt;alias&gt;</literal> elements in any order.
</para></refsect2> </para></refsect2>
<refsect2><title><literal>&lt;dir prefix="default"&gt;</literal></title><para> <refsect2><title><literal>&lt;dir prefix="default" map=""&gt;</literal></title><para>
This element contains a directory name which will be scanned for font files This element contains a directory name which will be scanned for font files
to include in the set of available fonts. If 'prefix' is set to "default" or "cwd", the current working directory will be added as the path prefix prior to the value. If 'prefix' is set to "xdg", the value in the XDG_DATA_HOME environment variable will be added as the path prefix. please see XDG Base Directory Specification for more details. If 'prefix' is set to "relative", the path of current file will be added prior to the value. to include in the set of available fonts. If 'prefix' is set to "default" or "cwd", the current working directory will be added as the path prefix prior to the value. If 'prefix' is set to "xdg", the value in the XDG_DATA_HOME environment variable will be added as the path prefix. please see XDG Base Directory Specification for more details. If 'prefix' is set to "relative", the path of current file will be added prior to the value.
</para><para>
If a 'map' attribute is specified, its value will replace the
directory path when matching cached information for fonts contained in
this directory. This is useful if the directory name is an alias
(via a bind mount or symlink) to another directory in the system for
which cached font information is likely to exist.
</para></refsect2> </para></refsect2>
<refsect2><title><literal>&lt;cachedir prefix="default"&gt;</literal></title><para> <refsect2><title><literal>&lt;cachedir prefix="default"&gt;</literal></title><para>
This element contains a directory name that is supposed to be stored or read This element contains a directory name that is supposed to be stored or read

View File

@ -203,7 +203,6 @@ scanDirs (FcStrList *list, FcConfig *config, FcBool force, FcBool really_force,
if (really_force) if (really_force)
{ {
FcDirCacheUnlink (dir, config); FcDirCacheUnlink (dir, config);
FcDirCacheCreateUUID ((FcChar8 *) dir, FcTrue, config);
} }
cache = NULL; cache = NULL;

View File

@ -14,6 +14,7 @@
--> -->
<!ELEMENT dir (#PCDATA)> <!ELEMENT dir (#PCDATA)>
<!ATTLIST dir <!ATTLIST dir
map CDATA #IMPLIED
prefix (default|xdg|relative|cwd) "default" prefix (default|xdg|relative|cwd) "default"
xml:space (default|preserve) 'preserve'> xml:space (default|preserve) 'preserve'>

View File

@ -77,7 +77,6 @@ AM_CPPFLAGS = \
$(ICONV_CFLAGS) \ $(ICONV_CFLAGS) \
$(LIBXML2_CFLAGS) \ $(LIBXML2_CFLAGS) \
$(EXPAT_CFLAGS) \ $(EXPAT_CFLAGS) \
$(UUID_CFLAGS) \
$(WARN_CFLAGS) \ $(WARN_CFLAGS) \
-DFC_CACHEDIR='"$(FC_CACHEDIR)"' \ -DFC_CACHEDIR='"$(FC_CACHEDIR)"' \
-DFONTCONFIG_PATH='"$(BASECONFIGDIR)"' \ -DFONTCONFIG_PATH='"$(BASECONFIGDIR)"' \
@ -167,7 +166,7 @@ lib_LTLIBRARIES = libfontconfig.la
libfontconfig_la_LDFLAGS = \ libfontconfig_la_LDFLAGS = \
-version-info @LIBT_VERSION_INFO@ -no-undefined $(export_symbols) -version-info @LIBT_VERSION_INFO@ -no-undefined $(export_symbols)
libfontconfig_la_LIBADD = $(ICONV_LIBS) $(FREETYPE_LIBS) $(LIBXML2_LIBS) $(EXPAT_LIBS) $(UUID_LIBS) $(LTLIBINTL) libfontconfig_la_LIBADD = $(ICONV_LIBS) $(FREETYPE_LIBS) $(LIBXML2_LIBS) $(EXPAT_LIBS) $(LTLIBINTL)
libfontconfig_la_DEPENDENCIES = $(fontconfig_def_dependency) libfontconfig_la_DEPENDENCIES = $(fontconfig_def_dependency)

View File

@ -51,103 +51,7 @@ FcDirCacheCreateUUID (FcChar8 *dir,
FcBool force, FcBool force,
FcConfig *config) FcConfig *config)
{ {
FcBool ret = FcTrue; return FcTrue;
#ifndef _WIN32
const FcChar8 *sysroot = FcConfigGetSysRoot (config);
FcChar8 *target;
FcChar8 *uuidname;
if (sysroot)
target = FcStrBuildFilename (sysroot, dir, NULL);
else
target = FcStrdup (dir);
uuidname = FcStrBuildFilename (target, ".uuid", NULL);
if (!uuidname)
{
FcStrFree (target);
return FcFalse;
}
if (force || access ((const char *) uuidname, F_OK) < 0)
{
FcAtomic *atomic;
int fd;
uuid_t uuid;
char out[37];
FcBool (* hash_add) (FcHashTable *, void*, void*);
struct stat statb;
struct timeval times[2];
if (FcStat (target, &statb) != 0)
{
ret = FcFalse;
goto bail1;
}
atomic = FcAtomicCreate (uuidname);
if (!atomic)
{
ret = FcFalse;
goto bail1;
}
if (!FcAtomicLock (atomic))
{
ret = FcFalse;
goto bail2;
}
fd = FcOpen ((char *)FcAtomicNewFile (atomic), O_RDWR | O_CREAT, 0644);
if (fd == -1)
{
ret = FcFalse;
goto bail3;
}
uuid_generate_random (uuid);
if (force)
hash_add = FcHashTableReplace;
else
hash_add = FcHashTableAdd;
if (!hash_add (config->uuid_table, target, uuid))
{
ret = FcFalse;
FcAtomicDeleteNew (atomic);
close (fd);
goto bail3;
}
uuid_unparse (uuid, out);
if (FcDebug () & FC_DBG_CACHE)
printf ("FcDirCacheCreateUUID %s: %s\n", uuidname, out);
write (fd, out, strlen (out));
close (fd);
FcAtomicReplaceOrig (atomic);
bail3:
FcAtomicUnlock (atomic);
bail2:
FcAtomicDestroy (atomic);
if (ret)
{
/* revert mtime of the directory */
times[0].tv_sec = statb.st_atime;
times[1].tv_sec = statb.st_mtime;
#ifdef HAVE_STRUCT_STAT_ST_MTIM
times[0].tv_usec = statb.st_atim.tv_nsec / 1000;
times[1].tv_usec = statb.st_mtim.tv_nsec / 1000;
#else
times[0].tv_usec = 0;
times[1].tv_usec = 0;
#endif
if (utimes ((const char *) target, times) != 0)
{
fprintf (stderr, "Unable to revert mtime: %s\n", target);
}
}
}
bail1:
FcStrFree (uuidname);
FcStrFree (target);
#endif
return ret;
} }
FcBool FcBool
@ -187,7 +91,6 @@ FcDirCacheDeleteUUID (const FcChar8 *dir,
{ {
fprintf (stderr, "Unable to revert mtime: %s\n", d); fprintf (stderr, "Unable to revert mtime: %s\n", d);
} }
FcHashTableRemove (config->uuid_table, target);
} }
FcStrFree (target); FcStrFree (target);
bail: bail:
@ -197,59 +100,6 @@ bail:
return ret; return ret;
} }
#ifndef _WIN32
static void
FcDirCacheReadUUID (FcChar8 *dir,
FcConfig *config)
{
void *u;
uuid_t uuid;
const FcChar8 *sysroot = FcConfigGetSysRoot (config);
FcChar8 *target;
if (sysroot)
target = FcStrBuildFilename (sysroot, dir, NULL);
else
target = FcStrdup (dir);
if (!FcHashTableFind (config->uuid_table, target, &u))
{
FcChar8 *uuidname = FcStrBuildFilename (target, ".uuid", NULL);
int fd;
if ((fd = FcOpen ((char *) uuidname, O_RDONLY)) >= 0)
{
char suuid[37];
ssize_t len;
memset (suuid, 0, sizeof (suuid));
len = read (fd, suuid, 36);
if (len != -1)
{
suuid[len] = 0;
memset (uuid, 0, sizeof (uuid));
if (uuid_parse (suuid, uuid) == 0)
{
if (FcDebug () & FC_DBG_CACHE)
printf ("FcDirCacheReadUUID %s -> %s\n", uuidname, suuid);
FcHashTableAdd (config->uuid_table, target, uuid);
}
}
close (fd);
}
else
{
if (FcDebug () & FC_DBG_CACHE)
printf ("FcDirCacheReadUUID Unable to read %s\n", uuidname);
}
FcStrFree (uuidname);
}
else
FcHashUuidFree (u);
FcStrFree (target);
}
#endif
struct MD5Context { struct MD5Context {
FcChar32 buf[4]; FcChar32 buf[4];
FcChar32 bits[2]; FcChar32 bits[2];
@ -300,18 +150,26 @@ static const char bin2hex[] = { '0', '1', '2', '3',
'c', 'd', 'e', 'f' }; 'c', 'd', 'e', 'f' };
static FcChar8 * static FcChar8 *
FcDirCacheBasenameMD5 (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN]) FcDirCacheBasenameMD5 (FcConfig *config, const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN])
{ {
FcChar8 *new_dir;
unsigned char hash[16]; unsigned char hash[16];
FcChar8 *hex_hash; FcChar8 *hex_hash;
int cnt; int cnt;
struct MD5Context ctx; struct MD5Context ctx;
new_dir = FcConfigMapFontPath(config, dir);
if (new_dir)
dir = new_dir;
MD5Init (&ctx); MD5Init (&ctx);
MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir)); MD5Update (&ctx, (const unsigned char *)dir, strlen ((const char *) dir));
MD5Final (hash, &ctx); MD5Final (hash, &ctx);
if (new_dir)
FcStrFree(new_dir);
cache_base[0] = '/'; cache_base[0] = '/';
hex_hash = cache_base + 1; hex_hash = cache_base + 1;
for (cnt = 0; cnt < 16; ++cnt) for (cnt = 0; cnt < 16; ++cnt)
@ -325,31 +183,6 @@ FcDirCacheBasenameMD5 (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN])
return cache_base; return cache_base;
} }
#ifndef _WIN32
static FcChar8 *
FcDirCacheBasenameUUID (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN], FcConfig *config)
{
void *u;
FcChar8 *target;
const FcChar8 *sysroot = FcConfigGetSysRoot (config);
if (sysroot)
target = FcStrBuildFilename (sysroot, dir, NULL);
else
target = FcStrdup (dir);
if (FcHashTableFind (config->uuid_table, target, &u))
{
uuid_unparse (u, (char *) cache_base);
strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
FcHashUuidFree (u);
FcStrFree (target);
return cache_base;
}
FcStrFree (target);
return NULL;
}
#endif
FcBool FcBool
FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config) FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
{ {
@ -359,10 +192,7 @@ FcDirCacheUnlink (const FcChar8 *dir, FcConfig *config)
FcChar8 *cache_dir; FcChar8 *cache_dir;
const FcChar8 *sysroot = FcConfigGetSysRoot (config); const FcChar8 *sysroot = FcConfigGetSysRoot (config);
#ifndef _WIN32 FcDirCacheBasenameMD5 (config, dir, cache_base);
if (!FcDirCacheBasenameUUID (dir, cache_base, config))
#endif
FcDirCacheBasenameMD5 (dir, cache_base);
list = FcStrListCreate (config->cacheDirs); list = FcStrListCreate (config->cacheDirs);
if (!list) if (!list)
@ -439,10 +269,7 @@ FcDirCacheProcess (FcConfig *config, const FcChar8 *dir,
} }
FcStrFree (d); FcStrFree (d);
#ifndef _WIN32 FcDirCacheBasenameMD5 (config, dir, cache_base);
if (!FcDirCacheBasenameUUID (dir, cache_base, config))
#endif
FcDirCacheBasenameMD5 (dir, cache_base);
list = FcStrListCreate (config->cacheDirs); list = FcStrListCreate (config->cacheDirs);
if (!list) if (!list)
@ -1070,9 +897,6 @@ FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
{ {
FcCache *cache = NULL; FcCache *cache = NULL;
#ifndef _WIN32
FcDirCacheReadUUID ((FcChar8 *) dir, config);
#endif
if (!FcDirCacheProcess (config, dir, if (!FcDirCacheProcess (config, dir,
FcDirCacheMapHelper, FcDirCacheMapHelper,
&cache, cache_file)) &cache, cache_file))
@ -1377,10 +1201,7 @@ FcDirCacheWrite (FcCache *cache, FcConfig *config)
if (!cache_dir) if (!cache_dir)
return FcFalse; return FcFalse;
#ifndef _WIN32 FcDirCacheBasenameMD5 (config, dir, cache_base);
if (!FcDirCacheBasenameUUID (dir, cache_base, config))
#endif
FcDirCacheBasenameMD5 (dir, cache_base);
cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL); cache_hashed = FcStrBuildFilename (cache_dir, cache_base, NULL);
FcStrFree (cache_dir); FcStrFree (cache_dir);
if (!cache_hashed) if (!cache_hashed)
@ -1577,10 +1398,7 @@ FcDirCacheLock (const FcChar8 *dir,
const FcChar8 *sysroot = FcConfigGetSysRoot (config); const FcChar8 *sysroot = FcConfigGetSysRoot (config);
int fd = -1; int fd = -1;
#ifndef _WIN32 FcDirCacheBasenameMD5 (config, dir, cache_base);
if (!FcDirCacheBasenameUUID (dir, cache_base, config))
#endif
FcDirCacheBasenameMD5 (dir, cache_base);
list = FcStrListCreate (config->cacheDirs); list = FcStrListCreate (config->cacheDirs);
if (!list) if (!list)
return -1; return -1;

View File

@ -52,18 +52,6 @@ retry:
return config; return config;
} }
static FcChar32
FcHashAsStrIgnoreCase (const void *data)
{
return FcStrHashIgnoreCase (data);
}
static int
FcCompareAsStr (const void *v1, const void *v2)
{
return FcStrCmp (v1, v2);
}
static void static void
FcDestroyAsRule (void *data) FcDestroyAsRule (void *data)
{ {
@ -76,12 +64,6 @@ FcDestroyAsRuleSet (void *data)
FcRuleSetDestroy (data); FcRuleSetDestroy (data);
} }
static void
FcDestroyAsStr (void *data)
{
FcStrFree (data);
}
FcBool FcBool
FcConfigInit (void) FcConfigInit (void)
{ {
@ -127,6 +109,10 @@ FcConfigCreate (void)
if (!config->configDirs) if (!config->configDirs)
goto bail1; goto bail1;
config->configMapDirs = FcStrSetCreate();
if (!config->configMapDirs)
goto bail1_5;
config->configFiles = FcStrSetCreate (); config->configFiles = FcStrSetCreate ();
if (!config->configFiles) if (!config->configFiles)
goto bail2; goto bail2;
@ -182,13 +168,6 @@ FcConfigCreate (void)
if (!config->availConfigFiles) if (!config->availConfigFiles)
goto bail10; goto bail10;
config->uuid_table = FcHashTableCreate (FcHashAsStrIgnoreCase,
FcCompareAsStr,
FcHashStrCopy,
FcHashUuidCopy,
FcDestroyAsStr,
FcHashUuidFree);
FcRefInit (&config->ref, 1); FcRefInit (&config->ref, 1);
return config; return config;
@ -213,6 +192,8 @@ bail4:
bail3: bail3:
FcStrSetDestroy (config->configFiles); FcStrSetDestroy (config->configFiles);
bail2: bail2:
FcStrSetDestroy (config->configMapDirs);
bail1_5:
FcStrSetDestroy (config->configDirs); FcStrSetDestroy (config->configDirs);
bail1: bail1:
free (config); free (config);
@ -324,6 +305,7 @@ FcConfigDestroy (FcConfig *config)
(void) fc_atomic_ptr_cmpexch (&_fcConfig, config, NULL); (void) fc_atomic_ptr_cmpexch (&_fcConfig, config, NULL);
FcStrSetDestroy (config->configDirs); FcStrSetDestroy (config->configDirs);
FcStrSetDestroy (config->configMapDirs);
FcStrSetDestroy (config->fontDirs); FcStrSetDestroy (config->fontDirs);
FcStrSetDestroy (config->cacheDirs); FcStrSetDestroy (config->cacheDirs);
FcStrSetDestroy (config->configFiles); FcStrSetDestroy (config->configFiles);
@ -350,8 +332,6 @@ FcConfigDestroy (FcConfig *config)
if (config->sysRoot) if (config->sysRoot)
FcStrFree (config->sysRoot); FcStrFree (config->sysRoot);
FcHashTableDestroy (config->uuid_table);
free (config); free (config);
} }
@ -559,9 +539,10 @@ FcConfigGetConfigDirs (FcConfig *config)
FcBool FcBool
FcConfigAddFontDir (FcConfig *config, FcConfigAddFontDir (FcConfig *config,
const FcChar8 *d) const FcChar8 *d,
const FcChar8 *m)
{ {
return FcStrSetAddFilename (config->fontDirs, d); return FcStrSetAddFilenamePair (config->fontDirs, d, m);
} }
FcStrList * FcStrList *
@ -576,6 +557,47 @@ FcConfigGetFontDirs (FcConfig *config)
return FcStrListCreate (config->fontDirs); return FcStrListCreate (config->fontDirs);
} }
static FcBool
FcConfigPathStartsWith(const FcChar8 *path,
const FcChar8 *start)
{
int len = strlen((char *) start);
if (strncmp((char *) path, (char *) start, len) != 0)
return FcFalse;
switch (path[len]) {
case '\0':
case FC_DIR_SEPARATOR:
return FcTrue;
default:
return FcFalse;
}
}
FcChar8 *
FcConfigMapFontPath(FcConfig *config,
const FcChar8 *path)
{
FcStrList *list;
FcChar8 *dir;
FcChar8 *map;
list = FcConfigGetFontDirs(config);
if (!list)
return 0;
while ((dir = FcStrListNext(list)))
if (FcConfigPathStartsWith(path, dir))
break;
FcStrListDone(list);
if (!dir)
return 0;
map = FcStrPairSecond(dir);
if (!map)
return 0;
return FcStrBuildFilename(map, path + strlen((char *) dir), NULL);
}
FcBool FcBool
FcConfigAddCacheDir (FcConfig *config, FcConfigAddCacheDir (FcConfig *config,
const FcChar8 *d) const FcChar8 *d)

View File

@ -413,7 +413,6 @@ FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
{ {
FcCache *cache = NULL; FcCache *cache = NULL;
FcDirCacheCreateUUID ((FcChar8 *) dir, FcFalse, config);
/* Try to use existing cache file */ /* Try to use existing cache file */
if (!force) if (!force)
cache = FcDirCacheLoad (dir, config, NULL); cache = FcDirCacheLoad (dir, config, NULL);

View File

@ -52,23 +52,6 @@ FcHashStrCopy (const void *src,
return *dest != NULL; return *dest != NULL;
} }
FcBool
FcHashUuidCopy (const void *src,
void **dest)
{
#ifndef _WIN32
*dest = malloc (sizeof (uuid_t));
uuid_copy (*dest, src);
#endif
return FcTrue;
}
void
FcHashUuidFree (void *data)
{
free (data);
}
FcHashTable * FcHashTable *
FcHashTableCreate (FcHashFunc hash_func, FcHashTableCreate (FcHashFunc hash_func,
FcCompareFunc compare_func, FcCompareFunc compare_func,

View File

@ -509,6 +509,7 @@ struct _FcConfig {
* and those directives may occur in any order * and those directives may occur in any order
*/ */
FcStrSet *configDirs; /* directories to scan for fonts */ FcStrSet *configDirs; /* directories to scan for fonts */
FcStrSet *configMapDirs; /* mapped names to generate cache entries */
/* /*
* List of directories containing fonts, * List of directories containing fonts,
* built by recursively scanning the set * built by recursively scanning the set
@ -565,7 +566,6 @@ struct _FcConfig {
FcChar8 *sysRoot; /* override the system root directory */ FcChar8 *sysRoot; /* override the system root directory */
FcStrSet *availConfigFiles; /* config files available */ FcStrSet *availConfigFiles; /* config files available */
FcPtrList *rulesetList; /* List of rulesets being installed */ FcPtrList *rulesetList; /* List of rulesets being installed */
FcHashTable *uuid_table; /* UUID table for cachedirs */
}; };
typedef struct _FcFileTime { typedef struct _FcFileTime {
@ -659,7 +659,12 @@ FcConfigAddConfigDir (FcConfig *config,
FcPrivate FcBool FcPrivate FcBool
FcConfigAddFontDir (FcConfig *config, FcConfigAddFontDir (FcConfig *config,
const FcChar8 *d); const FcChar8 *d,
const FcChar8 *m);
FcPrivate FcChar8 *
FcConfigMapFontPath(FcConfig *config,
const FcChar8 *path);
FcPrivate FcBool FcPrivate FcBool
FcConfigAddCacheDir (FcConfig *config, FcConfigAddCacheDir (FcConfig *config,
@ -1236,6 +1241,15 @@ FcStrSetAddLangs (FcStrSet *strs, const char *languages);
FcPrivate void FcPrivate void
FcStrSetSort (FcStrSet * set); FcStrSetSort (FcStrSet * set);
FcPrivate FcBool
FcStrSetAddPair (FcStrSet *set, const FcChar8 *a, const FcChar8 *b);
FcPrivate FcChar8 *
FcStrPairSecond (FcChar8 *s);
FcPrivate FcBool
FcStrSetAddFilenamePair (FcStrSet *strs, const FcChar8 *d, const FcChar8 *m);
FcPrivate void FcPrivate void
FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size); FcStrBufInit (FcStrBuf *buf, FcChar8 *init, int size);

View File

@ -36,6 +36,27 @@ FcStrCopy (const FcChar8 *s)
return FcStrdup (s); return FcStrdup (s);
} }
static FcChar8 *
FcStrMakePair (const FcChar8 *s1, const FcChar8 *s2)
{
int s1l = s1 ? strlen ((char *) s1) : 0;
int s2l = s2 ? strlen ((char *) s2) : 0;
int l = s1l + 1 + s2l + 1;
FcChar8 *s = malloc (l);
if (!s)
return 0;
if (s1)
memcpy (s, s1, s1l + 1);
else
s[0] = '\0';
if (s2)
memcpy (s + s1l + 1, s2, s2l + 1);
else
s[s1l + 1] = '\0';
return s;
}
FcChar8 * FcChar8 *
FcStrPlus (const FcChar8 *s1, const FcChar8 *s2) FcStrPlus (const FcChar8 *s1, const FcChar8 *s2)
{ {
@ -1233,6 +1254,30 @@ FcStrSetAdd (FcStrSet *set, const FcChar8 *s)
return FcTrue; return FcTrue;
} }
FcBool
FcStrSetAddPair (FcStrSet *set, const FcChar8 *a, const FcChar8 *b)
{
FcChar8 *new = FcStrMakePair (a, b);
if (!new)
return FcFalse;
if (!_FcStrSetAppend (set, new))
{
FcStrFree (new);
return FcFalse;
}
return FcTrue;
}
FcChar8 *
FcStrPairSecond (FcChar8 *str)
{
FcChar8 *second = str + strlen((char *) str) + 1;
if (*second == '\0')
return 0;
return second;
}
FcBool FcBool
FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s) FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
{ {
@ -1247,6 +1292,37 @@ FcStrSetAddFilename (FcStrSet *set, const FcChar8 *s)
return FcTrue; return FcTrue;
} }
FcBool
FcStrSetAddFilenamePair (FcStrSet *set, const FcChar8 *a, const FcChar8 *b)
{
FcChar8 *new_a = NULL;
FcChar8 *new_b = NULL;
FcBool ret;
if (a)
{
new_a = FcStrCopyFilename (a);
if (!new_a)
return FcFalse;
}
if (b)
{
new_b = FcStrCopyFilename(b);
if (!new_b)
{
if (new_a)
FcStrFree(new_a);
return FcFalse;
}
}
ret = FcStrSetAddPair (set, new_a, new_b);
if (new_a)
FcStrFree (new_a);
if (new_b)
FcStrFree (new_b);
return ret;
}
FcBool FcBool
FcStrSetAddLangs (FcStrSet *strs, const char *languages) FcStrSetAddLangs (FcStrSet *strs, const char *languages)
{ {

View File

@ -2071,13 +2071,14 @@ FcParseUnary (FcConfigParse *parse, FcOp op)
static void static void
FcParseDir (FcConfigParse *parse) FcParseDir (FcConfigParse *parse)
{ {
const FcChar8 *attr, *data; const FcChar8 *attr, *data, *map;
FcChar8 *prefix = NULL, *p; FcChar8 *prefix = NULL, *p;
#ifdef _WIN32 #ifdef _WIN32
FcChar8 buffer[1000]; FcChar8 buffer[1000];
#endif #endif
attr = FcConfigGetAttribute (parse, "prefix"); attr = FcConfigGetAttribute (parse, "prefix");
map = FcConfigGetAttribute (parse, "map");
data = FcStrBufDoneStatic (&parse->pstack->str); data = FcStrBufDoneStatic (&parse->pstack->str);
if (attr) if (attr)
{ {
@ -2188,7 +2189,7 @@ FcParseDir (FcConfigParse *parse)
FcConfigMessage (parse, FcSevereWarning, "empty font directory name ignored"); FcConfigMessage (parse, FcSevereWarning, "empty font directory name ignored");
else if (!parse->scanOnly && (!FcStrUsesHome (data) || FcConfigHome ())) else if (!parse->scanOnly && (!FcStrUsesHome (data) || FcConfigHome ()))
{ {
if (!FcConfigAddFontDir (parse->config, data)) if (!FcConfigAddFontDir (parse->config, data, map))
FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data); FcConfigMessage (parse, FcSevereError, "out of memory; cannot add directory %s", data);
} }
FcStrBufDestroy (&parse->pstack->str); FcStrBufDestroy (&parse->pstack->str);

View File

@ -1,4 +1,4 @@
check_SCRIPTS=run-test.sh check_SCRIPTS=run-test.sh run-test-map.sh
TEST_EXTENSIONS = \ TEST_EXTENSIONS = \
.sh \ .sh \
$(NULL) $(NULL)

View File

@ -1,4 +1,4 @@
<fontconfig> <fontconfig>
<dir>@FONTDIR@</dir> <dir @MAP@>@FONTDIR@</dir>
<cachedir>@CACHEDIR@</cachedir> <cachedir>@CACHEDIR@</cachedir>
</fontconfig> </fontconfig>

107
test/run-test-map.sh Executable file
View File

@ -0,0 +1,107 @@
#!/bin/bash
# fontconfig/test/run-test-cache-map.sh
#
# Copyright © 2018 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
# the above copyright notice appear in all copies and that both that copyright
# notice and this permission notice appear in supporting documentation, and
# that the name of the copyright holders not be used in advertising or
# publicity pertaining to distribution of the software without specific,
# written prior permission. The copyright holders make no representations
# about the suitability of this software for any purpose. It is provided "as
# is" without express or implied warranty.
#
# THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
# EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
# OF THIS SOFTWARE.
#
case "$OSTYPE" in
msys ) MyPWD=`pwd -W` ;; # On Msys/MinGW, returns a MS Windows style path.
* ) MyPWD=`pwd` ;; # On any other platforms, returns a Unix style path.
esac
TESTDIR=${srcdir-"$MyPWD"}
BUILDTESTDIR=${builddir-"$MyPWD"}
FONTDIRA="$MyPWD"/fontsa
FONTDIRB="$MyPWD"/fontsb
CACHEDIR="$MyPWD"/cache.dir
EXPECTEDIN=${EXPECTEDIN-"out-map.expected.in"}
EXPECTEDA="out-map-a.expected"
EXPECTEDB="out-map-b.expected"
EXPECTED="out-map.expected"
FCLIST=../fc-list/fc-list$EXEEXT
FCCACHE=../fc-cache/fc-cache$EXEEXT
which bwrap > /dev/null 2>&1
if [ $? -eq 0 ]; then
BWRAP=`which bwrap`
fi
FONT1=$TESTDIR/4x6.pcf
FONT2=$TESTDIR/8x16.pcf
check () {
$FCLIST - file family pixelsize | sort > out
echo "=" >> out
$FCLIST - file family pixelsize | sort >> out
echo "=" >> out
$FCLIST - file family pixelsize | sort >> out
tr -d '\015' <out >out.tmp; mv out.tmp out
if cmp out $BUILDTESTDIR/$EXPECTED > /dev/null ; then : ; else
echo "*** Test failed: $TEST"
echo "*** output is in 'out', expected output in '$EXPECTED'"
exit 1
fi
rm -f out
}
prep() {
rm -rf $CACHEDIR
rm -rf $FONTDIRA $FONTDIRB
mkdir $FONTDIRA
mkdir $CACHEDIR
}
dotest () {
TEST=$1
test x$VERBOSE = x || echo Running: $TEST
}
sed "s!@FONTDIR@!$FONTDIRA!
s!@MAP@!!
s!@CACHEDIR@!$CACHEDIR!" < $TESTDIR/fonts.conf.in > fonts-a.conf
sed "s!@FONTDIR@!$FONTDIRB!
s!@MAP@!map="'"'"$FONTDIRA"'"'"!
s!@CACHEDIR@!$CACHEDIR!" < $TESTDIR/fonts.conf.in > fonts-b.conf
sed "s!@FONTDIR@!$FONTDIRA!" < $EXPECTEDIN > $EXPECTEDA
sed "s!@FONTDIR@!$FONTDIRB!" < $EXPECTEDIN > $EXPECTEDB
FONTCONFIG_FILE="$MyPWD"/fonts-a.conf
export FONTCONFIG_FILE
dotest "Basic check"
prep
cp $FONT1 $FONT2 $FONTDIRA
cp $EXPECTEDA $EXPECTED
$FCCACHE $FONTDIRA
check
dotest "mapped check"
prep
cp $FONT1 $FONT2 $FONTDIRA
cp $EXPECTEDB $EXPECTED
$FCCACHE $FONTDIRA
mv $FONTDIRA $FONTDIRB
export FONTCONFIG_FILE="$MyPWD"/fonts-b.conf
check

View File

@ -73,6 +73,7 @@ dotest () {
} }
sed "s!@FONTDIR@!$FONTDIR! sed "s!@FONTDIR@!$FONTDIR!
s!@MAP@!!
s!@CACHEDIR@!$CACHEDIR!" < $TESTDIR/fonts.conf.in > fonts.conf s!@CACHEDIR@!$CACHEDIR!" < $TESTDIR/fonts.conf.in > fonts.conf
FONTCONFIG_FILE="$MyPWD"/fonts.conf FONTCONFIG_FILE="$MyPWD"/fonts.conf
@ -145,6 +146,7 @@ sleep 1
ls -l $CACHEDIR > out1 ls -l $CACHEDIR > out1
TESTTMPDIR=`mktemp -d /tmp/fontconfig.XXXXXXXX` TESTTMPDIR=`mktemp -d /tmp/fontconfig.XXXXXXXX`
sed "s!@FONTDIR@!$TESTTMPDIR/fonts! sed "s!@FONTDIR@!$TESTTMPDIR/fonts!
s!@MAP@!map="'"'"$FONTDIR"'"'"!
s!@CACHEDIR@!$TESTTMPDIR/cache.dir!" < $TESTDIR/fonts.conf.in > bind-fonts.conf s!@CACHEDIR@!$TESTTMPDIR/cache.dir!" < $TESTDIR/fonts.conf.in > bind-fonts.conf
$BWRAP --bind / / --bind $CACHEDIR $TESTTMPDIR/cache.dir --bind $FONTDIR $TESTTMPDIR/fonts --bind .. $TESTTMPDIR/build --dev-bind /dev /dev --setenv FONTCONFIG_FILE $TESTTMPDIR/build/test/bind-fonts.conf $TESTTMPDIR/build/fc-match/fc-match$EXEEXT -f "%{file}\n" ":foundry=Misc" > xxx $BWRAP --bind / / --bind $CACHEDIR $TESTTMPDIR/cache.dir --bind $FONTDIR $TESTTMPDIR/fonts --bind .. $TESTTMPDIR/build --dev-bind /dev /dev --setenv FONTCONFIG_FILE $TESTTMPDIR/build/test/bind-fonts.conf $TESTTMPDIR/build/fc-match/fc-match$EXEEXT -f "%{file}\n" ":foundry=Misc" > xxx
$BWRAP --bind / / --bind $CACHEDIR $TESTTMPDIR/cache.dir --bind $FONTDIR $TESTTMPDIR/fonts --bind .. $TESTTMPDIR/build --dev-bind /dev /dev --setenv FONTCONFIG_FILE $TESTTMPDIR/build/test/bind-fonts.conf $TESTTMPDIR/build/test/test-bz106618$EXEEXT | sort > flist1 $BWRAP --bind / / --bind $CACHEDIR $TESTTMPDIR/cache.dir --bind $FONTDIR $TESTTMPDIR/fonts --bind .. $TESTTMPDIR/build --dev-bind /dev /dev --setenv FONTCONFIG_FILE $TESTTMPDIR/build/test/bind-fonts.conf $TESTTMPDIR/build/test/test-bz106618$EXEEXT | sort > flist1