Replace RCS Id tags with the file name
[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 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 "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 -1:
300             return FcTrue;
301         case FcTypeDouble:
302         case FcTypeInteger:
303             if (type == FcTypeDouble || type == FcTypeInteger)
304                 return FcTrue;
305             break;
306         case FcTypeLangSet:
307             if (type == FcTypeLangSet || type == FcTypeString)
308                 return FcTrue;
309             break;
310         default:
311             if (type == t->type)
312                 return FcTrue;
313             break;
314         }
315         return FcFalse;
316     }
317     return FcTrue;
318 }
319
320 FcObject
321 FcObjectFromName (const char * name)
322 {
323     FcObjectType    *o = FcObjectFindByName (name, FcTrue);
324
325     if (o)
326         return FcObjectId (o);
327     return 0;
328 }
329
330 FcBool
331 FcObjectInit (void)
332 {
333     int i;
334
335     if (FcObjectsInited)
336         return FcTrue;
337
338     FcObjectsInited = FcTrue;
339     for (i = 0; i < NUM_OBJECT_TYPES; i++)
340         if (!FcObjectHashInsert (&_FcBaseObjectTypes[i], FcFalse))
341             return FcFalse;
342     return FcTrue;
343 }
344
345 void
346 FcObjectFini (void)
347 {
348     int             i;
349     FcObjectBucket  *b, *next;
350
351     for (i = 0; i < OBJECT_HASH_SIZE; i++)
352     {
353         for (b = FcObjectBuckets[i]; b; b = next)
354         {
355             next = b->next;
356             free (b);
357         }
358         FcObjectBuckets[i] = 0;
359     }
360     for (i = 0; i < FcObjectsNumber; i++)
361         if (FcObjects[i].type == -1)
362             free ((void*) FcObjects[i].object);
363     if (FcObjects != _FcBaseObjectTypes)
364         free (FcObjects);
365     FcObjects = (FcObjectType *) _FcBaseObjectTypes;
366     FcObjectsNumber = NUM_OBJECT_TYPES;
367     FcObjectsSize = 0;
368     FcObjectsInited = FcFalse;
369 }
370
371 const char *
372 FcObjectName (FcObject object)
373 {
374     FcObjectType    *o = FcObjectFindById (object);
375
376     if (o)
377         return o->object;
378     return NULL;
379 }
380
381 static const FcConstant _FcBaseConstants[] = {
382     { (FcChar8 *) "thin",           "weight",   FC_WEIGHT_THIN, },
383     { (FcChar8 *) "extralight",     "weight",   FC_WEIGHT_EXTRALIGHT, },
384     { (FcChar8 *) "ultralight",     "weight",   FC_WEIGHT_EXTRALIGHT, },
385     { (FcChar8 *) "light",          "weight",   FC_WEIGHT_LIGHT, },
386     { (FcChar8 *) "book",           "weight",   FC_WEIGHT_BOOK, },
387     { (FcChar8 *) "regular",        "weight",   FC_WEIGHT_REGULAR, },
388     { (FcChar8 *) "medium",         "weight",   FC_WEIGHT_MEDIUM, },
389     { (FcChar8 *) "demibold",       "weight",   FC_WEIGHT_DEMIBOLD, },
390     { (FcChar8 *) "semibold",       "weight",   FC_WEIGHT_DEMIBOLD, },
391     { (FcChar8 *) "bold",           "weight",   FC_WEIGHT_BOLD, },
392     { (FcChar8 *) "extrabold",      "weight",   FC_WEIGHT_EXTRABOLD, },
393     { (FcChar8 *) "ultrabold",      "weight",   FC_WEIGHT_EXTRABOLD, },
394     { (FcChar8 *) "black",          "weight",   FC_WEIGHT_BLACK, },
395     { (FcChar8 *) "heavy",          "weight",   FC_WEIGHT_HEAVY, },
396
397     { (FcChar8 *) "roman",          "slant",    FC_SLANT_ROMAN, },
398     { (FcChar8 *) "italic",         "slant",    FC_SLANT_ITALIC, },
399     { (FcChar8 *) "oblique",        "slant",    FC_SLANT_OBLIQUE, },
400
401     { (FcChar8 *) "ultracondensed", "width",    FC_WIDTH_ULTRACONDENSED },
402     { (FcChar8 *) "extracondensed", "width",    FC_WIDTH_EXTRACONDENSED },
403     { (FcChar8 *) "condensed",      "width",    FC_WIDTH_CONDENSED },
404     { (FcChar8 *) "semicondensed", "width",     FC_WIDTH_SEMICONDENSED },
405     { (FcChar8 *) "normal",         "width",    FC_WIDTH_NORMAL },
406     { (FcChar8 *) "semiexpanded",   "width",    FC_WIDTH_SEMIEXPANDED },
407     { (FcChar8 *) "expanded",       "width",    FC_WIDTH_EXPANDED },
408     { (FcChar8 *) "extraexpanded",  "width",    FC_WIDTH_EXTRAEXPANDED },
409     { (FcChar8 *) "ultraexpanded",  "width",    FC_WIDTH_ULTRAEXPANDED },
410     
411     { (FcChar8 *) "proportional",   "spacing",  FC_PROPORTIONAL, },
412     { (FcChar8 *) "dual",           "spacing",  FC_DUAL, },
413     { (FcChar8 *) "mono",           "spacing",  FC_MONO, },
414     { (FcChar8 *) "charcell",       "spacing",  FC_CHARCELL, },
415
416     { (FcChar8 *) "unknown",        "rgba",         FC_RGBA_UNKNOWN },
417     { (FcChar8 *) "rgb",            "rgba",         FC_RGBA_RGB, },
418     { (FcChar8 *) "bgr",            "rgba",         FC_RGBA_BGR, },
419     { (FcChar8 *) "vrgb",           "rgba",         FC_RGBA_VRGB },
420     { (FcChar8 *) "vbgr",           "rgba",         FC_RGBA_VBGR },
421     { (FcChar8 *) "none",           "rgba",         FC_RGBA_NONE },
422
423     { (FcChar8 *) "hintnone",       "hintstyle",   FC_HINT_NONE },
424     { (FcChar8 *) "hintslight",     "hintstyle",   FC_HINT_SLIGHT },
425     { (FcChar8 *) "hintmedium",     "hintstyle",   FC_HINT_MEDIUM },
426     { (FcChar8 *) "hintfull",       "hintstyle",   FC_HINT_FULL },
427
428     { (FcChar8 *) "antialias",      "antialias",    FcTrue },
429     { (FcChar8 *) "hinting",        "hinting",      FcTrue },
430     { (FcChar8 *) "verticallayout", "verticallayout",   FcTrue },
431     { (FcChar8 *) "autohint",       "autohint",     FcTrue },
432     { (FcChar8 *) "globaladvance",  "globaladvance",    FcTrue },
433     { (FcChar8 *) "outline",        "outline",      FcTrue },
434     { (FcChar8 *) "scalable",       "scalable",     FcTrue },
435     { (FcChar8 *) "minspace",       "minspace",     FcTrue },
436     { (FcChar8 *) "embolden",       "embolden",     FcTrue },
437     { (FcChar8 *) "embeddedbitmap", "embeddedbitmap",   FcTrue },
438     { (FcChar8 *) "decorative",     "decorative",   FcTrue },
439     { (FcChar8 *) "lcdnone",        "lcdfilter",    FC_LCD_NONE },
440     { (FcChar8 *) "lcddefault",     "lcdfilter",    FC_LCD_DEFAULT },
441     { (FcChar8 *) "lcdlight",       "lcdfilter",    FC_LCD_LIGHT },
442     { (FcChar8 *) "lcdlegacy",      "lcdfilter",    FC_LCD_LEGACY },
443 };
444
445 #define NUM_FC_CONSTANTS   (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
446
447 typedef struct _FcConstantList FcConstantList;
448
449 struct _FcConstantList {
450     const FcConstantList    *next;
451     const FcConstant        *consts;
452     int                     nconsts;
453 };
454
455 static const FcConstantList _FcBaseConstantList = {
456     0,
457     _FcBaseConstants,
458     NUM_FC_CONSTANTS
459 };
460
461 static const FcConstantList     *_FcConstants = &_FcBaseConstantList;
462
463 FcBool
464 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
465 {
466     FcConstantList      *l;
467
468     l = (FcConstantList *) malloc (sizeof (FcConstantList));
469     if (!l)
470         return FcFalse;
471     FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
472     l->consts = consts;
473     l->nconsts = nconsts;
474     l->next = _FcConstants;
475     _FcConstants = l;
476     return FcTrue;
477 }
478
479 FcBool
480 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
481 {
482     const FcConstantList        *l, **prev;
483
484     for (prev = &_FcConstants; 
485          (l = *prev); 
486          prev = (const FcConstantList **) &(l->next))
487     {
488         if (l->consts == consts && l->nconsts == nconsts)
489         {
490             *prev = l->next;
491             FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
492             free ((void *) l);
493             return FcTrue;
494         }
495     }
496     return FcFalse;
497 }
498
499 const FcConstant *
500 FcNameGetConstant (FcChar8 *string)
501 {
502     const FcConstantList    *l;
503     int                     i;
504
505     for (l = _FcConstants; l; l = l->next)
506     {
507         for (i = 0; i < l->nconsts; i++)
508             if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
509                 return &l->consts[i];
510     }
511     return 0;
512 }
513
514 FcBool
515 FcNameConstant (FcChar8 *string, int *result)
516 {
517     const FcConstant    *c;
518
519     if ((c = FcNameGetConstant(string)))
520     {
521         *result = c->value;
522         return FcTrue;
523     }
524     return FcFalse;
525 }
526
527 FcBool
528 FcNameBool (const FcChar8 *v, FcBool *result)
529 {
530     char    c0, c1;
531
532     c0 = *v;
533     c0 = FcToLower (c0);
534     if (c0 == 't' || c0 == 'y' || c0 == '1')
535     {
536         *result = FcTrue;
537         return FcTrue;
538     }
539     if (c0 == 'f' || c0 == 'n' || c0 == '0')
540     {
541         *result = FcFalse;
542         return FcTrue;
543     }
544     if (c0 == 'o')
545     {
546         c1 = v[1];
547         c1 = FcToLower (c1);
548         if (c1 == 'n')
549         {
550             *result = FcTrue;
551             return FcTrue;
552         }
553         if (c1 == 'f')
554         {
555             *result = FcFalse;
556             return FcTrue;
557         }
558     }
559     return FcFalse;
560 }
561
562 static FcValue
563 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
564 {
565     FcValue     v;
566
567     v.type = type;
568     switch (v.type) {
569     case FcTypeInteger:
570         if (!FcNameConstant (string, &v.u.i))
571             v.u.i = atoi ((char *) string);
572         break;
573     case FcTypeString:
574         v.u.s = FcStrStaticName(string);
575         if (!v.u.s)
576             v.type = FcTypeVoid;
577         break;
578     case FcTypeBool:
579         if (!FcNameBool (string, &v.u.b))
580             v.u.b = FcFalse;
581         break;
582     case FcTypeDouble:
583         v.u.d = strtod ((char *) string, 0);
584         break;
585     case FcTypeMatrix:
586         v.u.m = m;
587         sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
588         break;
589     case FcTypeCharSet:
590         v.u.c = FcNameParseCharSet (string);
591         if (!v.u.c)
592             v.type = FcTypeVoid;
593         break;
594     case FcTypeLangSet:
595         v.u.l = FcNameParseLangSet (string);
596         if (!v.u.l)
597             v.type = FcTypeVoid;
598         break;
599     default:
600         break;
601     }
602     return v;
603 }
604
605 static const FcChar8 *
606 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
607 {
608     FcChar8    c;
609     
610     while ((c = *cur))
611     {
612         if (c == '\\')
613         {
614             ++cur;
615             if (!(c = *cur))
616                 break;
617         }
618         else if (strchr (delim, c))
619             break;
620         ++cur;
621         *save++ = c;
622     }
623     *save = 0;
624     *last = *cur;
625     if (*cur)
626         cur++;
627     return cur;
628 }
629
630 FcPattern *
631 FcNameParse (const FcChar8 *name)
632 {
633     FcChar8             *save;
634     FcPattern           *pat;
635     double              d;
636     FcChar8             *e;
637     FcChar8             delim;
638     FcValue             v;
639     FcMatrix            m;
640     const FcObjectType  *t;
641     const FcConstant    *c;
642
643     /* freed below */
644     save = malloc (strlen ((char *) name) + 1);
645     if (!save)
646         goto bail0;
647     pat = FcPatternCreate ();
648     if (!pat)
649         goto bail1;
650
651     for (;;)
652     {
653         name = FcNameFindNext (name, "-,:", save, &delim);
654         if (save[0])
655         {
656             if (!FcPatternAddString (pat, FC_FAMILY, save))
657                 goto bail2;
658         }
659         if (delim != ',')
660             break;
661     }
662     if (delim == '-')
663     {
664         for (;;)
665         {
666             name = FcNameFindNext (name, "-,:", save, &delim);
667             d = strtod ((char *) save, (char **) &e);
668             if (e != save)
669             {
670                 if (!FcPatternAddDouble (pat, FC_SIZE, d))
671                     goto bail2;
672             }
673             if (delim != ',')
674                 break;
675         }
676     }
677     while (delim == ':')
678     {
679         name = FcNameFindNext (name, "=_:", save, &delim);
680         if (save[0])
681         {
682             if (delim == '=' || delim == '_')
683             {
684                 t = FcNameGetObjectType ((char *) save);
685                 for (;;)
686                 {
687                     name = FcNameFindNext (name, ":,", save, &delim);
688                     if (t)
689                     {
690                         v = FcNameConvert (t->type, save, &m);
691                         if (!FcPatternAdd (pat, t->object, v, FcTrue))
692                         {
693                             switch (v.type) {
694                             case FcTypeCharSet:
695                                 FcCharSetDestroy ((FcCharSet *) v.u.c);
696                                 break;
697                             case FcTypeLangSet:
698                                 FcLangSetDestroy ((FcLangSet *) v.u.l);
699                                 break;
700                             default:
701                                 break;
702                             }
703                             goto bail2;
704                         }
705                         switch (v.type) {
706                         case FcTypeCharSet:
707                             FcCharSetDestroy ((FcCharSet *) v.u.c);
708                             break;
709                         case FcTypeLangSet:
710                             FcLangSetDestroy ((FcLangSet *) v.u.l);
711                             break;
712                         default:
713                             break;
714                         }
715                     }
716                     if (delim != ',')
717                         break;
718                 }
719             }
720             else
721             {
722                 if ((c = FcNameGetConstant (save)))
723                 {
724                     t = FcNameGetObjectType ((char *) c->object);
725                     switch (t->type) {
726                     case FcTypeInteger:
727                     case FcTypeDouble:
728                         if (!FcPatternAddInteger (pat, c->object, c->value))
729                             goto bail2;
730                         break;
731                     case FcTypeBool:
732                         if (!FcPatternAddBool (pat, c->object, c->value))
733                             goto bail2;
734                         break;
735                     default:
736                         break;
737                     }
738                 }
739             }
740         }
741     }
742
743     free (save);
744     return pat;
745
746 bail2:
747     FcPatternDestroy (pat);
748 bail1:
749     free (save);
750 bail0:
751     return 0;
752 }
753 static FcBool
754 FcNameUnparseString (FcStrBuf       *buf, 
755                      const FcChar8  *string,
756                      const FcChar8  *escape)
757 {
758     FcChar8 c;
759     while ((c = *string++))
760     {
761         if (escape && strchr ((char *) escape, (char) c))
762         {
763             if (!FcStrBufChar (buf, escape[0]))
764                 return FcFalse;
765         }
766         if (!FcStrBufChar (buf, c))
767             return FcFalse;
768     }
769     return FcTrue;
770 }
771
772 static FcBool
773 FcNameUnparseValue (FcStrBuf    *buf,
774                     FcValue     *v0,
775                     FcChar8     *escape)
776 {
777     FcChar8     temp[1024];
778     FcValue v = FcValueCanonicalize(v0);
779     
780     switch (v.type) {
781     case FcTypeVoid:
782         return FcTrue;
783     case FcTypeInteger:
784         sprintf ((char *) temp, "%d", v.u.i);
785         return FcNameUnparseString (buf, temp, 0);
786     case FcTypeDouble:
787         sprintf ((char *) temp, "%g", v.u.d);
788         return FcNameUnparseString (buf, temp, 0);
789     case FcTypeString:
790         return FcNameUnparseString (buf, v.u.s, escape);
791     case FcTypeBool:
792         return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
793     case FcTypeMatrix:
794         sprintf ((char *) temp, "%g %g %g %g", 
795                  v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
796         return FcNameUnparseString (buf, temp, 0);
797     case FcTypeCharSet:
798         return FcNameUnparseCharSet (buf, v.u.c);
799     case FcTypeLangSet:
800         return FcNameUnparseLangSet (buf, v.u.l);
801     case FcTypeFTFace:
802         return FcTrue;
803     }
804     return FcFalse;
805 }
806
807 static FcBool
808 FcNameUnparseValueList (FcStrBuf        *buf,
809                         FcValueListPtr  v,
810                         FcChar8         *escape)
811 {
812     while (v)
813     {
814         if (!FcNameUnparseValue (buf, &v->value, escape))
815             return FcFalse;
816         if ((v = FcValueListNext(v)) != NULL)
817             if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
818                 return FcFalse;
819     }
820     return FcTrue;
821 }
822
823 #define FC_ESCAPE_FIXED    "\\-:,"
824 #define FC_ESCAPE_VARIABLE "\\=_:,"
825
826 FcChar8 *
827 FcNameUnparse (FcPattern *pat)
828 {
829     return FcNameUnparseEscaped (pat, FcTrue);
830 }
831
832 FcChar8 *
833 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
834 {
835     FcStrBuf                buf;
836     FcChar8                 buf_static[8192];
837     int                     i;
838     FcPatternElt            *e;
839     const FcObjectTypeList  *l;
840     const FcObjectType      *o;
841
842     FcStrBufInit (&buf, buf_static, sizeof (buf_static));
843     e = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
844     if (e)
845     {
846         if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
847             goto bail0;
848     }
849     e = FcPatternObjectFindElt (pat, FC_SIZE_OBJECT);
850     if (e)
851     {
852         if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
853             goto bail0;
854         if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
855             goto bail0;
856     }
857     for (l = _FcObjectTypes; l; l = l->next)
858     {
859         for (i = 0; i < l->ntypes; i++)
860         {
861             o = &l->types[i];
862             if (!strcmp (o->object, FC_FAMILY) || 
863                 !strcmp (o->object, FC_SIZE) ||
864                 !strcmp (o->object, FC_FILE))
865                 continue;
866             
867             e = FcPatternObjectFindElt (pat, FcObjectFromName (o->object));
868             if (e)
869             {
870                 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
871                     goto bail0;
872                 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
873                     goto bail0;
874                 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
875                     goto bail0;
876                 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? 
877                                              (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
878                     goto bail0;
879             }
880         }
881     }
882     return FcStrBufDone (&buf);
883 bail0:
884     FcStrBufDestroy (&buf);
885     return 0;
886 }
887 #define __fcname__
888 #include "fcaliastail.h"
889 #undef __fcname__