1a6844aabSmrg/*
2a6844aabSmrg * fontconfig/doc/fcformat.fncs
3a6844aabSmrg *
4a6844aabSmrg * Copyright © 2008 Behdad Esfahbod
5a6844aabSmrg *
6a6844aabSmrg * Permission to use, copy, modify, distribute, and sell this software and its
7a6844aabSmrg * documentation for any purpose is hereby granted without fee, provided that
8a6844aabSmrg * the above copyright notice appear in all copies and that both that
9a6844aabSmrg * copyright notice and this permission notice appear in supporting
10ca08ab68Smrg * documentation, and that the name of the author(s) not be used in
11a6844aabSmrg * advertising or publicity pertaining to distribution of the software without
12ca08ab68Smrg * specific, written prior permission.  The authors make no
13a6844aabSmrg * representations about the suitability of this software for any purpose.  It
14a6844aabSmrg * is provided "as is" without express or implied warranty.
15a6844aabSmrg *
16a6844aabSmrg * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17a6844aabSmrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18a6844aabSmrg * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19a6844aabSmrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20a6844aabSmrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21a6844aabSmrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22a6844aabSmrg * PERFORMANCE OF THIS SOFTWARE.
23a6844aabSmrg */
24a6844aabSmrg
251cc69409Smrg@RET@           FcChar8 *
261cc69409Smrg@FUNC@          FcPatternFormat
271cc69409Smrg@TYPE1@         FcPattern *                     @ARG1@          pat
281cc69409Smrg@TYPE2@         const FcChar8 *                 @ARG2@          format
291cc69409Smrg@PURPOSE@       Format a pattern into a string according to a format specifier
30a6844aabSmrg@DESC@
31a6844aabSmrg
32a6844aabSmrgConverts given pattern <parameter>pat</parameter> into text described by
33a6844aabSmrgthe format specifier <parameter>format</parameter>.
34a6844aabSmrgThe return value refers to newly allocated memory which should be freed by the
35a6844aabSmrgcaller using free(), or NULL if <parameter>format</parameter> is invalid.
36a6844aabSmrg
37a6844aabSmrg</para><para>
38a6844aabSmrg
39ca08ab68SmrgThe format is loosely modeled after printf-style format string.
40a6844aabSmrgThe format string is composed of zero or more  directives: ordinary
41a6844aabSmrgcharacters (not "%"), which are copied unchanged to the output stream;
42a6844aabSmrgand tags which are interpreted to construct text from the pattern in a
43a6844aabSmrgvariety of ways (explained below).
44a6844aabSmrgSpecial characters can be escaped
45a6844aabSmrgusing backslash.  C-string style special characters like \n and \r are
46a6844aabSmrgalso supported (this is useful when the format string is not a C string
47a6844aabSmrgliteral).
48a6844aabSmrgIt is advisable to always escape curly braces that
49a6844aabSmrgare meant to be copied to the output as ordinary characters.
50a6844aabSmrg
51a6844aabSmrg</para><para>
52a6844aabSmrg
53ca08ab68SmrgEach tag is introduced by the character "%",
54a6844aabSmrgfollowed by an optional minimum field width,
55a6844aabSmrgfollowed by tag contents in curly braces ({}).
56a6844aabSmrgIf the minimum field width value is provided the tag
57a6844aabSmrgwill be expanded and the result padded to achieve the minimum width.
58a6844aabSmrgIf the minimum field width is positive, the padding will right-align
59a6844aabSmrgthe text.  Negative field width will left-align.
60a6844aabSmrgThe rest of this section describes various supported tag contents
61a6844aabSmrgand their expansion.
62a6844aabSmrg
63a6844aabSmrg</para><para>
64a6844aabSmrg
65a6844aabSmrgA <firstterm>simple</firstterm> tag
66a6844aabSmrgis one where the content is an identifier.  When simple
67a6844aabSmrgtags are expanded, the named identifier will be looked up in
68a6844aabSmrg<parameter>pattern</parameter> and the resulting list of values returned,
69ca08ab68Smrgjoined together using comma.  For example, to print the family name and style of the
70a6844aabSmrgpattern, use the format "%{family} %{style}\n".  To extend the family column
71a6844aabSmrgto forty characters use "%-40{family}%{style}\n".
72a6844aabSmrg
73a6844aabSmrg</para><para>
74a6844aabSmrg
75a6844aabSmrgSimple tags expand to list of all values for an element.  To only choose
76a6844aabSmrgone of the values, one can index using the syntax "%{elt[idx]}".  For example,
77a6844aabSmrgto get the first family name only, use "%{family[0]}".
78a6844aabSmrg
79a6844aabSmrg</para><para>
80a6844aabSmrg
81a6844aabSmrgIf a simple tag ends with "=" and the element is found in the pattern, the
82a6844aabSmrgname of the element followed by "=" will be output before the list of values.
83a6844aabSmrgFor example, "%{weight=}" may expand to the string "weight=80".  Or to the empty
84a6844aabSmrgstring if <parameter>pattern</parameter> does not have weight set.
85a6844aabSmrg
86a6844aabSmrg</para><para>
87a6844aabSmrg
88a6844aabSmrgIf a simple tag starts with ":" and the element is found in the pattern, ":"
89a6844aabSmrgwill be printed first.  For example, combining this with the =, the format
90a6844aabSmrg"%{:weight=}" may expand to ":weight=80" or to the empty string
91a6844aabSmrgif <parameter>pattern</parameter> does not have weight set.
92a6844aabSmrg
93a6844aabSmrg</para><para>
94a6844aabSmrg
95a6844aabSmrgIf a simple tag contains the string ":-", the rest of the the tag contents
96a6844aabSmrgwill be used as a default string.  The default string is output if the element
97a6844aabSmrgis not found in the pattern.  For example, the format
98a6844aabSmrg"%{:weight=:-123}" may expand to ":weight=80" or to the string
99a6844aabSmrg":weight=123" if <parameter>pattern</parameter> does not have weight set.
100a6844aabSmrg
101a6844aabSmrg</para><para>
102a6844aabSmrg
103a6844aabSmrgA <firstterm>count</firstterm> tag
104a6844aabSmrgis one that starts with the character "#" followed by an element
105a6844aabSmrgname, and expands to the number of values for the element in the pattern.
106a6844aabSmrgFor example, "%{#family}" expands to the number of family names
107a6844aabSmrg<parameter>pattern</parameter> has set, which may be zero.
108a6844aabSmrg
109a6844aabSmrg</para><para>
110a6844aabSmrg
111a6844aabSmrgA <firstterm>sub-expression</firstterm> tag
112a6844aabSmrgis one that expands a sub-expression.  The tag contents
113a6844aabSmrgare the sub-expression to expand placed inside another set of curly braces.
114a6844aabSmrgSub-expression tags are useful for aligning an entire sub-expression, or to
115ca08ab68Smrgapply converters (explained later) to the entire sub-expression output.
116a6844aabSmrgFor example, the format "%40{{%{family} %{style}}}" expands the sub-expression
117a6844aabSmrgto construct the family name followed by the style, then takes the entire
118a6844aabSmrgstring and pads it on the left to be at least forty characters.
119a6844aabSmrg
120a6844aabSmrg</para><para>
121a6844aabSmrg
122a6844aabSmrgA <firstterm>filter-out</firstterm> tag
123a6844aabSmrgis one starting with the character "-" followed by a
124a6844aabSmrgcomma-separated list of element names, followed by a sub-expression enclosed
125a6844aabSmrgin curly braces.  The sub-expression will be expanded but with a pattern that
126a6844aabSmrghas the listed elements removed from it.
127a6844aabSmrgFor example, the format "%{-size,pixelsize{sub-expr}}" will expand "sub-expr"
128a6844aabSmrgwith <parameter>pattern</parameter> sans the size and pixelsize elements.
129a6844aabSmrg
130a6844aabSmrg</para><para>
131a6844aabSmrg
132a6844aabSmrgA <firstterm>filter-in</firstterm> tag
133a6844aabSmrgis one starting with the character "+" followed by a
134a6844aabSmrgcomma-separated list of element names, followed by a sub-expression enclosed
135a6844aabSmrgin curly braces.  The sub-expression will be expanded but with a pattern that
136a6844aabSmrgonly has the listed elements from the surrounding pattern.
137a6844aabSmrgFor example, the format "%{+family,familylang{sub-expr}}" will expand "sub-expr"
138a6844aabSmrgwith a sub-pattern consisting only the family and family lang elements of
139a6844aabSmrg<parameter>pattern</parameter>.
140a6844aabSmrg
141a6844aabSmrg</para><para>
142a6844aabSmrg
143a6844aabSmrgA <firstterm>conditional</firstterm> tag
144a6844aabSmrgis one starting with the character "?" followed by a
145a6844aabSmrgcomma-separated list of element conditions, followed by two sub-expression
146a6844aabSmrgenclosed in curly braces.  An element condition can be an element name,
147a6844aabSmrgin which case it tests whether the element is defined in pattern, or
148a6844aabSmrgthe character "!" followed by an element name, in which case the test
149a6844aabSmrgis negated.  The conditional passes if all the element conditions pass.
150a6844aabSmrgThe tag expands the first sub-expression if the conditional passes, and
151a6844aabSmrgexpands the second sub-expression otherwise.
152a6844aabSmrgFor example, the format "%{?size,dpi,!pixelsize{pass}{fail}}" will expand
153a6844aabSmrgto "pass" if <parameter>pattern</parameter> has size and dpi elements but
154a6844aabSmrgno pixelsize element, and to "fail" otherwise.
155a6844aabSmrg
156a6844aabSmrg</para><para>
157a6844aabSmrg
158a6844aabSmrgAn <firstterm>enumerate</firstterm> tag
159a6844aabSmrgis one starting with the string "[]" followed by a
160a6844aabSmrgcomma-separated list of element names, followed by a sub-expression enclosed
161a6844aabSmrgin curly braces.  The list of values for the named elements are walked in
162a6844aabSmrgparallel and the sub-expression expanded each time with a pattern just having
163a6844aabSmrga single value for those elements, starting from the first value and
164a6844aabSmrgcontinuing as long as any of those elements has a value.
165a6844aabSmrgFor example, the format "%{[]family,familylang{%{family} (%{familylang})\n}}"
166a6844aabSmrgwill expand the pattern "%{family} (%{familylang})\n" with a pattern
167ca08ab68Smrghaving only the first value of the family and familylang elements, then expands
168a6844aabSmrgit with the second values, then the third, etc.
169a6844aabSmrg
170a6844aabSmrg</para><para>
171a6844aabSmrg
172a6844aabSmrgAs a special case, if an enumerate tag has only one element, and that element
173a6844aabSmrghas only one value in the pattern, and that value is of type FcLangSet, the
174a6844aabSmrgindividual languages in the language set are enumerated.
175a6844aabSmrg
176a6844aabSmrg</para><para>
177a6844aabSmrg
178a6844aabSmrgA <firstterm>builtin</firstterm> tag
179a6844aabSmrgis one starting with the character "=" followed by a builtin
180a6844aabSmrgname.  The following builtins are defined:
181a6844aabSmrg
182a6844aabSmrg<variablelist>
183a6844aabSmrg
184a6844aabSmrg<varlistentry><term>
185a6844aabSmrgunparse
186a6844aabSmrg</term><listitem><para>
187a6844aabSmrgExpands to the result of calling FcNameUnparse() on the pattern.
188a6844aabSmrg</para></listitem></varlistentry>
189a6844aabSmrg
190a6844aabSmrg<varlistentry><term>
191a6844aabSmrgfcmatch
192a6844aabSmrg</term><listitem><para>
193a6844aabSmrgExpands to the output of the default output format of the fc-match
194a6844aabSmrgcommand on the pattern, without the final newline.
195a6844aabSmrg</para></listitem></varlistentry>
196a6844aabSmrg
197a6844aabSmrg<varlistentry><term>
198a6844aabSmrgfclist
199a6844aabSmrg</term><listitem><para>
200a6844aabSmrgExpands to the output of the default output format of the fc-list
201a6844aabSmrgcommand on the pattern, without the final newline.
202a6844aabSmrg</para></listitem></varlistentry>
203a6844aabSmrg
204ca08ab68Smrg<varlistentry><term>
205ca08ab68Smrgfccat
206ca08ab68Smrg</term><listitem><para>
207ca08ab68SmrgExpands to the output of the default output format of the fc-cat
208ca08ab68Smrgcommand on the pattern, without the final newline.
209ca08ab68Smrg</para></listitem></varlistentry>
210ca08ab68Smrg
211a6844aabSmrg<varlistentry><term>
212a6844aabSmrgpkgkit
213a6844aabSmrg</term><listitem><para>
214a6844aabSmrgExpands to the list of PackageKit font() tags for the pattern.
215a6844aabSmrgCurrently this includes tags for each family name, and each language
216a6844aabSmrgfrom the pattern, enumerated and sanitized into a set of tags terminated
217a6844aabSmrgby newline.  Package management systems can use these tags to tag their
218a6844aabSmrgpackages accordingly.
219a6844aabSmrg</para></listitem></varlistentry>
220a6844aabSmrg
221a6844aabSmrg</variablelist>
222a6844aabSmrg
223a6844aabSmrgFor example, the format "%{+family,style{%{=unparse}}}\n" will expand
224a6844aabSmrgto an unparsed name containing only the family and style element values
225a6844aabSmrgfrom <parameter>pattern</parameter>.
226a6844aabSmrg
227a6844aabSmrg</para><para>
228a6844aabSmrg
229a6844aabSmrgThe contents of any tag can be followed by a set of zero or more
230a6844aabSmrg<firstterm>converter</firstterm>s.  A converter is specified by the
231a6844aabSmrgcharacter "|" followed by the converter name and arguments.  The
232a6844aabSmrgfollowing converters are defined:
233a6844aabSmrg
234a6844aabSmrg<variablelist>
235a6844aabSmrg
236a6844aabSmrg<varlistentry><term>
237a6844aabSmrgbasename
238a6844aabSmrg</term><listitem><para>
239a6844aabSmrgReplaces text with the results of calling FcStrBasename() on it.
240a6844aabSmrg</para></listitem></varlistentry>
241a6844aabSmrg
242a6844aabSmrg<varlistentry><term>
243a6844aabSmrgdirname
244a6844aabSmrg</term><listitem><para>
245a6844aabSmrgReplaces text with the results of calling FcStrDirname() on it.
246a6844aabSmrg</para></listitem></varlistentry>
247a6844aabSmrg
248a6844aabSmrg<varlistentry><term>
249a6844aabSmrgdowncase
250a6844aabSmrg</term><listitem><para>
251a6844aabSmrgReplaces text with the results of calling FcStrDowncase() on it.
252a6844aabSmrg</para></listitem></varlistentry>
253a6844aabSmrg
254a6844aabSmrg<varlistentry><term>
255a6844aabSmrgshescape
256a6844aabSmrg</term><listitem><para>
257a6844aabSmrgEscapes text for one level of shell expansion.
258a6844aabSmrg(Escapes single-quotes, also encloses text in single-quotes.)
259a6844aabSmrg</para></listitem></varlistentry>
260a6844aabSmrg
261a6844aabSmrg<varlistentry><term>
262a6844aabSmrgcescape
263a6844aabSmrg</term><listitem><para>
264a6844aabSmrgEscapes text such that it can be used as part of a C string literal.
265a6844aabSmrg(Escapes backslash and double-quotes.)
266a6844aabSmrg</para></listitem></varlistentry>
267a6844aabSmrg
268a6844aabSmrg<varlistentry><term>
269a6844aabSmrgxmlescape
270a6844aabSmrg</term><listitem><para>
271a6844aabSmrgEscapes text such that it can be used in XML and HTML.
272a6844aabSmrg(Escapes less-than, greater-than, and ampersand.)
273a6844aabSmrg</para></listitem></varlistentry>
274a6844aabSmrg
275a6844aabSmrg<varlistentry><term>
276a6844aabSmrgdelete(<parameter>chars</parameter>)
277a6844aabSmrg</term><listitem><para>
278a6844aabSmrgDeletes all occurrences of each of the characters in <parameter>chars</parameter>
279a6844aabSmrgfrom the text.
280a6844aabSmrgFIXME: This converter is not UTF-8 aware yet.
281a6844aabSmrg</para></listitem></varlistentry>
282a6844aabSmrg
283a6844aabSmrg<varlistentry><term>
284a6844aabSmrgescape(<parameter>chars</parameter>)
285a6844aabSmrg</term><listitem><para>
286a6844aabSmrgEscapes all occurrences of each of the characters in <parameter>chars</parameter>
287a6844aabSmrgby prepending it by the first character in <parameter>chars</parameter>.
288a6844aabSmrgFIXME: This converter is not UTF-8 aware yet.
289a6844aabSmrg</para></listitem></varlistentry>
290a6844aabSmrg
291a6844aabSmrg<varlistentry><term>
292a6844aabSmrgtranslate(<parameter>from</parameter>,<parameter>to</parameter>)
293a6844aabSmrg</term><listitem><para>
294a6844aabSmrgTranslates all occurrences of each of the characters in <parameter>from</parameter>
295a6844aabSmrgby replacing them with their corresponding character in <parameter>to</parameter>.
296a6844aabSmrgIf <parameter>to</parameter> has fewer characters than
297a6844aabSmrg<parameter>from</parameter>, it will be extended by repeating its last
298a6844aabSmrgcharacter.
299a6844aabSmrgFIXME: This converter is not UTF-8 aware yet.
300a6844aabSmrg</para></listitem></varlistentry>
301a6844aabSmrg
302a6844aabSmrg</variablelist>
303a6844aabSmrg
304a6844aabSmrgFor example, the format "%{family|downcase|delete( )}\n" will expand
305a6844aabSmrgto the values of the family element in <parameter>pattern</parameter>,
306a6844aabSmrglower-cased and with spaces removed.
307a6844aabSmrg
3081cc69409Smrg@SINCE@         2.9.0
309a6844aabSmrg@@
310