]>
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, | |
88 | FcBool force, | |
89 | FcConfig *config) | |
90 | { | |
91 | if (config && !FcConfigAcceptFilename (config, file)) | |
92 | return FcTrue; | |
93 | ||
94 | if (FcFileIsDir (file)) | |
95 | return FcStrSetAdd (dirs, file); | |
96 | else | |
97 | return FcFileScanFontConfig (set, blanks, file, config); | |
98 | } | |
99 | ||
d47c9d6e KP |
100 | FcBool |
101 | FcFileScan (FcFontSet *set, | |
102 | FcStrSet *dirs, | |
00f059e9 | 103 | FcFileCache *cache, /* XXX unused */ |
d47c9d6e KP |
104 | FcBlanks *blanks, |
105 | const FcChar8 *file, | |
106 | FcBool force) | |
107 | { | |
00f059e9 | 108 | return FcFileScanConfig (set, dirs, blanks, file, force, 0); |
d47c9d6e KP |
109 | } |
110 | ||
e3c6d336 PL |
111 | /* |
112 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
113 | */ | |
114 | ||
115 | static int | |
116 | cmpstringp(const void *p1, const void *p2) | |
117 | { | |
118 | return strcmp(* (char **) p1, * (char **) p2); | |
119 | } | |
120 | ||
c8d5753c KP |
121 | /* |
122 | * Scan 'dir', adding font files to 'set' and | |
123 | * subdirectories to 'dirs' | |
124 | */ | |
125 | ||
24330d27 | 126 | FcBool |
d47c9d6e KP |
127 | FcDirScanConfig (FcFontSet *set, |
128 | FcStrSet *dirs, | |
d47c9d6e KP |
129 | FcBlanks *blanks, |
130 | const FcChar8 *dir, | |
131 | FcBool force, | |
132 | FcConfig *config) | |
24330d27 | 133 | { |
327a7fd4 KP |
134 | DIR *d; |
135 | struct dirent *e; | |
00f059e9 | 136 | FcStrSet *dirlist, *filelist; |
327a7fd4 KP |
137 | FcChar8 *file; |
138 | FcChar8 *base; | |
139 | FcBool ret = FcTrue; | |
eb0cf671 PL |
140 | FcFontSet *tmpSet; |
141 | int i; | |
24330d27 | 142 | |
d47c9d6e KP |
143 | if (config && !FcConfigAcceptFilename (config, dir)) |
144 | return FcTrue; | |
145 | ||
327a7fd4 KP |
146 | if (!force) |
147 | { | |
00f059e9 | 148 | if (FcDirCacheRead (set, dirs, dir, config)) |
2eb84374 | 149 | return FcTrue; |
327a7fd4 KP |
150 | } |
151 | ||
00f059e9 KP |
152 | if (FcDebug () & FC_DBG_FONTSET) |
153 | printf ("cache scan dir %s\n", dir); | |
154 | ||
9dac3c59 | 155 | /* freed below */ |
179c3995 | 156 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
24330d27 KP |
157 | if (!file) |
158 | return FcFalse; | |
159 | ||
ccb3e93b KP |
160 | strcpy ((char *) file, (char *) dir); |
161 | strcat ((char *) file, "/"); | |
162 | base = file + strlen ((char *) file); | |
24330d27 | 163 | |
a8386abc KP |
164 | if (FcDebug () & FC_DBG_SCAN) |
165 | printf ("\tScanning dir %s\n", dir); | |
166 | ||
ccb3e93b | 167 | d = opendir ((char *) dir); |
24330d27 KP |
168 | if (!d) |
169 | { | |
170 | free (file); | |
179c3995 KP |
171 | /* Don't complain about missing directories */ |
172 | if (errno == ENOENT) | |
173 | return FcTrue; | |
24330d27 KP |
174 | return FcFalse; |
175 | } | |
eb0cf671 PL |
176 | |
177 | tmpSet = FcFontSetCreate(); | |
65448e8b PL |
178 | if (!tmpSet) |
179 | { | |
180 | ret = FcFalse; | |
181 | goto bail0; | |
eb0cf671 PL |
182 | } |
183 | ||
00f059e9 | 184 | dirlist = FcStrSetCreate (); |
65448e8b PL |
185 | if (!dirlist) |
186 | { | |
187 | ret = FcFalse; | |
188 | goto bail1; | |
189 | } | |
00f059e9 KP |
190 | filelist = FcStrSetCreate (); |
191 | if (!filelist) | |
192 | { | |
193 | ret = FcFalse; | |
194 | goto bail2; | |
195 | } | |
e3c6d336 | 196 | while ((e = readdir (d))) |
24330d27 | 197 | { |
179c3995 | 198 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 199 | { |
00f059e9 KP |
200 | strcpy ((char *) base, (char *) e->d_name); |
201 | if (FcFileIsDir (file)) { | |
202 | if (!FcStrSetAdd (dirlist, file)) { | |
65448e8b | 203 | ret = FcFalse; |
00f059e9 KP |
204 | goto bail3; |
205 | } | |
206 | } else { | |
207 | if (!FcStrSetAdd (filelist, file)) { | |
208 | ret = FcFalse; | |
209 | goto bail3; | |
65448e8b | 210 | } |
65448e8b | 211 | } |
24330d27 KP |
212 | } |
213 | } | |
00f059e9 KP |
214 | /* |
215 | * Sort files and dirs to make things prettier | |
216 | */ | |
217 | qsort(dirlist->strs, dirlist->num, sizeof(FcChar8 *), cmpstringp); | |
218 | qsort(filelist->strs, filelist->num, sizeof(FcChar8 *), cmpstringp); | |
219 | ||
220 | for (i = 0; i < filelist->num; i++) | |
221 | FcFileScanFontConfig (tmpSet, blanks, filelist->strs[i], config); | |
222 | ||
af180c40 KP |
223 | /* |
224 | * Now that the directory has been scanned, | |
225 | * write out the cache file | |
226 | */ | |
227 | FcDirCacheWrite (tmpSet, dirlist, dir, config); | |
00f059e9 | 228 | |
c8d5753c | 229 | /* |
00f059e9 | 230 | * Add the discovered fonts to our internal non-cache list |
c8d5753c | 231 | */ |
eb0cf671 PL |
232 | for (i = 0; i < tmpSet->nfont; i++) |
233 | FcFontSetAdd (set, tmpSet->fonts[i]); | |
234 | ||
00f059e9 KP |
235 | /* |
236 | * the patterns in tmpset now belong to set; don't free them | |
237 | */ | |
238 | tmpSet->nfont = 0; | |
65448e8b | 239 | |
00f059e9 KP |
240 | /* |
241 | * Add the discovered directories to the list to be scanned | |
242 | */ | |
243 | for (i = 0; i < dirlist->num; i++) | |
244 | if (!FcStrSetAdd (dirs, dirlist->strs[i])) { | |
245 | ret = FcFalse; | |
246 | goto bail3; | |
247 | } | |
248 | ||
249 | bail3: | |
250 | FcStrSetDestroy (filelist); | |
65448e8b | 251 | bail2: |
00f059e9 | 252 | FcStrSetDestroy (dirlist); |
65448e8b | 253 | bail1: |
00f059e9 | 254 | FcFontSetDestroy (tmpSet); |
65448e8b PL |
255 | |
256 | bail0: | |
257 | closedir (d); | |
258 | ||
259 | free (file); | |
24330d27 KP |
260 | return ret; |
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 | { | |
00f059e9 | 271 | return FcDirScanConfig (set, dirs, blanks, dir, force, 0); |
d47c9d6e KP |
272 | } |
273 | ||
24330d27 | 274 | FcBool |
2304e38f | 275 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) |
24330d27 | 276 | { |
7410e40b | 277 | return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ()); |
24330d27 | 278 | } |