[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:
parent
967267556c
commit
c493c3b770
|
@ -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().
|
||||
@@
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue