fc-query.c 5.06 KB
Newer Older
Behdad Esfahbod's avatar
Behdad Esfahbod committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * fontconfig/fc-query/fc-query.c
 *
 * Copyright © 2003 Keith Packard
 * Copyright © 2008 Red Hat, Inc.
 * Red Hat Author(s): Behdad Esfahbod
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
12
 * documentation, and that the name of the author(s) not be used in
Behdad Esfahbod's avatar
Behdad Esfahbod committed
13
 * advertising or publicity pertaining to distribution of the software without
14
 * specific, written prior permission.  The authors make no
Behdad Esfahbod's avatar
Behdad Esfahbod committed
15 16 17
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
18
 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
Behdad Esfahbod's avatar
Behdad Esfahbod committed
19
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
20
 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
Behdad Esfahbod's avatar
Behdad Esfahbod committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#ifdef linux
#define HAVE_GETOPT_LONG 1
#endif
#define HAVE_GETOPT 1
#endif

#include <fontconfig/fontconfig.h>
#include <fontconfig/fcfreetype.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
Akira TAGOH's avatar
Akira TAGOH committed
42
#include <locale.h>
Behdad Esfahbod's avatar
Behdad Esfahbod committed
43

44 45 46 47 48 49 50 51
#ifdef ENABLE_NLS
#include <libintl.h>
#define _(x)		(dgettext(GETTEXT_PACKAGE, x))
#else
#define dgettext(d, s)	(s)
#define _(x)		(x)
#endif

Behdad Esfahbod's avatar
Behdad Esfahbod committed
52 53 54 55 56 57 58 59 60 61 62 63 64
#ifndef HAVE_GETOPT
#define HAVE_GETOPT 0
#endif
#ifndef HAVE_GETOPT_LONG
#define HAVE_GETOPT_LONG 0
#endif

#if HAVE_GETOPT_LONG
#undef  _GNU_SOURCE
#define _GNU_SOURCE
#include <getopt.h>
static const struct option longopts[] = {
    {"index", 1, 0, 'i'},
65
    {"brief", 0, 0, 'b'},
66
    {"format", 1, 0, 'f'},
Behdad Esfahbod's avatar
Behdad Esfahbod committed
67
    {"version", 0, 0, 'V'},
68
    {"help", 0, 0, 'h'},
Behdad Esfahbod's avatar
Behdad Esfahbod committed
69 70 71 72 73 74 75 76 77
    {NULL,0,0,0},
};
#else
#if HAVE_GETOPT
extern char *optarg;
extern int optind, opterr, optopt;
#endif
#endif

78 79
static void
usage (char *program, int error)
Behdad Esfahbod's avatar
Behdad Esfahbod committed
80
{
81
    FILE *file = error ? stderr : stdout;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
82
#if HAVE_GETOPT_LONG
83
    fprintf (file, _("usage: %s [-bVh] [-i index] [-f FORMAT] [--index index] [--brief] [--format FORMAT] [--version] [--help] font-file...\n"),
Behdad Esfahbod's avatar
Behdad Esfahbod committed
84 85
	     program);
#else
86
    fprintf (file, _("usage: %s [-bVh] [-i index] [-f FORMAT] font-file...\n"),
Behdad Esfahbod's avatar
Behdad Esfahbod committed
87 88
	     program);
#endif
89
    fprintf (file, _("Query font files and print resulting pattern(s)\n"));
90
    fprintf (file, "\n");
Behdad Esfahbod's avatar
Behdad Esfahbod committed
91
#if HAVE_GETOPT_LONG
92 93 94 95 96
    fprintf (file, _("  -i, --index INDEX    display the INDEX face of each font file only\n"));
    fprintf (file, _("  -b, --brief          display font pattern briefly\n"));
    fprintf (file, _("  -f, --format=FORMAT  use the given output format\n"));
    fprintf (file, _("  -V, --version        display font config version and exit\n"));
    fprintf (file, _("  -h, --help           display this help and exit\n"));
Behdad Esfahbod's avatar
Behdad Esfahbod committed
97
#else
98 99 100 101 102
    fprintf (file, _("  -i INDEX   (index)         display the INDEX face of each font file only\n"));
    fprintf (file, _("  -b         (brief)         display font pattern briefly\n"));
    fprintf (file, _("  -f FORMAT  (format)        use the given output format\n"));
    fprintf (file, _("  -V         (version)       display font config version and exit\n"));
    fprintf (file, _("  -h         (help)          display this help and exit\n"));
Behdad Esfahbod's avatar
Behdad Esfahbod committed
103
#endif
104
    exit (error);
Behdad Esfahbod's avatar
Behdad Esfahbod committed
105 106 107 108 109
}

