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