]> git.wh0rd.org Git - fontconfig.git/blob - src/fcinit.c
Reimplement FC_LANG as FcTypeLang, freeze patterns, other cleanup
[fontconfig.git] / src / fcinit.c
1 /*
2  * $XFree86: xc/lib/fontconfig/src/fcinit.c,v 1.6 2002/05/23 23:00:46 keithp Exp $
3  *
4  * Copyright © 2001 Keith Packard, member of The XFree86 Project, Inc.
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 <stdlib.h>
26 #include "fcint.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     return config;
39
40 bail1:
41     FcConfigDestroy (config);
42 bail0:
43     return 0;
44 }
45
46 int
47 FcGetVersion (void)
48 {
49     return FC_VERSION;
50 }
51
52 /*
53  * Load the configuration files
54  */
55 FcConfig *
56 FcInitLoadConfig (void)
57 {
58     FcConfig    *config;
59     
60     config = FcConfigCreate ();
61     if (!config)
62         return FcFalse;
63     
64     if (!FcConfigParseAndLoad (config, 0, FcTrue))
65     {
66         FcConfigDestroy (config);
67         return FcInitFallbackConfig ();
68     }
69
70     return config;
71 }
72
73 /*
74  * Load the configuration files and scan for available fonts
75  */
76 FcConfig *
77 FcInitLoadConfigAndFonts (void)
78 {
79     FcConfig    *config = FcInitLoadConfig ();
80
81     if (!config)
82         return 0;
83     if (!FcConfigBuildFonts (config))
84     {
85         FcConfigDestroy (config);
86         return 0;
87     }
88     return config;
89 }
90
91 /*
92  * Initialize the default library configuration
93  */
94 FcBool
95 FcInit (void)
96 {
97     FcConfig    *config;
98
99     if (_fcConfig)
100         return FcTrue;
101     config = FcInitLoadConfigAndFonts ();
102     if (!config)
103         return FcTrue;
104     FcConfigSetCurrent (config);
105     if (FcDebug() & FC_DBG_MEMORY)
106         FcMemReport ();
107     return FcTrue;
108 }
109
110 /*
111  * Reread the configuration and available font lists
112  */
113 FcBool
114 FcInitReinitialize (void)
115 {
116     FcConfig    *config;
117
118     config = FcInitLoadConfigAndFonts ();
119     if (!config)
120         return FcFalse;
121     FcConfigSetCurrent (config);
122     return FcTrue;
123 }
124
125 FcBool
126 FcInitBringUptoDate (void)
127 {
128     FcConfig    *config = FcConfigGetCurrent ();
129     time_t      now;
130
131     /*
132      * rescanInterval == 0 disables automatic up to date
133      */
134     if (config->rescanInterval == 0)
135         return FcTrue;
136     /*
137      * Check no more often than rescanInterval seconds
138      */
139     now = time (0);
140     if (config->rescanTime + config->rescanInterval - now > 0)
141         return FcTrue;
142     /*
143      * If up to date, don't reload configuration
144      */
145     if (FcConfigUptoDate (0))
146         return FcTrue;
147     return FcInitReinitialize ();
148 }
149
150 static struct {
151     char    *name;
152     int     alloc_count;
153     int     alloc_mem;
154     int     free_count;
155     int     free_mem;
156 } FcInUse[FC_MEM_NUM] = {
157     { "charset", 0, 0 },
158     { "charleaf", 0 ,0 },
159     { "fontset", 0, 0 },
160     { "fontptr", 0, 0 },
161     { "objectset", 0, 0 },
162     { "objectptr", 0, 0 },
163     { "matrix", 0, 0 },
164     { "pattern", 0, 0 },
165     { "patelt", 0, 0 },
166     { "vallist", 0, 0 },
167     { "substate", 0, 0 },
168     { "string", 0, 0 },
169     { "listbuck", 0, 0 },
170     { "strset", 0, 0 },
171     { "strlist", 0, 0 },
172     { "config", 0, 0 },
173     { "langset", 0, 0 },
174 };
175
176 static int  FcAllocCount, FcAllocMem;
177 static int  FcFreeCount, FcFreeMem;
178
179 static int  FcMemNotice = 1*1024*1024;
180
181 static int  FcAllocNotify, FcFreeNotify;
182
183 void
184 FcValueListReport (void);
185
186 void
187 FcMemReport (void)
188 {
189     int i;
190     printf ("Fc Memory Usage:\n");
191     printf ("\t   Which       Alloc           Free           Active\n");
192     printf ("\t           count   bytes   count   bytes   count   bytes\n");
193     for (i = 0; i < FC_MEM_NUM; i++)
194         printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
195                 FcInUse[i].name,
196                 FcInUse[i].alloc_count, FcInUse[i].alloc_mem,
197                 FcInUse[i].free_count, FcInUse[i].free_mem,
198                 FcInUse[i].alloc_count - FcInUse[i].free_count,
199                 FcInUse[i].alloc_mem - FcInUse[i].free_mem);
200     printf ("\t%8.8s%8d%8d%8d%8d%8d%8d\n",
201             "Total",
202             FcAllocCount, FcAllocMem,
203             FcFreeCount, FcFreeMem,
204             FcAllocCount - FcFreeCount,
205             FcAllocMem - FcFreeMem);
206     FcAllocNotify = 0;
207     FcFreeNotify = 0;
208     FcValueListReport ();
209 }
210
211 void
212 FcMemAlloc (int kind, int size)
213 {
214     if (FcDebug() & FC_DBG_MEMORY)
215     {
216         FcInUse[kind].alloc_count++;
217         FcInUse[kind].alloc_mem += size;
218         FcAllocCount++;
219         FcAllocMem += size;
220         FcAllocNotify += size;
221         if (FcAllocNotify > FcMemNotice)
222             FcMemReport ();
223     }
224 }
225
226 void
227 FcMemFree (int kind, int size)
228 {
229     if (FcDebug() & FC_DBG_MEMORY)
230     {
231         FcInUse[kind].free_count++;
232         FcInUse[kind].free_mem += size;
233         FcFreeCount++;
234         FcFreeMem += size;
235         FcFreeNotify += size;
236         if (FcFreeNotify > FcMemNotice)
237             FcMemReport ();
238     }
239 }