Debian Codegraph @

src/fcinit.c

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