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