Merge branch 'upstream' into tizen
[platform/upstream/fontconfig.git] / src / fcinit.c
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 the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make 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
28 #if defined(FC_ATOMIC_INT_NIL)
29 #pragma message("Could not find any system to define atomic_int macros, library may NOT be thread-safe.")
30 #endif
31 #if defined(FC_MUTEX_IMPL_NIL)
32 #pragma message("Could not find any system to define mutex macros, library may NOT be thread-safe.")
33 #endif
34 #if defined(FC_ATOMIC_INT_NIL) || defined(FC_MUTEX_IMPL_NIL)
35 #pragma message("To suppress these warnings, define FC_NO_MT.")
36 #endif
37
38 static FcConfig *
39 FcInitFallbackConfig (const FcChar8 *sysroot)
40 {
41     FcConfig    *config;
42
43     config = FcConfigCreate ();
44     if (!config)
45         goto bail0;
46     FcConfigSetSysRoot (config, sysroot);
47     if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
48         goto bail1;
49     if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
50         goto bail1;
51     return config;
52
53 bail1:
54     FcConfigDestroy (config);
55 bail0:
56     return 0;
57 }
58
59 int
60 FcGetVersion (void)
61 {
62     return FC_VERSION;
63 }
64
65 /*
66  * Load the configuration files
67  */
68 FcConfig *
69 FcInitLoadOwnConfig (FcConfig *config)
70 {
71     if (!config)
72     {
73         config = FcConfigCreate ();
74         if (!config)
75             return NULL;
76     }
77
78     FcInitDebug ();
79
80     if (!FcConfigParseAndLoad (config, 0, FcTrue))
81     {
82         const FcChar8 *sysroot = FcConfigGetSysRoot (config);
83         FcConfig *fallback = FcInitFallbackConfig (sysroot);
84
85         FcConfigDestroy (config);
86
87         return fallback;
88     }
89
90     if (config->cacheDirs && config->cacheDirs->num == 0)
91     {
92         FcChar8 *prefix, *p;
93         size_t plen;
94
95         fprintf (stderr,
96                  "Fontconfig warning: no <cachedir> elements found. Check configuration.\n");
97         fprintf (stderr,
98                  "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
99                  FC_CACHEDIR);
100         prefix = FcConfigXdgCacheHome ();
101         if (!prefix)
102             goto bail;
103         plen = strlen ((const char *)prefix);
104         p = realloc (prefix, plen + 12);
105         if (!p)
106             goto bail;
107         prefix = p;
108         memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11);
109         prefix[plen + 11] = 0;
110         fprintf (stderr,
111                  "Fontconfig warning: adding <cachedir prefix=\"xdg\">fontconfig</cachedir>\n");
112
113         if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR) ||
114             !FcConfigAddCacheDir (config, (FcChar8 *) prefix))
115         {
116             FcConfig *fallback;
117             const FcChar8 *sysroot;
118
119           bail:
120             sysroot = FcConfigGetSysRoot (config);
121             fprintf (stderr,
122                      "Fontconfig error: out of memory");
123             if (prefix)
124                 FcStrFree (prefix);
125             fallback = FcInitFallbackConfig (sysroot);
126             FcConfigDestroy (config);
127
128             return fallback;
129         }
130         FcStrFree (prefix);
131     }
132
133     return config;
134 }
135
136 FcConfig *
137 FcInitLoadConfig (void)
138 {
139     return FcInitLoadOwnConfig (NULL);
140 }
141
142 /*
143  * Load the configuration files and scan for available fonts
144  */
145 FcConfig *
146 FcInitLoadOwnConfigAndFonts (FcConfig *config)
147 {
148     config = FcInitLoadOwnConfig (config);
149     if (!config)
150         return 0;
151     if (!FcConfigBuildFonts (config))
152     {
153         FcConfigDestroy (config);
154         return 0;
155     }
156     return config;
157 }
158
159 FcConfig *
160 FcInitLoadConfigAndFonts (void)
161 {
162     return FcInitLoadOwnConfigAndFonts (NULL);
163 }
164
165 /*
166  * Initialize the default library configuration
167  */
168 FcBool
169 FcInit (void)
170 {
171     return FcConfigInit ();
172 }
173
174 /*
175  * Free all library-allocated data structures.
176  */
177 void
178 FcFini (void)
179 {
180     FcConfigFini ();
181     FcCacheFini ();
182     FcDefaultFini ();
183 }
184
185 /*
186  * Reread the configuration and available font lists
187  */
188 FcBool
189 FcInitReinitialize (void)
190 {
191     FcConfig    *config;
192     FcBool      ret;
193
194     config = FcInitLoadConfigAndFonts ();
195     if (!config)
196         return FcFalse;
197     ret = FcConfigSetCurrent (config);
198     /* FcConfigSetCurrent() increases the refcount.
199      * decrease it here to avoid the memory leak.
200      */
201     FcConfigDestroy (config);
202
203     return ret;
204 }
205
206 FcBool
207 FcInitBringUptoDate (void)
208 {
209     FcConfig    *config = FcConfigGetCurrent ();
210     time_t      now;
211
212     if (!config)
213         return FcFalse;
214     /*
215      * rescanInterval == 0 disables automatic up to date
216      */
217     if (config->rescanInterval == 0)
218         return FcTrue;
219     /*
220      * Check no more often than rescanInterval seconds
221      */
222     now = time (0);
223     if (config->rescanTime + config->rescanInterval - now > 0)
224         return FcTrue;
225     /*
226      * If up to date, don't reload configuration
227      */
228     if (FcConfigUptoDate (0))
229         return FcTrue;
230     return FcInitReinitialize ();
231 }
232
233 #define __fcinit__
234 #include "fcaliastail.h"
235 #undef __fcinit__