]> git.wh0rd.org Git - fontconfig.git/blob - fc-query/fc-query.c
Add fc-query (#13019)
[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     {"version", 0, 0, 'V'},
57     {"help", 0, 0, '?'},
58     {NULL,0,0,0},
59 };
60 #else
61 #if HAVE_GETOPT
62 extern char *optarg;
63 extern int optind, opterr, optopt;
64 #endif
65 #endif
66
67 static void usage (char *program)
68 {
69 #if HAVE_GETOPT_LONG
70     fprintf (stderr, "usage: %s [-V?] [-i index] [--index index] [--version] [--help] font-file...\n",
71              program);
72 #else
73     fprintf (stderr, "usage: %s [-V?] [-i index] font-file...\n",
74              program);
75 #endif
76     fprintf (stderr, "Query font files and print resulting pattern(s)\n");
77     fprintf (stderr, "\n");
78 #if HAVE_GETOPT_LONG
79     fprintf (stderr, "  -i, --index INDEX    display the INDEX face of each font file only\n");
80     fprintf (stderr, "  -V, --version        display font config version and exit\n");
81     fprintf (stderr, "  -?, --help           display this help and exit\n");
82 #else
83     fprintf (stderr, "  -i INDEX   (index)   display the INDEX face of each font file only\n");
84     fprintf (stderr, "  -a         (all)     display unpruned sorted list of matches\n");
85     fprintf (stderr, "  -V         (version) display font config version and exit\n");
86     fprintf (stderr, "  -?         (help)    display this help and exit\n");
87 #endif
88     exit (1);
89 }
90
91 int
92 main (int argc, char **argv)
93 {
94     int         index_set = 0;
95     int         set_index = 0;
96     int         err = 0;
97     int         i;
98     FcBlanks    *blanks;
99 #if HAVE_GETOPT_LONG || HAVE_GETOPT
100     int         c;
101
102 #if HAVE_GETOPT_LONG
103     while ((c = getopt_long (argc, argv, "i:sVv?", longopts, NULL)) != -1)
104 #else
105     while ((c = getopt (argc, argv, "i:asVv?")) != -1)
106 #endif
107     {
108         switch (c) {
109         case 'i':
110             index_set = 1;
111             set_index = atoi (optarg);
112             break;
113         case 'V':
114             fprintf (stderr, "fontconfig version %d.%d.%d\n",
115                      FC_MAJOR, FC_MINOR, FC_REVISION);
116             exit (0);
117         default:
118             usage (argv[0]);
119         }
120     }
121     i = optind;
122 #else
123     i = 1;
124 #endif
125
126     if (i == argc)
127         usage (argv[0]);
128
129     if (!FcInit ())
130     {
131         fprintf (stderr, "Can't init font config library\n");
132         return 1;
133     }
134
135     blanks = FcConfigGetBlanks (NULL);
136
137     for (; i < argc; i++)
138     {
139         int index;
140         int count = 0;
141
142         index = set_index;
143
144         do {
145             FcPattern *pat;
146
147             pat = FcFreeTypeQuery ((FcChar8 *) argv[i], index, blanks, &count);
148             if (pat)
149             {
150                 FcPatternPrint (pat);
151                 FcPatternDestroy (pat);
152             }
153             else
154             {
155                 fprintf (stderr, "Can't query face %d of font file %s\n",
156                          index, argv[i]);
157                 err = 1;
158             }
159
160             index++;
161         } while (!index_set && index < count);
162     }
163
164     FcFini ();
165     return err;
166 }