Blame SOURCES/fontconfig-fix-flatpak.patch

f02dd2
diff -pruN fontconfig-2.13.0.orig/src/fccache.c fontconfig-2.13.0/src/fccache.c
f02dd2
--- fontconfig-2.13.0.orig/src/fccache.c	2017-12-18 21:45:13.000000000 +0900
f02dd2
+++ fontconfig-2.13.0/src/fccache.c	2018-06-08 18:39:33.079539192 +0900
f02dd2
@@ -51,13 +51,23 @@ FcDirCacheCreateUUID (FcChar8  *dir,
f02dd2
 		      FcBool    force,
f02dd2
 		      FcConfig *config)
f02dd2
 {
f02dd2
+    const FcChar8 *sysroot = FcConfigGetSysRoot (config);
f02dd2
+    FcChar8 *target;
f02dd2
     FcBool ret = FcTrue;
f02dd2
 #ifndef _WIN32
f02dd2
     FcChar8 *uuidname;
f02dd2
 
f02dd2
-    uuidname = FcStrBuildFilename (dir, ".uuid", NULL);
f02dd2
+    if (sysroot)
f02dd2
+	target = FcStrBuildFilename (sysroot, dir, NULL);
f02dd2
+    else
f02dd2
+	target = FcStrdup (dir);
f02dd2
+    uuidname = FcStrBuildFilename (target, ".uuid", NULL);
f02dd2
+
f02dd2
     if (!uuidname)
f02dd2
+    {
f02dd2
+	FcStrFree (target);
f02dd2
 	return FcFalse;
f02dd2
+    }
f02dd2
 
f02dd2
     if (force || access ((const char *) uuidname, F_OK) < 0)
f02dd2
     {
f02dd2
@@ -69,7 +79,7 @@ FcDirCacheCreateUUID (FcChar8  *dir,
f02dd2
 	struct stat statb;
f02dd2
 	struct timeval times[2];
f02dd2
 
f02dd2
-	if (FcStat (dir, &statb) != 0)
f02dd2
+	if (FcStat (target, &statb) != 0)
f02dd2
 	{
f02dd2
 	    ret = FcFalse;
f02dd2
 	    goto bail1;
f02dd2
@@ -96,7 +106,7 @@ FcDirCacheCreateUUID (FcChar8  *dir,
f02dd2
 	    hash_add = FcHashTableReplace;
f02dd2
 	else
f02dd2
 	    hash_add = FcHashTableAdd;
f02dd2
-	if (!hash_add (config->uuid_table, dir, uuid))
f02dd2
+	if (!hash_add (config->uuid_table, target, uuid))
f02dd2
 	{
f02dd2
 	    ret = FcFalse;
f02dd2
 	    goto bail3;
f02dd2
@@ -124,14 +134,15 @@ FcDirCacheCreateUUID (FcChar8  *dir,
f02dd2
 	    times[0].tv_usec = 0;
f02dd2
 	    times[1].tv_usec = 0;
f02dd2
 #endif
f02dd2
-	    if (utimes ((const  char *) dir, times) != 0)
f02dd2
+	    if (utimes ((const  char *) target, times) != 0)
f02dd2
 	    {
f02dd2
-		fprintf (stderr, "Unable to revert mtime: %s\n", dir);
f02dd2
+		fprintf (stderr, "Unable to revert mtime: %s\n", target);
f02dd2
 	    }
f02dd2
 	}
f02dd2
     }
f02dd2
-    bail1:
f02dd2
+bail1:
f02dd2
     FcStrFree (uuidname);
f02dd2
+    FcStrFree (target);
f02dd2
 #endif
f02dd2
 
f02dd2
     return ret;
f02dd2
@@ -144,10 +155,17 @@ FcDirCacheReadUUID (FcChar8  *dir,
f02dd2
 {
f02dd2
     void *u;
f02dd2
     uuid_t uuid;
f02dd2
+    const FcChar8 *sysroot = FcConfigGetSysRoot (config);
f02dd2
+    FcChar8 *target;
f02dd2
 
f02dd2
-    if (!FcHashTableFind (config->uuid_table, dir, &u))
f02dd2
+    if (sysroot)
f02dd2
+	target = FcStrBuildFilename (sysroot, dir, NULL);
f02dd2
+    else
f02dd2
+	target = FcStrdup (dir);
f02dd2
+
f02dd2
+    if (!FcHashTableFind (config->uuid_table, target, &u))
f02dd2
     {
f02dd2
-	FcChar8 *uuidname = FcStrBuildFilename (dir, ".uuid", NULL);
f02dd2
+	FcChar8 *uuidname = FcStrBuildFilename (target, ".uuid", NULL);
f02dd2
 	int fd;
f02dd2
 
f02dd2
 	if ((fd = FcOpen ((char *) uuidname, O_RDONLY)) >= 0)
f02dd2
@@ -162,7 +180,7 @@ FcDirCacheReadUUID (FcChar8  *dir,
f02dd2
 		{
f02dd2
 		    if (FcDebug () & FC_DBG_CACHE)
f02dd2
 			printf ("FcDirCacheReadUUID %s -> %s\n", uuidname, suuid);
f02dd2
-		    FcHashTableAdd (config->uuid_table, dir, uuid);
f02dd2
+		    FcHashTableAdd (config->uuid_table, target, uuid);
f02dd2
 		}
f02dd2
 	    }
f02dd2
 	    close (fd);
f02dd2
@@ -176,6 +194,7 @@ FcDirCacheReadUUID (FcChar8  *dir,
f02dd2
     }
f02dd2
     else
f02dd2
 	FcHashUuidFree (u);
f02dd2
+    FcStrFree (target);
f02dd2
 }
f02dd2
 #endif
f02dd2
 
f02dd2
@@ -259,19 +278,22 @@ static FcChar8 *
f02dd2
 FcDirCacheBasenameUUID (const FcChar8 *dir, FcChar8 cache_base[CACHEBASE_LEN], FcConfig *config)
f02dd2
 {
f02dd2
     void *u;
f02dd2
-    FcChar8 *alias;
f02dd2
+    FcChar8 *target;
f02dd2
+    const FcChar8 *sysroot = FcConfigGetSysRoot (config);
f02dd2
 
f02dd2
-    if (!FcHashTableFind (config->alias_table, dir, (void **)&alias))
f02dd2
-	alias = FcStrdup (dir);
f02dd2
-    if (FcHashTableFind (config->uuid_table, alias, &u))
f02dd2
+    if (sysroot)
f02dd2
+	target = FcStrBuildFilename (sysroot, dir, NULL);
f02dd2
+    else
f02dd2
+	target = FcStrdup (dir);
f02dd2
+    if (FcHashTableFind (config->uuid_table, target, &u))
f02dd2
     {
f02dd2
 	uuid_unparse (u, (char *) cache_base);
f02dd2
 	strcat ((char *) cache_base, "-" FC_ARCHITECTURE FC_CACHE_SUFFIX);
f02dd2
 	FcHashUuidFree (u);
f02dd2
-	FcStrFree (alias);
f02dd2
+	FcStrFree (target);
f02dd2
 	return cache_base;
f02dd2
     }
f02dd2
-    FcStrFree (alias);
f02dd2
+    FcStrFree (target);
f02dd2
     return NULL;
f02dd2
 }
f02dd2
 #endif
f02dd2
@@ -417,6 +439,7 @@ struct _FcCacheSkip {
f02dd2
     FcCache	    *cache;
f02dd2
     FcRef	    ref;
f02dd2
     intptr_t	    size;
f02dd2
+    void	   *allocated;
f02dd2
     dev_t	    cache_dev;
f02dd2
     ino_t	    cache_ino;
f02dd2
     time_t	    cache_mtime;
f02dd2
@@ -542,6 +565,7 @@ FcCacheInsert (FcCache *cache, struct st
f02dd2
 
f02dd2
     s->cache = cache;
f02dd2
     s->size = cache->size;
f02dd2
+    s->allocated = NULL;
f02dd2
     FcRefInit (&s->ref, 1);
f02dd2
     if (cache_stat)
f02dd2
     {
f02dd2
@@ -616,6 +640,7 @@ FcCacheRemoveUnlocked (FcCache *cache)
f02dd2
     FcCacheSkip	    **update[FC_CACHE_MAX_LEVEL];
f02dd2
     FcCacheSkip	    *s, **next;
f02dd2
     int		    i;
f02dd2
+    void            *allocated;
f02dd2
 
f02dd2
     /*
f02dd2
      * Find links along each chain
f02dd2
@@ -633,6 +658,15 @@ FcCacheRemoveUnlocked (FcCache *cache)
f02dd2
 	*update[i] = s->next[i];
f02dd2
     while (fcCacheMaxLevel > 0 && fcCacheChains[fcCacheMaxLevel - 1] == NULL)
f02dd2
 	fcCacheMaxLevel--;
f02dd2
+
f02dd2
+    allocated = s->allocated;
f02dd2
+    while (allocated)
f02dd2
+    {
f02dd2
+	/* First element in allocated chunk is the free list */
f02dd2
+	next = *(void **)allocated;
f02dd2
+	free (allocated);
f02dd2
+	allocated = next;
f02dd2
+    }
f02dd2
     free (s);
f02dd2
 }
f02dd2
 
f02dd2
@@ -702,6 +736,30 @@ FcCacheObjectDereference (void *object)
f02dd2
     unlock_cache ();
f02dd2
 }
f02dd2
 
f02dd2
+void *
f02dd2
+FcCacheAllocate (FcCache *cache, size_t len)
f02dd2
+{
f02dd2
+    FcCacheSkip	*skip;
f02dd2
+    void *allocated = NULL;
f02dd2
+
f02dd2
+    lock_cache ();
f02dd2
+    skip = FcCacheFindByAddrUnlocked (cache);
f02dd2
+    if (skip)
f02dd2
+    {
f02dd2
+      void *chunk = malloc (sizeof (void *) + len);
f02dd2
+      if (chunk)
f02dd2
+      {
f02dd2
+	  /* First element in allocated chunk is the free list */
f02dd2
+	  *(void **)chunk = skip->allocated;
f02dd2
+	  skip->allocated = chunk;
f02dd2
+	  /* Return the rest */
f02dd2
+	  allocated = ((FcChar8 *)chunk) + sizeof (void *);
f02dd2
+      }
f02dd2
+    }
f02dd2
+    unlock_cache ();
f02dd2
+    return allocated;
f02dd2
+}
f02dd2
+
f02dd2
 void
f02dd2
 FcCacheFini (void)
f02dd2
 {
f02dd2
@@ -955,7 +1013,6 @@ FcCache *
f02dd2
 FcDirCacheLoad (const FcChar8 *dir, FcConfig *config, FcChar8 **cache_file)
f02dd2
 {
f02dd2
     FcCache *cache = NULL;
f02dd2
-    const FcChar8 *d;
f02dd2
 
f02dd2
 #ifndef _WIN32
f02dd2
     FcDirCacheReadUUID ((FcChar8 *) dir, config);
f02dd2
@@ -965,10 +1022,6 @@ FcDirCacheLoad (const FcChar8 *dir, FcCo
f02dd2
 			    &cache, cache_file))
f02dd2
 	return NULL;
f02dd2
 
f02dd2
-    d = FcCacheDir (cache);
f02dd2
-    if (FcStrCmp (dir, d))
f02dd2
-	FcHashTableAdd (config->alias_table, (FcChar8 *) d, (FcChar8 *) dir);
f02dd2
-
f02dd2
     return cache;
f02dd2
 }
f02dd2
 
f02dd2
diff -pruN fontconfig-2.13.0.orig/src/fccfg.c fontconfig-2.13.0/src/fccfg.c
f02dd2
--- fontconfig-2.13.0.orig/src/fccfg.c	2018-06-08 18:34:36.546946321 +0900
f02dd2
+++ fontconfig-2.13.0/src/fccfg.c	2018-06-08 18:39:33.079539192 +0900
f02dd2
@@ -144,12 +144,6 @@ FcConfigCreate (void)
f02dd2
 					    FcHashUuidCopy,
f02dd2
 					    (FcDestroyFunc) FcStrFree,
f02dd2
 					    FcHashUuidFree);
f02dd2
-    config->alias_table = FcHashTableCreate ((FcHashFunc) FcStrHashIgnoreCase,
f02dd2
-					     (FcCompareFunc) FcStrCmp,
f02dd2
-					     FcHashStrCopy,
f02dd2
-					     FcHashStrCopy,
f02dd2
-					     (FcDestroyFunc) FcStrFree,
f02dd2
-					     (FcDestroyFunc) FcStrFree);
f02dd2
 
f02dd2
     FcRefInit (&config->ref, 1);
f02dd2
 
f02dd2
@@ -313,7 +307,6 @@ FcConfigDestroy (FcConfig *config)
f02dd2
 	FcStrFree (config->sysRoot);
f02dd2
 
f02dd2
     FcHashTableDestroy (config->uuid_table);
f02dd2
-    FcHashTableDestroy (config->alias_table);
f02dd2
 
f02dd2
     free (config);
f02dd2
 }
f02dd2
@@ -324,11 +317,15 @@ FcConfigDestroy (FcConfig *config)
f02dd2
 
f02dd2
 FcBool
f02dd2
 FcConfigAddCache (FcConfig *config, FcCache *cache,
f02dd2
-		  FcSetName set, FcStrSet *dirSet)
f02dd2
+		  FcSetName set, FcStrSet *dirSet, FcChar8 *forDir)
f02dd2
 {
f02dd2
     FcFontSet	*fs;
f02dd2
     intptr_t	*dirs;
f02dd2
     int		i;
f02dd2
+    FcBool      relocated = FcFalse;
f02dd2
+
f02dd2
+    if (strcmp ((char *)FcCacheDir(cache), (char *)forDir) != 0)
f02dd2
+      relocated = FcTrue;
f02dd2
 
f02dd2
     /*
f02dd2
      * Add fonts
f02dd2
@@ -342,23 +339,43 @@ FcConfigAddCache (FcConfig *config, FcCa
f02dd2
 	{
f02dd2
 	    FcPattern	*font = FcFontSetFont (fs, i);
f02dd2
 	    FcChar8	*font_file;
f02dd2
+	    FcChar8	*relocated_font_file = NULL;
f02dd2
 
f02dd2
-	    /*
f02dd2
-	     * Check to see if font is banned by filename
f02dd2
-	     */
f02dd2
 	    if (FcPatternObjectGetString (font, FC_FILE_OBJECT,
f02dd2
-					  0, &font_file) == FcResultMatch &&
f02dd2
-		!FcConfigAcceptFilename (config, font_file))
f02dd2
+					  0, &font_file) == FcResultMatch)
f02dd2
 	    {
f02dd2
-		continue;
f02dd2
+		if (relocated)
f02dd2
+		  {
f02dd2
+		    FcChar8 *slash = FcStrLastSlash (font_file);
f02dd2
+		    relocated_font_file = FcStrBuildFilename (forDir, slash + 1, NULL);
f02dd2
+		    font_file = relocated_font_file;
f02dd2
+		  }
f02dd2
+
f02dd2
+		/*
f02dd2
+		 * Check to see if font is banned by filename
f02dd2
+		 */
f02dd2
+		if (!FcConfigAcceptFilename (config, font_file))
f02dd2
+		{
f02dd2
+		    free (relocated_font_file);
f02dd2
+		    continue;
f02dd2
+		}
f02dd2
 	    }
f02dd2
-		
f02dd2
+
f02dd2
 	    /*
f02dd2
 	     * Check to see if font is banned by pattern
f02dd2
 	     */
f02dd2
 	    if (!FcConfigAcceptFont (config, font))
f02dd2
+	    {
f02dd2
+		free (relocated_font_file);
f02dd2
 		continue;
f02dd2
-		
f02dd2
+	    }
f02dd2
+
f02dd2
+	    if (relocated_font_file)
f02dd2
+	    {
f02dd2
+	      font = FcPatternCacheRewriteFile (font, cache, relocated_font_file);
f02dd2
+	      free (relocated_font_file);
f02dd2
+	    }
f02dd2
+
f02dd2
 	    if (FcFontSetAdd (config->fonts[set], font))
f02dd2
 		nref++;
f02dd2
 	}
f02dd2
@@ -374,18 +391,14 @@ FcConfigAddCache (FcConfig *config, FcCa
f02dd2
 	for (i = 0; i < cache->dirs_count; i++)
f02dd2
 	{
f02dd2
 	    const FcChar8 *dir = FcCacheSubdir (cache, i);
f02dd2
-	    FcChar8 *alias;
f02dd2
-	    FcChar8 *d = FcStrDirname (dir);
f02dd2
 	    FcChar8 *s = NULL;
f02dd2
 
f02dd2
-	    if (FcHashTableFind (config->alias_table, d, (void **)&alias))
f02dd2
+	    if (relocated)
f02dd2
 	    {
f02dd2
 		FcChar8 *base = FcStrBasename (dir);
f02dd2
-		dir = s = FcStrBuildFilename (alias, base, NULL);
f02dd2
-		FcStrFree (alias);
f02dd2
+		dir = s = FcStrBuildFilename (forDir, base, NULL);
f02dd2
 		FcStrFree (base);
f02dd2
 	    }
f02dd2
-	    FcStrFree (d);
f02dd2
 	    if (FcConfigAcceptFilename (config, dir))
f02dd2
 		FcStrSetAddFilename (dirSet, dir);
f02dd2
 	    if (s)
f02dd2
@@ -413,7 +426,7 @@ FcConfigAddDirList (FcConfig *config, Fc
f02dd2
 	cache = FcDirCacheRead (dir, FcFalse, config);
f02dd2
 	if (!cache)
f02dd2
 	    continue;
f02dd2
-	FcConfigAddCache (config, cache, set, dirSet);
f02dd2
+	FcConfigAddCache (config, cache, set, dirSet, dir);
f02dd2
 	FcDirCacheUnload (cache);
f02dd2
     }
f02dd2
     FcStrListDone (dirlist);
f02dd2
diff -pruN fontconfig-2.13.0.orig/src/fcint.h fontconfig-2.13.0/src/fcint.h
f02dd2
--- fontconfig-2.13.0.orig/src/fcint.h	2018-02-04 19:20:56.000000000 +0900
f02dd2
+++ fontconfig-2.13.0/src/fcint.h	2018-06-08 18:39:33.080539208 +0900
f02dd2
@@ -566,7 +566,6 @@ struct _FcConfig {
f02dd2
     FcStrSet	*availConfigFiles;  /* config files available */
f02dd2
     FcPtrList	*rulesetList;	    /* List of rulesets being installed */
f02dd2
     FcHashTable *uuid_table;	    /* UUID table for cachedirs */
f02dd2
-    FcHashTable *alias_table;	    /* alias table for cachedirs */
f02dd2
 };
f02dd2
 
f02dd2
 typedef struct _FcFileTime {
f02dd2
@@ -617,9 +616,13 @@ FcCacheObjectReference (void *object);
f02dd2
 FcPrivate void
f02dd2
 FcCacheObjectDereference (void *object);
f02dd2
 
f02dd2
+FcPrivate void *
f02dd2
+FcCacheAllocate (FcCache *cache, size_t len);
f02dd2
+
f02dd2
 FcPrivate void
f02dd2
 FcCacheFini (void);
f02dd2
 
f02dd2
+
f02dd2
 FcPrivate void
f02dd2
 FcDirCacheReference (FcCache *cache, int nref);
f02dd2
 
f02dd2
@@ -708,7 +711,7 @@ FcConfigModifiedTime (FcConfig *config);
f02dd2
 
f02dd2
 FcPrivate FcBool
f02dd2
 FcConfigAddCache (FcConfig *config, FcCache *cache,
f02dd2
-		  FcSetName set, FcStrSet *dirSet);
f02dd2
+		  FcSetName set, FcStrSet *dirSet, FcChar8 *forDir);
f02dd2
 
f02dd2
 FcPrivate FcRuleSet *
f02dd2
 FcRuleSetCreate (const FcChar8 *name);
f02dd2
@@ -1150,6 +1153,9 @@ FcPatternAppend (FcPattern *p, FcPattern
f02dd2
 FcPrivate int
f02dd2
 FcPatternPosition (const FcPattern *p, const char *object);
f02dd2
 
f02dd2
+FcPrivate FcPattern *
f02dd2
+FcPatternCacheRewriteFile (const FcPattern *pat, FcCache *cache, const FcChar8 *relocated_font_file);
f02dd2
+
f02dd2
 FcPrivate FcChar32
f02dd2
 FcStringHash (const FcChar8 *s);
f02dd2
 
f02dd2
diff -pruN fontconfig-2.13.0.orig/src/fclist.c fontconfig-2.13.0/src/fclist.c
f02dd2
--- fontconfig-2.13.0.orig/src/fclist.c	2017-12-05 21:44:22.000000000 +0900
f02dd2
+++ fontconfig-2.13.0/src/fclist.c	2018-06-08 18:39:33.049538728 +0900
f02dd2
@@ -448,41 +448,6 @@ FcListAppend (FcListHashTable	*table,
f02dd2
 	e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[o]));
f02dd2
 	if (e)
f02dd2
 	{
f02dd2
-	    if (FcRefIsConst (&font->ref) && !strcmp (os->objects[o], FC_FILE))
f02dd2
-	    {
f02dd2
-		FcChar8 *dir, *alias;
f02dd2
-		FcConfig *config = FcConfigGetCurrent (); /* FIXME: this may need to be exported as API? */
f02dd2
-
f02dd2
-		for (v = FcPatternEltValues (e); v->value.type != FcTypeString; v = FcValueListNext (v));
f02dd2
-		if (!v)
f02dd2
-		    goto bail2;
f02dd2
-		dir = FcStrDirname (FcValueString (&v->value));
f02dd2
-		if (FcHashTableFind (config->alias_table, dir, (void **) &alias))
f02dd2
-		{
f02dd2
-		    FcChar8 *base = FcStrBasename (FcValueString (&v->value));
f02dd2
-		    FcChar8 *s = FcStrBuildFilename (alias, base, NULL);
f02dd2
-		    FcValue vv;
f02dd2
-
f02dd2
-		    FcStrFree (alias);
f02dd2
-		    FcStrFree (base);
f02dd2
-		    vv.type = FcTypeString;
f02dd2
-		    vv.u.s = s;
f02dd2
-		    if (!FcPatternAdd (bucket->pattern,
f02dd2
-				       os->objects[o],
f02dd2
-				       FcValueCanonicalize (&vv),
f02dd2
-				       FcTrue))
f02dd2
-		    {
f02dd2
-			FcStrFree (s);
f02dd2
-			FcStrFree (dir);
f02dd2
-			goto bail2;
f02dd2
-		    }
f02dd2
-		    FcStrFree (s);
f02dd2
-		    FcStrFree (dir);
f02dd2
-		    goto bail3;
f02dd2
-		}
f02dd2
-		else
f02dd2
-		    FcStrFree (dir);
f02dd2
-	    }
f02dd2
 	    for (v = FcPatternEltValues(e), idx = 0; v;
f02dd2
 		 v = FcValueListNext(v), ++idx)
f02dd2
 	    {
f02dd2
@@ -491,7 +456,6 @@ FcListAppend (FcListHashTable	*table,
f02dd2
 				   FcValueCanonicalize(&v->value), defidx != idx))
f02dd2
 		    goto bail2;
f02dd2
 	    }
f02dd2
-	  bail3:;
f02dd2
 	}
f02dd2
     }
f02dd2
     *prev = bucket;
f02dd2
diff -pruN fontconfig-2.13.0.orig/src/fcmatch.c fontconfig-2.13.0/src/fcmatch.c
f02dd2
--- fontconfig-2.13.0.orig/src/fcmatch.c	2017-12-23 14:06:22.000000000 +0900
f02dd2
+++ fontconfig-2.13.0/src/fcmatch.c	2018-06-08 18:39:33.050538743 +0900
f02dd2
@@ -682,43 +682,9 @@ FcFontRenderPrepare (FcConfig	    *confi
f02dd2
 	}
f02dd2
 	else
f02dd2
 	{
f02dd2
-	    if (FcRefIsConst (&font->ref) && fe->object == FC_FILE_OBJECT)
f02dd2
-	    {
f02dd2
-		FcValueListPtr l = FcPatternEltValues (fe);
f02dd2
-		FcChar8 *dir, *alias;
f02dd2
-
f02dd2
-		while (l->value.type != FcTypeString)
f02dd2
-		    l = FcValueListNext (l);
f02dd2
-		if (!l)
f02dd2
-		    goto bail0;
f02dd2
-		dir = FcStrDirname (FcValueString (&l->value));
f02dd2
-		if (!config)
f02dd2
-		    config = FcConfigGetCurrent ();
f02dd2
-		if (config && FcHashTableFind (config->alias_table, dir, (void **) &alias))
f02dd2
-		{
f02dd2
-		    FcChar8 *base = FcStrBasename (FcValueString (&l->value));
f02dd2
-		    FcChar8 *s = FcStrBuildFilename (alias, base, NULL);
f02dd2
-		    FcValue v;
f02dd2
-
f02dd2
-		    FcStrFree (alias);
f02dd2
-		    FcStrFree (base);
f02dd2
-		    v.type = FcTypeString;
f02dd2
-		    v.u.s = s;
f02dd2
-		    FcPatternObjectAddWithBinding (new, fe->object,
f02dd2
-						   FcValueCanonicalize (&v),
f02dd2
-						   l->binding,
f02dd2
-						   FcTrue);
f02dd2
-		    FcStrFree (s);
f02dd2
-		    FcStrFree (dir);
f02dd2
-		    goto bail0;
f02dd2
-		}
f02dd2
-		else
f02dd2
-		    FcStrFree (dir);
f02dd2
-	    }
f02dd2
 	    FcPatternObjectListAdd (new, fe->object,
f02dd2
 				    FcValueListDuplicate (FcPatternEltValues (fe)),
f02dd2
 				    FcTrue);
f02dd2
-	  bail0:;
f02dd2
 	}
f02dd2
     }
f02dd2
     for (i = 0; i < pat->num; i++)
f02dd2
diff -pruN fontconfig-2.13.0.orig/src/fcpat.c fontconfig-2.13.0/src/fcpat.c
f02dd2
--- fontconfig-2.13.0.orig/src/fcpat.c	2017-12-20 19:20:15.000000000 +0900
f02dd2
+++ fontconfig-2.13.0/src/fcpat.c	2018-06-08 18:39:33.050538743 +0900
f02dd2
@@ -373,6 +373,71 @@ FcValueListHash (FcValueListPtr l)
f02dd2
     return hash;
f02dd2
 }
