Debian Codegraph @

subprojects/fontconfig/fc-query/fc-query.c

raw
1 /*
2 * fontconfig/fc-query/fc-query.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 #if ENABLE_FONTATIONS
40 # include <fontconfig/fcfontations.h>
41 #endif
42
43 #include <locale.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #ifdef HAVE_UNISTD_H
49 # include <unistd.h>
50 #endif
51
52 #ifdef ENABLE_NLS
53 # include <libintl.h>
54 # define _(x) (dgettext (GETTEXT_PACKAGE, x))
55 #else
56 # define dgettext(d, s) (s)
57 # define _(x) (x)
58 #endif
59
60 #ifndef HAVE_GETOPT
61 # define HAVE_GETOPT 0
62 #endif
63 #ifndef HAVE_GETOPT_LONG
64 # define HAVE_GETOPT_LONG 0
65 #endif
66
67 #if HAVE_GETOPT_LONG
68 # undef _GNU_SOURCE
69 # define _GNU_SOURCE
70 # include <getopt.h>
71 static const struct option longopts[] = {
72 { "index", 1, 0, 'i' },
73 { "brief", 0, 0, 'b' },
74 { "format", 1, 0, 'f' },
75 { "version", 0, 0, 'V' },
76 { "help", 0, 0, 'h' },
77 { NULL, 0, 0, 0 },
78 };
79 #else
80 # if HAVE_GETOPT
81 extern char *optarg;
82 extern int optind, opterr, optopt;
83 # endif
84 #endif
85
86 static void
87 usage (char *program, int error)
88 {
89 FILE *file = error ? stderr : stdout;
90 #if HAVE_GETOPT_LONG
91 fprintf (file, _("usage: %s [-bVh] [-i index] [-f FORMAT] [--index index] [--brief] [--format FORMAT] [--version] [--help] font-file...\n"),
92 program);
93 #else
94 fprintf (file, _("usage: %s [-bVh] [-i index] [-f FORMAT] font-file...\n"),
95 program);
96 #endif
97 fprintf (file, _("Query font files and print resulting pattern(s)\n"));
98 fprintf (file, "\n");
99 #if HAVE_GETOPT_LONG
100 fprintf (file, _(" -i, --index INDEX display the INDEX face of each font file only\n"));
101 fprintf (file, _(" -b, --brief display font pattern briefly\n"));
102 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
103 fprintf (file, _(" -V, --version display font config version and exit\n"));
104 fprintf (file, _(" -h, --help display this help and exit\n"));
105 #else
106 fprintf (file, _(" -i INDEX (index) display the INDEX face of each font file only\n"));
107 fprintf (file, _(" -b (brief) display font pattern briefly\n"));
108 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
109 fprintf (file, _(" -V (version) display font config version and exit\n"));
110 fprintf (file, _(" -h (help) display this help and exit\n"));
111 #endif
112 exit (error);
113 }
114
115 int
116 main (int argc, char **argv)
117 {
118 unsigned int id = (unsigned int)-1;
119 int brief = 0;
120 FcFontSet *fs;
121 FcChar8 *format = NULL;
122 int err = 0;
123 int i;
124 #if HAVE_GETOPT_LONG || HAVE_GETOPT
125 int c;
126
127 setlocale (LC_ALL, "");
128 # if HAVE_GETOPT_LONG
129 while ((c = getopt_long (argc, argv, "i:bf:Vh", longopts, NULL)) != -1)
130 # else
131 while ((c = getopt (argc, argv, "i:bf:Vh")) != -1)
132 # endif
133 {
134 switch (c) {
135 case 'i':
136 id = (unsigned int)strtol (optarg, NULL, 0); /* strtol() To handle -1. */
137 break;
138 case 'b':
139 brief = 1;
140 break;
141 case 'f':
142 format = (FcChar8 *)strdup (optarg);
143 break;
144 case 'V':
145 fprintf (stderr, "fontconfig version %d.%d.%d\n",
146 FC_MAJOR, FC_MINOR, FC_REVISION);
147 exit (0);
148 case 'h':
149 usage (argv[0], 0);
150 default:
151 usage (argv[0], 1);
152 }
153 }
154 i = optind;
155 #else
156 i = 1;
157 #endif
158
159 if (i == argc)
160 usage (argv[0], 1);
161
162 fs = FcFontSetCreate();
163
164 unsigned int (*query_function) (const FcChar8 *, unsigned int, FcBlanks *, int *, FcFontSet *) = FcFreeTypeQueryAll;
165 #if ENABLE_FONTATIONS
166 if (getenv ("FC_FONTATIONS") != NULL) {
167 query_function = FcFontationsQueryAll;
168 }
169 #endif
170
171 for (; i < argc; i++) {
172 if (!query_function ((FcChar8 *)argv[i], id, NULL, NULL, fs)) {
173 fprintf (stderr, _("Can't query face %u of font file %s\n"), id, argv[i]);
174 err = 1;
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 err;
205 }