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