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