]>
git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.7 2002/06/03 08:31:15 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
);
64 FcValueSave (FcValue v
)
68 v
.u
.s
= FcStrCopy (v
.u
.s
);
73 v
.u
.m
= FcMatrixCopy (v
.u
.m
);
78 v
.u
.c
= FcCharSetCopy ((FcCharSet
*) v
.u
.c
);
89 FcValueListDestroy (FcValueList
*l
)
94 switch (l
->value
.type
) {
96 FcStrFree ((FcChar8
*) l
->value
.u
.s
);
99 FcMatrixFree ((FcMatrix
*) l
->value
.u
.m
);
102 FcCharSetDestroy ((FcCharSet
*) l
->value
.u
.c
);
108 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
114 FcValueEqual (FcValue va
, FcValue vb
)
116 if (va
.type
!= vb
.type
)
118 if (va
.type
== FcTypeInteger
)
120 va
.type
= FcTypeDouble
;
123 if (vb
.type
== FcTypeInteger
)
125 vb
.type
= FcTypeDouble
;
128 if (va
.type
!= vb
.type
)
135 return va
.u
.i
== vb
.u
.i
;
137 return va
.u
.d
== vb
.u
.d
;
139 return FcStrCmpIgnoreCase (va
.u
.s
, vb
.u
.s
) == 0;
141 return va
.u
.b
== vb
.u
.b
;
143 return FcMatrixEqual (va
.u
.m
, vb
.u
.m
);
145 return FcCharSetEqual (va
.u
.c
, vb
.u
.c
);
147 return va
.u
.f
== vb
.u
.f
;
153 FcDoubleHash (double d
)
163 FcStringHash (const FcChar8
*s
)
170 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
175 FcValueHash (FcValue v
)
181 return (FcChar32
) v
.u
.i
;
183 return FcDoubleHash (v
.u
.d
);
185 return FcStringHash (v
.u
.s
);
187 return (FcChar32
) v
.u
.b
;
189 return (FcDoubleHash (v
.u
.m
->xx
) ^
190 FcDoubleHash (v
.u
.m
->xy
) ^
191 FcDoubleHash (v
.u
.m
->yx
) ^
192 FcDoubleHash (v
.u
.m
->yy
));
194 return (FcChar32
) v
.u
.c
->num
;
196 return FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->family_name
) ^
197 FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->style_name
);
203 FcValueListEqual (FcValueList
*la
, FcValueList
*lb
)
207 if (!FcValueEqual (la
->value
, lb
->value
))
218 FcValueListHash (FcValueList
*l
)
224 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcValueHash (l
->value
);
231 FcPatternDestroy (FcPattern
*p
)
238 for (i
= 0; i
< p
->num
; i
++)
239 FcValueListDestroy (p
->elts
[i
].values
);
244 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
249 FcMemFree (FC_MEM_PATTERN
, sizeof (FcPattern
));
254 FcPatternPosition (const FcPattern
*p
, const char *object
)
256 int low
, high
, mid
, c
;
264 mid
= (low
+ high
) >> 1;
265 c
= strcmp (p
->elts
[mid
].object
, object
);
279 FcPatternFindElt (const FcPattern
*p
, const char *object
)
281 int i
= FcPatternPosition (p
, object
);
288 FcPatternInsertElt (FcPattern
*p
, const char *object
)
293 i
= FcPatternPosition (p
, object
);
299 if (p
->num
+ 1 >= p
->size
)
301 int s
= p
->size
+ 16;
303 e
= (FcPatternElt
*) realloc (p
->elts
, s
* sizeof (FcPatternElt
));
305 e
= (FcPatternElt
*) malloc (s
* sizeof (FcPatternElt
));
310 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
311 FcMemAlloc (FC_MEM_PATELT
, s
* sizeof (FcPatternElt
));
314 p
->elts
[p
->size
].object
= 0;
315 p
->elts
[p
->size
].values
= 0;
321 memmove (p
->elts
+ i
+ 1,
323 sizeof (FcPatternElt
) *
329 p
->elts
[i
].object
= object
;
330 p
->elts
[i
].values
= 0;
337 FcPatternEqual (const FcPattern
*pa
, const FcPattern
*pb
)
341 if (pa
->num
!= pb
->num
)
343 for (i
= 0; i
< pa
->num
; i
++)
345 if (strcmp (pa
->elts
[i
].object
, pb
->elts
[i
].object
) != 0)
347 if (!FcValueListEqual (pa
->elts
[i
].values
, pb
->elts
[i
].values
))
354 FcPatternHash (const FcPattern
*p
)
359 for (i
= 0; i
< p
->num
; i
++)
361 h
= (((h
<< 1) | (h
>> 31)) ^
362 FcStringHash ((const FcChar8
*) p
->elts
[i
].object
) ^
363 FcValueListHash (p
->elts
[i
].values
));
369 FcPatternEqualSubset (const FcPattern
*pa
, const FcPattern
*pb
, const FcObjectSet
*os
)
371 FcPatternElt
*ea
, *eb
;
374 for (i
= 0; i
< os
->nobject
; i
++)
376 ea
= FcPatternFindElt (pa
, os
->objects
[i
]);
377 eb
= FcPatternFindElt (pb
, os
->objects
[i
]);
382 if (!FcValueListEqual (ea
->values
, eb
->values
))
395 FcPatternAdd (FcPattern
*p
, const char *object
, FcValue value
, FcBool append
)
398 FcValueList
*new, **prev
;
400 new = (FcValueList
*) malloc (sizeof (FcValueList
));
404 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
406 value
= FcValueSave (value
);
407 if (value
.type
== FcTypeVoid
)
413 e
= FcPatternInsertElt (p
, object
);
419 for (prev
= &e
->values
; *prev
; prev
= &(*prev
)->next
);
424 new->next
= e
->values
;
431 switch (value
.type
) {
433 FcStrFree ((FcChar8
*) value
.u
.s
);
436 FcMatrixFree ((FcMatrix
*) value
.u
.m
);
439 FcCharSetDestroy ((FcCharSet
*) value
.u
.c
);
445 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
452 FcPatternDel (FcPattern
*p
, const char *object
)
457 e
= FcPatternFindElt (p
, object
);
464 FcValueListDestroy (e
->values
);
466 /* shuffle existing ones down */
467 memmove (e
, e
+1, (p
->elts
+ p
->num
- (e
+ 1)) * sizeof (FcPatternElt
));
469 p
->elts
[p
->num
].object
= 0;
470 p
->elts
[p
->num
].values
= 0;
475 FcPatternAddInteger (FcPattern
*p
, const char *object
, int i
)
479 v
.type
= FcTypeInteger
;
481 return FcPatternAdd (p
, object
, v
, FcTrue
);
485 FcPatternAddDouble (FcPattern
*p
, const char *object
, double d
)
489 v
.type
= FcTypeDouble
;
491 return FcPatternAdd (p
, object
, v
, FcTrue
);
496 FcPatternAddString (FcPattern
*p
, const char *object
, const FcChar8
*s
)
500 v
.type
= FcTypeString
;
502 return FcPatternAdd (p
, object
, v
, FcTrue
);
506 FcPatternAddMatrix (FcPattern
*p
, const char *object
, const FcMatrix
*s
)
510 v
.type
= FcTypeMatrix
;
511 v
.u
.m
= (FcMatrix
*) s
;
512 return FcPatternAdd (p
, object
, v
, FcTrue
);
517 FcPatternAddBool (FcPattern
*p
, const char *object
, FcBool b
)
523 return FcPatternAdd (p
, object
, v
, FcTrue
);
527 FcPatternAddCharSet (FcPattern
*p
, const char *object
, const FcCharSet
*c
)
531 v
.type
= FcTypeCharSet
;
532 v
.u
.c
= (FcCharSet
*) c
;
533 return FcPatternAdd (p
, object
, v
, FcTrue
);
537 FcPatternAddFTFace (FcPattern
*p
, const char *object
, const FT_Face f
)
541 v
.type
= FcTypeFTFace
;
543 return FcPatternAdd (p
, object
, v
, FcTrue
);
547 FcPatternGet (FcPattern
*p
, const char *object
, int id
, FcValue
*v
)
552 e
= FcPatternFindElt (p
, object
);
554 return FcResultNoMatch
;
555 for (l
= e
->values
; l
; l
= l
->next
)
560 return FcResultMatch
;
568 FcPatternGetInteger (FcPattern
*p
, const char *object
, int id
, int *i
)
573 r
= FcPatternGet (p
, object
, id
, &v
);
574 if (r
!= FcResultMatch
)
584 return FcResultTypeMismatch
;
586 return FcResultMatch
;
590 FcPatternGetDouble (FcPattern
*p
, const char *object
, int id
, double *d
)
595 r
= FcPatternGet (p
, object
, id
, &v
);
596 if (r
!= FcResultMatch
)
606 return FcResultTypeMismatch
;
608 return FcResultMatch
;
612 FcPatternGetString (FcPattern
*p
, const char *object
, int id
, FcChar8
** s
)
617 r
= FcPatternGet (p
, object
, id
, &v
);
618 if (r
!= FcResultMatch
)
620 if (v
.type
!= FcTypeString
)
621 return FcResultTypeMismatch
;
622 *s
= (FcChar8
*) v
.u
.s
;
623 return FcResultMatch
;
627 FcPatternGetMatrix (FcPattern
*p
, const char *object
, int id
, FcMatrix
**m
)
632 r
= FcPatternGet (p
, object
, id
, &v
);
633 if (r
!= FcResultMatch
)
635 if (v
.type
!= FcTypeMatrix
)
636 return FcResultTypeMismatch
;
637 *m
= (FcMatrix
*) v
.u
.m
;
638 return FcResultMatch
;
643 FcPatternGetBool (FcPattern
*p
, const char *object
, int id
, FcBool
*b
)
648 r
= FcPatternGet (p
, object
, id
, &v
);
649 if (r
!= FcResultMatch
)
651 if (v
.type
!= FcTypeBool
)
652 return FcResultTypeMismatch
;
654 return FcResultMatch
;
658 FcPatternGetCharSet (FcPattern
*p
, const char *object
, int id
, FcCharSet
**c
)
663 r
= FcPatternGet (p
, object
, id
, &v
);
664 if (r
!= FcResultMatch
)
666 if (v
.type
!= FcTypeCharSet
)
667 return FcResultTypeMismatch
;
668 *c
= (FcCharSet
*) v
.u
.c
;
669 return FcResultMatch
;
673 FcPatternGetFTFace (FcPattern
*p
, const char *object
, int id
, FT_Face
*f
)
678 r
= FcPatternGet (p
, object
, id
, &v
);
679 if (r
!= FcResultMatch
)
681 if (v
.type
!= FcTypeFTFace
)
682 return FcResultTypeMismatch
;
683 *f
= (FT_Face
) v
.u
.f
;
684 return FcResultMatch
;
688 FcPatternDuplicate (FcPattern
*orig
)
694 new = FcPatternCreate ();
698 for (i
= 0; i
< orig
->num
; i
++)
700 for (l
= orig
->elts
[i
].values
; l
; l
= l
->next
)
701 if (!FcPatternAdd (new, orig
->elts
[i
].object
, l
->value
, FcTrue
))
708 FcPatternDestroy (new);
714 FcPatternReference (FcPattern
*p
)
720 FcPatternVaBuild (FcPattern
*orig
, va_list va
)
724 FcPatternVapBuild (ret
, orig
, va
);
729 FcPatternBuild (FcPattern
*orig
, ...)
734 FcPatternVapBuild (orig
, orig
, va
);