]>
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 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 * - conditional/filtering/deletion on binding (using '(w)'/'(s)'/'(=)' notation)
39 #define FCMATCH_FORMAT "%{file:-<unknown filename>|basename}: \"%{family[0]:-<unknown family>}\" \"%{style[0]:-<unknown style>}\""
40 #define FCLIST_FORMAT "%{?file{%{file}: }}%{=unparse}"
41 #define PKGKIT_FORMAT "%{[]family{font(%{family|downcase|delete( )})\n}}%{[]lang{font(:lang=%{lang|downcase|translate(_,-)})\n}}"
45 message (const char *fmt
, ...)
49 fprintf (stderr
, "Fontconfig: Pattern format error: ");
50 vfprintf (stderr
, fmt
, args
);
51 fprintf (stderr
, ".\n");
56 typedef struct _FcFormatContext
58 const FcChar8
*format_orig
;
59 const FcChar8
*format
;
62 FcBool word_allocated
;
66 FcFormatContextInit (FcFormatContext
*c
,
67 const FcChar8
*format
,
71 c
->format_orig
= c
->format
= format
;
72 c
->format_len
= strlen ((const char *) format
);
74 if (c
->format_len
< scratch_len
)
77 c
->word_allocated
= FcFalse
;
81 c
->word
= malloc (c
->format_len
+ 1);
82 c
->word_allocated
= FcTrue
;
85 return c
->word
!= NULL
;
89 FcFormatContextDone (FcFormatContext
*c
)
91 if (c
&& c
->word_allocated
)
98 consume_char (FcFormatContext
*c
,
101 if (*c
->format
!= term
)
109 expect_char (FcFormatContext
*c
,
112 FcBool res
= consume_char (c
, term
);
115 if (c
->format
== c
->format_orig
+ c
->format_len
)
116 message ("format ended while expecting '%c'",
119 message ("expected '%c' at %d",
120 term
, c
->format
- c
->format_orig
+ 1);
126 FcCharIsPunct (const FcChar8 c
)
145 static char escaped_char(const char ch
)
148 case 'a': return '\a';
149 case 'b': return '\b';
150 case 'f': return '\f';
151 case 'n': return '\n';
152 case 'r': return '\r';
153 case 't': return '\t';
154 case 'v': return '\v';
160 read_word (FcFormatContext
*c
)
168 if (*c
->format
== '\\')
172 *p
++ = escaped_char (*c
->format
++);
175 else if (FcCharIsPunct (*c
->format
))
184 message ("expected identifier at %d",
185 c
->format
- c
->format_orig
+ 1);
193 read_chars (FcFormatContext
*c
,
200 while (*c
->format
&& *c
->format
!= '}' && *c
->format
!= term
)
202 if (*c
->format
== '\\')
206 *p
++ = escaped_char (*c
->format
++);
216 message ("expected character data at %d",
217 c
->format
- c
->format_orig
+ 1);
225 FcPatternFormatToBuf (FcPattern
*pat
,
226 const FcChar8
*format
,
230 interpret_builtin (FcFormatContext
*c
,
237 if (!expect_char (c
, '=') ||
241 /* try simple builtins first */
243 #define BUILTIN(name, func) \
244 else if (0 == strcmp ((const char *) c->word, name))\
245 do { new_str = func (pat); ret = FcTrue; } while (0)
246 BUILTIN ("unparse", FcNameUnparse
);
247 /* BUILTIN ("verbose", FcPatternPrint); XXX */
256 FcStrBufString (buf
, new_str
);
264 /* now try our custom formats */
266 #define BUILTIN(name, format) \
267 else if (0 == strcmp ((const char *) c->word, name))\
268 ret = FcPatternFormatToBuf (pat, (const FcChar8 *) format, buf)
269 BUILTIN ("fcmatch", FCMATCH_FORMAT
);
270 BUILTIN ("fclist", FCLIST_FORMAT
);
271 BUILTIN ("pkgkit", PKGKIT_FORMAT
);
277 message ("unknown builtin \"%s\"",
284 interpret_expr (FcFormatContext
*c
,
290 interpret_subexpr (FcFormatContext
*c
,
294 return expect_char (c
, '{') &&
295 interpret_expr (c
, pat
, buf
, '}') &&
296 expect_char (c
, '}');
300 maybe_interpret_subexpr (FcFormatContext
*c
,
304 return (*c
->format
== '{') ?
305 interpret_subexpr (c
, pat
, buf
) :
310 skip_subexpr (FcFormatContext
*c
);
313 skip_percent (FcFormatContext
*c
)
317 if (!expect_char (c
, '%'))
320 /* skip an optional width specifier */
321 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
323 if (!expect_char (c
, '{'))
326 while(*c
->format
&& *c
->format
!= '}')
331 c
->format
++; /* skip over '\\' */
336 if (!skip_subexpr (c
))
343 return expect_char (c
, '}');
347 skip_expr (FcFormatContext
*c
)
349 while(*c
->format
&& *c
->format
!= '}')
354 c
->format
++; /* skip over '\\' */
359 if (!skip_percent (c
))
370 skip_subexpr (FcFormatContext
*c
)
372 return expect_char (c
, '{') &&
374 expect_char (c
, '}');
378 maybe_skip_subexpr (FcFormatContext
*c
)
380 return (*c
->format
== '{') ?
386 interpret_filter (FcFormatContext
*c
,
393 if (!expect_char (c
, '+'))
396 os
= FcObjectSetCreate ();
402 if (!read_word (c
) ||
403 !FcObjectSetAdd (os
, (const char *) c
->word
))
405 FcObjectSetDestroy (os
);
409 while (consume_char (c
, ','));
411 subpat
= FcPatternFilter (pat
, os
);
412 FcObjectSetDestroy (os
);
415 !interpret_subexpr (c
, subpat
, buf
))
418 FcPatternDestroy (subpat
);
423 interpret_delete (FcFormatContext
*c
,
429 if (!expect_char (c
, '-'))
432 subpat
= FcPatternDuplicate (pat
);
440 FcPatternDestroy (subpat
);
444 FcPatternDel (subpat
, (const char *) c
->word
);
446 while (consume_char (c
, ','));
448 if (!interpret_subexpr (c
, subpat
, buf
))
451 FcPatternDestroy (subpat
);
456 interpret_cond (FcFormatContext
*c
,
462 if (!expect_char (c
, '?'))
472 negate
= consume_char (c
, '!');
480 FcPatternGet (pat
, (const char *) c
->word
, 0, &v
)));
482 while (consume_char (c
, ','));
486 if (!interpret_subexpr (c
, pat
, buf
) ||
487 !maybe_skip_subexpr (c
))
492 if (!skip_subexpr (c
) ||
493 !maybe_interpret_subexpr (c
, pat
, buf
))
501 interpret_count (FcFormatContext
*c
,
507 FcChar8 buf_static
[64];
509 if (!expect_char (c
, '#'))
516 e
= FcPatternObjectFindElt (pat
,
517 FcObjectFromName ((const char *) c
->word
));
522 for (l
= FcPatternEltValues(e
);
528 snprintf ((char *) buf_static
, sizeof (buf_static
), "%d", count
);
529 FcStrBufString (buf
, buf_static
);
535 interpret_array (FcFormatContext
*c
,
541 const FcChar8
*format_save
;
544 FcStrList
*lang_strs
;
546 if (!expect_char (c
, '[') ||
547 !expect_char (c
, ']'))
550 os
= FcObjectSetCreate ();
558 if (!read_word (c
) ||
559 !FcObjectSetAdd (os
, (const char *) c
->word
))
561 FcObjectSetDestroy (os
);
565 while (consume_char (c
, ','));
567 /* If we have one element and it's of type FcLangSet, we want
568 * to enumerate the languages in it. */
570 if (os
->nobject
== 1)
574 FcPatternGetLangSet (pat
, os
->objects
[0], idx
, &langset
))
577 if (!(ss
= FcLangSetGetLangs (langset
)) ||
578 !(lang_strs
= FcStrListCreate (ss
)))
583 subpat
= FcPatternDuplicate (pat
);
587 format_save
= c
->format
;
599 FcPatternDel (subpat
, os
->objects
[0]);
600 if ((lang
= FcStrListNext (lang_strs
)))
602 FcPatternAddString (subpat
, os
->objects
[0], lang
);
608 for (i
= 0; i
< os
->nobject
; i
++)
612 /* XXX this can be optimized by accessing valuelist linked lists
613 * directly and remembering where we were. Most (all) value lists
614 * in normal uses are pretty short though (language tags are
615 * stored as a LangSet, not separate values.). */
616 FcPatternDel (subpat
, os
->objects
[i
]);
618 FcPatternGet (pat
, os
->objects
[i
], idx
, &v
))
620 FcPatternAdd (subpat
, os
->objects
[i
], v
, FcFalse
);
628 c
->format
= format_save
;
629 ret
= interpret_subexpr (c
, subpat
, buf
);
637 if (c
->format
== format_save
)
641 FcPatternDestroy (subpat
);
644 FcStrListDone (lang_strs
);
645 FcObjectSetDestroy (os
);
651 interpret_simple (FcFormatContext
*c
,
656 FcBool add_colon
= FcFalse
;
657 FcBool add_elt_name
= FcFalse
;
659 FcChar8
*else_string
;
661 if (consume_char (c
, ':'))
668 if (consume_char (c
, '['))
670 idx
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
673 message ("expected non-negative number at %d",
674 c
->format
-1 - c
->format_orig
+ 1);
677 if (!expect_char (c
, ']'))
681 if (consume_char (c
, '='))
682 add_elt_name
= FcTrue
;
686 if (consume_char (c
, ':'))
689 /* divert the c->word for now */
691 c
->word
= c
->word
+ strlen ((const char *) c
->word
) + 1;
692 /* for now we just support 'default value' */
693 if (!expect_char (c
, '-') ||
694 !read_chars (c
, '\0'))
699 else_string
= c
->word
;
703 e
= FcPatternObjectFindElt (pat
,
704 FcObjectFromName ((const char *) c
->word
));
710 FcStrBufChar (buf
, ':');
713 FcStrBufString (buf
, c
->word
);
714 FcStrBufChar (buf
, '=');
717 l
= FcPatternEltValues(e
);
723 l
= FcValueListNext(l
);
728 if (!FcNameUnparseValue (buf
, &l
->value
, '\0'))
735 FcNameUnparseValueList (buf
, l
, '\0');
742 printf ("%s", else_string
);
749 cescape (FcFormatContext
*c
,
759 FcStrBufChar (buf
, '\\');
762 FcStrBufChar (buf
, *str
++);
768 shescape (FcFormatContext
*c
,
772 FcStrBufChar (buf
, '\'');
776 FcStrBufString (buf
, (const FcChar8
*) "'\\''");
778 FcStrBufChar (buf
, *str
);
781 FcStrBufChar (buf
, '\'');
786 xmlescape (FcFormatContext
*c
,
794 case '&': FcStrBufString (buf
, (const FcChar8
*) "&"); break;
795 case '<': FcStrBufString (buf
, (const FcChar8
*) "<"); break;
796 case '>': FcStrBufString (buf
, (const FcChar8
*) ">"); break;
797 default: FcStrBufChar (buf
, *str
); break;
805 delete_chars (FcFormatContext
*c
,
809 /* XXX not UTF-8 aware */
811 if (!expect_char (c
, '(') ||
812 !read_chars (c
, ')') ||
813 !expect_char (c
, ')'))
820 p
= (FcChar8
*) strpbrk ((const char *) str
, (const char *) c
->word
);
823 FcStrBufData (buf
, str
, p
- str
);
828 FcStrBufString (buf
, str
);
838 escape_chars (FcFormatContext
*c
,
842 /* XXX not UTF-8 aware */
844 if (!expect_char (c
, '(') ||
845 !read_chars (c
, ')') ||
846 !expect_char (c
, ')'))
853 p
= (FcChar8
*) strpbrk ((const char *) str
, (const char *) c
->word
);
856 FcStrBufData (buf
, str
, p
- str
);
857 FcStrBufChar (buf
, c
->word
[0]);
858 FcStrBufChar (buf
, *p
);
863 FcStrBufString (buf
, str
);
873 translate_chars (FcFormatContext
*c
,
877 char *from
, *to
, repeat
;
878 int from_len
, to_len
;
880 /* XXX not UTF-8 aware */
882 if (!expect_char (c
, '(') ||
883 !read_chars (c
, ',') ||
884 !expect_char (c
, ','))
887 from
= (char *) c
->word
;
888 from_len
= strlen (from
);
889 to
= from
+ from_len
+ 1;
891 /* hack: we temporarily divert c->word */
892 c
->word
= (FcChar8
*) to
;
893 if (!read_chars (c
, ')'))
895 c
->word
= (FcChar8
*) from
;
898 c
->word
= (FcChar8
*) from
;
900 to_len
= strlen (to
);
901 repeat
= to
[to_len
- 1];
903 if (!expect_char (c
, ')'))
910 p
= (FcChar8
*) strpbrk ((const char *) str
, (const char *) from
);
914 FcStrBufData (buf
, str
, p
- str
);
915 i
= strchr (from
, *p
) - from
;
916 FcStrBufChar (buf
, i
< to_len
? to
[i
] : repeat
);
921 FcStrBufString (buf
, str
);
931 interpret_convert (FcFormatContext
*c
,
938 FcChar8 buf_static
[8192];
941 if (!expect_char (c
, '|') ||
945 /* prepare the buffer */
946 FcStrBufChar (buf
, '\0');
949 str
= buf
->buf
+ start
;
952 /* try simple converters first */
954 #define CONVERTER(name, func) \
955 else if (0 == strcmp ((const char *) c->word, name))\
956 do { new_str = func (str); ret = FcTrue; } while (0)
957 CONVERTER ("downcase", FcStrDowncase
);
958 CONVERTER ("basename", FcStrBasename
);
959 CONVERTER ("dirname", FcStrDirname
);
968 FcStrBufString (buf
, new_str
);
976 FcStrBufInit (&new_buf
, buf_static
, sizeof (buf_static
));
978 /* now try our custom converters */
980 #define CONVERTER(name, func) \
981 else if (0 == strcmp ((const char *) c->word, name))\
982 ret = func (c, str, &new_buf)
983 CONVERTER ("cescape", cescape
);
984 CONVERTER ("shescape", shescape
);
985 CONVERTER ("xmlescape", xmlescape
);
986 CONVERTER ("delete", delete_chars
);
987 CONVERTER ("escape", escape_chars
);
988 CONVERTER ("translate", translate_chars
);
995 FcStrBufChar (&new_buf
, '\0');
996 FcStrBufString (buf
, new_buf
.buf
);
999 message ("unknown converter \"%s\"",
1002 FcStrBufDestroy (&new_buf
);
1008 maybe_interpret_converts (FcFormatContext
*c
,
1012 while (*c
->format
== '|')
1013 if (!interpret_convert (c
, buf
, start
))
1020 align_to_width (FcStrBuf
*buf
,
1029 len
= buf
->len
- start
;
1033 while (len
++ < -width
)
1034 FcStrBufChar (buf
, ' ');
1036 else if (len
< width
)
1041 while (len
++ < width
)
1042 FcStrBufChar (buf
, ' ');
1046 memmove (buf
->buf
+ buf
->len
- len
,
1047 buf
->buf
+ buf
->len
- width
,
1049 memset (buf
->buf
+ buf
->len
- width
,
1054 return !buf
->failed
;
1057 interpret_percent (FcFormatContext
*c
,
1064 if (!expect_char (c
, '%'))
1067 if (consume_char (c
, '%')) /* "%%" */
1069 FcStrBufChar (buf
, '%');
1073 /* parse an optional width specifier */
1074 width
= strtol ((const char *) c
->format
, (char **) &c
->format
, 10);
1076 if (!expect_char (c
, '{'))
1081 switch (*c
->format
) {
1082 case '=': ret
= interpret_builtin (c
, pat
, buf
); break;
1083 case '{': ret
= interpret_subexpr (c
, pat
, buf
); break;
1084 case '+': ret
= interpret_filter (c
, pat
, buf
); break;
1085 case '-': ret
= interpret_delete (c
, pat
, buf
); break;
1086 case '?': ret
= interpret_cond (c
, pat
, buf
); break;
1087 case '#': ret
= interpret_count (c
, pat
, buf
); break;
1088 case '[': ret
= interpret_array (c
, pat
, buf
); break;
1089 default: ret
= interpret_simple (c
, pat
, buf
); break;
1093 maybe_interpret_converts (c
, buf
, start
) &&
1094 align_to_width (buf
, start
, width
) &&
1095 expect_char (c
, '}');
1099 interpret_expr (FcFormatContext
*c
,
1104 while (*c
->format
&& *c
->format
!= term
)
1109 c
->format
++; /* skip over '\\' */
1111 FcStrBufChar (buf
, escaped_char (*c
->format
++));
1114 if (!interpret_percent (c
, pat
, buf
))
1118 FcStrBufChar (buf
, *c
->format
++);
1124 FcPatternFormatToBuf (FcPattern
*pat
,
1125 const FcChar8
*format
,
1129 FcChar8 word_static
[1024];
1132 if (!FcFormatContextInit (&c
, format
, word_static
, sizeof (word_static
)))
1135 ret
= interpret_expr (&c
, pat
, buf
, '\0');
1137 FcFormatContextDone (&c
);
1143 FcPatternFormat (FcPattern
*pat
,
1144 const FcChar8
*format
)
1147 FcChar8 buf_static
[8192 - 1024];
1150 FcStrBufInit (&buf
, buf_static
, sizeof (buf_static
));
1152 ret
= FcPatternFormatToBuf (pat
, format
, &buf
);
1155 return FcStrBufDone (&buf
);
1158 FcStrBufDestroy (&buf
);
1163 #define __fcformat__
1164 #include "fcaliastail.h"