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