]>
git.wh0rd.org - fontconfig.git/blob - src/fcpat.c
2 * $XFree86: xc/lib/fontconfig/src/fcpat.c,v 1.6 2002/05/31 23:21:25 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
));
45 FcValueDestroy (FcValue v
)
49 FcStrFree ((FcChar8
*) v
.u
.s
);
52 FcMatrixFree ((FcMatrix
*) v
.u
.m
);
55 FcCharSetDestroy ((FcCharSet
*) v
.u
.c
);
63 FcValueSave (FcValue v
)
67 v
.u
.s
= FcStrCopy (v
.u
.s
);
72 v
.u
.m
= FcMatrixCopy (v
.u
.m
);
77 v
.u
.c
= FcCharSetCopy ((FcCharSet
*) v
.u
.c
);
88 FcValueListDestroy (FcValueList
*l
)
93 switch (l
->value
.type
) {
95 FcStrFree ((FcChar8
*) l
->value
.u
.s
);
98 FcMatrixFree ((FcMatrix
*) l
->value
.u
.m
);
101 FcCharSetDestroy ((FcCharSet
*) l
->value
.u
.c
);
107 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
113 FcValueEqual (FcValue va
, FcValue vb
)
115 if (va
.type
!= vb
.type
)
117 if (va
.type
== FcTypeInteger
)
119 va
.type
= FcTypeDouble
;
122 if (vb
.type
== FcTypeInteger
)
124 vb
.type
= FcTypeDouble
;
127 if (va
.type
!= vb
.type
)
134 return va
.u
.i
== vb
.u
.i
;
136 return va
.u
.d
== vb
.u
.d
;
138 return FcStrCmpIgnoreCase (va
.u
.s
, vb
.u
.s
) == 0;
140 return va
.u
.b
== vb
.u
.b
;
142 return FcMatrixEqual (va
.u
.m
, vb
.u
.m
);
144 return FcCharSetEqual (va
.u
.c
, vb
.u
.c
);
146 return va
.u
.f
== vb
.u
.f
;
152 FcDoubleHash (double d
)
162 FcStringHash (const FcChar8
*s
)
169 h
= ((h
<< 1) | (h
>> 31)) ^ c
;
174 FcValueHash (FcValue v
)
180 return (FcChar32
) v
.u
.i
;
182 return FcDoubleHash (v
.u
.d
);
184 return FcStringHash (v
.u
.s
);
186 return (FcChar32
) v
.u
.b
;
188 return (FcDoubleHash (v
.u
.m
->xx
) ^
189 FcDoubleHash (v
.u
.m
->xy
) ^
190 FcDoubleHash (v
.u
.m
->yx
) ^
191 FcDoubleHash (v
.u
.m
->yy
));
193 return (FcChar32
) v
.u
.c
->num
;
195 return FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->family_name
) ^
196 FcStringHash ((const FcChar8
*) ((FT_Face
) v
.u
.f
)->style_name
);
202 FcValueListEqual (FcValueList
*la
, FcValueList
*lb
)
206 if (!FcValueEqual (la
->value
, lb
->value
))
217 FcValueListHash (FcValueList
*l
)
223 hash
= ((hash
<< 1) | (hash
>> 31)) ^ FcValueHash (l
->value
);
230 FcPatternDestroy (FcPattern
*p
)
234 for (i
= 0; i
< p
->num
; i
++)
235 FcValueListDestroy (p
->elts
[i
].values
);
240 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
245 FcMemFree (FC_MEM_PATTERN
, sizeof (FcPattern
));
250 FcPatternPosition (const FcPattern
*p
, const char *object
)
252 int low
, high
, mid
, c
;
260 mid
= (low
+ high
) >> 1;
261 c
= strcmp (p
->elts
[mid
].object
, object
);
275 FcPatternFindElt (const FcPattern
*p
, const char *object
)
277 int i
= FcPatternPosition (p
, object
);
284 FcPatternInsertElt (FcPattern
*p
, const char *object
)
289 i
= FcPatternPosition (p
, object
);
295 if (p
->num
+ 1 >= p
->size
)
297 int s
= p
->size
+ 16;
299 e
= (FcPatternElt
*) realloc (p
->elts
, s
* sizeof (FcPatternElt
));
301 e
= (FcPatternElt
*) malloc (s
* sizeof (FcPatternElt
));
306 FcMemFree (FC_MEM_PATELT
, p
->size
* sizeof (FcPatternElt
));
307 FcMemAlloc (FC_MEM_PATELT
, s
* sizeof (FcPatternElt
));
310 p
->elts
[p
->size
].object
= 0;
311 p
->elts
[p
->size
].values
= 0;
317 memmove (p
->elts
+ i
+ 1,
319 sizeof (FcPatternElt
) *
325 p
->elts
[i
].object
= object
;
326 p
->elts
[i
].values
= 0;
333 FcPatternEqual (const FcPattern
*pa
, const FcPattern
*pb
)
337 if (pa
->num
!= pb
->num
)
339 for (i
= 0; i
< pa
->num
; i
++)
341 if (strcmp (pa
->elts
[i
].object
, pb
->elts
[i
].object
) != 0)
343 if (!FcValueListEqual (pa
->elts
[i
].values
, pb
->elts
[i
].values
))
350 FcPatternHash (const FcPattern
*p
)
355 for (i
= 0; i
< p
->num
; i
++)
357 h
= (((h
<< 1) | (h
>> 31)) ^
358 FcStringHash ((const FcChar8
*) p
->elts
[i
].object
) ^
359 FcValueListHash (p
->elts
[i
].values
));
365 FcPatternEqualSubset (const FcPattern
*pa
, const FcPattern
*pb
, const FcObjectSet
*os
)
367 FcPatternElt
*ea
, *eb
;
370 for (i
= 0; i
< os
->nobject
; i
++)
372 ea
= FcPatternFindElt (pa
, os
->objects
[i
]);
373 eb
= FcPatternFindElt (pb
, os
->objects
[i
]);
378 if (!FcValueListEqual (ea
->values
, eb
->values
))
391 FcPatternAdd (FcPattern
*p
, const char *object
, FcValue value
, FcBool append
)
394 FcValueList
*new, **prev
;
396 new = (FcValueList
*) malloc (sizeof (FcValueList
));
400 FcMemAlloc (FC_MEM_VALLIST
, sizeof (FcValueList
));
402 value
= FcValueSave (value
);
403 if (value
.type
== FcTypeVoid
)
409 e
= FcPatternInsertElt (p
, object
);
415 for (prev
= &e
->values
; *prev
; prev
= &(*prev
)->next
);
420 new->next
= e
->values
;
427 switch (value
.type
) {
429 FcStrFree ((FcChar8
*) value
.u
.s
);
432 FcMatrixFree ((FcMatrix
*) value
.u
.m
);
435 FcCharSetDestroy ((FcCharSet
*) value
.u
.c
);
441 FcMemFree (FC_MEM_VALLIST
, sizeof (FcValueList
));
448 FcPatternDel (FcPattern
*p
, const char *object
)
453 e
= FcPatternFindElt (p
, object
);
460 FcValueListDestroy (e
->values
);
462 /* shuffle existing ones down */
463 memmove (e
, e
+1, (p
->elts
+ p
->num
- (e
+ 1)) * sizeof (FcPatternElt
));
465 p
->elts
[p
->num
].object
= 0;
466 p
->elts
[p
->num
].values
= 0;
471 FcPatternAddInteger (FcPattern
*p
, const char *object
, int i
)
475 v
.type
= FcTypeInteger
;
477 return FcPatternAdd (p
, object
, v
, FcTrue
);
481 FcPatternAddDouble (FcPattern
*p
, const char *object
, double d
)
485 v
.type
= FcTypeDouble
;
487 return FcPatternAdd (p
, object
, v
, FcTrue
);
492 FcPatternAddString (FcPattern
*p
, const char *object
, const FcChar8
*s
)
496 v
.type
= FcTypeString
;
498 return FcPatternAdd (p
, object
, v
, FcTrue
);
502 FcPatternAddMatrix (FcPattern
*p
, const char *object
, const FcMatrix
*s
)
506 v
.type
= FcTypeMatrix
;
507 v
.u
.m
= (FcMatrix
*) s
;
508 return FcPatternAdd (p
, object
, v
, FcTrue
);
513 FcPatternAddBool (FcPattern
*p
, const char *object
, FcBool b
)
519 return FcPatternAdd (p
, object
, v
, FcTrue
);
523 FcPatternAddCharSet (FcPattern
*p
, const char *object
, const FcCharSet
*c
)
527 v
.type
= FcTypeCharSet
;
528 v
.u
.c
= (FcCharSet
*) c
;
529 return FcPatternAdd (p
, object
, v
, FcTrue
);
533 FcPatternAddFTFace (FcPattern
*p
, const char *object
, const FT_Face f
)
537 v
.type
= FcTypeFTFace
;
539 return FcPatternAdd (p
, object
, v
, FcTrue
);
543 FcPatternGet (FcPattern
*p
, const char *object
, int id
, FcValue
*v
)
548 e
= FcPatternFindElt (p
, object
);
550 return FcResultNoMatch
;
551 for (l
= e
->values
; l
; l
= l
->next
)
556 return FcResultMatch
;
564 FcPatternGetInteger (FcPattern
*p
, const char *object
, int id
, int *i
)
569 r
= FcPatternGet (p
, object
, id
, &v
);
570 if (r
!= FcResultMatch
)
580 return FcResultTypeMismatch
;
582 return FcResultMatch
;
586 FcPatternGetDouble (FcPattern
*p
, const char *object
, int id
, double *d
)
591 r
= FcPatternGet (p
, object
, id
, &v
);
592 if (r
!= FcResultMatch
)
602 return FcResultTypeMismatch
;
604 return FcResultMatch
;
608 FcPatternGetString (FcPattern
*p
, const char *object
, int id
, FcChar8
** s
)
613 r
= FcPatternGet (p
, object
, id
, &v
);
614 if (r
!= FcResultMatch
)
616 if (v
.type
!= FcTypeString
)
617 return FcResultTypeMismatch
;
618 *s
= (FcChar8
*) v
.u
.s
;
619 return FcResultMatch
;
623 FcPatternGetMatrix (FcPattern
*p
, const char *object
, int id
, FcMatrix
**m
)
628 r
= FcPatternGet (p
, object
, id
, &v
);
629 if (r
!= FcResultMatch
)
631 if (v
.type
!= FcTypeMatrix
)
632 return FcResultTypeMismatch
;
633 *m
= (FcMatrix
*) v
.u
.m
;
634 return FcResultMatch
;
639 FcPatternGetBool (FcPattern
*p
, const char *object
, int id
, FcBool
*b
)
644 r
= FcPatternGet (p
, object
, id
, &v
);
645 if (r
!= FcResultMatch
)
647 if (v
.type
!= FcTypeBool
)
648 return FcResultTypeMismatch
;
650 return FcResultMatch
;
654 FcPatternGetCharSet (FcPattern
*p
, const char *object
, int id
, FcCharSet
**c
)
659 r
= FcPatternGet (p
, object
, id
, &v
);
660 if (r
!= FcResultMatch
)
662 if (v
.type
!= FcTypeCharSet
)
663 return FcResultTypeMismatch
;
664 *c
= (FcCharSet
*) v
.u
.c
;
665 return FcResultMatch
;
669 FcPatternGetFTFace (FcPattern
*p
, const char *object
, int id
, FT_Face
*f
)
674 r
= FcPatternGet (p
, object
, id
, &v
);
675 if (r
!= FcResultMatch
)
677 if (v
.type
!= FcTypeFTFace
)
678 return FcResultTypeMismatch
;
679 *f
= (FT_Face
) v
.u
.f
;
680 return FcResultMatch
;
684 FcPatternDuplicate (FcPattern
*orig
)
690 new = FcPatternCreate ();
694 for (i
= 0; i
< orig
->num
; i
++)
696 for (l
= orig
->elts
[i
].values
; l
; l
= l
->next
)
697 if (!FcPatternAdd (new, orig
->elts
[i
].object
, l
->value
, FcTrue
))
704 FcPatternDestroy (new);
710 FcPatternVaBuild (FcPattern
*orig
, va_list va
)
714 FcPatternVapBuild (ret
, orig
, va
);
719 FcPatternBuild (FcPattern
*orig
, ...)
724 FcPatternVapBuild (orig
, orig
, va
);