2 * fontconfig/src/fclist.c
4 * Copyright © 2000 Keith Packard
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 the author(s) not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. The authors make no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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));
43 FcObjectSetAdd (FcObjectSet *os, const char *object)
47 int high, low, mid, c;
49 if (os->nobject == os->sobject)
53 objects = (const char **) realloc ((void *) os->objects,
54 s * sizeof (const char *));
56 objects = (const char **) malloc (s * sizeof (const char *));
59 os->objects = objects;
62 high = os->nobject - 1;
66 object = strdup (object);
69 mid = (low + high) >> 1;
70 c = 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)
97 for (i = 0; i < os->nobject; i++)
98 FcFree (os->objects[i]);
100 free ((void *) os->objects);
106 FcObjectSetVaBuild (const char *first, va_list va)
110 FcObjectSetVapBuild (ret, first, va);
115 FcObjectSetBuild (const char *first, ...)
120 va_start (va, first);
121 FcObjectSetVapBuild (os, first, va);
127 * Font must have a containing value for every value in the pattern
130 FcListValueListMatchAny (FcValueListPtr patOrig, /* pattern */
131 FcValueListPtr fntOrig) /* font */
133 FcValueListPtr pat, fnt;
135 for (pat = patOrig; pat != NULL; pat = FcValueListNext(pat))
137 for (fnt = fntOrig; fnt != NULL; fnt = FcValueListNext(fnt))
140 * make sure the font 'contains' the pattern.
141 * (OpListing is OpContains except for strings
142 * where it requires an exact match)
144 if (FcConfigCompareValue (&fnt->value,
145 FC_OP (FcOpListing, FcOpFlagIgnoreBlanks),
156 FcListValueListEqual (FcValueListPtr v1orig,
157 FcValueListPtr v2orig)
159 FcValueListPtr v1, v2;
161 for (v1 = v1orig; v1 != NULL; v1 = FcValueListNext(v1))
163 for (v2 = v2orig; v2 != NULL; v2 = FcValueListNext(v2))
164 if (FcValueEqual (FcValueCanonicalize(&(v1)->value),
165 FcValueCanonicalize(&(v2)->value)))
170 for (v2 = v2orig; v2 != NULL; v2 = FcValueListNext(v2))
172 for (v1 = v1orig; v1 != NULL; v1 = FcValueListNext(v1))
173 if (FcValueEqual (FcValueCanonicalize(&v1->value),
174 FcValueCanonicalize(&v2->value)))
183 FcListPatternEqual (FcPattern *p1,
188 FcPatternElt *e1, *e2;
190 for (i = 0; i < os->nobject; i++)
192 e1 = FcPatternObjectFindElt (p1, FcObjectFromName (os->objects[i]));
193 e2 = FcPatternObjectFindElt (p2, FcObjectFromName (os->objects[i]));
198 if (!FcListValueListEqual (FcPatternEltValues(e1),
199 FcPatternEltValues(e2)))
206 * FcTrue iff all objects in "p" match "font"
210 FcListPatternMatchAny (const FcPattern *p,
211 const FcPattern *font)
217 for (i = 0; i < p->num; i++)
219 FcPatternElt *pe = &FcPatternElts(p)[i];
222 if (pe->object == FC_NAMELANG_OBJECT)
224 /* "namelang" object is the alias object to change "familylang",
225 * "stylelang" and "fullnamelang" object alltogether. it won't be
226 * available on the font pattern. so checking its availability
227 * causes no results. we should ignore it here.
231 fe = FcPatternObjectFindElt (font, pe->object);
234 if (!FcListValueListMatchAny (FcPatternEltValues(pe), /* pat elts */
235 FcPatternEltValues(fe))) /* font elts */
242 FcListMatrixHash (const FcMatrix *m)
244 int xx = (int) (m->xx * 100),
245 xy = (int) (m->xy * 100),
246 yx = (int) (m->yx * 100),
247 yy = (int) (m->yy * 100);
249 return ((FcChar32) xx) ^ ((FcChar32) xy) ^ ((FcChar32) yx) ^ ((FcChar32) yy);
253 FcListValueHash (FcValue *value)
255 FcValue v = FcValueCanonicalize(value);
261 return (FcChar32) v.u.i;
263 return (FcChar32) (int) v.u.d;
265 return FcStrHashIgnoreCase (v.u.s);
267 return (FcChar32) v.u.b;
269 return FcListMatrixHash (v.u.m);
271 return FcCharSetCount (v.u.c);
275 return FcLangSetHash (v.u.l);
281 FcListValueListHash (FcValueListPtr list)
287 h = h ^ FcListValueHash (&list->value);
288 list = FcValueListNext(list);
294 FcListPatternHash (FcPattern *font,
301 for (n = 0; n < os->nobject; n++)
303 e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[n]));
305 h = h ^ FcListValueListHash (FcPatternEltValues(e));
310 typedef struct _FcListBucket {
311 struct _FcListBucket *next;
316 #define FC_LIST_HASH_SIZE 4099
318 typedef struct _FcListHashTable {
320 FcListBucket *buckets[FC_LIST_HASH_SIZE];
324 FcListHashTableInit (FcListHashTable *table)
327 memset (table->buckets, '\0', sizeof (table->buckets));
331 FcListHashTableCleanup (FcListHashTable *table)
334 FcListBucket *bucket, *next;
336 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
338 for (bucket = table->buckets[i]; bucket; bucket = next)
341 FcPatternDestroy (bucket->pattern);
344 table->buckets[i] = 0;
350 FcGetDefaultObjectLangIndex (FcPattern *font, FcObject object, const FcChar8 *lang)
352 FcPatternElt *e = FcPatternObjectFindElt (font, object);
361 for (v = FcPatternEltValues(e), i = 0; v; v = FcValueListNext(v), ++i)
363 value = FcValueCanonicalize (&v->value);
365 if (value.type == FcTypeString)
367 FcLangResult res = FcLangCompare (value.u.s, lang);
368 if (res == FcLangEqual)
371 if (res == FcLangDifferentCountry && idx < 0)
375 /* workaround for fonts that has non-English value
376 * at the head of values.
378 res = FcLangCompare (value.u.s, (FcChar8 *)"en");
379 if (res == FcLangEqual)
386 return (idx > 0) ? idx : (defidx > 0) ? defidx : 0;
390 FcListAppend (FcListHashTable *table,
399 FcListBucket **prev, *bucket;
401 int fullnameidx = -1;
406 hash = FcListPatternHash (font, os);
407 for (prev = &table->buckets[hash % FC_LIST_HASH_SIZE];
408 (bucket = *prev); prev = &(bucket->next))
410 if (bucket->hash == hash &&
411 FcListPatternEqual (bucket->pattern, font, os))
414 bucket = (FcListBucket *) malloc (sizeof (FcListBucket));
419 bucket->pattern = FcPatternCreate ();
420 if (!bucket->pattern)
423 for (o = 0; o < os->nobject; o++)
425 if (!strcmp (os->objects[o], FC_FAMILY) || !strcmp (os->objects[o], FC_FAMILYLANG))
428 familyidx = FcGetDefaultObjectLangIndex (font, FC_FAMILYLANG_OBJECT, lang);
431 else if (!strcmp (os->objects[o], FC_FULLNAME) || !strcmp (os->objects[o], FC_FULLNAMELANG))
434 fullnameidx = FcGetDefaultObjectLangIndex (font, FC_FULLNAMELANG_OBJECT, lang);
435 defidx = fullnameidx;
437 else if (!strcmp (os->objects[o], FC_STYLE) || !strcmp (os->objects[o], FC_STYLELANG))
440 styleidx = FcGetDefaultObjectLangIndex (font, FC_STYLELANG_OBJECT, lang);
446 e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[o]));
449 for (v = FcPatternEltValues(e), idx = 0; v;
450 v = FcValueListNext(v), ++idx)
452 if (!FcPatternAdd (bucket->pattern,
454 FcValueCanonicalize(&v->value), defidx != idx))
465 FcPatternDestroy (bucket->pattern);
473 FcFontSetList (FcConfig *config,
483 FcListHashTable table;
485 FcListBucket *bucket;
490 if (!FcInitBringUptoDate ())
493 config = FcConfigGetCurrent ();
497 FcListHashTableInit (&table);
501 os = FcObjectGetSet ();
506 * Walk all available fonts adding those that
507 * match to the hash table
509 for (set = 0; set < nsets; set++)
514 for (f = 0; f < s->nfont; f++)
515 if (FcListPatternMatchAny (p, /* pattern */
516 s->fonts[f])) /* font */
520 if (FcPatternObjectGetString (p, FC_NAMELANG_OBJECT, 0, &lang) != FcResultMatch)
522 lang = FcGetDefaultLang ();
524 if (!FcListAppend (&table, s->fonts[f], os, lang))
534 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
536 if ((bucket = table.buckets[i]))
539 for (; bucket; bucket = bucket->next)
549 printf ("used: %d max: %d avg: %g\n", full, max,
550 (double) ents / FC_LIST_HASH_SIZE);
554 * Walk the hash table and build
557 ret = FcFontSetCreate ();
560 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
561 while ((bucket = table.buckets[i]))
563 if (!FcFontSetAdd (ret, bucket->pattern))
565 table.buckets[i] = bucket->next;
572 FcFontSetDestroy (ret);
574 FcListHashTableCleanup (&table);
577 FcObjectSetDestroy (os);
582 FcFontList (FcConfig *config,
591 if (!FcInitBringUptoDate ())
594 config = FcConfigGetCurrent ();
599 if (config->fonts[FcSetSystem])
600 sets[nsets++] = config->fonts[FcSetSystem];
601 if (config->fonts[FcSetApplication])
602 sets[nsets++] = config->fonts[FcSetApplication];
603 return FcFontSetList (config, sets, nsets, p, os);
606 #include "fcaliastail.h"