Debian Codegraph @

subprojects/fontconfig/fc-validate/fc-validate.c

raw
1 /*
2 * fontconfig/fc-validate/fc-validate.c
3 *
4 * Copyright © 2003 Keith Packard
5 * Copyright © 2012 Red Hat, Inc.
6 * Red Hat Author(s): Akira TAGOH
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
39 #include <locale.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47
48 #ifdef ENABLE_NLS
49 # include <libintl.h>
50 # define _(x) (dgettext (GETTEXT_PACKAGE, x))
51 #else
52 # define dgettext(d, s) (s)
53 # define _(x) (x)
54 #endif
55
56 #ifndef HAVE_GETOPT
57 # define HAVE_GETOPT 0
58 #endif
59 #ifndef HAVE_GETOPT_LONG
60 # define HAVE_GETOPT_LONG 0
61 #endif
62
63 #if HAVE_GETOPT_LONG
64 # undef _GNU_SOURCE
65 # define _GNU_SOURCE
66 # include <getopt.h>
67 static const struct option longopts[] = {
68 { "index", 1, 0, 'i' },
69 { "lang", 1, 0, 'l' },
70 { "verbose", 0, 0, 'v' },
71 { "version", 0, 0, 'V' },
72 { "help", 0, 0, 'h' },
73 { NULL, 0, 0, 0 },
74 };
75 #else
76 # if HAVE_GETOPT
77 extern char *optarg;
78 extern int optind, opterr, optopt;
79 # endif
80 #endif
81
82 static void
83 usage (char *program, int error)
84 {
85 FILE *file = error ? stderr : stdout;
86 #if HAVE_GETOPT_LONG
87 fprintf (file, _("usage: %s [-Vhv] [-i index] [-l LANG] [--index index] [--lang LANG] [--verbose] [--version] [--help] font-file...\n"),
88 program);
89 #else
90 fprintf (file, _("usage: %s [-Vhv] [-i index] [-l LANG] font-file...\n"),
91 program);
92 #endif
93 fprintf (file, _("Validate font files and print result\n"));
94 fprintf (file, "\n");
95 #if HAVE_GETOPT_LONG
96 fprintf (file, _(" -i, --index INDEX display the INDEX face of each font file only\n"));
97 fprintf (file, _(" -l, --lang=LANG set LANG instead of current locale\n"));
98 fprintf (file, _(" -v, --verbose show more detailed information\n"));
99 fprintf (file, _(" -V, --version display font config version and exit\n"));
100 fprintf (file, _(" -h, --help display this help and exit\n"));
101 #else
102 fprintf (file, _(" -i INDEX (index) display the INDEX face of each font file only\n"));
103 fprintf (file, _(" -l LANG (lang) set LANG instead of current locale\n"));
104 fprintf (file, _(" -v (verbose) show more detailed information\n"));
105 fprintf (file, _(" -V (version) display font config version and exit\n"));
106 fprintf (file, _(" -h (help) display this help and exit\n"));
107 #endif
108 exit (error);
109 }
110
111 int
112 main (int argc, char **argv)
113 {
114 int index_set = 0;
115 int set_index = 0;
116 FcChar8 *lang = NULL;
117 const FcCharSet *fcs_lang = NULL;
118 int err = 0;
119 int i;
120 FT_Library ftlib;
121 FcBool verbose = FcFalse;
122 #if HAVE_GETOPT_LONG || HAVE_GETOPT
123 int c;
124
125 setlocale (LC_ALL, "");
126
127 # if HAVE_GETOPT_LONG
128 while ((c = getopt_long (argc, argv, "i:l:mVhv", longopts, NULL)) != -1)
129 # else
130 while ((c = getopt (argc, argv, "i:l:mVhv")) != -1)
131 # endif
132 {
133 switch (c) {
134 case 'i':
135 index_set = 1;
136 set_index = atoi (optarg);
137 break;
138 case 'l':
139 lang = (FcChar8 *)FcLangNormalize ((const FcChar8 *)optarg);
140 break;
141 case 'v':
142 verbose = FcTrue;
143 break;
144 case 'V':
145 fprintf (stderr, "fontconfig version %d.%d.%d\n",
146 FC_MAJOR, FC_MINOR, FC_REVISION);
147 exit (0);
148 case 'h':
149 usage (argv[0], 0);
150 default:
151 usage (argv[0], 1);
152 }
153 }
154 i = optind;
155 #else
156 i = 1;
157 verbose = FcTrue;
158 #endif
159
160 if (i == argc)
161 usage (argv[0], 1);
162
163 if (!lang)
164 lang = FcLangNormalize ((const FcChar8 *)setlocale (LC_CTYPE, NULL));
165
166 if (lang)
167 fcs_lang = FcLangGetCharSet (lang);
168
169 if (FT_Init_FreeType (&ftlib)) {
170 fprintf (stderr, _("Can't initialize FreeType library\n"));
171 return 1;
172 }
173
174 for (; i < argc; i++) {
175 int index;
176
177 index = set_index;
178
179 do {
180 FT_Face face;
181 FcCharSet *fcs, *fcs_sub;
182
183 if (FT_New_Face (ftlib, argv[i], index, &face)) {
184 if (!index_set && index > 0)
185 break;
186 fprintf (stderr, _("Unable to open %s\n"), argv[i]);
187 err = 1;
188 } else {
189 FcChar32 count;
190
191 fcs = FcFreeTypeCharSet (face, NULL);
192 fcs_sub = FcCharSetSubtract (fcs_lang, fcs);
193
194 count = FcCharSetCount (fcs_sub);
195 if (count > 0) {
196 FcChar32 ucs4, pos, map[FC_CHARSET_MAP_SIZE];
197
198 err = 1;
199 printf (_("%s:%d Missing %d glyph(s) to satisfy the coverage for %s language\n"),
200 argv[i], index, count, lang);
201
202 if (verbose) {
203 for (ucs4 = FcCharSetFirstPage (fcs_sub, map, &pos);
204 ucs4 != FC_CHARSET_DONE;
205 ucs4 = FcCharSetNextPage (fcs_sub, map, &pos)) {
206 int j;
207
208 for (j = 0; j < FC_CHARSET_MAP_SIZE; j++) {
209 FcChar32 bits = map[j];
210 FcChar32 base = ucs4 + j * 32;
211 int b = 0;
212
213 while (bits) {
214 if (bits & 1)
215 printf (" 0x%04x\n", base + b);
216 bits >>= 1;
217 b++;
218 }
219 }
220 }
221 }
222 } else {
223 printf (_("%s:%d Satisfy the coverage for %s language\n"), argv[i], index, lang);
224 }
225
226 FcCharSetDestroy (fcs);
227 FcCharSetDestroy (fcs_sub);
228
229 FT_Done_Face (face);
230 }
231
232 index++;
233 } while (index_set == 0);
234 }
235
236 FT_Done_FreeType (ftlib);
237
238 if (lang)
239 FcStrFree (lang);
240
241 FcFini();
242 return err;
243 }