]> git.wh0rd.org Git - fontconfig.git/blob - src/fcinit.c
FcInit should return FcFalse when FcInitLoadConfigAndFonts fails. (bug 10976)
[fontconfig.git] / src / fcinit.c
1 /*
2  * $RCSId: xc/lib/fontconfig/src/fcinit.c,v 1.7 2002/08/22 07:36:44 keithp Exp $
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  * 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
25 #include "fcint.h"
26 #include <stdlib.h>
27
28 static FcConfig *
29 FcInitFallbackConfig (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
42 bail1:
43     FcConfigDestroy (config);
44 bail0:
45     return 0;
46 }
47
48 int
49 FcGetVersion (void)
50 {
51     return FC_VERSION;
52 }
53
54 /*
55  * Load the configuration files
56  */
57 FcConfig *
58 FcInitLoadConfig (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  */
98 FcConfig *
99 FcInitLoadConfigAndFonts (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  */
117 FcBool
118 FcInit (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  */
136 void
137 FcFini (void)
138 {
139     if (_fcConfig)
140         FcConfigDestroy (_fcConfig);
141
142     FcPatternFini ();
143     FcCacheFini ();
144 }
145
146 /*
147  * Reread the configuration and available font lists
148  */
149 FcBool
150 FcInitReinitialize (void)
151 {
152     FcConfig    *config;
153
154     config = FcInitLoadConfigAndFonts ();
155     if (!config)
156         return FcFalse;
157     FcConfigSetCurrent (config);
158     return FcTrue;
159 }
160
161 FcBool
162 FcInitBringUptoDate (void)
163 {
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 ();
184 }
185
186 static struct {
187     char    name[16];
188     int     alloc_count;
189     int     alloc_mem;
190     int     free_count;
191     int     free_mem;
192 } FcInUse[FC_MEM_NUM] = {
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" },
222     { "staticstr" },
223 };
224
225 static int  FcAllocCount, FcAllocMem;
226 static int  FcFreeCount, FcFreeMem;
227
228 static int  FcMemNotice = 1*1024*1024;
229
230 static int  FcAllocNotify, FcFreeNotify;
231
232 void
233 FcMemReport (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++)
240         printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
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);
246     printf ("%16.16s%8d%8d%8d%8d%8d%8d\n",
247             "Total",
248             FcAllocCount, FcAllocMem,
249             FcFreeCount, FcFreeMem,
250             FcAllocCount - FcFreeCount,
251             FcAllocMem - FcFreeMem);
252     FcAllocNotify = 0;
253     FcFreeNotify = 0;
254 }
255
256 void
257 FcMemAlloc (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
271 void
272 FcMemFree (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 }
285 #define __fcinit__
286 #include "fcaliastail.h"
287 #undef __fcinit__