Debian Codegraph @

subprojects/fontconfig/fc-scan/fc-scan.c

raw
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
39 #include <locale.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47
48 #ifdef ENABLE_NLS
49 # include <libintl.h>
50 # define _(x) (dgettext (GETTEXT_PACKAGE, x))
51 #else
52 # define dgettext(d, s) (s)
53 # define _(x) (x)
54 #endif
55
56 #ifndef HAVE_GETOPT
57 # define HAVE_GETOPT 0
58 #endif
59 #ifndef HAVE_GETOPT_LONG
60 # define HAVE_GETOPT_LONG 0
61 #endif
62
63 #if HAVE_GETOPT_LONG
64 # undef _GNU_SOURCE
65 # define _GNU_SOURCE
66 # include <getopt.h>
67 static const struct option longopts[] = {
68 { "brief", 0, 0, 'b' },
69 { "format", 1, 0, 'f' },
70 { "sysroot", required_argument, 0, 'y' },
71 { "version", 0, 0, 'V' },
72 { "help", 0, 0, 'h' },
73 { NULL, 0, 0, 0 },
74 };
75 #else
76 # if HAVE_GETOPT
77 extern char *optarg;
78 extern int optind, opterr, optopt;
79 # endif
80 #endif
81
82 static void
83 usage (char *program, int error)
84 {
85 FILE *file = error ? stderr : stdout;
86 #if HAVE_GETOPT_LONG
87 fprintf (file, _("usage: %s [-bcVh] [-f FORMAT] [-y SYSROOT] [--brief] [--format FORMAT] [--version] [--help] font-file...\n"),
88 program);
89 #else
90 fprintf (file, _("usage: %s [-bcVh] [-f FORMAT] [-y SYSROOT] font-file...\n"),
91 program);
92 #endif
93 fprintf (file, _("Scan font files and directories, and print resulting pattern(s)\n"));
94 fprintf (file, "\n");
95 #if HAVE_GETOPT_LONG
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, _(" -y, --sysroot=SYSROOT prepend SYSROOT to all paths for scanning\n"));
99 fprintf (file, _(" -V, --version display font config version and exit\n"));
100 fprintf (file, _(" -h, --help display this help and exit\n"));
101 #else
102 fprintf (file, _(" -b (brief) display font pattern briefly\n"));
103 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
104 fprintf (file, _(" -y SYSROOT (sysroot) prepend SYSROOT to all paths for scanning\n"));
105 fprintf (file, _(" -V (version) display font config version and exit\n"));
106 fprintf (file, _(" -h (help) display this help and exit\n"));
107 #endif
108 exit (error);
109 }
110
111 int
112 main (int argc, char **argv)
113 {
114 int brief = 0;
115 FcChar8 *format = NULL, *sysroot = NULL;
116 int i;
117 FcFontSet *fs;
118 #if HAVE_GETOPT_LONG || HAVE_GETOPT
119 int c;
120
121 setlocale (LC_ALL, "");
122 # if HAVE_GETOPT_LONG
123 while ((c = getopt_long (argc, argv, "bf:y:Vh", longopts, NULL)) != -1)
124 # else
125 while ((c = getopt (argc, argv, "bf:y:Vh")) != -1)
126 # endif
127 {
128 switch (c) {
129 case 'b':
130 brief = 1;
131 break;
132 case 'f':
133 format = (FcChar8 *)strdup (optarg);
134 break;
135 case 'y':
136 sysroot = FcStrCopy ((const FcChar8 *)optarg);
137 break;
138 case 'V':
139 fprintf (stderr, "fontconfig version %d.%d.%d\n",
140 FC_MAJOR, FC_MINOR, FC_REVISION);
141 exit (0);
142 case 'h':
143 usage (argv[0], 0);
144 default:
145 usage (argv[0], 1);
146 }
147 }
148 i = optind;
149 #else
150 i = 1;
151 #endif
152
153 if (i == argc)
154 usage (argv[0], 1);
155
156 if (sysroot) {
157 FcConfigSetSysRoot (NULL, sysroot);
158 FcStrFree (sysroot);
159 }
160 fs = FcFontSetCreate();
161
162 for (; i < argc; i++) {
163 const FcChar8 *file = (FcChar8 *)argv[i];
164
165 if (!FcFileIsDir (file))
166 FcFileScan (fs, NULL, NULL, NULL, file, FcTrue);
167 else {
168 FcStrSet *dirs = FcStrSetCreate();
169 FcStrList *strlist = FcStrListCreate (dirs);
170 do {
171 FcDirScan (fs, dirs, NULL, NULL, file, FcTrue);
172 } while ((file = FcStrListNext (strlist)));
173 FcStrListDone (strlist);
174 FcStrSetDestroy (dirs);
175 }
176 }
177
178 for (i = 0; i < fs->nfont; i++) {
179 FcPattern *pat = fs->fonts[i];
180
181 if (brief) {
182 FcPatternDel (pat, FC_CHARSET);
183 FcPatternDel (pat, FC_LANG);
184 }
185
186 if (format) {
187 FcChar8 *s;
188
189 s = FcPatternFormat (pat, format);
190 if (s) {
191 printf ("%s", s);
192 FcStrFree (s);
193 }
194 } else {
195 FcPatternPrint (pat);
196 }
197 }
198
199 FcFontSetDestroy (fs);
200 if (format)
201 free (format);
202
203 FcFini();
204 return i > 0 ? 0 : 1;
205 }