Blame fc-scan/fc-scan.c

Packit 352660
/*
Packit 352660
 * fontconfig/fc-scan/fc-scan.c
Packit 352660
 *
Packit 352660
 * Copyright © 2003 Keith Packard
Packit 352660
 * Copyright © 2008 Red Hat, Inc.
Packit 352660
 * Red Hat Author(s): Behdad Esfahbod
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
    {"brief", 0, 0, 'b'},
Packit 352660
    {"format", 1, 0, 'f'},
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 [-bVh] [-f FORMAT] [--brief] [--format FORMAT] [--version] [--help] font-file...\n"),
Packit 352660
	     program);
Packit 352660
#else
Packit 352660
    fprintf (file, _("usage: %s [-bVh] [-f FORMAT] font-file...\n"),
Packit 352660
	     program);
Packit 352660
#endif
Packit 352660
    fprintf (file, _("Scan font files and directories, and print resulting pattern(s)\n"));
Packit 352660
    fprintf (file, "\n");
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    fprintf (file, _("  -b, --brief          display font pattern briefly\n"));
Packit 352660
    fprintf (file, _("  -f, --format=FORMAT  use the given output format\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, _("  -b         (brief)         display font pattern briefly\n"));
Packit 352660
    fprintf (file, _("  -f FORMAT  (format)        use the given output format\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         brief = 0;
Packit 352660
    FcChar8     *format = NULL;
Packit 352660
    int		i;
Packit 352660
    FcFontSet   *fs;
Packit 352660
#if HAVE_GETOPT_LONG || HAVE_GETOPT
Packit 352660
    int		c;
Packit 352660
Packit 352660
    setlocale (LC_ALL, "");
Packit 352660
#if HAVE_GETOPT_LONG
Packit 352660
    while ((c = getopt_long (argc, argv, "bf:Vh", longopts, NULL)) != -1)
Packit 352660
#else
Packit 352660
    while ((c = getopt (argc, argv, "bf:Vh")) != -1)
Packit 352660
#endif
Packit 352660
    {
Packit 352660
	switch (c) {
Packit 352660
	case 'b':
Packit 352660
	    brief = 1;
Packit 352660
	    break;
Packit 352660
	case 'f':
Packit 352660
	    format = (FcChar8 *) strdup (optarg);
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
#endif
Packit 352660
Packit 352660
    if (i == argc)
Packit 352660
	usage (argv[0], 1);
Packit 352660
Packit 352660
    fs = FcFontSetCreate ();
Packit 352660
Packit 352660
    for (; i < argc; i++)
Packit 352660
    {
Packit 352660
	const FcChar8 *file = (FcChar8*) argv[i];
Packit 352660
Packit 352660
	if (!FcFileIsDir (file))
Packit 352660
	    FcFileScan (fs, NULL, NULL, NULL, file, FcTrue);
Packit 352660
	else
Packit 352660
	{
Packit 352660
	    FcStrSet *dirs = FcStrSetCreate ();
Packit 352660
	    FcStrList *strlist = FcStrListCreate (dirs);
Packit 352660
	    do
Packit 352660
	    {
Packit 352660
		FcDirScan (fs, dirs, NULL, NULL, file, FcTrue);
Packit 352660
	    }
Packit 352660
	    while ((file = FcStrListNext (strlist)));
Packit 352660
	    FcStrListDone (strlist);
Packit 352660
	    FcStrSetDestroy (dirs);
Packit 352660
	}
Packit 352660
    }
Packit 352660
Packit 352660
    for (i = 0; i < fs->nfont; i++)
Packit 352660
    {
Packit 352660
	FcPattern *pat = fs->fonts[i];
Packit 352660
Packit 352660
	if (brief)
Packit 352660
	{
Packit 352660
	    FcPatternDel (pat, FC_CHARSET);
Packit 352660
	    FcPatternDel (pat, FC_LANG);
Packit 352660
	}
Packit 352660
Packit 352660
	if (format)
Packit 352660
	{
Packit 352660
	    FcChar8 *s;
Packit 352660
Packit 352660
	    s = FcPatternFormat (pat, format);
Packit 352660
	    if (s)
Packit 352660
	    {
Packit 352660
		printf ("%s", s);
Packit 352660
		FcStrFree (s);
Packit 352660
	    }
Packit 352660
	}
Packit 352660
	else
Packit 352660
	{
Packit 352660
	    FcPatternPrint (pat);
Packit 352660
	}
Packit 352660
    }
Packit 352660
Packit 352660
    FcFontSetDestroy (fs);
Packit 352660
Packit 352660
    FcFini ();
Packit 352660
    return i > 0 ? 0 : 1;
Packit 352660
}