fc-scan.c revision ca08ab68
1/*
2 * fontconfig/fc-scan/fc-scan.c
3 *
4 * Copyright © 2003 Keith Packard
5 * Copyright © 2008 Red Hat, Inc.
6 * Red Hat Author(s): Behdad Esfahbod
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
43#ifndef HAVE_GETOPT
44#define HAVE_GETOPT 0
45#endif
46#ifndef HAVE_GETOPT_LONG
47#define HAVE_GETOPT_LONG 0
48#endif
49
50#if HAVE_GETOPT_LONG
51#undef  _GNU_SOURCE
52#define _GNU_SOURCE
53#include <getopt.h>
54static const struct option longopts[] = {
55    {"format", 1, 0, 'f'},
56    {"version", 0, 0, 'V'},
57    {"help", 0, 0, 'h'},
58    {NULL,0,0,0},
59};
60#else
61#if HAVE_GETOPT
62extern char *optarg;
63extern int optind, opterr, optopt;
64#endif
65#endif
66
67static void
68usage (char *program, int error)
69{
70    FILE *file = error ? stderr : stdout;
71#if HAVE_GETOPT_LONG
72    fprintf (file, "usage: %s [-Vh] [-f FORMAT] [--format FORMAT] [--version] [--help] font-file...\n",
73	     program);
74#else
75    fprintf (file, "usage: %s [-Vh] [-f FORMAT] font-file...\n",
76	     program);
77#endif
78    fprintf (file, "Scan font files and directories, and print resulting pattern(s)\n");
79    fprintf (file, "\n");
80#if HAVE_GETOPT_LONG
81    fprintf (file, "  -f, --format=FORMAT  use the given output format\n");
82    fprintf (file, "  -V, --version        display font config version and exit\n");
83    fprintf (file, "  -h, --help           display this help and exit\n");
84#else
85    fprintf (file, "  -f FORMAT  (format)  use the given output format\n");
86    fprintf (file, "  -V         (version) display font config version and exit\n");
87    fprintf (file, "  -h         (help)    display this help and exit\n");
88#endif
89    exit (error);
90}
91
92int
93main (int argc, char **argv)
94{
95    FcChar8     *format = NULL;
96    int		i;
97    FcFontSet   *fs;
98#if HAVE_GETOPT_LONG || HAVE_GETOPT
99    int		c;
100
101#if HAVE_GETOPT_LONG
102    while ((c = getopt_long (argc, argv, "f:Vh", longopts, NULL)) != -1)
103#else
104    while ((c = getopt (argc, argv, "f:Vh")) != -1)
105#endif
106    {
107	switch (c) {
108	case 'f':
109	    format = (FcChar8 *) strdup (optarg);
110	    break;
111	case 'V':
112	    fprintf (stderr, "fontconfig version %d.%d.%d\n",
113		     FC_MAJOR, FC_MINOR, FC_REVISION);
114	    exit (0);
115	case 'h':
116	    usage (argv[0], 0);
117	default:
118	    usage (argv[0], 1);
119	}
120    }
121    i = optind;
122#else
123    i = 1;
124#endif
125
126    if (i == argc)
127	usage (argv[0], 1);
128
129    if (!FcInit ())
130    {
131	fprintf (stderr, "Can't init font config library\n");
132	return 1;
133    }
134
135    fs = FcFontSetCreate ();
136
137    for (; i < argc; i++)
138    {
139	const FcChar8 *file = (FcChar8*) argv[i];
140
141	if (!FcFileIsDir (file))
142	    FcFileScan (fs, NULL, NULL, NULL, file, FcTrue);
143	else
144	{
145	    FcStrSet *dirs = FcStrSetCreate ();
146	    FcStrList *strlist = FcStrListCreate (dirs);
147	    do
148	    {
149		FcDirScan (fs, dirs, NULL, NULL, file, FcTrue);
150	    }
151	    while ((file = FcStrListNext (strlist)));
152	    FcStrListDone (strlist);
153	    FcStrSetDestroy (dirs);
154	}
155    }
156
157    for (i = 0; i < fs->nfont; i++)
158    {
159	FcPattern *pat;
160
161	pat = fs->fonts[i];
162
163	if (format)
164	{
165	    FcChar8 *s;
166
167	    s = FcPatternFormat (pat, format);
168	    if (s)
169	    {
170		printf ("%s", s);
171		FcStrFree (s);
172	    }
173	}
174	else
175	{
176	    FcPatternPrint (pat);
177	}
178    }
179
180    FcFontSetDestroy (fs);
181
182    FcFini ();
183    return i > 0 ? 0 : 1;
184}
185