]>
git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.12 2002/08/07 01:45:59 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.
30 FcPatternCreate (void)
34 p
= (FcPattern
*) malloc (sizeof (FcPattern
));
37 FcMemAlloc (FC_MEM_PATTERN
, sizeof (FcPattern
));
46 FcValueDestroy (FcValue v
)
50 FcStrFree ((FcChar8
*) v
.u
.s
);
53 FcMatrixFree ((FcMatrix
*) v
.u
.m
);
56 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
59 FcPatternDestroy ((FcPattern
*) v
.u
.p
);
67 FcValueSave (FcValue v
)
71 v
.u
.s
= FcStrCopy (v
.u
.s
);
76 v
.u
.m
= FcMatrixCopy (v
.u
.m
);
81 v
.u
.c
= FcCharSetCopy ((FcCharSet
*) v
.u
.c
);
86 FcPatternReference ((FcPattern
*) v
.u
.p
);
95 FcValueListDestroy (FcValueList
*l
)
100 switch (l
->value
.type
) {
102 FcStrFree ((FcChar8
*) l
->value
.u
.s
);
105 FcMatrixFree ((FcMatrix
*) l
->value
.u
.m
);
108 FcCharSetDestroy ((FcCharSet
*) l
->value
.u
.c
);
111 FcPatternDestroy ((FcPattern
*) l
->value
.u
.p
);
117 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
123 FcValueEqual (FcValue va
, FcValue vb
)
125 if (va
.type
!= vb
.type
)
127 if (va
.type
== FcTypeInteger
)
129 va
.type
= FcTypeDouble
;
132 if (vb
.type
== FcTypeInteger
)
134 vb
.type
= FcTypeDouble
;
137 if (va
.type
!= vb
.type
)
144 return va
.u
.i
== vb
.u
.i
;
146 return va
.u
.d
== vb
.u
.d
;
148 return FcStrCmpIgnoreCase (va
.u
.s
, vb
.u
.s
) == 0;
150 return va
.u
.b
== vb
.u
.b
;
152 return FcMatrixEqual (va
.u
.m
, vb
.u
.m
);
154 return FcCharSetEqual (va
.u
.c
, vb
.u
.c
);
156 return va
.u
.f
== vb
.u
.f
;
158 return FcPatternEqual (va
.u
.p
, vb
.u
.p
);
164 FcDoubleHash (double d
)
174 FcStringHash (const FcChar8
*s
)
181 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
186 FcValueHash (FcValue v
)
192 return (FcChar32
) v
.u
.i
;
194 return FcDoubleHash (v
.u
.d
);
196 return FcStringHash (v
.u
.s
);
198 return (FcChar32
) v
.u
.b
;
200 return (FcDoubleHash (v
.u
.m
->xx
) ^
201 FcDoubleHash (v
.u
.m
->xy
) ^
202 FcDoubleHash (v
.u
.m
->yx
) ^
203 FcDoubleHash (v
.u
.m
->yy
));
205 return (FcChar32
) v
.u
.c
->num
;
207 return FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->family_name
) ^
208 FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->style_name
);
210 return (FcChar32
) v
.u
.p
->num
;
216 FcValueListEqual (FcValueList
*la
, FcValueList
*lb
)
220 if (!FcValueEqual (la
->value
, lb
->value
))
231 FcValueListHash (FcValueList
*l
)
237 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcValueHash (l
->value
);
244 FcPatternDestroy (FcPattern
*p
)
251 for (i
= 0; i
< p
->num
; i
++)
252 FcValueListDestroy (p
->elts
[i
].values
);
257 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
262 FcMemFree (FC_MEM_PATTERN
, sizeof (FcPattern
));
267 FcPatternPosition (const FcPattern
*p
, const char *object
)
269 int low
, high
, mid
, c
;
277 mid
= (low
+ high
) >> 1;
278 c
= strcmp (p
->elts
[mid
].object
, object
);
292 FcPatternFindElt (const FcPattern
*p
, const char *object
)
294 int i
= FcPatternPosition (p
, object
);
301 FcPatternInsertElt (FcPattern
*p
, const char *object
)
306 i
= FcPatternPosition (p
, object
);
312 if (p
->num
+ 1 >= p
->size
)
314 int s
= p
->size
+ 16;
316 e
= (FcPatternElt
*) realloc (p
->elts
, s
* sizeof (FcPatternElt
));
318 e
= (FcPatternElt
*) malloc (s
* sizeof (FcPatternElt
));
323 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
324 FcMemAlloc (FC_MEM_PATELT
, s
* sizeof (FcPatternElt
));
327 p
->elts
[p
->size
].object
= 0;
328 p
->elts
[p
->size
].values
= 0;
334 memmove (p
->elts
+ i
+ 1,
336 sizeof (FcPatternElt
) *
342 p
->elts
[i
].object
= object
;
343 p
->elts
[i
].values
= 0;
350 FcPatternEqual (const FcPattern
*pa
, const FcPattern
*pb
)
357 if (pa
->num
!= pb
->num
)
359 for (i
= 0; i
< pa
->num
; i
++)
361 if (strcmp (pa
->elts
[i
].object
, pb
->elts
[i
].object
) != 0)
363 if (!FcValueListEqual (pa
->elts
[i
].values
, pb
->elts
[i
].values
))
370 FcPatternHash (const FcPattern
*p
)
375 for (i
= 0; i
< p
->num
; i
++)
377 h
= (((h
<< 1) | (h
>> 31)) ^
378 FcStringHash ((const FcChar8
*) p
->elts
[i
].object
) ^
379 FcValueListHash (p
->elts
[i
].values
));
385 FcPatternEqualSubset (const FcPattern
*pa
, const FcPattern
*pb
, const FcObjectSet
*os
)
387 FcPatternElt
*ea
, *eb
;
390 for (i
= 0; i
< os
->nobject
; i
++)
392 ea
= FcPatternFindElt (pa
, os
->objects
[i
]);
393 eb
= FcPatternFindElt (pb
, os
->objects
[i
]);
398 if (!FcValueListEqual (ea
->values
, eb
->values
))
411 FcPatternAddWithBinding (FcPattern
*p
,
414 FcValueBinding binding
,
418 FcValueList
*new, **prev
;
420 new = (FcValueList
*) malloc (sizeof (FcValueList
));
424 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
426 value
= FcValueSave (value
);
427 if (value
.type
== FcTypeVoid
)
431 new->binding
= binding
;
434 e
= FcPatternInsertElt (p
, object
);
440 for (prev
= &e
->values
; *prev
; prev
= &(*prev
)->next
);
445 new->next
= e
->values
;
452 switch (value
.type
) {
454 FcStrFree ((FcChar8
*) value
.u
.s
);
457 FcMatrixFree ((FcMatrix
*) value
.u
.m
);
460 FcCharSetDestroy ((FcCharSet
*) value
.u
.c
);
463 FcPatternDestroy ((FcPattern
*) value
.u
.p
);
469 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
476 FcPatternAdd (FcPattern
*p
, const char *object
, FcValue value
, FcBool append
)
478 return FcPatternAddWithBinding (p
, object
, value
, FcValueBindingStrong
, append
);
482 FcPatternAddWeak (FcPattern
*p
, const char *object
, FcValue value
, FcBool append
)
484 return FcPatternAddWithBinding (p
, object
, value
, FcValueBindingWeak
, append
);
488 FcPatternDel (FcPattern
*p
, const char *object
)
493 e
= FcPatternFindElt (p
, object
);
500 FcValueListDestroy (e
->values
);
502 /* shuffle existing ones down */
503 memmove (e
, e
+1, (p
->elts
+ p
->num
- (e
+ 1)) * sizeof (FcPatternElt
));
505 p
->elts
[p
->num
].object
= 0;
506 p
->elts
[p
->num
].values
= 0;
511 FcPatternAddInteger (FcPattern
*p
, const char *object
, int i
)
515 v
.type
= FcTypeInteger
;
517 return FcPatternAdd (p
, object
, v
, FcTrue
);
521 FcPatternAddDouble (FcPattern
*p
, const char *object
, double d
)
525 v
.type
= FcTypeDouble
;
527 return FcPatternAdd (p
, object
, v
, FcTrue
);
532 FcPatternAddString (FcPattern
*p
, const char *object
, const FcChar8
*s
)
536 v
.type
= FcTypeString
;
538 return FcPatternAdd (p
, object
, v
, FcTrue
);
542 FcPatternAddMatrix (FcPattern
*p
, const char *object
, const FcMatrix
*s
)
546 v
.type
= FcTypeMatrix
;
547 v
.u
.m
= (FcMatrix
*) s
;
548 return FcPatternAdd (p
, object
, v
, FcTrue
);
553 FcPatternAddBool (FcPattern
*p
, const char *object
, FcBool b
)
559 return FcPatternAdd (p
, object
, v
, FcTrue
);
563 FcPatternAddCharSet (FcPattern
*p
, const char *object
, const FcCharSet
*c
)
567 v
.type
= FcTypeCharSet
;
568 v
.u
.c
= (FcCharSet
*) c
;
569 return FcPatternAdd (p
, object
, v
, FcTrue
);
573 FcPatternAddFTFace (FcPattern
*p
, const char *object
, const FT_Face f
)
577 v
.type
= FcTypeFTFace
;
579 return FcPatternAdd (p
, object
, v
, FcTrue
);
583 FcPatternAddPattern (FcPattern
*p
, const char *object
, const FcPattern
*pp
)
587 v
.type
= FcTypePattern
;
589 return FcPatternAdd (p
, object
, v
, FcTrue
);
593 FcPatternGet (FcPattern
*p
, const char *object
, int id
, FcValue
*v
)
598 e
= FcPatternFindElt (p
, object
);
600 return FcResultNoMatch
;
601 for (l
= e
->values
; l
; l
= l
->next
)
606 return FcResultMatch
;
614 FcPatternGetInteger (FcPattern
*p
, const char *object
, int id
, int *i
)
619 r
= FcPatternGet (p
, object
, id
, &v
);
620 if (r
!= FcResultMatch
)
630 return FcResultTypeMismatch
;
632 return FcResultMatch
;
636 FcPatternGetDouble (FcPattern
*p
, const char *object
, int id
, double *d
)
641 r
= FcPatternGet (p
, object
, id
, &v
);
642 if (r
!= FcResultMatch
)
652 return FcResultTypeMismatch
;
654 return FcResultMatch
;
658 FcPatternGetString (FcPattern
*p
, const char *object
, int id
, FcChar8
** s
)
663 r
= FcPatternGet (p
, object
, id
, &v
);
664 if (r
!= FcResultMatch
)
666 if (v
.type
!= FcTypeString
)
667 return FcResultTypeMismatch
;
668 *s
= (FcChar8
*) v
.u
.s
;
669 return FcResultMatch
;
673 FcPatternGetMatrix (FcPattern
*p
, const char *object
, int id
, FcMatrix
**m
)
678 r
= FcPatternGet (p
, object
, id
, &v
);
679 if (r
!= FcResultMatch
)
681 if (v
.type
!= FcTypeMatrix
)
682 return FcResultTypeMismatch
;
683 *m
= (FcMatrix
*) v
.u
.m
;
684 return FcResultMatch
;
689 FcPatternGetBool (FcPattern
*p
, const char *object
, int id
, FcBool
*b
)
694 r
= FcPatternGet (p
, object
, id
, &v
);
695 if (r
!= FcResultMatch
)
697 if (v
.type
!= FcTypeBool
)
698 return FcResultTypeMismatch
;
700 return FcResultMatch
;
704 FcPatternGetCharSet (FcPattern
*p
, const char *object
, int id
, FcCharSet
**c
)
709 r
= FcPatternGet (p
, object
, id
, &v
);
710 if (r
!= FcResultMatch
)
712 if (v
.type
!= FcTypeCharSet
)
713 return FcResultTypeMismatch
;
714 *c
= (FcCharSet
*) v
.u
.c
;
715 return FcResultMatch
;
719 FcPatternGetFTFace (FcPattern
*p
, const char *object
, int id
, FT_Face
*f
)
724 r
= FcPatternGet (p
, object
, id
, &v
);
725 if (r
!= FcResultMatch
)
727 if (v
.type
!= FcTypeFTFace
)
728 return FcResultTypeMismatch
;
729 *f
= (FT_Face
) v
.u
.f
;
730 return FcResultMatch
;
734 FcPatternGetPattern (FcPattern
*p
, const char *object
, int id
, FcPattern
**pp
)
739 r
= FcPatternGet (p
, object
, id
, &v
);
740 if (r
!= FcResultMatch
)
742 if (v
.type
!= FcTypePattern
)
743 return FcResultTypeMismatch
;
744 *pp
= (FcPattern
*) v
.u
.p
;
745 return FcResultMatch
;
749 FcPatternDuplicate (FcPattern
*orig
)
755 new = FcPatternCreate ();
759 for (i
= 0; i
< orig
->num
; i
++)
761 for (l
= orig
->elts
[i
].values
; l
; l
= l
->next
)
762 if (!FcPatternAdd (new, orig
->elts
[i
].object
, l
->value
, FcTrue
))
769 FcPatternDestroy (new);
775 FcPatternReference (FcPattern
*p
)
781 FcPatternVaBuild (FcPattern
*orig
, va_list va
)
785 FcPatternVapBuild (ret
, orig
, va
);
790 FcPatternBuild (FcPattern
*orig
, ...)
795 FcPatternVapBuild (orig
, orig
, va
);