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>
46 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
48 #define dgettext(d, s) (s)
55 #ifndef HAVE_GETOPT_LONG
56 #define HAVE_GETOPT_LONG 0
63 static const struct option longopts[] = {
66 {"verbose", 0, 0, 'v'},
68 {"format", 1, 0, 'f'},
69 {"version", 0, 0, 'V'},
76 extern int optind, opterr, optopt;
81 usage (char *program, int error)
83 FILE *file = error ? stderr : stdout;
85 fprintf (file, _("usage: %s [-savbVh] [-f FORMAT] [--sort] [--all] [--verbose] [--brief] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
88 fprintf (file, _("usage: %s [-savVh] [-f FORMAT] [pattern] {element...}\n"),
91 fprintf (file, _("List best font matching [pattern]\n"));
94 fprintf (file, _(" -s, --sort display sorted list of matches\n"));
95 fprintf (file, _(" -a, --all display unpruned sorted list of matches\n"));
96 fprintf (file, _(" -v, --verbose display entire font pattern verbosely\n"));
97 fprintf (file, _(" -b, --brief display entire font pattern briefly\n"));
98 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
99 fprintf (file, _(" -V, --version display font config version and exit\n"));
100 fprintf (file, _(" -h, --help display this help and exit\n"));
102 fprintf (file, _(" -s, (sort) display sorted list of matches\n"));
103 fprintf (file, _(" -a (all) display unpruned sorted list of matches\n"));
104 fprintf (file, _(" -v (verbose) display entire font pattern verbosely\n"));
105 fprintf (file, _(" -b (brief) display entire font pattern briefly\n"));
106 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
107 fprintf (file, _(" -V (version) display font config version and exit\n"));
108 fprintf (file, _(" -h (help) display this help and exit\n"));
114 main (int argc, char **argv)
118 int sort = 0, all = 0;
119 const FcChar8 *format = NULL;
120 const FcChar8 *format_optarg = NULL;
126 #if HAVE_GETOPT_LONG || HAVE_GETOPT
129 setlocale (LC_ALL, "");
131 while ((c = getopt_long (argc, argv, "asvbf:Vh", longopts, NULL)) != -1)
133 while ((c = getopt (argc, argv, "asvbf:Vh")) != -1)
150 format = format_optarg = (FcChar8 *) strdup (optarg);
153 fprintf (stderr, "fontconfig version %d.%d.%d\n",
154 FC_MAJOR, FC_MINOR, FC_REVISION);
169 pat = FcNameParse ((FcChar8 *) argv[i]);
172 fprintf (stderr, _("Unable to parse the pattern\n"));
178 os = FcObjectSetCreate ();
179 FcObjectSetAdd (os, argv[i]);
183 pat = FcPatternCreate ();
188 FcConfigSubstitute (0, pat, FcMatchPattern);
189 FcDefaultSubstitute (pat);
191 fs = FcFontSetCreate ();
195 FcFontSet *font_patterns;
197 font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
199 if (!font_patterns || font_patterns->nfont == 0)
201 fprintf (stderr, _("No fonts installed on the system\n"));
204 for (j = 0; j < font_patterns->nfont; j++)
206 FcPattern *font_pattern;
208 font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]);
210 FcFontSetAdd (fs, font_pattern);
213 FcFontSetSortDestroy (font_patterns);
218 match = FcFontMatch (0, pat, &result);
220 FcFontSetAdd (fs, match);
222 FcPatternDestroy (pat);
227 format = (const FcChar8 *) "%{=unparse}\n";
229 format = (const FcChar8 *) "%{=fcmatch}\n";
236 for (j = 0; j < fs->nfont; j++)
240 font = FcPatternFilter (fs->fonts[j], os);
242 if (verbose || brief)
246 FcPatternDel (font, FC_CHARSET);
247 FcPatternDel (font, FC_LANG);
249 FcPatternPrint (font);
255 s = FcPatternFormat (font, format);
263 FcPatternDestroy (font);
265 FcFontSetDestroy (fs);
269 FcObjectSetDestroy (os);
274 free ((void*)format_optarg);
275 format_optarg = NULL;