]> git.wh0rd.org - fontconfig.git/blame - src/fcdir.c
Remove stuff we don't use, make get_{char,short,long} functions of ftglue
[fontconfig.git] / src / fcdir.c
CommitLineData
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 28FcBool
179c3995
KP
29FcFileIsDir (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
38FcBool
d47c9d6e
KP
39FcFileScanConfig (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;
327a7fd4 50 int count = 0;
24330d27 51
d47c9d6e
KP
52 if (config && !FcConfigAcceptFilename (config, file))
53 return FcTrue;
54
98592bbb
PL
55 if (FcFileIsDir (file))
56 return FcStrSetAdd (dirs, file);
57
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");
327a7fd4
KP
73 /*
74 * Add the font
75 */
4f27c1c0 76 if (font && (!config || FcConfigAcceptFont (config, font)))
24330d27
KP
77 {
78 if (!FcFontSetAdd (set, font))
79 {
80 FcPatternDestroy (font);
81 font = 0;
82 ret = FcFalse;
83 }
84 }
47b49bf1
KP
85 else if (font)
86 FcPatternDestroy (font);
24330d27
KP
87 id++;
88 } while (font && ret && id < count);
89 return ret;
90}
91
d47c9d6e
KP
92FcBool
93FcFileScan (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
c8d5753c
KP
103/*
104 * Scan 'dir', adding font files to 'set' and
105 * subdirectories to 'dirs'
106 */
107
24330d27 108FcBool
d47c9d6e
KP
109FcDirScanConfig (FcFontSet *set,
110 FcStrSet *dirs,
111 FcGlobalCache *cache,
112 FcBlanks *blanks,
113 const FcChar8 *dir,
114 FcBool force,
115 FcConfig *config)
24330d27 116{
327a7fd4
KP
117 DIR *d;
118 struct dirent *e;
119 FcChar8 *file;
5c3deb29 120 const FcChar8 *d_can = 0;
327a7fd4
KP
121 FcChar8 *base;
122 FcBool ret = FcTrue;
eb0cf671
PL
123 FcFontSet *tmpSet;
124 int i;
24330d27 125
d47c9d6e
KP
126 if (config && !FcConfigAcceptFilename (config, dir))
127 return FcTrue;
128
58bdd296 129 if (config)
5c3deb29
PL
130 d_can = FcConfigNormalizeFontDir (config, dir);
131 if (d_can)
132 dir = d_can;
58bdd296 133
327a7fd4
KP
134 if (!force)
135 {
327a7fd4 136 /*
f21f40f3 137 * Check ~/.fonts.cache-<version> file
327a7fd4 138 */
8245771d 139 if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
327a7fd4 140 return FcTrue;
2eb84374 141
530e66b0
PL
142 if (FcDirCacheValid (dir) &&
143 FcDirCacheHasCurrentArch (dir) &&
144 FcDirCacheRead (set, dirs, dir, config))
2eb84374 145 return FcTrue;
327a7fd4
KP
146 }
147
9dac3c59 148 /* freed below */
179c3995 149 file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
24330d27
KP
150 if (!file)
151 return FcFalse;
152
ccb3e93b
KP
153 strcpy ((char *) file, (char *) dir);
154 strcat ((char *) file, "/");
155 base = file + strlen ((char *) file);
24330d27 156
a8386abc
KP
157 if (FcDebug () & FC_DBG_SCAN)
158 printf ("\tScanning dir %s\n", dir);
159
ccb3e93b 160 d = opendir ((char *) dir);
24330d27
KP
161 if (!d)
162 {
163 free (file);
179c3995
KP
164 /* Don't complain about missing directories */
165 if (errno == ENOENT)
166 return FcTrue;
24330d27
KP
167 return FcFalse;
168 }
eb0cf671
PL
169
170 tmpSet = FcFontSetCreate();
171 if (!tmpSet)
172 {
173 free (file);
174 return FcFalse;
175 }
176
24330d27
KP
177 while (ret && (e = readdir (d)))
178 {
179c3995 179 if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
24330d27 180 {
ccb3e93b 181 strcpy ((char *) base, (char *) e->d_name);
eb0cf671 182 ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
24330d27
KP
183 }
184 }
185 free (file);
186 closedir (d);
c8d5753c
KP
187 /*
188 * Now that the directory has been scanned,
189 * add the cache entry
190 */
327a7fd4 191 if (ret && cache)
971cf180 192 FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
eb0cf671
PL
193
194 for (i = 0; i < tmpSet->nfont; i++)
195 FcFontSetAdd (set, tmpSet->fonts[i]);
196
197 if (tmpSet->fonts)
198 {
199 FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
200 free (tmpSet->fonts);
201 }
202 FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
203 free (tmpSet);
327a7fd4 204
24330d27
KP
205 return ret;
206}
207
d47c9d6e
KP
208FcBool
209FcDirScan (FcFontSet *set,
210 FcStrSet *dirs,
211 FcGlobalCache *cache,
212 FcBlanks *blanks,
213 const FcChar8 *dir,
214 FcBool force)
215{
216 return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
217}
218
24330d27 219FcBool
2304e38f 220FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
24330d27 221{
2304e38f 222 return FcDirCacheWrite (set, dirs, dir);
24330d27 223}