]> git.wh0rd.org - fontconfig.git/blame - src/fcdir.c
Bug 44826 - <alias> must contain only a single <family>
[fontconfig.git] / src / fcdir.c
CommitLineData
24330d27 1/*
317b8492 2 * fontconfig/src/fcdir.c
24330d27 3 *
46b51147 4 * Copyright © 2000 Keith Packard
24330d27
KP
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
5aaf466d 10 * documentation, and that the name of the author(s) not be used in
24330d27 11 * advertising or publicity pertaining to distribution of the software without
5aaf466d 12 * specific, written prior permission. The authors make no
24330d27
KP
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
3074a73b 16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24330d27 17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
3074a73b 18 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
24330d27
KP
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
24330d27 25#include "fcint.h"
179c3995
KP
26#include <dirent.h>
27
2d9c79c0 28FcBool
179c3995
KP
29FcFileIsDir (const FcChar8 *file)
30{
31 struct stat statb;
24330d27 32
c21fb9ac 33 if (FcStat (file, &statb) != 0)
179c3995
KP
34 return FcFalse;
35 return S_ISDIR(statb.st_mode);
36}
24330d27 37
00f059e9
KP
38static FcBool
39FcFileScanFontConfig (FcFontSet *set,
40 FcBlanks *blanks,
41 const FcChar8 *file,
42 FcConfig *config)
24330d27 43{
00f059e9
KP
44 FcPattern *font;
45 FcBool ret = FcTrue;
46 int id;
47 int count = 0;
594dcef0 48
24330d27
KP
49 id = 0;
50 do
51 {
327a7fd4
KP
52 font = 0;
53 /*
eb0cf671 54 * Nothing in the cache, scan the file
327a7fd4 55 */
eb0cf671 56 if (FcDebug () & FC_DBG_SCAN)
24330d27 57 {
eb0cf671
PL
58 printf ("\tScanning file %s...", file);
59 fflush (stdout);
24330d27 60 }
eb0cf671
PL
61 font = FcFreeTypeQuery (file, id, blanks, &count);
62 if (FcDebug () & FC_DBG_SCAN)
63 printf ("done\n");
c2c6976d
KP
64
65 /*
66 * Edit pattern with user-defined rules
67 */
0fcf866d 68 if (font && config && !FcConfigSubstitute (config, font, FcMatchScan))
c2c6976d
KP
69 {
70 FcPatternDestroy (font);
71 font = NULL;
72 ret = FcFalse;
73 }
74
327a7fd4
KP
75 /*
76 * Add the font
77 */
0fcf866d 78 if (font)
24330d27 79 {
c2c6976d
KP
80 if (FcDebug() & FC_DBG_SCANV)
81 {
82 printf ("Final font pattern:\n");
83 FcPatternPrint (font);
84 }
24330d27
KP
85 if (!FcFontSetAdd (set, font))
86 {
87 FcPatternDestroy (font);
c2c6976d 88 font = NULL;
24330d27
KP
89 ret = FcFalse;
90 }
91 }
47b49bf1
KP
92 else if (font)
93 FcPatternDestroy (font);
24330d27
KP
94 id++;
95 } while (font && ret && id < count);
96 return ret;
97}
98
00f059e9
KP
99FcBool
100FcFileScanConfig (FcFontSet *set,
101 FcStrSet *dirs,
102 FcBlanks *blanks,
103 const FcChar8 *file,
00f059e9
KP
104 FcConfig *config)
105{
00f059e9
KP
106 if (FcFileIsDir (file))
107 return FcStrSetAdd (dirs, file);
108 else
109 return FcFileScanFontConfig (set, blanks, file, config);
110}
111
d47c9d6e
KP
112FcBool
113FcFileScan (FcFontSet *set,
114 FcStrSet *dirs,
00f059e9 115 FcFileCache *cache, /* XXX unused */
d47c9d6e
KP
116 FcBlanks *blanks,
117 const FcChar8 *file,
118 FcBool force)
119{
5e544b32 120 return FcFileScanConfig (set, dirs, blanks, file, FcConfigGetCurrent ());
d47c9d6e
KP
121}
122
e3c6d336
PL
123/*
124 * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
125 */
e3c6d336
PL
126static int
127cmpstringp(const void *p1, const void *p2)
128{
129 return strcmp(* (char **) p1, * (char **) p2);
130}
131
4074fd25
BE
132FcBool
133FcDirScanConfig (FcFontSet *set,
134 FcStrSet *dirs,
135 FcBlanks *blanks,
136 const FcChar8 *dir,
137 FcBool force, /* XXX unused */
138 FcConfig *config)
24330d27 139{
327a7fd4
KP
140 DIR *d;
141 struct dirent *e;
bc5e487f 142 FcStrSet *files;
327a7fd4
KP
143 FcChar8 *file;
144 FcChar8 *base;
145 FcBool ret = FcTrue;
eb0cf671 146 int i;
24330d27 147
4074fd25
BE
148 if (!force)
149 return FcFalse;
150
151 if (!set && !dirs)
152 return FcTrue;
153
154 if (!blanks)
155 blanks = FcConfigGetBlanks (config);
00f059e9 156
9dac3c59 157 /* freed below */
bc5e487f 158 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
0d9e31c8
KP
159 if (!file) {
160 ret = FcFalse;
161 goto bail;
162 }
24330d27 163
bc5e487f 164 strcpy ((char *) file, (char *) dir);
ccb3e93b
KP
165 strcat ((char *) file, "/");
166 base = file + strlen ((char *) file);
594dcef0 167
a8386abc 168 if (FcDebug () & FC_DBG_SCAN)
bc5e487f 169 printf ("\tScanning dir %s\n", dir);
a8386abc 170
bc5e487f 171 d = opendir ((char *) dir);
24330d27
KP
172 if (!d)
173 {
179c3995 174 /* Don't complain about missing directories */
4074fd25 175 if (errno != ENOENT)
0d9e31c8 176 ret = FcFalse;
4074fd25 177 goto bail;
eb0cf671
PL
178 }
179
bc5e487f
KP
180 files = FcStrSetCreate ();
181 if (!files)
65448e8b
PL
182 {
183 ret = FcFalse;
184 goto bail1;
185 }
e3c6d336 186 while ((e = readdir (d)))
24330d27 187 {
179c3995 188 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
24330d27 189 {
00f059e9 190 strcpy ((char *) base, (char *) e->d_name);
bc5e487f
KP
191 if (!FcStrSetAdd (files, file)) {
192 ret = FcFalse;
193 goto bail2;
65448e8b 194 }
24330d27
KP
195 }
196 }
bc5e487f 197
00f059e9 198 /*
bc5e487f 199 * Sort files to make things prettier
00f059e9 200 */
bc5e487f 201 qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
4074fd25 202
af180c40 203 /*
bc5e487f 204 * Scan file files to build font patterns
af180c40 205 */
bc5e487f
KP
206 for (i = 0; i < files->num; i++)
207 FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
594dcef0 208
4074fd25
BE
209bail2:
210 FcStrSetDestroy (files);
211bail1:
212 closedir (d);
213bail:
214 return ret;
215}
216
217FcBool
218FcDirScan (FcFontSet *set,
219 FcStrSet *dirs,
220 FcFileCache *cache, /* XXX unused */
221 FcBlanks *blanks,
222 const FcChar8 *dir,
223 FcBool force /* XXX unused */)
224{
225 if (cache || !force)
226 return FcFalse;
227
5e544b32 228 return FcDirScanConfig (set, dirs, blanks, dir, force, FcConfigGetCurrent ());
4074fd25
BE
229}
230
231/*
232 * Scan the specified directory and construct a cache of its contents
233 */
234FcCache *
235FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
236{
237 FcStrSet *dirs;
238 FcBool ret = FcTrue;
239 FcFontSet *set;
240 FcCache *cache = NULL;
241 struct stat dir_stat;
242
243 if (FcDebug () & FC_DBG_FONTSET)
244 printf ("cache scan dir %s\n", dir);
245
c21fb9ac 246 if (FcStat (dir, &dir_stat) < 0)
4074fd25
BE
247 {
248 if (errno != ENOENT)
249 ret = FcFalse;
250 goto bail;
251 }
252
253 set = FcFontSetCreate();
254 if (!set)
255 {
256 ret = FcFalse;
257 goto bail;
258 }
259
260 dirs = FcStrSetCreate ();
261 if (!dirs)
262 {
263 ret = FcFalse;
264 goto bail1;
265 }
266
267 /*
268 * Scan the dir
269 */
270 if (!FcDirScanConfig (set, dirs, NULL, dir, FcTrue, config))
271 {
272 ret = FcFalse;
273 goto bail2;
274 }
275
00f059e9 276 /*
bc5e487f 277 * Build the cache object
00f059e9 278 */
db6f19f1 279 cache = FcDirCacheBuild (set, dir, &dir_stat, dirs);
bc5e487f 280 if (!cache)
4074fd25
BE
281 {
282 ret = FcFalse;
283 goto bail2;
284 }
594dcef0 285
00f059e9 286 /*
bc5e487f 287 * Write out the cache file, ignoring any troubles
00f059e9 288 */
bc5e487f 289 FcDirCacheWrite (cache, config);
594dcef0 290
65448e8b 291 bail2:
4074fd25 292 FcStrSetDestroy (dirs);
65448e8b 293 bail1:
bc5e487f 294 FcFontSetDestroy (set);
0d9e31c8 295 bail:
bc5e487f
KP
296 return cache;
297}
298
299/*
300 * Read (or construct) the cache for a directory
301 */
302FcCache *
303FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
304{
305 FcCache *cache = NULL;
bc5e487f 306
bc5e487f
KP
307 /* Try to use existing cache file */
308 if (!force)
9b511b29 309 cache = FcDirCacheLoad (dir, config, NULL);
594dcef0 310
bc5e487f
KP
311 /* Not using existing cache file, construct new cache */
312 if (!cache)
9b511b29 313 cache = FcDirCacheScan (dir, config);
594dcef0 314
bc5e487f
KP
315 return cache;
316}
317
24330d27 318FcBool
2304e38f 319FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
24330d27 320{
bc5e487f 321 return FcFalse; /* XXX deprecated */
24330d27 322}
23816bf9
KP
323#define __fcdir__
324#include "fcaliastail.h"
325#undef __fcdir__