Blame fc-validate/fc-validate.c

Packit 352660
/*
Packit 352660
 * fontconfig/fc-validate/fc-validate.c
Packit 352660
 *
Packit 352660
 * Copyright © 2003 Keith Packard
Packit 352660
 * Copyright © 2012 Red Hat, Inc.
Packit 352660
 * Red Hat Author(s): Akira TAGOH
Packit 352660
 *
Packit 352660
 * Permission to use, copy, modify, distribute, and sell this software and its
Packit 352660
 * documentation for any purpose is hereby granted without fee, provided that
Packit 352660
 * the above copyright notice appear in all copies and that both that
Packit 352660
 * copyright notice and this permission notice appear in supporting
Packit 352660
 * documentation, and that the name of the author(s) not be used in
Packit 352660
 * advertising or publicity pertaining to distribution of the software without
Packit 352660
 * specific, written prior permission.  The authors make no
Packit 352660
 * representations about the suitability of this software for any purpose.  It
Packit 352660
 * is provided "as is" without express or implied warranty.
Packit 352660
 *
Packit 352660
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Packit 352660
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
Packit 352660
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Packit 352660
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
Packit 352660
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
Packit 352660
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
Packit 352660
 * PERFORMANCE OF THIS SOFTWARE.
Packit 352660
 */
Packit 352660
Packit 352660
#ifdef HAVE_CONFIG_H
Packit 352660
#include <config.h>
Packit 352660
#else
Packit 352660
#ifdef linux
Packit 352660
#define HAVE_GETOPT_LONG 1
Packit 352660
#endif
Packit 352660
#define HAVE_GETOPT 1
Packit 352660
#endif
Packit 352660
Packit 352660
#include <fontconfig/fontconfig.h>
Packit 352660
#include <fontconfig/fcfreetype.h>
Packit 352660
#include <stdio.h>
Packit 352660
#include <unistd.h>
Packit 352660
#include <stdlib.h>
Packit 352660
#include <string.h>
Packit 352660
#include <locale.h>
Packit 352660
Packit 352660
#ifdef ENABLE_NLS
Packit 352660
#include <libintl.h>
Packit 352660
#define _(x)		(dgettext(GETTEXT_PACKAGE, x))
Packit 352660
#else
Packit 352660
#define dgettext(d, s)	(s)
Packit 352660
#define _(x)		(x)
Packit 352660
#endif
Packit 352660
Packit 352660
#ifndef HAVE_GETOPT
Packit 352660
#define HAVE_GETOPT 0
Packit 352660
#endif
Packit 352660
#ifndef HAVE_GETOPT_LONG
Packit 352660
#define HAVE_GETOPT_LONG 0
Packit 352660
#endif
Packit 352660
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
#undef  _GNU_SOURCE
Packit 352660
#define _GNU_SOURCE
Packit 352660
#include <getopt.h>
Packit 352660
static const struct option longopts[] = {
Packit 352660
    {"index", 1, 0, 'i'},
Packit 352660
    {"lang", 1, 0, 'l'},
Packit 352660
    {"verbose", 0, 0, 'v'},
Packit 352660
    {"version", 0, 0, 'V'},
Packit 352660
    {"help", 0, 0, 'h'},
Packit 352660
    {NULL,0,0,0},
Packit 352660
};
Packit 352660
#else
Packit 352660
#if HAVE_GETOPT
Packit 352660
extern char *optarg;
Packit 352660
extern int optind, opterr, optopt;
Packit 352660
#endif
Packit 352660
#endif
Packit 352660
Packit 352660
static void
Packit 352660
usage (char *program, int error)
Packit 352660
{
Packit 352660
    FILE *file = error ? stderr : stdout;
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    fprintf (file, _("usage: %s [-Vhv] [-i index] [-l LANG] [--index index] [--lang LANG] [--verbose] [--version] [--help] font-file...\n"),
Packit 352660
	     program);
Packit 352660
#else
Packit 352660
    fprintf (file, _("usage: %s [-Vhv] [-i index] [-l LANG] font-file...\n"),
Packit 352660
	     program);
Packit 352660
#endif
Packit 352660
    fprintf (file, _("Validate font files and print result\n"));
Packit 352660
    fprintf (file, "\n");
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    fprintf (file, _("  -i, --index INDEX    display the INDEX face of each font file only\n"));
Packit 352660
    fprintf (file, _("  -l, --lang=LANG      set LANG instead of current locale\n"));
Packit 352660
    fprintf (file, _("  -v, --verbose        show more detailed information\n"));
Packit 352660
    fprintf (file, _("  -V, --version        display font config version and exit\n"));
Packit 352660
    fprintf (file, _("  -h, --help           display this help and exit\n"));
Packit 352660
#else
Packit 352660
    fprintf (file, _("  -i INDEX   (index)        display the INDEX face of each font file only\n"));
Packit 352660
    fprintf (file, _("  -l LANG    (lang)         set LANG instead of current locale\n"));
Packit 352660
    fprintf (file, _("  -v         (verbose)      show more detailed information\n"));
Packit 352660
    fprintf (file, _("  -V         (version)      display font config version and exit\n"));
Packit 352660
    fprintf (file, _("  -h         (help)         display this help and exit\n"));
Packit 352660
#endif
Packit 352660
    exit (error);
Packit 352660
}
Packit 352660
Packit 352660
int
Packit 352660
main (int argc, char **argv)
Packit 352660
{
Packit 352660
    int		index_set = 0;
Packit 352660
    int		set_index = 0;
Packit 352660
    FcChar8     *lang = NULL;
Packit 352660
    const FcCharSet *fcs_lang = NULL;
Packit 352660
    int		err = 0;
Packit 352660
    int		i;
Packit 352660
    FT_Library  ftlib;
Packit 352660
    FcBool      verbose = FcFalse;
Packit 352660
#if HAVE_GETOPT_LONG || HAVE_GETOPT
Packit 352660
    int		c;
Packit 352660
Packit 352660
    setlocale (LC_ALL, "");
Packit 352660
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    while ((c = getopt_long (argc, argv, "i:l:mVhv", longopts, NULL)) != -1)
Packit 352660
#else
Packit 352660
    while ((c = getopt (argc, argv, "i:l:mVhv")) != -1)
Packit 352660
#endif
Packit 352660
    {
Packit 352660
	switch (c) {
Packit 352660
	case 'i':
Packit 352660
	    index_set = 1;
Packit 352660
	    set_index = atoi (optarg);
Packit 352660
	    break;
Packit 352660
	case 'l':
Packit 352660
	    lang = (FcChar8 *) FcLangNormalize ((const FcChar8 *) optarg);
Packit 352660
	    break;
Packit 352660
	case 'v':
Packit 352660
	    verbose = FcTrue;
Packit 352660
	    break;
Packit 352660
	case 'V':
Packit 352660
	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
Packit 352660
		     FC_MAJOR, FC_MINOR, FC_REVISION);
Packit 352660
	    exit (0);
Packit 352660
	case 'h':
Packit 352660
	    usage (argv[0], 0);
Packit 352660
	default:
Packit 352660
	    usage (argv[0], 1);
Packit 352660
	}
Packit 352660
    }
