fcinit.c revision 18bd4a06
1/*
2 * fontconfig/src/fcinit.c
3 *
4 * Copyright © 2001 Keith Packard
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission.  The authors make no
13 * representations about the suitability of this software for any purpose.  It
14 * is provided "as is" without express or implied warranty.
15 *
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#include "fcint.h"
26#include <stdlib.h>
27
28#if defined(FC_ATOMIC_INT_NIL)
29#warning Could not find any system to define atomic_int macros, library may NOT be thread-safe.
30#endif
31#if defined(FC_MUTEX_IMPL_NIL)
32#warning Could not find any system to define mutex macros, library may NOT be thread-safe.
33#endif
34#if defined(FC_ATOMIC_INT_NIL) || defined(FC_MUTEX_IMPL_NIL)
35#warning To suppress these warnings, define FC_NO_MT.
36#endif
37
38static FcConfig *
39FcInitFallbackConfig (const FcChar8 *sysroot)
40{
41    FcConfig	*config;
42
43    config = FcConfigCreate ();
44    if (!config)
45	goto bail0;
46    FcConfigSetSysRoot (config, sysroot);
47    if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
48	goto bail1;
49    if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
50	goto bail1;
51    return config;
52
53bail1:
54    FcConfigDestroy (config);
55bail0:
56    return 0;
57}
58
59int
60FcGetVersion (void)
61{
62    return FC_VERSION;
63}
64
65/*
66 * Load the configuration files
67 */
68FcConfig *
69FcInitLoadOwnConfig (FcConfig *config)
70{
71    if (!config)
72    {
73	config = FcConfigCreate ();
74	if (!config)
75	    return NULL;
76    }
77
78    FcInitDebug ();
79
80    if (!FcConfigParseAndLoad (config, 0, FcTrue))
81    {
82	const FcChar8 *sysroot = FcConfigGetSysRoot (config);
83	FcConfig *fallback = FcInitFallbackConfig (sysroot);
84
85	FcConfigDestroy (config);
86
87	return fallback;
88    }
89
90    if (config->cacheDirs && config->cacheDirs->num == 0)
91    {
92	FcChar8 *prefix, *p;
93	size_t plen;
94	FcBool have_own = FcFalse;
95	char *env_file, *env_path;
96
97	env_file = getenv ("FONTCONFIG_FILE");
98	env_path = getenv ("FONTCONFIG_PATH");
99	if ((env_file != NULL && env_file[0] != 0) ||
100	    (env_path != NULL && env_path[0] != 0))
101	    have_own = FcTrue;
102
103	if (!have_own)
104	{
105	    fprintf (stderr,
106		     "Fontconfig warning: no <cachedir> elements found. Check configuration.\n");
107	    fprintf (stderr,
108		     "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
109		     FC_CACHEDIR);
110	}
111	prefix = FcConfigXdgCacheHome ();
112	if (!prefix)
113	    goto bail;
114	plen = strlen ((const char *)prefix);
115	p = realloc (prefix, plen + 12);
116	if (!p)
117	    goto bail;
118	prefix = p;
119	memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11);
120	prefix[plen + 11] = 0;
121	if (!have_own)
122	    fprintf (stderr,
123		     "Fontconfig warning: adding <cachedir prefix=\"xdg\">fontconfig</cachedir>\n");
124
125	if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR) ||
126	    !FcConfigAddCacheDir (config, (FcChar8 *) prefix))
127	{
128	    FcConfig *fallback;
129	    const FcChar8 *sysroot;
130
131	  bail:
132	    sysroot = FcConfigGetSysRoot (config);
133	    fprintf (stderr,
134		     "Fontconfig error: out of memory");
135	    if (prefix)
136		FcStrFree (prefix);
137	    fallback = FcInitFallbackConfig (sysroot);
138	    FcConfigDestroy (config);
139
140	    return fallback;
141	}
142	FcStrFree (prefix);
143    }
144
145    return config;
146}
147
148FcConfig *
149FcInitLoadConfig (void)
150{
151    return FcInitLoadOwnConfig (NULL);
152}
153
154/*
155 * Load the configuration files and scan for available fonts
156 */
157FcConfig *
158FcInitLoadOwnConfigAndFonts (FcConfig *config)
159{
160    config = FcInitLoadOwnConfig (config);
161    if (!config)
162	return 0;
163    if (!FcConfigBuildFonts (config))
164    {
165	FcConfigDestroy (config);
166	return 0;
167    }
168    return config;
169}
170
171FcConfig *
172FcInitLoadConfigAndFonts (void)
173{
174    return FcInitLoadOwnConfigAndFonts (NULL);
175}
176
177/*
178 * Initialize the default library configuration
179 */
180FcBool
181FcInit (void)
182{
183    return FcConfigInit ();
184}
185
186/*
187 * Free all library-allocated data structures.
188 */
189void
190FcFini (void)
191{
192    FcConfigFini ();
193    FcCacheFini ();
194    FcDefaultFini ();
195    FcObjectFini ();
196    FcConfigPathFini ();
197}
198
199/*
200 * Reread the configuration and available font lists
201 */
202FcBool
203FcInitReinitialize (void)
204{
205    FcConfig	*config;
206    FcBool	ret;
207
208    config = FcInitLoadConfigAndFonts ();
209    if (!config)
210	return FcFalse;
211    ret = FcConfigSetCurrent (config);
212    /* FcConfigSetCurrent() increases the refcount.
213     * decrease it here to avoid the memory leak.
214     */
215    FcConfigDestroy (config);
216
217    return ret;
218}
219
220FcBool
221FcInitBringUptoDate (void)
222{
223    FcConfig	*config = FcConfigGetCurrent ();
224    time_t	now;
225
226    if (!config)
227	return FcFalse;
228    /*
229     * rescanInterval == 0 disables automatic up to date
230     */
231    if (config->rescanInterval == 0)
232	return FcTrue;
233    /*
234     * Check no more often than rescanInterval seconds
235     */
236    now = time (0);
237    if (config->rescanTime + config->rescanInterval - now > 0)
238	return FcTrue;
239    /*
240     * If up to date, don't reload configuration
241     */
242    if (FcConfigUptoDate (0))
243	return FcTrue;
244    return FcInitReinitialize ();
245}
246
247#define __fcinit__
248#include "fcaliastail.h"
249#undef __fcinit__
250