Imported Upstream version 2.10.91
[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 (void)
40 {
41     FcConfig    *config;
42
43     config = FcConfigCreate ();
44     if (!config)
45         goto bail0;
46     if (!FcConfigAddDir (config, (FcChar8 *) FC_DEFAULT_FONTS))
47         goto bail1;
48     if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR))
49         goto bail1;
50     return config;
51
52 bail1:
53     FcConfigDestroy (config);
54 bail0:
55     return 0;
56 }
57
58 int
59 FcGetVersion (void)
60 {
61     return FC_VERSION;
62 }
63
64 /*
65  * Load the configuration files
66  */
67 FcConfig *
68 FcInitLoadConfig (void)
69 {
70     FcConfig    *config;
71
72     FcInitDebug ();
73     config = FcConfigCreate ();
74     if (!config)
75         return NULL;
76
77     if (!FcConfigParseAndLoad (config, 0, FcTrue))
78     {
79         FcConfigDestroy (config);
80         return FcInitFallbackConfig ();
81     }
82
83     if (config->cacheDirs && config->cacheDirs->num == 0)
84     {
85         FcChar8 *prefix, *p;
86         size_t plen;
87
88         fprintf (stderr,
89                  "Fontconfig warning: no <cachedir> elements found. Check configuration.\n");
90         fprintf (stderr,
91                  "Fontconfig warning: adding <cachedir>%s</cachedir>\n",
92                  FC_CACHEDIR);
93         prefix = FcConfigXdgCacheHome ();
94         if (!prefix)
95             goto bail;
96         plen = strlen ((const char *)prefix);
97         p = realloc (prefix, plen + 12);
98         if (!p)
99             goto bail;
100         prefix = p;
101         memcpy (&prefix[plen], FC_DIR_SEPARATOR_S "fontconfig", 11);
102         prefix[plen + 11] = 0;
103         fprintf (stderr,
104                  "Fontconfig warning: adding <cachedir prefix=\"xdg\">fontconfig</cachedir>\n");
105
106         if (!FcConfigAddCacheDir (config, (FcChar8 *) FC_CACHEDIR) ||
107             !FcConfigAddCacheDir (config, (FcChar8 *) prefix))
108         {
109           bail:
110             fprintf (stderr,
111                      "Fontconfig error: out of memory");
112             if (prefix)
113                 FcStrFree (prefix);
114             FcConfigDestroy (config);
115             return FcInitFallbackConfig ();
116         }
117         FcStrFree (prefix);
118     }
119
120     return config;
121 }
122
123 /*
124  * Load the configuration files and scan for available fonts
125  */
126 FcConfig *
127 FcInitLoadConfigAndFonts (void)
128 {
129     FcConfig    *config = FcInitLoadConfig ();
130
131     FcInitDebug ();
132     if (!config)
133         return 0;
134     if (!FcConfigBuildFonts (config))
135     {
136         FcConfigDestroy (config);
137         return 0;
138     }
139     return config;
140 }
141
142 /*
143  * Initialize the default library configuration
144  */
145 FcBool
146 FcInit (void)
147 {
148     return FcConfigInit ();
149 }
150
151 /*
152  * Free all library-allocated data structures.
153  */
154 void
155 FcFini (void)
156 {
157     FcConfigFini ();
158     FcCacheFini ();
159     FcDefaultFini ();
160 }
161
162 /*
163  * Reread the configuration and available font lists
164  */
165 FcBool
166 FcInitReinitialize (void)
167 {
168     FcConfig    *config;
169
170     config = FcInitLoadConfigAndFonts ();
171     if (!config)
172         return FcFalse;
173     return FcConfigSetCurrent (config);
174 }
175
176 FcBool
177 FcInitBringUptoDate (void)
178 {
179     FcConfig    *config = FcConfigGetCurrent ();
180     time_t      now;
181
182     /*
183      * rescanInterval == 0 disables automatic up to date
184      */
185     if (config->rescanInterval == 0)
186         return FcTrue;
187     /*
188      * Check no more often than rescanInterval seconds
189      */
190     now = time (0);
191     if (config->rescanTime + config->rescanInterval - now > 0)
192         return FcTrue;
193     /*
194      * If up to date, don't reload configuration
195      */
196     if (FcConfigUptoDate (0))
197         return FcTrue;
198     return FcInitReinitialize ();
199 }
200
201 #define __fcinit__
202 #include "fcaliastail.h"
203 #undef __fcinit__