]>
git.wh0rd.org - fontconfig.git/blob - src/fcformat.c
30c5a8d3bb3c935ff245ac02116c4b0f91ca90f8
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: Pattern format error:");
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
)
81 expect_char (FcFormatContext
*c
,
84 FcBool res
= consume_char (c
, term
);
87 if (c
->format
== c
->format_orig
+ c
->format_len
)
88 message ("format ended while expecting '%c'",
91 message ("expected '%c' at %d",
92 term
, c
->format
- c
->format_orig
+ 1);
98 FcCharIsPunct (const FcChar8 c
)
118 read_elt_name_to_scratch (FcFormatContext
*c
)
126 if (*c
->format
== '\\')
133 else if (FcCharIsPunct (*c
->format
))
142 message ("expected element name at %d",
143 c
->format
- c
->format_orig
+ 1);
151 interpret (FcFormatContext
*c
,
157 interpret_percent (FcFormatContext
*c
,
165 FcPattern
*subpat
= pat
;
166 FcBool add_colon
= FcFalse
;
167 FcBool add_elt_name
= FcFalse
;
169 if (!expect_char (c
, '%'))
172 if (consume_char (c
, '%')) /* "%%" */
174 FcStrBufChar (buf
, '%');
178 /* parse an optional width specifier */
179 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
183 if (!expect_char (c
, '{'))
186 if (consume_char (c
, '{'))
188 /* it's just a subexpression. no tag involved */
189 interpret (c
, pat
, buf
, '}');
190 expect_char (c
, '}');
194 switch (*c
->format
) {
197 consume_char (c
, ':');
202 if (!read_elt_name_to_scratch (c
))
205 if (consume_char (c
, '='))
206 add_elt_name
= FcTrue
;
208 e
= FcPatternObjectFindElt (pat
,
209 FcObjectFromName ((const char *) c
->scratch
));
215 FcStrBufChar (buf
, ':');
218 FcStrBufString (buf
, c
->scratch
);
219 FcStrBufChar (buf
, '=');
222 l
= FcPatternEltValues(e
);
223 FcNameUnparseValueList (buf
, l
, '\0');
227 /* handle filters, if any */
237 len
= after
- before
;
242 while (len
++ < -width
)
243 FcStrBufChar (buf
, ' ');
245 else if (len
< width
)
248 while (len
++ < width
)
249 FcStrBufChar (buf
, ' ');
250 len
= after
- before
;
251 memmove (buf
->buf
+ buf
->len
- len
,
252 buf
->buf
+ buf
->len
- width
,
254 memset (buf
->buf
+ buf
->len
- width
,
260 expect_char (c
, '}');
264 FcPatternDestroy (subpat
);
267 static char escaped_char(const char ch
)
270 case 'a': return '\a';
271 case 'b': return '\b';
272 case 'f': return '\f';
273 case 'n': return '\n';
274 case 'r': return '\r';
275 case 't': return '\t';
276 case 'v': return '\v';
282 interpret (FcFormatContext
*c
,
287 for (; *c
->format
&& *c
->format
!= term
;)
292 c
->format
++; /* skip over '\\' */
294 FcStrBufChar (buf
, escaped_char (*c
->format
++));
297 interpret_percent (c
, pat
, buf
);
300 FcStrBufChar (buf
, *c
->format
++);
305 FcPatternFormat (FcPattern
*pat
, const FcChar8
*format
)
310 FcStrBufInit (&buf
, 0, 0);
311 FcFormatContextInit (&c
, format
);
313 interpret (&c
, pat
, &buf
, '\0');
315 FcFormatContextDone (&c
);
316 return FcStrBufDone (&buf
);
320 #include "fcaliastail.h"