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 #ifndef HAVE_GETOPT_LONG
44 #define HAVE_GETOPT_LONG 0
51 static const struct option longopts[] = {
54 {"verbose", 0, 0, 'v'},
55 {"format", 1, 0, 'f'},
56 {"version", 0, 0, 'V'},
63 extern int optind, opterr, optopt;
68 usage (char *program, int error)
70 FILE *file = error ? stderr : stdout;
72 fprintf (file, "usage: %s [-savVh] [-f FORMAT] [--sort] [--all] [--verbose] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n",
75 fprintf (file, "usage: %s [-savVh] [-f FORMAT] [pattern] {element...}\n",
78 fprintf (file, "List best font matching [pattern]\n");
81 fprintf (file, " -s, --sort display sorted list of matches\n");
82 fprintf (file, " -a, --all display unpruned sorted list of matches\n");
83 fprintf (file, " -v, --verbose display entire font pattern verbosely\n");
84 fprintf (file, " -f, --format=FORMAT use the given output format\n");
85 fprintf (file, " -V, --version display font config version and exit\n");
86 fprintf (file, " -h, --help display this help and exit\n");
88 fprintf (file, " -s, (sort) display sorted list of matches\n");
89 fprintf (file, " -a (all) display unpruned sorted list of matches\n");
90 fprintf (file, " -v (verbose) display entire font pattern verbosely\n");
91 fprintf (file, " -f FORMAT (format) use the given output format\n");
92 fprintf (file, " -V (version) display font config version and exit\n");
93 fprintf (file, " -h (help) display this help and exit\n");
99 main (int argc, char **argv)
102 int sort = 0, all = 0;
103 const FcChar8 *format = NULL;
109 #if HAVE_GETOPT_LONG || HAVE_GETOPT
113 while ((c = getopt_long (argc, argv, "asvf:Vh", longopts, NULL)) != -1)
115 while ((c = getopt (argc, argv, "asvf:Vh")) != -1)
129 format = (FcChar8 *) strdup (optarg);
132 fprintf (stderr, "fontconfig version %d.%d.%d\n",
133 FC_MAJOR, FC_MINOR, FC_REVISION);
148 pat = FcNameParse ((FcChar8 *) argv[i]);
151 fputs ("Unable to parse the pattern\n", stderr);
157 os = FcObjectSetCreate ();
158 FcObjectSetAdd (os, argv[i]);
162 pat = FcPatternCreate ();
167 FcConfigSubstitute (0, pat, FcMatchPattern);
168 FcDefaultSubstitute (pat);
170 fs = FcFontSetCreate ();
174 FcFontSet *font_patterns;
176 font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
178 if (!font_patterns || font_patterns->nfont == 0)
180 fputs("No fonts installed on the system\n", stderr);
183 for (j = 0; j < font_patterns->nfont; j++)
185 FcPattern *font_pattern;
187 font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]);
189 FcFontSetAdd (fs, font_pattern);
192 FcFontSetSortDestroy (font_patterns);
197 match = FcFontMatch (0, pat, &result);
199 FcFontSetAdd (fs, match);
201 FcPatternDestroy (pat);
206 format = (const FcChar8 *) "%{=unparse}\n";
208 format = (const FcChar8 *) "%{=fcmatch}\n";
215 for (j = 0; j < fs->nfont; j++)
219 font = FcPatternFilter (fs->fonts[j], os);
223 FcPatternPrint (font);
229 s = FcPatternFormat (font, format);
237 FcPatternDestroy (font);
239 FcFontSetDestroy (fs);
243 FcObjectSetDestroy (os);