]> git.wh0rd.org - fontconfig.git/commitdiff
[fcformat] Add value-count syntax
authorBehdad Esfahbod <behdad@behdad.org>
Tue, 10 Feb 2009 10:05:53 +0000 (05:05 -0500)
committerBehdad Esfahbod <behdad@behdad.org>
Sun, 15 Feb 2009 21:40:29 +0000 (13:40 -0800)
The format '%{#family}' expands to the number of values for the element
'family', or '0' if no such element exists in the pattern.

src/fcformat.c

index 238f8d2d93afaf98f54dd7c331faa2e46662f852..6a463fdb3eb4bd5aa410b01973f4f4b4fbce2b12 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2008 Red Hat, Inc.
+ * Copyright © 2008,2009 Red Hat, Inc.
  *
  * Red Hat Author(s): Behdad Esfahbod
  *
@@ -31,7 +31,6 @@
 /*
  * Some ideas for future syntax extensions:
  *
- * - number of values for element using '%{#elt}'
  * - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
  * - allow indexing simple tags using '%{elt[idx]}'
  * - conditional/filtering/deletion on binding (using '(w)'/'(s)' notation)
@@ -260,45 +259,6 @@ maybe_skip_subexpr (FcFormatContext *c)
           FcTrue;
 }
 
-static FcBool
-interpret_simple (FcFormatContext *c,
-                 FcPattern       *pat,
-                 FcStrBuf        *buf)
-{
-    FcPatternElt *e;
-    FcBool        add_colon = FcFalse;
-    FcBool        add_elt_name = FcFalse;
-
-    if (consume_char (c, ':'))
-       add_colon = FcTrue;
-
-    if (!read_word (c))
-       return FcFalse;
-
-    if (consume_char (c, '='))
-       add_elt_name = FcTrue;
-
-    e = FcPatternObjectFindElt (pat,
-                               FcObjectFromName ((const char *) c->word));
-    if (e)
-    {
-       FcValueListPtr l;
-
-       if (add_colon)
-           FcStrBufChar (buf, ':');
-       if (add_elt_name)
-       {
-           FcStrBufString (buf, c->word);
-           FcStrBufChar (buf, '=');
-       }
-
-       l = FcPatternEltValues(e);
-       FcNameUnparseValueList (buf, l, '\0');
-    }
-
-    return FcTrue;
-}
-
 static FcBool
 interpret_filter (FcFormatContext *c,
                  FcPattern       *pat,
@@ -415,6 +375,79 @@ interpret_cond (FcFormatContext *c,
     return FcTrue;
 }
 
+static FcBool
+interpret_count (FcFormatContext *c,
+                FcPattern       *pat,
+                FcStrBuf        *buf)
+{
+    int count;
+    FcPatternElt *e;
+    FcChar8 buf_static[64];
+
+    if (!expect_char (c, '#'))
+       return FcFalse;
+
+    if (!read_word (c))
+       return FcFalse;
+
+    count = 0;
+    e = FcPatternObjectFindElt (pat,
+                               FcObjectFromName ((const char *) c->word));
+    if (e)
+    {
+       FcValueListPtr l;
+       count++;
+       for (l = FcPatternEltValues(e);
+            l->next;
+            l = l->next)
+           count++;
+    }
+
+    snprintf (buf_static, sizeof (buf_static), "%d", count);
+    FcStrBufString (buf, buf_static);
+
+    return FcTrue;
+}
+
+static FcBool
+interpret_simple (FcFormatContext *c,
+                 FcPattern       *pat,
+                 FcStrBuf        *buf)
+{
+    FcPatternElt *e;
+    FcBool        add_colon = FcFalse;
+    FcBool        add_elt_name = FcFalse;
+
+    if (consume_char (c, ':'))
+       add_colon = FcTrue;
+
+    if (!read_word (c))
+       return FcFalse;
+
+    if (consume_char (c, '='))
+       add_elt_name = FcTrue;
+
+    e = FcPatternObjectFindElt (pat,
+                               FcObjectFromName ((const char *) c->word));
+    if (e)
+    {
+       FcValueListPtr l;
+
+       if (add_colon)
+           FcStrBufChar (buf, ':');
+       if (add_elt_name)
+       {
+           FcStrBufString (buf, c->word);
+           FcStrBufChar (buf, '=');
+       }
+
+       l = FcPatternEltValues(e);
+       FcNameUnparseValueList (buf, l, '\0');
+    }
+
+    return FcTrue;
+}
+
 static FcChar8 *
 cescape (const FcChar8 *str)
 {
@@ -593,6 +626,7 @@ interpret_percent (FcFormatContext *c,
     case '+': ret = interpret_filter  (c, pat, buf); break;
     case '-': ret = interpret_delete  (c, pat, buf); break;
     case '?': ret = interpret_cond    (c, pat, buf); break;
+    case '#': ret = interpret_count   (c, pat, buf); break;
     default:  ret = interpret_simple  (c, pat, buf); break;
     }