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