]>
git.wh0rd.org - fontconfig.git/blob - src/fcformat.c
2 * Copyright © 2008,2009 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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
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.
31 /* The language is documented in doc/fcformat.fncs
32 * These are the features implemented:
39 * default %{elt:-word}
42 * filter-out %{-elt1,elt2,elt3{expr}}
43 * filter-in %{+elt1,elt2,elt3{expr}}
44 * conditional %{?elt1,elt2,!elt3{}{}}
45 * enumerate %{[]elt1,elt2{expr}}
46 * langset langset enumeration using the same syntax
48 * convert %{elt|conv1|conv2|conv3}
51 * basename FcStrBasename
52 * dirname FcStrDirname
53 * downcase FcStrDowncase
59 * translate translate chars
62 * unparse FcNameUnparse
63 * fcmatch fc-match default
64 * fclist fc-list default
65 * fccat fc-cat default
66 * pkgkit PackageKit package tag format
69 * Some ideas for future syntax extensions:
71 * - verbose builtin that is like FcPatternPrint
72 * - allow indexing subexprs using '%{[idx]elt1,elt2{subexpr}}'
73 * - allow indexing in +, -, ? filtering?
74 * - conditional/filtering/deletion on binding (using '(w)'/'(s)'/'(=)' notation)
78 #define FCCAT_FORMAT "\"%{file|basename|cescape}\" %{index} \"%{-file{%{=unparse|cescape}}}\""
79 #define FCMATCH_FORMAT "%{file:-<unknown filename>|basename}: \"%{family[0]:-<unknown family>}\" \"%{style[0]:-<unknown style>}\""
80 #define FCLIST_FORMAT "%{?file{%{file}: }}%{-file{%{=unparse}}}"
81 #define PKGKIT_FORMAT "%{[]family{font(%{family|downcase|delete( )})\n}}%{[]lang{font(:lang=%{lang|downcase|translate(_,-)})\n}}"
85 message (const char *fmt
, ...)
89 fprintf (stderr
, "Fontconfig: Pattern format error: ");
90 vfprintf (stderr
, fmt
, args
);
91 fprintf (stderr
, ".\n");
96 typedef struct _FcFormatContext
98 const FcChar8
*format_orig
;
99 const FcChar8
*format
;
102 FcBool word_allocated
;
106 FcFormatContextInit (FcFormatContext
*c
,
107 const FcChar8
*format
,
111 c
->format_orig
= c
->format
= format
;
112 c
->format_len
= strlen ((const char *) format
);
114 if (c
->format_len
< scratch_len
)
117 c
->word_allocated
= FcFalse
;
121 c
->word
= malloc (c
->format_len
+ 1);
122 c
->word_allocated
= FcTrue
;
125 return c
->word
!= NULL
;
129 FcFormatContextDone (FcFormatContext
*c
)
131 if (c
&& c
->word_allocated
)
138 consume_char (FcFormatContext
*c
,
141 if (*c
->format
!= term
)
149 expect_char (FcFormatContext
*c
,
152 FcBool res
= consume_char (c
, term
);
155 if (c
->format
== c
->format_orig
+ c
->format_len
)
156 message ("format ended while expecting '%c'",
159 message ("expected '%c' at %d",
160 term
, c
->format
- c
->format_orig
+ 1);
166 FcCharIsPunct (const FcChar8 c
)
185 static char escaped_char(const char ch
)
188 case 'a': return '\a';
189 case 'b': return '\b';
190 case 'f': return '\f';
191 case 'n': return '\n';
192 case 'r': return '\r';
193 case 't': return '\t';
194 case 'v': return '\v';
200 read_word (FcFormatContext
*c
)
208 if (*c
->format
== '\\')
212 *p
++ = escaped_char (*c
->format
++);
215 else if (FcCharIsPunct (*c
->format
))
224 message ("expected identifier at %d",
225 c
->format
- c
->format_orig
+ 1);
233 read_chars (FcFormatContext
*c
,
240 while (*c
->format
&& *c
->format
!= '}' && *c
->format
!= term
)
242 if (*c
->format
== '\\')
246 *p
++ = escaped_char (*c
->format
++);
256 message ("expected character data at %d",
257 c
->format
- c
->format_orig
+ 1);
265 FcPatternFormatToBuf (FcPattern
*pat
,
266 const FcChar8
*format
,
270 interpret_builtin (FcFormatContext
*c
,
277 if (!expect_char (c
, '=') ||
281 /* try simple builtins first */
283 #define BUILTIN(name, func) \
284 else if (0 == strcmp ((const char *) c->word, name))\
285 do { new_str = func (pat); ret = FcTrue; } while (0)
286 BUILTIN ("unparse", FcNameUnparse
);
287 /* BUILTIN ("verbose", FcPatternPrint); XXX */
296 FcStrBufString (buf
, new_str
);
304 /* now try our custom formats */
306 #define BUILTIN(name, format) \
307 else if (0 == strcmp ((const char *) c->word, name))\
308 ret = FcPatternFormatToBuf (pat, (const FcChar8 *) format, buf)
309 BUILTIN ("fccat", FCCAT_FORMAT
);
310 BUILTIN ("fcmatch", FCMATCH_FORMAT
);
311 BUILTIN ("fclist", FCLIST_FORMAT
);
312 BUILTIN ("pkgkit", PKGKIT_FORMAT
);
318 message ("unknown builtin \"%s\"",
325 interpret_expr (FcFormatContext
*c
,
331 interpret_subexpr (FcFormatContext
*c
,
335 return expect_char (c
, '{') &&
336 interpret_expr (c
, pat
, buf
, '}') &&
337 expect_char (c
, '}');
341 maybe_interpret_subexpr (FcFormatContext
*c
,
345 return (*c
->format
== '{') ?
346 interpret_subexpr (c
, pat
, buf
) :
351 skip_subexpr (FcFormatContext
*c
);
354 skip_percent (FcFormatContext
*c
)
356 if (!expect_char (c
, '%'))
359 /* skip an optional width specifier */
360 strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
362 if (!expect_char (c
, '{'))
365 while(*c
->format
&& *c
->format
!= '}')
370 c
->format
++; /* skip over '\\' */
375 if (!skip_subexpr (c
))
382 return expect_char (c
, '}');
386 skip_expr (FcFormatContext
*c
)
388 while(*c
->format
&& *c
->format
!= '}')
393 c
->format
++; /* skip over '\\' */
398 if (!skip_percent (c
))
409 skip_subexpr (FcFormatContext
*c
)
411 return expect_char (c
, '{') &&
413 expect_char (c
, '}');
417 maybe_skip_subexpr (FcFormatContext
*c
)
419 return (*c
->format
== '{') ?
425 interpret_filter_in (FcFormatContext
*c
,
432 if (!expect_char (c
, '+'))
435 os
= FcObjectSetCreate ();
442 if (!read_word (c
) ||
443 !FcObjectSetAdd (os
, (const char *) c
->word
))
445 FcObjectSetDestroy (os
);
449 while (consume_char (c
, ','));
451 subpat
= FcPatternFilter (pat
, os
);
452 FcObjectSetDestroy (os
);
455 !interpret_subexpr (c
, subpat
, buf
))
458 FcPatternDestroy (subpat
);
463 interpret_filter_out (FcFormatContext
*c
,
469 if (!expect_char (c
, '-'))
472 subpat
= FcPatternDuplicate (pat
);
480 FcPatternDestroy (subpat
);
484 FcPatternDel (subpat
, (const char *) c
->word
);
486 while (consume_char (c
, ','));
488 if (!interpret_subexpr (c
, subpat
, buf
))
491 FcPatternDestroy (subpat
);
496 interpret_cond (FcFormatContext
*c
,
502 if (!expect_char (c
, '?'))
512 negate
= consume_char (c
, '!');
520 FcPatternGet (pat
, (const char *) c
->word
, 0, &v
)));
522 while (consume_char (c
, ','));
526 if (!interpret_subexpr (c
, pat
, buf
) ||
527 !maybe_skip_subexpr (c
))
532 if (!skip_subexpr (c
) ||
533 !maybe_interpret_subexpr (c
, pat
, buf
))
541 interpret_count (FcFormatContext
*c
,
547 FcChar8 buf_static
[64];
549 if (!expect_char (c
, '#'))
556 e
= FcPatternObjectFindElt (pat
,
557 FcObjectFromName ((const char *) c
->word
));
562 for (l
= FcPatternEltValues(e
);
568 snprintf ((char *) buf_static
, sizeof (buf_static
), "%d", count
);
569 FcStrBufString (buf
, buf_static
);
575 interpret_enumerate (FcFormatContext
*c
,
581 const FcChar8
*format_save
;
584 FcStrList
*lang_strs
;
586 if (!expect_char (c
, '[') ||
587 !expect_char (c
, ']'))
590 os
= FcObjectSetCreate ();
598 if (!read_word (c
) ||
599 !FcObjectSetAdd (os
, (const char *) c
->word
))
601 FcObjectSetDestroy (os
);
605 while (consume_char (c
, ','));
607 /* If we have one element and it's of type FcLangSet, we want
608 * to enumerate the languages in it. */
610 if (os
->nobject
== 1)
614 FcPatternGetLangSet (pat
, os
->objects
[0], 0, &langset
))
617 if (!(ss
= FcLangSetGetLangs (langset
)) ||
618 !(lang_strs
= FcStrListCreate (ss
)))
623 subpat
= FcPatternDuplicate (pat
);
627 format_save
= c
->format
;
639 FcPatternDel (subpat
, os
->objects
[0]);
640 if ((lang
= FcStrListNext (lang_strs
)))
643 FcPatternAddString (subpat
, os
->objects
[0], lang
);
649 for (i
= 0; i
< os
->nobject
; i
++)
653 /* XXX this can be optimized by accessing valuelist linked lists
654 * directly and remembering where we were. Most (all) value lists
655 * in normal uses are pretty short though (language tags are
656 * stored as a LangSet, not separate values.). */
657 FcPatternDel (subpat
, os
->objects
[i
]);
659 FcPatternGet (pat
, os
->objects
[i
], idx
, &v
))
662 FcPatternAdd (subpat
, os
->objects
[i
], v
, FcFalse
);
670 c
->format
= format_save
;
671 ret
= interpret_subexpr (c
, subpat
, buf
);
679 if (c
->format
== format_save
)
683 FcPatternDestroy (subpat
);
686 FcStrListDone (lang_strs
);
687 FcObjectSetDestroy (os
);
693 interpret_simple (FcFormatContext
*c
,
698 FcBool add_colon
= FcFalse
;
699 FcBool add_elt_name
= FcFalse
;
701 FcChar8
*else_string
;
703 if (consume_char (c
, ':'))
710 if (consume_char (c
, '['))
712 idx
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
715 message ("expected non-negative number at %d",
716 c
->format
-1 - c
->format_orig
+ 1);
719 if (!expect_char (c
, ']'))
723 if (consume_char (c
, '='))
724 add_elt_name
= FcTrue
;
728 if (consume_char (c
, ':'))
731 /* divert the c->word for now */
733 c
->word
= c
->word
+ strlen ((const char *) c
->word
) + 1;
734 /* for now we just support 'default value' */
735 if (!expect_char (c
, '-') ||
736 !read_chars (c
, '|'))
741 else_string
= c
->word
;
745 e
= FcPatternObjectFindElt (pat
,
746 FcObjectFromName ((const char *) c
->word
));
747 if (e
|| else_string
)
749 FcValueListPtr l
= NULL
;
752 FcStrBufChar (buf
, ':');
755 FcStrBufString (buf
, c
->word
);
756 FcStrBufChar (buf
, '=');
760 l
= FcPatternEltValues(e
);
766 l
= FcValueListNext(l
);
771 if (!FcNameUnparseValue (buf
, &l
->value
, '\0'))
778 FcNameUnparseValueList (buf
, l
, '\0');
784 FcStrBufString (buf
, else_string
);
792 cescape (FcFormatContext
*c
,
796 /* XXX escape \n etc? */
804 FcStrBufChar (buf
, '\\');
807 FcStrBufChar (buf
, *str
++);
813 shescape (FcFormatContext
*c
,
817 FcStrBufChar (buf
, '\'');
821 FcStrBufString (buf
, (const FcChar8
*) "'\\''");
823 FcStrBufChar (buf
, *str
);
826 FcStrBufChar (buf
, '\'');
831 xmlescape (FcFormatContext
*c
,
835 /* XXX escape \n etc? */
841 case '&': FcStrBufString (buf
, (const FcChar8
*) "&"); break;
842 case '<': FcStrBufString (buf
, (const FcChar8
*) "<"); break;
843 case '>': FcStrBufString (buf
, (const FcChar8
*) ">"); break;
844 default: FcStrBufChar (buf
, *str
); break;
852 delete_chars (FcFormatContext
*c
,
856 /* XXX not UTF-8 aware */
858 if (!expect_char (c
, '(') ||
859 !read_chars (c
, ')') ||
860 !expect_char (c
, ')'))
867 p
= (FcChar8
*) strpbrk ((const char *) str
, (const char *) c
->word
);
870 FcStrBufData (buf
, str
, p
- str
);
875 FcStrBufString (buf
, str
);
885 escape_chars (FcFormatContext
*c
,
889 /* XXX not UTF-8 aware */
891 if (!expect_char (c
, '(') ||
892 !read_chars (c
, ')') ||
893 !expect_char (c
, ')'))
900 p
= (FcChar8
*) strpbrk ((const char *) str
, (const char *) c
->word
);
903 FcStrBufData (buf
, str
, p
- str
);
904 FcStrBufChar (buf
, c
->word
[0]);
905 FcStrBufChar (buf
, *p
);
910 FcStrBufString (buf
, str
);
920 translate_chars (FcFormatContext
*c
,
924 char *from
, *to
, repeat
;
925 int from_len
, to_len
;
927 /* XXX not UTF-8 aware */
929 if (!expect_char (c
, '(') ||
930 !read_chars (c
, ',') ||
931 !expect_char (c
, ','))
934 from
= (char *) c
->word
;
935 from_len
= strlen (from
);
936 to
= from
+ from_len
+ 1;
938 /* hack: we temporarily divert c->word */
939 c
->word
= (FcChar8
*) to
;
940 if (!read_chars (c
, ')'))
942 c
->word
= (FcChar8
*) from
;
945 c
->word
= (FcChar8
*) from
;
947 to_len
= strlen (to
);
948 repeat
= to
[to_len
- 1];
950 if (!expect_char (c
, ')'))
957 p
= (FcChar8
*) strpbrk ((const char *) str
, (const char *) from
);
961 FcStrBufData (buf
, str
, p
- str
);
962 i
= strchr (from
, *p
) - from
;
963 FcStrBufChar (buf
, i
< to_len
? to
[i
] : repeat
);
968 FcStrBufString (buf
, str
);
978 interpret_convert (FcFormatContext
*c
,
985 FcChar8 buf_static
[8192];
988 if (!expect_char (c
, '|') ||
992 /* prepare the buffer */
993 FcStrBufChar (buf
, '\0');
996 str
= buf
->buf
+ start
;
999 /* try simple converters first */
1001 #define CONVERTER(name, func) \
1002 else if (0 == strcmp ((const char *) c->word, name))\
1003 do { new_str = func (str); ret = FcTrue; } while (0)
1004 CONVERTER ("downcase", FcStrDowncase
);
1005 CONVERTER ("basename", FcStrBasename
);
1006 CONVERTER ("dirname", FcStrDirname
);
1015 FcStrBufString (buf
, new_str
);
1023 FcStrBufInit (&new_buf
, buf_static
, sizeof (buf_static
));
1025 /* now try our custom converters */
1027 #define CONVERTER(name, func) \
1028 else if (0 == strcmp ((const char *) c->word, name))\
1029 ret = func (c, str, &new_buf)
1030 CONVERTER ("cescape", cescape
);
1031 CONVERTER ("shescape", shescape
);
1032 CONVERTER ("xmlescape", xmlescape
);
1033 CONVERTER ("delete", delete_chars
);
1034 CONVERTER ("escape", escape_chars
);
1035 CONVERTER ("translate", translate_chars
);
1042 FcStrBufChar (&new_buf
, '\0');
1043 FcStrBufString (buf
, new_buf
.buf
);
1046 message ("unknown converter \"%s\"",
1049 FcStrBufDestroy (&new_buf
);
1055 maybe_interpret_converts (FcFormatContext
*c
,
1059 while (*c
->format
== '|')
1060 if (!interpret_convert (c
, buf
, start
))
1067 align_to_width (FcStrBuf
*buf
,
1076 len
= buf
->len
- start
;
1080 while (len
++ < -width
)
1081 FcStrBufChar (buf
, ' ');
1083 else if (len
< width
)
1088 while (len
++ < width
)
1089 FcStrBufChar (buf
, ' ');
1093 memmove (buf
->buf
+ buf
->len
- len
,
1094 buf
->buf
+ buf
->len
- width
,
1096 memset (buf
->buf
+ buf
->len
- width
,
1101 return !buf
->failed
;
1104 interpret_percent (FcFormatContext
*c
,
1111 if (!expect_char (c
, '%'))
1114 if (consume_char (c
, '%')) /* "%%" */
1116 FcStrBufChar (buf
, '%');
1120 /* parse an optional width specifier */
1121 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
1123 if (!expect_char (c
, '{'))
1128 switch (*c
->format
) {
1129 case '=': ret
= interpret_builtin (c
, pat
, buf
); break;
1130 case '{': ret
= interpret_subexpr (c
, pat
, buf
); break;
1131 case '+': ret
= interpret_filter_in (c
, pat
, buf
); break;
1132 case '-': ret
= interpret_filter_out (c
, pat
, buf
); break;
1133 case '?': ret
= interpret_cond (c
, pat
, buf
); break;
1134 case '#': ret
= interpret_count (c
, pat
, buf
); break;
1135 case '[': ret
= interpret_enumerate (c
, pat
, buf
); break;
1136 default: ret
= interpret_simple (c
, pat
, buf
); break;
1140 maybe_interpret_converts (c
, buf
, start
) &&
1141 align_to_width (buf
, start
, width
) &&
1142 expect_char (c
, '}');
1146 interpret_expr (FcFormatContext
*c
,
1151 while (*c
->format
&& *c
->format
!= term
)
1156 c
->format
++; /* skip over '\\' */
1158 FcStrBufChar (buf
, escaped_char (*c
->format
++));
1161 if (!interpret_percent (c
, pat
, buf
))
1165 FcStrBufChar (buf
, *c
->format
++);
1171 FcPatternFormatToBuf (FcPattern
*pat
,
1172 const FcChar8
*format
,
1176 FcChar8 word_static
[1024];
1179 if (!FcFormatContextInit (&c
, format
, word_static
, sizeof (word_static
)))
1182 ret
= interpret_expr (&c
, pat
, buf
, '\0');
1184 FcFormatContextDone (&c
);
1190 FcPatternFormat (FcPattern
*pat
,
1191 const FcChar8
*format
)
1194 FcChar8 buf_static
[8192 - 1024];
1197 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
1199 ret
= FcPatternFormatToBuf (pat
, format
, &buf
);
1202 return FcStrBufDone (&buf
);
1205 FcStrBufDestroy (&buf
);
1210 #define __fcformat__
1211 #include "fcaliastail.h"