* PERFORMANCE OF THIS SOFTWARE.
*/
+#include "fcint.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
-#include "fcint.h"
static FcPattern ** _fcPatterns = 0;
static int fcpattern_bank_count = 0, fcpattern_ptr, fcpattern_count;
static FcBool
FcStrHashed (const FcChar8 *name);
-static const char *
-FcPatternFindFullFname (const FcPattern *p);
-
-/* If you are trying to duplicate an FcPattern which will be used for
- * rendering, be aware that (internally) you also have to use
- * FcPatternTransferFullFname to transfer the associated filename. If
- * you are copying the font (externally) using FcPatternGetString,
- * then everything's fine; this caveat only applies if you're copying
- * the bits individually. */
-
FcPattern *
FcPatternCreate (void)
{
if (p->ref == FC_REF_CONSTANT || --p->ref > 0)
return;
- if (FcPatternFindFullFname (p))
- {
- FcStrFree ((FcChar8 *)FcPatternFindFullFname (p));
- FcPatternAddFullFname (p, 0);
- }
-
for (i = 0; i < p->num; i++)
FcValueListDestroy ((FcPatternEltU(p->elts)+i)->values);
free (p);
}
-#define FC_VALUE_LIST_HASH_SIZE 257
-#define FC_PATTERN_HASH_SIZE 67
-
-typedef struct _FcValueListEnt FcValueListEnt;
-
-struct _FcValueListEnt {
- FcValueListEnt *next;
- FcValueListPtr list;
- FcChar32 hash, pad;
-};
-
-typedef union _FcValueListAlign {
- FcValueListEnt ent;
- FcValueList list;
-} FcValueListAlign;
-
-static int FcValueListFrozenCount[FcTypeLangSet + 1];
-static int FcValueListFrozenBytes[FcTypeLangSet + 1];
-static char FcValueListFrozenName[][8] = {
- "Void",
- "Integer",
- "Double",
- "String",
- "Bool",
- "Matrix",
- "CharSet",
- "FTFace",
- "LangSet"
-};
-
-void
-FcValueListReport (void);
-
-void
-FcValueListReport (void)
-{
- FcType t;
-
- printf ("Fc Frozen Values:\n");
- printf ("\t%8s %9s %9s\n", "Type", "Count", "Bytes");
- for (t = FcTypeVoid; t <= FcTypeLangSet; t++)
- printf ("\t%8s %9d %9d\n", FcValueListFrozenName[t],
- FcValueListFrozenCount[t], FcValueListFrozenBytes[t]);
-}
-
-static FcValueListEnt *
-FcValueListEntCreate (FcValueListPtr h)
-{
- FcValueListAlign *ea;
- FcValueListEnt *e;
- FcValueListPtr l;
- FcValueList *new;
- int n;
- int size;
-
- n = 0;
- for (l = h; FcValueListPtrU(l); l = FcValueListPtrU(l)->next)
- n++;
- size = sizeof (FcValueListAlign) + n * sizeof (FcValueList);
- FcValueListFrozenCount[FcValueListPtrU(h)->value.type]++;
- FcValueListFrozenBytes[FcValueListPtrU(h)->value.type] += size;
- /* this leaks for some reason */
- ea = malloc (sizeof (FcValueListAlign));
- if (!ea)
- return 0;
- new = malloc (n * sizeof (FcValueList));
- if (!new)
- return 0;
- memset(new, 0, n * sizeof (FcValueList));
- FcMemAlloc (FC_MEM_VALLIST, size);
- e = &ea->ent;
- e->list = FcValueListPtrCreateDynamic(new);
- for (l = h; FcValueListPtrU(l);
- l = FcValueListPtrU(l)->next, new++)
- {
- if ((FcValueListPtrU(l)->value.type & ~FC_STORAGE_STATIC) == FcTypeString)
- {
- new->value.type = FcTypeString;
- new->value.u.s = FcStrStaticName
- (fc_value_string(&FcValueListPtrU(l)->value));
- }
- else
- {
- new->value = FcValueSave (FcValueCanonicalize
- (&FcValueListPtrU(l)->value));
- }
- new->binding = FcValueListPtrU(l)->binding;
- if (FcValueListPtrU(FcValueListPtrU(l)->next))
- {
- new->next = FcValueListPtrCreateDynamic(new + 1);
- }
- else
- {
- new->next = FcValueListPtrCreateDynamic(0);
- }
- }
- return e;
-}
-
-static void
-FcValueListEntDestroy (FcValueListEnt *e)
-{
- FcValueListPtr l;
-
- FcValueListFrozenCount[FcValueListPtrU(e->list)->value.type]--;
-
- /* XXX: We should perform these two operations with "size" as
- computed in FcValueListEntCreate, but we don't have access to
- that value here. Without this, the FcValueListFrozenBytes
- values will be wrong as will the FcMemFree counts.
-
- FcValueListFrozenBytes[e->list->value.type] -= size;
- FcMemFree (FC_MEM_VALLIST, size);
- */
-
- for (l = e->list; FcValueListPtrU(l);
- l = FcValueListPtrU(l)->next)
- {
- if (FcValueListPtrU(l)->value.type != FcTypeString)
- FcValueDestroy (FcValueListPtrU(l)->value);
- }
- /* XXX: Are we being too chummy with the implementation here to
- free(e) when it was actually the enclosing FcValueListAlign
- that was allocated? */
- free (e);
-}
-
-static int FcValueListTotal;
-static int FcValueListUsed;
-
-static FcValueListEnt *FcValueListHashTable[FC_VALUE_LIST_HASH_SIZE];
-
-static FcValueListPtr
-FcValueListFreeze (FcValueListPtr l)
-{
- FcChar32 hash = FcValueListHash (l);
- FcValueListEnt **bucket = &FcValueListHashTable[hash % FC_VALUE_LIST_HASH_SIZE];
- FcValueListEnt *ent;
-
- FcValueListTotal++;
- for (ent = *bucket; ent; ent = ent->next)
- {
- if (ent->hash == hash && FcValueListEqual (ent->list, l))
- return ent->list;
- }
-
- ent = FcValueListEntCreate (l);
- if (!ent)
- return FcValueListPtrCreateDynamic(0);
-
- FcValueListUsed++;
- ent->hash = hash;
- ent->next = *bucket;
- *bucket = ent;
- return ent->list;
-}
-
-static void
-FcValueListThawAll (void)
-{
- int i;
- FcValueListEnt *ent, *next;
-
- for (i = 0; i < FC_VALUE_LIST_HASH_SIZE; i++)
- {
- for (ent = FcValueListHashTable[i]; ent; ent = next)
- {
- next = ent->next;
- FcValueListEntDestroy (ent);
- }
- FcValueListHashTable[i] = 0;
- }
-
- FcValueListTotal = 0;
- FcValueListUsed = 0;
-}
-
-static FcChar32
-FcPatternBaseHash (FcPattern *b)
-{
- FcChar32 hash = b->num;
- int i;
-
- for (i = 0; i < b->num; i++)
- hash = ((hash << 1) | (hash >> 31)) ^
- (long) (FcValueListPtrU((FcPatternEltU(b->elts)+i)->values));
- return hash;
-}
-
-typedef struct _FcPatternEnt FcPatternEnt;
-
-struct _FcPatternEnt {
- FcPatternEnt *next;
- FcChar32 hash;
- FcPattern *pattern;
-};
-
-static int FcPatternTotal;
-static int FcPatternUsed;
-
-static FcPatternEnt *FcPatternHashTable[FC_VALUE_LIST_HASH_SIZE];
-
-static FcPattern *
-FcPatternBaseFreeze (FcPattern *b)
-{
- FcPattern *ep;
- FcPatternElt *epp;
- FcChar32 hash = FcPatternBaseHash (b);
- FcPatternEnt **bucket = &FcPatternHashTable[hash % FC_VALUE_LIST_HASH_SIZE];
- FcPatternEnt *ent;
- int i;
-
- FcPatternTotal++;
- for (ent = *bucket; ent; ent = ent->next)
- {
- if (ent->hash == hash && b->num == ent->pattern->num)
- {
- for (i = 0; i < b->num; i++)
- {
- if (FcObjectPtrCompare((FcPatternEltU(b->elts)+i)->object,
- (FcPatternEltU(ent->pattern->elts)+i)->object) != 0)
- break;
- if (FcValueListPtrU((FcPatternEltU(b->elts)+i)->values) !=
- FcValueListPtrU((FcPatternEltU(ent->pattern->elts)+i)->values))
- break;
- }
- if (i == b->num)
- return ent->pattern;
- }
- }
-
- /*
- * Compute size of pattern + elts
- */
- ent = malloc (sizeof (FcPatternEnt));
- if (!ent)
- return 0;
-
- FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPatternEnt));
- FcPatternUsed++;
-
- ep = FcPatternCreate();
- if (!ep)
- return 0;
- ent->pattern = ep;
- epp = malloc(b->num * sizeof (FcPatternElt));
- if (!epp)
- goto bail;
- ep->elts = FcPatternEltPtrCreateDynamic(epp);
-
- FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt)*(b->num));
-
- ep->num = b->num;
- ep->size = b->num;
- ep->ref = FC_REF_CONSTANT;
-
- for (i = 0; i < b->num; i++)
- {
- (FcPatternEltU(ep->elts)+i)->values =
- (FcPatternEltU(b->elts)+i)->values;
- (FcPatternEltU(ep->elts)+i)->object =
- (FcPatternEltU(b->elts)+i)->object;
- }
-
- if (FcPatternFindElt (b, FC_FILE))
- FcPatternTransferFullFname (ep, b);
-
- ent->hash = hash;
- ent->next = *bucket;
- *bucket = ent;
- return ent->pattern;
- bail:
- free(ent);
- FcMemFree (FC_MEM_PATTERN, sizeof (FcPatternEnt));
- FcPatternUsed--;
- return 0;
-}
-
-static void
-FcPatternBaseThawAll (void)
-{
- int i;
- FcPatternEnt *ent, *next;
-
- for (i = 0; i < FC_VALUE_LIST_HASH_SIZE; i++)
- {
- for (ent = FcPatternHashTable[i]; ent; ent = next)
- {
- next = ent->next;
- free (ent);
- }
- FcPatternHashTable[i] = 0;
- }
-
- FcPatternTotal = 0;
- FcPatternUsed = 0;
-}
-
-FcPattern *
-FcPatternFreeze (FcPattern *p)
-{
- FcPattern *b, *n = 0;
- FcPatternElt *e;
- int i;
-
- if (p->ref == FC_REF_CONSTANT)
- return p;
-
- b = FcPatternCreate();
- if (!b)
- return 0;
-
- b->num = p->num;
- b->size = b->num;
- b->ref = 1;
-
- e = malloc(b->num * sizeof (FcPatternElt));
- if (!e)
- return 0;
- b->elts = FcPatternEltPtrCreateDynamic(e);
- FcMemAlloc (FC_MEM_PATELT, sizeof (FcPatternElt)*(b->num));
-
- /*
- * Freeze object lists
- */
- for (i = 0; i < p->num; i++)
- {
- (FcPatternEltU(b->elts)+i)->object =
- (FcPatternEltU(p->elts)+i)->object;
- (FcPatternEltU(b->elts)+i)->values =
- FcValueListFreeze((FcPatternEltU(p->elts)+i)->values);
- if (!FcValueListPtrU((FcPatternEltU(p->elts)+i)->values))
- goto bail;
- }
-
- if (FcPatternFindElt (p, FC_FILE))
- FcPatternTransferFullFname (b, p);
-
- /*
- * Freeze base
- */
- n = FcPatternBaseFreeze (b);
-#ifdef CHATTY
- if (FcDebug() & FC_DBG_MEMORY)
- {
- printf ("ValueLists: total %9d used %9d\n", FcValueListTotal, FcValueListUsed);
- printf ("Patterns: total %9d used %9d\n", FcPatternTotal, FcPatternUsed);
- }
-#endif
- bail:
- free(FcPatternEltU(b->elts));
- b->elts = FcPatternEltPtrCreateDynamic(0);
- FcMemFree (FC_MEM_PATELT, sizeof (FcPatternElt)*(b->num));
- b->num = -1;
-#ifdef DEBUG
- assert (FcPatternEqual (n, p));
-#endif
- return n;
-}
-
static int
FcPatternPosition (const FcPattern *p, const char *object)
{
return FcPatternAdd (p, object, v, FcTrue);
}
-static FcResult
-FcPatternGetFile (const FcPattern *p, const char *object, int id, FcChar8 ** s)
-{
- const char *fn, *fpath;
- FcChar8 *fname;
- int size;
-
- fn = FcPatternFindFullFname(p);
- if (fn)
- {
- *s = (FcChar8 *) fn;
- return FcResultMatch;
- }
-
- if (!p->bank)
- return FcResultMatch;
-
- fpath = FcCacheFindBankDir (p->bank);
- size = strlen((char *)fpath) + 1 + strlen ((char *)*s) + 1;
- fname = malloc (size);
- if (!fname)
- return FcResultOutOfMemory;
-
- FcMemAlloc (FC_MEM_STRING, size);
- strcpy ((char *)fname, (char *)fpath);
- strcat ((char *)fname, "/");
- strcat ((char *)fname, (char *)*s);
-
- FcPatternAddFullFname (p, (const char *)fname);
- *s = (FcChar8 *)fname;
- return FcResultMatch;
-}
-
FcResult
FcPatternGet (const FcPattern *p, const char *object, int id, FcValue *v)
{
if (!id)
{
*v = FcValueCanonicalize(&FcValueListPtrU(l)->value);
-
- /* Pull the FC_FILE trick here too. */
- if (v->type == FcTypeString &&
- FcObjectToPtr(object) == FcObjectToPtr(FC_FILE))
- return FcPatternGetFile (p, object, id, (FcChar8 **)&(v->u.s));
-
return FcResultMatch;
}
id--;
if (v.type != FcTypeString)
return FcResultTypeMismatch;
- if (FcObjectToPtr(object) == FcObjectToPtr(FC_FILE))
- return FcPatternGetFile (p, object, id, s);
-
*s = (FcChar8 *) v.u.s;
return FcResultMatch;
}
FcTrue))
goto bail1;
}
- FcPatternTransferFullFname (new, orig);
return new;
void
FcPatternFini (void)
{
- FcPatternBaseThawAll ();
- FcValueListThawAll ();
FcStrStaticNameFini ();
FcObjectStaticNameFini ();
}
int
FcPatternNeededBytesAlign (void)
{
- return __alignof__ (FcPattern) + __alignof__ (FcPatternElt) +
+ return fc_alignof (FcPattern) + fc_alignof (FcPatternElt) +
FcValueListNeededBytesAlign ();
}
FcValueListNeededBytesAlign (void)
{
return FcCharSetNeededBytesAlign() + FcLangSetNeededBytesAlign() +
- FcStrNeededBytesAlign() + __alignof__ (FcValueList);
+ FcStrNeededBytesAlign() + fc_alignof (FcValueList);
}
static FcBool
static int
FcStrNeededBytesAlign (void)
{
- return __alignof__ (char);
+ return fc_alignof (char);
}
static FcBool
return block_ptr;
}
-
-/* we don't store these in the FcPattern itself because
- * we don't want to serialize the directory names */
-
-/* I suppose this should be cleaned upon termination, too... */
-typedef struct _FcPatternDirMapping {
- const FcPattern *p;
- const char *fname;
-} FcPatternDirMapping;
-
-#define PATTERNDIR_HASH_SIZE 31
-static struct patternDirBucket {
- struct patternDirBucket *next;
- FcPatternDirMapping m;
-} FcPatternDirBuckets[PATTERNDIR_HASH_SIZE];
-
-void
-FcPatternAddFullFname (const FcPattern *p, const char *fname)
-{
- struct patternDirBucket *pb;
-
- /* N.B. FcPatternHash fails, since it's contents-based, not
- * address-based, and we're in the process of mutating the FcPattern. */
- for (pb = &FcPatternDirBuckets
- [((unsigned long)p / sizeof (FcPattern *)) % PATTERNDIR_HASH_SIZE];
- pb->m.p != p && pb->next;
- pb = pb->next)
- ;
-
- if (pb->m.p == p)
- {
- pb->m.fname = fname;
- return;
- }
-
- pb->next = malloc (sizeof (struct patternDirBucket));
- if (!pb->next)
- return;
- FcMemAlloc (FC_MEM_CACHE, sizeof (struct patternDirBucket));
-
- pb->next->next = 0;
- pb->next->m.p = p;
- pb->next->m.fname = fname;
-}
-
-static const char *
-FcPatternFindFullFname (const FcPattern *p)
-{
- struct patternDirBucket *pb;
-
- for (pb = &FcPatternDirBuckets
- [((unsigned long)p / sizeof (FcPattern *)) % PATTERNDIR_HASH_SIZE];
- pb; pb = pb->next)
- if (pb->m.p == p)
- return pb->m.fname;
-
- return 0;
-}
-
-void
-FcPatternTransferFullFname (const FcPattern *new, const FcPattern *orig)
-{
- FcChar8 * s;
- FcPatternGetString (orig, FC_FILE, 0, &s);
- FcPatternAddFullFname (new,
- (char *)FcStrCopy
- ((FcChar8 *)FcPatternFindFullFname(orig)));
-}