2 * $XFree86: xc/lib/fontconfig/src/fclist.c,v 1.5 2002/06/03 08:31:15 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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.
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.
29 FcObjectSetCreate (void)
33 os = (FcObjectSet *) malloc (sizeof (FcObjectSet));
36 FcMemAlloc (FC_MEM_OBJECTSET, sizeof (FcObjectSet));
44 FcObjectSetAdd (FcObjectSet *os, const char *object)
48 int high, low, mid, c;
50 if (os->nobject == os->sobject)
54 objects = (const char **) realloc ((void *) os->objects,
55 s * sizeof (const char *));
57 objects = (const char **) malloc (s * sizeof (const char *));
61 FcMemFree (FC_MEM_OBJECTPTR, os->sobject * sizeof (const char *));
62 FcMemAlloc (FC_MEM_OBJECTPTR, s * sizeof (const char *));
63 os->objects = objects;
66 high = os->nobject - 1;
72 mid = (low + high) >> 1;
73 c = strcmp (os->objects[mid], object);
83 memmove (os->objects + mid + 1, os->objects + mid,
84 (os->nobject - mid) * sizeof (const char *));
85 os->objects[mid] = object;
91 FcObjectSetDestroy (FcObjectSet *os)
95 FcMemFree (FC_MEM_OBJECTPTR, os->sobject * sizeof (const char *));
96 free ((void *) os->objects);
98 FcMemFree (FC_MEM_OBJECTSET, sizeof (FcObjectSet));
103 FcObjectSetVaBuild (const char *first, va_list va)
107 FcObjectSetVapBuild (ret, first, va);
112 FcObjectSetBuild (const char *first, ...)
117 va_start (va, first);
118 FcObjectSetVapBuild (os, first, va);
124 FcListValueListMatchAny (FcValueList *v1orig,
127 FcValueList *v1, *v2;
129 for (v1 = v1orig; v1; v1 = v1->next)
130 for (v2 = v2orig; v2; v2 = v2->next)
131 if (FcConfigCompareValue (v2->value, FcOpContains, v1->value))
137 FcListValueListEqual (FcValueList *v1orig,
140 FcValueList *v1, *v2;
142 for (v1 = v1orig; v1; v1 = v1->next)
144 for (v2 = v2orig; v2; v2 = v2->next)
145 if (FcConfigCompareValue (v1->value, FcOpEqual, v2->value))
150 for (v2 = v2orig; v2; v2 = v2->next)
152 for (v1 = v1orig; v1; v1 = v1->next)
153 if (FcConfigCompareValue (v1->value, FcOpEqual, v2->value))
162 FcListPatternEqual (FcPattern *p1,
167 FcPatternElt *e1, *e2;
169 for (i = 0; i < os->nobject; i++)
171 e1 = FcPatternFindElt (p1, os->objects[i]);
172 e2 = FcPatternFindElt (p2, os->objects[i]);
177 if (!FcListValueListEqual (e1->values, e2->values))
184 * FcTrue iff all objects in "p" match "font"
188 FcListPatternMatchAny (FcPattern *p,
194 for (i = 0; i < p->num; i++)
196 e = FcPatternFindElt (font, p->elts[i].object);
199 if (!FcListValueListMatchAny (p->elts[i].values, e->values))
206 FcListStringHash (const FcChar8 *s)
214 h = ((h << 3) ^ (h >> 3)) ^ c;
220 FcListMatrixHash (const FcMatrix *m)
222 int xx = (int) (m->xx * 100),
223 xy = (int) (m->xy * 100),
224 yx = (int) (m->yx * 100),
225 yy = (int) (m->yy * 100);
227 return ((FcChar32) xx) ^ ((FcChar32) xy) ^ ((FcChar32) yx) ^ ((FcChar32) yy);
231 FcListValueHash (FcValue v)
237 return (FcChar32) v.u.i;
239 return (FcChar32) (int) v.u.d;
241 return FcListStringHash (v.u.s);
243 return (FcChar32) v.u.b;
245 return FcListMatrixHash (v.u.m);
247 return FcCharSetCount (v.u.c);
249 return (FcChar32) v.u.f;
255 FcListValueListHash (FcValueList *list)
261 h = h ^ FcListValueHash (list->value);
268 FcListPatternHash (FcPattern *font,
275 for (n = 0; n < os->nobject; n++)
277 e = FcPatternFindElt (font, os->objects[n]);
279 h = h ^ FcListValueListHash (e->values);
284 typedef struct _FcListBucket {
285 struct _FcListBucket *next;
290 #define FC_LIST_HASH_SIZE 4099
292 typedef struct _FcListHashTable {
294 FcListBucket *buckets[FC_LIST_HASH_SIZE];
298 FcListHashTableInit (FcListHashTable *table)
301 memset (table->buckets, '\0', sizeof (table->buckets));
305 FcListHashTableCleanup (FcListHashTable *table)
308 FcListBucket *bucket, *next;
310 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
312 for (bucket = table->buckets[i]; bucket; bucket = next)
315 FcPatternDestroy (bucket->pattern);
316 FcMemFree (FC_MEM_LISTBUCK, sizeof (FcListBucket));
319 table->buckets[i] = 0;
325 FcListAppend (FcListHashTable *table,
333 FcListBucket **prev, *bucket;
335 hash = FcListPatternHash (font, os);
336 for (prev = &table->buckets[hash % FC_LIST_HASH_SIZE];
337 (bucket = *prev); prev = &(bucket->next))
339 if (bucket->hash == hash &&
340 FcListPatternEqual (bucket->pattern, font, os))
343 bucket = (FcListBucket *) malloc (sizeof (FcListBucket));
346 FcMemAlloc (FC_MEM_LISTBUCK, sizeof (FcListBucket));
349 bucket->pattern = FcPatternCreate ();
350 if (!bucket->pattern)
353 for (o = 0; o < os->nobject; o++)
355 e = FcPatternFindElt (font, os->objects[o]);
358 for (v = e->values; v; v = v->next)
360 if (!FcPatternAdd (bucket->pattern,
373 FcPatternDestroy (bucket->pattern);
375 FcMemFree (FC_MEM_LISTBUCK, sizeof (FcListBucket));
382 FcFontSetList (FcConfig *config,
392 FcListHashTable table;
394 FcListBucket *bucket;
398 if (!FcInitBringUptoDate ())
401 config = FcConfigGetCurrent ();
405 FcListHashTableInit (&table);
407 * Walk all available fonts adding those that
408 * match to the hash table
410 for (set = 0; set < nsets; set++)
415 for (f = 0; f < s->nfont; f++)
416 if (FcListPatternMatchAny (p, s->fonts[f]))
417 if (!FcListAppend (&table, s->fonts[f], os))
426 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
428 if ((bucket = table.buckets[i]))
431 for (; bucket; bucket = bucket->next)
441 printf ("used: %d max: %d avg: %g\n", full, max,
442 (double) ents / FC_LIST_HASH_SIZE);
446 * Walk the hash table and build
449 ret = FcFontSetCreate ();
452 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
453 while ((bucket = table.buckets[i]))
455 if (!FcFontSetAdd (ret, bucket->pattern))
457 table.buckets[i] = bucket->next;
458 FcMemFree (FC_MEM_LISTBUCK, sizeof (FcListBucket));
465 FcFontSetDestroy (ret);
467 FcListHashTableCleanup (&table);
473 FcFontList (FcConfig *config,
482 config = FcConfigGetCurrent ();
487 if (config->fonts[FcSetSystem])
488 sets[nsets++] = config->fonts[FcSetSystem];
489 if (config->fonts[FcSetApplication])
490 sets[nsets++] = config->fonts[FcSetApplication];
491 return FcFontSetList (config, sets, nsets, p, os);