Debian Codegraph @

subprojects/fontconfig/fc-match/fc-match.c

raw
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
36 #include <locale.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44
45 #ifdef ENABLE_NLS
46 # include <libintl.h>
47 # define _(x) (dgettext (GETTEXT_PACKAGE, x))
48 #else
49 # define dgettext(d, s) (s)
50 # define _(x) (x)
51 #endif
52
53 #ifndef HAVE_GETOPT
54 # define HAVE_GETOPT 0
55 #endif
56 #ifndef HAVE_GETOPT_LONG
57 # define HAVE_GETOPT_LONG 0
58 #endif
59
60 #if HAVE_GETOPT_LONG
61 # undef _GNU_SOURCE
62 # define _GNU_SOURCE
63 # include <getopt.h>
64 static const struct option longopts[] = {
65 { "sort", 0, 0, 's' },
66 { "all", 0, 0, 'a' },
67 { "verbose", 0, 0, 'v' },
68 { "brief", 0, 0, 'b' },
69 { "format", 1, 0, 'f' },
70 { "version", 0, 0, 'V' },
71 { "help", 0, 0, 'h' },
72 { NULL, 0, 0, 0 },
73 };
74 #else
75 # if HAVE_GETOPT
76 extern char *optarg;
77 extern int optind, opterr, optopt;
78 # endif
79 #endif
80
81 static void
82 usage (char *program, int error)
83 {
84 FILE *file = error ? stderr : stdout;
85 #if HAVE_GETOPT_LONG
86 fprintf (file, _("usage: %s [-savbVh] [-f FORMAT] [--sort] [--all] [--verbose] [--brief] [--format=FORMAT] [--version] [--help] [pattern] {element...}\n"),
87 program);
88 #else
89 fprintf (file, _("usage: %s [-savVh] [-f FORMAT] [pattern] {element...}\n"),
90 program);
91 #endif
92 fprintf (file, _("List best font matching [pattern]\n"));
93 fprintf (file, "\n");
94 #if HAVE_GETOPT_LONG
95 fprintf (file, _(" -s, --sort display sorted list of matches\n"));
96 fprintf (file, _(" -a, --all display unpruned sorted list of matches\n"));
97 fprintf (file, _(" -v, --verbose display entire font pattern verbosely\n"));
98 fprintf (file, _(" -b, --brief display entire 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 #else
103 fprintf (file, _(" -s, (sort) display sorted list of matches\n"));
104 fprintf (file, _(" -a (all) display unpruned sorted list of matches\n"));
105 fprintf (file, _(" -v (verbose) display entire font pattern verbosely\n"));
106 fprintf (file, _(" -b (brief) display entire font pattern briefly\n"));
107 fprintf (file, _(" -f FORMAT (format) use the given output format\n"));
108 fprintf (file, _(" -V (version) display font config version and exit\n"));
109 fprintf (file, _(" -h (help) display this help and exit\n"));
110 #endif
111 exit (error);
112 }
113
114 int
115 main (int argc, char **argv)
116 {
117 int verbose = 0;
118 int brief = 0;
119 int sort = 0, all = 0;
120 const FcChar8 *format = NULL;
121 const FcChar8 *format_optarg = NULL;
122 int i;
123 FcObjectSet *os = 0;
124 FcFontSet *fs;
125 FcPattern *pat;
126 FcResult result;
127 #if HAVE_GETOPT_LONG || HAVE_GETOPT
128 int c;
129
130 setlocale (LC_ALL, "");
131 # if HAVE_GETOPT_LONG
132 while ((c = getopt_long (argc, argv, "asvbf:Vh", longopts, NULL)) != -1)
133 # else
134 while ((c = getopt (argc, argv, "asvbf:Vh")) != -1)
135 # endif
136 {
137 switch (c) {
138 case 'a':
139 all = 1;
140 break;
141 case 's':
142 sort = 1;
143 break;
144 case 'v':
145 verbose = 1;
146 break;
147 case 'b':
148 brief = 1;
149 break;
150 case 'f':
151 format = format_optarg = (FcChar8 *)strdup (optarg);
152 break;
153 case 'V':
154 fprintf (stderr, "fontconfig version %d.%d.%d\n",
155 FC_MAJOR, FC_MINOR, FC_REVISION);
156 exit (0);
157 case 'h':
158 usage (argv[0], 0);
159 default:
160 usage (argv[0], 1);
161 }
162 }
163 i = optind;
164 #else
165 i = 1;
166 #endif
167
168 if (argv[i]) {
169 pat = FcNameParse ((FcChar8 *)argv[i]);
170 if (!pat) {
171 fprintf (stderr, _("Unable to parse the pattern\n"));
172 return 1;
173 }
174 while (argv[++i]) {
175 if (!os)
176 os = FcObjectSetCreate();
177 FcObjectSetAdd (os, argv[i]);
178 }
179 } else
180 pat = FcPatternCreate();
181
182 if (!pat)
183 return 1;
184
185 FcConfigSubstitute (0, pat, FcMatchPattern);
186 FcConfigSetDefaultSubstitute (0, pat);
187
188 fs = FcFontSetCreate();
189
190 if (sort || all) {
191 FcFontSet *font_patterns;
192 int j;
193 font_patterns = FcFontSort (0, pat, all ? FcFalse : FcTrue, 0, &result);
194
195 if (!font_patterns || font_patterns->nfont == 0) {
196 fprintf (stderr, _("No fonts installed on the system\n"));
197 return 1;
198 }
199 for (j = 0; j < font_patterns->nfont; j++) {
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 } else {
209 FcPattern *match;
210 match = FcFontMatch (0, pat, &result);
211 if (match)
212 FcFontSetAdd (fs, match);
213 }
214 FcPatternDestroy (pat);
215
216 if (!format) {
217 if (os)
218 format = (const FcChar8 *)"%{=unparse}\n";
219 else
220 format = (const FcChar8 *)"%{=fcmatch}\n";
221 }
222
223 if (fs) {
224 int j;
225
226 for (j = 0; j < fs->nfont; j++) {
227 FcPattern *font;
228
229 font = FcPatternFilter (fs->fonts[j], os);
230
231 if (verbose || brief) {
232 if (brief) {
233 FcPatternDel (font, FC_CHARSET);
234 FcPatternDel (font, FC_LANG);
235 }
236 FcPatternPrint (font);
237 } else {
238 FcChar8 *s;
239
240 s = FcPatternFormat (font, format);
241 if (s) {
242 printf ("%s", s);
243 FcStrFree (s);
244 }
245 }
246
247 FcPatternDestroy (font);
248 }
249 FcFontSetDestroy (fs);
250 }
251
252 if (os)
253 FcObjectSetDestroy (os);
254
255 FcFini();
256
257 if (format_optarg) {
258 free ((void *)format_optarg);
259 format_optarg = NULL;
260 }
261
262 return 0;
263 }