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