2008-12-30 02:00:26 +01:00
|
|
|
/*
|
2009-02-10 11:05:53 +01:00
|
|
|
* Copyright © 2008,2009 Red Hat, Inc.
|
2008-12-30 02:00:26 +01:00
|
|
|
*
|
|
|
|
* Red Hat Author(s): Behdad Esfahbod
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
|
|
* documentation for any purpose is hereby granted without fee, provided that
|
|
|
|
* the above copyright notice appear in all copies and that both that
|
|
|
|
* copyright notice and this permission notice appear in supporting
|
|
|
|
* documentation, and that the name of Keith Packard not be used in
|
|
|
|
* advertising or publicity pertaining to distribution of the software without
|
|
|
|
* specific, written prior permission. Keith Packard makes no
|
|
|
|
* representations about the suitability of this software for any purpose. It
|
|
|
|
* is provided "as is" without express or implied warranty.
|
|
|
|
*
|
|
|
|
* KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
|
|
* EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
|
|
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
|
|
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fcint.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
/*
|
|
|
|
* Some ideas for future syntax extensions:
|
|
|
|
*
|
|
|
|
* - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
|
|
|
|
* - allow indexing simple tags using '%{elt[idx]}'
|
|
|
|
* - conditional/filtering/deletion on binding (using '(w)'/'(s)' notation)
|
|
|
|
*/
|
|
|
|
|
2008-12-30 02:00:26 +01:00
|
|
|
static void
|
|
|
|
message (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start (args, fmt);
|
2009-02-10 05:08:08 +01:00
|
|
|
fprintf (stderr, "Fontconfig: Pattern format error: ");
|
2008-12-30 02:00:26 +01:00
|
|
|
vfprintf (stderr, fmt, args);
|
2009-02-10 02:49:45 +01:00
|
|
|
fprintf (stderr, ".\n");
|
2008-12-30 02:00:26 +01:00
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-10 01:13:07 +01:00
|
|
|
typedef struct _FcFormatContext
|
2008-12-30 02:00:26 +01:00
|
|
|
{
|
2009-02-10 01:13:07 +01:00
|
|
|
const FcChar8 *format_orig;
|
|
|
|
const FcChar8 *format;
|
|
|
|
int format_len;
|
2009-02-10 09:38:22 +01:00
|
|
|
FcChar8 *word;
|
2009-02-10 01:13:07 +01:00
|
|
|
} FcFormatContext;
|
2009-02-10 00:18:59 +01:00
|
|
|
|
2009-02-10 05:08:08 +01:00
|
|
|
static FcBool
|
2009-02-10 01:13:07 +01:00
|
|
|
FcFormatContextInit (FcFormatContext *c,
|
|
|
|
const FcChar8 *format)
|
|
|
|
{
|
|
|
|
c->format_orig = c->format = format;
|
|
|
|
c->format_len = strlen ((const char *) format);
|
2009-02-10 09:38:22 +01:00
|
|
|
c->word = malloc (c->format_len + 1);
|
2009-02-10 05:08:08 +01:00
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
return c->word != NULL;
|
2009-02-10 01:13:07 +01:00
|
|
|
}
|
2009-02-10 00:18:59 +01:00
|
|
|
|
2009-02-10 01:13:07 +01:00
|
|
|
static void
|
|
|
|
FcFormatContextDone (FcFormatContext *c)
|
|
|
|
{
|
|
|
|
if (c)
|
|
|
|
{
|
2009-02-10 09:38:22 +01:00
|
|
|
free (c->word);
|
2009-02-10 01:13:07 +01:00
|
|
|
}
|
|
|
|
}
|
2009-02-10 00:18:59 +01:00
|
|
|
|
2009-02-10 01:13:07 +01:00
|
|
|
static FcBool
|
|
|
|
consume_char (FcFormatContext *c,
|
|
|
|
FcChar8 term)
|
|
|
|
{
|
|
|
|
if (*c->format != term)
|
2009-02-10 02:49:45 +01:00
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
c->format++;
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
expect_char (FcFormatContext *c,
|
|
|
|
FcChar8 term)
|
|
|
|
{
|
|
|
|
FcBool res = consume_char (c, term);
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
if (c->format == c->format_orig + c->format_len)
|
|
|
|
message ("format ended while expecting '%c'",
|
|
|
|
term);
|
|
|
|
else
|
|
|
|
message ("expected '%c' at %d",
|
|
|
|
term, c->format - c->format_orig + 1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
FcCharIsPunct (const FcChar8 c)
|
|
|
|
{
|
|
|
|
if (c < '0')
|
|
|
|
return FcTrue;
|
|
|
|
if (c <= '9')
|
|
|
|
return FcFalse;
|
|
|
|
if (c < 'A')
|
|
|
|
return FcTrue;
|
|
|
|
if (c <= 'Z')
|
|
|
|
return FcFalse;
|
|
|
|
if (c < 'a')
|
|
|
|
return FcTrue;
|
|
|
|
if (c <= 'z')
|
|
|
|
return FcFalse;
|
|
|
|
if (c <= '~')
|
|
|
|
return FcTrue;
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
2009-02-10 09:38:22 +01:00
|
|
|
read_word (FcFormatContext *c)
|
2009-02-10 02:49:45 +01:00
|
|
|
{
|
|
|
|
FcChar8 *p;
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
p = c->word;
|
2009-02-10 02:49:45 +01:00
|
|
|
|
|
|
|
while (*c->format)
|
|
|
|
{
|
|
|
|
if (*c->format == '\\')
|
|
|
|
{
|
|
|
|
c->format++;
|
|
|
|
if (*c->format)
|
|
|
|
c->format++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (FcCharIsPunct (*c->format))
|
|
|
|
break;
|
|
|
|
|
|
|
|
*p++ = *c->format++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
if (p == c->word)
|
2008-12-30 02:00:26 +01:00
|
|
|
{
|
2009-02-10 02:49:45 +01:00
|
|
|
message ("expected element name at %d",
|
|
|
|
c->format - c->format_orig + 1);
|
2009-02-10 01:13:07 +01:00
|
|
|
return FcFalse;
|
|
|
|
}
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-10 01:13:07 +01:00
|
|
|
return FcTrue;
|
|
|
|
}
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-10 05:08:08 +01:00
|
|
|
static FcBool
|
2009-02-10 06:15:08 +01:00
|
|
|
interpret_expr (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf,
|
|
|
|
FcChar8 term);
|
2009-02-10 02:49:45 +01:00
|
|
|
|
2009-02-10 05:08:08 +01:00
|
|
|
static FcBool
|
|
|
|
interpret_subexpr (FcFormatContext *c,
|
2009-02-10 01:13:07 +01:00
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
2009-02-10 05:08:08 +01:00
|
|
|
return expect_char (c, '{') &&
|
2009-02-10 06:15:08 +01:00
|
|
|
interpret_expr (c, pat, buf, '}') &&
|
2009-02-10 05:08:08 +01:00
|
|
|
expect_char (c, '}');
|
|
|
|
}
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-10 06:15:08 +01:00
|
|
|
static FcBool
|
|
|
|
maybe_interpret_subexpr (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
|
|
|
return (*c->format == '{') ?
|
|
|
|
interpret_subexpr (c, pat, buf) :
|
|
|
|
FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
skip_subexpr (FcFormatContext *c);
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
skip_percent (FcFormatContext *c)
|
|
|
|
{
|
2009-02-10 09:38:22 +01:00
|
|
|
int width;
|
|
|
|
|
2009-02-10 06:15:08 +01:00
|
|
|
if (!expect_char (c, '%'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
/* skip an optional width specifier */
|
2009-02-10 09:38:22 +01:00
|
|
|
width = strtol ((const char *) c->format, (char **) &c->format, 10);
|
2009-02-10 06:15:08 +01:00
|
|
|
|
|
|
|
if (!expect_char (c, '{'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
while(*c->format && *c->format != '}')
|
|
|
|
{
|
|
|
|
switch (*c->format)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
c->format++; /* skip over '\\' */
|
|
|
|
if (*c->format)
|
|
|
|
c->format++;
|
|
|
|
continue;
|
|
|
|
case '{':
|
|
|
|
if (!skip_subexpr (c))
|
|
|
|
return FcFalse;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c->format++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return expect_char (c, '}');
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
skip_expr (FcFormatContext *c)
|
|
|
|
{
|
|
|
|
while(*c->format && *c->format != '}')
|
|
|
|
{
|
|
|
|
switch (*c->format)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
c->format++; /* skip over '\\' */
|
|
|
|
if (*c->format)
|
|
|
|
c->format++;
|
|
|
|
continue;
|
|
|
|
case '%':
|
|
|
|
if (!skip_percent (c))
|
|
|
|
return FcFalse;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
c->format++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
skip_subexpr (FcFormatContext *c)
|
|
|
|
{
|
|
|
|
return expect_char (c, '{') &&
|
|
|
|
skip_expr (c) &&
|
|
|
|
expect_char (c, '}');
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
maybe_skip_subexpr (FcFormatContext *c)
|
|
|
|
{
|
|
|
|
return (*c->format == '{') ?
|
|
|
|
skip_subexpr (c) :
|
|
|
|
FcTrue;
|
|
|
|
}
|
|
|
|
|
2009-02-10 05:08:08 +01:00
|
|
|
static FcBool
|
|
|
|
interpret_filter (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
|
|
|
FcObjectSet *os;
|
|
|
|
FcPattern *subpat;
|
|
|
|
|
|
|
|
if (!expect_char (c, '+'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
os = FcObjectSetCreate ();
|
|
|
|
if (!os)
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2009-02-10 09:38:22 +01:00
|
|
|
if (!read_word (c) ||
|
|
|
|
!FcObjectSetAdd (os, (const char *) c->word))
|
2009-02-10 05:08:08 +01:00
|
|
|
{
|
|
|
|
FcObjectSetDestroy (os);
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (consume_char (c, ','));
|
|
|
|
|
|
|
|
subpat = FcPatternFilter (pat, os);
|
|
|
|
FcObjectSetDestroy (os);
|
|
|
|
|
|
|
|
if (!subpat ||
|
|
|
|
!interpret_subexpr (c, subpat, buf))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
FcPatternDestroy (subpat);
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
interpret_delete (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
|
|
|
FcPattern *subpat;
|
|
|
|
|
|
|
|
if (!expect_char (c, '-'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
subpat = FcPatternDuplicate (pat);
|
|
|
|
if (!subpat)
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2009-02-10 09:38:22 +01:00
|
|
|
if (!read_word (c))
|
2009-02-10 05:08:08 +01:00
|
|
|
{
|
|
|
|
FcPatternDestroy (subpat);
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
FcPatternDel (subpat, (const char *) c->word);
|
2009-02-10 05:08:08 +01:00
|
|
|
}
|
|
|
|
while (consume_char (c, ','));
|
|
|
|
|
|
|
|
if (!interpret_subexpr (c, subpat, buf))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
FcPatternDestroy (subpat);
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
2009-02-10 06:15:08 +01:00
|
|
|
static FcBool
|
2009-02-10 09:38:22 +01:00
|
|
|
interpret_cond (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 06:15:08 +01:00
|
|
|
{
|
|
|
|
FcBool pass;
|
|
|
|
|
|
|
|
if (!expect_char (c, '?'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
pass = FcTrue;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
FcBool negate;
|
|
|
|
FcValue v;
|
|
|
|
|
|
|
|
negate = consume_char (c, '!');
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
if (!read_word (c))
|
2009-02-10 06:15:08 +01:00
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
pass = pass &&
|
|
|
|
(negate ^
|
2009-02-10 09:38:22 +01:00
|
|
|
(FcResultMatch == FcPatternGet (pat,
|
|
|
|
(const char *) c->word,
|
|
|
|
0, &v)));
|
2009-02-10 06:15:08 +01:00
|
|
|
}
|
|
|
|
while (consume_char (c, ','));
|
|
|
|
|
|
|
|
if (pass)
|
|
|
|
{
|
|
|
|
if (!interpret_subexpr (c, pat, buf) ||
|
|
|
|
!maybe_skip_subexpr (c))
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!skip_subexpr (c) ||
|
|
|
|
!maybe_interpret_subexpr (c, pat, buf))
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
2009-02-10 11:05:53 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-10 10:44:54 +01:00
|
|
|
static FcChar8 *
|
|
|
|
cescape (const FcChar8 *str)
|
|
|
|
{
|
|
|
|
FcStrBuf buf;
|
|
|
|
FcChar8 buf_static[8192];
|
|
|
|
|
|
|
|
FcStrBufInit (&buf, buf_static, sizeof (buf_static));
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
switch (*str)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case '"':
|
|
|
|
FcStrBufChar (&buf, '\\');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
FcStrBufChar (&buf, *str++);
|
|
|
|
}
|
|
|
|
return FcStrBufDone (&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcChar8 *
|
|
|
|
shescape (const FcChar8 *str)
|
|
|
|
{
|
|
|
|
FcStrBuf buf;
|
|
|
|
FcChar8 buf_static[8192];
|
|
|
|
|
|
|
|
FcStrBufInit (&buf, buf_static, sizeof (buf_static));
|
|
|
|
FcStrBufChar (&buf, '\'');
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if (*str == '\'')
|
|
|
|
FcStrBufString (&buf, (const FcChar8 *) "'\\''");
|
|
|
|
else
|
|
|
|
FcStrBufChar (&buf, *str);
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
FcStrBufChar (&buf, '\'');
|
|
|
|
return FcStrBufDone (&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcChar8 *
|
|
|
|
xmlescape (const FcChar8 *str)
|
|
|
|
{
|
|
|
|
FcStrBuf buf;
|
|
|
|
FcChar8 buf_static[8192];
|
|
|
|
|
|
|
|
FcStrBufInit (&buf, buf_static, sizeof (buf_static));
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
switch (*str)
|
|
|
|
{
|
|
|
|
case '&': FcStrBufString (&buf, (const FcChar8 *) "&"); break;
|
|
|
|
case '<': FcStrBufString (&buf, (const FcChar8 *) "<"); break;
|
|
|
|
case '>': FcStrBufString (&buf, (const FcChar8 *) ">"); break;
|
|
|
|
default: FcStrBufChar (&buf, *str); break;
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
return FcStrBufDone (&buf);
|
|
|
|
}
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
static FcChar8 *
|
|
|
|
convert (FcFormatContext *c,
|
|
|
|
const FcChar8 *str)
|
|
|
|
{
|
|
|
|
if (!read_word (c))
|
|
|
|
return NULL;
|
2009-02-10 10:44:54 +01:00
|
|
|
#define CONVERTER(name, func) \
|
|
|
|
else if (0 == strcmp ((const char *) c->word, name))\
|
|
|
|
return func (str)
|
|
|
|
CONVERTER ("downcase", FcStrDowncase);
|
|
|
|
CONVERTER ("basename", FcStrBasename);
|
|
|
|
CONVERTER ("dirname", FcStrDirname);
|
|
|
|
CONVERTER ("cescape", cescape);
|
|
|
|
CONVERTER ("shescape", shescape);
|
|
|
|
CONVERTER ("xmlescape", xmlescape);
|
2009-02-10 09:38:22 +01:00
|
|
|
|
|
|
|
message ("unknown converter \"%s\"",
|
|
|
|
c->word);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
maybe_interpret_converts (FcFormatContext *c,
|
|
|
|
FcStrBuf *buf,
|
|
|
|
int start)
|
|
|
|
{
|
|
|
|
while (consume_char (c, '|'))
|
|
|
|
{
|
|
|
|
const FcChar8 *str;
|
|
|
|
FcChar8 *new_str;
|
|
|
|
|
|
|
|
/* nul-terminate the buffer */
|
|
|
|
FcStrBufChar (buf, '\0');
|
|
|
|
if (buf->failed)
|
|
|
|
return FcFalse;
|
|
|
|
str = buf->buf + start;
|
|
|
|
|
|
|
|
if (!(new_str = convert (c, str)))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
/* replace in the buffer */
|
|
|
|
buf->len = start;
|
|
|
|
FcStrBufString (buf, new_str);
|
|
|
|
free (new_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
align_to_width (FcStrBuf *buf,
|
|
|
|
int start,
|
|
|
|
int width)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
|
|
|
|
if (buf->failed)
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
len = buf->len - start;
|
|
|
|
if (len < -width)
|
|
|
|
{
|
|
|
|
/* left align */
|
|
|
|
while (len++ < -width)
|
|
|
|
FcStrBufChar (buf, ' ');
|
|
|
|
}
|
|
|
|
else if (len < width)
|
|
|
|
{
|
|
|
|
int old_len;
|
|
|
|
old_len = len;
|
|
|
|
/* right align */
|
|
|
|
while (len++ < width)
|
|
|
|
FcStrBufChar (buf, ' ');
|
|
|
|
if (buf->failed)
|
|
|
|
return FcFalse;
|
|
|
|
len = old_len;
|
|
|
|
memmove (buf->buf + buf->len - len,
|
|
|
|
buf->buf + buf->len - width,
|
|
|
|
len);
|
|
|
|
memset (buf->buf + buf->len - width,
|
|
|
|
' ',
|
|
|
|
width - len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return !buf->failed;
|
|
|
|
}
|
2009-02-10 05:08:08 +01:00
|
|
|
static FcBool
|
|
|
|
interpret_percent (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
2009-02-10 09:38:22 +01:00
|
|
|
int width, start;
|
|
|
|
FcBool ret;
|
2009-02-10 05:08:08 +01:00
|
|
|
|
|
|
|
if (!expect_char (c, '%'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
if (consume_char (c, '%')) /* "%%" */
|
|
|
|
{
|
|
|
|
FcStrBufChar (buf, '%');
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse an optional width specifier */
|
|
|
|
width = strtol ((const char *) c->format, (char **) &c->format, 10);
|
|
|
|
|
|
|
|
if (!expect_char (c, '{'))
|
|
|
|
return FcFalse;
|
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
start = buf->len;
|
2009-02-10 06:15:08 +01:00
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
switch (*c->format) {
|
|
|
|
case '{': ret = interpret_subexpr (c, pat, buf); break;
|
|
|
|
case '+': ret = interpret_filter (c, pat, buf); break;
|
|
|
|
case '-': ret = interpret_delete (c, pat, buf); break;
|
|
|
|
case '?': ret = interpret_cond (c, pat, buf); break;
|
2009-02-10 11:05:53 +01:00
|
|
|
case '#': ret = interpret_count (c, pat, buf); break;
|
2009-02-10 09:38:22 +01:00
|
|
|
default: ret = interpret_simple (c, pat, buf); break;
|
2008-12-30 02:00:26 +01:00
|
|
|
}
|
2009-02-10 00:18:59 +01:00
|
|
|
|
2009-02-10 09:38:22 +01:00
|
|
|
return ret &&
|
|
|
|
maybe_interpret_converts (c, buf, start) &&
|
|
|
|
align_to_width (buf, start, width) &&
|
|
|
|
expect_char (c, '}');
|
2008-12-30 02:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static char escaped_char(const char ch)
|
|
|
|
{
|
|
|
|
switch (ch) {
|
|
|
|
case 'a': return '\a';
|
|
|
|
case 'b': return '\b';
|
|
|
|
case 'f': return '\f';
|
|
|
|
case 'n': return '\n';
|
|
|
|
case 'r': return '\r';
|
|
|
|
case 't': return '\t';
|
|
|
|
case 'v': return '\v';
|
|
|
|
default: return ch;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-10 05:08:08 +01:00
|
|
|
static FcBool
|
2009-02-10 06:15:08 +01:00
|
|
|
interpret_expr (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf,
|
|
|
|
FcChar8 term)
|
2008-12-30 02:00:26 +01:00
|
|
|
{
|
2009-02-10 06:15:08 +01:00
|
|
|
while (*c->format && *c->format != term)
|
2008-12-30 02:00:26 +01:00
|
|
|
{
|
2009-02-10 01:13:07 +01:00
|
|
|
switch (*c->format)
|
2008-12-30 02:00:26 +01:00
|
|
|
{
|
|
|
|
case '\\':
|
2009-02-10 01:13:07 +01:00
|
|
|
c->format++; /* skip over '\\' */
|
|
|
|
if (*c->format)
|
|
|
|
FcStrBufChar (buf, escaped_char (*c->format++));
|
2008-12-30 02:00:26 +01:00
|
|
|
continue;
|
|
|
|
case '%':
|
2009-02-10 05:08:08 +01:00
|
|
|
if (!interpret_percent (c, pat, buf))
|
|
|
|
return FcFalse;
|
2008-12-30 02:00:26 +01:00
|
|
|
continue;
|
|
|
|
}
|
2009-02-10 01:13:07 +01:00
|
|
|
FcStrBufChar (buf, *c->format++);
|
2008-12-30 02:00:26 +01:00
|
|
|
}
|
2009-02-10 05:08:08 +01:00
|
|
|
return FcTrue;
|
2008-12-30 02:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FcChar8 *
|
|
|
|
FcPatternFormat (FcPattern *pat, const FcChar8 *format)
|
|
|
|
{
|
2009-02-10 10:44:54 +01:00
|
|
|
FcStrBuf buf;
|
|
|
|
FcChar8 buf_static[8192];
|
2009-02-10 01:13:07 +01:00
|
|
|
FcFormatContext c;
|
2009-02-10 10:44:54 +01:00
|
|
|
FcBool ret;
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-10 10:44:54 +01:00
|
|
|
FcStrBufInit (&buf, buf_static, sizeof (buf_static));
|
2009-02-10 05:08:08 +01:00
|
|
|
if (!FcFormatContextInit (&c, format))
|
|
|
|
return NULL;
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-10 06:15:08 +01:00
|
|
|
ret = interpret_expr (&c, pat, &buf, '\0');
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-10 01:13:07 +01:00
|
|
|
FcFormatContextDone (&c);
|
2009-02-10 05:08:08 +01:00
|
|
|
if (ret)
|
|
|
|
return FcStrBufDone (&buf);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FcStrBufDestroy (&buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-12-30 02:00:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define __fcformat__
|
|
|
|
#include "fcaliastail.h"
|
|
|
|
#undef __fcformat__
|