int
main (int argc, char **argv)
{
110
    unsigned int id = (unsigned int) -1;
111
    int         brief = 0;
112
    FcFontSet   *fs;
113
    FcChar8     *format = NULL;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
114 115 116 117 118
    int		err = 0;
    int		i;
#if HAVE_GETOPT_LONG || HAVE_GETOPT
    int		c;

Akira TAGOH's avatar
Akira TAGOH committed
119
    setlocale (LC_ALL, "");
Behdad Esfahbod's avatar
Behdad Esfahbod committed
120
#if HAVE_GETOPT_LONG
121
    while ((c = getopt_long (argc, argv, "i:bf:Vh", longopts, NULL)) != -1)
Behdad Esfahbod's avatar
Behdad Esfahbod committed
122
#else
123
    while ((c = getopt (argc, argv, "i:bf:Vh")) != -1)
Behdad Esfahbod's avatar
Behdad Esfahbod committed
124 125 126 127
#endif
    {
	switch (c) {
	case 'i':
128
	    id = (unsigned int) strtol (optarg, NULL, 0); /* strtol() To handle -1. */
Behdad Esfahbod's avatar
Behdad Esfahbod committed
129
	    break;
130 131 132
	case 'b':
	    brief = 1;
	    break;
133 134 135
	case 'f':
	    format = (FcChar8 *) strdup (optarg);
	    break;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
136 137 138 139
	case 'V':
	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
		     FC_MAJOR, FC_MINOR, FC_REVISION);
	    exit (0);
140 141
	case 'h':
	    usage (argv[0], 0);
Behdad Esfahbod's avatar
Behdad Esfahbod committed
142
	default:
143
	    usage (argv[0], 1);
Behdad Esfahbod's avatar
Behdad Esfahbod committed
144 145 146 147 148 149 150 151
	}
    }
    i = optind;
#else
    i = 1;
#endif

    if (i == argc)
152
	usage (argv[0], 1);
Behdad Esfahbod's avatar
Behdad Esfahbod committed
153

154 155
    fs = FcFontSetCreate ();

Behdad Esfahbod's avatar
Behdad Esfahbod committed
156 157
    for (; i < argc; i++)
    {
158
	if (!FcFreeTypeQueryAll ((FcChar8*) argv[i], id, NULL, NULL, fs))
159
	{
160
	    fprintf (stderr, _("Can't query face %u of font file %s\n"), id, argv[i]);
161
	    err = 1;
162
	}
163
    }
Behdad Esfahbod's avatar
Behdad Esfahbod committed
164

165 166 167
    for (i = 0; i < fs->nfont; i++)
    {
	FcPattern *pat = fs->fonts[i];
168

169 170 171 172 173 174
	if (brief)
	{
	    FcPatternDel (pat, FC_CHARSET);
	    FcPatternDel (pat, FC_LANG);
	}

175 176 177
	if (format)
	{
	    FcChar8 *s;
Behdad Esfahbod's avatar
Behdad Esfahbod committed
178

179 180
	    s = FcPatternFormat (pat, format);
	    if (s)
181
	    {
182 183
		printf ("%s", s);
		FcStrFree (s);
184
	    }
185 186 187 188 189
	}
	else
	{
	    FcPatternPrint (pat);
	}
Behdad Esfahbod's avatar
Behdad Esfahbod committed
190 191
    }

192 193
    FcFontSetDestroy (fs);

Behdad Esfahbod's avatar
Behdad Esfahbod committed
194 195 196
    FcFini ();
    return err;
}