]>
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 KP |
37 | |
38 | FcBool | |
d47c9d6e KP |
39 | FcFileScanConfig (FcFontSet *set, |
40 | FcStrSet *dirs, | |
41 | FcGlobalCache *cache, | |
42 | FcBlanks *blanks, | |
43 | const FcChar8 *file, | |
44 | FcBool force, | |
45 | FcConfig *config) | |
24330d27 | 46 | { |
327a7fd4 | 47 | int id; |
327a7fd4 KP |
48 | FcPattern *font; |
49 | FcBool ret = FcTrue; | |
50 | FcBool isDir; | |
51 | int count = 0; | |
24330d27 | 52 | |
d47c9d6e KP |
53 | if (config && !FcConfigAcceptFilename (config, file)) |
54 | return FcTrue; | |
55 | ||
327a7fd4 KP |
56 | if (force) |
57 | cache = 0; | |
24330d27 KP |
58 | id = 0; |
59 | do | |
60 | { | |
327a7fd4 KP |
61 | font = 0; |
62 | /* | |
eb0cf671 | 63 | * Nothing in the cache, scan the file |
327a7fd4 | 64 | */ |
eb0cf671 | 65 | if (FcDebug () & FC_DBG_SCAN) |
24330d27 | 66 | { |
eb0cf671 PL |
67 | printf ("\tScanning file %s...", file); |
68 | fflush (stdout); | |
24330d27 | 69 | } |
eb0cf671 PL |
70 | font = FcFreeTypeQuery (file, id, blanks, &count); |
71 | if (FcDebug () & FC_DBG_SCAN) | |
72 | printf ("done\n"); | |
73 | isDir = FcFalse; | |
74 | if (!font && FcFileIsDir (file)) | |
24330d27 | 75 | { |
eb0cf671 PL |
76 | isDir = FcTrue; |
77 | ret = FcStrSetAdd (dirs, file); | |
24330d27 | 78 | } |
327a7fd4 KP |
79 | /* |
80 | * Add the font | |
81 | */ | |
4f27c1c0 | 82 | if (font && (!config || FcConfigAcceptFont (config, font))) |
24330d27 KP |
83 | { |
84 | if (!FcFontSetAdd (set, font)) | |
85 | { | |
86 | FcPatternDestroy (font); | |
87 | font = 0; | |
88 | ret = FcFalse; | |
89 | } | |
90 | } | |
47b49bf1 KP |
91 | else if (font) |
92 | FcPatternDestroy (font); | |
24330d27 KP |
93 | id++; |
94 | } while (font && ret && id < count); | |
95 | return ret; | |
96 | } | |
97 | ||
d47c9d6e KP |
98 | FcBool |
99 | FcFileScan (FcFontSet *set, | |
100 | FcStrSet *dirs, | |
101 | FcGlobalCache *cache, | |
102 | FcBlanks *blanks, | |
103 | const FcChar8 *file, | |
104 | FcBool force) | |
105 | { | |
106 | return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0); | |
107 | } | |
108 | ||
c8d5753c KP |
109 | /* |
110 | * Scan 'dir', adding font files to 'set' and | |
111 | * subdirectories to 'dirs' | |
112 | */ | |
113 | ||
24330d27 | 114 | FcBool |
d47c9d6e KP |
115 | FcDirScanConfig (FcFontSet *set, |
116 | FcStrSet *dirs, | |
117 | FcGlobalCache *cache, | |
118 | FcBlanks *blanks, | |
119 | const FcChar8 *dir, | |
120 | FcBool force, | |
121 | FcConfig *config) | |
24330d27 | 122 | { |
327a7fd4 KP |
123 | DIR *d; |
124 | struct dirent *e; | |
125 | FcChar8 *file; | |
126 | FcChar8 *base; | |
127 | FcBool ret = FcTrue; | |
eb0cf671 PL |
128 | FcFontSet *tmpSet; |
129 | int i; | |
24330d27 | 130 | |
d47c9d6e KP |
131 | if (config && !FcConfigAcceptFilename (config, dir)) |
132 | return FcTrue; | |
133 | ||
58bdd296 | 134 | if (config) |
f468f568 PL |
135 | dir = FcConfigNormalizeFontDir (config, dir); |
136 | ||
137 | /* refuse to scan a directory that can't be normalized. */ | |
138 | if (!dir) | |
139 | return FcFalse; | |
58bdd296 | 140 | |
327a7fd4 KP |
141 | if (!force) |
142 | { | |
327a7fd4 | 143 | /* |
f21f40f3 | 144 | * Check ~/.fonts.cache-<version> file |
327a7fd4 | 145 | */ |
8245771d | 146 | if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config)) |
327a7fd4 | 147 | return FcTrue; |
2eb84374 | 148 | |
cd9bca69 | 149 | if (FcDirCacheValid (dir) && FcDirCacheRead (set, dirs, dir, config)) |
2eb84374 | 150 | return FcTrue; |
327a7fd4 KP |
151 | } |
152 | ||
9dac3c59 | 153 | /* freed below */ |
179c3995 | 154 | file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1); |
24330d27 KP |
155 | if (!file) |
156 | return FcFalse; | |
157 | ||
ccb3e93b KP |
158 | strcpy ((char *) file, (char *) dir); |
159 | strcat ((char *) file, "/"); | |
160 | base = file + strlen ((char *) file); | |
24330d27 | 161 | |
a8386abc KP |
162 | if (FcDebug () & FC_DBG_SCAN) |
163 | printf ("\tScanning dir %s\n", dir); | |
164 | ||
ccb3e93b | 165 | d = opendir ((char *) dir); |
24330d27 KP |
166 | if (!d) |
167 | { | |
168 | free (file); | |
179c3995 KP |
169 | /* Don't complain about missing directories */ |
170 | if (errno == ENOENT) | |
171 | return FcTrue; | |
24330d27 KP |
172 | return FcFalse; |
173 | } | |
eb0cf671 PL |
174 | |
175 | tmpSet = FcFontSetCreate(); | |
176 | if (!tmpSet) | |
177 | { | |
178 | free (file); | |
179 | return FcFalse; | |
180 | } | |
181 | ||
24330d27 KP |
182 | while (ret && (e = readdir (d))) |
183 | { | |
179c3995 | 184 | if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN) |
24330d27 | 185 | { |
ccb3e93b | 186 | strcpy ((char *) base, (char *) e->d_name); |
eb0cf671 | 187 | ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config); |
24330d27 KP |
188 | } |
189 | } | |
190 | free (file); | |
191 | closedir (d); | |
c8d5753c KP |
192 | /* |
193 | * Now that the directory has been scanned, | |
194 | * add the cache entry | |
195 | */ | |
327a7fd4 | 196 | if (ret && cache) |
8a0b0ed6 | 197 | FcGlobalCacheUpdate (cache, (char *)dir, tmpSet, config); |
eb0cf671 PL |
198 | |
199 | for (i = 0; i < tmpSet->nfont; i++) | |
200 | FcFontSetAdd (set, tmpSet->fonts[i]); | |
201 | ||
202 | if (tmpSet->fonts) | |
203 | { | |
204 | FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *)); | |
205 | free (tmpSet->fonts); | |
206 | } | |
207 | FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet)); | |
208 | free (tmpSet); | |
327a7fd4 | 209 | |
24330d27 KP |
210 | return ret; |
211 | } | |
212 | ||
d47c9d6e KP |
213 | FcBool |
214 | FcDirScan (FcFontSet *set, | |
215 | FcStrSet *dirs, | |
216 | FcGlobalCache *cache, | |
217 | FcBlanks *blanks, | |
218 | const FcChar8 *dir, | |
219 | FcBool force) | |
220 | { | |
221 | return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0); | |
222 | } | |
223 | ||
24330d27 | 224 | FcBool |
2304e38f | 225 | FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir) |
24330d27 | 226 | { |
2304e38f | 227 | return FcDirCacheWrite (set, dirs, dir); |
24330d27 | 228 | } |