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>
43 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
45 #define dgettext(d, s) (s)
52 #ifndef HAVE_GETOPT_LONG
53 #define HAVE_GETOPT_LONG 0
60 static const struct option longopts[] = {
63 {"verbose", 0, 0, 'v'},
65 {"format", 1, 0, 'f'},
66 {"version", 0, 0, 'V'},
73 extern int optind, opterr, optopt;
78 usage (char *program, int error)
80 FILE *file = error ? stderr : stdout;
82 fprintf (file, _("usage: %s [-savbVh] [-f FORMAT] [--sort] [--all] [--verbose] [--brief] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
85 fprintf (file, _("usage: %s [-savVh] [-f FORMAT] [pattern] {element...}\n"),
88 fprintf (file, _("List best font matching [pattern]\n"));
91 fprintf (file, _(" -s, --sort display sorted list of matches\n"));
92 fprintf (file, _(" -a, --all display unpruned sorted list of matches\n"));
93 fprintf (file, _(" -v, --verbose display entire font pattern verbosely\n"));
94 fprintf (file, _(" -b, --brief display entire font pattern briefly\n"));
95 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
96 fprintf (file, _(" -V, --version display font config version and exit\n"));
97 fprintf (file, _(" -h, --help display this help and exit\n"));
99 fprintf (file, _(" -s, (sort) display sorted list of matches\n"));
100 fprintf (file, _(" -a (all) display unpruned sorted list of matches\n"));
101 fprintf (file, _(" -v (verbose) display entire font pattern verbosely\n"));
102 fprintf (file, _(" -b (brief) display entire font pattern briefly\n"));
103 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
104 fprintf (file, _(" -V (version) display font config version and exit\n"));
105 fprintf (file, _(" -h (help) display this help and exit\n"));
111 main (int argc, char **argv)
115 int sort = 0, all = 0;
116 const FcChar8 *format = NULL;
122 #if HAVE_GETOPT_LONG || HAVE_GETOPT
125 setlocale (LC_ALL, "");
127 while ((c = getopt_long (argc, argv, "asvbf:Vh", longopts, NULL)) != -1)
129 while ((c = getopt (argc, argv, "asvbf:Vh")) != -1)
146 format = (FcChar8 *) strdup (optarg);
149 fprintf (stderr, "fontconfig version %d.%d.%d\n",
150 FC_MAJOR, FC_MINOR, FC_REVISION);
165 pat = FcNameParse ((FcChar8 *) argv[i]);
168 fprintf (stderr, _("Unable to parse the pattern\n"));
174 os = FcObjectSetCreate ();
175 FcObjectSetAdd (os, argv[i]);
179 pat = FcPatternCreate ();
184 FcConfigSubstitute (0, pat, FcMatchPattern);
185 FcDefaultSubstitute (pat);
187 fs = FcFontSetCreate ();
191 FcFontSet *font_patterns;
193 font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
195 if (!font_patterns || font_patterns->nfont == 0)
197 fprintf (stderr, _("No fonts installed on the system\n"));
200 for (j = 0; j < font_patterns->nfont; j++)
202 FcPattern *font_pattern;
204 font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]);
206 FcFontSetAdd (fs, font_pattern);
209 FcFontSetSortDestroy (font_patterns);
214 match = FcFontMatch (0, pat, &result);
216 FcFontSetAdd (fs, match);
218 FcPatternDestroy (pat);
223 format = (const FcChar8 *) "%{=unparse}\n";
225 format = (const FcChar8 *) "%{=fcmatch}\n";
232 for (j = 0; j < fs->nfont; j++)
236 font = FcPatternFilter (fs->fonts[j], os);
238 if (verbose || brief)
242 FcPatternDel (font, FC_CHARSET);
243 FcPatternDel (font, FC_LANG);
245 FcPatternPrint (font);
251 s = FcPatternFormat (font, format);
259 FcPatternDestroy (font);
261 FcFontSetDestroy (fs);
265 FcObjectSetDestroy (os);