/*
- * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.5 2002/05/29 22:07:33 keithp Exp $
+ * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.6 2002/05/31 23:21:25 keithp Exp $
*
* Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
*
return FcFalse;
}
+static FcChar32
+FcDoubleHash (double d)
+{
+ if (d < 0)
+ d = -d;
+ if (d > 0xffffffff)
+ d = 0xffffffff;
+ return (FcChar32) d;
+}
+
+static FcChar32
+FcStringHash (const FcChar8 *s)
+{
+ FcChar8 c;
+ FcChar32 h = 0;
+
+ if (s)
+ while ((c = *s++))
+ h = ((h << 1) | (h >> 31)) ^ c;
+ return h;
+}
+
+static FcChar32
+FcValueHash (FcValue v)
+{
+ switch (v.type) {
+ case FcTypeVoid:
+ return 0;
+ case FcTypeInteger:
+ return (FcChar32) v.u.i;
+ case FcTypeDouble:
+ return FcDoubleHash (v.u.d);
+ case FcTypeString:
+ return FcStringHash (v.u.s);
+ case FcTypeBool:
+ return (FcChar32) v.u.b;
+ case FcTypeMatrix:
+ return (FcDoubleHash (v.u.m->xx) ^
+ FcDoubleHash (v.u.m->xy) ^
+ FcDoubleHash (v.u.m->yx) ^
+ FcDoubleHash (v.u.m->yy));
+ case FcTypeCharSet:
+ return (FcChar32) v.u.c->num;
+ case FcTypeFTFace:
+ return FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->family_name) ^
+ FcStringHash ((const FcChar8 *) ((FT_Face) v.u.f)->style_name);
+ }
+ return FcFalse;
+}
+
static FcBool
FcValueListEqual (FcValueList *la, FcValueList *lb)
{
return FcTrue;
}
+static FcChar32
+FcValueListHash (FcValueList *l)
+{
+ FcChar32 hash = 0;
+
+ while (l)
+ {
+ hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (l->value);
+ l = l->next;
+ }
+ return hash;
+}
+
void
FcPatternDestroy (FcPattern *p)
{
free (p);
}
-FcPatternElt *
-FcPatternFind (FcPattern *p, const char *object, FcBool insert)
+static int
+FcPatternPosition (const FcPattern *p, const char *object)
{
- int i;
- int s;
- FcPatternElt *e;
-
- int low, high;
+ int low, high, mid, c;
- /* match existing */
low = 0;
- high = p->num;
-
- while (low + 1 < high)
+ high = p->num - 1;
+ c = 1;
+ mid = 0;
+ while (low <= high)
{
- i = (low + high) >> 1;
- s = strcmp (object, p->elts[i].object);
- if (s == 0)
- return &p->elts[i];
- if (s > 0)
- low = i;
+ mid = (low + high) >> 1;
+ c = strcmp (p->elts[mid].object, object);
+ if (c == 0)
+ return mid;
+ if (c < 0)
+ low = mid + 1;
else
- high = i;
- }
-
- i = low;
- while (i < high)
- {
- s = strcmp (object, p->elts[i].object);
- if (s == 0)
- return &p->elts[i];
- if (s < 0)
- break;
- i++;
+ high = mid - 1;
}
+ if (c < 0)
+ mid++;
+ return -(mid + 1);
+}
- if (!insert)
+FcPatternElt *
+FcPatternFindElt (const FcPattern *p, const char *object)
+{
+ int i = FcPatternPosition (p, object);
+ if (i < 0)
return 0;
+ return &p->elts[i];
+}
- /* grow array */
- if (p->num + 1 >= p->size)
+FcPatternElt *
+FcPatternInsertElt (FcPattern *p, const char *object)
+{
+ int i;
+ FcPatternElt *e;
+
+ i = FcPatternPosition (p, object);
+ if (i < 0)
{
- s = p->size + 16;
- if (p->elts)
- e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
- else
- e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
- if (!e)
- return FcFalse;
- p->elts = e;
- if (p->size)
- FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
- FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
- while (p->size < s)
+ i = -i - 1;
+
+ /* grow array */
+ if (p->num + 1 >= p->size)
{
- p->elts[p->size].object = 0;
- p->elts[p->size].values = 0;
- p->size++;
+ int s = p->size + 16;
+ if (p->elts)
+ e = (FcPatternElt *) realloc (p->elts, s * sizeof (FcPatternElt));
+ else
+ e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt));
+ if (!e)
+ return FcFalse;
+ p->elts = e;
+ if (p->size)
+ FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt));
+ FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt));
+ while (p->size < s)
+ {
+ p->elts[p->size].object = 0;
+ p->elts[p->size].values = 0;
+ p->size++;
+ }
}
+
+ /* move elts up */
+ memmove (p->elts + i + 1,
+ p->elts + i,
+ sizeof (FcPatternElt) *
+ (p->num - i));
+
+ /* bump count */
+ p->num++;
+
+ p->elts[i].object = object;
+ p->elts[i].values = 0;
}
- /* move elts up */
- memmove (p->elts + i + 1,
- p->elts + i,
- sizeof (FcPatternElt) *
- (p->num - i));
-
- /* bump count */
- p->num++;
-
- p->elts[i].object = object;
- p->elts[i].values = 0;
-
return &p->elts[i];
}
FcBool
-FcPatternEqual (FcPattern *pa, FcPattern *pb)
+FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
{
int i;
return FcTrue;
}
+FcChar32
+FcPatternHash (const FcPattern *p)
+{
+ int i;
+ FcChar32 h = 0;
+
+ for (i = 0; i < p->num; i++)
+ {
+ h = (((h << 1) | (h >> 31)) ^
+ FcStringHash ((const FcChar8 *) p->elts[i].object) ^
+ FcValueListHash (p->elts[i].values));
+ }
+ return h;
+}
+
+FcBool
+FcPatternEqualSubset (const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os)
+{
+ FcPatternElt *ea, *eb;
+ int i;
+
+ for (i = 0; i < os->nobject; i++)
+ {
+ ea = FcPatternFindElt (pa, os->objects[i]);
+ eb = FcPatternFindElt (pb, os->objects[i]);
+ if (ea)
+ {
+ if (!eb)
+ return FcFalse;
+ if (!FcValueListEqual (ea->values, eb->values))
+ return FcFalse;
+ }
+ else
+ {
+ if (eb)
+ return FcFalse;
+ }
+ }
+ return FcTrue;
+}
+
FcBool
FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
{
new->value = value;
new->next = 0;
- e = FcPatternFind (p, object, FcTrue);
+ e = FcPatternInsertElt (p, object);
if (!e)
goto bail2;
FcPatternElt *e;
int i;
- e = FcPatternFind (p, object, FcFalse);
+ e = FcPatternFindElt (p, object);
if (!e)
return FcFalse;
FcPatternElt *e;
FcValueList *l;
- e = FcPatternFind (p, object, FcFalse);
+ e = FcPatternFindElt (p, object);
if (!e)
return FcResultNoMatch;
for (l = e->values; l; l = l->next)