f02dd2
 
f02dd2
+static void *
f02dd2
+FcPatternGetCacheObject (FcPattern *p)
f02dd2
+{
f02dd2
+  /* We use a value to find the cache, instead of the FcPattern object
f02dd2
+   * because the pattern itself may be a cache allocation if we rewrote the path,
f02dd2
+   * so the p may not be in the cached region. */
f02dd2
+  return FcPatternEltValues(&FcPatternElts (p)[0]);
f02dd2
+}
f02dd2
+
f02dd2
+FcPattern *
f02dd2
+FcPatternCacheRewriteFile (const FcPattern *p,
f02dd2
+                           FcCache *cache,
f02dd2
+                           const FcChar8 *relocated_font_file)
f02dd2
+{
f02dd2
+    FcPatternElt *elts = FcPatternElts (p);
f02dd2
+    size_t i,j;
f02dd2
+    FcChar8 *data;
f02dd2
+    FcPattern *new_p;
f02dd2
+    FcPatternElt *new_elts;
f02dd2
+    FcValueList *new_value_list;
f02dd2
+    size_t new_path_len = strlen ((char *)relocated_font_file);
f02dd2
+    FcChar8 *new_path;
f02dd2
+
f02dd2
+    /* Allocate space for the patter, the PatternElt headers and
f02dd2
+     * the FC_FILE FcValueList and path that will be freed with the
f02dd2
+     * cache */
f02dd2
+    data = FcCacheAllocate (cache,
f02dd2
+			    sizeof (FcPattern) +
f02dd2
+			    p->num * sizeof (FcPatternElt) +
f02dd2
+			    sizeof (FcValueList) +
f02dd2
+			    new_path_len + 1);
f02dd2
+
f02dd2
+    new_p = (FcPattern *)data;
f02dd2
+    data += sizeof (FcPattern);
f02dd2
+    new_elts = (FcPatternElt *)(data);
f02dd2
+    data += p->num * sizeof (FcPatternElt);
f02dd2
+    new_value_list = (FcValueList *)data;
f02dd2
+    data += sizeof (FcValueList);
f02dd2
+    new_path = data;
f02dd2
+
f02dd2
+    *new_p = *p;
f02dd2
+    new_p->elts_offset = FcPtrToOffset (new_p, new_elts);
f02dd2
+
f02dd2
+    /* Copy all but the FILE values from the cache */
f02dd2
+    for (i = 0, j = 0; i < p->num; i++)
f02dd2
+    {
f02dd2
+	FcPatternElt *elt = &elts[i];
f02dd2
+	new_elts[j].object = elt->object;
f02dd2
+	if (elt->object != FC_FILE_OBJECT)
f02dd2
+	    new_elts[j++].values = FcPatternEltValues(elt);
f02dd2
+	else
f02dd2
+	    new_elts[j++].values = new_value_list;
f02dd2
+    }
f02dd2
+
f02dd2
+    new_value_list->next = NULL;
f02dd2
+    new_value_list->value.type = FcTypeString;
f02dd2
+    new_value_list->value.u.s = new_path;
f02dd2
+    new_value_list->binding = FcValueBindingWeak;
f02dd2
+
f02dd2
+    /* Add rewritten path at the end */
f02dd2
+    strcpy ((char *)new_path, (char *)relocated_font_file);
f02dd2
+
f02dd2
+    return new_p;
f02dd2
+}
f02dd2
+
f02dd2
 void
