]> git.wh0rd.org - fontconfig.git/blobdiff - src/fcpat.c
Add strong/weak pattern value binding, add known charsets for automatic
[fontconfig.git] / src / fcpat.c
index 68ded728c068eb406fd7b995cebce3dd04773fc5..bab44f4bb54f74da745c2e7241d6fb270abd3ed6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.6 2002/05/31 23:21:25 keithp Exp $
+ * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.9 2002/06/19 20:08:22 keithp Exp $
  *
  * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
  *
@@ -38,6 +38,7 @@ FcPatternCreate (void)
     p->num = 0;
     p->size = 0;
     p->elts = 0;
+    p->ref = 1;
     return p;
 }
 
@@ -148,6 +149,56 @@ FcValueEqual (FcValue va, FcValue vb)
     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)
 {
@@ -163,11 +214,27 @@ 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)
 {
     int                    i;
     
+    if (--p->ref > 0)
+       return;
+
     for (i = 0; i < p->num; i++)
        FcValueListDestroy (p->elts[i].values);
 
@@ -283,6 +350,21 @@ FcPatternEqual (const FcPattern *pa, const FcPattern *pb)
     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)
 {
@@ -326,6 +408,7 @@ FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append)
        goto bail1;
 
     new->value = value;
+    new->binding = FcValueBindingStrong;
     new->next = 0;
     
     e = FcPatternInsertElt (p, object);
@@ -628,6 +711,12 @@ bail0:
     return 0;
 }
 
+void
+FcPatternReference (FcPattern *p)
+{
+    p->ref++;
+}
+
 FcPattern *
 FcPatternVaBuild (FcPattern *orig, va_list va)
 {