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