]> git.wh0rd.org - fontconfig.git/blob - fc-list/fc-list.c
Implement FcPatternFormat and use it in cmdline tools (bug #17107)
[fontconfig.git] / fc-list / fc-list.c
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\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\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 if (!verbose)
148 while (argv[++i])
149 {
150 if (!os)
151 os = FcObjectSetCreate ();
152 FcObjectSetAdd (os, argv[i]);
153 }
154 }
155 else
156 pat = FcPatternCreate ();
157 if (quiet && !os)
158 os = FcObjectSetCreate ();
159 if (!verbose && !format && !os)
160 os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0);
161 fs = FcFontList (0, pat, os);
162 if (os)
163 FcObjectSetDestroy (os);
164 if (pat)
165 FcPatternDestroy (pat);
166
167 if (!quiet && fs)
168 {
169 int j;
170
171 for (j = 0; j < fs->nfont; j++)
172 {
173 FcChar8 *font;
174 FcChar8 *file;
175
176 if (verbose)
177 {
178 FcPatternPrint (fs->fonts[j]);
179 }
180 else if (format)
181 {
182 FcChar8 *s;
183
184 s = FcPatternFormat (fs->fonts[j], format);
185 printf ("%s", s);
186 free (s);
187 }
188 else
189 {
190 font = FcNameUnparse (fs->fonts[j]);
191 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch)
192 printf ("%s: ", file);
193 printf ("%s\n", font);
194 free (font);
195 }
196 }
197 }
198
199 if (fs) {
200 nfont = fs->nfont;
201 FcFontSetDestroy (fs);
202 }
203
204 FcFini ();
205
206 return quiet ? (nfont == 0 ? 1 : 0) : 0;
207 }