]> git.wh0rd.org - fontconfig.git/commitdiff
[fcformat] Add support for width modifiers
authorBehdad Esfahbod <behdad@behdad.org>
Mon, 9 Feb 2009 23:18:59 +0000 (18:18 -0500)
committerBehdad Esfahbod <behdad@behdad.org>
Sun, 15 Feb 2009 21:40:25 +0000 (13:40 -0800)
One can do '%30{family}' for example.  Or '%-30{family}' for the
left-aligned version.

doc/fcpattern.fncs
src/fcformat.c

index c89706a424b87e738fec977a59df0b8501a28d8e..22a89ad689fae16b8c7160b6fc9a0983872b5a0e 100644 (file)
@@ -405,11 +405,13 @@ which should be freed by the caller using free().
 
 Converts the given pattern into text format described by the format specifier.
 The format specifier is similar to a C style printf string, which the
-printf(2) man page provides a good introduction to. However, as RPM already
-knows the type of data that is being printed, you must omit the type
+printf(2) man page provides a good introduction to. However, as fontconfig
+already knows the type of data that is being printed, you must omit the type
 specifier. In its place put the element name you wish to print enclosed in
 curly braces ({}). For example, to print the family name and style the
 pattern, use the format "%{family} %{style}\n".
+There can be an option width specifier after the percent sign and before
+the opening brace.  The width modifier acts similar to those in printf.
 The return value refers to newly allocated memory which should be freed by the
 caller using free().
 @@
index 80c99c3d7c48da97fa3349ec8667248472b7c455..a780f56db36d27dc807887cddff69774ce7b2d58 100644 (file)
@@ -49,6 +49,13 @@ interpret_percent (FcPattern *pat,
                   FcStrBuf *buf,
                   const FcChar8 *format)
 {
+    int width, before;
+
+    /* parse an optional width specifier */
+    width = strtol (format, (char **) &format, 10);
+
+    before = buf->len;
+
     switch (*format) {
     case '{':
     {
@@ -61,7 +68,7 @@ interpret_percent (FcPattern *pat,
        if (!p)
        {
            message ("Pattern format missing closing brace");
-           return format;
+           break;
        }
        /* extract the element name */
        memcpy (scratch1, format, p - format);
@@ -76,13 +83,46 @@ interpret_percent (FcPattern *pat,
        }
 
        p++; /* skip over '}' */
-       return p;
+       format = p;
+       break;
     }
     default:
        message ("Pattern format has invalid character after '%%' at %d",
                 format - format_orig);
-       return format;
+       break;
+    }
+
+    /* align to width */
+    if (!buf->failed)
+    {
+       int after, len;
+
+       after = buf->len;
+
+       len = after - before;
+
+       if (len < -width)
+       {
+           /* left align */
+           while (len++ < -width)
+               FcStrBufChar (buf, ' ');
+       }
+       else if (len < width)
+       {
+           /* right align */
+           while (len++ < width)
+               FcStrBufChar (buf, ' ');
+           len = after - before;
+           memmove (buf->buf + buf->len - len,
+                    buf->buf + buf->len - width,
+                    len);
+           memset (buf->buf + buf->len - width,
+                   ' ',
+                   width - len);
+       }
     }
+
+    return format;
 }
 
 static char escaped_char(const char ch)