fcdir.c 5.24 KB
Newer Older
Keith Packard's avatar
Keith Packard committed
1
/*
Keith Packard's avatar
Keith Packard committed
2
 * $RCSId: xc/lib/fontconfig/src/fcdir.c,v 1.9 2002/08/31 22:17:32 keithp Exp $
Keith Packard's avatar
Keith Packard committed
3
 *
4
 * Copyright © 2000 Keith Packard
Keith Packard's avatar
Keith Packard committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "fcint.h"
26
27
#include <dirent.h>

28
FcBool
29
30
31
FcFileIsDir (const FcChar8 *file)
{
    struct stat	    statb;
Keith Packard's avatar
Keith Packard committed
32

33
34
35
36
    if (stat ((const char *) file, &statb) != 0)
	return FcFalse;
    return S_ISDIR(statb.st_mode);
}
Keith Packard's avatar
Keith Packard committed
37
38

FcBool
39
40
41
42
43
44
45
FcFileScanConfig (FcFontSet	*set,
		  FcStrSet	*dirs,
		  FcGlobalCache *cache,
		  FcBlanks	*blanks,
		  const FcChar8	*file,
		  FcBool	force,
		  FcConfig	*config)
Keith Packard's avatar
Keith Packard committed
46
{
47
48
49
50
    int			id;
    FcPattern		*font;
    FcBool		ret = FcTrue;
    int			count = 0;
Keith Packard's avatar
Keith Packard committed
51
    
52
53
54
    if (config && !FcConfigAcceptFilename (config, file))
	return FcTrue;

55
56
57
    if (FcFileIsDir (file))
	return FcStrSetAdd (dirs, file);

58
59
    if (force)
	cache = 0;
60

Keith Packard's avatar
Keith Packard committed
61
62
63
    id = 0;
    do
    {
64
65
	font = 0;
	/*
Patrick Lam's avatar
Patrick Lam committed
66
	 * Nothing in the cache, scan the file
67
	 */
Patrick Lam's avatar
Patrick Lam committed
68
	if (FcDebug () & FC_DBG_SCAN)
Keith Packard's avatar
Keith Packard committed
69
	{
Patrick Lam's avatar
Patrick Lam committed
70
71
	    printf ("\tScanning file %s...", file);
	    fflush (stdout);
Keith Packard's avatar
Keith Packard committed
72
	}
Patrick Lam's avatar
Patrick Lam committed
73
74
75
	font = FcFreeTypeQuery (file, id, blanks, &count);
	if (FcDebug () & FC_DBG_SCAN)
	    printf ("done\n");
76
77
78
	/*
	 * Add the font
	 */
79
	if (font && (!config || FcConfigAcceptFont (config, font)))
Keith Packard's avatar
Keith Packard committed
80
81
82
83
84
85
86
87
	{
	    if (!FcFontSetAdd (set, font))
	    {
		FcPatternDestroy (font);
		font = 0;
		ret = FcFalse;
	    }
	}
88
89
	else if (font)
	    FcPatternDestroy (font);
Keith Packard's avatar
Keith Packard committed
90
91
92
93
94
	id++;
    } while (font && ret && id < count);
    return ret;
}

95
96
97
98
99
100
101
102
103
104
105
FcBool
FcFileScan (FcFontSet	    *set,
	    FcStrSet	    *dirs,
	    FcGlobalCache   *cache,
	    FcBlanks	    *blanks,
	    const FcChar8   *file,
	    FcBool	    force)
{
    return FcFileScanConfig (set, dirs, cache, blanks, file, force, 0);
}

106
107
108
109
110
/*
 * Scan 'dir', adding font files to 'set' and
 * subdirectories to 'dirs'
 */

Keith Packard's avatar
Keith Packard committed
111
FcBool
112
113
114
115
116
117
118
FcDirScanConfig (FcFontSet	*set,
		 FcStrSet	*dirs,
		 FcGlobalCache  *cache,
		 FcBlanks	*blanks,
		 const FcChar8  *dir,
		 FcBool		force,
		 FcConfig	*config)
