]> git.wh0rd.org - fontconfig.git/blob - src/fcname.c
Fix bad behaviour on realloc resulting in crash.
[fontconfig.git] / src / fcname.c
1 /*
2 * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $
3 *
4 * Copyright © 2000 Keith Packard
5 *
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.
15 *
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.
23 */
24
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include "fcint.h"
30
31 /* Please do not revoke any of these bindings. */
32 /* The __DUMMY__ object enables callers to distinguish the error return
33 * of FcObjectToPtrLookup from FC_FAMILY's FcObjectPtr, which would
34 * otherwise be 0. */
35 static const FcObjectType _FcBaseObjectTypes[] = {
36 { "__DUMMY__", FcTypeVoid, },
37 { FC_FAMILY, FcTypeString, },
38 { FC_FAMILYLANG, FcTypeString, },
39 { FC_STYLE, FcTypeString, },
40 { FC_STYLELANG, FcTypeString, },
41 { FC_FULLNAME, FcTypeString, },
42 { FC_FULLNAMELANG, FcTypeString, },
43 { FC_SLANT, FcTypeInteger, },
44 { FC_WEIGHT, FcTypeInteger, },
45 { FC_WIDTH, FcTypeInteger, },
46 { FC_SIZE, FcTypeDouble, },
47 { FC_ASPECT, FcTypeDouble, },
48 { FC_PIXEL_SIZE, FcTypeDouble, },
49 { FC_SPACING, FcTypeInteger, },
50 { FC_FOUNDRY, FcTypeString, },
51 /* { FC_CORE, FcTypeBool, }, */
52 { FC_ANTIALIAS, FcTypeBool, },
53 { FC_HINT_STYLE, FcTypeInteger, },
54 { FC_HINTING, FcTypeBool, },
55 { FC_VERTICAL_LAYOUT, FcTypeBool, },
56 { FC_AUTOHINT, FcTypeBool, },
57 { FC_GLOBAL_ADVANCE, FcTypeBool, },
58 /* { FC_XLFD, FcTypeString, }, */
59 { FC_FILE, FcTypeString, },
60 { FC_INDEX, FcTypeInteger, },
61 { FC_RASTERIZER, FcTypeString, },
62 { FC_OUTLINE, FcTypeBool, },
63 { FC_SCALABLE, FcTypeBool, },
64 { FC_DPI, FcTypeDouble },
65 { FC_RGBA, FcTypeInteger, },
66 { FC_SCALE, FcTypeDouble, },
67 { FC_MINSPACE, FcTypeBool, },
68 /* { FC_RENDER, FcTypeBool, },*/
69 { FC_CHAR_WIDTH, FcTypeInteger },
70 { FC_CHAR_HEIGHT, FcTypeInteger },
71 { FC_MATRIX, FcTypeMatrix },
72 { FC_CHARSET, FcTypeCharSet },
73 { FC_LANG, FcTypeLangSet },
74 { FC_FONTVERSION, FcTypeInteger },
75 { FC_CAPABILITY, FcTypeString },
76 { FC_FONTFORMAT, FcTypeString },
77 { FC_EMBOLDEN, FcTypeBool },
78 { FC_EMBEDDED_BITMAP, FcTypeBool },
79 };
80
81 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
82
83 static FcObjectType * _FcUserObjectNames = 0;
84 static int user_obj_alloc = 0;
85 static int next_basic_offset = NUM_OBJECT_TYPES;
86
87 typedef struct _FcObjectTypeList FcObjectTypeList;
88
89 struct _FcObjectTypeList {
90 const FcObjectTypeList *next;
91 const FcObjectType *types;
92 int ntypes;
93 int basic_offset;
94 };
95
96 static const FcObjectTypeList _FcBaseObjectTypesList = {
97 0,
98 _FcBaseObjectTypes,
99 NUM_OBJECT_TYPES,
100 0
101 };
102
103 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
104
105 FcBool
106 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
107 {
108 FcObjectTypeList *l;
109
110 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
111 if (!l)
112 return FcFalse;
113 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
114 l->types = types;
115 l->ntypes = ntypes;
116 l->next = _FcObjectTypes;
117 l->basic_offset = next_basic_offset;
118 next_basic_offset += ntypes;
119 _FcObjectTypes = l;
120 return FcTrue;
121 }
122
123 static FcBool
124 FcNameUnregisterObjectTypesFree (const FcObjectType *types, int ntypes,
125 FcBool do_free)
126 {
127 const FcObjectTypeList *l, **prev;
128
129 for (prev = &_FcObjectTypes;
130 (l = *prev);
131 prev = (const FcObjectTypeList **) &(l->next))
132 {
133 if (l->types == types && l->ntypes == ntypes)
134 {
135 *prev = l->next;
136 if (do_free) {
137 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
138 free ((void *) l);
139 }
140 return FcTrue;
141 }
142 }
143 return FcFalse;
144 }
145
146 FcBool
147 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
148 {
149 return FcNameUnregisterObjectTypesFree (types, ntypes, FcTrue);
150 }
151
152 const FcObjectType *
153 FcNameGetObjectType (const char *object)
154 {
155 int i;
156 const FcObjectTypeList *l;
157 const FcObjectType *t;
158
159 for (l = _FcObjectTypes; l; l = l->next)
160 {
161 if (l == (FcObjectTypeList*)_FcUserObjectNames)
162 continue;
163
164 for (i = 0; i < l->ntypes; i++)
165 {
166 t = &l->types[i];
167 if (!strcmp (object, t->object))
168 return t;
169 }
170 }
171 return 0;
172 }
173
174 #define OBJECT_HASH_SIZE 31
175 struct objectBucket {
176 struct objectBucket *next;
177 FcChar32 hash;
178 int id;
179 };
180 static struct objectBucket *FcObjectBuckets[OBJECT_HASH_SIZE];
181
182 /* Design constraint: biggest_known_ntypes must never change
183 * after any call to FcNameRegisterObjectTypes. */
184 static const FcObjectType *biggest_known_types = _FcBaseObjectTypes;
185 static FcBool allocated_biggest_known_types;
186 static int biggest_known_ntypes = NUM_OBJECT_TYPES;
187 static int biggest_known_count = 0;
188 static char * biggest_ptr;
189
190
191 static FcObjectPtr
192 FcObjectToPtrLookup (const char * object)
193 {
194 FcObjectPtr i = 0, n;
195 const FcObjectTypeList *l;
196 FcObjectType *t = _FcUserObjectNames, *origNames;
197 FcBool replace;
198
199 for (l = _FcObjectTypes; l; l = l->next)
200 {
201 for (i = 0; i < l->ntypes; i++)
202 {
203 t = (FcObjectType *)&l->types[i];
204 if (!strcmp (object, t->object))
205 {
206 if (l == (FcObjectTypeList*)_FcUserObjectNames)
207 return -i;
208 else
209 return l->basic_offset + i;
210 }
211 }
212 }
213
214 /* We didn't match. Look for the application's FcObjectTypeList
215 * and replace it in-place. */
216 origNames = _FcUserObjectNames;
217 for (l = _FcObjectTypes; l; l = l->next)
218 {
219 if (l->types == _FcUserObjectNames)
220 break;
221 }
222
223 replace = l && l->types == _FcUserObjectNames;
224 if (!_FcUserObjectNames ||
225 (replace && user_obj_alloc <= l->ntypes))
226 {
227 int nt = user_obj_alloc + 4;
228 FcObjectType * tt = realloc (_FcUserObjectNames,
229 nt * sizeof (FcObjectType));
230 if (!tt)
231 return 0;
232 _FcUserObjectNames = tt;
233 user_obj_alloc = nt;
234 }
235
236 if (replace)
237 {
238 n = l->ntypes;
239 FcNameUnregisterObjectTypesFree (l->types, l->ntypes, FcFalse);
240 }
241 else
242 n = 0;
243
244 FcNameRegisterObjectTypes (_FcUserObjectNames, n+1);
245
246 for (l = _FcObjectTypes; l; l = l->next)
247 {
248 if (l->types == origNames)
249 {
250 t = (FcObjectType *)l->types;
251 break;
252 }
253 }
254
255 if (!t)
256 return 0;
257
258 t[n].object = object;
259 t[n].type = FcTypeVoid;
260
261 return -n;
262 }
263
264 FcObjectPtr
265 FcObjectToPtr (const char * name)
266 {
267 FcChar32 hash = FcStringHash ((const FcChar8 *) name);
268 struct objectBucket **p;
269 struct objectBucket *b;
270 int size;
271
272 for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next)
273 )
274 if (b->hash == hash && !strcmp (name, (char *) (b + 1)))
275 return b->id;
276 size = sizeof (struct objectBucket) + strlen (name) + 1;
277 /* workaround glibc bug which reads strlen in groups of 4 */
278 b = malloc (size + sizeof (int));
279 FcMemAlloc (FC_MEM_STATICSTR, size + sizeof(int));
280 if (!b)
281 return 0;
282 b->next = 0;
283 b->hash = hash;
284 b->id = FcObjectToPtrLookup (name);
285 strcpy ((char *) (b + 1), name);
286 *p = b;
287 return b->id;
288 }
289
290 void
291 FcObjectStaticNameFini (void)
292 {
293 int i, size;
294 struct objectBucket *b, *next;
295 char *name;
296
297 for (i = 0; i < OBJECT_HASH_SIZE; i++)
298 {
299 for (b = FcObjectBuckets[i]; b; b = next)
300 {
301 next = b->next;
302 name = (char *) (b + 1);
303 size = sizeof (struct objectBucket) + strlen (name) + 1;
304 FcMemFree (FC_MEM_STATICSTR, size);
305 free (b);
306 }
307 FcObjectBuckets[i] = 0;
308 }
309 }
310
311 const char *
312 FcObjectPtrU (FcObjectPtr si)
313 {
314 const FcObjectTypeList *l;
315 int i, j;
316
317 if (si > 0)
318 {
319 if (si < biggest_known_ntypes)
320 return biggest_known_types[si].object;
321
322 j = 0;
323 for (l = _FcObjectTypes; l; l = l->next)
324 for (i = 0; i < l->ntypes; i++, j++)
325 if (j == si)
326 return l->types[i].object;
327 }
328
329 return _FcUserObjectNames[-si].object;
330 }
331
332 int
333 FcObjectNeededBytes ()
334 {
335 int num = 0, i;
336 for (i = 0; i < biggest_known_ntypes; i++)
337 {
338 const char * t = biggest_known_types[i].object;
339 num = num + strlen(t) + 1;
340 }
341 biggest_known_count = num;
342 return num + sizeof(int);
343 }
344
345 int
346 FcObjectNeededBytesAlign (void)
347 {
348 return fc_alignof (int) + fc_alignof (char);
349 }
350
351 void *
352 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
353 {
354 block_ptr = ALIGN (block_ptr, int);
355 *(int *)block_ptr = biggest_known_ntypes;
356 block_ptr = (int *) block_ptr + 1;
357 block_ptr = ALIGN (block_ptr, char);
358 biggest_ptr = block_ptr;
359 block_ptr = (char *) block_ptr + biggest_known_count;
360 return block_ptr;
361 }
362
363 void
364 FcObjectSerialize (void)
365 {
366 int i;
367 for (i = 0; i < biggest_known_ntypes; i++)
368 {
369 const char * t = biggest_known_types[i].object;
370 strcpy (biggest_ptr, t);
371 biggest_ptr = biggest_ptr + strlen(t) + 1;
372 }
373 }
374
375 void *
376 FcObjectUnserialize (FcCache * metadata, void *block_ptr)
377 {
378 int new_biggest;
379 block_ptr = ALIGN (block_ptr, int);
380 new_biggest = *(int *)block_ptr;
381 block_ptr = (int *) block_ptr + 1;
382 if (biggest_known_ntypes < new_biggest)
383 {
384 int i;
385 char * bp = (char *)block_ptr;
386 FcObjectType * bn;
387
388 bn = malloc (sizeof (const FcObjectType) * (new_biggest + 1));
389 if (!bn)
390 return 0;
391
392 for (i = 0; i < new_biggest; i++)
393 {
394 const FcObjectType * t = FcNameGetObjectType(bp);
395 if (t)
396 bn[i].type = t->type;
397 else
398 bn[i].type = FcTypeVoid;
399 bn[i].object = bp;
400 bp = bp + strlen(bp) + 1;
401 }
402
403 FcNameUnregisterObjectTypesFree (biggest_known_types, biggest_known_ntypes, FcFalse);
404 if (allocated_biggest_known_types)
405 {
406 free ((FcObjectTypeList *)biggest_known_types);
407 }
408 else
409 allocated_biggest_known_types = FcTrue;
410
411 FcNameRegisterObjectTypes (bn, new_biggest);
412 biggest_known_ntypes = new_biggest;
413 biggest_known_types = (const FcObjectType *)bn;
414 }
415 block_ptr = ALIGN (block_ptr, char);
416 block_ptr = (char *) block_ptr + biggest_known_count;
417 return block_ptr;
418 }
419
420 static const FcConstant _FcBaseConstants[] = {
421 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
422 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
423 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
424 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
425 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
426 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
427 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
428 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
429 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
430 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
431 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
432 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
433 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
434
435 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
436 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
437 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
438
439 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
440 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
441 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
442 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
443 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
444 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
445 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
446 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
447 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
448
449 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
450 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
451 { (FcChar8 *) "mono", "spacing", FC_MONO, },
452 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
453
454 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
455 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
456 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
457 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
458 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
459 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
460
461 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
462 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
463 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
464 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
465 };
466
467 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
468
469 typedef struct _FcConstantList FcConstantList;
470
471 struct _FcConstantList {
472 const FcConstantList *next;
473 const FcConstant *consts;
474 int nconsts;
475 };
476
477 static const FcConstantList _FcBaseConstantList = {
478 0,
479 _FcBaseConstants,
480 NUM_FC_CONSTANTS
481 };
482
483 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
484
485 FcBool
486 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
487 {
488 FcConstantList *l;
489
490 l = (FcConstantList *) malloc (sizeof (FcConstantList));
491 if (!l)
492 return FcFalse;
493 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
494 l->consts = consts;
495 l->nconsts = nconsts;
496 l->next = _FcConstants;
497 _FcConstants = l;
498 return FcTrue;
499 }
500
501 FcBool
502 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
503 {
504 const FcConstantList *l, **prev;
505
506 for (prev = &_FcConstants;
507 (l = *prev);
508 prev = (const FcConstantList **) &(l->next))
509 {
510 if (l->consts == consts && l->nconsts == nconsts)
511 {
512 *prev = l->next;
513 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
514 free ((void *) l);
515 return FcTrue;
516 }
517 }
518 return FcFalse;
519 }
520
521 const FcConstant *
522 FcNameGetConstant (FcChar8 *string)
523 {
524 const FcConstantList *l;
525 int i;
526
527 for (l = _FcConstants; l; l = l->next)
528 {
529 for (i = 0; i < l->nconsts; i++)
530 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
531 return &l->consts[i];
532 }
533 return 0;
534 }
535
536 FcBool
537 FcNameConstant (FcChar8 *string, int *result)
538 {
539 const FcConstant *c;
540
541 if ((c = FcNameGetConstant(string)))
542 {
543 *result = c->value;
544 return FcTrue;
545 }
546 return FcFalse;
547 }
548
549 FcBool
550 FcNameBool (const FcChar8 *v, FcBool *result)
551 {
552 char c0, c1;
553
554 c0 = *v;
555 c0 = FcToLower (c0);
556 if (c0 == 't' || c0 == 'y' || c0 == '1')
557 {
558 *result = FcTrue;
559 return FcTrue;
560 }
561 if (c0 == 'f' || c0 == 'n' || c0 == '0')
562 {
563 *result = FcFalse;
564 return FcTrue;
565 }
566 if (c0 == 'o')
567 {
568 c1 = v[1];
569 c1 = FcToLower (c1);
570 if (c1 == 'n')
571 {
572 *result = FcTrue;
573 return FcTrue;
574 }
575 if (c1 == 'f')
576 {
577 *result = FcFalse;
578 return FcTrue;
579 }
580 }
581 return FcFalse;
582 }
583
584 static FcValue
585 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
586 {
587 FcValue v;
588
589 v.type = type;
590 switch (v.type) {
591 case FcTypeInteger:
592 if (!FcNameConstant (string, &v.u.i))
593 v.u.i = atoi ((char *) string);
594 break;
595 case FcTypeString:
596 v.u.s = FcStrStaticName(string);
597 break;
598 case FcTypeBool:
599 if (!FcNameBool (string, &v.u.b))
600 v.u.b = FcFalse;
601 break;
602 case FcTypeDouble:
603 v.u.d = strtod ((char *) string, 0);
604 break;
605 case FcTypeMatrix:
606 v.u.m = m;
607 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
608 break;
609 case FcTypeCharSet:
610 v.u.c = FcNameParseCharSet (string);
611 break;
612 case FcTypeLangSet:
613 v.u.l = FcNameParseLangSet (string);
614 break;
615 default:
616 break;
617 }
618 return v;
619 }
620
621 static const FcChar8 *
622 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
623 {
624 FcChar8 c;
625
626 while ((c = *cur))
627 {
628 if (c == '\\')
629 {
630 ++cur;
631 if (!(c = *cur))
632 break;
633 }
634 else if (strchr (delim, c))
635 break;
636 ++cur;
637 *save++ = c;
638 }
639 *save = 0;
640 *last = *cur;
641 if (*cur)
642 cur++;
643 return cur;
644 }
645
646 FcPattern *
647 FcNameParse (const FcChar8 *name)
648 {
649 FcChar8 *save;
650 FcPattern *pat;
651 double d;
652 FcChar8 *e;
653 FcChar8 delim;
654 FcValue v;
655 FcMatrix m;
656 const FcObjectType *t;
657 const FcConstant *c;
658
659 /* freed below */
660 save = malloc (strlen ((char *) name) + 1);
661 if (!save)
662 goto bail0;
663 pat = FcPatternCreate ();
664 if (!pat)
665 goto bail1;
666
667 for (;;)
668 {
669 name = FcNameFindNext (name, "-,:", save, &delim);
670 if (save[0])
671 {
672 if (!FcPatternAddString (pat, FC_FAMILY, save))
673 goto bail2;
674 }
675 if (delim != ',')
676 break;
677 }
678 if (delim == '-')
679 {
680 for (;;)
681 {
682 name = FcNameFindNext (name, "-,:", save, &delim);
683 d = strtod ((char *) save, (char **) &e);
684 if (e != save)
685 {
686 if (!FcPatternAddDouble (pat, FC_SIZE, d))
687 goto bail2;
688 }
689 if (delim != ',')
690 break;
691 }
692 }
693 while (delim == ':')
694 {
695 name = FcNameFindNext (name, "=_:", save, &delim);
696 if (save[0])
697 {
698 if (delim == '=' || delim == '_')
699 {
700 t = FcNameGetObjectType ((char *) save);
701 for (;;)
702 {
703 name = FcNameFindNext (name, ":,", save, &delim);
704 if (t && strcmp (t->object, _FcBaseObjectTypes[0].object))
705 {
706 v = FcNameConvert (t->type, save, &m);
707 if (!FcPatternAdd (pat, t->object, v, FcTrue))
708 {
709 switch (v.type) {
710 case FcTypeCharSet:
711 FcCharSetDestroy ((FcCharSet *) v.u.c);
712 break;
713 case FcTypeLangSet:
714 FcLangSetDestroy ((FcLangSet *) v.u.l);
715 break;
716 default:
717 break;
718 }
719 goto bail2;
720 }
721 switch (v.type) {
722 case FcTypeCharSet:
723 FcCharSetDestroy ((FcCharSet *) v.u.c);
724 break;
725 case FcTypeLangSet:
726 FcLangSetDestroy ((FcLangSet *) v.u.l);
727 break;
728 default:
729 break;
730 }
731 }
732 if (delim != ',')
733 break;
734 }
735 }
736 else
737 {
738 if ((c = FcNameGetConstant (save)))
739 {
740 if (!FcPatternAddInteger (pat, c->object, c->value))
741 goto bail2;
742 }
743 }
744 }
745 }
746
747 free (save);
748 return pat;
749
750 bail2:
751 FcPatternDestroy (pat);
752 bail1:
753 free (save);
754 bail0:
755 return 0;
756 }
757 static FcBool
758 FcNameUnparseString (FcStrBuf *buf,
759 const FcChar8 *string,
760 const FcChar8 *escape)
761 {
762 FcChar8 c;
763 while ((c = *string++))
764 {
765 if (escape && strchr ((char *) escape, (char) c))
766 {
767 if (!FcStrBufChar (buf, escape[0]))
768 return FcFalse;
769 }
770 if (!FcStrBufChar (buf, c))
771 return FcFalse;
772 }
773 return FcTrue;
774 }
775
776 static FcBool
777 FcNameUnparseValue (FcStrBuf *buf,
778 FcValue *v0,
779 FcChar8 *escape)
780 {
781 FcChar8 temp[1024];
782 FcValue v = FcValueCanonicalize(v0);
783
784 switch (v.type) {
785 case FcTypeVoid:
786 return FcTrue;
787 case FcTypeInteger:
788 sprintf ((char *) temp, "%d", v.u.i);
789 return FcNameUnparseString (buf, temp, 0);
790 case FcTypeDouble:
791 sprintf ((char *) temp, "%g", v.u.d);
792 return FcNameUnparseString (buf, temp, 0);
793 case FcTypeString:
794 return FcNameUnparseString (buf, v.u.s, escape);
795 case FcTypeBool:
796 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
797 case FcTypeMatrix:
798 sprintf ((char *) temp, "%g %g %g %g",
799 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
800 return FcNameUnparseString (buf, temp, 0);
801 case FcTypeCharSet:
802 return FcNameUnparseCharSet (buf, v.u.c);
803 case FcTypeLangSet:
804 return FcNameUnparseLangSet (buf, v.u.l);
805 case FcTypeFTFace:
806 return FcTrue;
807 }
808 return FcFalse;
809 }
810
811 static FcBool
812 FcNameUnparseValueList (FcStrBuf *buf,
813 FcValueListPtr v,
814 FcChar8 *escape)
815 {
816 while (FcValueListPtrU(v))
817 {
818 if (!FcNameUnparseValue (buf, &FcValueListPtrU(v)->value, escape))
819 return FcFalse;
820 if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
821 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
822 return FcFalse;
823 }
824 return FcTrue;
825 }
826
827 #define FC_ESCAPE_FIXED "\\-:,"
828 #define FC_ESCAPE_VARIABLE "\\=_:,"
829
830 FcChar8 *
831 FcNameUnparse (FcPattern *pat)
832 {
833 return FcNameUnparseEscaped (pat, FcTrue);
834 }
835
836 FcChar8 *
837 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
838 {
839 FcStrBuf buf;
840 FcChar8 buf_static[8192];
841 int i;
842 FcPatternElt *e;
843 const FcObjectTypeList *l;
844 const FcObjectType *o;
845
846 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
847 e = FcPatternFindElt (pat, FC_FAMILY);
848 if (e)
849 {
850 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
851 goto bail0;
852 }
853 e = FcPatternFindElt (pat, FC_SIZE);
854 if (e)
855 {
856 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
857 goto bail0;
858 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
859 goto bail0;
860 }
861 for (l = _FcObjectTypes; l; l = l->next)
862 {
863 for (i = 0; i < l->ntypes; i++)
864 {
865 o = &l->types[i];
866 if (!strcmp (o->object, FC_FAMILY) ||
867 !strcmp (o->object, FC_SIZE) ||
868 !strcmp (o->object, FC_FILE))
869 continue;
870
871 e = FcPatternFindElt (pat, o->object);
872 if (e)
873 {
874 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
875 goto bail0;
876 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
877 goto bail0;
878 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
879 goto bail0;
880 if (!FcNameUnparseValueList (&buf, e->values, escape ?
881 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
882 goto bail0;
883 }
884 }
885 }
886 return FcStrBufDone (&buf);
887 bail0:
888 FcStrBufDestroy (&buf);
889 return 0;
890 }