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