Keith Packard's avatar
Keith Packard committed
119
{
120
121
122
123
124
    DIR			*d;
    struct dirent	*e;
    FcChar8		*file;
    FcChar8		*base;
    FcBool		ret = FcTrue;
Patrick Lam's avatar
Patrick Lam committed
125
126
    FcFontSet		*tmpSet;
    int			i;
Keith Packard's avatar
Keith Packard committed
127

128
129
130
    if (config && !FcConfigAcceptFilename (config, dir))
	return FcTrue;

131
    if (config)
132
133
134
135
136
	dir = FcConfigNormalizeFontDir (config, dir);

    /* refuse to scan a directory that can't be normalized. */
    if (!dir)
	return FcFalse;
137

138
139
140
    if (!force)
    {
	/*
141
	 * Check ~/.fonts.cache-<version> file
142
	 */
143
	if (cache && FcGlobalCacheReadDir (set, dirs, cache, (char *)dir, config))
144
	    return FcTrue;
145

146
	if (FcDirCacheValid (dir) && FcDirCacheRead (set, dirs, dir, config))
147
	    return FcTrue;
148
149
    }
    
150
    /* freed below */
151
    file = (FcChar8 *) malloc (strlen ((char *) dir) + 1 + FC_MAX_FILE_LEN + 1);
Keith Packard's avatar
Keith Packard committed
152
153
154
    if (!file)
	return FcFalse;

155
156
157
    strcpy ((char *) file, (char *) dir);
    strcat ((char *) file, "/");
    base = file + strlen ((char *) file);
Keith Packard's avatar
Keith Packard committed
158
    
159
160
161
    if (FcDebug () & FC_DBG_SCAN)
	printf ("\tScanning dir %s\n", dir);
	
162
    d = opendir ((char *) dir);
Keith Packard's avatar
Keith Packard committed
163
164
165
    if (!d)
    {
	free (file);
166
167
168
	/* Don't complain about missing directories */
	if (errno == ENOENT)
	    return FcTrue;
Keith Packard's avatar
Keith Packard committed
169
170
	return FcFalse;
    }
Patrick Lam's avatar
Patrick Lam committed
171
172
173
174
175
176
177
178

    tmpSet = FcFontSetCreate();
    if (!tmpSet)
    {	
	free (file);
	return FcFalse;
    }

Keith Packard's avatar
Keith Packard committed
179
180
    while (ret && (e = readdir (d)))
    {
181
	if (e->d_name[0] != '.' && strlen (e->d_name) < FC_MAX_FILE_LEN)
Keith Packard's avatar
Keith Packard committed
182
	{
183
	    strcpy ((char *) base, (char *) e->d_name);
Patrick Lam's avatar
Patrick Lam committed
184
	    ret = FcFileScanConfig (tmpSet, dirs, cache, blanks, file, force, config);
Keith Packard's avatar
Keith Packard committed
185
186
187
188
	}
    }
    free (file);
    closedir (d);
189
190
191
192
    /*
     * Now that the directory has been scanned,
     * add the cache entry 
     */
193
    if (ret && cache)
194
	FcGlobalCacheUpdate (cache, dirs, (char *)dir, tmpSet, config);
Patrick Lam's avatar
Patrick Lam committed
195
196
197
198
199
200
201
202
203
204
205

    for (i = 0; i < tmpSet->nfont; i++)
	FcFontSetAdd (set, tmpSet->fonts[i]);

    if (tmpSet->fonts)
    {
	FcMemFree (FC_MEM_FONTPTR, tmpSet->sfont * sizeof (FcPattern *));
	free (tmpSet->fonts);
    }
    FcMemFree (FC_MEM_FONTSET, sizeof (FcFontSet));
    free (tmpSet);
206
	
Keith Packard's avatar
Keith Packard committed
207
208
209
    return ret;
}

210
211
212
213
214
215
216
217
218
219
220
FcBool
FcDirScan (FcFontSet	    *set,
	   FcStrSet	    *dirs,
	   FcGlobalCache    *cache,
	   FcBlanks	    *blanks,
	   const FcChar8    *dir,
	   FcBool	    force)
{
    return FcDirScanConfig (set, dirs, cache, blanks, dir, force, 0);
}

Keith Packard's avatar
Keith Packard committed
221
FcBool
Patrick Lam's avatar
:    
Patrick Lam committed
222
FcDirSave (FcFontSet *set, FcStrSet * dirs, const FcChar8 *dir)
Keith Packard's avatar
Keith Packard committed
223
{
Patrick Lam's avatar
:    
Patrick Lam committed
224
    return FcDirCacheWrite (set, dirs, dir);
Keith Packard's avatar
Keith Packard committed
225
}