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
|
2010-11-10 22:45:42 +01:00
|
|
|
* documentation, and that the name of the author(s) not be used in
|
2008-12-30 02:00:26 +01:00
|
|
|
* advertising or publicity pertaining to distribution of the software without
|
2010-11-10 22:45:42 +01:00
|
|
|
* specific, written prior permission. The authors make no
|
2008-12-30 02:00:26 +01:00
|
|
|
* representations about the suitability of this software for any purpose. It
|
|
|
|
* is provided "as is" without express or implied warranty.
|
|
|
|
*
|
2009-03-12 21:00:08 +01:00
|
|
|
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
2008-12-30 02:00:26 +01:00
|
|
|
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
2009-03-12 21:00:08 +01:00
|
|
|
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
2008-12-30 02:00:26 +01:00
|
|
|
* 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-03-13 22:26:21 +01:00
|
|
|
/* The language is documented in doc/fcformat.fncs
|
|
|
|
* These are the features implemented:
|
2009-02-14 02:18:11 +01:00
|
|
|
*
|
2009-03-13 22:26:21 +01:00
|
|
|
* simple %{elt}
|
|
|
|
* width %width{elt}
|
|
|
|
* index %{elt[idx]}
|
|
|
|
* name= %{elt=}
|
|
|
|
* :name= %{:elt}
|
|
|
|
* default %{elt:-word}
|
|
|
|
* count %{#elt}
|
|
|
|
* subexpr %{{expr}}
|
|
|
|
* filter-out %{-elt1,elt2,elt3{expr}}
|
|
|
|
* filter-in %{+elt1,elt2,elt3{expr}}
|
|
|
|
* conditional %{?elt1,elt2,!elt3{}{}}
|
|
|
|
* enumerate %{[]elt1,elt2{expr}}
|
|
|
|
* langset langset enumeration using the same syntax
|
|
|
|
* builtin %{=blt}
|
|
|
|
* convert %{elt|conv1|conv2|conv3}
|
2009-02-14 02:18:11 +01:00
|
|
|
*
|
2009-03-13 22:26:21 +01:00
|
|
|
* converters:
|
|
|
|
* basename FcStrBasename
|
|
|
|
* dirname FcStrDirname
|
|
|
|
* downcase FcStrDowncase
|
2009-02-14 02:18:11 +01:00
|
|
|
* shescape
|
|
|
|
* cescape
|
|
|
|
* xmlescape
|
2009-03-13 22:26:21 +01:00
|
|
|
* delete delete chars
|
|
|
|
* escape escape chars
|
|
|
|
* translate translate chars
|
2009-02-14 02:18:11 +01:00
|
|
|
*
|
|
|
|
* builtins:
|
2009-03-13 22:26:21 +01:00
|
|
|
* unparse FcNameUnparse
|
|
|
|
* fcmatch fc-match default
|
|
|
|
* fclist fc-list default
|
2011-06-20 17:55:25 +02:00
|
|
|
* fccat fc-cat default
|
2009-03-13 22:26:21 +01:00
|
|
|
* pkgkit PackageKit package tag format
|
|
|
|
*
|
|
|
|
*
|
2009-02-10 09:38:22 +01:00
|
|
|
* Some ideas for future syntax extensions:
|
|
|
|
*
|
2009-03-13 22:26:21 +01:00
|
|
|
* - verbose builtin that is like FcPatternPrint
|
2009-02-11 02:56:39 +01:00
|
|
|
* - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
|
2010-08-18 17:31:31 +02:00
|
|
|
* - allow indexing in +, -, ? filtering?
|
2009-02-10 12:22:55 +01:00
|
|
|
* - conditional/filtering/deletion on binding (using '(w)'/'(s)'/'(=)' notation)
|
2009-02-10 09:38:22 +01:00
|
|
|
*/
|
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
|
2011-06-20 17:55:25 +02:00
|
|
|
#define FCCAT_FORMAT "\"%{file|basename|cescape}\" %{index} \"%{-file{%{=unparse|cescape}}}\""
|
2009-02-12 05:55:11 +01:00
|
|
|
#define FCMATCH_FORMAT "%{file:-<unknown filename>|basename}: \"%{family[0]:-<unknown family>}\" \"%{style[0]:-<unknown style>}\""
|
2011-06-20 17:22:17 +02:00
|
|
|
#define FCLIST_FORMAT "%{?file{%{file}: }}%{-file{%{=unparse}}}"
|
2009-02-14 01:41:37 +01:00
|
|
|
#define PKGKIT_FORMAT "%{[]family{font(%{family|downcase|delete( )})\n}}%{[]lang{font(:lang=%{lang|downcase|translate(_,-)})\n}}"
|
2009-02-11 02:56:39 +01:00
|
|
|
|
|
|
|
|
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-11 00:57:34 +01:00
|
|
|
FcBool word_allocated;
|
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,
|
2009-02-11 00:57:34 +01:00
|
|
|
const FcChar8 *format,
|
|
|
|
FcChar8 *scratch,
|
|
|
|
int scratch_len)
|
2009-02-10 01:13:07 +01:00
|
|
|
{
|
|
|
|
c->format_orig = c->format = format;
|
|
|
|
c->format_len = strlen ((const char *) format);
|
2009-02-11 00:57:34 +01:00
|
|
|
|
|
|
|
if (c->format_len < scratch_len)
|
|
|
|
{
|
|
|
|
c->word = scratch;
|
|
|
|
c->word_allocated = FcFalse;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
c->word = malloc (c->format_len + 1);
|
|
|
|
c->word_allocated = FcTrue;
|
|
|
|
}
|
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)
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
if (c && c->word_allocated)
|
2009-02-10 01:13:07 +01:00
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2009-02-10 11:57:10 +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 02:49:45 +01:00
|
|
|
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)
|
2009-02-10 11:57:10 +01:00
|
|
|
*p++ = escaped_char (*c->format++);
|
2009-02-10 02:49:45 +01:00
|
|
|
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-11 00:57:34 +01:00
|
|
|
message ("expected identifier at %d",
|
2009-02-10 02:49:45 +01:00
|
|
|
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 11:57:10 +01:00
|
|
|
static FcBool
|
|
|
|
read_chars (FcFormatContext *c,
|
|
|
|
FcChar8 term)
|
|
|
|
{
|
|
|
|
FcChar8 *p;
|
|
|
|
|
|
|
|
p = c->word;
|
|
|
|
|
|
|
|
while (*c->format && *c->format != '}' && *c->format != term)
|
|
|
|
{
|
|
|
|
if (*c->format == '\\')
|
|
|
|
{
|
|
|
|
c->format++;
|
|
|
|
if (*c->format)
|
|
|
|
*p++ = escaped_char (*c->format++);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p++ = *c->format++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
if (p == c->word)
|
|
|
|
{
|
|
|
|
message ("expected character data at %d",
|
|
|
|
c->format - c->format_orig + 1);
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
static FcBool
|
|
|
|
FcPatternFormatToBuf (FcPattern *pat,
|
|
|
|
const FcChar8 *format,
|
|
|
|
FcStrBuf *buf);
|
|
|
|
|
2009-02-10 12:22:55 +01:00
|
|
|
static FcBool
|
|
|
|
interpret_builtin (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
2009-02-11 02:56:39 +01:00
|
|
|
FcChar8 *new_str;
|
|
|
|
FcBool ret;
|
2009-02-10 12:22:55 +01:00
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
if (!expect_char (c, '=') ||
|
|
|
|
!read_word (c))
|
2009-02-10 12:22:55 +01:00
|
|
|
return FcFalse;
|
2009-02-11 02:56:39 +01:00
|
|
|
|
|
|
|
/* try simple builtins first */
|
|
|
|
if (0) { }
|
2009-02-10 12:22:55 +01:00
|
|
|
#define BUILTIN(name, func) \
|
|
|
|
else if (0 == strcmp ((const char *) c->word, name))\
|
2009-02-11 02:56:39 +01:00
|
|
|
do { new_str = func (pat); ret = FcTrue; } while (0)
|
|
|
|
BUILTIN ("unparse", FcNameUnparse);
|
2009-02-12 05:55:11 +01:00
|
|
|
/* BUILTIN ("verbose", FcPatternPrint); XXX */
|
2009-02-11 02:56:39 +01:00
|
|
|
#undef BUILTIN
|
|
|
|
else
|
|
|
|
ret = FcFalse;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (new_str)
|
|
|
|
{
|
|
|
|
FcStrBufString (buf, new_str);
|
2012-06-01 12:06:17 +02:00
|
|
|
FcStrFree (new_str);
|
2009-02-11 02:56:39 +01:00
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* now try our custom formats */
|
|
|
|
if (0) { }
|
|
|
|
#define BUILTIN(name, format) \
|
|
|
|
else if (0 == strcmp ((const char *) c->word, name))\
|
|
|
|
ret = FcPatternFormatToBuf (pat, (const FcChar8 *) format, buf)
|
2011-06-20 17:55:25 +02:00
|
|
|
BUILTIN ("fccat", FCCAT_FORMAT);
|
2009-02-11 02:56:39 +01:00
|
|
|
BUILTIN ("fcmatch", FCMATCH_FORMAT);
|
|
|
|
BUILTIN ("fclist", FCLIST_FORMAT);
|
2009-02-14 01:41:37 +01:00
|
|
|
BUILTIN ("pkgkit", PKGKIT_FORMAT);
|
2009-02-11 02:56:39 +01:00
|
|
|
#undef BUILTIN
|
|
|
|
else
|
|
|
|
ret = FcFalse;
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
message ("unknown builtin \"%s\"",
|
|
|
|
c->word);
|
|
|
|
|
|
|
|
return ret;
|
2009-02-10 12:22:55 +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)
|
|
|
|
{
|
|
|
|
if (!expect_char (c, '%'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
/* skip an optional width specifier */
|
2011-11-07 21:18:26 +01:00
|
|
|
if (strtol ((const char *) c->format, (char **) &c->format, 10))
|
2012-12-30 04:11:09 +01:00
|
|
|
{/* don't care */}
|
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
|
2009-03-13 22:26:21 +01:00
|
|
|
interpret_filter_in (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 05:08:08 +01:00
|
|
|
{
|
|
|
|
FcObjectSet *os;
|
|
|
|
FcPattern *subpat;
|
|
|
|
|
|
|
|
if (!expect_char (c, '+'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
os = FcObjectSetCreate ();
|
|
|
|
if (!os)
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2010-08-18 17:31:31 +02:00
|
|
|
/* XXX binding */
|
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
|
2009-03-13 22:26:21 +01:00
|
|
|
interpret_filter_out (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 05:08:08 +01:00
|
|
|
{
|
|
|
|
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-13 04:48: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++;
|
|
|
|
}
|
|
|
|
|
2009-02-10 11:57:10 +01:00
|
|
|
snprintf ((char *) buf_static, sizeof (buf_static), "%d", count);
|
2009-02-10 11:05:53 +01:00
|
|
|
FcStrBufString (buf, buf_static);
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
2009-02-13 04:48:22 +01:00
|
|
|
static FcBool
|
2009-03-13 22:26:21 +01:00
|
|
|
interpret_enumerate (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
2009-02-13 04:48:22 +01:00
|
|
|
{
|
|
|
|
FcObjectSet *os;
|
|
|
|
FcPattern *subpat;
|
|
|
|
const FcChar8 *format_save;
|
|
|
|
int idx;
|
|
|
|
FcBool ret, done;
|
2009-02-14 01:33:58 +01:00
|
|
|
FcStrList *lang_strs;
|
2009-02-13 04:48:22 +01:00
|
|
|
|
|
|
|
if (!expect_char (c, '[') ||
|
|
|
|
!expect_char (c, ']'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
os = FcObjectSetCreate ();
|
|
|
|
if (!os)
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
ret = FcTrue;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!read_word (c) ||
|
|
|
|
!FcObjectSetAdd (os, (const char *) c->word))
|
|
|
|
{
|
|
|
|
FcObjectSetDestroy (os);
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (consume_char (c, ','));
|
|
|
|
|
2009-02-14 01:33:58 +01:00
|
|
|
/* If we have one element and it's of type FcLangSet, we want
|
|
|
|
* to enumerate the languages in it. */
|
|
|
|
lang_strs = NULL;
|
|
|
|
if (os->nobject == 1)
|
|
|
|
{
|
|
|
|
FcLangSet *langset;
|
|
|
|
if (FcResultMatch ==
|
2011-03-14 22:04:59 +01:00
|
|
|
FcPatternGetLangSet (pat, os->objects[0], 0, &langset))
|
2009-02-14 01:33:58 +01:00
|
|
|
{
|
|
|
|
FcStrSet *ss;
|
|
|
|
if (!(ss = FcLangSetGetLangs (langset)) ||
|
|
|
|
!(lang_strs = FcStrListCreate (ss)))
|
|
|
|
goto bail0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-13 04:48:22 +01:00
|
|
|
subpat = FcPatternDuplicate (pat);
|
|
|
|
if (!subpat)
|
|
|
|
goto bail0;
|
|
|
|
|
|
|
|
format_save = c->format;
|
|
|
|
idx = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
done = FcTrue;
|
|
|
|
|
2009-02-14 01:33:58 +01:00
|
|
|
if (lang_strs)
|
2009-02-13 04:48:22 +01:00
|
|
|
{
|
2009-02-14 01:33:58 +01:00
|
|
|
FcChar8 *lang;
|
|
|
|
|
|
|
|
FcPatternDel (subpat, os->objects[0]);
|
|
|
|
if ((lang = FcStrListNext (lang_strs)))
|
2009-02-13 04:48:22 +01:00
|
|
|
{
|
2010-08-18 17:31:31 +02:00
|
|
|
/* XXX binding? */
|
2009-02-14 01:33:58 +01:00
|
|
|
FcPatternAddString (subpat, os->objects[0], lang);
|
2009-02-13 04:48:22 +01:00
|
|
|
done = FcFalse;
|
|
|
|
}
|
|
|
|
}
|
2009-02-14 01:33:58 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < os->nobject; i++)
|
|
|
|
{
|
|
|
|
FcValue v;
|
|
|
|
|
|
|
|
/* XXX this can be optimized by accessing valuelist linked lists
|
|
|
|
* directly and remembering where we were. Most (all) value lists
|
|
|
|
* in normal uses are pretty short though (language tags are
|
|
|
|
* stored as a LangSet, not separate values.). */
|
|
|
|
FcPatternDel (subpat, os->objects[i]);
|
|
|
|
if (FcResultMatch ==
|
|
|
|
FcPatternGet (pat, os->objects[i], idx, &v))
|
|
|
|
{
|
2010-08-18 17:31:31 +02:00
|
|
|
/* XXX binding */
|
2009-02-14 01:33:58 +01:00
|
|
|
FcPatternAdd (subpat, os->objects[i], v, FcFalse);
|
|
|
|
done = FcFalse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-13 04:48:22 +01:00
|
|
|
|
|
|
|
if (!done)
|
|
|
|
{
|
|
|
|
c->format = format_save;
|
|
|
|
ret = interpret_subexpr (c, subpat, buf);
|
|
|
|
if (!ret)
|
|
|
|
goto bail;
|
|
|
|
}
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
} while (!done);
|
|
|
|
|
2009-02-14 01:33:58 +01:00
|
|
|
if (c->format == format_save)
|
|
|
|
skip_subexpr (c);
|
|
|
|
|
2009-02-13 04:48:22 +01:00
|
|
|
bail:
|
|
|
|
FcPatternDestroy (subpat);
|
|
|
|
bail0:
|
2009-02-14 01:33:58 +01:00
|
|
|
if (lang_strs)
|
|
|
|
FcStrListDone (lang_strs);
|
2009-02-13 04:48:22 +01:00
|
|
|
FcObjectSetDestroy (os);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-02-10 11:05:53 +01:00
|
|
|
static FcBool
|
|
|
|
interpret_simple (FcFormatContext *c,
|
|
|
|
FcPattern *pat,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
|
|
|
FcPatternElt *e;
|
|
|
|
FcBool add_colon = FcFalse;
|
|
|
|
FcBool add_elt_name = FcFalse;
|
2009-02-12 05:44:36 +01:00
|
|
|
int idx;
|
2009-02-12 05:55:11 +01:00
|
|
|
FcChar8 *else_string;
|
2009-02-10 11:05:53 +01:00
|
|
|
|
|
|
|
if (consume_char (c, ':'))
|
|
|
|
add_colon = FcTrue;
|
|
|
|
|
|
|
|
if (!read_word (c))
|
|
|
|
return FcFalse;
|
|
|
|
|
2009-02-12 05:44:36 +01:00
|
|
|
idx = -1;
|
|
|
|
if (consume_char (c, '['))
|
|
|
|
{
|
|
|
|
idx = strtol ((const char *) c->format, (char **) &c->format, 10);
|
|
|
|
if (idx < 0)
|
|
|
|
{
|
|
|
|
message ("expected non-negative number at %d",
|
|
|
|
c->format-1 - c->format_orig + 1);
|
|
|
|
return FcFalse;
|
|
|
|
}
|
2009-02-13 04:48:22 +01:00
|
|
|
if (!expect_char (c, ']'))
|
|
|
|
return FcFalse;
|
2009-02-12 05:44:36 +01:00
|
|
|
}
|
|
|
|
|
2009-02-10 11:05:53 +01:00
|
|
|
if (consume_char (c, '='))
|
|
|
|
add_elt_name = FcTrue;
|
|
|
|
|
2009-02-12 05:55:11 +01:00
|
|
|
/* modifiers */
|
|
|
|
else_string = NULL;
|
|
|
|
if (consume_char (c, ':'))
|
|
|
|
{
|
|
|
|
FcChar8 *orig;
|
|
|
|
/* divert the c->word for now */
|
|
|
|
orig = c->word;
|
|
|
|
c->word = c->word + strlen ((const char *) c->word) + 1;
|
|
|
|
/* for now we just support 'default value' */
|
|
|
|
if (!expect_char (c, '-') ||
|
2011-06-20 17:32:46 +02:00
|
|
|
!read_chars (c, '|'))
|
2009-02-12 05:55:11 +01:00
|
|
|
{
|
|
|
|
c->word = orig;
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
else_string = c->word;
|
|
|
|
c->word = orig;
|
|
|
|
}
|
|
|
|
|
2009-02-10 11:05:53 +01:00
|
|
|
e = FcPatternObjectFindElt (pat,
|
|
|
|
FcObjectFromName ((const char *) c->word));
|
2009-03-13 17:11:13 +01:00
|
|
|
if (e || else_string)
|
2009-02-10 11:05:53 +01:00
|
|
|
{
|
2009-03-13 17:11:13 +01:00
|
|
|
FcValueListPtr l = NULL;
|
2009-02-10 11:05:53 +01:00
|
|
|
|
|
|
|
if (add_colon)
|
|
|
|
FcStrBufChar (buf, ':');
|
|
|
|
if (add_elt_name)
|
|
|
|
{
|
|
|
|
FcStrBufString (buf, c->word);
|
|
|
|
FcStrBufChar (buf, '=');
|
|
|
|
}
|
|
|
|
|
2009-03-13 17:11:13 +01:00
|
|
|
if (e)
|
|
|
|
l = FcPatternEltValues(e);
|
2009-02-12 05:44:36 +01:00
|
|
|
|
|
|
|
if (idx != -1)
|
|
|
|
{
|
|
|
|
while (l && idx > 0)
|
|
|
|
{
|
|
|
|
l = FcValueListNext(l);
|
|
|
|
idx--;
|
|
|
|
}
|
|
|
|
if (l && idx == 0)
|
|
|
|
{
|
|
|
|
if (!FcNameUnparseValue (buf, &l->value, '\0'))
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
else goto notfound;
|
|
|
|
}
|
2009-03-13 17:11:13 +01:00
|
|
|
else if (l)
|
2009-02-12 05:44:36 +01:00
|
|
|
{
|
|
|
|
FcNameUnparseValueList (buf, l, '\0');
|
|
|
|
}
|
2009-03-13 17:11:13 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
notfound:
|
|
|
|
if (else_string)
|
|
|
|
FcStrBufString (buf, else_string);
|
|
|
|
}
|
2009-02-10 11:05:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
2012-12-30 04:32:56 +01:00
|
|
|
cescape (FcFormatContext *c FC_UNUSED,
|
2009-02-11 02:56:39 +01:00
|
|
|
const FcChar8 *str,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 10:44:54 +01:00
|
|
|
{
|
2010-08-18 17:31:31 +02:00
|
|
|
/* XXX escape \n etc? */
|
|
|
|
|
2009-02-10 10:44:54 +01:00
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
switch (*str)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
case '"':
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, '\\');
|
2009-02-10 10:44:54 +01:00
|
|
|
break;
|
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, *str++);
|
2009-02-10 10:44:54 +01:00
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcTrue;
|
2009-02-10 10:44:54 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
2012-12-30 04:32:56 +01:00
|
|
|
shescape (FcFormatContext *c FC_UNUSED,
|
2009-02-11 02:56:39 +01:00
|
|
|
const FcChar8 *str,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 10:44:54 +01:00
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, '\'');
|
2009-02-10 10:44:54 +01:00
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
if (*str == '\'')
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufString (buf, (const FcChar8 *) "'\\''");
|
2009-02-10 10:44:54 +01:00
|
|
|
else
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, *str);
|
2009-02-10 10:44:54 +01:00
|
|
|
str++;
|
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, '\'');
|
|
|
|
return FcTrue;
|
2009-02-10 10:44:54 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
2012-12-30 04:32:56 +01:00
|
|
|
xmlescape (FcFormatContext *c FC_UNUSED,
|
2009-02-11 02:56:39 +01:00
|
|
|
const FcChar8 *str,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 10:44:54 +01:00
|
|
|
{
|
2010-08-18 17:31:31 +02:00
|
|
|
/* XXX escape \n etc? */
|
|
|
|
|
2009-02-10 10:44:54 +01:00
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
switch (*str)
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
case '&': FcStrBufString (buf, (const FcChar8 *) "&"); break;
|
|
|
|
case '<': FcStrBufString (buf, (const FcChar8 *) "<"); break;
|
|
|
|
case '>': FcStrBufString (buf, (const FcChar8 *) ">"); break;
|
|
|
|
default: FcStrBufChar (buf, *str); break;
|
2009-02-10 10:44:54 +01:00
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcTrue;
|
2009-02-10 10:44:54 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
2009-02-10 11:57:10 +01:00
|
|
|
delete_chars (FcFormatContext *c,
|
2009-02-11 02:56:39 +01:00
|
|
|
const FcChar8 *str,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 11:57:10 +01:00
|
|
|
{
|
|
|
|
/* XXX not UTF-8 aware */
|
|
|
|
|
|
|
|
if (!expect_char (c, '(') ||
|
|
|
|
!read_chars (c, ')') ||
|
|
|
|
!expect_char (c, ')'))
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcFalse;
|
2009-02-10 11:57:10 +01:00
|
|
|
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
FcChar8 *p;
|
|
|
|
|
|
|
|
p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
|
|
|
|
if (p)
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufData (buf, str, p - str);
|
2009-02-10 11:57:10 +01:00
|
|
|
str = p + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufString (buf, str);
|
2009-02-10 11:57:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
|
|
|
|
return FcTrue;
|
2009-02-10 11:57:10 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
2009-02-10 11:57:10 +01:00
|
|
|
escape_chars (FcFormatContext *c,
|
2009-02-11 02:56:39 +01:00
|
|
|
const FcChar8 *str,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 11:57:10 +01:00
|
|
|
{
|
|
|
|
/* XXX not UTF-8 aware */
|
|
|
|
|
|
|
|
if (!expect_char (c, '(') ||
|
|
|
|
!read_chars (c, ')') ||
|
|
|
|
!expect_char (c, ')'))
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcFalse;
|
2009-02-10 11:57:10 +01:00
|
|
|
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
FcChar8 *p;
|
|
|
|
|
|
|
|
p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
|
|
|
|
if (p)
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufData (buf, str, p - str);
|
|
|
|
FcStrBufChar (buf, c->word[0]);
|
|
|
|
FcStrBufChar (buf, *p);
|
2009-02-10 11:57:10 +01:00
|
|
|
str = p + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufString (buf, str);
|
2009-02-10 11:57:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
|
|
|
|
return FcTrue;
|
2009-02-10 11:57:10 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
2009-02-10 11:57:10 +01:00
|
|
|
translate_chars (FcFormatContext *c,
|
2009-02-11 02:56:39 +01:00
|
|
|
const FcChar8 *str,
|
|
|
|
FcStrBuf *buf)
|
2009-02-10 11:57:10 +01:00
|
|
|
{
|
|
|
|
char *from, *to, repeat;
|
|
|
|
int from_len, to_len;
|
|
|
|
|
|
|
|
/* XXX not UTF-8 aware */
|
|
|
|
|
|
|
|
if (!expect_char (c, '(') ||
|
|
|
|
!read_chars (c, ',') ||
|
|
|
|
!expect_char (c, ','))
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcFalse;
|
2009-02-10 11:57:10 +01:00
|
|
|
|
|
|
|
from = (char *) c->word;
|
|
|
|
from_len = strlen (from);
|
|
|
|
to = from + from_len + 1;
|
|
|
|
|
2009-02-12 05:55:11 +01:00
|
|
|
/* hack: we temporarily divert c->word */
|
2009-02-10 11:57:10 +01:00
|
|
|
c->word = (FcChar8 *) to;
|
|
|
|
if (!read_chars (c, ')'))
|
|
|
|
{
|
|
|
|
c->word = (FcChar8 *) from;
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
c->word = (FcChar8 *) from;
|
|
|
|
|
|
|
|
to_len = strlen (to);
|
|
|
|
repeat = to[to_len - 1];
|
|
|
|
|
|
|
|
if (!expect_char (c, ')'))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
FcChar8 *p;
|
|
|
|
|
|
|
|
p = (FcChar8 *) strpbrk ((const char *) str, (const char *) from);
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
int i;
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufData (buf, str, p - str);
|
2009-02-10 11:57:10 +01:00
|
|
|
i = strchr (from, *p) - from;
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, i < to_len ? to[i] : repeat);
|
2009-02-10 11:57:10 +01:00
|
|
|
str = p + 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufString (buf, str);
|
2009-02-10 11:57:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2009-02-11 00:57:34 +01:00
|
|
|
|
|
|
|
return FcTrue;
|
2009-02-10 11:57:10 +01:00
|
|
|
}
|
|
|
|
|
2009-02-11 00:57:34 +01:00
|
|
|
static FcBool
|
|
|
|
interpret_convert (FcFormatContext *c,
|
|
|
|
FcStrBuf *buf,
|
|
|
|
int start)
|
2009-02-10 09:38:22 +01:00
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
const FcChar8 *str;
|
|
|
|
FcChar8 *new_str;
|
|
|
|
FcStrBuf new_buf;
|
|
|
|
FcChar8 buf_static[8192];
|
|
|
|
FcBool ret;
|
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
if (!expect_char (c, '|') ||
|
|
|
|
!read_word (c))
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcFalse;
|
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
/* prepare the buffer */
|
2009-02-11 00:57:34 +01:00
|
|
|
FcStrBufChar (buf, '\0');
|
|
|
|
if (buf->failed)
|
|
|
|
return FcFalse;
|
|
|
|
str = buf->buf + start;
|
|
|
|
buf->len = start;
|
|
|
|
|
|
|
|
/* try simple converters first */
|
|
|
|
if (0) { }
|
2009-02-10 10:44:54 +01:00
|
|
|
#define CONVERTER(name, func) \
|
|
|
|
else if (0 == strcmp ((const char *) c->word, name))\
|
2009-02-11 00:57:34 +01:00
|
|
|
do { new_str = func (str); ret = FcTrue; } while (0)
|
2009-02-10 11:57:10 +01:00
|
|
|
CONVERTER ("downcase", FcStrDowncase);
|
|
|
|
CONVERTER ("basename", FcStrBasename);
|
|
|
|
CONVERTER ("dirname", FcStrDirname);
|
2009-02-11 00:57:34 +01:00
|
|
|
#undef CONVERTER
|
|
|
|
else
|
|
|
|
ret = FcFalse;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
if (new_str)
|
|
|
|
{
|
|
|
|
FcStrBufString (buf, new_str);
|
2012-06-01 12:06:17 +02:00
|
|
|
FcStrFree (new_str);
|
2009-02-11 00:57:34 +01:00
|
|
|
return FcTrue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return FcFalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
FcStrBufInit (&new_buf, buf_static, sizeof (buf_static));
|
|
|
|
|
|
|
|
/* now try our custom converters */
|
|
|
|
if (0) { }
|
|
|
|
#define CONVERTER(name, func) \
|
|
|
|
else if (0 == strcmp ((const char *) c->word, name))\
|
2009-02-11 02:56:39 +01:00
|
|
|
ret = func (c, str, &new_buf)
|
2009-02-11 00:57:34 +01:00
|
|
|
CONVERTER ("cescape", cescape);
|
|
|
|
CONVERTER ("shescape", shescape);
|
|
|
|
CONVERTER ("xmlescape", xmlescape);
|
|
|
|
CONVERTER ("delete", delete_chars);
|
|
|
|
CONVERTER ("escape", escape_chars);
|
|
|
|
CONVERTER ("translate", translate_chars);
|
|
|
|
#undef CONVERTER
|
|
|
|
else
|
|
|
|
ret = FcFalse;
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
FcStrBufChar (&new_buf, '\0');
|
|
|
|
FcStrBufString (buf, new_buf.buf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
message ("unknown converter \"%s\"",
|
|
|
|
c->word);
|
|
|
|
|
|
|
|
FcStrBufDestroy (&new_buf);
|
|
|
|
|
|
|
|
return ret;
|
2009-02-10 09:38:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static FcBool
|
|
|
|
maybe_interpret_converts (FcFormatContext *c,
|
|
|
|
FcStrBuf *buf,
|
|
|
|
int start)
|
|
|
|
{
|
2009-02-11 00:57:34 +01:00
|
|
|
while (*c->format == '|')
|
|
|
|
if (!interpret_convert (c, buf, start))
|
2009-02-10 09:38:22 +01:00
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
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) {
|
2009-03-13 22:26:21 +01:00
|
|
|
case '=': ret = interpret_builtin (c, pat, buf); break;
|
|
|
|
case '{': ret = interpret_subexpr (c, pat, buf); break;
|
|
|
|
case '+': ret = interpret_filter_in (c, pat, buf); break;
|
|
|
|
case '-': ret = interpret_filter_out (c, pat, buf); break;
|
|
|
|
case '?': ret = interpret_cond (c, pat, buf); break;
|
|
|
|
case '#': ret = interpret_count (c, pat, buf); break;
|
|
|
|
case '[': ret = interpret_enumerate (c, pat, buf); break;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
static FcBool
|
|
|
|
FcPatternFormatToBuf (FcPattern *pat,
|
|
|
|
const FcChar8 *format,
|
|
|
|
FcStrBuf *buf)
|
|
|
|
{
|
|
|
|
FcFormatContext c;
|
|
|
|
FcChar8 word_static[1024];
|
|
|
|
FcBool ret;
|
|
|
|
|
|
|
|
if (!FcFormatContextInit (&c, format, word_static, sizeof (word_static)))
|
|
|
|
return FcFalse;
|
|
|
|
|
|
|
|
ret = interpret_expr (&c, pat, buf, '\0');
|
|
|
|
|
|
|
|
FcFormatContextDone (&c);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-30 02:00:26 +01:00
|
|
|
FcChar8 *
|
2009-02-11 02:56:39 +01:00
|
|
|
FcPatternFormat (FcPattern *pat,
|
|
|
|
const FcChar8 *format)
|
2008-12-30 02:00:26 +01:00
|
|
|
{
|
2009-02-10 10:44:54 +01:00
|
|
|
FcStrBuf buf;
|
2009-02-11 00:57:34 +01:00
|
|
|
FcChar8 buf_static[8192 - 1024];
|
2013-01-02 03:20:31 +01:00
|
|
|
FcPattern *alloced = NULL;
|
2009-02-10 10:44:54 +01:00
|
|
|
FcBool ret;
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2013-01-02 03:10:18 +01:00
|
|
|
if (!pat)
|
2013-01-02 03:20:31 +01:00
|
|
|
alloced = pat = FcPatternCreate ();
|
2013-01-02 03:10:18 +01:00
|
|
|
|
2009-02-10 10:44:54 +01:00
|
|
|
FcStrBufInit (&buf, buf_static, sizeof (buf_static));
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2009-02-11 02:56:39 +01:00
|
|
|
ret = FcPatternFormatToBuf (pat, format, &buf);
|
2008-12-30 02:00:26 +01:00
|
|
|
|
2013-01-02 03:20:31 +01:00
|
|
|
if (alloced)
|
|
|
|
FcPatternDestroy (alloced);
|
|
|
|
|
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__
|