]>
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 | FcPattern *font; | |
49 | FcBool ret = FcTrue; | |
50 | int count = 0; | |
51 | ||
52 | if (config && !FcConfigAcceptFilename (config, file)) | |
53 | return FcTrue; | |
54 | ||
55 | if (FcFileIsDir (file)) | |
56 | return FcStrSetAdd (dirs, file); | |
57 | ||
58 | id = 0; | |
59 | do | |
60 | { | |
61 | font = 0; | |
62 | /* | |
63 | * Nothing in the cache, scan the file | |
64 | */ | |
65 | if (FcDebug () & FC_DBG_SCAN) | |
66 | { | |
67 | printf ("\tScanning file %s...", file); | |
68 | fflush (stdout); | |
69 | } | |
70 | font = FcFreeTypeQuery (file, id, blanks, &count); | |
71 | if (FcDebug () & FC_DBG_SCAN) | |
72 | printf ("done\n"); | |
73 | /* | |
74 | * Add the font | |
75 | */ | |
76 | if (font && (!config || FcConfigAcceptFont (config, font))) | |
77 | { | |
78 | if (!FcFontSetAdd (set, font)) | |
79 | { | |
80 | FcPatternDestroy (font); | |
81 | font = 0; | |
82 | ret = FcFalse; | |
83 | } | |
84 | } | |
85 | else if (font) | |
86 | FcPatternDestroy (font); | |
87 | id++; | |
88 | } while (font && ret && id < count); | |
89 | return ret; | |
90 | } | |
91 | ||
92 | FcBool | |
93 | FcFileScan (FcFontSet *set, | |
94 | FcStrSet *dirs, | |
95 | FcGlobalCache *cache, | |
96 | FcBlanks *blanks, | |
97 | const FcChar8 *file, | |
98 | FcBool force) | |
99 | { | |
100 | return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); | |
101 | } | |
102 | ||
103 | /* | |
104 | * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage | |
105 | */ | |
106 | ||
107 | static int | |
108 | cmpstringp(const void *p1, const void *p2) | |
109 | { | |
110 | return strcmp(* (char **) p1, * (char **) p2); | |
111 | } | |
112 | ||
113 | /* | |
114 | * Scan 'dir', adding font files to 'set' and | |
115 | * subdirectories to 'dirs' | |
116 | */ | |
117 | ||
118 | FcBool | |
119 | FcDirScanConfig (FcFontSet *set, | |
120 | FcStrSet *dirs, | |
121 | FcGlobalCache *cache, | |
122 | FcBlanks *blanks, | |
123 | const FcChar8 *dir, | |
124 | FcBool force, | |
125 | FcConfig *config) | |
126 | { | |
127 | DIR *d; | |
128 | struct dirent *e; | |
129 | FcChar8 **dirlist; | |
130 | int dirlistlen, dirlistalloc; | |
131 | FcChar8 *file; | |
132 | FcChar8 *base; | |
133 | FcBool ret = FcTrue; | |
134 | FcFontSet *tmpSet; | |
135 | int i; | |
136 | ||
137 | if (config && !FcConfigAcceptFilename (config, dir)) | |
138 | return FcTrue; | |
139 | ||
140 | if (!force) | |
141 | { | |
142 | /* | |
143 | * Check ~/.fonts.cache-<version> file | |
144 | */ | |
145 | if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config)) | |
146 | return FcTrue; | |
147 | ||
148 | if (FcDirCacheValid (dir, config) && | |
149 | FcDirCacheHasCurrentArch (dir, config) && | |
150 | FcDirCacheRead (set, dirs, dir, config)) | |
151 | return FcTrue; | |
152 | } | |
153 | ||
154 | /* freed below */ | |
155 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); | |
156 | if (!file) | |
157 | return FcFalse; | |
158 | ||
159 | strcpy ((char *) file, (char *) dir); | |
160 | strcat ((char *) file, "/"); | |
161 | base = file + strlen ((char *) file); | |
162 | ||
163 | if (FcDebug () & FC_DBG_SCAN) | |
164 | printf ("\tScanning dir %s\n", dir); | |
165 | ||
166 | d = opendir ((char *) dir); | |
167 | if (!d) | |
168 | { | |
169 | free (file); | |
170 | /* Don't complain about missing directories */ | |
171 | if (errno == ENOENT) | |
172 | return FcTrue; | |
173 | return FcFalse; | |
174 | } | |
175 | ||
176 | tmpSet = FcFontSetCreate(); | |
177 | if (!tmpSet) | |
178 | { | |
179 | ret = FcFalse; | |
180 | goto bail0; | |
181 | } | |
182 | ||
183 | dirlistlen = 0; | |
184 | dirlistalloc = 8; | |
185 | dirlist = malloc(dirlistalloc * sizeof(FcChar8 *)); | |
186 | if (!dirlist) | |
187 | { | |
188 | ret = FcFalse; | |
189 | goto bail1; | |
190 | } | |
191 | while ((e = readdir (d))) | |
192 | { | |
193 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) | |
194 | { | |
195 | if (dirlistlen == dirlistalloc) | |
196 | { | |
197 | FcChar8 **tmp_dirlist; | |
198 | ||
199 | dirlistalloc *= 2; | |
200 | tmp_dirlist = realloc(dirlist, | |
201 | dirlistalloc * sizeof(FcChar8 *)); | |
202 | if (!tmp_dirlist) | |
203 | { | |
204 | ret = FcFalse; | |
205 | goto bail2; | |
206 | } | |
207 | dirlist = tmp_dirlist; | |
208 | } | |
209 | dirlist[dirlistlen] = malloc(strlen (e->d_name) + 1); | |
210 | if (!dirlist[dirlistlen]) | |
211 | { | |
212 | ret = FcFalse; | |
213 | goto bail2; | |
214 | } | |
215 | strcpy((char *)dirlist[dirlistlen], e->d_name); | |
216 | dirlistlen++; | |
217 | } | |
218 | } | |
219 | qsort(dirlist, dirlistlen, sizeof(FcChar8 *), cmpstringp); | |
220 | i = 0; | |
221 | while (ret && i < dirlistlen) | |
222 | { | |
223 | strcpy ((char *) base, (char *) dirlist[i]); | |
224 | ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config); | |
225 | i++; | |
226 | } | |
227 | /* | |
228 | * Now that the directory has been scanned, | |
229 | * add the cache entry | |
230 | */ | |
231 | if (ret && cache) | |
232 | FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config); | |
233 | ||
234 | for (i = 0; i < tmpSet->nfont; i++) | |
235 | FcFontSetAdd (set, tmpSet->fonts[i]); | |
236 | ||
237 | if (tmpSet->fonts) | |
238 | { | |
239 | FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *)); | |
240 | free (tmpSet->fonts); | |
241 | } | |
242 | FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet)); | |
243 | ||
244 | bail2: | |
245 | for (i = 0; i < dirlistlen; i++) | |
246 | free(dirlist[i]); | |
247 | ||
248 | free (dirlist); | |
249 | ||
250 | bail1: | |
251 | free (tmpSet); | |
252 | ||
253 | bail0: | |
254 | closedir (d); | |
255 | ||
256 | free (file); | |
257 | return ret; | |
258 | } | |
259 | ||
260 | FcBool | |
261 | FcDirScan (FcFontSet *set, | |
262 | FcStrSet *dirs, | |
263 | FcGlobalCache *cache, | |
264 | FcBlanks *blanks, | |
265 | const FcChar8 *dir, | |
266 | FcBool force) | |
267 | { | |
268 | return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); | |
269 | } | |
270 | ||
271 | FcBool | |
272 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) | |
273 | { | |
274 | return FcDirCacheWrite (set, dirs, dir, FcConfigGetCurrent ()); | |
275 | } |