1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
|
/*
* Copyright © 2007 Red Hat, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
* Author:
* Kristian Høgsberg <krh@redhat.com>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_READLINK
#include <X11/fonts/fntfilst.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
static const char CataloguePrefix[] = "catalogue:";
static int CatalogueFreeFPE (FontPathElementPtr fpe);
static int
CatalogueNameCheck (char *name)
{
return strncmp(name, CataloguePrefix, sizeof(CataloguePrefix) - 1) == 0;
}
typedef struct _CatalogueRec {
time_t mtime;
int fpeCount, fpeAlloc;
FontPathElementPtr *fpeList;
} CatalogueRec, *CataloguePtr;
static int
CatalogueAddFPE (CataloguePtr cat, FontPathElementPtr fpe)
{
FontPathElementPtr *new;
if (cat->fpeCount >= cat->fpeAlloc)
{
if (cat->fpeAlloc == 0)
cat->fpeAlloc = 16;
else
cat->fpeAlloc *= 2;
new = realloc(cat->fpeList, cat->fpeAlloc * sizeof(FontPathElementPtr));
if (new == NULL)
return AllocError;
cat->fpeList = new;
}
cat->fpeList[cat->fpeCount++] = fpe;
return Successful;
}
static const char PriorityAttribute[] = "pri=";
static int
ComparePriority(const void *p1, const void *p2)
{
FontDirectoryPtr dir1 = (*(FontPathElementPtr*) p1)->private;
FontDirectoryPtr dir2 = (*(FontPathElementPtr*) p2)->private;
const char *pri1 = NULL;
const char *pri2 = NULL;
if (dir1->attributes != NULL)
pri1 = strstr(dir1->attributes, PriorityAttribute);
if (dir2->attributes != NULL)
pri2 = strstr(dir2->attributes, PriorityAttribute);
if (pri1 == NULL && pri2 == NULL)
return 0;
else if (pri1 == NULL)
return 1;
else if (pri2 == NULL)
return -1;
else
return
atoi(pri1 + strlen(PriorityAttribute)) -
atoi(pri2 + strlen(PriorityAttribute));
}
static void
CatalogueUnrefFPEs (FontPathElementPtr fpe)
{
CataloguePtr cat = fpe->private;
FontPathElementPtr subfpe;
int i;
for (i = 0; i < cat->fpeCount; i++)
{
subfpe = cat->fpeList[i];
subfpe->refcount--;
if (subfpe->refcount == 0)
{
FontFileFreeFPE (subfpe);
free(subfpe->name);
free(subfpe);
}
}
cat->fpeCount = 0;
}
/* Rescan catalogue directory if modified timestamp has changed or
* the forceScan argument says to do it anyway (like on first load). */
static int
CatalogueRescan (FontPathElementPtr fpe, Bool forceScan)
{
CataloguePtr cat = fpe->private;
char link[MAXFONTFILENAMELEN];
char dest[MAXFONTFILENAMELEN];
char *attrib;
FontPathElementPtr subfpe;
struct stat statbuf;
const char *path;
DIR *dir;
struct dirent *entry;
int len;
int pathlen;
path = fpe->name + strlen(CataloguePrefix);
if (stat(path, &statbuf) < 0 || !S_ISDIR(statbuf.st_mode))
return BadFontPath;
if ((forceScan == FALSE) && (statbuf.st_mtime <= cat->mtime))
return Successful;
dir = opendir(path);
if (dir == NULL)
{
free(cat);
return BadFontPath;
}
CatalogueUnrefFPEs (fpe);
while (entry = readdir(dir), entry != NULL)
{
snprintf(link, sizeof link, "%s/%s", path, entry->d_name);
len = readlink(link, dest, sizeof dest - 1);
if (len < 0)
continue;
dest[len] = '\0';
if (dest[0] != '/')
{
pathlen = strlen(path);
memmove(dest + pathlen + 1, dest, sizeof dest - pathlen - 1);
memcpy(dest, path, pathlen);
memcpy(dest + pathlen, "/", 1);
len += pathlen + 1;
}
attrib = strchr(link, ':');
if (attrib && len + strlen(attrib) < sizeof dest)
{
memcpy(dest + len, attrib, strlen(attrib));
len += strlen(attrib);
}
subfpe = malloc(sizeof *subfpe);
if (subfpe == NULL)
continue;
/* The fonts returned by OpenFont will point back to the
* subfpe they come from. So set the type of the subfpe to
* what the catalogue fpe was assigned, so calls to CloseFont
* (which uses font->fpe->type) goes to CatalogueCloseFont. */
subfpe->type = fpe->type;
subfpe->name_length = len;
subfpe->name = malloc (len + 1);
if (subfpe->name == NULL)
{
free(subfpe);
continue;
}
memcpy(subfpe->name, dest, len);
subfpe->name[len] = '\0';
/* The X server will manipulate the subfpe ref counts
* associated with the font in OpenFont and CloseFont, so we
* have to make sure it's valid. */
subfpe->refcount = 1;
if (FontFileInitFPE (subfpe) != Successful)
{
free(subfpe->name);
free(subfpe);
continue;
}
if (CatalogueAddFPE(cat, subfpe) != Successful)
{
FontFileFreeFPE (subfpe);
free(subfpe);
continue;
}
}
closedir(dir);
qsort(cat->fpeList,
cat->fpeCount, sizeof cat->fpeList[0], ComparePriority);
cat->mtime = statbuf.st_mtime;
return Successful;
}
static int
CatalogueInitFPE (FontPathElementPtr fpe)
{
CataloguePtr cat;
cat = malloc(sizeof *cat);
if (cat == NULL)
return AllocError;
fpe->private = (pointer) cat;
cat->fpeCount = 0;
cat->fpeAlloc = 0;
cat->fpeList = NULL;
cat->mtime = 0;
return CatalogueRescan (fpe, TRUE);
}
static int
CatalogueResetFPE (FontPathElementPtr fpe)
{
/* Always just tell the caller to close and re-open */
return FPEResetFailed;
}
static int
CatalogueFreeFPE (FontPathElementPtr fpe)
{
CataloguePtr cat = fpe->private;
/* If the catalogue is modified while the xserver has fonts open
* from the previous subfpes, we'll unref the old subfpes when we
* reload the catalogue, and the xserver will the call FreeFPE on
* them once it drops its last reference. Thus, the FreeFPE call
* for the subfpe ends up here and we just forward it to
* FontFileFreeFPE. */
if (!CatalogueNameCheck (fpe->name))
return FontFileFreeFPE (fpe);
CatalogueUnrefFPEs (fpe);
free(cat->fpeList);
free(cat);
return Successful;
}
static int
CatalogueOpenFont (pointer client, FontPathElementPtr fpe, Mask flags,
char *name, int namelen,
fsBitmapFormat format, fsBitmapFormatMask fmask,
XID id, FontPtr *pFont, char **aliasName,
FontPtr non_cachable_font)
{
CataloguePtr cat = fpe->private;
FontPathElementPtr subfpe;
FontDirectoryPtr dir;
int i, status;
CatalogueRescan (fpe, FALSE);
for (i = 0; i < cat->fpeCount; i++)
{
subfpe = cat->fpeList[i];
dir = subfpe->private;
status = FontFileOpenFont(client, subfpe, flags,
name, namelen, format, fmask, id,
pFont, aliasName, non_cachable_font);
if (status == Successful || status == FontNameAlias)
return status;
}
return BadFontName;
}
static void
CatalogueCloseFont (FontPathElementPtr fpe, FontPtr pFont)
{
/* Note: this gets called with the actual subfpe where we found
* the font, not the catalogue fpe. */
FontFileCloseFont(fpe, pFont);
}
static int
CatalogueListFonts (pointer client, FontPathElementPtr fpe, char *pat,
int len, int max, FontNamesPtr names)
{
CataloguePtr cat = fpe->private;
FontPathElementPtr subfpe;
FontDirectoryPtr dir;
int i;
CatalogueRescan (fpe, FALSE);
for (i = 0; i < cat->fpeCount; i++)
{
subfpe = cat->fpeList[i];
dir = subfpe->private;
FontFileListFonts(client, subfpe, pat, len, max, names);
}
return Successful;
}
int
FontFileStartListFonts(pointer client, FontPathElementPtr fpe,
char *pat, int len, int max,
pointer *privatep, int mark_aliases);
typedef struct _LFWIData {
pointer *privates;
int current;
} LFWIDataRec, *LFWIDataPtr;
static int
CatalogueStartListFonts(pointer client, FontPathElementPtr fpe,
char *pat, int len, int max, pointer *privatep,
int mark_aliases)
{
CataloguePtr cat = fpe->private;
LFWIDataPtr data;
int ret, i, j;
CatalogueRescan (fpe, FALSE);
data = malloc (sizeof *data + sizeof data->privates[0] * cat->fpeCount);
if (!data)
return AllocError;
data->privates = (pointer *) (data + 1);
for (i = 0; i < cat->fpeCount; i++)
{
ret = FontFileStartListFonts(client, cat->fpeList[i], pat, len,
max, &data->privates[i], mark_aliases);
if (ret != Successful)
goto bail;
}
data->current = 0;
*privatep = (pointer) data;
return Successful;
bail:
for (j = 0; j < i; j++)
/* FIXME: we have no way to free the per-fpe privates. */;
free (data);
return AllocError;
}
static int
CatalogueStartListFontsWithInfo(pointer client, FontPathElementPtr fpe,
char *pat, int len, int max,
pointer *privatep)
{
return CatalogueStartListFonts(client, fpe, pat, len, max, privatep, 0);
}
static int
CatalogueListNextFontWithInfo(pointer client, FontPathElementPtr fpe,
char **namep, int *namelenp,
FontInfoPtr *pFontInfo,
int *numFonts, pointer private)
{
LFWIDataPtr data = private;
CataloguePtr cat = fpe->private;
int ret;
if (data->current == cat->fpeCount)
{
free(data);
return BadFontName;
}
ret = FontFileListNextFontWithInfo(client, cat->fpeList[data->current],
namep, namelenp,
pFontInfo, numFonts,
data->privates[data->current]);
if (ret == BadFontName)
{
data->current++;
return CatalogueListNextFontWithInfo(client, fpe, namep, namelenp,
pFontInfo, numFonts, private);
}
return ret;
}
static int
CatalogueStartListFontsAndAliases(pointer client, FontPathElementPtr fpe,
char *pat, int len, int max,
pointer *privatep)
{
return CatalogueStartListFonts(client, fpe, pat, len, max, privatep, 1);
}
static int
CatalogueListNextFontOrAlias(pointer client, FontPathElementPtr fpe,
char **namep, int *namelenp, char **resolvedp,
int *resolvedlenp, pointer private)
{
LFWIDataPtr data = private;
CataloguePtr cat = fpe->private;
int ret;
if (data->current == cat->fpeCount)
{
free(data);
return BadFontName;
}
ret = FontFileListNextFontOrAlias(client, cat->fpeList[data->current],
namep, namelenp,
resolvedp, resolvedlenp,
data->privates[data->current]);
if (ret == BadFontName)
{
data->current++;
return CatalogueListNextFontOrAlias(client, fpe, namep, namelenp,
resolvedp, resolvedlenp, private);
}
return ret;
}
void
CatalogueRegisterLocalFpeFunctions (void)
{
RegisterFPEFunctions(CatalogueNameCheck,
CatalogueInitFPE,
CatalogueFreeFPE,
CatalogueResetFPE,
CatalogueOpenFont,
CatalogueCloseFont,
CatalogueListFonts,
CatalogueStartListFontsWithInfo,
CatalogueListNextFontWithInfo,
NULL,
NULL,
NULL,
CatalogueStartListFontsAndAliases,
CatalogueListNextFontOrAlias,
FontFileEmptyBitmapSource);
}
#endif /* HAVE_READLINK */
|