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