]> git.wh0rd.org - fontconfig.git/blame - src/fcinit.c
Bug 44826 - <alias> must contain only a single <family>
[fontconfig.git] / src / fcinit.c
CommitLineData
24330d27 1/*
317b8492 2 * fontconfig/src/fcinit.c
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
5aaf466d 10 * documentation, and that the name of the author(s) not be used in
24330d27 11 * advertising or publicity pertaining to distribution of the software without
5aaf466d 12 * specific, written prior permission. The authors make no
24330d27
KP
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
3074a73b 16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24330d27 17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24330d27
KP
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;
594dcef0 61
b97a34b5 62 FcInitDebug ();
24330d27
KP
63 config = FcConfigCreate ();
64 if (!config)
1e7a2a4f 65 return NULL;
594dcef0 66
24330d27
KP
67 if (!FcConfigParseAndLoad (config, 0, FcTrue))
68 {
69 FcConfigDestroy (config);
70 return FcInitFallbackConfig ();
71 }
594dcef0 72
64d7e303
KP
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 ();
13a14cbf
KP
144 if (FcDebug() & FC_DBG_MEMORY)
145 FcMemReport ();
34cd0514
CW
146}
147
179c3995
KP
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;
24330d27
KP
159 FcConfigSetCurrent (config);
160 return FcTrue;
161}
162
163FcBool
179c3995 164FcInitBringUptoDate (void)
24330d27 165{
179c3995
KP
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 ();
24330d27
KP
186}
187
188static struct {
67accef4 189 char name[16];
24330d27
KP
190 int alloc_count;
191 int alloc_mem;
192 int free_count;
193 int free_mem;
194} FcInUse[FC_MEM_NUM] = {
9dac3c59
KP
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" },
1c52c0f0 224 { "staticstr" },
24330d27
KP
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++)
1c52c0f0 242 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
24330d27
KP
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);
1c52c0f0 248 printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
24330d27
KP
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}
23816bf9
KP
287#define __fcinit__
288#include "fcaliastail.h"
289#undef __fcinit__