[fcformat] Add support for width modifiers

One can do '%30{family}' for example.  Or '%-30{family}' for the
left-aligned version.
This commit is contained in:
Behdad Esfahbod 2009-02-09 18:18:59 -05:00
parent 967267556c
commit c493c3b770
2 changed files with 47 additions and 5 deletions

View 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. 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 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 printf(2) man page provides a good introduction to. However, as fontconfig
knows the type of data that is being printed, you must omit the type 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 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 curly braces ({}). For example, to print the family name and style the
pattern, use the format "%{family} %{style}\n". 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 The return value refers to newly allocated memory which should be freed by the
caller using free(). caller using free().
@@ @@

View File

@ -49,6 +49,13 @@ interpret_percent (FcPattern *pat,
FcStrBuf *buf, FcStrBuf *buf,
const FcChar8 *format) const FcChar8 *format)
{ {
int width, before;
/* parse an optional width specifier */
width = strtol (format, (char **) &format, 10);
before = buf->len;
switch (*format) { switch (*format) {
case '{': case '{':
{ {
@ -61,7 +68,7 @@ interpret_percent (FcPattern *pat,
if (!p) if (!p)
{ {
message ("Pattern format missing closing brace"); message ("Pattern format missing closing brace");
return format; break;
} }
/* extract the element name */ /* extract the element name */
memcpy (scratch1, format, p - format); memcpy (scratch1, format, p - format);
@ -76,13 +83,46 @@ interpret_percent (FcPattern *pat,
} }
p++; /* skip over '}' */ p++; /* skip over '}' */
return p; format = p;
break;
} }
default: default:
message ("Pattern format has invalid character after '%%' at %d", message ("Pattern format has invalid character after '%%' at %d",
format - format_orig); 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) static char escaped_char(const char ch)