]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright © 2000 Keith Packard | |
3 | * | |
4 | * Permission to use, copy, modify, distribute, and sell this software and its | |
5 | * documentation for any purpose is hereby granted without fee, provided that | |
6 | * the above copyright notice appear in all copies and that both that | |
7 | * copyright notice and this permission notice appear in supporting | |
8 | * documentation, and that the name of the author(s) not be used in | |
9 | * advertising or publicity pertaining to distribution of the software without | |
10 | * specific, written prior permission. The authors make no | |
11 | * representations about the suitability of this software for any purpose. It | |
12 | * is provided "as is" without express or implied warranty. | |
13 | * | |
14 | * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
15 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
16 | * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
17 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
18 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
19 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
20 | * PERFORMANCE OF THIS SOFTWARE. | |
21 | */ | |
22 | ||
23 | #include "fcint.h" | |
24 | #include "fcftint.h" | |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <assert.h> | |
28 | ||
29 | static FcBool | |
30 | FcHashOwnsName(const FcChar8 *name); | |
31 | ||
32 | FcPattern * | |
33 | FcPatternCreate (void) | |
34 | { | |
35 | FcPattern *p; | |
36 | ||
37 | p = (FcPattern *) malloc (sizeof (FcPattern)); | |
38 | if (!p) | |
39 | return 0; | |
40 | FcMemAlloc (FC_MEM_PATTERN, sizeof (FcPattern)); | |
41 | p->num = 0; | |
42 | p->size = 0; | |
43 | p->elts_offset = FcPtrToOffset (p, NULL); | |
44 | p->ref = 1; | |
45 | return p; | |
46 | } | |
47 | ||
48 | void | |
49 | FcValueDestroy (FcValue v) | |
50 | { | |
51 | switch (v.type) { | |
52 | case FcTypeString: | |
53 | if (!FcHashOwnsName(v.u.s)) | |
54 | FcStrFree ((FcChar8 *) v.u.s); | |
55 | break; | |
56 | case FcTypeMatrix: | |
57 | FcMatrixFree ((FcMatrix *) v.u.m); | |
58 | break; | |
59 | case FcTypeCharSet: | |
60 | FcCharSetDestroy ((FcCharSet *) v.u.c); | |
61 | break; | |
62 | case FcTypeLangSet: | |
63 | FcLangSetDestroy ((FcLangSet *) v.u.l); | |
64 | break; | |
65 | default: | |
66 | break; | |
67 | } | |
68 | } | |
69 | ||
70 | FcValue | |
71 | FcValueCanonicalize (const FcValue *v) | |
72 | { | |
73 | FcValue new; | |
74 | ||
75 | switch (v->type) | |
76 | { | |
77 | case FcTypeString: | |
78 | new.u.s = FcValueString(v); | |
79 | new.type = FcTypeString; | |
80 | break; | |
81 | case FcTypeCharSet: | |
82 | new.u.c = FcValueCharSet(v); | |
83 | new.type = FcTypeCharSet; | |
84 | break; | |
85 | case FcTypeLangSet: | |
86 | new.u.l = FcValueLangSet(v); | |
87 | new.type = FcTypeLangSet; | |
88 | break; | |
89 | default: | |
90 | new = *v; | |
91 | break; | |
92 | } | |
93 | return new; | |
94 | } | |
95 | ||
96 | FcValue | |
97 | FcValueSave (FcValue v) | |
98 | { | |
99 | switch (v.type) { | |
100 | case FcTypeString: | |
101 | v.u.s = FcStrStaticName (v.u.s); | |
102 | if (!v.u.s) | |
103 | v.type = FcTypeVoid; | |
104 | break; | |
105 | case FcTypeMatrix: | |
106 | v.u.m = FcMatrixCopy (v.u.m); | |
107 | if (!v.u.m) | |
108 | v.type = FcTypeVoid; | |
109 | break; | |
110 | case FcTypeCharSet: | |
111 | v.u.c = FcCharSetCopy ((FcCharSet *) v.u.c); | |
112 | if (!v.u.c) | |
113 | v.type = FcTypeVoid; | |
114 | break; | |
115 | case FcTypeLangSet: | |
116 | v.u.l = FcLangSetCopy (v.u.l); | |
117 | if (!v.u.l) | |
118 | v.type = FcTypeVoid; | |
119 | break; | |
120 | default: | |
121 | break; | |
122 | } | |
123 | return v; | |
124 | } | |
125 | ||
126 | void | |
127 | FcValueListDestroy (FcValueListPtr l) | |
128 | { | |
129 | FcValueListPtr next; | |
130 | for (; l; l = next) | |
131 | { | |
132 | switch (l->value.type) { | |
133 | case FcTypeString: | |
134 | if (!FcHashOwnsName((FcChar8 *)l->value.u.s)) | |
135 | FcStrFree ((FcChar8 *)l->value.u.s); | |
136 | break; | |
137 | case FcTypeMatrix: | |
138 | FcMatrixFree ((FcMatrix *)l->value.u.m); | |
139 | break; | |
140 | case FcTypeCharSet: | |
141 | FcCharSetDestroy | |
142 | ((FcCharSet *) (l->value.u.c)); | |
143 | break; | |
144 | case FcTypeLangSet: | |
145 | FcLangSetDestroy | |
146 | ((FcLangSet *) (l->value.u.l)); | |
147 | break; | |
148 | default: | |
149 | break; | |
150 | } | |
151 | next = FcValueListNext(l); | |
152 | FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList)); | |
153 | free(l); | |
154 | } | |
155 | } | |
156 | ||
157 | FcBool | |
158 | FcValueEqual (FcValue va, FcValue vb) | |
159 | { | |
160 | if (va.type != vb.type) | |
161 | { | |
162 | if (va.type == FcTypeInteger) | |
163 | { | |
164 | va.type = FcTypeDouble; | |
165 | va.u.d = va.u.i; | |
166 | } | |
167 | if (vb.type == FcTypeInteger) | |
168 | { | |
169 | vb.type = FcTypeDouble; | |
170 | vb.u.d = vb.u.i; | |
171 | } | |
172 | if (va.type != vb.type) | |
173 | return FcFalse; | |
174 | } | |
175 | switch (va.type) { | |
176 | case FcTypeVoid: | |
177 | return FcTrue; | |
178 | case FcTypeInteger: | |
179 | return va.u.i == vb.u.i; | |
180 | case FcTypeDouble: | |
181 | return va.u.d == vb.u.d; | |
182 | case FcTypeString: | |
183 | return FcStrCmpIgnoreCase (va.u.s, vb.u.s) == 0; | |
184 | case FcTypeBool: | |
185 | return va.u.b == vb.u.b; | |
186 | case FcTypeMatrix: | |
187 | return FcMatrixEqual (va.u.m, vb.u.m); | |
188 | case FcTypeCharSet: | |
189 | return FcCharSetEqual (va.u.c, vb.u.c); | |
190 | case FcTypeFTFace: | |
191 | return va.u.f == vb.u.f; | |
192 | case FcTypeLangSet: | |
193 | return FcLangSetEqual (va.u.l, vb.u.l); | |
194 | } | |
195 | return FcFalse; | |
196 | } | |
197 | ||
198 | static FcChar32 | |
199 | FcDoubleHash (double d) | |
200 | { | |
201 | if (d < 0) | |
202 | d = -d; | |
203 | if (d > 0xffffffff) | |
204 | d = 0xffffffff; | |
205 | return (FcChar32) d; | |
206 | } | |
207 | ||
208 | FcChar32 | |
209 | FcStringHash (const FcChar8 *s) | |
210 | { | |
211 | FcChar8 c; | |
212 | FcChar32 h = 0; | |
213 | ||
214 | if (s) | |
215 | while ((c = *s++)) | |
216 | h = ((h << 1) | (h >> 31)) ^ c; | |
217 | return h; | |
218 | } | |
219 | ||
220 | static FcChar32 | |
221 | FcValueHash (const FcValue *v) | |
222 | { | |
223 | switch (v->type) { | |
224 | case FcTypeVoid: | |
225 | return 0; | |
226 | case FcTypeInteger: | |
227 | return (FcChar32) v->u.i; | |
228 | case FcTypeDouble: | |
229 | return FcDoubleHash (v->u.d); | |
230 | case FcTypeString: | |
231 | return FcStringHash (FcValueString(v)); | |
232 | case FcTypeBool: | |
233 | return (FcChar32) v->u.b; | |
234 | case FcTypeMatrix: | |
235 | return (FcDoubleHash (v->u.m->xx) ^ | |
236 | FcDoubleHash (v->u.m->xy) ^ | |
237 | FcDoubleHash (v->u.m->yx) ^ | |
238 | FcDoubleHash (v->u.m->yy)); | |
239 | case FcTypeCharSet: | |
240 | return (FcChar32) FcValueCharSet(v)->num; | |
241 | case FcTypeFTFace: | |
242 | return FcStringHash ((const FcChar8 *) ((FT_Face) v->u.f)->family_name) ^ | |
243 | FcStringHash ((const FcChar8 *) ((FT_Face) v->u.f)->style_name); | |
244 | case FcTypeLangSet: | |
245 | return FcLangSetHash (FcValueLangSet(v)); | |
246 | } | |
247 | return FcFalse; | |
248 | } | |
249 | ||
250 | static FcBool | |
251 | FcValueListEqual (FcValueListPtr la, FcValueListPtr lb) | |
252 | { | |
253 | if (la == lb) | |
254 | return FcTrue; | |
255 | ||
256 | while (la && lb) | |
257 | { | |
258 | if (!FcValueEqual (la->value, lb->value)) | |
259 | return FcFalse; | |
260 | la = FcValueListNext(la); | |
261 | lb = FcValueListNext(lb); | |
262 | } | |
263 | if (la || lb) | |
264 | return FcFalse; | |
265 | return FcTrue; | |
266 | } | |
267 | ||
268 | static FcChar32 | |
269 | FcValueListHash (FcValueListPtr l) | |
270 | { | |
271 | FcChar32 hash = 0; | |
272 | ||
273 | for (; l; l = FcValueListNext(l)) | |
274 | { | |
275 | hash = ((hash << 1) | (hash >> 31)) ^ FcValueHash (&l->value); | |
276 | } | |
277 | return hash; | |
278 | } | |
279 | ||
280 | void | |
281 | FcPatternDestroy (FcPattern *p) | |
282 | { | |
283 | int i; | |
284 | FcPatternElt *elts; | |
285 | ||
286 | if (p->ref == FC_REF_CONSTANT) | |
287 | { | |
288 | FcCacheObjectDereference (p); | |
289 | return; | |
290 | } | |
291 | ||
292 | if (--p->ref > 0) | |
293 | return; | |
294 | ||
295 | elts = FcPatternElts (p); | |
296 | for (i = 0; i < p->num; i++) | |
297 | FcValueListDestroy (FcPatternEltValues(&elts[i])); | |
298 | ||
299 | FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt)); | |
300 | free (elts); | |
301 | FcMemFree (FC_MEM_PATTERN, sizeof (FcPattern)); | |
302 | free (p); | |
303 | } | |
304 | ||
305 | static int | |
306 | FcPatternObjectPosition (const FcPattern *p, FcObject object) | |
307 | { | |
308 | int low, high, mid, c; | |
309 | FcPatternElt *elts = FcPatternElts(p); | |
310 | ||
311 | low = 0; | |
312 | high = p->num - 1; | |
313 | c = 1; | |
314 | mid = 0; | |
315 | while (low <= high) | |
316 | { | |
317 | mid = (low + high) >> 1; | |
318 | c = elts[mid].object - object; | |
319 | if (c == 0) | |
320 | return mid; | |
321 | if (c < 0) | |
322 | low = mid + 1; | |
323 | else | |
324 | high = mid - 1; | |
325 | } | |
326 | if (c < 0) | |
327 | mid++; | |
328 | return -(mid + 1); | |
329 | } | |
330 | ||
331 | FcPatternElt * | |
332 | FcPatternObjectFindElt (const FcPattern *p, FcObject object) | |
333 | { | |
334 | int i = FcPatternObjectPosition (p, object); | |
335 | if (i < 0) | |
336 | return 0; | |
337 | return &FcPatternElts(p)[i]; | |
338 | } | |
339 | ||
340 | FcPatternElt * | |
341 | FcPatternObjectInsertElt (FcPattern *p, FcObject object) | |
342 | { | |
343 | int i; | |
344 | FcPatternElt *e; | |
345 | ||
346 | i = FcPatternObjectPosition (p, object); | |
347 | if (i < 0) | |
348 | { | |
349 | i = -i - 1; | |
350 | ||
351 | /* reallocate array */ | |
352 | if (p->num + 1 >= p->size) | |
353 | { | |
354 | int s = p->size + 16; | |
355 | if (p->size) | |
356 | { | |
357 | FcPatternElt *e0 = FcPatternElts(p); | |
358 | e = (FcPatternElt *) realloc (e0, s * sizeof (FcPatternElt)); | |
359 | if (!e) /* maybe it was mmapped */ | |
360 | { | |
361 | e = malloc(s * sizeof (FcPatternElt)); | |
362 | if (e) | |
363 | memcpy(e, e0, p->num * sizeof (FcPatternElt)); | |
364 | } | |
365 | } | |
366 | else | |
367 | e = (FcPatternElt *) malloc (s * sizeof (FcPatternElt)); | |
368 | if (!e) | |
369 | return FcFalse; | |
370 | p->elts_offset = FcPtrToOffset (p, e); | |
371 | if (p->size) | |
372 | FcMemFree (FC_MEM_PATELT, p->size * sizeof (FcPatternElt)); | |
373 | FcMemAlloc (FC_MEM_PATELT, s * sizeof (FcPatternElt)); | |
374 | while (p->size < s) | |
375 | { | |
376 | e[p->size].object = 0; | |
377 | e[p->size].values = NULL; | |
378 | p->size++; | |
379 | } | |
380 | } | |
381 | ||
382 | e = FcPatternElts(p); | |
383 | /* move elts up */ | |
384 | memmove (e + i + 1, | |
385 | e + i, | |
386 | sizeof (FcPatternElt) * | |
387 | (p->num - i)); | |
388 | ||
389 | /* bump count */ | |
390 | p->num++; | |
391 | ||
392 | e[i].object = object; | |
393 | e[i].values = NULL; | |
394 | } | |
395 | ||
396 | return FcPatternElts(p) + i; | |
397 | } | |
398 | ||
399 | FcBool | |
400 | FcPatternEqual (const FcPattern *pa, const FcPattern *pb) | |
401 | { | |
402 | int i; | |
403 | FcPatternElt *pae, *pbe; | |
404 | ||
405 | if (pa == pb) | |
406 | return FcTrue; | |
407 | ||
408 | if (pa->num != pb->num) | |
409 | return FcFalse; | |
410 | pae = FcPatternElts(pa); | |
411 | pbe = FcPatternElts(pb); | |
412 | for (i = 0; i < pa->num; i++) | |
413 | { | |
414 | if (pae[i].object != pbe[i].object) | |
415 | return FcFalse; | |
416 | if (!FcValueListEqual (FcPatternEltValues(&pae[i]), | |
417 | FcPatternEltValues(&pbe[i]))) | |
418 | return FcFalse; | |
419 | } | |
420 | return FcTrue; | |
421 | } | |
422 | ||
423 | FcChar32 | |
424 | FcPatternHash (const FcPattern *p) | |
425 | { | |
426 | int i; | |
427 | FcChar32 h = 0; | |
428 | FcPatternElt *pe = FcPatternElts(p); | |
429 | ||
430 | for (i = 0; i < p->num; i++) | |
431 | { | |
432 | h = (((h << 1) | (h >> 31)) ^ | |
433 | pe[i].object ^ | |
434 | FcValueListHash (FcPatternEltValues(&pe[i]))); | |
435 | } | |
436 | return h; | |
437 | } | |
438 | ||
439 | FcBool | |
440 | FcPatternEqualSubset (const FcPattern *pai, const FcPattern *pbi, const FcObjectSet *os) | |
441 | { | |
442 | FcPatternElt *ea, *eb; | |
443 | int i; | |
444 | ||
445 | for (i = 0; i < os->nobject; i++) | |
446 | { | |
447 | FcObject object = FcObjectFromName (os->objects[i]); | |
448 | ea = FcPatternObjectFindElt (pai, object); | |
449 | eb = FcPatternObjectFindElt (pbi, object); | |
450 | if (ea) | |
451 | { | |
452 | if (!eb) | |
453 | return FcFalse; | |
454 | if (!FcValueListEqual (FcPatternEltValues(ea), FcPatternEltValues(eb))) | |
455 | return FcFalse; | |
456 | } | |
457 | else | |
458 | { | |
459 | if (eb) | |
460 | return FcFalse; | |
461 | } | |
462 | } | |
463 | return FcTrue; | |
464 | } | |
465 | ||
466 | FcBool | |
467 | FcPatternObjectAddWithBinding (FcPattern *p, | |
468 | FcObject object, | |
469 | FcValue value, | |
470 | FcValueBinding binding, | |
471 | FcBool append) | |
472 | { | |
473 | FcPatternElt *e; | |
474 | FcValueListPtr new, *prev; | |
475 | ||
476 | if (p->ref == FC_REF_CONSTANT) | |
477 | goto bail0; | |
478 | ||
479 | new = malloc (sizeof (FcValueList)); | |
480 | if (!new) | |
481 | goto bail0; | |
482 | ||
483 | memset(new, 0, sizeof (FcValueList)); | |
484 | FcMemAlloc (FC_MEM_VALLIST, sizeof (FcValueList)); | |
485 | value = FcValueSave (value); | |
486 | if (value.type == FcTypeVoid) | |
487 | goto bail1; | |
488 | ||
489 | /* | |
490 | * Make sure the stored type is valid for built-in objects | |
491 | */ | |
492 | if (!FcObjectValidType (object, value.type)) | |
493 | { | |
494 | if (FcDebug() & FC_DBG_OBJTYPES) | |
495 | { | |
496 | printf ("FcPattern object %s does not accept value ", | |
497 | FcObjectName (object)); | |
498 | FcValuePrint (value); | |
499 | } | |
500 | goto bail1; | |
501 | } | |
502 | ||
503 | new->value = value; | |
504 | new->binding = binding; | |
505 | new->next = NULL; | |
506 | ||
507 | e = FcPatternObjectInsertElt (p, object); | |
508 | if (!e) | |
509 | goto bail2; | |
510 | ||
511 | if (append) | |
512 | { | |
513 | for (prev = &e->values; *prev; prev = &(*prev)->next) | |
514 | ; | |
515 | *prev = new; | |
516 | } | |
517 | else | |
518 | { | |
519 | new->next = e->values; | |
520 | e->values = new; | |
521 | } | |
522 | ||
523 | return FcTrue; | |
524 | ||
525 | bail2: | |
526 | FcValueDestroy (value); | |
527 | bail1: | |
528 | FcMemFree (FC_MEM_VALLIST, sizeof (FcValueList)); | |
529 | free (new); | |
530 | bail0: | |
531 | return FcFalse; | |
532 | } | |
533 | ||
534 | FcBool | |
535 | FcPatternObjectAdd (FcPattern *p, FcObject object, FcValue value, FcBool append) | |
536 | { | |
537 | return FcPatternObjectAddWithBinding (p, object, | |
538 | value, FcValueBindingStrong, append); | |
539 | } | |
540 | ||
541 | FcBool | |
542 | FcPatternAdd (FcPattern *p, const char *object, FcValue value, FcBool append) | |
543 | { | |
544 | return FcPatternObjectAddWithBinding (p, FcObjectFromName (object), | |
545 | value, FcValueBindingStrong, append); | |
546 | } | |
547 | ||
548 | FcBool | |
549 | FcPatternAddWeak (FcPattern *p, const char *object, FcValue value, FcBool append) | |
550 | { | |
551 | return FcPatternObjectAddWithBinding (p, FcObjectFromName (object), | |
552 | value, FcValueBindingWeak, append); | |
553 | } | |
554 | ||
555 | FcBool | |
556 | FcPatternObjectDel (FcPattern *p, FcObject object) | |
557 | { | |
558 | FcPatternElt *e; | |
559 | ||
560 | e = FcPatternObjectFindElt (p, object); | |
561 | if (!e) | |
562 | return FcFalse; | |
563 | ||
564 | /* destroy value */ | |
565 | FcValueListDestroy (e->values); | |
566 | ||
567 | /* shuffle existing ones down */ | |
568 | memmove (e, e+1, | |
569 | (FcPatternElts(p) + p->num - (e + 1)) * | |
570 | sizeof (FcPatternElt)); | |
571 | p->num--; | |
572 | e = FcPatternElts(p) + p->num; | |
573 | e->object = 0; | |
574 | e->values = NULL; | |
575 | return FcTrue; | |
576 | } | |
577 | ||
578 | FcBool | |
579 | FcPatternDel (FcPattern *p, const char *object) | |
580 | { | |
581 | return FcPatternObjectDel (p, FcObjectFromName (object)); | |
582 | } | |
583 | ||
584 | FcBool | |
585 | FcPatternRemove (FcPattern *p, const char *object, int id) | |
586 | { | |
587 | FcPatternElt *e; | |
588 | FcValueListPtr *prev, l; | |
589 | ||
590 | e = FcPatternObjectFindElt (p, FcObjectFromName (object)); | |
591 | if (!e) | |
592 | return FcFalse; | |
593 | for (prev = &e->values; (l = *prev); prev = &l->next) | |
594 | { | |
595 | if (!id) | |
596 | { | |
597 | *prev = l->next; | |
598 | l->next = NULL; | |
599 | FcValueListDestroy (l); | |
600 | if (!e->values) | |
601 | FcPatternDel (p, object); | |
602 | return FcTrue; | |
603 | } | |
604 | id--; | |
605 | } | |
606 | return FcFalse; | |
607 | } | |
608 | ||
609 | FcBool | |
610 | FcPatternObjectAddInteger (FcPattern *p, FcObject object, int i) | |
611 | { | |
612 | FcValue v; | |
613 | ||
614 | v.type = FcTypeInteger; | |
615 | v.u.i = i; | |
616 | return FcPatternObjectAdd (p, object, v, FcTrue); | |
617 | } | |
618 | ||
619 | FcBool | |
620 | FcPatternAddInteger (FcPattern *p, const char *object, int i) | |
621 | { | |
622 | return FcPatternObjectAddInteger (p, FcObjectFromName (object), i); | |
623 | } | |
624 | ||
625 | FcBool | |
626 | FcPatternObjectAddDouble (FcPattern *p, FcObject object, double d) | |
627 | { | |
628 | FcValue v; | |
629 | ||
630 | v.type = FcTypeDouble; | |
631 | v.u.d = d; | |
632 | return FcPatternObjectAdd (p, object, v, FcTrue); | |
633 | } | |
634 | ||
635 | ||
636 | FcBool | |
637 | FcPatternAddDouble (FcPattern *p, const char *object, double d) | |
638 | { | |
639 | return FcPatternObjectAddDouble (p, FcObjectFromName (object), d); | |
640 | } | |
641 | ||
642 | FcBool | |
643 | FcPatternObjectAddString (FcPattern *p, FcObject object, const FcChar8 *s) | |
644 | { | |
645 | FcValue v; | |
646 | ||
647 | if (!s) | |
648 | { | |
649 | v.type = FcTypeVoid; | |
650 | v.u.s = 0; | |
651 | return FcPatternObjectAdd (p, object, v, FcTrue); | |
652 | } | |
653 | ||
654 | v.type = FcTypeString; | |
655 | v.u.s = FcStrStaticName(s); | |
656 | return FcPatternObjectAdd (p, object, v, FcTrue); | |
657 | } | |
658 | ||
659 | FcBool | |
660 | FcPatternAddString (FcPattern *p, const char *object, const FcChar8 *s) | |
661 | { | |
662 | return FcPatternObjectAddString (p, FcObjectFromName (object), s); | |
663 | } | |
664 | ||
665 | FcBool | |
666 | FcPatternAddMatrix (FcPattern *p, const char *object, const FcMatrix *s) | |
667 | { | |
668 | FcValue v; | |
669 | ||
670 | v.type = FcTypeMatrix; | |
671 | v.u.m = s; | |
672 | return FcPatternAdd (p, object, v, FcTrue); | |
673 | } | |
674 | ||
675 | ||
676 | FcBool | |
677 | FcPatternObjectAddBool (FcPattern *p, FcObject object, FcBool b) | |
678 | { | |
679 | FcValue v; | |
680 | ||
681 | v.type = FcTypeBool; | |
682 | v.u.b = b; | |
683 | return FcPatternObjectAdd (p, object, v, FcTrue); | |
684 | } | |
685 | ||
686 | FcBool | |
687 | FcPatternAddBool (FcPattern *p, const char *object, FcBool b) | |
688 | { | |
689 | return FcPatternObjectAddBool (p, FcObjectFromName (object), b); | |
690 | } | |
691 | ||
692 | FcBool | |
693 | FcPatternAddCharSet (FcPattern *p, const char *object, const FcCharSet *c) | |
694 | { | |
695 | FcValue v; | |
696 | ||
697 | v.type = FcTypeCharSet; | |
698 | v.u.c = (FcCharSet *)c; | |
699 | return FcPatternAdd (p, object, v, FcTrue); | |
700 | } | |
701 | ||
702 | FcBool | |
703 | FcPatternAddFTFace (FcPattern *p, const char *object, const FT_Face f) | |
704 | { | |
705 | FcValue v; | |
706 | ||
707 | v.type = FcTypeFTFace; | |
708 | v.u.f = (void *) f; | |
709 | return FcPatternAdd (p, object, v, FcTrue); | |
710 | } | |
711 | ||
712 | FcBool | |
713 | FcPatternAddLangSet (FcPattern *p, const char *object, const FcLangSet *ls) | |
714 | { | |
715 | FcValue v; | |
716 | ||
717 | v.type = FcTypeLangSet; | |
718 | v.u.l = (FcLangSet *)ls; | |
719 | return FcPatternAdd (p, object, v, FcTrue); | |
720 | } | |
721 | ||
722 | FcResult | |
723 | FcPatternObjectGet (const FcPattern *p, FcObject object, int id, FcValue *v) | |
724 | { | |
725 | FcPatternElt *e; | |
726 | FcValueListPtr l; | |
727 | ||
728 | e = FcPatternObjectFindElt (p, object); | |
729 | if (!e) | |
730 | return FcResultNoMatch; | |
731 | for (l = FcPatternEltValues(e); l; l = FcValueListNext(l)) | |
732 | { | |
733 | if (!id) | |
734 | { | |
735 | *v = FcValueCanonicalize(&l->value); | |
736 | return FcResultMatch; | |
737 | } | |
738 | id--; | |
739 | } | |
740 | return FcResultNoId; | |
741 | } | |
742 | ||
743 | FcResult | |
744 | FcPatternGet (const FcPattern *p, const char *object, int id, FcValue *v) | |
745 | { | |
746 | return FcPatternObjectGet (p, FcObjectFromName (object), id, v); | |
747 | } | |
748 | ||
749 | FcResult | |
750 | FcPatternObjectGetInteger (const FcPattern *p, FcObject object, int id, int *i) | |
751 | { | |
752 | FcValue v; | |
753 | FcResult r; | |
754 | ||
755 | r = FcPatternObjectGet (p, object, id, &v); | |
756 | if (r != FcResultMatch) | |
757 | return r; | |
758 | switch (v.type) { | |
759 | case FcTypeDouble: | |
760 | *i = (int) v.u.d; | |
761 | break; | |
762 | case FcTypeInteger: | |
763 | *i = v.u.i; | |
764 | break; | |
765 | default: | |
766 | return FcResultTypeMismatch; | |
767 | } | |
768 | return FcResultMatch; | |
769 | } | |
770 | ||
771 | FcResult | |
772 | FcPatternGetInteger (const FcPattern *p, const char *object, int id, int *i) | |
773 | { | |
774 | return FcPatternObjectGetInteger (p, FcObjectFromName (object), id, i); | |
775 | } | |
776 | ||
777 | ||
778 | FcResult | |
779 | FcPatternObjectGetDouble (const FcPattern *p, FcObject object, int id, double *d) | |
780 | { | |
781 | FcValue v; | |
782 | FcResult r; | |
783 | ||
784 | r = FcPatternObjectGet (p, object, id, &v); | |
785 | if (r != FcResultMatch) | |
786 | return r; | |
787 | switch (v.type) { | |
788 | case FcTypeDouble: | |
789 | *d = v.u.d; | |
790 | break; | |
791 | case FcTypeInteger: | |
792 | *d = (double) v.u.i; | |
793 | break; | |
794 | default: | |
795 | return FcResultTypeMismatch; | |
796 | } | |
797 | return FcResultMatch; | |
798 | } | |
799 | ||
800 | FcResult | |
801 | FcPatternGetDouble (const FcPattern *p, const char *object, int id, double *d) | |
802 | { | |
803 | return FcPatternObjectGetDouble (p, FcObjectFromName (object), id, d); | |
804 | } | |
805 | ||
806 | FcResult | |
807 | FcPatternObjectGetString (const FcPattern *p, FcObject object, int id, FcChar8 ** s) | |
808 | { | |
809 | FcValue v; | |
810 | FcResult r; | |
811 | ||
812 | r = FcPatternObjectGet (p, object, id, &v); | |
813 | if (r != FcResultMatch) | |
814 | return r; | |
815 | if (v.type != FcTypeString) | |
816 | return FcResultTypeMismatch; | |
817 | ||
818 | *s = (FcChar8 *) v.u.s; | |
819 | return FcResultMatch; | |
820 | } | |
821 | ||
822 | FcResult | |
823 | FcPatternGetString (const FcPattern *p, const char *object, int id, FcChar8 ** s) | |
824 | { | |
825 | return FcPatternObjectGetString (p, FcObjectFromName (object), id, s); | |
826 | } | |
827 | ||
828 | FcResult | |
829 | FcPatternGetMatrix(const FcPattern *p, const char *object, int id, FcMatrix **m) | |
830 | { | |
831 | FcValue v; | |
832 | FcResult r; | |
833 | ||
834 | r = FcPatternGet (p, object, id, &v); | |
835 | if (r != FcResultMatch) | |
836 | return r; | |
837 | if (v.type != FcTypeMatrix) | |
838 | return FcResultTypeMismatch; | |
839 | *m = (FcMatrix *)v.u.m; | |
840 | return FcResultMatch; | |
841 | } | |
842 | ||
843 | ||
844 | FcResult | |
845 | FcPatternGetBool(const FcPattern *p, const char *object, int id, FcBool *b) | |
846 | { | |
847 | FcValue v; | |
848 | FcResult r; | |
849 | ||
850 | r = FcPatternGet (p, object, id, &v); | |
851 | if (r != FcResultMatch) | |
852 | return r; | |
853 | if (v.type != FcTypeBool) | |
854 | return FcResultTypeMismatch; | |
855 | *b = v.u.b; | |
856 | return FcResultMatch; | |
857 | } | |
858 | ||
859 | FcResult | |
860 | FcPatternGetCharSet(const FcPattern *p, const char *object, int id, FcCharSet **c) | |
861 | { | |
862 | FcValue v; | |
863 | FcResult r; | |
864 | ||
865 | r = FcPatternGet (p, object, id, &v); | |
866 | if (r != FcResultMatch) | |
867 | return r; | |
868 | if (v.type != FcTypeCharSet) | |
869 | return FcResultTypeMismatch; | |
870 | *c = (FcCharSet *)v.u.c; | |
871 | return FcResultMatch; | |
872 | } | |
873 | ||
874 | FcResult | |
875 | FcPatternGetFTFace(const FcPattern *p, const char *object, int id, FT_Face *f) | |
876 | { | |
877 | FcValue v; | |
878 | FcResult r; | |
879 | ||
880 | r = FcPatternGet (p, object, id, &v); | |
881 | if (r != FcResultMatch) | |
882 | return r; | |
883 | if (v.type != FcTypeFTFace) | |
884 | return FcResultTypeMismatch; | |
885 | *f = (FT_Face) v.u.f; | |
886 | return FcResultMatch; | |
887 | } | |
888 | ||
889 | FcResult | |
890 | FcPatternGetLangSet(const FcPattern *p, const char *object, int id, FcLangSet **ls) | |
891 | { | |
892 | FcValue v; | |
893 | FcResult r; | |
894 | ||
895 | r = FcPatternGet (p, object, id, &v); | |
896 | if (r != FcResultMatch) | |
897 | return r; | |
898 | if (v.type != FcTypeLangSet) | |
899 | return FcResultTypeMismatch; | |
900 | *ls = (FcLangSet *)v.u.l; | |
901 | return FcResultMatch; | |
902 | } | |
903 | ||
904 | FcPattern * | |
905 | FcPatternDuplicate (const FcPattern *orig) | |
906 | { | |
907 | FcPattern *new; | |
908 | FcPatternElt *e; | |
909 | int i; | |
910 | FcValueListPtr l; | |
911 | ||
912 | new = FcPatternCreate (); | |
913 | if (!new) | |
914 | goto bail0; | |
915 | ||
916 | e = FcPatternElts(orig); | |
917 | ||
918 | for (i = 0; i < orig->num; i++) | |
919 | { | |
920 | for (l = FcPatternEltValues(e + i); l; l = FcValueListNext(l)) | |
921 | { | |
922 | if (!FcPatternObjectAddWithBinding (new, e[i].object, | |
923 | FcValueCanonicalize(&l->value), | |
924 | l->binding, | |
925 | FcTrue)) | |
926 | goto bail1; | |
927 | ||
928 | } | |
929 | } | |
930 | ||
931 | return new; | |
932 | ||
933 | bail1: | |
934 | FcPatternDestroy (new); | |
935 | bail0: | |
936 | return 0; | |
937 | } | |
938 | ||
939 | void | |
940 | FcPatternReference (FcPattern *p) | |
941 | { | |
942 | if (p->ref != FC_REF_CONSTANT) | |
943 | p->ref++; | |
944 | else | |
945 | FcCacheObjectReference (p); | |
946 | } | |
947 | ||
948 | FcPattern * | |
949 | FcPatternVaBuild (FcPattern *p, va_list va) | |
950 | { | |
951 | FcPattern *ret; | |
952 | ||
953 | FcPatternVapBuild (ret, p, va); | |
954 | return ret; | |
955 | } | |
956 | ||
957 | FcPattern * | |
958 | FcPatternBuild (FcPattern *p, ...) | |
959 | { | |
960 | va_list va; | |
961 | ||
962 | va_start (va, p); | |
963 | FcPatternVapBuild (p, p, va); | |
964 | va_end (va); | |
965 | return p; | |
966 | } | |
967 | ||
968 | /* | |
969 | * Add all of the elements in 's' to 'p' | |
970 | */ | |
971 | FcBool | |
972 | FcPatternAppend (FcPattern *p, FcPattern *s) | |
973 | { | |
974 | int i; | |
975 | FcPatternElt *e; | |
976 | FcValueListPtr v; | |
977 | ||
978 | for (i = 0; i < s->num; i++) | |
979 | { | |
980 | e = FcPatternElts(s)+i; | |
981 | for (v = FcPatternEltValues(e); v; v = FcValueListNext(v)) | |
982 | { | |
983 | if (!FcPatternObjectAddWithBinding (p, e->object, | |
984 | FcValueCanonicalize(&v->value), | |
985 | v->binding, FcTrue)) | |
986 | return FcFalse; | |
987 | } | |
988 | } | |
989 | return FcTrue; | |
990 | } | |
991 | ||
992 | FcPattern * | |
993 | FcPatternFilter (FcPattern *p, const FcObjectSet *os) | |
994 | { | |
995 | int i; | |
996 | FcPattern *ret; | |
997 | FcPatternElt *e; | |
998 | FcValueListPtr v; | |
999 | ||
1000 | if (!os) | |
1001 | return FcPatternDuplicate (p); | |
1002 | ||
1003 | ret = FcPatternCreate (); | |
1004 | if (!ret) | |
1005 | return NULL; | |
1006 | ||
1007 | for (i = 0; i < os->nobject; i++) | |
1008 | { | |
1009 | FcObject object = FcObjectFromName (os->objects[i]); | |
1010 | e = FcPatternObjectFindElt (p, object); | |
1011 | if (e) | |
1012 | { | |
1013 | for (v = FcPatternEltValues(e); v; v = FcValueListNext(v)) | |
1014 | { | |
1015 | if (!FcPatternObjectAddWithBinding (ret, e->object, | |
1016 | FcValueCanonicalize(&v->value), | |
1017 | v->binding, FcTrue)) | |
1018 | goto bail0; | |
1019 | } | |
1020 | } | |
1021 | } | |
1022 | return ret; | |
1023 | ||
1024 | bail0: | |
1025 | FcPatternDestroy (ret); | |
1026 | return NULL; | |
1027 | } | |
1028 | ||
1029 | #define OBJECT_HASH_SIZE 31 | |
1030 | static struct objectBucket { | |
1031 | struct objectBucket *next; | |
1032 | FcChar32 hash; | |
1033 | } *FcObjectBuckets[OBJECT_HASH_SIZE]; | |
1034 | ||
1035 | static FcBool | |
1036 | FcHashOwnsName (const FcChar8 *name) | |
1037 | { | |
1038 | FcChar32 hash = FcStringHash (name); | |
1039 | struct objectBucket **p; | |
1040 | struct objectBucket *b; | |
1041 | ||
1042 | for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next)) | |
1043 | if (b->hash == hash && ((char *)name == (char *) (b + 1))) | |
1044 | return FcTrue; | |
1045 | return FcFalse; | |
1046 | } | |
1047 | ||
1048 | const FcChar8 * | |
1049 | FcStrStaticName (const FcChar8 *name) | |
1050 | { | |
1051 | FcChar32 hash = FcStringHash (name); | |
1052 | struct objectBucket **p; | |
1053 | struct objectBucket *b; | |
1054 | int size; | |
1055 | ||
1056 | for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next)) | |
1057 | if (b->hash == hash && !strcmp ((char *)name, (char *) (b + 1))) | |
1058 | return (FcChar8 *) (b + 1); | |
1059 | size = sizeof (struct objectBucket) + strlen ((char *)name) + 1; | |
1060 | /* | |
1061 | * workaround valgrind warning because glibc takes advantage of how it knows memory is | |
1062 | * allocated to implement strlen by reading in groups of 4 | |
1063 | */ | |
1064 | size = (size + 3) & ~3; | |
1065 | b = malloc (size); | |
1066 | FcMemAlloc (FC_MEM_STATICSTR, size); | |
1067 | if (!b) | |
1068 | return NULL; | |
1069 | b->next = 0; | |
1070 | b->hash = hash; | |
1071 | strcpy ((char *) (b + 1), (char *)name); | |
1072 | *p = b; | |
1073 | return (FcChar8 *) (b + 1); | |
1074 | } | |
1075 | ||
1076 | static void | |
1077 | FcStrStaticNameFini (void) | |
1078 | { | |
1079 | int i, size; | |
1080 | struct objectBucket *b, *next; | |
1081 | char *name; | |
1082 | ||
1083 | for (i = 0; i < OBJECT_HASH_SIZE; i++) | |
1084 | { | |
1085 | for (b = FcObjectBuckets[i]; b; b = next) | |
1086 | { | |
1087 | next = b->next; | |
1088 | name = (char *) (b + 1); | |
1089 | size = sizeof (struct objectBucket) + strlen (name) + 1; | |
1090 | FcMemFree (FC_MEM_STATICSTR, size + sizeof (int)); | |
1091 | free (b); | |
1092 | } | |
1093 | FcObjectBuckets[i] = 0; | |
1094 | } | |
1095 | } | |
1096 | ||
1097 | void | |
1098 | FcPatternFini (void) | |
1099 | { | |
1100 | FcStrStaticNameFini (); | |
1101 | FcObjectFini (); | |
1102 | } | |
1103 | ||
1104 | FcBool | |
1105 | FcPatternSerializeAlloc (FcSerialize *serialize, const FcPattern *pat) | |
1106 | { | |
1107 | int i; | |
1108 | FcPatternElt *elts = FcPatternElts(pat); | |
1109 | ||
1110 | if (!FcSerializeAlloc (serialize, pat, sizeof (FcPattern))) | |
1111 | return FcFalse; | |
1112 | if (!FcSerializeAlloc (serialize, elts, pat->num * sizeof (FcPatternElt))) | |
1113 | return FcFalse; | |
1114 | for (i = 0; i < pat->num; i++) | |
1115 | if (!FcValueListSerializeAlloc (serialize, FcPatternEltValues(elts+i))) | |
1116 | return FcFalse; | |
1117 | return FcTrue; | |
1118 | } | |
1119 | ||
1120 | FcPattern * | |
1121 | FcPatternSerialize (FcSerialize *serialize, const FcPattern *pat) | |
1122 | { | |
1123 | FcPattern *pat_serialized; | |
1124 | FcPatternElt *elts = FcPatternElts (pat); | |
1125 | FcPatternElt *elts_serialized; | |
1126 | FcValueList *values_serialized; | |
1127 | int i; | |
1128 | ||
1129 | pat_serialized = FcSerializePtr (serialize, pat); | |
1130 | if (!pat_serialized) | |
1131 | return NULL; | |
1132 | *pat_serialized = *pat; | |
1133 | pat_serialized->size = pat->num; | |
1134 | pat_serialized->ref = FC_REF_CONSTANT; | |
1135 | ||
1136 | elts_serialized = FcSerializePtr (serialize, elts); | |
1137 | if (!elts_serialized) | |
1138 | return NULL; | |
1139 | ||
1140 | pat_serialized->elts_offset = FcPtrToOffset (pat_serialized, | |
1141 | elts_serialized); | |
1142 | ||
1143 | for (i = 0; i < pat->num; i++) | |
1144 | { | |
1145 | values_serialized = FcValueListSerialize (serialize, FcPatternEltValues (elts+i)); | |
1146 | if (!values_serialized) | |
1147 | return NULL; | |
1148 | elts_serialized[i].object = elts[i].object; | |
1149 | elts_serialized[i].values = FcPtrToEncodedOffset (&elts_serialized[i], | |
1150 | values_serialized, | |
1151 | FcValueList); | |
1152 | } | |
1153 | if (FcDebug() & FC_DBG_CACHEV) { | |
1154 | printf ("Raw pattern:\n"); | |
1155 | FcPatternPrint (pat); | |
1156 | printf ("Serialized pattern:\n"); | |
1157 | FcPatternPrint (pat_serialized); | |
1158 | printf ("\n"); | |
1159 | } | |
1160 | return pat_serialized; | |
1161 | } | |
1162 | ||
1163 | FcBool | |
1164 | FcValueListSerializeAlloc (FcSerialize *serialize, const FcValueList *vl) | |
1165 | { | |
1166 | while (vl) | |
1167 | { | |
1168 | if (!FcSerializeAlloc (serialize, vl, sizeof (FcValueList))) | |
1169 | return FcFalse; | |
1170 | switch (vl->value.type) { | |
1171 | case FcTypeString: | |
1172 | if (!FcStrSerializeAlloc (serialize, vl->value.u.s)) | |
1173 | return FcFalse; | |
1174 | break; | |
1175 | case FcTypeCharSet: | |
1176 | if (!FcCharSetSerializeAlloc (serialize, vl->value.u.c)) | |
1177 | return FcFalse; | |
1178 | break; | |
1179 | case FcTypeLangSet: | |
1180 | if (!FcLangSetSerializeAlloc (serialize, vl->value.u.l)) | |
1181 | return FcFalse; | |
1182 | break; | |
1183 | default: | |
1184 | break; | |
1185 | } | |
1186 | vl = vl->next; | |
1187 | } | |
1188 | return FcTrue; | |
1189 | } | |
1190 | ||
1191 | FcValueList * | |
1192 | FcValueListSerialize (FcSerialize *serialize, const FcValueList *vl) | |
1193 | { | |
1194 | FcValueList *vl_serialized; | |
1195 | FcChar8 *s_serialized; | |
1196 | FcCharSet *c_serialized; | |
1197 | FcLangSet *l_serialized; | |
1198 | FcValueList *head_serialized = NULL; | |
1199 | FcValueList *prev_serialized = NULL; | |
1200 | ||
1201 | while (vl) | |
1202 | { | |
1203 | vl_serialized = FcSerializePtr (serialize, vl); | |
1204 | if (!vl_serialized) | |
1205 | return NULL; | |
1206 | ||
1207 | if (prev_serialized) | |
1208 | prev_serialized->next = FcPtrToEncodedOffset (prev_serialized, | |
1209 | vl_serialized, | |
1210 | FcValueList); | |
1211 | else | |
1212 | head_serialized = vl_serialized; | |
1213 | ||
1214 | vl_serialized->next = NULL; | |
1215 | vl_serialized->value.type = vl->value.type; | |
1216 | switch (vl->value.type) { | |
1217 | case FcTypeInteger: | |
1218 | vl_serialized->value.u.i = vl->value.u.i; | |
1219 | break; | |
1220 | case FcTypeDouble: | |
1221 | vl_serialized->value.u.d = vl->value.u.d; | |
1222 | break; | |
1223 | case FcTypeString: | |
1224 | s_serialized = FcStrSerialize (serialize, vl->value.u.s); | |
1225 | if (!s_serialized) | |
1226 | return NULL; | |
1227 | vl_serialized->value.u.s = FcPtrToEncodedOffset (&vl_serialized->value, | |
1228 | s_serialized, | |
1229 | FcChar8); | |
1230 | break; | |
1231 | case FcTypeBool: | |
1232 | vl_serialized->value.u.b = vl->value.u.b; | |
1233 | break; | |
1234 | case FcTypeMatrix: | |
1235 | /* can't happen */ | |
1236 | break; | |
1237 | case FcTypeCharSet: | |
1238 | c_serialized = FcCharSetSerialize (serialize, vl->value.u.c); | |
1239 | if (!c_serialized) | |
1240 | return NULL; | |
1241 | vl_serialized->value.u.c = FcPtrToEncodedOffset (&vl_serialized->value, | |
1242 | c_serialized, | |
1243 | FcCharSet); | |
1244 | break; | |
1245 | case FcTypeFTFace: | |
1246 | /* can't happen */ | |
1247 | break; | |
1248 | case FcTypeLangSet: | |
1249 | l_serialized = FcLangSetSerialize (serialize, vl->value.u.l); | |
1250 | if (!l_serialized) | |
1251 | return NULL; | |
1252 | vl_serialized->value.u.l = FcPtrToEncodedOffset (&vl_serialized->value, | |
1253 | l_serialized, | |
1254 | FcLangSet); | |
1255 | break; | |
1256 | default: | |
1257 | break; | |
1258 | } | |
1259 | prev_serialized = vl_serialized; | |
1260 | vl = vl->next; | |
1261 | } | |
1262 | return head_serialized; | |
1263 | } | |
1264 | #define __fcpat__ | |
1265 | #include "fcaliastail.h" | |
1266 | #include "fcftaliastail.h" | |
1267 | #undef __fcpat__ |