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