Set font-variations settings for standard axes in variable fonts

This is the last piece of the puzzle for variable-font support in
fontconfig.  This takes care of automatically setting the variation
settings when user requests a weight / width / size that has variation
in the matched font.
diff --git a/src/fcmatch.c b/src/fcmatch.c
index 04301f9..4656417 100644
--- a/src/fcmatch.c
+++ b/src/fcmatch.c
@@ -531,10 +531,22 @@
     FcPatternElt    *fe, *pe;
     FcValue	    v;
     FcResult	    result;
+    FcBool	    variable = FcFalse;
+    FcStrBuf        variations;
 
     assert (pat != NULL);
     assert (font != NULL);
 
+    FcPatternObjectGetBool (font, FC_VARIABLE_OBJECT, 0, &variable);
+    assert (variable != FcDontCare);
+    if (variable)
+    {
+	FcChar8 *vars = NULL;
+	FcStrBufInit (&variations, NULL, 0);
+	if (FcPatternObjectGetString (pat, FC_FONT_VARIATIONS_OBJECT, 0, &vars) == FcResultMatch)
+	    FcStrBufString (&variations, vars);
+    }
+
     new = FcPatternCreate ();
     if (!new)
 	return NULL;
@@ -639,6 +651,39 @@
 		return NULL;
 	    }
 	    FcPatternObjectAdd (new, fe->object, v, FcFalse);
+
+	    /* Set font-variations settings for standard axes in variable fonts. */
+	    if (variable &&
+		FcPatternEltValues(fe)->value.type == FcTypeRange &&
+		(fe->object == FC_WEIGHT_OBJECT ||
+		 fe->object == FC_WIDTH_OBJECT ||
+		 fe->object == FC_SIZE_OBJECT))
+	    {
+		double num;
+		FcChar8 temp[128];
+		const char *tag = "    ";
+		assert (v.type == FcTypeDouble);
+		num = v.u.d;
+		if (variations.len)
+		    FcStrBufChar (&variations, ',');
+		switch (fe->object)
+		{
+		    case FC_WEIGHT_OBJECT:
+			tag = "wght";
+			num = FcWeightToOpenType (num);
+			break;
+
+		    case FC_WIDTH_OBJECT:
+			tag = "wdth";
+			break;
+
+		    case FC_SIZE_OBJECT:
+			tag = "opsz";
+			break;
+		}
+		sprintf ((char *) temp, "%4s=%g", tag, num);
+		FcStrBufString (&variations, temp);
+	    }
 	}
 	else
 	{
@@ -652,8 +697,6 @@
 		if (!l)
 		    goto bail0;
 		dir = FcStrDirname (FcValueString (&l->value));
-		if (!config)
-			config = FcConfigGetCurrent ();
 		if (config && FcHashTableFind (config->alias_table, dir, (void **) &alias))
 		{
 		    FcChar8 *base = FcStrBasename (FcValueString (&l->value));
@@ -696,6 +739,13 @@
 	}
     }
 
+    if (variable)
+    {
+      FcPatternObjectDel (new, FC_FONT_VARIATIONS_OBJECT);
+      FcPatternObjectAddString (new, FC_FONT_VARIATIONS_OBJECT, FcStrBufDoneStatic (&variations));
+      FcStrBufDestroy (&variations);
+    }
+
     FcConfigSubstituteWithPat (config, new, pat, FcMatchFont);
     return new;
 }
diff --git a/src/fcpat.c b/src/fcpat.c
index dd1307d..e624aea 100644
--- a/src/fcpat.c
+++ b/src/fcpat.c
@@ -1021,12 +1021,12 @@
 
 
 FcResult
-FcPatternGetBool(const FcPattern *p, const char *object, int id, FcBool *b)
+FcPatternObjectGetBool (const FcPattern *p, FcObject object, int id, FcBool *b)
 {
     FcValue	v;
     FcResult	r;
 
-    r = FcPatternGet (p, object, id, &v);
+    r = FcPatternObjectGet (p, object, id, &v);
     if (r != FcResultMatch)
 	return r;
     if (v.type != FcTypeBool)
@@ -1036,6 +1036,12 @@
 }
 
 FcResult
+FcPatternGetBool(const FcPattern *p, const char *object, int id, FcBool *b)
+{
+    return FcPatternObjectGetBool (p, FcObjectFromName (object), id, b);
+}
+
+FcResult
 FcPatternGetCharSet(const FcPattern *p, const char *object, int id, FcCharSet **c)
 {
     FcValue	v;