fc-validate.c revision c9710b42
1c9710b42Smrg/*
2c9710b42Smrg * fontconfig/fc-validate/fc-validate.c
3c9710b42Smrg *
4c9710b42Smrg * Copyright © 2003 Keith Packard
5c9710b42Smrg * Copyright © 2012 Red Hat, Inc.
6c9710b42Smrg * Red Hat Author(s): Akira TAGOH
7c9710b42Smrg *
8c9710b42Smrg * Permission to use, copy, modify, distribute, and sell this software and its
9c9710b42Smrg * documentation for any purpose is hereby granted without fee, provided that
10c9710b42Smrg * the above copyright notice appear in all copies and that both that
11c9710b42Smrg * copyright notice and this permission notice appear in supporting
12c9710b42Smrg * documentation, and that the name of the author(s) not be used in
13c9710b42Smrg * advertising or publicity pertaining to distribution of the software without
14c9710b42Smrg * specific, written prior permission.  The authors make no
15c9710b42Smrg * representations about the suitability of this software for any purpose.  It
16c9710b42Smrg * is provided "as is" without express or implied warranty.
17c9710b42Smrg *
18c9710b42Smrg * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
19c9710b42Smrg * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20c9710b42Smrg * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
21c9710b42Smrg * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
22c9710b42Smrg * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
23c9710b42Smrg * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
24c9710b42Smrg * PERFORMANCE OF THIS SOFTWARE.
25c9710b42Smrg */
26c9710b42Smrg
27c9710b42Smrg#ifdef HAVE_CONFIG_H
28c9710b42Smrg#include <config.h>
29c9710b42Smrg#else
30c9710b42Smrg#ifdef linux
31c9710b42Smrg#define HAVE_GETOPT_LONG 1
32c9710b42Smrg#endif
33c9710b42Smrg#define HAVE_GETOPT 1
34c9710b42Smrg#endif
35c9710b42Smrg
36c9710b42Smrg#include <fontconfig/fontconfig.h>
37c9710b42Smrg#include <fontconfig/fcfreetype.h>
38c9710b42Smrg#include <stdio.h>
39c9710b42Smrg#include <unistd.h>
40c9710b42Smrg#include <stdlib.h>
41c9710b42Smrg#include <string.h>
42c9710b42Smrg#include <locale.h>
43c9710b42Smrg
44c9710b42Smrg#ifndef HAVE_GETOPT
45c9710b42Smrg#define HAVE_GETOPT 0
46c9710b42Smrg#endif
47c9710b42Smrg#ifndef HAVE_GETOPT_LONG
48c9710b42Smrg#define HAVE_GETOPT_LONG 0
49c9710b42Smrg#endif
50c9710b42Smrg
51c9710b42Smrg#if HAVE_GETOPT_LONG
52c9710b42Smrg#undef  _GNU_SOURCE
53c9710b42Smrg#define _GNU_SOURCE
54c9710b42Smrg#include <getopt.h>
55c9710b42Smrgstatic const struct option longopts[] = {
56c9710b42Smrg    {"index", 1, 0, 'i'},
57c9710b42Smrg    {"lang", 1, 0, 'l'},
58c9710b42Smrg    {"verbose", 0, 0, 'v'},
59c9710b42Smrg    {"version", 0, 0, 'V'},
60c9710b42Smrg    {"help", 0, 0, 'h'},
61c9710b42Smrg    {NULL,0,0,0},
62c9710b42Smrg};
63c9710b42Smrg#else
64c9710b42Smrg#if HAVE_GETOPT
65c9710b42Smrgextern char *optarg;
66c9710b42Smrgextern int optind, opterr, optopt;
67c9710b42Smrg#endif
68c9710b42Smrg#endif
69c9710b42Smrg
70c9710b42Smrgstatic void
71c9710b42Smrgusage (char *program, int error)
72c9710b42Smrg{
73c9710b42Smrg    FILE *file = error ? stderr : stdout;
74c9710b42Smrg#if HAVE_GETOPT_LONG
75c9710b42Smrg    fprintf (file, "usage: %s [-Vhv] [-i index] [-l LANG] [--index index] [--lang LANG] [--verbose] [--version] [--help] font-file...\n",
76c9710b42Smrg	     program);
77c9710b42Smrg#else
78c9710b42Smrg    fprintf (file, "usage: %s [-Vhv] [-i index] [-l LANG] font-file...\n",
79c9710b42Smrg	     program);
80c9710b42Smrg#endif
81c9710b42Smrg    fprintf (file, "Validate font files and print result\n");
82c9710b42Smrg    fprintf (file, "\n");
83c9710b42Smrg#if HAVE_GETOPT_LONG
84c9710b42Smrg    fprintf (file, "  -i, --index INDEX    display the INDEX face of each font file only\n");
85c9710b42Smrg    fprintf (file, "  -l, --lang=LANG      set LANG instead of current locale\n");
86c9710b42Smrg    fprintf (file, "  -v, --verbose        show more detailed information\n");
87c9710b42Smrg    fprintf (file, "  -V, --version        display font config version and exit\n");
88c9710b42Smrg    fprintf (file, "  -h, --help           display this help and exit\n");
89c9710b42Smrg#else
90c9710b42Smrg    fprintf (file, "  -i INDEX   (index)        display the INDEX face of each font file only\n");
91c9710b42Smrg    fprintf (file, "  -l LANG    (lang)         set LANG instead of current locale\n");
92c9710b42Smrg    fprintf (file, "  -v         (verbose)      show more detailed information\n");
93c9710b42Smrg    fprintf (file, "  -V         (version)      display font config version and exit\n");
94c9710b42Smrg    fprintf (file, "  -h         (help)         display this help and exit\n");
95c9710b42Smrg#endif
96c9710b42Smrg    exit (error);
97c9710b42Smrg}
98c9710b42Smrg
99c9710b42Smrgint
100c9710b42Smrgmain (int argc, char **argv)
101c9710b42Smrg{
102c9710b42Smrg    int		index_set = 0;
103c9710b42Smrg    int		set_index = 0;
104c9710b42Smrg    FcChar8     *lang = NULL;
105c9710b42Smrg    const FcCharSet *fcs_lang = NULL;
106c9710b42Smrg    int		err = 0;
107c9710b42Smrg    int		i;
108c9710b42Smrg    FT_Library  ftlib;
109c9710b42Smrg    FcBool      verbose = FcFalse;
110c9710b42Smrg#if HAVE_GETOPT_LONG || HAVE_GETOPT
111c9710b42Smrg    int		c;
112c9710b42Smrg
113c9710b42Smrg    setlocale (LC_ALL, "");
114c9710b42Smrg
115c9710b42Smrg#if HAVE_GETOPT_LONG
116c9710b42Smrg    while ((c = getopt_long (argc, argv, "i:l:mVhv", longopts, NULL)) != -1)
117c9710b42Smrg#else
118c9710b42Smrg    while ((c = getopt (argc, argv, "i:l:mVhv")) != -1)
119c9710b42Smrg#endif
120c9710b42Smrg    {
121c9710b42Smrg	switch (c) {
122c9710b42Smrg	case 'i':
123c9710b42Smrg	    index_set = 1;
124c9710b42Smrg	    set_index = atoi (optarg);
125c9710b42Smrg	    break;
126c9710b42Smrg	case 'l':
127c9710b42Smrg	    lang = (FcChar8 *) FcLangNormalize ((const FcChar8 *) optarg);
128c9710b42Smrg	    break;
129c9710b42Smrg	case 'v':
130c9710b42Smrg	    verbose = FcTrue;
131c9710b42Smrg	    break;
132c9710b42Smrg	case 'V':
133c9710b42Smrg	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
134c9710b42Smrg		     FC_MAJOR, FC_MINOR, FC_REVISION);
135c9710b42Smrg	    exit (0);
136c9710b42Smrg	case 'h':
137c9710b42Smrg	    usage (argv[0], 0);
138c9710b42Smrg	default:
139c9710b42Smrg	    usage (argv[0], 1);
140c9710b42Smrg	}
141c9710b42Smrg    }
142c9710b42Smrg    i = optind;
143c9710b42Smrg#else
144c9710b42Smrg    i = 1;
145c9710b42Smrg    verbose = FcTrue;
146c9710b42Smrg#endif
147c9710b42Smrg
148c9710b42Smrg    if (i == argc)
149c9710b42Smrg	usage (argv[0], 1);
150c9710b42Smrg
151c9710b42Smrg    if (!lang)
152c9710b42Smrg	lang = FcLangNormalize ((const FcChar8 *) setlocale (LC_CTYPE, NULL));
153c9710b42Smrg
154c9710b42Smrg    if (lang)
155c9710b42Smrg	fcs_lang = FcLangGetCharSet (lang);
156c9710b42Smrg
157c9710b42Smrg    if (FT_Init_FreeType (&ftlib))
158c9710b42Smrg    {
159c9710b42Smrg	fprintf (stderr, "Can't initalize FreeType library\n");
160c9710b42Smrg	return 1;
161c9710b42Smrg    }
162c9710b42Smrg
163c9710b42Smrg    for (; i < argc; i++)
164c9710b42Smrg    {
165c9710b42Smrg	int index;
166c9710b42Smrg
167c9710b42Smrg	index = set_index;
168c9710b42Smrg
169c9710b42Smrg	do {
170c9710b42Smrg	    FT_Face face;
171c9710b42Smrg	    FcCharSet *fcs, *fcs_sub;
172c9710b42Smrg
173c9710b42Smrg	    if (FT_New_Face (ftlib, argv[i], index, &face))
174c9710b42Smrg	    {
175c9710b42Smrg		if (!index_set && index > 0)
176c9710b42Smrg		    break;
177c9710b42Smrg		fprintf (stderr, "Unable to open %s\n", argv[i]);
178c9710b42Smrg		err = 1;
179c9710b42Smrg	    }
180c9710b42Smrg	    else
181c9710b42Smrg	    {
182c9710b42Smrg		FcChar32 count;
183c9710b42Smrg
184c9710b42Smrg		fcs = FcFreeTypeCharSet (face, NULL);
185c9710b42Smrg		fcs_sub = FcCharSetSubtract (fcs_lang, fcs);
186c9710b42Smrg
187c9710b42Smrg		count = FcCharSetCount (fcs_sub);
188c9710b42Smrg		if (count > 0)
189c9710b42Smrg		{
190c9710b42Smrg		    FcChar32 ucs4, pos, map[FC_CHARSET_MAP_SIZE];
191c9710b42Smrg
192c9710b42Smrg		    printf ("%s:%d Missing %d glyph(s) to satisfy the coverage for %s language\n",
193c9710b42Smrg			    argv[i], index, count, lang);
194c9710b42Smrg
195c9710b42Smrg		    if (verbose)
196c9710b42Smrg		    {
197c9710b42Smrg			for (ucs4 = FcCharSetFirstPage (fcs_sub, map, &pos);
198c9710b42Smrg			     ucs4 != FC_CHARSET_DONE;
199c9710b42Smrg			     ucs4 = FcCharSetNextPage (fcs_sub, map, &pos))
200c9710b42Smrg			{
201c9710b42Smrg			    int j;
202c9710b42Smrg
203c9710b42Smrg			    for (j = 0; j < FC_CHARSET_MAP_SIZE; j++)
204c9710b42Smrg			    {
205c9710b42Smrg				FcChar32 bits = map[j];
206c9710b42Smrg				FcChar32 base = ucs4 + j * 32;
207c9710b42Smrg				int b = 0;
208c9710b42Smrg
209c9710b42Smrg				while (bits)
210c9710b42Smrg				{
211c9710b42Smrg				    if (bits & 1)
212c9710b42Smrg					printf ("  0x%04x\n", base + b);
213c9710b42Smrg				    bits >>= 1;
214c9710b42Smrg				    b++;
215c9710b42Smrg				}
216c9710b42Smrg			    }
217c9710b42Smrg			}
218c9710b42Smrg		    }
219c9710b42Smrg		}
220c9710b42Smrg		else
221c9710b42Smrg		{
222c9710b42Smrg		    printf ("%s:%d Satisfy the coverage for %s language\n", argv[i], index, lang);
223c9710b42Smrg		}
224c9710b42Smrg
225c9710b42Smrg		FcCharSetDestroy (fcs);
226c9710b42Smrg		FcCharSetDestroy (fcs_sub);
227c9710b42Smrg
228c9710b42Smrg		FT_Done_Face (face);
229c9710b42Smrg	    }
230c9710b42Smrg
231c9710b42Smrg	    index++;
232c9710b42Smrg	} while (index_set == 0);
233c9710b42Smrg    }
234c9710b42Smrg
235c9710b42Smrg    FT_Done_FreeType (ftlib);
236c9710b42Smrg
237c9710b42Smrg    if (lang)
238c9710b42Smrg	FcStrFree (lang);
239c9710b42Smrg
240c9710b42Smrg    FcFini ();
241c9710b42Smrg    return err;
242c9710b42Smrg}
243