]>
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 * Some ideas for future syntax extensions:
34 * - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
35 * - allow indexing simple tags using '%{elt[idx]}'
36 * - conditional/filtering/deletion on binding (using '(w)'/'(s)' notation)
40 message (const char *fmt
, ...)
44 fprintf (stderr
, "Fontconfig: Pattern format error: ");
45 vfprintf (stderr
, fmt
, args
);
46 fprintf (stderr
, ".\n");
51 typedef struct _FcFormatContext
53 const FcChar8
*format_orig
;
54 const FcChar8
*format
;
60 FcFormatContextInit (FcFormatContext
*c
,
61 const FcChar8
*format
)
63 c
->format_orig
= c
->format
= format
;
64 c
->format_len
= strlen ((const char *) format
);
65 c
->word
= malloc (c
->format_len
+ 1);
67 return c
->word
!= NULL
;
71 FcFormatContextDone (FcFormatContext
*c
)
80 consume_char (FcFormatContext
*c
,
83 if (*c
->format
!= term
)
91 expect_char (FcFormatContext
*c
,
94 FcBool res
= consume_char (c
, term
);
97 if (c
->format
== c
->format_orig
+ c
->format_len
)
98 message ("format ended while expecting '%c'",
101 message ("expected '%c' at %d",
102 term
, c
->format
- c
->format_orig
+ 1);
108 FcCharIsPunct (const FcChar8 c
)
128 read_word (FcFormatContext
*c
)
136 if (*c
->format
== '\\')
143 else if (FcCharIsPunct (*c
->format
))
152 message ("expected element name at %d",
153 c
->format
- c
->format_orig
+ 1);
161 interpret_expr (FcFormatContext
*c
,
167 interpret_subexpr (FcFormatContext
*c
,
171 return expect_char (c
, '{') &&
172 interpret_expr (c
, pat
, buf
, '}') &&
173 expect_char (c
, '}');
177 maybe_interpret_subexpr (FcFormatContext
*c
,
181 return (*c
->format
== '{') ?
182 interpret_subexpr (c
, pat
, buf
) :
187 skip_subexpr (FcFormatContext
*c
);
190 skip_percent (FcFormatContext
*c
)
194 if (!expect_char (c
, '%'))
197 /* skip an optional width specifier */
198 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
200 if (!expect_char (c
, '{'))
203 while(*c
->format
&& *c
->format
!= '}')
208 c
->format
++; /* skip over '\\' */
213 if (!skip_subexpr (c
))
220 return expect_char (c
, '}');
224 skip_expr (FcFormatContext
*c
)
226 while(*c
->format
&& *c
->format
!= '}')
231 c
->format
++; /* skip over '\\' */
236 if (!skip_percent (c
))
247 skip_subexpr (FcFormatContext
*c
)
249 return expect_char (c
, '{') &&
251 expect_char (c
, '}');
255 maybe_skip_subexpr (FcFormatContext
*c
)
257 return (*c
->format
== '{') ?
263 interpret_simple (FcFormatContext
*c
,
268 FcBool add_colon
= FcFalse
;
269 FcBool add_elt_name
= FcFalse
;
271 if (consume_char (c
, ':'))
277 if (consume_char (c
, '='))
278 add_elt_name
= FcTrue
;
280 e
= FcPatternObjectFindElt (pat
,
281 FcObjectFromName ((const char *) c
->word
));
287 FcStrBufChar (buf
, ':');
290 FcStrBufString (buf
, c
->word
);
291 FcStrBufChar (buf
, '=');
294 l
= FcPatternEltValues(e
);
295 FcNameUnparseValueList (buf
, l
, '\0');
302 interpret_filter (FcFormatContext
*c
,
309 if (!expect_char (c
, '+'))
312 os
= FcObjectSetCreate ();
318 if (!read_word (c
) ||
319 !FcObjectSetAdd (os
, (const char *) c
->word
))
321 FcObjectSetDestroy (os
);
325 while (consume_char (c
, ','));
327 subpat
= FcPatternFilter (pat
, os
);
328 FcObjectSetDestroy (os
);
331 !interpret_subexpr (c
, subpat
, buf
))
334 FcPatternDestroy (subpat
);
339 interpret_delete (FcFormatContext
*c
,
345 if (!expect_char (c
, '-'))
348 subpat
= FcPatternDuplicate (pat
);
356 FcPatternDestroy (subpat
);
360 FcPatternDel (subpat
, (const char *) c
->word
);
362 while (consume_char (c
, ','));
364 if (!interpret_subexpr (c
, subpat
, buf
))
367 FcPatternDestroy (subpat
);
372 interpret_cond (FcFormatContext
*c
,
378 if (!expect_char (c
, '?'))
388 negate
= consume_char (c
, '!');
395 (FcResultMatch
== FcPatternGet (pat
,
396 (const char *) c
->word
,
399 while (consume_char (c
, ','));
403 if (!interpret_subexpr (c
, pat
, buf
) ||
404 !maybe_skip_subexpr (c
))
409 if (!skip_subexpr (c
) ||
410 !maybe_interpret_subexpr (c
, pat
, buf
))
418 convert (FcFormatContext
*c
,
423 else if (0 == strcmp ((const char *) c
->word
, "downcase"))
424 return FcStrDowncase (str
);
425 else if (0 == strcmp ((const char *) c
->word
, "basename"))
426 return FcStrBasename (str
);
427 else if (0 == strcmp ((const char *) c
->word
, "dirname"))
428 return FcStrDirname (str
);
430 message ("unknown converter \"%s\"",
436 maybe_interpret_converts (FcFormatContext
*c
,
440 while (consume_char (c
, '|'))
445 /* nul-terminate the buffer */
446 FcStrBufChar (buf
, '\0');
449 str
= buf
->buf
+ start
;
451 if (!(new_str
= convert (c
, str
)))
454 /* replace in the buffer */
456 FcStrBufString (buf
, new_str
);
464 align_to_width (FcStrBuf
*buf
,
473 len
= buf
->len
- start
;
477 while (len
++ < -width
)
478 FcStrBufChar (buf
, ' ');
480 else if (len
< width
)
485 while (len
++ < width
)
486 FcStrBufChar (buf
, ' ');
490 memmove (buf
->buf
+ buf
->len
- len
,
491 buf
->buf
+ buf
->len
- width
,
493 memset (buf
->buf
+ buf
->len
- width
,
501 interpret_percent (FcFormatContext
*c
,
508 if (!expect_char (c
, '%'))
511 if (consume_char (c
, '%')) /* "%%" */
513 FcStrBufChar (buf
, '%');
517 /* parse an optional width specifier */
518 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
520 if (!expect_char (c
, '{'))
525 switch (*c
->format
) {
526 case '{': ret
= interpret_subexpr (c
, pat
, buf
); break;
527 case '+': ret
= interpret_filter (c
, pat
, buf
); break;
528 case '-': ret
= interpret_delete (c
, pat
, buf
); break;
529 case '?': ret
= interpret_cond (c
, pat
, buf
); break;
530 default: ret
= interpret_simple (c
, pat
, buf
); break;
534 maybe_interpret_converts (c
, buf
, start
) &&
535 align_to_width (buf
, start
, width
) &&
536 expect_char (c
, '}');
539 static char escaped_char(const char ch
)
542 case 'a': return '\a';
543 case 'b': return '\b';
544 case 'f': return '\f';
545 case 'n': return '\n';
546 case 'r': return '\r';
547 case 't': return '\t';
548 case 'v': return '\v';
554 interpret_expr (FcFormatContext
*c
,
559 while (*c
->format
&& *c
->format
!= term
)
564 c
->format
++; /* skip over '\\' */
566 FcStrBufChar (buf
, escaped_char (*c
->format
++));
569 if (!interpret_percent (c
, pat
, buf
))
573 FcStrBufChar (buf
, *c
->format
++);
579 FcPatternFormat (FcPattern
*pat
, const FcChar8
*format
)
585 FcStrBufInit (&buf
, 0, 0);
586 if (!FcFormatContextInit (&c
, format
))
589 ret
= interpret_expr (&c
, pat
, &buf
, '\0');
593 FcFormatContextDone (&c
);
595 return FcStrBufDone (&buf
);
598 FcStrBufDestroy (&buf
);
604 #include "fcaliastail.h"