]> git.wh0rd.org - fontconfig.git/blob - fc-query/fc-query.c
Implement FcPatternFormat and use it in cmdline tools (bug #17107)
[fontconfig.git] / fc-query / fc-query.c
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 Keith Packard not be used in
13 * advertising or publicity pertaining to distribution of the software without
14 * specific, written prior permission. Keith Packard makes 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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20 * EVENT SHALL KEITH PACKARD 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
43 #ifndef HAVE_GETOPT
44 #define HAVE_GETOPT 0
45 #endif
46 #ifndef HAVE_GETOPT_LONG
47 #define HAVE_GETOPT_LONG 0
48 #endif
49
50 #if HAVE_GETOPT_LONG
51 #undef _GNU_SOURCE
52 #define _GNU_SOURCE
53 #include <getopt.h>
54 static const struct option longopts[] = {
55 {"index", 1, 0, 'i'},
56 {"format", 1, 0, 'f'},
57 {"version", 0, 0, 'V'},
58 {"help", 0, 0, 'h'},
59 {NULL,0,0,0},
60 };
61 #else
62 #if HAVE_GETOPT
63 extern char *optarg;
64 extern int optind, opterr, optopt;
65 #endif
66 #endif
67
68 static void
69 usage (char *program, int error)
70 {
71 FILE *file = error ? stderr : stdout;
72 #if HAVE_GETOPT_LONG
73 fprintf (file, "usage: %s [-Vh] [-i index] [-f FORMAT] [--index index] [--format FORMAT] [--version] [--help] font-file...\n",
74 program);
75 #else
76 fprintf (file, "usage: %s [-Vh] [-i index] [-f FORMAT] font-file...\n",
77 program);
78 #endif
79 fprintf (file, "Query font files and print resulting pattern(s)\n");
80 fprintf (file, "\n");
81 #if HAVE_GETOPT_LONG
82 fprintf (file, " -i, --index INDEX display the INDEX face of each font file only\n");
83 fprintf (file, " -f, --format=FORMAT use the given output format\n");
84 fprintf (file, " -V, --version display font config version and exit\n");
85 fprintf (file, " -h, --help display this help and exit\n");
86 #else
87 fprintf (file, " -i INDEX (index) display the INDEX face of each font file only\n");
88 fprintf (file, " -f FORMAT (format) use the given output format\n");
89 fprintf (file, " -a (all) display unpruned sorted list of matches\n");
90 fprintf (file, " -V (version) display font config version and exit\n");
91 fprintf (file, " -h (help) display this help and exit\n");
92 #endif
93 exit (error);
94 }
95
96 int
97 main (int argc, char **argv)
98 {
99 int index_set = 0;
100 int set_index = 0;
101 FcChar8 *format = NULL;
102 int err = 0;
103 int i;
104 FcBlanks *blanks;
105 #if HAVE_GETOPT_LONG || HAVE_GETOPT
106 int c;
107
108 #if HAVE_GETOPT_LONG
109 while ((c = getopt_long (argc, argv, "i:sVvh", longopts, NULL)) != -1)
110 #else
111 while ((c = getopt (argc, argv, "i:asVvh")) != -1)
112 #endif
113 {
114 switch (c) {
115 case 'i':
116 index_set = 1;
117 set_index = atoi (optarg);
118 break;
119 case 'f':
120 format = (FcChar8 *) strdup (optarg);
121 break;
122 case 'V':
123 fprintf (stderr, "fontconfig version %d.%d.%d\n",
124 FC_MAJOR, FC_MINOR, FC_REVISION);
125 exit (0);
126 case 'h':
127 usage (argv[0], 0);
128 default:
129 usage (argv[0], 1);
130 }
131 }
132 i = optind;
133 #else
134 i = 1;
135 #endif
136
137 if (i == argc)
138 usage (argv[0], 1);
139
140 if (!FcInit ())
141 {
142 fprintf (stderr, "Can't init font config library\n");
143 return 1;
144 }
145
146 blanks = FcConfigGetBlanks (NULL);
147
148 for (; i < argc; i++)
149 {
150 int index;
151 int count = 0;
152
153 index = set_index;
154
155 do {
156 FcPattern *pat;
157
158 pat = FcFreeTypeQuery ((FcChar8 *) argv[i], index, blanks, &count);
159 if (pat)
160 {
161 if (format)
162 {
163 FcChar8 *s;
164
165 s = FcPatternFormat (pat, format);
166 printf ("%s", s);
167 free (s);
168 }
169 else
170 {
171 FcPatternPrint (pat);
172 }
173
174 FcPatternDestroy (pat);
175 }
176 else
177 {
178 fprintf (stderr, "Can't query face %d of font file %s\n",
179 index, argv[i]);
180 err = 1;
181 }
182
183 index++;
184 } while (!index_set && index < count);
185 }
186
187 FcFini ();
188 return err;
189 }