fcinit.c revision a6844aab
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission.  Keith Packard makes 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
28static FcConfig *
29FcInitFallbackConfig (void)
30{
31    FcConfig	*config;
32
33    config = FcConfigCreate ();
34    if (!config)
35	goto bail0;
36    if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
37	goto bail1;
38    if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
39	goto bail1;
40    return config;
41
42bail1:
43    FcConfigDestroy (config);
44bail0:
45    return 0;
46}
47
48int
49FcGetVersion (void)
50{
51    return FC_VERSION;
52}
53
54/*
55 * Load the configuration files
56 */
57FcConfig *
58FcInitLoadConfig (void)
59{
60    FcConfig	*config;
61
62    FcInitDebug ();
63    config = FcConfigCreate ();
64    if (!config)
65	return FcFalse;
66
67    if (!FcConfigParseAndLoad (config, 0, FcTrue))
68    {
69	FcConfigDestroy (config);
70	return FcInitFallbackConfig ();
71    }
72
73    if (config->cacheDirs && config->cacheDirs->num == 0)
74    {
75	fprintf (stderr,
76		 "Fontconfig warning: no <cachedir> elements found. Check configuration.\n");
77	fprintf (stderr,
78		 "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
79		 FC_CACHEDIR);
80	fprintf (stderr,
81		 "Fontconfig warning: adding <cachedir>~/.fontconfig</cachedir>\n");
82	if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR) ||
83	    !FcConfigAddCacheDir (config, (FcChar8 *) "~/.fontconfig"))
84	{
85	    fprintf (stderr,
86		     "Fontconfig error: out of memory");
87	    FcConfigDestroy (config);
88	    return FcInitFallbackConfig ();
89	}
90    }
91
92    return config;
93}
94
95/*
96 * Load the configuration files and scan for available fonts
97 */
98FcConfig *
99FcInitLoadConfigAndFonts (void)
100{
101    FcConfig	*config = FcInitLoadConfig ();
102
103    FcInitDebug ();
104    if (!config)
105	return 0;
106    if (!FcConfigBuildFonts (config))
107    {
108	FcConfigDestroy (config);
109	return 0;
110    }
111    return config;
112}
113
114/*
115 * Initialize the default library configuration
116 */
117FcBool
118FcInit (void)
119{
120    FcConfig	*config;
121
122    if (_fcConfig)
123	return FcTrue;
124    config = FcInitLoadConfigAndFonts ();
125    if (!config)
126	return FcFalse;
127    FcConfigSetCurrent (config);
128    if (FcDebug() & FC_DBG_MEMORY)
129	FcMemReport ();
130    return FcTrue;
131}
132
133/*
134 * Free all library-allocated data structures.
135 */
136void
137FcFini (void)
138{
139    if (_fcConfig)
140	FcConfigDestroy (_fcConfig);
141
142    FcPatternFini ();
143    FcCacheFini ();
144    if (FcDebug() & FC_DBG_MEMORY)
145	FcMemReport ();
146}
147
148/*
149 * Reread the configuration and available font lists
150 */
151FcBool
152FcInitReinitialize (void)
153{
154    FcConfig	*config;
155
156    config = FcInitLoadConfigAndFonts ();
157    if (!config)
158	return FcFalse;
159    FcConfigSetCurrent (config);
160    return FcTrue;
161}
162
163FcBool
164FcInitBringUptoDate (void)
165{
166    FcConfig	*config = FcConfigGetCurrent ();
167    time_t	now;
168
169    /*
170     * rescanInterval == 0 disables automatic up to date
171     */
172    if (config->rescanInterval == 0)
173	return FcTrue;
174    /*
175     * Check no more often than rescanInterval seconds
176     */
177    now = time (0);
178    if (config->rescanTime + config->rescanInterval - now > 0)
179	return FcTrue;
180    /*
181     * If up to date, don't reload configuration
182     */
183    if (FcConfigUptoDate (0))
184	return FcTrue;
185    return FcInitReinitialize ();
186}
187
188static struct {
189    char    name[16];
190    int	    alloc_count;
191    int	    alloc_mem;
192    int	    free_count;
193    int	    free_mem;
194} FcInUse[FC_MEM_NUM] = {
195    { "charset" },
196    { "charleaf" },
197    { "fontset" },
198    { "fontptr" },
199    { "objectset" },
200    { "objectptr" },
201    { "matrix" },
202    { "pattern" },
203    { "patelt" },
204    { "vallist" },
205    { "substate" },
206    { "string" },
207    { "listbuck" },
208    { "strset" },
209    { "strlist" },
210    { "config" },
211    { "langset" },
212    { "atomic" },
213    { "blanks" },
214    { "cache" },
215    { "strbuf" },
216    { "subst" },
217    { "objecttype" },
218    { "constant" },
219    { "test" },
220    { "expr" },
221    { "vstack" },
222    { "attr" },
223    { "pstack" },
224    { "staticstr" },
225};
226
227static int  FcAllocCount, FcAllocMem;
228static int  FcFreeCount, FcFreeMem;
229
230static int  FcMemNotice = 1*1024*1024;
231
232static int  FcAllocNotify, FcFreeNotify;
233
234void
235FcMemReport (void)
236{
237    int	i;
238    printf ("Fc Memory Usage:\n");
239    printf ("\t   Which       Alloc           Free           Active\n");
240    printf ("\t           count   bytes   count   bytes   count   bytes\n");
241    for (i = 0; i < FC_MEM_NUM; i++)
242	printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
243		FcInUse[i].name,
244		FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
245		FcInUse[i].free_count, FcInUse[i].free_mem,
246		FcInUse[i].alloc_count - FcInUse[i].free_count,
247		FcInUse[i].alloc_mem - FcInUse[i].free_mem);
248    printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
249	    "Total",
250	    FcAllocCount, FcAllocMem,
251	    FcFreeCount, FcFreeMem,
252	    FcAllocCount - FcFreeCount,
253	    FcAllocMem - FcFreeMem);
254    FcAllocNotify = 0;
255    FcFreeNotify = 0;
256}
257
258void
259FcMemAlloc (int kind, int size)
260{
261    if (FcDebug() & FC_DBG_MEMORY)
262    {
263	FcInUse[kind].alloc_count++;
264	FcInUse[kind].alloc_mem += size;
265	FcAllocCount++;
266	FcAllocMem += size;
267	FcAllocNotify += size;
268	if (FcAllocNotify > FcMemNotice)
269	    FcMemReport ();
270    }
271}
272
273void
274FcMemFree (int kind, int size)
275{
276    if (FcDebug() & FC_DBG_MEMORY)
277    {
278	FcInUse[kind].free_count++;
279	FcInUse[kind].free_mem += size;
280	FcFreeCount++;
281	FcFreeMem += size;
282	FcFreeNotify += size;
283	if (FcFreeNotify > FcMemNotice)
284	    FcMemReport ();
285    }
286}
287#define __fcinit__
288#include "fcaliastail.h"
289#undef __fcinit__
290