f02dd2
 FcPatternDestroy (FcPattern *p)
f02dd2
 {
f02dd2
@@ -384,10 +449,10 @@ FcPatternDestroy (FcPattern *p)
f02dd2
 
f02dd2
     if (FcRefIsConst (&p->ref))
f02dd2
     {
f02dd2
-	FcCacheObjectDereference (p);
f02dd2
+	FcCacheObjectDereference (FcPatternGetCacheObject(p));
f02dd2
 	return;
f02dd2
     }
f02dd2
-	
f02dd2
+
f02dd2
     if (FcRefDec (&p->ref) != 1)
f02dd2
 	return;
f02dd2
 
f02dd2
@@ -1155,7 +1220,7 @@ FcPatternReference (FcPattern *p)
f02dd2
     if (!FcRefIsConst (&p->ref))
f02dd2
 	FcRefInc (&p->ref);
f02dd2
     else
f02dd2
-	FcCacheObjectReference (p);
f02dd2
+	FcCacheObjectReference (FcPatternGetCacheObject(p));
f02dd2
 }
f02dd2
 
f02dd2
 FcPattern *
f02dd2
diff -pruN fontconfig-2.13.0.orig/test/run-test.sh fontconfig-2.13.0/test/run-test.sh
f02dd2
--- fontconfig-2.13.0.orig/test/run-test.sh	2017-12-18 21:25:18.000000000 +0900
f02dd2
+++ fontconfig-2.13.0/test/run-test.sh	2018-06-08 18:39:33.031538449 +0900
f02dd2
@@ -202,4 +202,29 @@ fi
f02dd2
 rm -rf $TESTTMPDIR out1 out2 xxx bind-fonts.conf
