]>
git.wh0rd.org - fontconfig.git/blob - src/fcformat.c
2 * Copyright © 2008 Red Hat, Inc.
4 * Red Hat Author(s): Behdad Esfahbod
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
32 message (const char *fmt
, ...)
36 fprintf (stderr
, "Fontconfig: ");
37 vfprintf (stderr
, fmt
, args
);
38 fprintf (stderr
, "\n");
43 typedef struct _FcFormatContext
45 const FcChar8
*format_orig
;
46 const FcChar8
*format
;
52 FcFormatContextInit (FcFormatContext
*c
,
53 const FcChar8
*format
)
55 c
->format_orig
= c
->format
= format
;
56 c
->format_len
= strlen ((const char *) format
);
57 c
->scratch
= malloc (c
->format_len
+ 1);
61 FcFormatContextDone (FcFormatContext
*c
)
70 consume_char (FcFormatContext
*c
,
73 if (*c
->format
!= term
)
75 message ("Pattern format error: expected '%c' at %d",
76 term
, c
->format
- c
->format_orig
+ 1);
85 interpret_percent (FcFormatContext
*c
,
93 if (!consume_char (c
, '%'))
96 if (*c
->format
== '%') /* "%%" */
98 FcStrBufChar (buf
, *c
->format
++);
102 /* parse an optional width specifier */
103 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
107 if (!consume_char (c
, '{'))
110 p
= (FcChar8
*) strpbrk ((const char *) c
->format
, "}" /* "=?:}" */);
113 message ("Pattern format missing closing brace for opening brace at %d",
114 c
->format
-1 - c
->format_orig
+ 1);
117 /* extract the element name */
118 memcpy (c
->scratch
, c
->format
, p
- c
->format
);
119 c
->scratch
[p
- c
->format
] = '\0';
122 e
= FcPatternObjectFindElt (pat
, FcObjectFromName ((const char *) c
->scratch
));
126 l
= FcPatternEltValues(e
);
127 FcNameUnparseValueList (buf
, l
, '\0');
130 /* handle filters, if any */
140 len
= after
- before
;
145 while (len
++ < -width
)
146 FcStrBufChar (buf
, ' ');
148 else if (len
< width
)
151 while (len
++ < width
)
152 FcStrBufChar (buf
, ' ');
153 len
= after
- before
;
154 memmove (buf
->buf
+ buf
->len
- len
,
155 buf
->buf
+ buf
->len
- width
,
157 memset (buf
->buf
+ buf
->len
- width
,
163 consume_char (c
, '}');
166 static char escaped_char(const char ch
)
169 case 'a': return '\a';
170 case 'b': return '\b';
171 case 'f': return '\f';
172 case 'n': return '\n';
173 case 'r': return '\r';
174 case 't': return '\t';
175 case 'v': return '\v';
181 interpret (FcFormatContext
*c
,
186 for (; *c
->format
&& *c
->format
!= term
;)
191 c
->format
++; /* skip over '\\' */
193 FcStrBufChar (buf
, escaped_char (*c
->format
++));
196 interpret_percent (c
, pat
, buf
);
199 FcStrBufChar (buf
, *c
->format
++);
201 if (*c
->format
!= term
)
202 message ("Pattern format ended while looking for '%c'", term
);
206 FcPatternFormat (FcPattern
*pat
, const FcChar8
*format
)
211 FcStrBufInit (&buf
, 0, 0);
212 FcFormatContextInit (&c
, format
);
214 interpret (&c
, pat
, &buf
, '\0');
216 FcFormatContextDone (&c
);
217 return FcStrBufDone (&buf
);
221 #include "fcaliastail.h"