]>
Commit | Line | Data |
---|---|---|
24330d27 | 1 | /* |
4bd4418a | 2 | * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $ |
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 | |
10 | * documentation, and that the name of Keith Packard not be used in | |
11 | * advertising or publicity pertaining to distribution of the software without | |
12 | * specific, written prior permission. Keith Packard makes 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 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
18 | * EVENT SHALL KEITH PACKARD 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 | ||
24330d27 | 25 | #include "fcint.h" |
179c3995 KP |
26 | #include <dirent.h> |
27 | ||
2d9c79c0 | 28 | FcBool |
179c3995 KP |
29 | FcFileIsDir (const FcChar8 *file) |
30 | { | |
31 | struct stat statb; | |
24330d27 | 32 | |
179c3995 KP |
33 | if (stat ((const char *) file, &statb) != 0) |
34 | return FcFalse; | |
35 | return S_ISDIR(statb.st_mode); | |
36 | } | |
24330d27 | 37 | |
00f059e9 KP |
38 | static FcBool |
39 | FcFileScanFontConfig (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; | |
24330d27 KP |
48 | |
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"); | |
327a7fd4 KP |
64 | /* |
65 | * Add the font | |
66 | */ | |
4f27c1c0 | 67 | if (font && (!config || FcConfigAcceptFont (config, font))) |
24330d27 KP |
68 | { |
69 | if (!FcFontSetAdd (set, font)) | |
70 | { | |
71 | FcPatternDestroy (font); | |
72 | font = 0; | |
73 | ret = FcFalse; | |
74 | } | |
75 | } | |
47b49bf1 KP |
76 | else if (font) |
77 | FcPatternDestroy (font); | |
24330d27 KP |
78 | id++; |
79 | } while (font && ret && id < count); | |
80 | return ret; | |
81 | } | |
82 | ||
00f059e9 KP |
83 | FcBool |
84 | FcFileScanConfig (FcFontSet *set, | |
85 | FcStrSet *dirs, | |
86 | FcBlanks *blanks, | |
87 | const FcChar8 *file, | |
00f059e9 KP |
88 | FcConfig *config) |
89 | { | |
00f059e9 KP |
90 | if (FcFileIsDir (file)) |
91 | return FcStrSetAdd (dirs, file); | |
92 | else | |
93 | return FcFileScanFontConfig (set, blanks, file, config); | |
94 | } | |
95 | ||
d47c9d6e KP |
96 | FcBool |
97 | FcFileScan (FcFontSet *set, | |
98 | FcStrSet *dirs, | |
00f059e9 | 99 | FcFileCache *cache, /* XXX unused */ |
d47c9d6e KP |
100 | FcBlanks *blanks, |
101 | const FcChar8 *file, | |
102 | FcBool force) | |
103 | { | |
bc5e487f | 104 | return FcFileScanConfig (set, dirs, blanks, file, NULL); |
d47c9d6e KP |
105 | } |
106 | ||
e3c6d336 PL |
107 | /* |
108 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
109 | */ | |
e3c6d336 PL |
110 | static int |
111 | cmpstringp(const void *p1, const void *p2) | |
112 | { | |
113 | return strcmp(* (char **) p1, * (char **) p2); | |
114 | } | |
115 | ||
c8d5753c | 116 | /* |
bc5e487f | 117 | * Scan the specified directory and construct a cache of its contents |
c8d5753c | 118 | */ |
bc5e487f KP |
119 | FcCache * |
120 | FcDirCacheScan (const FcChar8 *dir, FcConfig *config) | |
24330d27 | 121 | { |
327a7fd4 KP |
122 | DIR *d; |
123 | struct dirent *e; | |
bc5e487f KP |
124 | FcStrSet *files; |
125 | FcStrSet *dirs; | |
327a7fd4 KP |
126 | FcChar8 *file; |
127 | FcChar8 *base; | |
128 | FcBool ret = FcTrue; | |
bc5e487f | 129 | FcFontSet *set; |
eb0cf671 | 130 | int i; |
bc5e487f KP |
131 | FcBlanks *blanks = FcConfigGetBlanks (config); |
132 | FcCache *cache = NULL; | |
24330d27 | 133 | |
00f059e9 | 134 | if (FcDebug () & FC_DBG_FONTSET) |
bc5e487f | 135 | printf ("cache scan dir %s\n", dir); |
00f059e9 | 136 | |
9dac3c59 | 137 | /* freed below */ |
bc5e487f | 138 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
0d9e31c8 KP |
139 | if (!file) { |
140 | ret = FcFalse; | |
141 | goto bail; | |
142 | } | |
24330d27 | 143 | |
bc5e487f | 144 | strcpy ((char *) file, (char *) dir); |
ccb3e93b KP |
145 | strcat ((char *) file, "/"); |
146 | base = file + strlen ((char *) file); | |
24330d27 | 147 | |
a8386abc | 148 | if (FcDebug () & FC_DBG_SCAN) |
bc5e487f | 149 | printf ("\tScanning dir %s\n", dir); |
a8386abc | 150 | |
bc5e487f | 151 | d = opendir ((char *) dir); |
24330d27 KP |
152 | if (!d) |
153 | { | |
179c3995 KP |
154 | /* Don't complain about missing directories */ |
155 | if (errno == ENOENT) | |
0d9e31c8 KP |
156 | ret = FcTrue; |
157 | else | |
158 | ret = FcFalse; | |
159 | goto bail_1; | |
24330d27 | 160 | } |
eb0cf671 | 161 | |
bc5e487f KP |
162 | set = FcFontSetCreate(); |
163 | if (!set) | |
65448e8b PL |
164 | { |
165 | ret = FcFalse; | |
166 | goto bail0; | |
eb0cf671 PL |
167 | } |
168 | ||
bc5e487f KP |
169 | files = FcStrSetCreate (); |
170 | if (!files) | |
65448e8b PL |
171 | { |
172 | ret = FcFalse; | |
173 | goto bail1; | |
174 | } | |
e3c6d336 | 175 | while ((e = readdir (d))) |
24330d27 | 176 | { |
179c3995 | 177 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 178 | { |
00f059e9 | 179 | strcpy ((char *) base, (char *) e->d_name); |
bc5e487f KP |
180 | if (!FcStrSetAdd (files, file)) { |
181 | ret = FcFalse; | |
182 | goto bail2; | |
65448e8b | 183 | } |
24330d27 KP |
184 | } |
185 | } | |
bc5e487f | 186 | |
00f059e9 | 187 | /* |
bc5e487f | 188 | * Sort files to make things prettier |
00f059e9 | 189 | */ |
bc5e487f | 190 | qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp); |
00f059e9 | 191 | |
bc5e487f KP |
192 | dirs = FcStrSetCreate (); |
193 | if (!dirs) | |
194 | goto bail2; | |
00f059e9 | 195 | |
af180c40 | 196 | /* |
bc5e487f | 197 | * Scan file files to build font patterns |
af180c40 | 198 | */ |
bc5e487f KP |
199 | for (i = 0; i < files->num; i++) |
200 | FcFileScanConfig (set, dirs, blanks, files->strs[i], config); | |
201 | ||
00f059e9 | 202 | /* |
bc5e487f | 203 | * Build the cache object |
00f059e9 | 204 | */ |
bc5e487f KP |
205 | cache = FcDirCacheBuild (set, dir, dirs); |
206 | if (!cache) | |
207 | goto bail3; | |
208 | ||
00f059e9 | 209 | /* |
bc5e487f | 210 | * Write out the cache file, ignoring any troubles |
00f059e9 | 211 | */ |
bc5e487f | 212 | FcDirCacheWrite (cache, config); |
00f059e9 KP |
213 | |
214 | bail3: | |
bc5e487f | 215 | FcStrSetDestroy (dirs); |
65448e8b | 216 | bail2: |
bc5e487f | 217 | FcStrSetDestroy (files); |
65448e8b | 218 | bail1: |
bc5e487f | 219 | FcFontSetDestroy (set); |
65448e8b PL |
220 | |
221 | bail0: | |
222 | closedir (d); | |
223 | ||
0d9e31c8 | 224 | bail_1: |
65448e8b | 225 | free (file); |
0d9e31c8 | 226 | bail: |
bc5e487f KP |
227 | return cache; |
228 | } | |
229 | ||
230 | /* | |
231 | * Read (or construct) the cache for a directory | |
232 | */ | |
233 | FcCache * | |
234 | FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config) | |
235 | { | |
236 | FcCache *cache = NULL; | |
bc5e487f | 237 | |
9b511b29 KP |
238 | if (config && !FcConfigAcceptFilename (config, dir)) |
239 | return NULL; | |
bc5e487f KP |
240 | |
241 | /* Try to use existing cache file */ | |
242 | if (!force) | |
9b511b29 | 243 | cache = FcDirCacheLoad (dir, config, NULL); |
bc5e487f KP |
244 | |
245 | /* Not using existing cache file, construct new cache */ | |
246 | if (!cache) | |
9b511b29 | 247 | cache = FcDirCacheScan (dir, config); |
bc5e487f KP |
248 | |
249 | return cache; | |
250 | } | |
251 | ||
252 | FcBool | |
253 | FcDirScanConfig (FcFontSet *set, | |
254 | FcStrSet *dirs, | |
255 | FcBlanks *blanks, | |
256 | const FcChar8 *dir, | |
257 | FcBool force, | |
258 | FcConfig *config) | |
259 | { | |
260 | return FcFalse; /* XXX fixme */ | |
24330d27 KP |
261 | } |
262 | ||
d47c9d6e KP |
263 | FcBool |
264 | FcDirScan (FcFontSet *set, | |
265 | FcStrSet *dirs, | |
00f059e9 | 266 | FcFileCache *cache, /* XXX unused */ |
d47c9d6e KP |
267 | FcBlanks *blanks, |
268 | const FcChar8 *dir, | |
269 | FcBool force) | |
270 | { | |
bc5e487f | 271 | return FcDirScanConfig (set, dirs, blanks, dir, force, NULL); |
d47c9d6e KP |
272 | } |
273 | ||
24330d27 | 274 | FcBool |
2304e38f | 275 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) |
24330d27 | 276 | { |
bc5e487f | 277 | return FcFalse; /* XXX deprecated */ |
24330d27 | 278 | } |