]>
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: 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);
59 return c
->scratch
!= NULL
;
63 FcFormatContextDone (FcFormatContext
*c
)
72 consume_char (FcFormatContext
*c
,
75 if (*c
->format
!= term
)
83 expect_char (FcFormatContext
*c
,
86 FcBool res
= consume_char (c
, term
);
89 if (c
->format
== c
->format_orig
+ c
->format_len
)
90 message ("format ended while expecting '%c'",
93 message ("expected '%c' at %d",
94 term
, c
->format
- c
->format_orig
+ 1);
100 FcCharIsPunct (const FcChar8 c
)
120 read_elt_name_to_scratch (FcFormatContext
*c
)
128 if (*c
->format
== '\\')
135 else if (FcCharIsPunct (*c
->format
))
144 message ("expected element name at %d",
145 c
->format
- c
->format_orig
+ 1);
153 interpret (FcFormatContext
*c
,
159 interpret_subexpr (FcFormatContext
*c
,
163 return expect_char (c
, '{') &&
164 interpret (c
, pat
, buf
, '}') &&
165 expect_char (c
, '}');
169 interpret_simple_tag (FcFormatContext
*c
,
174 FcBool add_colon
= FcFalse
;
175 FcBool add_elt_name
= FcFalse
;
177 if (consume_char (c
, ':'))
180 if (!read_elt_name_to_scratch (c
))
183 if (consume_char (c
, '='))
184 add_elt_name
= FcTrue
;
186 e
= FcPatternObjectFindElt (pat
,
187 FcObjectFromName ((const char *) c
->scratch
));
193 FcStrBufChar (buf
, ':');
196 FcStrBufString (buf
, c
->scratch
);
197 FcStrBufChar (buf
, '=');
200 l
= FcPatternEltValues(e
);
201 FcNameUnparseValueList (buf
, l
, '\0');
208 interpret_filter (FcFormatContext
*c
,
215 if (!expect_char (c
, '+'))
218 os
= FcObjectSetCreate ();
224 if (!read_elt_name_to_scratch (c
) ||
225 !FcObjectSetAdd (os
, (const char *) c
->scratch
))
227 FcObjectSetDestroy (os
);
231 while (consume_char (c
, ','));
233 subpat
= FcPatternFilter (pat
, os
);
234 FcObjectSetDestroy (os
);
237 !interpret_subexpr (c
, subpat
, buf
))
240 FcPatternDestroy (subpat
);
245 interpret_delete (FcFormatContext
*c
,
251 if (!expect_char (c
, '-'))
254 subpat
= FcPatternDuplicate (pat
);
260 if (!read_elt_name_to_scratch (c
))
262 FcPatternDestroy (subpat
);
266 FcPatternDel (subpat
, (const char *) c
->scratch
);
268 while (consume_char (c
, ','));
270 if (!interpret_subexpr (c
, subpat
, buf
))
273 FcPatternDestroy (subpat
);
278 interpret_percent (FcFormatContext
*c
,
284 if (!expect_char (c
, '%'))
287 if (consume_char (c
, '%')) /* "%%" */
289 FcStrBufChar (buf
, '%');
293 /* parse an optional width specifier */
294 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
298 if (!expect_char (c
, '{'))
301 switch (*c
->format
) {
305 if (!interpret_subexpr (c
, pat
, buf
))
310 /* filtering pattern elements */
311 if (!interpret_filter (c
, pat
, buf
))
316 /* deleting pattern elements */
317 if (!interpret_delete (c
, pat
, buf
))
323 if (!interpret_simple_tag (c
, pat
, buf
))
329 /* handle filters, if any */
339 len
= after
- before
;
344 while (len
++ < -width
)
345 FcStrBufChar (buf
, ' ');
347 else if (len
< width
)
350 while (len
++ < width
)
351 FcStrBufChar (buf
, ' ');
352 len
= after
- before
;
353 memmove (buf
->buf
+ buf
->len
- len
,
354 buf
->buf
+ buf
->len
- width
,
356 memset (buf
->buf
+ buf
->len
- width
,
362 return expect_char (c
, '}');
365 static char escaped_char(const char ch
)
368 case 'a': return '\a';
369 case 'b': return '\b';
370 case 'f': return '\f';
371 case 'n': return '\n';
372 case 'r': return '\r';
373 case 't': return '\t';
374 case 'v': return '\v';
380 interpret (FcFormatContext
*c
,
385 for (; *c
->format
&& *c
->format
!= term
;)
390 c
->format
++; /* skip over '\\' */
392 FcStrBufChar (buf
, escaped_char (*c
->format
++));
395 if (!interpret_percent (c
, pat
, buf
))
399 FcStrBufChar (buf
, *c
->format
++);
405 FcPatternFormat (FcPattern
*pat
, const FcChar8
*format
)
411 FcStrBufInit (&buf
, 0, 0);
412 if (!FcFormatContextInit (&c
, format
))
415 ret
= interpret (&c
, pat
, &buf
, '\0');
419 FcFormatContextDone (&c
);
421 return FcStrBufDone (&buf
);
424 FcStrBufDestroy (&buf
);
430 #include "fcaliastail.h"