79027079b6f926dbadcc2394113c2d299c73ff6d
[platform/upstream/fontconfig.git] / fc-match / fc-match.c
1 /*
2  * fontconfig/fc-match/fc-match.c
3  *
4  * Copyright © 2003 Keith Packard
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make no
13  * representations about the suitability of this software for any purpose.  It
14  * is provided "as is" without express or implied warranty.
15  *
16  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #else
28 #ifdef linux
29 #define HAVE_GETOPT_LONG 1
30 #endif
31 #define HAVE_GETOPT 1
32 #endif
33
34 #include <fontconfig/fontconfig.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #ifdef ENABLE_NLS
41 #include <libintl.h>
42 #define _(x)            (dgettext(GETTEXT_PACKAGE, x))
43 #else
44 #define dgettext(d, s)  (s)
45 #define _(x)            (x)
46 #endif
47
48 #ifndef HAVE_GETOPT
49 #define HAVE_GETOPT 0
50 #endif
51 #ifndef HAVE_GETOPT_LONG
52 #define HAVE_GETOPT_LONG 0
53 #endif
54
55 #if HAVE_GETOPT_LONG
56 #undef  _GNU_SOURCE
57 #define _GNU_SOURCE
58 #include <getopt.h>
59 static const struct option longopts[] = {
60     {"sort", 0, 0, 's'},
61     {"all", 0, 0, 'a'},
62     {"verbose", 0, 0, 'v'},
63     {"brief", 0, 0, 'b'},
64     {"format", 1, 0, 'f'},
65     {"version", 0, 0, 'V'},
66     {"help", 0, 0, 'h'},
67     {NULL,0,0,0},
68 };
69 #else
70 #if HAVE_GETOPT
71 extern char *optarg;
72 extern int optind, opterr, optopt;
73 #endif
74 #endif
75
76 static void
77 usage (char *program, int error)
78 {
79     FILE *file = error ? stderr : stdout;
80 #if HAVE_GETOPT_LONG
81     fprintf (file, _("usage: %s [-savbVh] [-f FORMAT] [--sort] [--all] [--verbose] [--brief] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
82              program);
83 #else
84     fprintf (file, _("usage: %s [-savVh] [-f FORMAT] [pattern] {element...}\n"),
85              program);
86 #endif
87     fprintf (file, _("List best font matching [pattern]\n"));
88     fprintf (file, "\n");
89 #if HAVE_GETOPT_LONG
90     fprintf (file, _("  -s, --sort           display sorted list of matches\n"));
91     fprintf (file, _("  -a, --all            display unpruned sorted list of matches\n"));
92     fprintf (file, _("  -v, --verbose        display entire font pattern verbosely\n"));
93     fprintf (file, _("  -b, --brief          display entire 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, _("  -s,        (sort)    display sorted list of matches\n"));
99     fprintf (file, _("  -a         (all)     display unpruned sorted list of matches\n"));
100     fprintf (file, _("  -v         (verbose) display entire font pattern verbosely\n"));
101     fprintf (file, _("  -b         (brief)   display entire font pattern briefly\n"));
102     fprintf (file, _("  -f FORMAT  (format)  use the given output format\n"));
103     fprintf (file, _("  -V         (version) display font config version and exit\n"));
104     fprintf (file, _("  -h         (help)    display this help and exit\n"));
105 #endif
106     exit (error);
107 }
108
109 int
110 main (int argc, char **argv)
111 {
112     int                 verbose = 0;
113     int                 brief = 0;
114     int                 sort = 0, all = 0;
115     const FcChar8       *format = NULL;
116     int                 i;
117     FcObjectSet         *os = 0;
118     FcFontSet           *fs;
119     FcPattern           *pat;
120     FcResult            result;
121 #if HAVE_GETOPT_LONG || HAVE_GETOPT
122     int                 c;
123
124 #if HAVE_GETOPT_LONG
125     while ((c = getopt_long (argc, argv, "asvbf:Vh", longopts, NULL)) != -1)
126 #else
127     while ((c = getopt (argc, argv, "asvbf:Vh")) != -1)
128 #endif
129     {
130         switch (c) {
131         case 'a':
132             all = 1;
133             break;
134         case 's':
135             sort = 1;
136             break;
137         case 'v':
138             verbose = 1;
139             break;
140         case 'b':
141             brief = 1;
142             break;
143         case 'f':
144             format = (FcChar8 *) strdup (optarg);
145             break;
146         case 'V':
147             fprintf (stderr, "fontconfig version %d.%d.%d\n", 
148                      FC_MAJOR, FC_MINOR, FC_REVISION);
149             exit (0);
150         case 'h':
151             usage (argv[0], 0);
152         default:
153             usage (argv[0], 1);
154         }
155     }
156     i = optind;
157 #else
158     i = 1;
159 #endif
160
161     if (argv[i])
162     {
163         pat = FcNameParse ((FcChar8 *) argv[i]);
164         if (!pat)
165         {
166             fprintf (stderr, _("Unable to parse the pattern\n"));
167             return 1;
168         }
169         while (argv[++i])
170         {
171             if (!os)
172                 os = FcObjectSetCreate ();
173             FcObjectSetAdd (os, argv[i]);
174         }
175     }
176     else
177         pat = FcPatternCreate ();
178
179     if (!pat)
180         return 1;
181
182     FcConfigSubstitute (0, pat, FcMatchPattern);
183     FcDefaultSubstitute (pat);
184     
185     fs = FcFontSetCreate ();
186
187     if (sort || all)
188     {
189         FcFontSet       *font_patterns;
190         int     j;
191         font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
192
193         if (!font_patterns || font_patterns->nfont == 0)
194         {
195             fprintf (stderr, _("No fonts installed on the system\n"));
196             return 1;
197         }
198         for (j = 0; j < font_patterns->nfont; j++)
199         {
200             FcPattern  *font_pattern;
201
202             font_pattern = FcFontRenderPrepare (NULL, pat, font_patterns->fonts[j]);
203             if (font_pattern)
204                 FcFontSetAdd (fs, font_pattern);
205         }
206
207         FcFontSetSortDestroy (font_patterns);
208     }
209     else
210     {
211         FcPattern   *match;
212         match = FcFontMatch (0, pat, &result);
213         if (match)
214             FcFontSetAdd (fs, match);
215     }
216     FcPatternDestroy (pat);
217
218     if (!format)
219     {
220         if (os)
221             format = (const FcChar8 *) "%{=unparse}\n";
222         else
223             format = (const FcChar8 *) "%{=fcmatch}\n";
224     }
225
226     if (fs)
227     {
228         int     j;
229
230         for (j = 0; j < fs->nfont; j++)
231         {
232             FcPattern *font;
233
234             font = FcPatternFilter (fs->fonts[j], os);
235
236             if (verbose || brief)
237             {
238                 if (brief)
239                 {
240                     FcPatternDel (font, FC_CHARSET);
241                     FcPatternDel (font, FC_LANG);
242                 }
243                 FcPatternPrint (font);
244             }
245             else
246             {
247                 FcChar8 *s;
248
249                 s = FcPatternFormat (font, format);
250                 if (s)
251                 {
252                     printf ("%s", s);
253                     FcStrFree (s);
254                 }
255             }
256
257             FcPatternDestroy (font);
258         }
259         FcFontSetDestroy (fs);
260     }
261
262     if (os)
263         FcObjectSetDestroy (os);
264
265     FcFini ();
266
267     return 0;
268 }