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 <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 {"index", 1, 0, 'i'},
65 {"brief", 0, 0, 'b'},
66 {"format", 1, 0, 'f'},
67 {"version", 0, 0, 'V'},
68 {"help", 0, 0, 'h'},
69 {NULL,0,0,0},
70 };
71 #else
72 #if HAVE_GETOPT
73 extern char *optarg;
74 extern int optind, opterr, optopt;
75 #endif
76 #endif
77
78 static void
usage(char * program,int error)79 usage (char *program, int error)
80 {
81 FILE *file = error ? stderr : stdout;
82 #if HAVE_GETOPT_LONG
83 fprintf (file, _("usage: %s [-bVh] [-i index] [-f FORMAT] [--index index] [--brief] [--format FORMAT] [--version] [--help] font-file...\n"),
84 program);
85 #else
86 fprintf (file, _("usage: %s [-bVh] [-i index] [-f FORMAT] font-file...\n"),
87 program);
88 #endif
89 fprintf (file, _("Query font files and print resulting pattern(s)\n"));
90 fprintf (file, "\n");
91 #if HAVE_GETOPT_LONG
92 fprintf (file, _(" -i, --index INDEX display the INDEX face of each font file only\n"));
93 fprintf (file, _(" -b, --brief display font pattern briefly\n"));
94 fprintf (file, _(" -f, --format=FORMAT use the given output format\n"));
95 fprintf (file, _(" -V, --version display font config version and exit\n"));
96 fprintf (file, _(" -h, --help display this help and exit\n"));
97 #else
98 fprintf (file, _(" -i INDEX (index) display the INDEX face of each font file only\n"));
99 fprintf (file, _(" -b (brief) display font pattern briefly\n"));
100 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
101 fprintf (file, _(" -V (version) display font config version and exit\n"));
102 fprintf (file, _(" -h (help) display this help and exit\n"));
103 #endif
104 exit (error);
105 }
106
107 int
main(int argc,char ** argv)108 main (int argc, char **argv)
109 {
110 unsigned int id = (unsigned int) -1;
111 int brief = 0;
112 FcFontSet *fs;
113 FcChar8 *format = NULL;
114 int err = 0;
115 int i;
116 #if HAVE_GETOPT_LONG || HAVE_GETOPT
117 int c;
118
119 setlocale (LC_ALL, "");
120 #if HAVE_GETOPT_LONG
121 while ((c = getopt_long (argc, argv, "i:bf:Vh", longopts, NULL)) != -1)
122 #else
123 while ((c = getopt (argc, argv, "i:bf:Vh")) != -1)
124 #endif
125 {
126 switch (c) {
127 case 'i':
128 id = (unsigned int) strtol (optarg, NULL, 0); /* strtol() To handle -1. */
129 break;
130 case 'b':
131 brief = 1;
132 break;
133 case 'f':
134 format = (FcChar8 *) strdup (optarg);
135 break;
136 case 'V':
137 fprintf (stderr, "fontconfig version %d.%d.%d\n",
138 FC_MAJOR, FC_MINOR, FC_REVISION);
139 exit (0);
140 case 'h':
141 usage (argv[0], 0);
142 default:
143 usage (argv[0], 1);
144 }
145 }
146 i = optind;
147 #else
148 i = 1;
149 #endif
150
151 if (i == argc)
152 usage (argv[0], 1);
153
154 fs = FcFontSetCreate ();
155
156 for (; i < argc; i++)
157 {
158 if (!FcFreeTypeQueryAll ((FcChar8*) argv[i], id, NULL, NULL, fs))
159 {
160 fprintf (stderr, _("Can't query face %u of font file %s\n"), id, argv[i]);
161 err = 1;
162 }
163 }
164
165 for (i = 0; i < fs->nfont; i++)
166 {
167 FcPattern *pat = fs->fonts[i];
168
169 if (brief)
170 {
171 FcPatternDel (pat, FC_CHARSET);
172 FcPatternDel (pat, FC_LANG);
173 }
174
175 if (format)
176 {
177 FcChar8 *s;
178
179 s = FcPatternFormat (pat, format);
180 if (s)
181 {
182 printf ("%s", s);
183 FcStrFree (s);
184 }
185 }
186 else
187 {
188 FcPatternPrint (pat);
189 }
190 }
191
192 FcFontSetDestroy (fs);
193
194 FcFini ();
195 return err;
196 }
197