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