]> git.wh0rd.org - fontconfig.git/blob - fc-match/fc-match.c
Add fc-match program
[fontconfig.git] / fc-match / fc-match.c
1 /*
2 * $RCSId: xc/lib/fontconfig/fc-list/fc-list.c,v 1.5 2002/06/30 23:45:40 keithp Exp $
3 *
4 * Copyright © 2003 Keith Packard
5 *
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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.
23 */
24
25 #include <fontconfig/fontconfig.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #ifdef HAVE_CONFIG_H
30 #include <config.h>
31 #else
32 #ifdef linux
33 #define HAVE_GETOPT_LONG 1
34 #endif
35 #define HAVE_GETOPT 1
36 #endif
37
38 #ifndef HAVE_GETOPT
39 #define HAVE_GETOPT 0
40 #endif
41 #ifndef HAVE_GETOPT_LONG
42 #define HAVE_GETOPT_LONG 0
43 #endif
44
45 #if HAVE_GETOPT_LONG
46 #undef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #include <getopt.h>
49 const struct option longopts[] = {
50 {"sort", 0, 0, 's'},
51 {"version", 0, 0, 'V'},
52 {"verbose", 0, 0, 'v'},
53 {"help", 0, 0, '?'},
54 {NULL,0,0,0},
55 };
56 #else
57 #if HAVE_GETOPT
58 extern char *optarg;
59 extern int optind, opterr, optopt;
60 #endif
61 #endif
62
63 static void usage (char *program)
64 {
65 #if HAVE_GETOPT_LONG
66 fprintf (stderr, "usage: %s [-svV?] [--sort] [--verbose] [--version] [--help] [pattern]\n",
67 program);
68 #else
69 fprintf (stderr, "usage: %s [-svV?] [pattern]\n",
70 program);
71 #endif
72 fprintf (stderr, "List fonts matching [pattern]\n");
73 fprintf (stderr, "\n");
74 #if HAVE_GETOPT_LONG
75 fprintf (stderr, " -s, --sort display sorted list of matches\n");
76 fprintf (stderr, " -v, --verbose display entire font pattern\n");
77 fprintf (stderr, " -V, --version display font config version and exit\n");
78 fprintf (stderr, " -?, --help display this help and exit\n");
79 #else
80 fprintf (stderr, " -s, (sort) display sorted list of matches\n");
81 fprintf (stderr, " -v (verbose) display entire font pattern\n");
82 fprintf (stderr, " -V (version) display font config version and exit\n");
83 fprintf (stderr, " -? (help) display this help and exit\n");
84 #endif
85 exit (1);
86 }
87
88 int
89 main (int argc, char **argv)
90 {
91 int verbose = 0;
92 int sort = 0;
93 int i;
94 FcObjectSet *os = 0;
95 FcFontSet *fs;
96 FcPattern *pat;
97 FcResult result;
98 #if HAVE_GETOPT_LONG || HAVE_GETOPT
99 int c;
100
101 #if HAVE_GETOPT_LONG
102 while ((c = getopt_long (argc, argv, "Vv?", longopts, NULL)) != -1)
103 #else
104 while ((c = getopt (argc, argv, "sVv?")) != -1)
105 #endif
106 {
107 switch (c) {
108 case 's':
109 sort = 1;
110 break;
111 case 'V':
112 fprintf (stderr, "fontconfig version %d.%d.%d\n",
113 FC_MAJOR, FC_MINOR, FC_REVISION);
114 exit (0);
115 case 'v':
116 verbose = 1;
117 break;
118 default:
119 usage (argv[0]);
120 }
121 }
122 i = optind;
123 #else
124 i = 1;
125 #endif
126
127 if (!FcInit ())
128 {
129 fprintf (stderr, "Can't init font config library\n");
130 return 1;
131 }
132 if (argv[i])
133 pat = FcNameParse ((FcChar8 *) argv[i]);
134 else
135 pat = FcPatternCreate ();
136
137 FcConfigSubstitute (0, pat, FcMatchPattern);
138 FcDefaultSubstitute (pat);
139
140 if (sort)
141 fs = FcFontSort (0, pat, FcTrue, 0, &result);
142 else
143 {
144 FcPattern *match;
145 fs = FcFontSetCreate ();
146 match = FcFontMatch (0, pat, &result);
147 if (match)
148 FcFontSetAdd (fs, match);
149 }
150 if (pat)
151 FcPatternDestroy (pat);
152
153 if (fs)
154 {
155 int j;
156
157 for (j = 0; j < fs->nfont; j++)
158 {
159 if (verbose)
160 {
161 FcPatternPrint (fs->fonts[j]);
162 }
163 else
164 {
165 FcChar8 *family;
166 FcChar8 *style;
167 FcChar8 *file;
168
169 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch)
170 file = "<unknown filename>";
171 else
172 {
173 FcChar8 *slash = strrchr (file, '/');
174 if (slash)
175 file = slash+1;
176 }
177 if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
178 family = "<unknown family>";
179 if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch)
180 file = "<unknown style>";
181
182 printf ("%s: \"%s\" \"%s\"\n", file, family, style);
183 }
184 }
185 FcFontSetDestroy (fs);
186 }
187 return 0;
188 }