]> git.wh0rd.org - fontconfig.git/blame - fc-match/fc-match.c
Prevent terrible perf regression by getting the if-condition right
[fontconfig.git] / fc-match / fc-match.c
CommitLineData
8bc4bc13
KP
1/*
2 * $RCSId: xc/lib/fontconfig/fc-list/fc-list.c,v 1.5 2002/06/30 23:45:40 keithp Exp $
3 *
46b51147 4 * Copyright © 2003 Keith Packard
8bc4bc13
KP
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes 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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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#include <fontconfig/fontconfig.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <stdlib.h>
34cd0514 29#include <string.h>
8bc4bc13
KP
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#else
33#ifdef linux
34#define HAVE_GETOPT_LONG 1
35#endif
36#define HAVE_GETOPT 1
37#endif
38
39#ifndef HAVE_GETOPT
40#define HAVE_GETOPT 0
41#endif
42#ifndef HAVE_GETOPT_LONG
43#define HAVE_GETOPT_LONG 0
44#endif
45
46#if HAVE_GETOPT_LONG
47#undef _GNU_SOURCE
48#define _GNU_SOURCE
49#include <getopt.h>
0d745819 50static const struct option longopts[] = {
8bc4bc13
KP
51 {"sort", 0, 0, 's'},
52 {"version", 0, 0, 'V'},
53 {"verbose", 0, 0, 'v'},
54 {"help", 0, 0, '?'},
55 {NULL,0,0,0},
56};
57#else
58#if HAVE_GETOPT
59extern char *optarg;
60extern int optind, opterr, optopt;
61#endif
62#endif
63
64static void usage (char *program)
65{
66#if HAVE_GETOPT_LONG
67 fprintf (stderr, "usage: %s [-svV?] [--sort] [--verbose] [--version] [--help] [pattern]\n",
68 program);
69#else
70 fprintf (stderr, "usage: %s [-svV?] [pattern]\n",
71 program);
72#endif
73 fprintf (stderr, "List fonts matching [pattern]\n");
74 fprintf (stderr, "\n");
75#if HAVE_GETOPT_LONG
76 fprintf (stderr, " -s, --sort display sorted list of matches\n");
77 fprintf (stderr, " -v, --verbose display entire font pattern\n");
78 fprintf (stderr, " -V, --version display font config version and exit\n");
79 fprintf (stderr, " -?, --help display this help and exit\n");
80#else
81 fprintf (stderr, " -s, (sort) display sorted list of matches\n");
82 fprintf (stderr, " -v (verbose) display entire font pattern\n");
83 fprintf (stderr, " -V (version) display font config version and exit\n");
84 fprintf (stderr, " -? (help) display this help and exit\n");
85#endif
86 exit (1);
87}
88
89int
90main (int argc, char **argv)
91{
92 int verbose = 0;
93 int sort = 0;
94 int i;
8bc4bc13
KP
95 FcFontSet *fs;
96 FcPattern *pat;
97 FcResult result;
98#if HAVE_GETOPT_LONG || HAVE_GETOPT
99 int c;
100
101#if HAVE_GETOPT_LONG
435fc660 102 while ((c = getopt_long (argc, argv, "sVv?", longopts, NULL)) != -1)
8bc4bc13
KP
103#else
104 while ((c = getopt (argc, argv, "sVv?")) != -1)
105#endif
106 {
107 switch (c) {
108 case 's':
109 sort = 1;
110 break;
111 case 'V':
112 fprintf (stderr, "fontconfig version %d.%d.%d\n",
113 FC_MAJOR, FC_MINOR, FC_REVISION);
114 exit (0);
115 case 'v':
116 verbose = 1;
117 break;
118 default:
119 usage (argv[0]);
120 }
121 }
122 i = optind;
123#else
124 i = 1;
125#endif
126
127 if (!FcInit ())
128 {
129 fprintf (stderr, "Can't init font config library\n");
130 return 1;
131 }
132 if (argv[i])
133 pat = FcNameParse ((FcChar8 *) argv[i]);
134 else
135 pat = FcPatternCreate ();
136
ae2aafe6
PL
137 if (!pat)
138 return 1;
139
8bc4bc13
KP
140 FcConfigSubstitute (0, pat, FcMatchPattern);
141 FcDefaultSubstitute (pat);
142
143 if (sort)
144 fs = FcFontSort (0, pat, FcTrue, 0, &result);
145 else
146 {
147 FcPattern *match;
148 fs = FcFontSetCreate ();
149 match = FcFontMatch (0, pat, &result);
150 if (match)
151 FcFontSetAdd (fs, match);
152 }
ae2aafe6 153 FcPatternDestroy (pat);
8bc4bc13
KP
154
155 if (fs)
156 {
157 int j;
158
159 for (j = 0; j < fs->nfont; j++)
160 {
161 if (verbose)
162 {
163 FcPatternPrint (fs->fonts[j]);
164 }
165 else
166 {
167 FcChar8 *family;
168 FcChar8 *style;
169 FcChar8 *file;
170
171 if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) != FcResultMatch)
8245771d 172 file = (FcChar8 *) "<unknown filename>";
8bc4bc13
KP
173 else
174 {
8245771d 175 FcChar8 *slash = (FcChar8 *) strrchr ((char *) file, '/');
8bc4bc13
KP
176 if (slash)
177 file = slash+1;
178 }
179 if (FcPatternGetString (fs->fonts[j], FC_FAMILY, 0, &family) != FcResultMatch)
8245771d 180 family = (FcChar8 *) "<unknown family>";
8bc4bc13 181 if (FcPatternGetString (fs->fonts[j], FC_STYLE, 0, &style) != FcResultMatch)
8245771d 182 file = (FcChar8 *) "<unknown style>";
8bc4bc13
KP
183
184 printf ("%s: \"%s\" \"%s\"\n", file, family, style);
185 }
186 }
187 FcFontSetDestroy (fs);
188 }
189 return 0;
190}