]> git.wh0rd.org - fontconfig.git/blob - fc-match/fc-match.c
Skip broken caches. Cache files are auto-written, don't rewrite in fc-cache.
[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 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #else
28 #ifdef linux
29 #define HAVE_GETOPT_LONG 1
30 #endif
31 #define HAVE_GETOPT 1
32 #endif
33
34 #include <fontconfig/fontconfig.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #ifndef HAVE_GETOPT
41 #define HAVE_GETOPT 0
42 #endif
43 #ifndef HAVE_GETOPT_LONG
44 #define HAVE_GETOPT_LONG 0
45 #endif
46
47 #if HAVE_GETOPT_LONG
48 #undef _GNU_SOURCE
49 #define _GNU_SOURCE
50 #include <getopt.h>
51 static const struct option longopts[] = {
52 {"sort", 0, 0, 's'},
53 {"version", 0, 0, 'V'},
54 {"verbose", 0, 0, 'v'},
55 {"help", 0, 0, '?'},
56 {NULL,0,0,0},
57 };
58 #else
59 #if HAVE_GETOPT
60 extern char *optarg;
61 extern int optind, opterr, optopt;
62 #endif
63 #endif
64
65 static void usage (char *program)
66 {
67 #if HAVE_GETOPT_LONG
68 fprintf (stderr, "usage: %s [-svV?] [--sort] [--verbose] [--version] [--help] [pattern]\n",
69 program);
70 #else
71 fprintf (stderr, "usage: %s [-svV?] [pattern]\n",
72 program);
73 #endif
74 fprintf (stderr, "List fonts matching [pattern]\n");
75 fprintf (stderr, "\n");
76 #if HAVE_GETOPT_LONG
77 fprintf (stderr, " -s, --sort display sorted list of matches\n");
78 fprintf (stderr, " -v, --verbose display entire font pattern\n");
79 fprintf (stderr, " -V, --version display font config version and exit\n");
80 fprintf (stderr, " -?, --help display this help and exit\n");
81 #else
82 fprintf (stderr, " -s, (sort) display sorted list of matches\n");
83 fprintf (stderr, " -v (verbose) display entire font pattern\n");
84 fprintf (stderr, " -V (version) display font config version and exit\n");
85 fprintf (stderr, " -? (help) display this help and exit\n");
86 #endif
87 exit (1);
88 }
89
90 int
91 main (int argc, char **argv)
92 {
93 int verbose = 0;
94 int sort = 0;
95 int i;
96 FcFontSet *fs;
97 FcPattern *pat;
98 FcResult result;
99 #if HAVE_GETOPT_LONG || HAVE_GETOPT
100 int c;
101
102 #if HAVE_GETOPT_LONG
103 while ((c = getopt_long (argc, argv, "sVv?", longopts, NULL)) != -1)
104 #else
105 while ((c = getopt (argc, argv, "sVv?")) != -1)
106 #endif
107 {
108 switch (c) {
109 case 's':
110 sort = 1;
111 break;
112 case 'V':
113 fprintf (stderr, "fontconfig version %d.%d.%d\n",
114 FC_MAJOR, FC_MINOR, FC_REVISION);
115 exit (0);
116 case 'v':
117 verbose = 1;
118 break;
119 default:
120 usage (argv[0]);
121 }
122 }
123 i = optind;
124 #else
125 i = 1;
126 #endif
127
128 if (!FcInit ())
129 {
130 fprintf (stderr, "Can't init font config library\n");
131 return 1;
132 }
133 if (argv[i])
134 pat = FcNameParse ((FcChar8 *) argv[i]);
135 else
136 pat = FcPatternCreate ();
137
138 if (!pat)
139 return 1;
140
141 FcConfigSubstitute (0, pat, FcMatchPattern);
142 FcDefaultSubstitute (pat);
143
144 if (sort)
145 fs = FcFontSort (0, pat, FcTrue, 0, &result);
146 else
147 {
148 FcPattern *match;
149 fs = FcFontSetCreate ();
150 match = FcFontMatch (0, pat, &result);
151 if (match)
152 FcFontSetAdd (fs, match);
153 }
154 FcPatternDestroy (pat);
155
156 if (fs)
157 {
158 int j;
159
160 for (j = 0; j < fs->nfont; j++)
161 {
162 if (verbose)
163 {
164 FcPatternPrint (fs->fonts[j]);
165 }
166 else
167 {
168 FcChar8 *family;
169 FcChar8 *style;
170 FcChar8 *file;
171
172 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch)
173 file = (FcChar8 *) "<unknown filename>";
174 else
175 {
176 FcChar8 *slash = (FcChar8 *) strrchr ((char *) file, '/');
177 if (slash)
178 file = slash+1;
179 }
180 if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
181 family = (FcChar8 *) "<unknown family>";
182 if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch)
183 file = (FcChar8 *) "<unknown style>";
184
185 printf ("%s: \"%s\" \"%s\"\n", file, family, style);
186 }
187 }
188 FcFontSetDestroy (fs);
189 }
190 FcFini ();
191 return 0;
192 }