]>
Commit | Line | Data |
---|---|---|
8bc4bc13 KP |
1 | /* |
2 | * $RCSId: xc/lib/fontconfig/fc-list/fc-list.c,v 1.5 2002/06/30 23:45:40 keithp Exp $ | |
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 KP |
54 | {"version", 0, 0, 'V'}, |
55 | {"verbose", 0, 0, 'v'}, | |
56 | {"help", 0, 0, '?'}, | |
57 | {NULL,0,0,0}, | |
58 | }; | |
59 | #else | |
60 | #if HAVE_GETOPT | |
61 | extern char *optarg; | |
62 | extern int optind, opterr, optopt; | |
63 | #endif | |
64 | #endif | |
65 | ||
66 | static void usage (char *program) | |
67 | { | |
68 | #if HAVE_GETOPT_LONG | |
c014142a | 69 | fprintf (stderr, "usage: %s [-svV?] [--sort] [--all] [--verbose] [--version] [--help] [pattern]\n", |
8bc4bc13 KP |
70 | program); |
71 | #else | |
72 | fprintf (stderr, "usage: %s [-svV?] [pattern]\n", | |
73 | program); | |
74 | #endif | |
75 | fprintf (stderr, "List fonts matching [pattern]\n"); | |
76 | fprintf (stderr, "\n"); | |
77 | #if HAVE_GETOPT_LONG | |
78 | fprintf (stderr, " -s, --sort display sorted list of matches\n"); | |
c014142a | 79 | fprintf (stderr, " -a, --all display unpruned sorted list of matches\n"); |
8bc4bc13 KP |
80 | fprintf (stderr, " -v, --verbose display entire font pattern\n"); |
81 | fprintf (stderr, " -V, --version display font config version and exit\n"); | |
82 | fprintf (stderr, " -?, --help display this help and exit\n"); | |
83 | #else | |
84 | fprintf (stderr, " -s, (sort) display sorted list of matches\n"); | |
c014142a | 85 | fprintf (stderr, " -a (all) display unpruned sorted list of matches\n"); |
8bc4bc13 KP |
86 | fprintf (stderr, " -v (verbose) display entire font pattern\n"); |
87 | fprintf (stderr, " -V (version) display font config version and exit\n"); | |
88 | fprintf (stderr, " -? (help) display this help and exit\n"); | |
89 | #endif | |
90 | exit (1); | |
91 | } | |
92 | ||
93 | int | |
94 | main (int argc, char **argv) | |
95 | { | |
96 | int verbose = 0; | |
c014142a | 97 | int sort = 0, all = 0; |
8bc4bc13 | 98 | int i; |
8bc4bc13 KP |
99 | FcFontSet *fs; |
100 | FcPattern *pat; | |
101 | FcResult result; | |
102 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
103 | int c; | |
104 | ||
105 | #if HAVE_GETOPT_LONG | |
c014142a | 106 | while ((c = getopt_long (argc, argv, "asVv?", longopts, NULL)) != -1) |
8bc4bc13 | 107 | #else |
c014142a | 108 | while ((c = getopt (argc, argv, "asVv?")) != -1) |
8bc4bc13 KP |
109 | #endif |
110 | { | |
111 | switch (c) { | |
c014142a KP |
112 | case 'a': |
113 | all = 1; | |
114 | break; | |
8bc4bc13 KP |
115 | case 's': |
116 | sort = 1; | |
117 | break; | |
118 | case 'V': | |
119 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
120 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
121 | exit (0); | |
122 | case 'v': | |
123 | verbose = 1; | |
124 | break; | |
125 | default: | |
126 | usage (argv[0]); | |
127 | } | |
128 | } | |
129 | i = optind; | |
130 | #else | |
131 | i = 1; | |
132 | #endif | |
133 | ||
134 | if (!FcInit ()) | |
135 | { | |
136 | fprintf (stderr, "Can't init font config library\n"); | |
137 | return 1; | |
138 | } | |
139 | if (argv[i]) | |
140 | pat = FcNameParse ((FcChar8 *) argv[i]); | |
141 | else | |
142 | pat = FcPatternCreate (); | |
143 | ||
ae2aafe6 PL |
144 | if (!pat) |
145 | return 1; | |
146 | ||
8bc4bc13 KP |
147 | FcConfigSubstitute (0, pat, FcMatchPattern); |
148 | FcDefaultSubstitute (pat); | |
149 | ||
0602c605 BE |
150 | fs = FcFontSetCreate (); |
151 | ||
c014142a | 152 | if (sort || all) |
0602c605 BE |
153 | { |
154 | FcFontSet *font_patterns; | |
155 | int j; | |
c014142a | 156 | font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result); |
0602c605 BE |
157 | |
158 | for (j = 0; j < font_patterns->nfont; j++) | |
159 | { | |
160 | FcPattern *font_pattern; | |
161 | ||
162 | font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]); | |
163 | if (font_pattern) | |
164 | FcFontSetAdd (fs, font_pattern); | |
165 | } | |
166 | ||
167 | FcFontSetSortDestroy (font_patterns); | |
168 | } | |
8bc4bc13 KP |
169 | else |
170 | { | |
171 | FcPattern *match; | |
8bc4bc13 KP |
172 | match = FcFontMatch (0, pat, &result); |
173 | if (match) | |
174 | FcFontSetAdd (fs, match); | |
175 | } | |
ae2aafe6 | 176 | FcPatternDestroy (pat); |
8bc4bc13 KP |
177 | |
178 | if (fs) | |
179 | { | |
180 | int j; | |
181 | ||
182 | for (j = 0; j < fs->nfont; j++) | |
183 | { | |
184 | if (verbose) | |
185 | { | |
186 | FcPatternPrint (fs->fonts[j]); | |
187 | } | |
188 | else | |
189 | { | |
190 | FcChar8 *family; | |
191 | FcChar8 *style; | |
192 | FcChar8 *file; | |
193 | ||
194 | if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch) | |
8245771d | 195 | file = (FcChar8 *) "<unknown filename>"; |
8bc4bc13 KP |
196 | else |
197 | { | |
8245771d | 198 | FcChar8 *slash = (FcChar8 *) strrchr ((char *) file, '/'); |
8bc4bc13 KP |
199 | if (slash) |
200 | file = slash+1; | |
201 | } | |
202 | if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch) | |
8245771d | 203 | family = (FcChar8 *) "<unknown family>"; |
8bc4bc13 | 204 | if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch) |
bdbc26f3 | 205 | style = (FcChar8 *) "<unknown style>"; |
8bc4bc13 KP |
206 | |
207 | printf ("%s: \"%s\" \"%s\"\n", file, family, style); | |
208 | } | |
209 | } | |
210 | FcFontSetDestroy (fs); | |
211 | } | |
2d3387fd | 212 | FcFini (); |
8bc4bc13 KP |
213 | return 0; |
214 | } |