]> git.wh0rd.org - fontconfig.git/blame - src/fcinit.c
FcInit should return FcFalse when FcInitLoadConfigAndFonts fails. (bug 10976)
[fontconfig.git] / src / fcinit.c
CommitLineData
24330d27 1/*
4bd4418a 2 * $RCSId: xc/lib/fontconfig/src/fcinit.c,v 1.7 2002/08/22 07:36:44 keithp Exp $
24330d27 3 *
46b51147 4 * Copyright © 2001 Keith Packard
24330d27
KP
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 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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
24330d27 25#include "fcint.h"
f045376c 26#include <stdlib.h>
24330d27 27
179c3995 28static FcConfig *
24330d27
KP
29FcInitFallbackConfig (void)
30{
31 FcConfig *config;
32
33 config = FcConfigCreate ();
34 if (!config)
35 goto bail0;
446bb9c9 36 if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
24330d27 37 goto bail1;
7410e40b
PL
38 if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
39 goto bail1;
179c3995 40 return config;
24330d27
KP
41
42bail1:
43 FcConfigDestroy (config);
44bail0:
179c3995 45 return 0;
24330d27
KP
46}
47
48db40f6
KP
48int
49FcGetVersion (void)
50{
51 return FC_VERSION;
52}
53
24330d27 54/*
179c3995 55 * Load the configuration files
24330d27 56 */
179c3995
KP
57FcConfig *
58FcInitLoadConfig (void)
24330d27 59{
179c3995 60 FcConfig *config;
24330d27 61
b97a34b5 62 FcInitDebug ();
24330d27
KP
63 config = FcConfigCreate ();
64 if (!config)
65 return FcFalse;
66
67 if (!FcConfigParseAndLoad (config, 0, FcTrue))
68 {
69 FcConfigDestroy (config);
70 return FcInitFallbackConfig ();
71 }
64d7e303
KP
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 }
179c3995
KP
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
b97a34b5 103 FcInitDebug ();
179c3995
KP
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)
4657944d 126 return FcFalse;
179c3995 127 FcConfigSetCurrent (config);
d8d73958
KP
128 if (FcDebug() & FC_DBG_MEMORY)
129 FcMemReport ();
179c3995
KP
130 return FcTrue;
131}
132
34cd0514
CW
133/*
134 * Free all library-allocated data structures.
135 */
136void
137FcFini (void)
138{
139 if (_fcConfig)
140 FcConfigDestroy (_fcConfig);
141
e1b9d091 142 FcPatternFini ();
8fe2104a 143 FcCacheFini ();
34cd0514
CW
144}
145
179c3995
KP
146/*
147 * Reread the configuration and available font lists
148 */
149FcBool
150FcInitReinitialize (void)
151{
152 FcConfig *config;
153
154 config = FcInitLoadConfigAndFonts ();
155 if (!config)
156 return FcFalse;
24330d27
KP
157 FcConfigSetCurrent (config);
158 return FcTrue;
159}
160
161FcBool
179c3995 162FcInitBringUptoDate (void)
24330d27 163{
179c3995
KP
164 FcConfig *config = FcConfigGetCurrent ();
165 time_t now;
166
167 /*
168 * rescanInterval == 0 disables automatic up to date
169 */
170 if (config->rescanInterval == 0)
171 return FcTrue;
172 /*
173 * Check no more often than rescanInterval seconds
174 */
175 now = time (0);
176 if (config->rescanTime + config->rescanInterval - now > 0)
177 return FcTrue;
178 /*
179 * If up to date, don't reload configuration
180 */
181 if (FcConfigUptoDate (0))
182 return FcTrue;
183 return FcInitReinitialize ();
24330d27
KP
184}
185
186static struct {
67accef4 187 char name[16];
24330d27
KP
188 int alloc_count;
189 int alloc_mem;
190 int free_count;
191 int free_mem;
192} FcInUse[FC_MEM_NUM] = {
9dac3c59
KP
193 { "charset" },
194 { "charleaf" },
195 { "fontset" },
196 { "fontptr" },
197 { "objectset" },
198 { "objectptr" },
199 { "matrix" },
200 { "pattern" },
201 { "patelt" },
202 { "vallist" },
203 { "substate" },
204 { "string" },
205 { "listbuck" },
206 { "strset" },
207 { "strlist" },
208 { "config" },
209 { "langset" },
210 { "atomic" },
211 { "blanks" },
212 { "cache" },
213 { "strbuf" },
214 { "subst" },
215 { "objecttype" },
216 { "constant" },
217 { "test" },
218 { "expr" },
219 { "vstack" },
220 { "attr" },
221 { "pstack" },
1c52c0f0 222 { "staticstr" },
24330d27
KP
223};
224
225static int FcAllocCount, FcAllocMem;
226static int FcFreeCount, FcFreeMem;
227
228static int FcMemNotice = 1*1024*1024;
229
230static int FcAllocNotify, FcFreeNotify;
231
232void
233FcMemReport (void)
234{
235 int i;
236 printf ("Fc Memory Usage:\n");
237 printf ("\t Which Alloc Free Active\n");
238 printf ("\t count bytes count bytes count bytes\n");
239 for (i = 0; i < FC_MEM_NUM; i++)
1c52c0f0 240 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
24330d27
KP
241 FcInUse[i].name,
242 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
243 FcInUse[i].free_count, FcInUse[i].free_mem,
244 FcInUse[i].alloc_count - FcInUse[i].free_count,
245 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
1c52c0f0 246 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
24330d27
KP
247 "Total",
248 FcAllocCount, FcAllocMem,
249 FcFreeCount, FcFreeMem,
250 FcAllocCount - FcFreeCount,
251 FcAllocMem - FcFreeMem);
252 FcAllocNotify = 0;
253 FcFreeNotify = 0;
254}
255
256void
257FcMemAlloc (int kind, int size)
258{
259 if (FcDebug() & FC_DBG_MEMORY)
260 {
261 FcInUse[kind].alloc_count++;
262 FcInUse[kind].alloc_mem += size;
263 FcAllocCount++;
264 FcAllocMem += size;
265 FcAllocNotify += size;
266 if (FcAllocNotify > FcMemNotice)
267 FcMemReport ();
268 }
269}
270
271void
272FcMemFree (int kind, int size)
273{
274 if (FcDebug() & FC_DBG_MEMORY)
275 {
276 FcInUse[kind].free_count++;
277 FcInUse[kind].free_mem += size;
278 FcFreeCount++;
279 FcFreeMem += size;
280 FcFreeNotify += size;
281 if (FcFreeNotify > FcMemNotice)
282 FcMemReport ();
283 }
284}
23816bf9
KP
285#define __fcinit__
286#include "fcaliastail.h"
287#undef __fcinit__