]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcformat.c
[fcformat] Support 'default value' for simple tags
[fontconfig.git] / src / fcformat.c
index 334eee6a71bc09ab27cde3a62d9d9eb5649e4f9e..f91b6e0fb695e18643a6d5ab4b015db32b20ad27 100644 (file)
  *
  * - array enumeration using '%{[]family,familylang{expr}|decorator}'
  * - langset enumeration using same syntax as array enumeration
- * - allow indexing simple tags using '%{elt[idx]}'
  * - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
  * - conditional/filtering/deletion on binding (using '(w)'/'(s)'/'(=)' notation)
  */
 
 
-/* fc-match needs '<unknown filename>', etc handling, as well as printing
- * printing first value only. */
-#define FCMATCH_FORMAT "%{file|basename}: \"%{family}\" \"%{style}\""
+#define FCMATCH_FORMAT "%{file:-<unknown filename>|basename}: \"%{family[0]:-<unknown family>}\" \"%{style[0]:-<unknown style>}\""
 #define FCLIST_FORMAT  "%{?file{%{file}: }}%{=unparse}"
 
 
@@ -248,7 +245,7 @@ interpret_builtin (FcFormatContext *c,
     else if (0 == strcmp ((const char *) c->word, name))\
        do { new_str = func (pat); ret = FcTrue; } while (0)
     BUILTIN ("unparse",  FcNameUnparse);
- /* BUILTIN ("verbose",  FcPatternPrint); */
+ /* BUILTIN ("verbose",  FcPatternPrint); XXX */
 #undef BUILTIN
     else
        ret = FcFalse;
@@ -543,6 +540,8 @@ interpret_simple (FcFormatContext *c,
     FcPatternElt *e;
     FcBool        add_colon = FcFalse;
     FcBool        add_elt_name = FcFalse;
+    int           idx;
+    FcChar8      *else_string;
 
     if (consume_char (c, ':'))
        add_colon = FcTrue;
@@ -550,9 +549,41 @@ interpret_simple (FcFormatContext *c,
     if (!read_word (c))
        return FcFalse;
 
+    idx = -1;
+    if (consume_char (c, '['))
+    {
+       idx = strtol ((const char *) c->format, (char **) &c->format, 10);
+       if (idx < 0)
+       {
+           message ("expected non-negative number at %d",
+                    c->format-1 - c->format_orig + 1);
+           return FcFalse;
+       }
+       expect_char (c, ']');
+    }
+
     if (consume_char (c, '='))
        add_elt_name = FcTrue;
 
+    /* modifiers */
+    else_string = NULL;
+    if (consume_char (c, ':'))
+    {
+       FcChar8 *orig;
+       /* divert the c->word for now */
+       orig = c->word;
+       c->word = c->word + strlen ((const char *) c->word) + 1;
+       /* for now we just support 'default value' */
+       if (!expect_char (c, '-') ||
+           !read_chars (c, '\0'))
+       {
+           c->word = orig;
+           return FcFalse;
+       }
+       else_string = c->word;
+       c->word = orig;
+    }
+
     e = FcPatternObjectFindElt (pat,
                                FcObjectFromName ((const char *) c->word));
     if (e)
@@ -568,7 +599,31 @@ interpret_simple (FcFormatContext *c,
        }
 
        l = FcPatternEltValues(e);
-       FcNameUnparseValueList (buf, l, '\0');
+
+       if (idx != -1)
+       {
+           while (l && idx > 0)
+           {
+               l = FcValueListNext(l);
+               idx--;
+           }
+           if (l && idx == 0)
+           {
+               if (!FcNameUnparseValue (buf, &l->value, '\0'))
+                   return FcFalse;
+           }
+           else goto notfound;
+        }
+       else
+       {
+           FcNameUnparseValueList (buf, l, '\0');
+       }
+    }
+    else
+notfound:
+    {
+       if (else_string)
+           printf ("%s", else_string);
     }
 
     return FcTrue;
@@ -717,7 +772,7 @@ translate_chars (FcFormatContext *c,
     from_len = strlen (from);
     to = from + from_len + 1;
 
-    /* hack: we temporarily diverge c->word */
+    /* hack: we temporarily divert c->word */
     c->word = (FcChar8 *) to;
     if (!read_chars (c, ')'))
     {