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 = (char *)FcSharedStr ((FcChar8 *)object);
69 mid = (low + high) >> 1;
70 c = os->objects[mid] - object;
73 FcSharedStrFree ((FcChar8 *)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 FcSharedStrFree ((FcChar8 *)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)
215 for (i = 0; i < p->num; i++)
217 FcPatternElt *pe = &FcPatternElts(p)[i];
220 if (pe->object == FC_NAMELANG_OBJECT)
222 /* "namelang" object is the alias object to change "familylang",
223 * "stylelang" and "fullnamelang" object alltogether. it won't be
224 * available on the font pattern. so checking its availability
225 * causes no results. we should ignore it here.
229 fe = FcPatternObjectFindElt (font, pe->object);
232 if (!FcListValueListMatchAny (FcPatternEltValues(pe), /* pat elts */
233 FcPatternEltValues(fe))) /* font elts */
240 FcListMatrixHash (const FcMatrix *m)
242 int xx = (int) (m->xx * 100),
243 xy = (int) (m->xy * 100),
244 yx = (int) (m->yx * 100),
245 yy = (int) (m->yy * 100);
247 return ((FcChar32) xx) ^ ((FcChar32) xy) ^ ((FcChar32) yx) ^ ((FcChar32) yy);
251 FcListValueHash (FcValue *value)
253 FcValue v = FcValueCanonicalize(value);
258 return (FcChar32) v.u.i;
260 return (FcChar32) (int) v.u.d;
262 return FcStrHashIgnoreCase (v.u.s);
264 return (FcChar32) v.u.b;
266 return FcListMatrixHash (v.u.m);
268 return FcCharSetCount (v.u.c);
272 return FcLangSetHash (v.u.l);
278 FcListValueListHash (FcValueListPtr list)
284 h = h ^ FcListValueHash (&list->value);
285 list = FcValueListNext(list);
291 FcListPatternHash (FcPattern *font,
298 for (n = 0; n < os->nobject; n++)
300 e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[n]));
302 h = h ^ FcListValueListHash (FcPatternEltValues(e));
307 typedef struct _FcListBucket {
308 struct _FcListBucket *next;
313 #define FC_LIST_HASH_SIZE 4099
315 typedef struct _FcListHashTable {
317 FcListBucket *buckets[FC_LIST_HASH_SIZE];
321 FcListHashTableInit (FcListHashTable *table)
324 memset (table->buckets, '\0', sizeof (table->buckets));
328 FcListHashTableCleanup (FcListHashTable *table)
331 FcListBucket *bucket, *next;
333 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
335 for (bucket = table->buckets[i]; bucket; bucket = next)
338 FcPatternDestroy (bucket->pattern);
341 table->buckets[i] = 0;
347 FcGetDefaultObjectLangIndex (FcPattern *font, FcObject object, const FcChar8 *lang)
349 FcPatternElt *e = FcPatternObjectFindElt (font, object);
358 for (v = FcPatternEltValues(e), i = 0; v; v = FcValueListNext(v), ++i)
360 value = FcValueCanonicalize (&v->value);
362 if (value.type == FcTypeString)
364 FcLangResult res = FcLangCompare (value.u.s, lang);
365 if (res == FcLangEqual)
368 if (res == FcLangDifferentCountry && idx < 0)
372 /* workaround for fonts that has non-English value
373 * at the head of values.
375 res = FcLangCompare (value.u.s, (FcChar8 *)"en");
376 if (res == FcLangEqual)
383 return (idx > 0) ? idx : (defidx > 0) ? defidx : 0;
387 FcListAppend (FcListHashTable *table,
396 FcListBucket **prev, *bucket;
398 int fullnameidx = -1;
403 hash = FcListPatternHash (font, os);
404 for (prev = &table->buckets[hash % FC_LIST_HASH_SIZE];
405 (bucket = *prev); prev = &(bucket->next))
407 if (bucket->hash == hash &&
408 FcListPatternEqual (bucket->pattern, font, os))
411 bucket = (FcListBucket *) malloc (sizeof (FcListBucket));
416 bucket->pattern = FcPatternCreate ();
417 if (!bucket->pattern)
420 for (o = 0; o < os->nobject; o++)
422 if (!strcmp (os->objects[o], FC_FAMILY) || !strcmp (os->objects[o], FC_FAMILYLANG))
425 familyidx = FcGetDefaultObjectLangIndex (font, FC_FAMILYLANG_OBJECT, lang);
428 else if (!strcmp (os->objects[o], FC_FULLNAME) || !strcmp (os->objects[o], FC_FULLNAMELANG))
431 fullnameidx = FcGetDefaultObjectLangIndex (font, FC_FULLNAMELANG_OBJECT, lang);
432 defidx = fullnameidx;
434 else if (!strcmp (os->objects[o], FC_STYLE) || !strcmp (os->objects[o], FC_STYLELANG))
437 styleidx = FcGetDefaultObjectLangIndex (font, FC_STYLELANG_OBJECT, lang);
443 e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[o]));
446 for (v = FcPatternEltValues(e), idx = 0; v;
447 v = FcValueListNext(v), ++idx)
449 if (!FcPatternAdd (bucket->pattern,
451 FcValueCanonicalize(&v->value), defidx != idx))
462 FcPatternDestroy (bucket->pattern);
470 FcFontSetList (FcConfig *config,
480 FcListHashTable table;
482 FcListBucket *bucket;
487 if (!FcInitBringUptoDate ())
490 config = FcConfigGetCurrent ();
494 FcListHashTableInit (&table);
498 os = FcObjectGetSet ();
503 * Walk all available fonts adding those that
504 * match to the hash table
506 for (set = 0; set < nsets; set++)
511 for (f = 0; f < s->nfont; f++)
512 if (FcListPatternMatchAny (p, /* pattern */
513 s->fonts[f])) /* font */
517 if (FcPatternObjectGetString (p, FC_NAMELANG_OBJECT, 0, &lang) != FcResultMatch)
519 lang = FcGetDefaultLang ();
521 if (!FcListAppend (&table, s->fonts[f], os, lang))
531 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
533 if ((bucket = table.buckets[i]))
536 for (; bucket; bucket = bucket->next)
546 printf ("used: %d max: %d avg: %g\n", full, max,
547 (double) ents / FC_LIST_HASH_SIZE);
551 * Walk the hash table and build
554 ret = FcFontSetCreate ();
557 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
558 while ((bucket = table.buckets[i]))
560 if (!FcFontSetAdd (ret, bucket->pattern))
562 table.buckets[i] = bucket->next;
569 FcFontSetDestroy (ret);
571 FcListHashTableCleanup (&table);
574 FcObjectSetDestroy (os);
579 FcFontList (FcConfig *config,
588 if (!FcInitBringUptoDate ())
591 config = FcConfigGetCurrent ();
596 if (config->fonts[FcSetSystem])
597 sets[nsets++] = config->fonts[FcSetSystem];
598 if (config->fonts[FcSetApplication])
599 sets[nsets++] = config->fonts[FcSetApplication];
600 return FcFontSetList (config, sets, nsets, p, os);
603 #include "fcaliastail.h"