1 /*
2 * fontconfig/fc-scan/fc-scan.c
3 *
4 * Copyright © 2003 Keith Packard
5 * Copyright © 2008 Red Hat, Inc.
6 * Red Hat Author(s): Behdad Esfahbod
7 *
8 * Permission to use, copy, modify, distribute, and sell this software and its
9 * documentation for any purpose is hereby granted without fee, provided that
10 * the above copyright notice appear in all copies and that both that
11 * copyright notice and this permission notice appear in supporting
12 * documentation, and that the name of the author(s) not be used in
13 * advertising or publicity pertaining to distribution of the software without
14 * specific, written prior permission. The authors make no
15 * representations about the suitability of this software for any purpose. It
16 * is provided "as is" without express or implied warranty.
17 *
18 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
24 * PERFORMANCE OF THIS SOFTWARE.
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #else
30 #ifdef linux
31 #define HAVE_GETOPT_LONG 1
32 #endif
33 #define HAVE_GETOPT 1
34 #endif
35
36 #include <fontconfig/fontconfig.h>
37 #include <fontconfig/fcfreetype.h>
38 #include <stdio.h>
39 #include <unistd.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <locale.h>
43
44 #ifdef ENABLE_NLS
45 #include <libintl.h>
46 #define _(x) (dgettext(GETTEXT_PACKAGE, x))
47 #else
48 #define dgettext(d, s) (s)
49 #define _(x) (x)
50 #endif
51
52 #ifndef HAVE_GETOPT
53 #define HAVE_GETOPT 0
54 #endif
55 #ifndef HAVE_GETOPT_LONG
56 #define HAVE_GETOPT_LONG 0
57 #endif
58
59 #if HAVE_GETOPT_LONG
60 #undef _GNU_SOURCE
61 #define _GNU_SOURCE
62 #include <getopt.h>
63 static const struct option longopts[] = {
64 {"brief", 0, 0, 'b'},
65 {"format", 1, 0, 'f'},
66 {"version", 0, 0, 'V'},
67 {"help", 0, 0, 'h'},
68 {NULL,0,0,0},
69 };
70 #else
71 #if HAVE_GETOPT
72 extern char *optarg;
73 extern int optind, opterr, optopt;
74 #endif
75 #endif
76
77 static void
usage(char * program,int error)78 usage (char *program, int error)
79 {
80 FILE *file = error ? stderr : stdout;
81 #if HAVE_GETOPT_LONG
82 fprintf (file, _("usage: %s [-bVh] [-f FORMAT] [--brief] [--format FORMAT] [--version] [--help] font-file...\n"),
83 program);
84 #else
85 fprintf (file, _("usage: %s [-bVh] [-f FORMAT] font-file...\n"),
86 program);
87 #endif
88 fprintf (file, _("Scan font files and directories, and print resulting pattern(s)\n"));
89 fprintf (file, "\n");
90 #if HAVE_GETOPT_LONG
91 fprintf (file, _(" -b, --brief display font pattern briefly\n"));
92 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
93 fprintf (file, _(" -V, --version display font config version and exit\n"));
94 fprintf (file, _(" -h, --help display this help and exit\n"));
95 #else
96 fprintf (file, _(" -b (brief) display font pattern briefly\n"));
97 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
98 fprintf (file, _(" -V (version) display font config version and exit\n"));
99 fprintf (file, _(" -h (help) display this help and exit\n"));
100 #endif
101 exit (error);
102 }
103
104 int
main(int argc,char ** argv)105 main (int argc, char **argv)
106 {
107 int brief = 0;
108 FcChar8 *format = NULL;
109 int i;
110 FcFontSet *fs;
111 #if HAVE_GETOPT_LONG || HAVE_GETOPT
112 int c;
113
114 setlocale (LC_ALL, "");
115 #if HAVE_GETOPT_LONG
116 while ((c = getopt_long (argc, argv, "bf:Vh", longopts, NULL)) != -1)
117 #else
118 while ((c = getopt (argc, argv, "bf:Vh")) != -1)
119 #endif
120 {
121 switch (c) {
122 case 'b':
123 brief = 1;
124 break;
125 case 'f':
126 format = (FcChar8 *) strdup (optarg);
127 break;
128 case 'V':
129 fprintf (stderr, "fontconfig version %d.%d.%d\n",
130 FC_MAJOR, FC_MINOR, FC_REVISION);
131 exit (0);
132 case 'h':
133 usage (argv[0], 0);
134 default:
135 usage (argv[0], 1);
136 }
137 }
138 i = optind;
139 #else
140 i = 1;
141 #endif
142
143 if (i == argc)
144 usage (argv[0], 1);
145
146 fs = FcFontSetCreate ();
147
148 for (; i < argc; i++)
149 {
150 const FcChar8 *file = (FcChar8*) argv[i];
151
152 if (!FcFileIsDir (file))
153 FcFileScan (fs, NULL, NULL, NULL, file, FcTrue);
154 else
155 {
156 FcStrSet *dirs = FcStrSetCreate ();
157 FcStrList *strlist = FcStrListCreate (dirs);
158 do
159 {
160 FcDirScan (fs, dirs, NULL, NULL, file, FcTrue);
161 }
162 while ((file = FcStrListNext (strlist)));
163 FcStrListDone (strlist);
164 FcStrSetDestroy (dirs);
165 }
166 }
167
168 for (i = 0; i < fs->nfont; i++)
169 {
170 FcPattern *pat = fs->fonts[i];
171
172 if (brief)
173 {
174 FcPatternDel (pat, FC_CHARSET);
175 FcPatternDel (pat, FC_LANG);
176 }
177
178 if (format)
179 {
180 FcChar8 *s;
181
182 s = FcPatternFormat (pat, format);
183 if (s)
184 {
185 printf ("%s", s);
186 FcStrFree (s);
187 }
188 }
189 else
190 {
191 FcPatternPrint (pat);
192 }
193 }
194
195 FcFontSetDestroy (fs);
196
197 FcFini ();
198 return i > 0 ? 0 : 1;
199 }
200