]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * fontconfig/fc-list/fc-list.c | |
3 | * | |
4 | * Copyright © 2002 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 | #include <string.h> | |
30 | #ifdef HAVE_CONFIG_H | |
31 | #include <config.h> | |
32 | #else | |
33 | #ifdef linux | |
34 | #define HAVE_GETOPT_LONG 1 | |
35 | #endif | |
36 | #define HAVE_GETOPT 1 | |
37 | #endif | |
38 | ||
39 | #ifndef HAVE_GETOPT | |
40 | #define HAVE_GETOPT 0 | |
41 | #endif | |
42 | #ifndef HAVE_GETOPT_LONG | |
43 | #define HAVE_GETOPT_LONG 0 | |
44 | #endif | |
45 | ||
46 | #if HAVE_GETOPT_LONG | |
47 | #undef _GNU_SOURCE | |
48 | #define _GNU_SOURCE | |
49 | #include <getopt.h> | |
50 | const struct option longopts[] = { | |
51 | {"verbose", 0, 0, 'v'}, | |
52 | {"format", 1, 0, 'f'}, | |
53 | {"quiet", 0, 0, 'q'}, | |
54 | {"version", 0, 0, 'V'}, | |
55 | {"help", 0, 0, 'h'}, | |
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 | |
66 | usage (char *program, int error) | |
67 | { | |
68 | FILE *file = error ? stderr : stdout; | |
69 | #if HAVE_GETOPT_LONG | |
70 | fprintf (file, "usage: %s [-vqVh] [-f FORMAT] [--verbose] [--format=FORMAT] [--quiet] [--version] [--help] [pattern] {element ...} \n", | |
71 | program); | |
72 | #else | |
73 | fprintf (file, "usage: %s [-vqVh] [-f FORMAT] [pattern] {element ...} \n", | |
74 | program); | |
75 | #endif | |
76 | fprintf (file, "List fonts matching [pattern]\n"); | |
77 | fprintf (file, "\n"); | |
78 | #if HAVE_GETOPT_LONG | |
79 | fprintf (file, " -v, --verbose display entire font pattern verbosely\n"); | |
80 | fprintf (file, " -f, --format=FORMAT use the given output format\n"); | |
81 | fprintf (file, " -q, --quiet suppress all normal output, exit 1 if no fonts matched\n"); | |
82 | fprintf (file, " -V, --version display font config version and exit\n"); | |
83 | fprintf (file, " -h, --help display this help and exit\n"); | |
84 | #else | |
85 | fprintf (file, " -v (verbose) display entire font pattern verbosely\n"); | |
86 | fprintf (file, " -f FORMAT (format) use the given output format\n"); | |
87 | fprintf (file, " -q, (quiet) suppress all normal output, exit 1 if no fonts matched\n"); | |
88 | fprintf (file, " -V (version) display font config version and exit\n"); | |
89 | fprintf (file, " -h (help) display this help and exit\n"); | |
90 | #endif | |
91 | exit (error); | |
92 | } | |
93 | ||
94 | int | |
95 | main (int argc, char **argv) | |
96 | { | |
97 | int verbose = 0; | |
98 | int quiet = 0; | |
99 | FcChar8 *format = NULL; | |
100 | int nfont = 0; | |
101 | int i; | |
102 | FcObjectSet *os = 0; | |
103 | FcFontSet *fs; | |
104 | FcPattern *pat; | |
105 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
106 | int c; | |
107 | ||
108 | #if HAVE_GETOPT_LONG | |
109 | while ((c = getopt_long (argc, argv, "vf:qVh", longopts, NULL)) != -1) | |
110 | #else | |
111 | while ((c = getopt (argc, argv, "vf:qVh")) != -1) | |
112 | #endif | |
113 | { | |
114 | switch (c) { | |
115 | case 'v': | |
116 | verbose = 1; | |
117 | break; | |
118 | case 'f': | |
119 | format = (FcChar8 *) strdup (optarg); | |
120 | break; | |
121 | case 'q': | |
122 | quiet = 1; | |
123 | break; | |
124 | case 'V': | |
125 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
126 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
127 | exit (0); | |
128 | case 'h': | |
129 | usage (argv[0], 0); | |
130 | default: | |
131 | usage (argv[0], 1); | |
132 | } | |
133 | } | |
134 | i = optind; | |
135 | #else | |
136 | i = 1; | |
137 | #endif | |
138 | ||
139 | if (!FcInit ()) | |
140 | { | |
141 | fprintf (stderr, "Can't init font config library\n"); | |
142 | return 1; | |
143 | } | |
144 | if (argv[i]) | |
145 | { | |
146 | pat = FcNameParse ((FcChar8 *) argv[i]); | |
147 | while (argv[++i]) | |
148 | { | |
149 | if (!os) | |
150 | os = FcObjectSetCreate (); | |
151 | FcObjectSetAdd (os, argv[i]); | |
152 | } | |
153 | } | |
154 | else | |
155 | pat = FcPatternCreate (); | |
156 | if (quiet && !os) | |
157 | os = FcObjectSetCreate (); | |
158 | if (!verbose && !format && !os) | |
159 | os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0); | |
160 | fs = FcFontList (0, pat, os); | |
161 | if (os) | |
162 | FcObjectSetDestroy (os); | |
163 | if (pat) | |
164 | FcPatternDestroy (pat); | |
165 | ||
166 | if (!quiet && fs) | |
167 | { | |
168 | int j; | |
169 | ||
170 | for (j = 0; j < fs->nfont; j++) | |
171 | { | |
172 | if (verbose) | |
173 | { | |
174 | FcPatternPrint (fs->fonts[j]); | |
175 | } | |
176 | else if (format) | |
177 | { | |
178 | FcChar8 *s; | |
179 | ||
180 | s = FcPatternFormat (fs->fonts[j], format); | |
181 | if (s) | |
182 | { | |
183 | printf ("%s", s); | |
184 | free (s); | |
185 | } | |
186 | } | |
187 | else | |
188 | { | |
189 | FcChar8 *str; | |
190 | FcChar8 *file; | |
191 | ||
192 | str = FcNameUnparse (fs->fonts[j]); | |
193 | if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) | |
194 | printf ("%s: ", file); | |
195 | printf ("%s\n", str); | |
196 | free (str); | |
197 | } | |
198 | } | |
199 | } | |
200 | ||
201 | if (fs) { | |
202 | nfont = fs->nfont; | |
203 | FcFontSetDestroy (fs); | |
204 | } | |
205 | ||
206 | FcFini (); | |
207 | ||
208 | return quiet ? (nfont == 0 ? 1 : 0) : 0; | |
209 | } |