]>
Commit | Line | Data |
---|---|---|
8bc4bc13 | 1 | /* |
317b8492 | 2 | * fontconfig/fc-match/fc-match.c |
8bc4bc13 | 3 | * |
46b51147 | 4 | * Copyright © 2003 Keith Packard |
8bc4bc13 KP |
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 | ||
8bc4bc13 KP |
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 | ||
f045376c PL |
34 | #include <fontconfig/fontconfig.h> |
35 | #include <stdio.h> | |
36 | #include <unistd.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | ||
8bc4bc13 KP |
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> | |
0d745819 | 51 | static const struct option longopts[] = { |
8bc4bc13 | 52 | {"sort", 0, 0, 's'}, |
c014142a | 53 | {"all", 0, 0, 'a'}, |
8bc4bc13 | 54 | {"verbose", 0, 0, 'v'}, |
0c93b91d BE |
55 | {"format", 1, 0, 'f'}, |
56 | {"version", 0, 0, 'V'}, | |
1439c8f2 | 57 | {"help", 0, 0, 'h'}, |
8bc4bc13 KP |
58 | {NULL,0,0,0}, |
59 | }; | |
60 | #else | |
61 | #if HAVE_GETOPT | |
62 | extern char *optarg; | |
63 | extern int optind, opterr, optopt; | |
64 | #endif | |
65 | #endif | |
66 | ||
1439c8f2 BE |
67 | static void |
68 | usage (char *program, int error) | |
8bc4bc13 | 69 | { |
1439c8f2 | 70 | FILE *file = error ? stderr : stdout; |
8bc4bc13 | 71 | #if HAVE_GETOPT_LONG |
0c93b91d | 72 | fprintf (file, "usage: %s [-savVh] [-f FORMAT] [--sort] [--all] [--verbose] [--format=FORMAT] [--version] [--help] [pattern]\n", |
8bc4bc13 KP |
73 | program); |
74 | #else | |
0c93b91d | 75 | fprintf (file, "usage: %s [-savVh] [-f FORMAT] [pattern]\n", |
8bc4bc13 KP |
76 | program); |
77 | #endif | |
1439c8f2 BE |
78 | fprintf (file, "List fonts matching [pattern]\n"); |
79 | fprintf (file, "\n"); | |
8bc4bc13 | 80 | #if HAVE_GETOPT_LONG |
1439c8f2 BE |
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\n"); | |
0c93b91d | 84 | fprintf (file, " -f, --format=FORMAT use the given output format\n"); |
1439c8f2 BE |
85 | fprintf (file, " -V, --version display font config version and exit\n"); |
86 | fprintf (file, " -h, --help display this help and exit\n"); | |
8bc4bc13 | 87 | #else |
1439c8f2 BE |
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\n"); | |
0c93b91d | 91 | fprintf (file, " -f FORMAT (format) use the given output format\n"); |
1439c8f2 BE |
92 | fprintf (file, " -V (version) display font config version and exit\n"); |
93 | fprintf (file, " -h (help) display this help and exit\n"); | |
8bc4bc13 | 94 | #endif |
1439c8f2 | 95 | exit (error); |
8bc4bc13 KP |
96 | } |
97 | ||
98 | int | |
99 | main (int argc, char **argv) | |
100 | { | |
101 | int verbose = 0; | |
c014142a | 102 | int sort = 0, all = 0; |
0c93b91d | 103 | FcChar8 *format = NULL; |
8bc4bc13 | 104 | int i; |
8bc4bc13 KP |
105 | FcFontSet *fs; |
106 | FcPattern *pat; | |
107 | FcResult result; | |
108 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
109 | int c; | |
110 | ||
111 | #if HAVE_GETOPT_LONG | |
0c93b91d | 112 | while ((c = getopt_long (argc, argv, "asvf:Vh", longopts, NULL)) != -1) |
8bc4bc13 | 113 | #else |
0c93b91d | 114 | while ((c = getopt (argc, argv, "asvf:Vh")) != -1) |
8bc4bc13 KP |
115 | #endif |
116 | { | |
117 | switch (c) { | |
c014142a KP |
118 | case 'a': |
119 | all = 1; | |
120 | break; | |
8bc4bc13 KP |
121 | case 's': |
122 | sort = 1; | |
123 | break; | |
0c93b91d BE |
124 | case 'v': |
125 | verbose = 1; | |
126 | break; | |
127 | case 'f': | |
128 | format = (FcChar8 *) strdup (optarg); | |
129 | break; | |
8bc4bc13 KP |
130 | case 'V': |
131 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
132 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
133 | exit (0); | |
1439c8f2 BE |
134 | case 'h': |
135 | usage (argv[0], 0); | |
8bc4bc13 | 136 | default: |
1439c8f2 | 137 | usage (argv[0], 1); |
8bc4bc13 KP |
138 | } |
139 | } | |
140 | i = optind; | |
141 | #else | |
142 | i = 1; | |
143 | #endif | |
144 | ||
145 | if (!FcInit ()) | |
146 | { | |
147 | fprintf (stderr, "Can't init font config library\n"); | |
148 | return 1; | |
149 | } | |
150 | if (argv[i]) | |
151 | pat = FcNameParse ((FcChar8 *) argv[i]); | |
152 | else | |
153 | pat = FcPatternCreate (); | |
154 | ||
ae2aafe6 PL |
155 | if (!pat) |
156 | return 1; | |
157 | ||
8bc4bc13 KP |
158 | FcConfigSubstitute (0, pat, FcMatchPattern); |
159 | FcDefaultSubstitute (pat); | |
160 | ||
0602c605 BE |
161 | fs = FcFontSetCreate (); |
162 | ||
c014142a | 163 | if (sort || all) |
0602c605 BE |
164 | { |
165 | FcFontSet *font_patterns; | |
166 | int j; | |
c014142a | 167 | font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result); |
0602c605 BE |
168 | |
169 | for (j = 0; j < font_patterns->nfont; j++) | |
170 | { | |
171 | FcPattern *font_pattern; | |
172 | ||
173 | font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]); | |
174 | if (font_pattern) | |
175 | FcFontSetAdd (fs, font_pattern); | |
176 | } | |
177 | ||
178 | FcFontSetSortDestroy (font_patterns); | |
179 | } | |
8bc4bc13 KP |
180 | else |
181 | { | |
182 | FcPattern *match; | |
8bc4bc13 KP |
183 | match = FcFontMatch (0, pat, &result); |
184 | if (match) | |
185 | FcFontSetAdd (fs, match); | |
186 | } | |
ae2aafe6 | 187 | FcPatternDestroy (pat); |
8bc4bc13 KP |
188 | |
189 | if (fs) | |
190 | { | |
191 | int j; | |
192 | ||
193 | for (j = 0; j < fs->nfont; j++) | |
194 | { | |
195 | if (verbose) | |
196 | { | |
197 | FcPatternPrint (fs->fonts[j]); | |
198 | } | |
0c93b91d BE |
199 | else if (format) |
200 | { | |
201 | FcChar8 *s; | |
202 | ||
203 | s = FcPatternFormat (fs->fonts[j], format); | |
204 | printf ("%s", s); | |
205 | free (s); | |
206 | } | |
8bc4bc13 KP |
207 | else |
208 | { | |
209 | FcChar8 *family; | |
210 | FcChar8 *style; | |
211 | FcChar8 *file; | |
212 | ||
213 | if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch) | |
8245771d | 214 | file = (FcChar8 *) "<unknown filename>"; |
8bc4bc13 KP |
215 | else |
216 | { | |
8245771d | 217 | FcChar8 *slash = (FcChar8 *) strrchr ((char *) file, '/'); |
8bc4bc13 KP |
218 | if (slash) |
219 | file = slash+1; | |
220 | } | |
221 | if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch) | |
8245771d | 222 | family = (FcChar8 *) "<unknown family>"; |
8bc4bc13 | 223 | if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch) |
bdbc26f3 | 224 | style = (FcChar8 *) "<unknown style>"; |
8bc4bc13 KP |
225 | |
226 | printf ("%s: \"%s\" \"%s\"\n", file, family, style); | |
227 | } | |
228 | } | |
229 | FcFontSetDestroy (fs); | |
230 | } | |
2d3387fd | 231 | FcFini (); |
8bc4bc13 KP |
232 | return 0; |
233 | } |