Merge branch 'fc-2_4_branch' to master
[platform/upstream/fontconfig.git] / src / fcdir.c
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 static FcBool
39 FcFileScanFontConfig (FcFontSet         *set,
40                       FcBlanks          *blanks,
41                       const FcChar8     *file,
42                       FcConfig          *config)
43 {
44     FcPattern   *font;
45     FcBool      ret = FcTrue;
46     int         id;
47     int         count = 0;
48     
49     id = 0;
50     do
51     {
52         font = 0;
53         /*
54          * Nothing in the cache, scan the file
55          */
56         if (FcDebug () & FC_DBG_SCAN)
57         {
58             printf ("\tScanning file %s...", file);
59             fflush (stdout);
60         }
61         font = FcFreeTypeQuery (file, id, blanks, &count);
62         if (FcDebug () & FC_DBG_SCAN)
63             printf ("done\n");
64
65         /*
66          * Edit pattern with user-defined rules
67          */
68         if (config && !FcConfigSubstituteWithPat (config, font, NULL, FcMatchScan))
69         {
70             FcPatternDestroy (font);
71             font = NULL;
72             ret = FcFalse;
73         }
74
75         /*
76          * Add the font
77          */
78         if (font && (!config || FcConfigAcceptFont (config, font)))
79         {
80             if (FcDebug() & FC_DBG_SCANV)
81             {
82                 printf ("Final font pattern:\n");
83                 FcPatternPrint (font);
84             }
85             if (!FcFontSetAdd (set, font))
86             {
87                 FcPatternDestroy (font);
88                 font = NULL;
89                 ret = FcFalse;
90             }
91         }
92         else if (font)
93             FcPatternDestroy (font);
94         id++;
95     } while (font && ret && id < count);
96     return ret;
97 }
98
99 FcBool
100 FcFileScanConfig (FcFontSet     *set,
101                   FcStrSet      *dirs,
102                   FcBlanks      *blanks,
103                   const FcChar8 *file,
104                   FcConfig      *config)
105 {
106     if (FcFileIsDir (file))
107         return FcStrSetAdd (dirs, file);
108     else
109         return FcFileScanFontConfig (set, blanks, file, config);
110 }
111
112 FcBool
113 FcFileScan (FcFontSet       *set,
114             FcStrSet        *dirs,
115             FcFileCache     *cache, /* XXX unused */
116             FcBlanks        *blanks,
117             const FcChar8   *file,
118             FcBool          force)
119 {
120     return FcFileScanConfig (set, dirs, blanks, file, NULL);
121 }
122
123 /*
124  * Strcmp helper that takes pointers to pointers, copied from qsort(3) manpage
125  */
126 static int
127 cmpstringp(const void *p1, const void *p2)
128 {
129     return strcmp(* (char **) p1, * (char **) p2);
130 }
131
132 /*
133  * Scan the specified directory and construct a cache of its contents
134  */
135 FcCache *
136 FcDirCacheScan (const FcChar8 *dir, FcConfig *config)
137 {
138     DIR                 *d;
139     struct dirent       *e;
140     FcStrSet            *files;
141     FcStrSet            *dirs;
142     FcChar8             *file;
143     FcChar8             *base;
144     FcBool              ret = FcTrue;
145     FcFontSet           *set;
146     int                 i;
147     FcBlanks            *blanks = FcConfigGetBlanks (config);
148     FcCache             *cache = NULL;
149
150     if (FcDebug () & FC_DBG_FONTSET)
151         printf ("cache scan dir %s\n", dir);
152
153     /* freed below */
154     file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
155     if (!file) {
156         ret = FcFalse;
157         goto bail;
158     }
159
160     strcpy ((char *) file, (char *) dir);
161     strcat ((char *) file, "/");
162     base = file + strlen ((char *) file);
163     
164     if (FcDebug () & FC_DBG_SCAN)
165         printf ("\tScanning dir %s\n", dir);
166         
167     d = opendir ((char *) dir);
168     if (!d)
169     {
170         /* Don't complain about missing directories */
171         if (errno == ENOENT)
172             ret = FcTrue;
173         else
174             ret = FcFalse;
175         goto bail_1;
176     }
177
178     set = FcFontSetCreate();
179     if (!set) 
180     {
181         ret = FcFalse;
182         goto bail0;
183     }
184
185     files = FcStrSetCreate ();
186     if (!files)
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             strcpy ((char *) base, (char *) e->d_name);
196             if (!FcStrSetAdd (files, file)) {
197                 ret = FcFalse;
198                 goto bail2;
199             }
200         }
201     }
202
203     /*
204      * Sort files to make things prettier
205      */
206     qsort(files->strs, files->num, sizeof(FcChar8 *), cmpstringp);
207     
208     dirs = FcStrSetCreate ();
209     if (!dirs)
210         goto bail2;
211     
212     /*
213      * Scan file files to build font patterns
214      */
215     for (i = 0; i < files->num; i++)
216         FcFileScanConfig (set, dirs, blanks, files->strs[i], config);
217     
218     /*
219      * Build the cache object
220      */
221     cache = FcDirCacheBuild (set, dir, dirs);
222     if (!cache)
223         goto bail3;
224     
225     /*
226      * Write out the cache file, ignoring any troubles
227      */
228     FcDirCacheWrite (cache, config);
229     
230  bail3:
231     FcStrSetDestroy (dirs);
232  bail2:
233     FcStrSetDestroy (files);
234  bail1:
235     FcFontSetDestroy (set);
236     
237  bail0:
238     closedir (d);
239     
240  bail_1:
241     free (file);
242  bail:
243     return cache;
244 }
245
246 /*
247  * Read (or construct) the cache for a directory
248  */
249 FcCache *
250 FcDirCacheRead (const FcChar8 *dir, FcBool force, FcConfig *config)
251 {
252     FcCache             *cache = NULL;
253
254     if (config && !FcConfigAcceptFilename (config, dir))
255         return NULL;
256
257     /* Try to use existing cache file */
258     if (!force)
259         cache = FcDirCacheLoad (dir, config, NULL);
260     
261     /* Not using existing cache file, construct new cache */
262     if (!cache)
263         cache = FcDirCacheScan (dir, config);
264     
265     return cache;
266 }
267
268 FcBool
269 FcDirScanConfig (FcFontSet      *set,
270                  FcStrSet       *dirs,
271                  FcBlanks       *blanks,
272                  const FcChar8  *dir,
273                  FcBool         force,
274                  FcConfig       *config)
275 {
276     return FcFalse; /* XXX fixme */
277 }
278
279 FcBool
280 FcDirScan (FcFontSet        *set,
281            FcStrSet         *dirs,
282            FcFileCache      *cache, /* XXX unused */
283            FcBlanks         *blanks,
284            const FcChar8    *dir,
285            FcBool           force)
286 {
287     return FcDirScanConfig (set, dirs, blanks, dir, force, NULL);
288 }
289
290 FcBool
291 FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
292 {
293     return FcFalse; /* XXX deprecated */
294 }