2 * fontconfig/fc-match/fc-match.c
4 * Copyright © 2003 Keith Packard
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.
29 #define HAVE_GETOPT_LONG 1
34 #include <fontconfig/fontconfig.h>
42 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
44 #define dgettext(d, s) (s)
51 #ifndef HAVE_GETOPT_LONG
52 #define HAVE_GETOPT_LONG 0
59 static const struct option longopts[] = {
62 {"verbose", 0, 0, 'v'},
64 {"format", 1, 0, 'f'},
65 {"version", 0, 0, 'V'},
72 extern int optind, opterr, optopt;
77 usage (char *program, int error)
79 FILE *file = error ? stderr : stdout;
81 fprintf (file, _("usage: %s [-savbVh] [-f FORMAT] [--sort] [--all] [--verbose] [--brief] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
84 fprintf (file, _("usage: %s [-savVh] [-f FORMAT] [pattern] {element...}\n"),
87 fprintf (file, _("List best font matching [pattern]\n"));
90 fprintf (file, _(" -s, --sort display sorted list of matches\n"));
91 fprintf (file, _(" -a, --all display unpruned sorted list of matches\n"));
92 fprintf (file, _(" -v, --verbose display entire font pattern verbosely\n"));
93 fprintf (file, _(" -b, --brief display entire font pattern briefly\n"));
94 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
95 fprintf (file, _(" -V, --version display font config version and exit\n"));
96 fprintf (file, _(" -h, --help display this help and exit\n"));
98 fprintf (file, _(" -s, (sort) display sorted list of matches\n"));
99 fprintf (file, _(" -a (all) display unpruned sorted list of matches\n"));
100 fprintf (file, _(" -v (verbose) display entire font pattern verbosely\n"));
101 fprintf (file, _(" -b (brief) display entire font pattern briefly\n"));
102 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
103 fprintf (file, _(" -V (version) display font config version and exit\n"));
104 fprintf (file, _(" -h (help) display this help and exit\n"));
110 main (int argc, char **argv)
114 int sort = 0, all = 0;
115 const FcChar8 *format = NULL;
121 #if HAVE_GETOPT_LONG || HAVE_GETOPT
125 while ((c = getopt_long (argc, argv, "asvbf:Vh", longopts, NULL)) != -1)
127 while ((c = getopt (argc, argv, "asvbf:Vh")) != -1)
144 format = (FcChar8 *) strdup (optarg);
147 fprintf (stderr, "fontconfig version %d.%d.%d\n",
148 FC_MAJOR, FC_MINOR, FC_REVISION);
163 pat = FcNameParse ((FcChar8 *) argv[i]);
166 fprintf (stderr, _("Unable to parse the pattern\n"));
172 os = FcObjectSetCreate ();
173 FcObjectSetAdd (os, argv[i]);
177 pat = FcPatternCreate ();
182 FcConfigSubstitute (0, pat, FcMatchPattern);
183 FcDefaultSubstitute (pat);
185 fs = FcFontSetCreate ();
189 FcFontSet *font_patterns;
191 font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
193 if (!font_patterns || font_patterns->nfont == 0)
195 fprintf (stderr, _("No fonts installed on the system\n"));
198 for (j = 0; j < font_patterns->nfont; j++)
200 FcPattern *font_pattern;
202 font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]);
204 FcFontSetAdd (fs, font_pattern);
207 FcFontSetSortDestroy (font_patterns);
212 match = FcFontMatch (0, pat, &result);
214 FcFontSetAdd (fs, match);
216 FcPatternDestroy (pat);
221 format = (const FcChar8 *) "%{=unparse}\n";
223 format = (const FcChar8 *) "%{=fcmatch}\n";
230 for (j = 0; j < fs->nfont; j++)
234 font = FcPatternFilter (fs->fonts[j], os);
236 if (verbose || brief)
240 FcPatternDel (font, FC_CHARSET);
241 FcPatternDel (font, FC_LANG);
243 FcPatternPrint (font);
249 s = FcPatternFormat (font, format);
257 FcPatternDestroy (font);
259 FcFontSetDestroy (fs);
263 FcObjectSetDestroy (os);