]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
317b8492 | 2 | * fontconfig/fc-list/fc-list.c |
24330d27 | 3 | * |
46b51147 | 4 | * Copyright © 2002 Keith Packard |
24330d27 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> | |
ccb3e93b | 28 | #include <stdlib.h> |
24330d27 KP |
29 | #ifdef HAVE_CONFIG_H |
30 | #include <config.h> | |
31 | #else | |
c4bd0638 MALF |
32 | #ifdef linux |
33 | #define HAVE_GETOPT_LONG 1 | |
34 | #endif | |
24330d27 KP |
35 | #define HAVE_GETOPT 1 |
36 | #endif | |
37 | ||
c4bd0638 MALF |
38 | #ifndef HAVE_GETOPT |
39 | #define HAVE_GETOPT 0 | |
40 | #endif | |
41 | #ifndef HAVE_GETOPT_LONG | |
42 | #define HAVE_GETOPT_LONG 0 | |
43 | #endif | |
44 | ||
24330d27 | 45 | #if HAVE_GETOPT_LONG |
c4bd0638 | 46 | #undef _GNU_SOURCE |
24330d27 KP |
47 | #define _GNU_SOURCE |
48 | #include <getopt.h> | |
49 | const struct option longopts[] = { | |
50 | {"version", 0, 0, 'V'}, | |
51 | {"verbose", 0, 0, 'v'}, | |
1439c8f2 | 52 | {"help", 0, 0, 'h'}, |
24330d27 KP |
53 | {NULL,0,0,0}, |
54 | }; | |
55 | #else | |
56 | #if HAVE_GETOPT | |
57 | extern char *optarg; | |
58 | extern int optind, opterr, optopt; | |
59 | #endif | |
60 | #endif | |
61 | ||
1439c8f2 BE |
62 | static void |
63 | usage (char *program, int error) | |
24330d27 | 64 | { |
1439c8f2 | 65 | FILE *file = error ? stderr : stdout; |
86b12431 | 66 | #if HAVE_GETOPT_LONG |
1439c8f2 | 67 | fprintf (file, "usage: %s [-vVh] [--verbose] [--version] [--help] [pattern] {element ...} \n", |
24330d27 | 68 | program); |
86b12431 | 69 | #else |
1439c8f2 | 70 | fprintf (file, "usage: %s [-vVh] [pattern] {element ...} \n", |
86b12431 KP |
71 | program); |
72 | #endif | |
1439c8f2 BE |
73 | fprintf (file, "List fonts matching [pattern]\n"); |
74 | fprintf (file, "\n"); | |
86b12431 | 75 | #if HAVE_GETOPT_LONG |
1439c8f2 BE |
76 | fprintf (file, " -v, --verbose display entire font pattern\n"); |
77 | fprintf (file, " -V, --version display font config version and exit\n"); | |
78 | fprintf (file, " -h, --help display this help and exit\n"); | |
86b12431 | 79 | #else |
1439c8f2 BE |
80 | fprintf (file, " -v (verbose) display entire font pattern\n"); |
81 | fprintf (file, " -V (version) display font config version and exit\n"); | |
82 | fprintf (file, " -h (help) display this help and exit\n"); | |
86b12431 | 83 | #endif |
1439c8f2 | 84 | exit (error); |
24330d27 KP |
85 | } |
86 | ||
87 | int | |
88 | main (int argc, char **argv) | |
89 | { | |
29874098 | 90 | int verbose = 0; |
24330d27 | 91 | int i; |
e6099fe9 | 92 | FcObjectSet *os = 0; |
24330d27 KP |
93 | FcFontSet *fs; |
94 | FcPattern *pat; | |
95 | #if HAVE_GETOPT_LONG || HAVE_GETOPT | |
96 | int c; | |
97 | ||
98 | #if HAVE_GETOPT_LONG | |
1439c8f2 | 99 | while ((c = getopt_long (argc, argv, "Vvh", longopts, NULL)) != -1) |
24330d27 | 100 | #else |
1439c8f2 | 101 | while ((c = getopt (argc, argv, "Vvh")) != -1) |
24330d27 KP |
102 | #endif |
103 | { | |
104 | switch (c) { | |
105 | case 'V': | |
106 | fprintf (stderr, "fontconfig version %d.%d.%d\n", | |
107 | FC_MAJOR, FC_MINOR, FC_REVISION); | |
108 | exit (0); | |
109 | case 'v': | |
29874098 | 110 | verbose = 1; |
24330d27 | 111 | break; |
1439c8f2 BE |
112 | case 'h': |
113 | usage (argv[0], 0); | |
24330d27 | 114 | default: |
1439c8f2 | 115 | usage (argv[0], 1); |
24330d27 KP |
116 | } |
117 | } | |
118 | i = optind; | |
119 | #else | |
120 | i = 1; | |
121 | #endif | |
122 | ||
123 | if (!FcInit ()) | |
124 | { | |
125 | fprintf (stderr, "Can't init font config library\n"); | |
126 | return 1; | |
127 | } | |
128 | if (argv[i]) | |
e6099fe9 | 129 | { |
80c053b7 | 130 | pat = FcNameParse ((FcChar8 *) argv[i]); |
29874098 BE |
131 | if (!verbose) |
132 | while (argv[++i]) | |
133 | { | |
134 | if (!os) | |
135 | os = FcObjectSetCreate (); | |
136 | FcObjectSetAdd (os, argv[i]); | |
137 | } | |
e6099fe9 | 138 | } |
24330d27 KP |
139 | else |
140 | pat = FcPatternCreate (); | |
141 | ||
29874098 | 142 | if (!verbose && !os) |
18906a87 | 143 | os = FcObjectSetBuild (FC_FAMILY, FC_STYLE, (char *) 0); |
24330d27 | 144 | fs = FcFontList (0, pat, os); |
29874098 BE |
145 | if (os) |
146 | FcObjectSetDestroy (os); | |
24330d27 KP |
147 | if (pat) |
148 | FcPatternDestroy (pat); | |
149 | ||
150 | if (fs) | |
151 | { | |
152 | int j; | |
153 | ||
154 | for (j = 0; j < fs->nfont; j++) | |
155 | { | |
156 | FcChar8 *font; | |
e6099fe9 | 157 | FcChar8 *file; |
24330d27 | 158 | |
29874098 BE |
159 | if (verbose) |
160 | FcPatternPrint (fs->fonts[j]); | |
161 | else | |
162 | { | |
163 | font = FcNameUnparse (fs->fonts[j]); | |
164 | if (FcPatternGetString (fs->fonts[j], FC_FILE, 0, &file) == FcResultMatch) | |
165 | printf ("%s: ", file); | |
166 | printf ("%s\n", font); | |
167 | free (font); | |
168 | } | |
24330d27 KP |
169 | } |
170 | FcFontSetDestroy (fs); | |
171 | } | |
34cd0514 CW |
172 | |
173 | FcFini (); | |
174 | ||
24330d27 KP |
175 | return 0; |
176 | } |