]> git.wh0rd.org - fontconfig.git/blob - src/fcformat.c
99c4f858fcd31cd4c8643739863f899f2363d35d
[fontconfig.git] / src / fcformat.c
1 /*
2 * Copyright © 2008,2009 Red Hat, Inc.
3 *
4 * Red Hat Author(s): Behdad Esfahbod
5 *
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.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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.
23 */
24
25 #include "fcint.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdarg.h>
29
30
31 /* XXX Document the language.
32 *
33 * These are mostly the features implemented but not documented:
34 *
35 * width %[[-]width]{tag}
36 * index %{tag[ids]}
37 * name= %{tag=|decorator}
38 * :name= %{:tag=|decorator}
39 * subexpr %{{expr}|decorator1|decorator2}
40 * delete %{-charset,lang{expr}|decorator}
41 * filter %{+family,familylang{expr}|decorator}
42 * cond %{?tag1,tag2,!tag3{}{}}
43 * decorat %{tag|decorator1|decorator2}
44 * default %{parameter:-word}
45 * array %{[]family,familylang{expr}|decorator}
46 * langset enumeration using the same syntax as arrays
47 *
48 * filters:
49 * basename FcStrBasename
50 * dirname FcStrDirname
51 * downcase FcStrDowncase
52 * shescape
53 * cescape
54 * xmlescape
55 * delete delete chars
56 * escape escape chars
57 * translate translate chars
58 *
59 * builtins:
60 * unparse
61 * fcmatch
62 * fclist
63 * pkgkit
64 */
65
66 /*
67 * Some ideas for future syntax extensions:
68 *
69 * - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
70 * - conditional/filtering/deletion on binding (using '(w)'/'(s)'/'(=)' notation)
71 */
72
73
74 #define FCMATCH_FORMAT "%{file:-<unknown filename>|basename}: \"%{family[0]:-<unknown family>}\" \"%{style[0]:-<unknown style>}\""
75 #define FCLIST_FORMAT "%{?file{%{file}: }}%{=unparse}"
76 #define PKGKIT_FORMAT "%{[]family{font(%{family|downcase|delete( )})\n}}%{[]lang{font(:lang=%{lang|downcase|translate(_,-)})\n}}"
77
78
79 static void
80 message (const char *fmt, ...)
81 {
82 va_list args;
83 va_start (args, fmt);
84 fprintf (stderr, "Fontconfig: Pattern format error: ");
85 vfprintf (stderr, fmt, args);
86 fprintf (stderr, ".\n");
87 va_end (args);
88 }
89
90
91 typedef struct _FcFormatContext
92 {
93 const FcChar8 *format_orig;
94 const FcChar8 *format;
95 int format_len;
96 FcChar8 *word;
97 FcBool word_allocated;
98 } FcFormatContext;
99
100 static FcBool
101 FcFormatContextInit (FcFormatContext *c,
102 const FcChar8 *format,
103 FcChar8 *scratch,
104 int scratch_len)
105 {
106 c->format_orig = c->format = format;
107 c->format_len = strlen ((const char *) format);
108
109 if (c->format_len < scratch_len)
110 {
111 c->word = scratch;
112 c->word_allocated = FcFalse;
113 }
114 else
115 {
116 c->word = malloc (c->format_len + 1);
117 c->word_allocated = FcTrue;
118 }
119
120 return c->word != NULL;
121 }
122
123 static void
124 FcFormatContextDone (FcFormatContext *c)
125 {
126 if (c && c->word_allocated)
127 {
128 free (c->word);
129 }
130 }
131
132 static FcBool
133 consume_char (FcFormatContext *c,
134 FcChar8 term)
135 {
136 if (*c->format != term)
137 return FcFalse;
138
139 c->format++;
140 return FcTrue;
141 }
142
143 static FcBool
144 expect_char (FcFormatContext *c,
145 FcChar8 term)
146 {
147 FcBool res = consume_char (c, term);
148 if (!res)
149 {
150 if (c->format == c->format_orig + c->format_len)
151 message ("format ended while expecting '%c'",
152 term);
153 else
154 message ("expected '%c' at %d",
155 term, c->format - c->format_orig + 1);
156 }
157 return res;
158 }
159
160 static FcBool
161 FcCharIsPunct (const FcChar8 c)
162 {
163 if (c < '0')
164 return FcTrue;
165 if (c <= '9')
166 return FcFalse;
167 if (c < 'A')
168 return FcTrue;
169 if (c <= 'Z')
170 return FcFalse;
171 if (c < 'a')
172 return FcTrue;
173 if (c <= 'z')
174 return FcFalse;
175 if (c <= '~')
176 return FcTrue;
177 return FcFalse;
178 }
179
180 static char escaped_char(const char ch)
181 {
182 switch (ch) {
183 case 'a': return '\a';
184 case 'b': return '\b';
185 case 'f': return '\f';
186 case 'n': return '\n';
187 case 'r': return '\r';
188 case 't': return '\t';
189 case 'v': return '\v';
190 default: return ch;
191 }
192 }
193
194 static FcBool
195 read_word (FcFormatContext *c)
196 {
197 FcChar8 *p;
198
199 p = c->word;
200
201 while (*c->format)
202 {
203 if (*c->format == '\\')
204 {
205 c->format++;
206 if (*c->format)
207 *p++ = escaped_char (*c->format++);
208 continue;
209 }
210 else if (FcCharIsPunct (*c->format))
211 break;
212
213 *p++ = *c->format++;
214 }
215 *p = '\0';
216
217 if (p == c->word)
218 {
219 message ("expected identifier at %d",
220 c->format - c->format_orig + 1);
221 return FcFalse;
222 }
223
224 return FcTrue;
225 }
226
227 static FcBool
228 read_chars (FcFormatContext *c,
229 FcChar8 term)
230 {
231 FcChar8 *p;
232
233 p = c->word;
234
235 while (*c->format && *c->format != '}' && *c->format != term)
236 {
237 if (*c->format == '\\')
238 {
239 c->format++;
240 if (*c->format)
241 *p++ = escaped_char (*c->format++);
242 continue;
243 }
244
245 *p++ = *c->format++;
246 }
247 *p = '\0';
248
249 if (p == c->word)
250 {
251 message ("expected character data at %d",
252 c->format - c->format_orig + 1);
253 return FcFalse;
254 }
255
256 return FcTrue;
257 }
258
259 static FcBool
260 FcPatternFormatToBuf (FcPattern *pat,
261 const FcChar8 *format,
262 FcStrBuf *buf);
263
264 static FcBool
265 interpret_builtin (FcFormatContext *c,
266 FcPattern *pat,
267 FcStrBuf *buf)
268 {
269 FcChar8 *new_str;
270 FcBool ret;
271
272 if (!expect_char (c, '=') ||
273 !read_word (c))
274 return FcFalse;
275
276 /* try simple builtins first */
277 if (0) { }
278 #define BUILTIN(name, func) \
279 else if (0 == strcmp ((const char *) c->word, name))\
280 do { new_str = func (pat); ret = FcTrue; } while (0)
281 BUILTIN ("unparse", FcNameUnparse);
282 /* BUILTIN ("verbose", FcPatternPrint); XXX */
283 #undef BUILTIN
284 else
285 ret = FcFalse;
286
287 if (ret)
288 {
289 if (new_str)
290 {
291 FcStrBufString (buf, new_str);
292 free (new_str);
293 return FcTrue;
294 }
295 else
296 return FcFalse;
297 }
298
299 /* now try our custom formats */
300 if (0) { }
301 #define BUILTIN(name, format) \
302 else if (0 == strcmp ((const char *) c->word, name))\
303 ret = FcPatternFormatToBuf (pat, (const FcChar8 *) format, buf)
304 BUILTIN ("fcmatch", FCMATCH_FORMAT);
305 BUILTIN ("fclist", FCLIST_FORMAT);
306 BUILTIN ("pkgkit", PKGKIT_FORMAT);
307 #undef BUILTIN
308 else
309 ret = FcFalse;
310
311 if (!ret)
312 message ("unknown builtin \"%s\"",
313 c->word);
314
315 return ret;
316 }
317
318 static FcBool
319 interpret_expr (FcFormatContext *c,
320 FcPattern *pat,
321 FcStrBuf *buf,
322 FcChar8 term);
323
324 static FcBool
325 interpret_subexpr (FcFormatContext *c,
326 FcPattern *pat,
327 FcStrBuf *buf)
328 {
329 return expect_char (c, '{') &&
330 interpret_expr (c, pat, buf, '}') &&
331 expect_char (c, '}');
332 }
333
334 static FcBool
335 maybe_interpret_subexpr (FcFormatContext *c,
336 FcPattern *pat,
337 FcStrBuf *buf)
338 {
339 return (*c->format == '{') ?
340 interpret_subexpr (c, pat, buf) :
341 FcTrue;
342 }
343
344 static FcBool
345 skip_subexpr (FcFormatContext *c);
346
347 static FcBool
348 skip_percent (FcFormatContext *c)
349 {
350 int width;
351
352 if (!expect_char (c, '%'))
353 return FcFalse;
354
355 /* skip an optional width specifier */
356 width = strtol ((const char *) c->format, (char **) &c->format, 10);
357
358 if (!expect_char (c, '{'))
359 return FcFalse;
360
361 while(*c->format && *c->format != '}')
362 {
363 switch (*c->format)
364 {
365 case '\\':
366 c->format++; /* skip over '\\' */
367 if (*c->format)
368 c->format++;
369 continue;
370 case '{':
371 if (!skip_subexpr (c))
372 return FcFalse;
373 continue;
374 }
375 c->format++;
376 }
377
378 return expect_char (c, '}');
379 }
380
381 static FcBool
382 skip_expr (FcFormatContext *c)
383 {
384 while(*c->format && *c->format != '}')
385 {
386 switch (*c->format)
387 {
388 case '\\':
389 c->format++; /* skip over '\\' */
390 if (*c->format)
391 c->format++;
392 continue;
393 case '%':
394 if (!skip_percent (c))
395 return FcFalse;
396 continue;
397 }
398 c->format++;
399 }
400
401 return FcTrue;
402 }
403
404 static FcBool
405 skip_subexpr (FcFormatContext *c)
406 {
407 return expect_char (c, '{') &&
408 skip_expr (c) &&
409 expect_char (c, '}');
410 }
411
412 static FcBool
413 maybe_skip_subexpr (FcFormatContext *c)
414 {
415 return (*c->format == '{') ?
416 skip_subexpr (c) :
417 FcTrue;
418 }
419
420 static FcBool
421 interpret_filter (FcFormatContext *c,
422 FcPattern *pat,
423 FcStrBuf *buf)
424 {
425 FcObjectSet *os;
426 FcPattern *subpat;
427
428 if (!expect_char (c, '+'))
429 return FcFalse;
430
431 os = FcObjectSetCreate ();
432 if (!os)
433 return FcFalse;
434
435 do
436 {
437 if (!read_word (c) ||
438 !FcObjectSetAdd (os, (const char *) c->word))
439 {
440 FcObjectSetDestroy (os);
441 return FcFalse;
442 }
443 }
444 while (consume_char (c, ','));
445
446 subpat = FcPatternFilter (pat, os);
447 FcObjectSetDestroy (os);
448
449 if (!subpat ||
450 !interpret_subexpr (c, subpat, buf))
451 return FcFalse;
452
453 FcPatternDestroy (subpat);
454 return FcTrue;
455 }
456
457 static FcBool
458 interpret_delete (FcFormatContext *c,
459 FcPattern *pat,
460 FcStrBuf *buf)
461 {
462 FcPattern *subpat;
463
464 if (!expect_char (c, '-'))
465 return FcFalse;
466
467 subpat = FcPatternDuplicate (pat);
468 if (!subpat)
469 return FcFalse;
470
471 do
472 {
473 if (!read_word (c))
474 {
475 FcPatternDestroy (subpat);
476 return FcFalse;
477 }
478
479 FcPatternDel (subpat, (const char *) c->word);
480 }
481 while (consume_char (c, ','));
482
483 if (!interpret_subexpr (c, subpat, buf))
484 return FcFalse;
485
486 FcPatternDestroy (subpat);
487 return FcTrue;
488 }
489
490 static FcBool
491 interpret_cond (FcFormatContext *c,
492 FcPattern *pat,
493 FcStrBuf *buf)
494 {
495 FcBool pass;
496
497 if (!expect_char (c, '?'))
498 return FcFalse;
499
500 pass = FcTrue;
501
502 do
503 {
504 FcBool negate;
505 FcValue v;
506
507 negate = consume_char (c, '!');
508
509 if (!read_word (c))
510 return FcFalse;
511
512 pass = pass &&
513 (negate ^
514 (FcResultMatch ==
515 FcPatternGet (pat, (const char *) c->word, 0, &v)));
516 }
517 while (consume_char (c, ','));
518
519 if (pass)
520 {
521 if (!interpret_subexpr (c, pat, buf) ||
522 !maybe_skip_subexpr (c))
523 return FcFalse;
524 }
525 else
526 {
527 if (!skip_subexpr (c) ||
528 !maybe_interpret_subexpr (c, pat, buf))
529 return FcFalse;
530 }
531
532 return FcTrue;
533 }
534
535 static FcBool
536 interpret_count (FcFormatContext *c,
537 FcPattern *pat,
538 FcStrBuf *buf)
539 {
540 int count;
541 FcPatternElt *e;
542 FcChar8 buf_static[64];
543
544 if (!expect_char (c, '#'))
545 return FcFalse;
546
547 if (!read_word (c))
548 return FcFalse;
549
550 count = 0;
551 e = FcPatternObjectFindElt (pat,
552 FcObjectFromName ((const char *) c->word));
553 if (e)
554 {
555 FcValueListPtr l;
556 count++;
557 for (l = FcPatternEltValues(e);
558 l->next;
559 l = l->next)
560 count++;
561 }
562
563 snprintf ((char *) buf_static, sizeof (buf_static), "%d", count);
564 FcStrBufString (buf, buf_static);
565
566 return FcTrue;
567 }
568
569 static FcBool
570 interpret_array (FcFormatContext *c,
571 FcPattern *pat,
572 FcStrBuf *buf)
573 {
574 FcObjectSet *os;
575 FcPattern *subpat;
576 const FcChar8 *format_save;
577 int idx;
578 FcBool ret, done;
579 FcStrList *lang_strs;
580
581 if (!expect_char (c, '[') ||
582 !expect_char (c, ']'))
583 return FcFalse;
584
585 os = FcObjectSetCreate ();
586 if (!os)
587 return FcFalse;
588
589 ret = FcTrue;
590
591 do
592 {
593 if (!read_word (c) ||
594 !FcObjectSetAdd (os, (const char *) c->word))
595 {
596 FcObjectSetDestroy (os);
597 return FcFalse;
598 }
599 }
600 while (consume_char (c, ','));
601
602 /* If we have one element and it's of type FcLangSet, we want
603 * to enumerate the languages in it. */
604 lang_strs = NULL;
605 if (os->nobject == 1)
606 {
607 FcLangSet *langset;
608 if (FcResultMatch ==
609 FcPatternGetLangSet (pat, os->objects[0], idx, &langset))
610 {
611 FcStrSet *ss;
612 if (!(ss = FcLangSetGetLangs (langset)) ||
613 !(lang_strs = FcStrListCreate (ss)))
614 goto bail0;
615 }
616 }
617
618 subpat = FcPatternDuplicate (pat);
619 if (!subpat)
620 goto bail0;
621
622 format_save = c->format;
623 idx = 0;
624 do
625 {
626 int i;
627
628 done = FcTrue;
629
630 if (lang_strs)
631 {
632 FcChar8 *lang;
633
634 FcPatternDel (subpat, os->objects[0]);
635 if ((lang = FcStrListNext (lang_strs)))
636 {
637 FcPatternAddString (subpat, os->objects[0], lang);
638 done = FcFalse;
639 }
640 }
641 else
642 {
643 for (i = 0; i < os->nobject; i++)
644 {
645 FcValue v;
646
647 /* XXX this can be optimized by accessing valuelist linked lists
648 * directly and remembering where we were. Most (all) value lists
649 * in normal uses are pretty short though (language tags are
650 * stored as a LangSet, not separate values.). */
651 FcPatternDel (subpat, os->objects[i]);
652 if (FcResultMatch ==
653 FcPatternGet (pat, os->objects[i], idx, &v))
654 {
655 FcPatternAdd (subpat, os->objects[i], v, FcFalse);
656 done = FcFalse;
657 }
658 }
659 }
660
661 if (!done)
662 {
663 c->format = format_save;
664 ret = interpret_subexpr (c, subpat, buf);
665 if (!ret)
666 goto bail;
667 }
668
669 idx++;
670 } while (!done);
671
672 if (c->format == format_save)
673 skip_subexpr (c);
674
675 bail:
676 FcPatternDestroy (subpat);
677 bail0:
678 if (lang_strs)
679 FcStrListDone (lang_strs);
680 FcObjectSetDestroy (os);
681
682 return ret;
683 }
684
685 static FcBool
686 interpret_simple (FcFormatContext *c,
687 FcPattern *pat,
688 FcStrBuf *buf)
689 {
690 FcPatternElt *e;
691 FcBool add_colon = FcFalse;
692 FcBool add_elt_name = FcFalse;
693 int idx;
694 FcChar8 *else_string;
695
696 if (consume_char (c, ':'))
697 add_colon = FcTrue;
698
699 if (!read_word (c))
700 return FcFalse;
701
702 idx = -1;
703 if (consume_char (c, '['))
704 {
705 idx = strtol ((const char *) c->format, (char **) &c->format, 10);
706 if (idx < 0)
707 {
708 message ("expected non-negative number at %d",
709 c->format-1 - c->format_orig + 1);
710 return FcFalse;
711 }
712 if (!expect_char (c, ']'))
713 return FcFalse;
714 }
715
716 if (consume_char (c, '='))
717 add_elt_name = FcTrue;
718
719 /* modifiers */
720 else_string = NULL;
721 if (consume_char (c, ':'))
722 {
723 FcChar8 *orig;
724 /* divert the c->word for now */
725 orig = c->word;
726 c->word = c->word + strlen ((const char *) c->word) + 1;
727 /* for now we just support 'default value' */
728 if (!expect_char (c, '-') ||
729 !read_chars (c, '\0'))
730 {
731 c->word = orig;
732 return FcFalse;
733 }
734 else_string = c->word;
735 c->word = orig;
736 }
737
738 e = FcPatternObjectFindElt (pat,
739 FcObjectFromName ((const char *) c->word));
740 if (e)
741 {
742 FcValueListPtr l;
743
744 if (add_colon)
745 FcStrBufChar (buf, ':');
746 if (add_elt_name)
747 {
748 FcStrBufString (buf, c->word);
749 FcStrBufChar (buf, '=');
750 }
751
752 l = FcPatternEltValues(e);
753
754 if (idx != -1)
755 {
756 while (l && idx > 0)
757 {
758 l = FcValueListNext(l);
759 idx--;
760 }
761 if (l && idx == 0)
762 {
763 if (!FcNameUnparseValue (buf, &l->value, '\0'))
764 return FcFalse;
765 }
766 else goto notfound;
767 }
768 else
769 {
770 FcNameUnparseValueList (buf, l, '\0');
771 }
772 }
773 else
774 notfound:
775 {
776 if (else_string)
777 printf ("%s", else_string);
778 }
779
780 return FcTrue;
781 }
782
783 static FcBool
784 cescape (FcFormatContext *c,
785 const FcChar8 *str,
786 FcStrBuf *buf)
787 {
788 while(*str)
789 {
790 switch (*str)
791 {
792 case '\\':
793 case '"':
794 FcStrBufChar (buf, '\\');
795 break;
796 }
797 FcStrBufChar (buf, *str++);
798 }
799 return FcTrue;
800 }
801
802 static FcBool
803 shescape (FcFormatContext *c,
804 const FcChar8 *str,
805 FcStrBuf *buf)
806 {
807 FcStrBufChar (buf, '\'');
808 while(*str)
809 {
810 if (*str == '\'')
811 FcStrBufString (buf, (const FcChar8 *) "'\\''");
812 else
813 FcStrBufChar (buf, *str);
814 str++;
815 }
816 FcStrBufChar (buf, '\'');
817 return FcTrue;
818 }
819
820 static FcBool
821 xmlescape (FcFormatContext *c,
822 const FcChar8 *str,
823 FcStrBuf *buf)
824 {
825 while(*str)
826 {
827 switch (*str)
828 {
829 case '&': FcStrBufString (buf, (const FcChar8 *) "&amp;"); break;
830 case '<': FcStrBufString (buf, (const FcChar8 *) "&lt;"); break;
831 case '>': FcStrBufString (buf, (const FcChar8 *) "&gt;"); break;
832 default: FcStrBufChar (buf, *str); break;
833 }
834 str++;
835 }
836 return FcTrue;
837 }
838
839 static FcBool
840 delete_chars (FcFormatContext *c,
841 const FcChar8 *str,
842 FcStrBuf *buf)
843 {
844 /* XXX not UTF-8 aware */
845
846 if (!expect_char (c, '(') ||
847 !read_chars (c, ')') ||
848 !expect_char (c, ')'))
849 return FcFalse;
850
851 while(*str)
852 {
853 FcChar8 *p;
854
855 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
856 if (p)
857 {
858 FcStrBufData (buf, str, p - str);
859 str = p + 1;
860 }
861 else
862 {
863 FcStrBufString (buf, str);
864 break;
865 }
866
867 }
868
869 return FcTrue;
870 }
871
872 static FcBool
873 escape_chars (FcFormatContext *c,
874 const FcChar8 *str,
875 FcStrBuf *buf)
876 {
877 /* XXX not UTF-8 aware */
878
879 if (!expect_char (c, '(') ||
880 !read_chars (c, ')') ||
881 !expect_char (c, ')'))
882 return FcFalse;
883
884 while(*str)
885 {
886 FcChar8 *p;
887
888 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) c->word);
889 if (p)
890 {
891 FcStrBufData (buf, str, p - str);
892 FcStrBufChar (buf, c->word[0]);
893 FcStrBufChar (buf, *p);
894 str = p + 1;
895 }
896 else
897 {
898 FcStrBufString (buf, str);
899 break;
900 }
901
902 }
903
904 return FcTrue;
905 }
906
907 static FcBool
908 translate_chars (FcFormatContext *c,
909 const FcChar8 *str,
910 FcStrBuf *buf)
911 {
912 char *from, *to, repeat;
913 int from_len, to_len;
914
915 /* XXX not UTF-8 aware */
916
917 if (!expect_char (c, '(') ||
918 !read_chars (c, ',') ||
919 !expect_char (c, ','))
920 return FcFalse;
921
922 from = (char *) c->word;
923 from_len = strlen (from);
924 to = from + from_len + 1;
925
926 /* hack: we temporarily divert c->word */
927 c->word = (FcChar8 *) to;
928 if (!read_chars (c, ')'))
929 {
930 c->word = (FcChar8 *) from;
931 return FcFalse;
932 }
933 c->word = (FcChar8 *) from;
934
935 to_len = strlen (to);
936 repeat = to[to_len - 1];
937
938 if (!expect_char (c, ')'))
939 return FcFalse;
940
941 while(*str)
942 {
943 FcChar8 *p;
944
945 p = (FcChar8 *) strpbrk ((const char *) str, (const char *) from);
946 if (p)
947 {
948 int i;
949 FcStrBufData (buf, str, p - str);
950 i = strchr (from, *p) - from;
951 FcStrBufChar (buf, i < to_len ? to[i] : repeat);
952 str = p + 1;
953 }
954 else
955 {
956 FcStrBufString (buf, str);
957 break;
958 }
959
960 }
961
962 return FcTrue;
963 }
964
965 static FcBool
966 interpret_convert (FcFormatContext *c,
967 FcStrBuf *buf,
968 int start)
969 {
970 const FcChar8 *str;
971 FcChar8 *new_str;
972 FcStrBuf new_buf;
973 FcChar8 buf_static[8192];
974 FcBool ret;
975
976 if (!expect_char (c, '|') ||
977 !read_word (c))
978 return FcFalse;
979
980 /* prepare the buffer */
981 FcStrBufChar (buf, '\0');
982 if (buf->failed)
983 return FcFalse;
984 str = buf->buf + start;
985 buf->len = start;
986
987 /* try simple converters first */
988 if (0) { }
989 #define CONVERTER(name, func) \
990 else if (0 == strcmp ((const char *) c->word, name))\
991 do { new_str = func (str); ret = FcTrue; } while (0)
992 CONVERTER ("downcase", FcStrDowncase);
993 CONVERTER ("basename", FcStrBasename);
994 CONVERTER ("dirname", FcStrDirname);
995 #undef CONVERTER
996 else
997 ret = FcFalse;
998
999 if (ret)
1000 {
1001 if (new_str)
1002 {
1003 FcStrBufString (buf, new_str);
1004 free (new_str);
1005 return FcTrue;
1006 }
1007 else
1008 return FcFalse;
1009 }
1010
1011 FcStrBufInit (&new_buf, buf_static, sizeof (buf_static));
1012
1013 /* now try our custom converters */
1014 if (0) { }
1015 #define CONVERTER(name, func) \
1016 else if (0 == strcmp ((const char *) c->word, name))\
1017 ret = func (c, str, &new_buf)
1018 CONVERTER ("cescape", cescape);
1019 CONVERTER ("shescape", shescape);
1020 CONVERTER ("xmlescape", xmlescape);
1021 CONVERTER ("delete", delete_chars);
1022 CONVERTER ("escape", escape_chars);
1023 CONVERTER ("translate", translate_chars);
1024 #undef CONVERTER
1025 else
1026 ret = FcFalse;
1027
1028 if (ret)
1029 {
1030 FcStrBufChar (&new_buf, '\0');
1031 FcStrBufString (buf, new_buf.buf);
1032 }
1033 else
1034 message ("unknown converter \"%s\"",
1035 c->word);
1036
1037 FcStrBufDestroy (&new_buf);
1038
1039 return ret;
1040 }
1041
1042 static FcBool
1043 maybe_interpret_converts (FcFormatContext *c,
1044 FcStrBuf *buf,
1045 int start)
1046 {
1047 while (*c->format == '|')
1048 if (!interpret_convert (c, buf, start))
1049 return FcFalse;
1050
1051 return FcTrue;
1052 }
1053
1054 static FcBool
1055 align_to_width (FcStrBuf *buf,
1056 int start,
1057 int width)
1058 {
1059 int len;
1060
1061 if (buf->failed)
1062 return FcFalse;
1063
1064 len = buf->len - start;
1065 if (len < -width)
1066 {
1067 /* left align */
1068 while (len++ < -width)
1069 FcStrBufChar (buf, ' ');
1070 }
1071 else if (len < width)
1072 {
1073 int old_len;
1074 old_len = len;
1075 /* right align */
1076 while (len++ < width)
1077 FcStrBufChar (buf, ' ');
1078 if (buf->failed)
1079 return FcFalse;
1080 len = old_len;
1081 memmove (buf->buf + buf->len - len,
1082 buf->buf + buf->len - width,
1083 len);
1084 memset (buf->buf + buf->len - width,
1085 ' ',
1086 width - len);
1087 }
1088
1089 return !buf->failed;
1090 }
1091 static FcBool
1092 interpret_percent (FcFormatContext *c,
1093 FcPattern *pat,
1094 FcStrBuf *buf)
1095 {
1096 int width, start;
1097 FcBool ret;
1098
1099 if (!expect_char (c, '%'))
1100 return FcFalse;
1101
1102 if (consume_char (c, '%')) /* "%%" */
1103 {
1104 FcStrBufChar (buf, '%');
1105 return FcTrue;
1106 }
1107
1108 /* parse an optional width specifier */
1109 width = strtol ((const char *) c->format, (char **) &c->format, 10);
1110
1111 if (!expect_char (c, '{'))
1112 return FcFalse;
1113
1114 start = buf->len;
1115
1116 switch (*c->format) {
1117 case '=': ret = interpret_builtin (c, pat, buf); break;
1118 case '{': ret = interpret_subexpr (c, pat, buf); break;
1119 case '+': ret = interpret_filter (c, pat, buf); break;
1120 case '-': ret = interpret_delete (c, pat, buf); break;
1121 case '?': ret = interpret_cond (c, pat, buf); break;
1122 case '#': ret = interpret_count (c, pat, buf); break;
1123 case '[': ret = interpret_array (c, pat, buf); break;
1124 default: ret = interpret_simple (c, pat, buf); break;
1125 }
1126
1127 return ret &&
1128 maybe_interpret_converts (c, buf, start) &&
1129 align_to_width (buf, start, width) &&
1130 expect_char (c, '}');
1131 }
1132
1133 static FcBool
1134 interpret_expr (FcFormatContext *c,
1135 FcPattern *pat,
1136 FcStrBuf *buf,
1137 FcChar8 term)
1138 {
1139 while (*c->format && *c->format != term)
1140 {
1141 switch (*c->format)
1142 {
1143 case '\\':
1144 c->format++; /* skip over '\\' */
1145 if (*c->format)
1146 FcStrBufChar (buf, escaped_char (*c->format++));
1147 continue;
1148 case '%':
1149 if (!interpret_percent (c, pat, buf))
1150 return FcFalse;
1151 continue;
1152 }
1153 FcStrBufChar (buf, *c->format++);
1154 }
1155 return FcTrue;
1156 }
1157
1158 static FcBool
1159 FcPatternFormatToBuf (FcPattern *pat,
1160 const FcChar8 *format,
1161 FcStrBuf *buf)
1162 {
1163 FcFormatContext c;
1164 FcChar8 word_static[1024];
1165 FcBool ret;
1166
1167 if (!FcFormatContextInit (&c, format, word_static, sizeof (word_static)))
1168 return FcFalse;
1169
1170 ret = interpret_expr (&c, pat, buf, '\0');
1171
1172 FcFormatContextDone (&c);
1173
1174 return ret;
1175 }
1176
1177 FcChar8 *
1178 FcPatternFormat (FcPattern *pat,
1179 const FcChar8 *format)
1180 {
1181 FcStrBuf buf;
1182 FcChar8 buf_static[8192 - 1024];
1183 FcBool ret;
1184
1185 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
1186
1187 ret = FcPatternFormatToBuf (pat, format, &buf);
1188
1189 if (ret)
1190 return FcStrBufDone (&buf);
1191 else
1192 {
1193 FcStrBufDestroy (&buf);
1194 return NULL;
1195 }
1196 }
1197
1198 #define __fcformat__
1199 #include "fcaliastail.h"
1200 #undef __fcformat__