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)
99 for (i = 0; i < os->nobject; i++)
100 FcFree (os->objects[i]);
102 free ((void *) os->objects);
109 FcObjectSetVaBuild (const char *first, va_list va)
113 FcObjectSetVapBuild (ret, first, va);
118 FcObjectSetBuild (const char *first, ...)
123 va_start (va, first);
124 FcObjectSetVapBuild (os, first, va);
130 * Font must have a containing value for every value in the pattern
133 FcListValueListMatchAny (FcValueListPtr patOrig, /* pattern */
134 FcValueListPtr fntOrig) /* font */
136 FcValueListPtr pat, fnt;
138 for (pat = patOrig; pat != NULL; pat = FcValueListNext(pat))
140 for (fnt = fntOrig; fnt != NULL; fnt = FcValueListNext(fnt))
143 * make sure the font 'contains' the pattern.
144 * (OpListing is OpContains except for strings
145 * where it requires an exact match)
147 if (FcConfigCompareValue (&fnt->value,
148 FC_OP (FcOpListing, FcOpFlagIgnoreBlanks),
159 FcListValueListEqual (FcValueListPtr v1orig,
160 FcValueListPtr v2orig)
162 FcValueListPtr v1, v2;
164 for (v1 = v1orig; v1 != NULL; v1 = FcValueListNext(v1))
166 for (v2 = v2orig; v2 != NULL; v2 = FcValueListNext(v2))
167 if (FcValueEqual (FcValueCanonicalize(&(v1)->value),
168 FcValueCanonicalize(&(v2)->value)))
173 for (v2 = v2orig; v2 != NULL; v2 = FcValueListNext(v2))
175 for (v1 = v1orig; v1 != NULL; v1 = FcValueListNext(v1))
176 if (FcValueEqual (FcValueCanonicalize(&v1->value),
177 FcValueCanonicalize(&v2->value)))
186 FcListPatternEqual (FcPattern *p1,
191 FcPatternElt *e1, *e2;
193 for (i = 0; i < os->nobject; i++)
195 e1 = FcPatternObjectFindElt (p1, FcObjectFromName (os->objects[i]));
196 e2 = FcPatternObjectFindElt (p2, FcObjectFromName (os->objects[i]));
201 if (!FcListValueListEqual (FcPatternEltValues(e1),
202 FcPatternEltValues(e2)))
209 * FcTrue iff all objects in "p" match "font"
213 FcListPatternMatchAny (const FcPattern *p,
214 const FcPattern *font)
220 for (i = 0; i < p->num; i++)
222 FcPatternElt *pe = &FcPatternElts(p)[i];
225 if (pe->object == FC_NAMELANG_OBJECT)
227 /* "namelang" object is the alias object to change "familylang",
228 * "stylelang" and "fullnamelang" object all together. it won't be
229 * available on the font pattern. so checking its availability
230 * causes no results. we should ignore it here.
234 fe = FcPatternObjectFindElt (font, pe->object);
237 if (!FcListValueListMatchAny (FcPatternEltValues(pe), /* pat elts */
238 FcPatternEltValues(fe))) /* font elts */
245 FcListMatrixHash (const FcMatrix *m)
247 int xx = (int) (m->xx * 100),
248 xy = (int) (m->xy * 100),
249 yx = (int) (m->yx * 100),
250 yy = (int) (m->yy * 100);
252 return ((FcChar32) xx) ^ ((FcChar32) xy) ^ ((FcChar32) yx) ^ ((FcChar32) yy);
256 FcListValueHash (FcValue *value)
258 FcValue v = FcValueCanonicalize(value);
264 return (FcChar32) v.u.i;
266 return (FcChar32) (int) v.u.d;
268 return FcStrHashIgnoreCase (v.u.s);
270 return (FcChar32) v.u.b;
272 return FcListMatrixHash (v.u.m);
274 return FcCharSetCount (v.u.c);
276 return (intptr_t) v.u.f;
278 return FcLangSetHash (v.u.l);
280 return FcRangeHash (v.u.r);
286 FcListValueListHash (FcValueListPtr list)
292 h = h ^ FcListValueHash (&list->value);
293 list = FcValueListNext(list);
299 FcListPatternHash (FcPattern *font,
306 for (n = 0; n < os->nobject; n++)
308 e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[n]));
310 h = h ^ FcListValueListHash (FcPatternEltValues(e));
315 typedef struct _FcListBucket {
316 struct _FcListBucket *next;
321 #define FC_LIST_HASH_SIZE 4099
323 typedef struct _FcListHashTable {
325 FcListBucket *buckets[FC_LIST_HASH_SIZE];
329 FcListHashTableInit (FcListHashTable *table)
332 memset (table->buckets, '\0', sizeof (table->buckets));
336 FcListHashTableCleanup (FcListHashTable *table)
339 FcListBucket *bucket, *next;
341 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
343 for (bucket = table->buckets[i]; bucket; bucket = next)
346 FcPatternDestroy (bucket->pattern);
349 table->buckets[i] = 0;
355 FcGetDefaultObjectLangIndex (FcPattern *font, FcObject object, const FcChar8 *lang)
357 FcPatternElt *e = FcPatternObjectFindElt (font, object);
366 for (v = FcPatternEltValues(e), i = 0; v; v = FcValueListNext(v), ++i)
368 value = FcValueCanonicalize (&v->value);
370 if (value.type == FcTypeString)
372 FcLangResult res = FcLangCompare (value.u.s, lang);
373 if (res == FcLangEqual)
376 if (res == FcLangDifferentCountry && idx < 0)
380 /* workaround for fonts that has non-English value
381 * at the head of values.
383 res = FcLangCompare (value.u.s, (FcChar8 *)"en");
384 if (res == FcLangEqual)
391 return (idx > 0) ? idx : (defidx > 0) ? defidx : 0;
395 FcListAppend (FcListHashTable *table,
404 FcListBucket **prev, *bucket;
406 int fullnameidx = -1;
411 hash = FcListPatternHash (font, os);
412 for (prev = &table->buckets[hash % FC_LIST_HASH_SIZE];
413 (bucket = *prev); prev = &(bucket->next))
415 if (bucket->hash == hash &&
416 FcListPatternEqual (bucket->pattern, font, os))
419 bucket = (FcListBucket *) malloc (sizeof (FcListBucket));
424 bucket->pattern = FcPatternCreate ();
425 if (!bucket->pattern)
428 for (o = 0; o < os->nobject; o++)
430 if (!strcmp (os->objects[o], FC_FAMILY) || !strcmp (os->objects[o], FC_FAMILYLANG))
433 familyidx = FcGetDefaultObjectLangIndex (font, FC_FAMILYLANG_OBJECT, lang);
436 else if (!strcmp (os->objects[o], FC_FULLNAME) || !strcmp (os->objects[o], FC_FULLNAMELANG))
439 fullnameidx = FcGetDefaultObjectLangIndex (font, FC_FULLNAMELANG_OBJECT, lang);
440 defidx = fullnameidx;
442 else if (!strcmp (os->objects[o], FC_STYLE) || !strcmp (os->objects[o], FC_STYLELANG))
445 styleidx = FcGetDefaultObjectLangIndex (font, FC_STYLELANG_OBJECT, lang);
451 e = FcPatternObjectFindElt (font, FcObjectFromName (os->objects[o]));
454 for (v = FcPatternEltValues(e), idx = 0; v;
455 v = FcValueListNext(v), ++idx)
457 if (!FcPatternAdd (bucket->pattern,
459 FcValueCanonicalize(&v->value), defidx != idx))
470 FcPatternDestroy (bucket->pattern);
478 FcFontSetList (FcConfig *config,
488 FcListHashTable table;
490 FcListBucket *bucket;
495 if (!FcInitBringUptoDate ())
498 config = FcConfigReference (config);
501 FcListHashTableInit (&table);
505 os = FcObjectGetSet ();
510 * Walk all available fonts adding those that
511 * match to the hash table
513 for (set = 0; set < nsets; set++)
518 for (f = 0; f < s->nfont; f++)
519 if (FcListPatternMatchAny (p, /* pattern */
520 s->fonts[f])) /* font */
524 if (FcPatternObjectGetString (p, FC_NAMELANG_OBJECT, 0, &lang) != FcResultMatch)
526 lang = FcGetDefaultLang ();
528 if (!FcListAppend (&table, s->fonts[f], os, lang))
538 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
540 if ((bucket = table.buckets[i]))
543 for (; bucket; bucket = bucket->next)
553 printf ("used: %d max: %d avg: %g\n", full, max,
554 (double) ents / FC_LIST_HASH_SIZE);
558 * Walk the hash table and build
561 ret = FcFontSetCreate ();
564 for (i = 0; i < FC_LIST_HASH_SIZE; i++)
565 while ((bucket = table.buckets[i]))
567 if (!FcFontSetAdd (ret, bucket->pattern))
569 table.buckets[i] = bucket->next;
574 FcObjectSetDestroy (os);
575 FcConfigDestroy (config);
580 FcFontSetDestroy (ret);
582 FcListHashTableCleanup (&table);
583 FcConfigDestroy (config);
586 FcObjectSetDestroy (os);
591 FcFontList (FcConfig *config,
595 FcFontSet *sets[2], *ret;
600 if (!FcInitBringUptoDate ())
603 config = FcConfigReference (config);
607 if (config->fonts[FcSetSystem])
608 sets[nsets++] = config->fonts[FcSetSystem];
609 if (config->fonts[FcSetApplication])
610 sets[nsets++] = config->fonts[FcSetApplication];
611 ret = FcFontSetList (config, sets, nsets, p, os);
612 FcConfigDestroy (config);
617 #include "fcaliastail.h"