2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.13 2002/08/11 18:10:42 keithp Exp $
4 * Copyright © 2000 Keith Packard, member of The XFree86 Project, Inc.
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 Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD 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.
31 FcPatternCreate (void)
35 p
= (FcPattern
*) malloc (sizeof (FcPattern
));
38 FcMemAlloc (FC_MEM_PATTERN
, sizeof (FcPattern
));
47 FcValueDestroy (FcValue v
)
51 FcStrFree ((FcChar8
*) v
.u
.s
);
54 FcMatrixFree ((FcMatrix
*) v
.u
.m
);
57 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
60 FcLangSetDestroy ((FcLangSet
*) v
.u
.l
);
68 FcValueSave (FcValue v
)
72 v
.u
.s
= FcStrCopy (v
.u
.s
);
77 v
.u
.m
= FcMatrixCopy (v
.u
.m
);
82 v
.u
.c
= FcCharSetCopy ((FcCharSet
*) v
.u
.c
);
87 v
.u
.l
= FcLangSetCopy (v
.u
.l
);
98 FcValueListDestroy (FcValueList
*l
)
103 switch (l
->value
.type
) {
105 FcStrFree ((FcChar8
*) l
->value
.u
.s
);
108 FcMatrixFree ((FcMatrix
*) l
->value
.u
.m
);
111 FcCharSetDestroy ((FcCharSet
*) l
->value
.u
.c
);
114 FcLangSetDestroy ((FcLangSet
*) l
->value
.u
.l
);
120 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
126 FcValueEqual (FcValue va
, FcValue vb
)
128 if (va
.type
!= vb
.type
)
130 if (va
.type
== FcTypeInteger
)
132 va
.type
= FcTypeDouble
;
135 if (vb
.type
== FcTypeInteger
)
137 vb
.type
= FcTypeDouble
;
140 if (va
.type
!= vb
.type
)
147 return va
.u
.i
== vb
.u
.i
;
149 return va
.u
.d
== vb
.u
.d
;
151 return FcStrCmpIgnoreCase (va
.u
.s
, vb
.u
.s
) == 0;
153 return va
.u
.b
== vb
.u
.b
;
155 return FcMatrixEqual (va
.u
.m
, vb
.u
.m
);
157 return FcCharSetEqual (va
.u
.c
, vb
.u
.c
);
159 return va
.u
.f
== vb
.u
.f
;
161 return FcLangSetEqual (va
.u
.l
, vb
.u
.l
);
167 FcDoubleHash (double d
)
177 FcStringHash (const FcChar8
*s
)
184 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
189 FcValueHash (FcValue v
)
195 return (FcChar32
) v
.u
.i
;
197 return FcDoubleHash (v
.u
.d
);
199 return FcStringHash (v
.u
.s
);
201 return (FcChar32
) v
.u
.b
;
203 return (FcDoubleHash (v
.u
.m
->xx
) ^
204 FcDoubleHash (v
.u
.m
->xy
) ^
205 FcDoubleHash (v
.u
.m
->yx
) ^
206 FcDoubleHash (v
.u
.m
->yy
));
208 return (FcChar32
) v
.u
.c
->num
;
210 return FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->family_name
) ^
211 FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->style_name
);
213 return FcLangSetHash (v
.u
.l
);
219 FcValueListEqual (FcValueList
*la
, FcValueList
*lb
)
226 if (!FcValueEqual (la
->value
, lb
->value
))
237 FcValueListHash (FcValueList
*l
)
243 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcValueHash (l
->value
);
250 FcPatternDestroy (FcPattern
*p
)
254 if (p
->ref
== FC_REF_CONSTANT
|| --p
->ref
> 0)
257 for (i
= 0; i
< p
->num
; i
++)
258 FcValueListDestroy (p
->elts
[i
].values
);
263 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
268 FcMemFree (FC_MEM_PATTERN
, sizeof (FcPattern
));
272 #define FC_VALUE_LIST_HASH_SIZE 257
273 #define FC_PATTERN_HASH_SIZE 67
275 typedef struct _FcValueListEnt FcValueListEnt
;
277 struct _FcValueListEnt
{
278 FcValueListEnt
*next
;
283 static int FcValueListFrozenCount
[FcTypeLangSet
+ 1];
284 static int FcValueListFrozenBytes
[FcTypeLangSet
+ 1];
285 static char *FcValueListFrozenName
[] = {
298 FcValueListReport (void);
301 FcValueListReport (void)
305 printf ("Fc Frozen Values:\n");
306 printf ("\t%8s %9s %9s\n", "Type", "Count", "Bytes");
307 for (t
= FcTypeVoid
; t
<= FcTypeLangSet
; t
++)
308 printf ("\t%8s %9d %9d\n", FcValueListFrozenName
[t
],
309 FcValueListFrozenCount
[t
], FcValueListFrozenBytes
[t
]);
312 static FcValueListEnt
*
313 FcValueListEntCreate (FcValueList
*h
)
316 FcValueList
*l
, *new;
323 for (l
= h
; l
; l
= l
->next
)
325 if (l
->value
.type
== FcTypeString
)
326 string_size
+= strlen ((char *) l
->value
.u
.s
) + 1;
329 size
= sizeof (FcValueListEnt
) + n
* sizeof (FcValueList
) + string_size
;
330 FcValueListFrozenCount
[h
->value
.type
]++;
331 FcValueListFrozenBytes
[h
->value
.type
] += size
;
335 FcMemAlloc (FC_MEM_VALLIST
, size
);
336 e
->list
= (FcValueList
*) (e
+ 1);
337 strs
= (FcChar8
*) (e
->list
+ n
);
339 for (l
= h
; l
; l
= l
->next
)
341 if (l
->value
.type
== FcTypeString
)
343 new->value
.type
= FcTypeString
;
344 new->value
.u
.s
= strs
;
345 strcpy ((char *) strs
, (char *) l
->value
.u
.s
);
346 strs
+= strlen ((char *) strs
) + 1;
349 new->value
= FcValueSave (l
->value
);
350 new->binding
= l
->binding
;
359 static int FcValueListTotal
;
360 static int FcValueListUsed
;
363 FcValueListFreeze (FcValueList
*l
)
365 static FcValueListEnt
*hashTable
[FC_VALUE_LIST_HASH_SIZE
];
366 FcChar32 hash
= FcValueListHash (l
);
367 FcValueListEnt
**bucket
= &hashTable
[hash
% FC_VALUE_LIST_HASH_SIZE
];
371 for (ent
= *bucket
; ent
; ent
= ent
->next
)
373 if (ent
->hash
== hash
&& FcValueListEqual (ent
->list
, l
))
377 ent
= FcValueListEntCreate (l
);
389 FcPatternBaseHash (FcPattern
*b
)
391 FcChar32 hash
= b
->num
;
394 for (i
= 0; i
< b
->num
; i
++)
395 hash
= ((hash
<< 1) | (hash
>> 31)) ^ ((FcChar32
) b
->elts
[i
].values
);
399 typedef struct _FcPatternEnt FcPatternEnt
;
401 struct _FcPatternEnt
{
407 static int FcPatternTotal
;
408 static int FcPatternUsed
;
411 FcPatternBaseFreeze (FcPattern
*b
)
413 static FcPatternEnt
*hashTable
[FC_VALUE_LIST_HASH_SIZE
];
414 FcChar32 hash
= FcPatternBaseHash (b
);
415 FcPatternEnt
**bucket
= &hashTable
[hash
% FC_VALUE_LIST_HASH_SIZE
];
423 for (ent
= *bucket
; ent
; ent
= ent
->next
)
425 if (ent
->hash
== hash
&& b
->num
== ent
->pattern
.num
)
427 for (i
= 0; i
< b
->num
; i
++)
429 if (strcmp (b
->elts
[i
].object
, ent
->pattern
.elts
[i
].object
))
431 if (b
->elts
[i
].values
!= ent
->pattern
.elts
[i
].values
)
435 return &ent
->pattern
;
440 * Compute size of pattern + elts + object names
443 for (i
= 0; i
< b
->num
; i
++)
444 size_objects
+= strlen (b
->elts
[i
].object
) + 1;
446 size
= sizeof (FcPatternEnt
) + b
->num
*sizeof (FcPatternElt
) + size_objects
;
451 FcMemAlloc (FC_MEM_PATTERN
, size
);
454 ent
->pattern
.elts
= (FcPatternElt
*) (ent
+ 1);
455 ent
->pattern
.num
= b
->num
;
456 ent
->pattern
.size
= b
->num
;
457 ent
->pattern
.ref
= FC_REF_CONSTANT
;
459 objects
= (char *) (ent
->pattern
.elts
+ b
->num
);
460 for (i
= 0; i
< b
->num
; i
++)
462 ent
->pattern
.elts
[i
].values
= b
->elts
[i
].values
;
463 strcpy (objects
, b
->elts
[i
].object
);
464 ent
->pattern
.elts
[i
].object
= objects
;
465 objects
+= strlen (objects
) + 1;
471 return &ent
->pattern
;
475 FcPatternFreeze (FcPattern
*p
)
477 FcPattern
*b
, *n
= 0;
481 size
= sizeof (FcPattern
) + p
->num
* sizeof (FcPatternElt
);
482 b
= (FcPattern
*) malloc (size
);
488 b
->elts
= (FcPatternElt
*) (b
+ 1);
490 * Freeze object lists
492 for (i
= 0; i
< p
->num
; i
++)
494 b
->elts
[i
].object
= p
->elts
[i
].object
;
495 b
->elts
[i
].values
= FcValueListFreeze (p
->elts
[i
].values
);
496 if (!b
->elts
[i
].values
)
502 n
= FcPatternBaseFreeze (b
);
504 if (FcDebug() & FC_DBG_MEMORY
)
506 printf ("ValueLists: total %9d used %9d\n", FcValueListTotal
, FcValueListUsed
);
507 printf ("Patterns: total %9d used %9d\n", FcPatternTotal
, FcPatternUsed
);
513 assert (FcPatternEqual (n
, p
));
519 FcPatternPosition (const FcPattern
*p
, const char *object
)
521 int low
, high
, mid
, c
;
529 mid
= (low
+ high
) >> 1;
530 c
= strcmp (p
->elts
[mid
].object
, object
);
544 FcPatternFindElt (const FcPattern
*p
, const char *object
)
546 int i
= FcPatternPosition (p
, object
);
553 FcPatternInsertElt (FcPattern
*p
, const char *object
)
558 i
= FcPatternPosition (p
, object
);
564 if (p
->num
+ 1 >= p
->size
)
566 int s
= p
->size
+ 16;
568 e
= (FcPatternElt
*) realloc (p
->elts
, s
* sizeof (FcPatternElt
));
570 e
= (FcPatternElt
*) malloc (s
* sizeof (FcPatternElt
));
575 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
576 FcMemAlloc (FC_MEM_PATELT
, s
* sizeof (FcPatternElt
));
579 p
->elts
[p
->size
].object
= 0;
580 p
->elts
[p
->size
].values
= 0;
586 memmove (p
->elts
+ i
+ 1,
588 sizeof (FcPatternElt
) *
594 p
->elts
[i
].object
= object
;
595 p
->elts
[i
].values
= 0;
602 FcPatternEqual (const FcPattern
*pa
, const FcPattern
*pb
)
609 if (pa
->num
!= pb
->num
)
611 for (i
= 0; i
< pa
->num
; i
++)
613 if (strcmp (pa
->elts
[i
].object
, pb
->elts
[i
].object
) != 0)
615 if (!FcValueListEqual (pa
->elts
[i
].values
, pb
->elts
[i
].values
))
622 FcPatternHash (const FcPattern
*p
)
627 for (i
= 0; i
< p
->num
; i
++)
629 h
= (((h
<< 1) | (h
>> 31)) ^
630 FcStringHash ((const FcChar8
*) p
->elts
[i
].object
) ^
631 FcValueListHash (p
->elts
[i
].values
));
637 FcPatternEqualSubset (const FcPattern
*pa
, const FcPattern
*pb
, const FcObjectSet
*os
)
639 FcPatternElt
*ea
, *eb
;
642 for (i
= 0; i
< os
->nobject
; i
++)
644 ea
= FcPatternFindElt (pa
, os
->objects
[i
]);
645 eb
= FcPatternFindElt (pb
, os
->objects
[i
]);
650 if (!FcValueListEqual (ea
->values
, eb
->values
))
663 FcPatternAddWithBinding (FcPattern
*p
,
666 FcValueBinding binding
,
670 FcValueList
*new, **prev
;
672 if (p
->ref
== FC_REF_CONSTANT
)
675 new = (FcValueList
*) malloc (sizeof (FcValueList
));
679 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
681 value
= FcValueSave (value
);
682 if (value
.type
== FcTypeVoid
)
686 new->binding
= binding
;
689 e
= FcPatternInsertElt (p
, object
);
695 for (prev
= &e
->values
; *prev
; prev
= &(*prev
)->next
);
700 new->next
= e
->values
;
707 switch (value
.type
) {
709 FcStrFree ((FcChar8
*) value
.u
.s
);
712 FcMatrixFree ((FcMatrix
*) value
.u
.m
);
715 FcCharSetDestroy ((FcCharSet
*) value
.u
.c
);
718 FcLangSetDestroy ((FcLangSet
*) value
.u
.l
);
724 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
731 FcPatternAdd (FcPattern
*p
, const char *object
, FcValue value
, FcBool append
)
733 return FcPatternAddWithBinding (p
, object
, value
, FcValueBindingStrong
, append
);
737 FcPatternAddWeak (FcPattern
*p
, const char *object
, FcValue value
, FcBool append
)
739 return FcPatternAddWithBinding (p
, object
, value
, FcValueBindingWeak
, append
);
743 FcPatternDel (FcPattern
*p
, const char *object
)
748 e
= FcPatternFindElt (p
, object
);
755 FcValueListDestroy (e
->values
);
757 /* shuffle existing ones down */
758 memmove (e
, e
+1, (p
->elts
+ p
->num
- (e
+ 1)) * sizeof (FcPatternElt
));
760 p
->elts
[p
->num
].object
= 0;
761 p
->elts
[p
->num
].values
= 0;
766 FcPatternAddInteger (FcPattern
*p
, const char *object
, int i
)
770 v
.type
= FcTypeInteger
;
772 return FcPatternAdd (p
, object
, v
, FcTrue
);
776 FcPatternAddDouble (FcPattern
*p
, const char *object
, double d
)
780 v
.type
= FcTypeDouble
;
782 return FcPatternAdd (p
, object
, v
, FcTrue
);
787 FcPatternAddString (FcPattern
*p
, const char *object
, const FcChar8
*s
)
791 v
.type
= FcTypeString
;
793 return FcPatternAdd (p
, object
, v
, FcTrue
);
797 FcPatternAddMatrix (FcPattern
*p
, const char *object
, const FcMatrix
*s
)
801 v
.type
= FcTypeMatrix
;
802 v
.u
.m
= (FcMatrix
*) s
;
803 return FcPatternAdd (p
, object
, v
, FcTrue
);
808 FcPatternAddBool (FcPattern
*p
, const char *object
, FcBool b
)
814 return FcPatternAdd (p
, object
, v
, FcTrue
);
818 FcPatternAddCharSet (FcPattern
*p
, const char *object
, const FcCharSet
*c
)
822 v
.type
= FcTypeCharSet
;
823 v
.u
.c
= (FcCharSet
*) c
;
824 return FcPatternAdd (p
, object
, v
, FcTrue
);
828 FcPatternAddFTFace (FcPattern
*p
, const char *object
, const FT_Face f
)
832 v
.type
= FcTypeFTFace
;
834 return FcPatternAdd (p
, object
, v
, FcTrue
);
838 FcPatternAddLangSet (FcPattern
*p
, const char *object
, const FcLangSet
*ls
)
842 v
.type
= FcTypeLangSet
;
843 v
.u
.l
= (FcLangSet
*) ls
;
844 return FcPatternAdd (p
, object
, v
, FcTrue
);
848 FcPatternGet (FcPattern
*p
, const char *object
, int id
, FcValue
*v
)
853 e
= FcPatternFindElt (p
, object
);
855 return FcResultNoMatch
;
856 for (l
= e
->values
; l
; l
= l
->next
)
861 return FcResultMatch
;
869 FcPatternGetInteger (FcPattern
*p
, const char *object
, int id
, int *i
)
874 r
= FcPatternGet (p
, object
, id
, &v
);
875 if (r
!= FcResultMatch
)
885 return FcResultTypeMismatch
;
887 return FcResultMatch
;
891 FcPatternGetDouble (FcPattern
*p
, const char *object
, int id
, double *d
)
896 r
= FcPatternGet (p
, object
, id
, &v
);
897 if (r
!= FcResultMatch
)
907 return FcResultTypeMismatch
;
909 return FcResultMatch
;
913 FcPatternGetString (FcPattern
*p
, const char *object
, int id
, FcChar8
** s
)
918 r
= FcPatternGet (p
, object
, id
, &v
);
919 if (r
!= FcResultMatch
)
921 if (v
.type
!= FcTypeString
)
922 return FcResultTypeMismatch
;
923 *s
= (FcChar8
*) v
.u
.s
;
924 return FcResultMatch
;
928 FcPatternGetMatrix (FcPattern
*p
, const char *object
, int id
, FcMatrix
**m
)
933 r
= FcPatternGet (p
, object
, id
, &v
);
934 if (r
!= FcResultMatch
)
936 if (v
.type
!= FcTypeMatrix
)
937 return FcResultTypeMismatch
;
938 *m
= (FcMatrix
*) v
.u
.m
;
939 return FcResultMatch
;
944 FcPatternGetBool (FcPattern
*p
, const char *object
, int id
, FcBool
*b
)
949 r
= FcPatternGet (p
, object
, id
, &v
);
950 if (r
!= FcResultMatch
)
952 if (v
.type
!= FcTypeBool
)
953 return FcResultTypeMismatch
;
955 return FcResultMatch
;
959 FcPatternGetCharSet (FcPattern
*p
, const char *object
, int id
, FcCharSet
**c
)
964 r
= FcPatternGet (p
, object
, id
, &v
);
965 if (r
!= FcResultMatch
)
967 if (v
.type
!= FcTypeCharSet
)
968 return FcResultTypeMismatch
;
969 *c
= (FcCharSet
*) v
.u
.c
;
970 return FcResultMatch
;
974 FcPatternGetFTFace (FcPattern
*p
, const char *object
, int id
, FT_Face
*f
)
979 r
= FcPatternGet (p
, object
, id
, &v
);
980 if (r
!= FcResultMatch
)
982 if (v
.type
!= FcTypeFTFace
)
983 return FcResultTypeMismatch
;
984 *f
= (FT_Face
) v
.u
.f
;
985 return FcResultMatch
;
989 FcPatternGetLangSet (FcPattern
*p
, const char *object
, int id
, FcLangSet
**ls
)
994 r
= FcPatternGet (p
, object
, id
, &v
);
995 if (r
!= FcResultMatch
)
997 if (v
.type
!= FcTypeLangSet
)
998 return FcResultTypeMismatch
;
999 *ls
= (FcLangSet
*) v
.u
.l
;
1000 return FcResultMatch
;
1004 FcPatternDuplicate (FcPattern
*orig
)
1010 new = FcPatternCreate ();
1014 for (i
= 0; i
< orig
->num
; i
++)
1016 for (l
= orig
->elts
[i
].values
; l
; l
= l
->next
)
1017 if (!FcPatternAdd (new, orig
->elts
[i
].object
, l
->value
, FcTrue
))
1024 FcPatternDestroy (new);
1030 FcPatternReference (FcPattern
*p
)
1032 if (p
->ref
!= FC_REF_CONSTANT
)
1037 FcPatternVaBuild (FcPattern
*orig
, va_list va
)
1041 FcPatternVapBuild (ret
, orig
, va
);
1046 FcPatternBuild (FcPattern
*orig
, ...)
1050 va_start (va
, orig
);
1051 FcPatternVapBuild (orig
, orig
, va
);