Packit 352660
    i = optind;
Packit 352660
#else
Packit 352660
    i = 1;
Packit 352660
    verbose = FcTrue;
Packit 352660
#endif
Packit 352660
Packit 352660
    if (i == argc)
Packit 352660
	usage (argv[0], 1);
Packit 352660
Packit 352660
    if (!lang)
Packit 352660
	lang = FcLangNormalize ((const FcChar8 *) setlocale (LC_CTYPE, NULL));
Packit 352660
Packit 352660
    if (lang)
Packit 352660
	fcs_lang = FcLangGetCharSet (lang);
Packit 352660
Packit 352660
    if (FT_Init_FreeType (&ftlib))
Packit 352660
    {
Packit 352660
	fprintf (stderr, _("Can't initalize FreeType library\n"));
Packit 352660
	return 1;
Packit 352660
    }
Packit 352660
Packit 352660
    for (; i < argc; i++)
Packit 352660
    {
Packit 352660
	int index;
Packit 352660
Packit 352660
	index = set_index;
Packit 352660
Packit 352660
	do {
Packit 352660
	    FT_Face face;
Packit 352660
	    FcCharSet *fcs, *fcs_sub;
Packit 352660
Packit 352660
	    if (FT_New_Face (ftlib, argv[i], index, &face))
Packit 352660
	    {
Packit 352660
		if (!index_set && index > 0)
Packit 352660
		    break;
Packit 352660
		fprintf (stderr, _("Unable to open %s\n"), argv[i]);
Packit 352660
		err = 1;
Packit 352660
	    }
Packit 352660
	    else
Packit 352660
	    {
Packit 352660
		FcChar32 count;
Packit 352660
Packit 352660
		fcs = FcFreeTypeCharSet (face, NULL);
Packit 352660
		fcs_sub = FcCharSetSubtract (fcs_lang, fcs);
Packit 352660
Packit 352660
		count = FcCharSetCount (fcs_sub);
Packit 352660
		if (count > 0)
Packit 352660
		{
Packit 352660
		    FcChar32 ucs4, pos, map[FC_CHARSET_MAP_SIZE];
Packit 352660
Packit 352660
		    printf (_("%s:%d Missing %d glyph(s) to satisfy the coverage for %s language\n"),
Packit 352660
			    argv[i], index, count, lang);
Packit 352660
Packit 352660
		    if (verbose)
Packit 352660
		    {
Packit 352660
			for (ucs4 = FcCharSetFirstPage (fcs_sub, map, &pos;;
Packit 352660
			     ucs4 != FC_CHARSET_DONE;
Packit 352660
			     ucs4 = FcCharSetNextPage (fcs_sub, map, &pos))
Packit 352660
			{
Packit 352660
			    int j;
Packit 352660
Packit 352660
			    for (j = 0; j < FC_CHARSET_MAP_SIZE; j++)
Packit 352660
			    {
Packit 352660
				FcChar32 bits = map[j];
Packit 352660
				FcChar32 base = ucs4 + j * 32;
Packit 352660
				int b = 0;
Packit 352660
Packit 352660
				while (bits)
Packit 352660
				{
Packit 352660
				    if (bits & 1)
Packit 352660
					printf ("  0x%04x\n", base + b);
Packit 352660
				    bits >>= 1;
Packit 352660
				    b++;
Packit 352660
				}
Packit 352660
			    }
Packit 352660
			}
Packit 352660
		    }
Packit 352660
		}
Packit 352660
		else
Packit 352660
		{
Packit 352660
		    printf (_("%s:%d Satisfy the coverage for %s language\n"), argv[i], index, lang);
Packit 352660
		}
Packit 352660
Packit 352660
		FcCharSetDestroy (fcs);
Packit 352660
		FcCharSetDestroy (fcs_sub);
Packit 352660
Packit 352660
		FT_Done_Face (face);
Packit 352660
	    }
Packit 352660
Packit 352660
	    index++;
Packit 352660
	} while (index_set == 0);
Packit 352660
    }
Packit 352660
Packit 352660
    FT_Done_FreeType (ftlib);
Packit 352660
Packit 352660
    if (lang)
Packit 352660
	FcStrFree (lang);
Packit 352660
Packit 352660
    FcFini ();
Packit 352660
    return err;
Packit 352660
}