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