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