]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $ | |
3 | * | |
4 | * Copyright © 2000 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 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 | ||
25 | #include "fcint.h" | |
26 | #include <dirent.h> | |
27 | ||
28 | FcBool | |
29 | FcFileIsDir (const FcChar8 *file) | |
30 | { | |
31 | struct stat statb; | |
32 | ||
33 | if (stat ((const char *) file, &statb) != 0) | |
34 | return FcFalse; | |
35 | return S_ISDIR(statb.st_mode); | |
36 | } | |
37 | ||
38 | FcBool | |
39 | FcFileScanConfig (FcFontSet *set, | |
40 | FcStrSet *dirs, | |
41 | FcGlobalCache *cache, | |
42 | FcBlanks *blanks, | |
43 | const FcChar8 *file, | |
44 | FcBool force, | |
45 | FcConfig *config) | |
46 | { | |
47 | int id; | |
48 | #if 0 | |
49 | FcChar8 *name; | |
50 | FcGlobalCacheFile *cache_file; | |
51 | FcGlobalCacheDir *cache_dir; | |
52 | #endif | |
53 | FcPattern *font; | |
54 | FcBool ret = FcTrue; | |
55 | FcBool isDir; | |
56 | int count = 0; | |
57 | FcBool need_scan; | |
58 | ||
59 | if (config && !FcConfigAcceptFilename (config, file)) | |
60 | return FcTrue; | |
61 | ||
62 | if (force) | |
63 | cache = 0; | |
64 | id = 0; | |
65 | do | |
66 | { | |
67 | need_scan = FcTrue; | |
68 | font = 0; | |
69 | #if 0 | |
70 | /* | |
71 | * Check the cache | |
72 | */ | |
73 | if (cache) | |
74 | { | |
75 | if ((cache_file = FcGlobalCacheFileGet (cache, file, id, &count))) | |
76 | { | |
77 | /* | |
78 | * Found a cache entry for the file | |
79 | */ | |
80 | if (FcGlobalCacheCheckTime (file, &cache_file->info)) | |
81 | { | |
82 | name = cache_file->name; | |
83 | need_scan = FcFalse; | |
84 | FcGlobalCacheReferenced (cache, &cache_file->info); | |
85 | /* "." means the file doesn't contain a font */ | |
86 | if (FcStrCmp (name, FC_FONT_FILE_INVALID) != 0) | |
87 | { | |
88 | font = FcNameParse (name); | |
89 | if (font) | |
90 | if (!FcPatternAddString (font, FC_FILE, file)) | |
91 | ret = FcFalse; | |
92 | } | |
93 | } | |
94 | } | |
95 | else if ((cache_dir = FcGlobalCacheDirGet (cache, file, | |
96 | strlen ((const char *) file), | |
97 | FcFalse))) | |
98 | { | |
99 | if (FcGlobalCacheCheckTime (cache_dir->info.file, | |
100 | &cache_dir->info)) | |
101 | { | |
102 | font = 0; | |
103 | need_scan = FcFalse; | |
104 | FcGlobalCacheReferenced (cache, &cache_dir->info); | |
105 | if (!FcStrSetAdd (dirs, file)) | |
106 | ret = FcFalse; | |
107 | } | |
108 | } | |
109 | } | |
110 | #endif | |
111 | /* | |
112 | * Nothing in the cache, scan the file | |
113 | */ | |
114 | if (need_scan) | |
115 | { | |
116 | if (FcDebug () & FC_DBG_SCAN) | |
117 | { | |
118 | printf ("\tScanning file %s...", file); | |
119 | fflush (stdout); | |
120 | } | |
121 | font = FcFreeTypeQuery (file, id, blanks, &count); | |
122 | if (FcDebug () & FC_DBG_SCAN) | |
123 | printf ("done\n"); | |
124 | isDir = FcFalse; | |
125 | if (!font && FcFileIsDir (file)) | |
126 | { | |
127 | isDir = FcTrue; | |
128 | ret = FcStrSetAdd (dirs, file); | |
129 | } | |
130 | #if 0 | |
131 | /* | |
132 | * Update the cache | |
133 | */ | |
134 | if (cache && font) | |
135 | { | |
136 | FcChar8 *unparse; | |
137 | ||
138 | unparse = FcNameUnparse (font); | |
139 | if (unparse) | |
140 | { | |
141 | (void) FcGlobalCacheUpdate (cache, file, id, unparse); | |
142 | FcStrFree (unparse); | |
143 | } | |
144 | } | |
145 | #endif | |
146 | } | |
147 | /* | |
148 | * Add the font | |
149 | */ | |
150 | if (font && (!config || FcConfigAcceptFont (config, font))) | |
151 | { | |
152 | if (!FcFontSetAdd (set, font)) | |
153 | { | |
154 | FcPatternDestroy (font); | |
155 | font = 0; | |
156 | ret = FcFalse; | |
157 | } | |
158 | } | |
159 | else if (font) | |
160 | FcPatternDestroy (font); | |
161 | id++; | |
162 | } while (font && ret && id < count); | |
163 | return ret; | |
164 | } | |
165 | ||
166 | FcBool | |
167 | FcFileScan (FcFontSet *set, | |
168 | FcStrSet *dirs, | |
169 | FcGlobalCache *cache, | |
170 | FcBlanks *blanks, | |
171 | const FcChar8 *file, | |
172 | FcBool force) | |
173 | { | |
174 | return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); | |
175 | } | |
176 | ||
177 | /* | |
178 | * Scan 'dir', adding font files to 'set' and | |
179 | * subdirectories to 'dirs' | |
180 | */ | |
181 | ||
182 | FcBool | |
183 | FcDirScanConfig (FcFontSet *set, | |
184 | FcStrSet *dirs, | |
185 | FcGlobalCache *cache, | |
186 | FcBlanks *blanks, | |
187 | const FcChar8 *dir, | |
188 | FcBool force, | |
189 | FcConfig *config) | |
190 | { | |
191 | DIR *d; | |
192 | struct dirent *e; | |
193 | FcChar8 *file; | |
194 | FcChar8 *base; | |
195 | FcBool ret = FcTrue; | |
196 | ||
197 | if (config && !FcConfigAcceptFilename (config, dir)) | |
198 | return FcTrue; | |
199 | ||
200 | if (!force) | |
201 | { | |
202 | #if 0 | |
203 | /* | |
204 | * Check fonts.cache-<version> file | |
205 | */ | |
206 | if (FcDirCacheReadDir (set, dirs, dir, config)) | |
207 | { | |
208 | if (cache) | |
209 | FcGlobalCacheReferenceSubdir (cache, dir); | |
210 | return FcTrue; | |
211 | } | |
212 | ||
213 | /* | |
214 | * Check ~/.fonts.cache-<version> file | |
215 | */ | |
216 | if (cache && FcGlobalCacheScanDir (set, dirs, cache, dir, config)) | |
217 | return FcTrue; | |
218 | #endif | |
219 | } | |
220 | ||
221 | /* freed below */ | |
222 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); | |
223 | if (!file) | |
224 | return FcFalse; | |
225 | ||
226 | strcpy ((char *) file, (char *) dir); | |
227 | strcat ((char *) file, "/"); | |
228 | base = file + strlen ((char *) file); | |
229 | ||
230 | if (FcDebug () & FC_DBG_SCAN) | |
231 | printf ("\tScanning dir %s\n", dir); | |
232 | ||
233 | d = opendir ((char *) dir); | |
234 | ||
235 | if (!d) | |
236 | { | |
237 | free (file); | |
238 | /* Don't complain about missing directories */ | |
239 | if (errno == ENOENT) | |
240 | return FcTrue; | |
241 | return FcFalse; | |
242 | } | |
243 | while (ret && (e = readdir (d))) | |
244 | { | |
245 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) | |
246 | { | |
247 | strcpy ((char *) base, (char *) e->d_name); | |
248 | ret = FcFileScanConfig (set, dirs, cache, blanks, file, force, config); | |
249 | } | |
250 | } | |
251 | free (file); | |
252 | closedir (d); | |
253 | /* | |
254 | * Now that the directory has been scanned, | |
255 | * add the cache entry | |
256 | */ | |
257 | #if 0 | |
258 | if (ret && cache) | |
259 | FcGlobalCacheUpdate (cache, dir, 0, 0); | |
260 | #endif | |
261 | ||
262 | return ret; | |
263 | } | |
264 | ||
265 | FcBool | |
266 | FcDirScan (FcFontSet *set, | |
267 | FcStrSet *dirs, | |
268 | FcGlobalCache *cache, | |
269 | FcBlanks *blanks, | |
270 | const FcChar8 *dir, | |
271 | FcBool force) | |
272 | { | |
273 | return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); | |
274 | } | |
275 | ||
276 | FcBool | |
277 | FcDirSave (FcFontSet *set, const FcChar8 *dir) | |
278 | { | |
279 | static int rand_state = 0; | |
280 | int bank; | |
281 | ||
282 | if (!rand_state) | |
283 | rand_state = time(0L); | |
284 | bank = rand_r(&rand_state); | |
285 | ||
286 | while (FcCacheHaveBank(bank)) | |
287 | bank = rand_r(&rand_state); | |
288 | ||
289 | return FcDirCacheWrite (bank, set, dir); | |
290 | } |