f02dd2
 fi
f02dd2
 
f02dd2
+dotest "sysroot option"
f02dd2
+prep
f02dd2
+mkdir -p $MyPWD/sysroot/$FONTDIR
f02dd2
+mkdir -p $MyPWD/sysroot/$CACHEDIR
f02dd2
+cp $FONT1 $MyPWD/sysroot/$FONTDIR
f02dd2
+cp $MyPWD/fonts.conf $MyPWD/sysroot/$MyPWD/fonts.conf
f02dd2
+$FCCACHE -y $MyPWD/sysroot
f02dd2
+stat $MyPWD/sysroot/$FONTDIR/.uuid
f02dd2
+if test $? != 0; then
f02dd2
+  echo "*** Test failed: $TEST"
f02dd2
+  exit 1
f02dd2
+fi
f02dd2
+
f02dd2
+dotest "creating uuid-based cache file on sysroot"
f02dd2
+uuid=`cat $MyPWD/sysroot/$FONTDIR/.uuid`
f02dd2
+ls $MyPWD/sysroot/$CACHEDIR/$uuid*
f02dd2
+if [ $? != 0 ]; then
f02dd2
+  echo "*** Test failed: $TEST"
f02dd2
+  echo "No cache for $uuid"
f02dd2
+  ls $MyPWD/sysroot/$CACHEDIR
f02dd2
+  exit 1
f02dd2
+fi
f02dd2
+
f02dd2
+rm -rf $MyPWD/sysroot
f02dd2
+
f02dd2
 rm -rf $FONTDIR $CACHEFILE $CACHEDIR $FONTCONFIG_FILE out