From c493c3b770ab12ab1c61a4fb10419c490d2b5ba6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 9 Feb 2009 18:18:59 -0500 Subject: [PATCH] [fcformat] Add support for width modifiers One can do '%30{family}' for example. Or '%-30{family}' for the left-aligned version. --- doc/fcpattern.fncs | 6 ++++-- src/fcformat.c | 46 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/doc/fcpattern.fncs b/doc/fcpattern.fncs index c89706a..22a89ad 100644 --- a/doc/fcpattern.fncs +++ b/doc/fcpattern.fncs @@ -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(). @@ diff --git a/src/fcformat.c b/src/fcformat.c index 80c99c3..a780f56 100644 --- a/src/fcformat.c +++ b/src/fcformat.c @@ -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) -- 2.39.2