]> git.wh0rd.org - fontconfig.git/blame - src/fcname.c
Bump version to 2.3.93.
[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 },
720298e7 75 { FC_EMBEDDED_BITMAP, FcTypeBool },
24330d27
KP
76};
77
78#define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
79
13cdf607
PL
80static FcObjectType * _FcUserObjectNames = 0;
81static int user_obj_alloc = 0;
14143250 82static int next_basic_offset = NUM_OBJECT_TYPES;
13cdf607 83
24330d27
KP
84typedef struct _FcObjectTypeList FcObjectTypeList;
85
86struct _FcObjectTypeList {
87 const FcObjectTypeList *next;
88 const FcObjectType *types;
89 int ntypes;
14143250 90 int basic_offset;
24330d27
KP
91};
92
93static const FcObjectTypeList _FcBaseObjectTypesList = {
94 0,
95 _FcBaseObjectTypes,
9ab79bdf
PL
96 NUM_OBJECT_TYPES,
97 0
24330d27
KP
98};
99
100static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
101
102FcBool
103FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
104{
105 FcObjectTypeList *l;
106
107 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
108 if (!l)
109 return FcFalse;
9dac3c59 110 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
24330d27
KP
111 l->types = types;
112 l->ntypes = ntypes;
113 l->next = _FcObjectTypes;
14143250
PL
114 l->basic_offset = next_basic_offset;
115 next_basic_offset += ntypes;
24330d27
KP
116 _FcObjectTypes = l;
117 return FcTrue;
118}
119
7f37423d
PL
120static FcBool
121FcNameUnregisterObjectTypesFree (const FcObjectType *types, int ntypes,
122 FcBool do_free)
24330d27
KP
123{
124 const FcObjectTypeList *l, **prev;
125
126 for (prev = &_FcObjectTypes;
127 (l = *prev);
128 prev = (const FcObjectTypeList **) &(l->next))
129 {
130 if (l->types == types && l->ntypes == ntypes)
131 {
132 *prev = l->next;
7f37423d
PL
133 if (do_free) {
134 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
135 free ((void *) l);
136 }
24330d27
KP
137 return FcTrue;
138 }
139 }
140 return FcFalse;
141}
142
7f37423d
PL
143FcBool
144FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
145{
146 return FcNameUnregisterObjectTypesFree (types, ntypes, FcTrue);
147}
148
24330d27
KP
149const FcObjectType *
150FcNameGetObjectType (const char *object)
151{
152 int i;
153 const FcObjectTypeList *l;
154 const FcObjectType *t;
155
156 for (l = _FcObjectTypes; l; l = l->next)
157 {
c6103dfb
PL
158 if (l == (FcObjectTypeList*)_FcUserObjectNames)
159 continue;
160
24330d27
KP
161 for (i = 0; i < l->ntypes; i++)
162 {
163 t = &l->types[i];
bc9469ba 164 if (!strcmp (object, t->object))
24330d27
KP
165 return t;
166 }
167 }
168 return 0;
169}
170
13cdf607
PL
171#define OBJECT_HASH_SIZE 31
172struct objectBucket {
173 struct objectBucket *next;
174 FcChar32 hash;
175 int id;
176};
177static struct objectBucket *FcObjectBuckets[OBJECT_HASH_SIZE];
178
14143250
PL
179/* Design constraint: biggest_known_ntypes must never change
180 * after any call to FcNameRegisterObjectTypes. */
13cdf607
PL
181static const FcObjectType *biggest_known_types = _FcBaseObjectTypes;
182static FcBool allocated_biggest_known_types;
183static int biggest_known_ntypes = NUM_OBJECT_TYPES;
184static int biggest_known_count = 0;
185static char * biggest_ptr;
186
187
7f37423d
PL
188static FcObjectPtr
189FcObjectToPtrLookup (const char * object)
4262e0b3 190{
13cdf607 191 FcObjectPtr i = 0, n;
4262e0b3 192 const FcObjectTypeList *l;
13cdf607 193 FcObjectType *t = _FcUserObjectNames;
7f37423d 194
4262e0b3
PL
195 for (l = _FcObjectTypes; l; l = l->next)
196 {
197 for (i = 0; i < l->ntypes; i++)
198 {
13cdf607 199 t = (FcObjectType *)&l->types[i];
4262e0b3 200 if (!strcmp (object, t->object))
13cdf607
PL
201 {
202 if (l == (FcObjectTypeList*)_FcUserObjectNames)
14143250 203 return -i;
13cdf607 204 else
14143250 205 return l->basic_offset + i;
13cdf607 206 }
4262e0b3
PL
207 }
208 }
4262e0b3 209
13cdf607
PL
210 /* We didn't match. Look for the correct FcObjectTypeList
211 * to replace it in-place. */
212 for (l = _FcObjectTypes; l; l = l->next)
213 {
214 if (l->types == _FcUserObjectNames)
215 break;
216 }
7f37423d 217
13cdf607
PL
218 if (!_FcUserObjectNames ||
219 (l && l->types == _FcUserObjectNames && user_obj_alloc < l->ntypes))
220 {
221 int nt = user_obj_alloc + 4;
222 FcObjectType * t = realloc (_FcUserObjectNames,
223 nt * sizeof (FcObjectType));
224 if (!t)
14143250 225 return 0;
13cdf607
PL
226 _FcUserObjectNames = t;
227 user_obj_alloc = nt;
228 }
229
230 if (l && l->types == _FcUserObjectNames)
231 {
232 n = l->ntypes;
233 FcNameUnregisterObjectTypesFree (l->types, l->ntypes, FcFalse);
234 }
235 else
236 n = 0;
237
238 FcNameRegisterObjectTypes (_FcUserObjectNames, n+1);
239
240 for (l = _FcObjectTypes; l; l = l->next)
241 {
242 if (l->types == _FcUserObjectNames)
243 {
244 t = (FcObjectType *)l->types;
245 break;
246 }
247 }
248
249 if (!t)
250 return 0;
251
252 t[n].object = object;
253 t[n].type = FcTypeVoid;
254
14143250 255 return -n;
13cdf607 256}
7f37423d
PL
257
258FcObjectPtr
259FcObjectToPtr (const char * name)
260{
261 FcChar32 hash = FcStringHash ((const FcChar8 *) name);
262 struct objectBucket **p;
263 struct objectBucket *b;
264 int size;
265
266 for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next)
267)
268 if (b->hash == hash && !strcmp (name, (char *) (b + 1)))
269 return b->id;
270 size = sizeof (struct objectBucket) + strlen (name) + 1;
23787a8f
PL
271 /* workaround glibc bug which reads strlen in groups of 4 */
272 b = malloc (size + sizeof (int));
273 FcMemAlloc (FC_MEM_STATICSTR, size + sizeof(int));
7f37423d 274 if (!b)
14143250 275 return 0;
7f37423d
PL
276 b->next = 0;
277 b->hash = hash;
278 b->id = FcObjectToPtrLookup (name);
279 strcpy ((char *) (b + 1), name);
280 *p = b;
281 return b->id;
282}
283
284void
285FcObjectStaticNameFini (void)
286{
287 int i, size;
288 struct objectBucket *b, *next;
289 char *name;
290
291 for (i = 0; i < OBJECT_HASH_SIZE; i++)
292 {
293 for (b = FcObjectBuckets[i]; b; b = next)
294 {
295 next = b->next;
296 name = (char *) (b + 1);
297 size = sizeof (struct objectBucket) + strlen (name) + 1;
298 FcMemFree (FC_MEM_STATICSTR, size);
299 free (b);
300 }
301 FcObjectBuckets[i] = 0;
302 }
303}
304
4262e0b3
PL
305const char *
306FcObjectPtrU (FcObjectPtr si)
307{
14143250
PL
308 const FcObjectTypeList *l;
309 int i, j;
310
311 if (si > 0)
312 {
313 if (si < biggest_known_ntypes)
314 return biggest_known_types[si].object;
315
316 j = 0;
317 for (l = _FcObjectTypes; l; l = l->next)
318 for (i = 0; i < l->ntypes; i++, j++)
319 if (j == si)
320 return l->types[i].object;
321 }
322
323 return _FcUserObjectNames[-si].object;
4262e0b3
PL
324}
325
326int
7f37423d 327FcObjectNeededBytes ()
4262e0b3 328{
7f37423d
PL
329 int num = 0, i;
330 for (i = 0; i < biggest_known_ntypes; i++)
331 {
332 const char * t = biggest_known_types[i].object;
333 num = num + strlen(t) + 1;
334 }
335 biggest_known_count = num;
336 return num + sizeof(int);
4262e0b3
PL
337}
338
7fd7221e
PL
339int
340FcObjectNeededBytesAlign (void)
341{
342 return __alignof__ (int) + __alignof__ (char);
343}
344
4262e0b3
PL
345void *
346FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
347{
7fd7221e 348 block_ptr = ALIGN (block_ptr, int);
1c5b6345 349 *(int *)block_ptr = biggest_known_ntypes;
7f37423d 350 block_ptr = (int *) block_ptr + 1;
7fd7221e 351 block_ptr = ALIGN (block_ptr, char);
1c5b6345 352 biggest_ptr = block_ptr;
7f37423d 353 block_ptr = (char *) block_ptr + biggest_known_count;
4262e0b3
PL
354 return block_ptr;
355}
356
7f37423d
PL
357void
358FcObjectSerialize ()
4262e0b3 359{
7f37423d
PL
360 int i;
361 for (i = 0; i < biggest_known_ntypes; i++)
362 {
363 const char * t = biggest_known_types[i].object;
364 strcpy (biggest_ptr, t);
365 biggest_ptr = biggest_ptr + strlen(t) + 1;
366 }
4262e0b3
PL
367}
368
7f37423d 369void *
61571f3f 370FcObjectUnserialize (FcCache * metadata, void *block_ptr)
4262e0b3 371{
7f37423d
PL
372 int new_biggest;
373 new_biggest = *(int *)block_ptr;
1c5b6345 374 block_ptr = ALIGN (block_ptr, int);
7f37423d
PL
375 block_ptr = (int *) block_ptr + 1;
376 if (biggest_known_ntypes < new_biggest)
377 {
378 int i;
379 char * bp = (char *)block_ptr;
380 FcObjectType * bn;
381 FcObjectTypeList * bnl;
382
383 bn = malloc (sizeof (const FcObjectType) * (new_biggest + 1));
384 if (!bn)
385 return 0;
386
387 bnl = malloc (sizeof (FcObjectTypeList));
388 if (!bnl)
389 {
390 free (bn);
391 return 0;
392 }
393
394 for (i = 0; i < new_biggest; i++)
395 {
396 const FcObjectType * t = FcNameGetObjectType(bp);
397 if (t)
398 bn[i].type = t->type;
399 else
400 bn[i].type = FcTypeVoid;
401 bn[i].object = bp;
402 bp = bp + strlen(bp) + 1;
403 }
404
405 FcNameUnregisterObjectTypesFree (biggest_known_types, biggest_known_ntypes, FcFalse);
406 if (allocated_biggest_known_types)
407 {
408 free ((FcObjectTypeList *)biggest_known_types);
409 }
410 else
411 allocated_biggest_known_types = FcTrue;
412
413 FcNameRegisterObjectTypes (bn, new_biggest);
414 biggest_known_ntypes = new_biggest;
415 biggest_known_types = (const FcObjectType *)bn;
416 }
1c5b6345 417 block_ptr = ALIGN (block_ptr, char);
7f37423d
PL
418 block_ptr = (char *) block_ptr + biggest_known_count;
419 return block_ptr;
4262e0b3
PL
420}
421
24330d27 422static const FcConstant _FcBaseConstants[] = {
81fa16c3
KP
423 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
424 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
425 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
ccb3e93b 426 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
1f71c4d8 427 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
81fa16c3 428 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
ccb3e93b
KP
429 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
430 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
81fa16c3 431 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
ccb3e93b 432 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
81fa16c3
KP
433 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
434 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
ccb3e93b
KP
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
81fa16c3
KP
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
ccb3e93b 451 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
a05d257f 452 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
ccb3e93b
KP
453 { (FcChar8 *) "mono", "spacing", FC_MONO, },
454 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
455
1852d490 456 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
ccb3e93b
KP
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 },
1852d490 461 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
f077d662
OT
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 },
24330d27
KP
467};
468
469#define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
470
471typedef struct _FcConstantList FcConstantList;
472
473struct _FcConstantList {
474 const FcConstantList *next;
475 const FcConstant *consts;
476 int nconsts;
477};
478
479static const FcConstantList _FcBaseConstantList = {
480 0,
481 _FcBaseConstants,
482 NUM_FC_CONSTANTS
483};
484
485static const FcConstantList *_FcConstants = &_FcBaseConstantList;
486
487FcBool
488FcNameRegisterConstants (const FcConstant *consts, int nconsts)
489{
490 FcConstantList *l;
491
492 l = (FcConstantList *) malloc (sizeof (FcConstantList));
493 if (!l)
494 return FcFalse;
9dac3c59 495 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
24330d27
KP
496 l->consts = consts;
497 l->nconsts = nconsts;
498 l->next = _FcConstants;
499 _FcConstants = l;
500 return FcTrue;
501}
502
503FcBool
504FcNameUnregisterConstants (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;
9dac3c59 515 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
24330d27
KP
516 free ((void *) l);
517 return FcTrue;
518 }
519 }
520 return FcFalse;
521}
522
523const FcConstant *
ccb3e93b 524FcNameGetConstant (FcChar8 *string)
24330d27
KP
525{
526 const FcConstantList *l;
527 int i;
94421e40 528
24330d27
KP
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
538FcBool
ccb3e93b 539FcNameConstant (FcChar8 *string, int *result)
24330d27
KP
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
551FcBool
ca60d2b5 552FcNameBool (const FcChar8 *v, FcBool *result)
24330d27
KP
553{
554 char c0, c1;
555
556 c0 = *v;
94421e40 557 c0 = FcToLower (c0);
24330d27
KP
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];
94421e40 571 c1 = FcToLower (c1);
24330d27
KP
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
586static FcValue
ccb3e93b 587FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
24330d27
KP
588{
589 FcValue v;
590
591 v.type = type;
592 switch (v.type) {
593 case FcTypeInteger:
594 if (!FcNameConstant (string, &v.u.i))
ccb3e93b 595 v.u.i = atoi ((char *) string);
24330d27
KP
596 break;
597 case FcTypeString:
7f37423d 598 v.u.s = FcStrStaticName(string);
24330d27
KP
599 break;
600 case FcTypeBool:
601 if (!FcNameBool (string, &v.u.b))
602 v.u.b = FcFalse;
603 break;
604 case FcTypeDouble:
ccb3e93b 605 v.u.d = strtod ((char *) string, 0);
24330d27
KP
606 break;
607 case FcTypeMatrix:
4262e0b3 608 v.u.m = m;
ccb3e93b 609 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
24330d27
KP
610 break;
611 case FcTypeCharSet:
4262e0b3 612 v.u.c = FcNameParseCharSet (string);
24330d27 613 break;
d8d73958 614 case FcTypeLangSet:
4262e0b3 615 v.u.l = FcNameParseLangSet (string);
d8d73958 616 break;
24330d27
KP
617 default:
618 break;
619 }
620 return v;
621}
622
ccb3e93b
KP
623static const FcChar8 *
624FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
24330d27 625{
ccb3e93b 626 FcChar8 c;
24330d27
KP
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
648FcPattern *
ccb3e93b 649FcNameParse (const FcChar8 *name)
24330d27 650{
ccb3e93b 651 FcChar8 *save;
24330d27
KP
652 FcPattern *pat;
653 double d;
ccb3e93b
KP
654 FcChar8 *e;
655 FcChar8 delim;
24330d27
KP
656 FcValue v;
657 FcMatrix m;
658 const FcObjectType *t;
659 const FcConstant *c;
660
9dac3c59 661 /* freed below */
ccb3e93b 662 save = malloc (strlen ((char *) name) + 1);
24330d27
KP
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);
ccb3e93b 685 d = strtod ((char *) save, (char **) &e);
24330d27
KP
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 {
ccb3e93b 702 t = FcNameGetObjectType ((char *) save);
24330d27
KP
703 for (;;)
704 {
705 name = FcNameFindNext (name, ":,", save, &delim);
c552f59b 706 if (t)
24330d27
KP
707 {
708 v = FcNameConvert (t->type, save, &m);
709 if (!FcPatternAdd (pat, t->object, v, FcTrue))
710 {
d8d73958
KP
711 switch (v.type) {
712 case FcTypeCharSet:
4262e0b3 713 FcCharSetDestroy ((FcCharSet *) v.u.c);
d8d73958
KP
714 break;
715 case FcTypeLangSet:
4262e0b3 716 FcLangSetDestroy ((FcLangSet *) v.u.l);
d8d73958
KP
717 break;
718 default:
719 break;
720 }
24330d27
KP
721 goto bail2;
722 }
d8d73958
KP
723 switch (v.type) {
724 case FcTypeCharSet:
4262e0b3 725 FcCharSetDestroy ((FcCharSet *) v.u.c);
d8d73958
KP
726 break;
727 case FcTypeLangSet:
4262e0b3 728 FcLangSetDestroy ((FcLangSet *) v.u.l);
d8d73958
KP
729 break;
730 default:
731 break;
732 }
24330d27
KP
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
752bail2:
753 FcPatternDestroy (pat);
754bail1:
755 free (save);
756bail0:
757 return 0;
758}
24330d27 759static FcBool
c2e7c611 760FcNameUnparseString (FcStrBuf *buf,
24330d27
KP
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 {
c2e7c611 769 if (!FcStrBufChar (buf, escape[0]))
24330d27
KP
770 return FcFalse;
771 }
c2e7c611 772 if (!FcStrBufChar (buf, c))
24330d27
KP
773 return FcFalse;
774 }
775 return FcTrue;
776}
777
778static FcBool
c2e7c611 779FcNameUnparseValue (FcStrBuf *buf,
4262e0b3 780 FcValue *v0,
24330d27
KP
781 FcChar8 *escape)
782{
783 FcChar8 temp[1024];
4262e0b3 784 FcValue v = FcValueCanonicalize(v0);
24330d27
KP
785
786 switch (v.type) {
787 case FcTypeVoid:
788 return FcTrue;
789 case FcTypeInteger:
790 sprintf ((char *) temp, "%d", v.u.i);
791 return FcNameUnparseString (buf, temp, 0);
792 case FcTypeDouble:
793 sprintf ((char *) temp, "%g", v.u.d);
794 return FcNameUnparseString (buf, temp, 0);
795 case FcTypeString:
4262e0b3 796 return FcNameUnparseString (buf, v.u.s, escape);
24330d27 797 case FcTypeBool:
ccb3e93b 798 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
24330d27
KP
799 case FcTypeMatrix:
800 sprintf ((char *) temp, "%g %g %g %g",
4262e0b3 801 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
24330d27
KP
802 return FcNameUnparseString (buf, temp, 0);
803 case FcTypeCharSet:
4262e0b3 804 return FcNameUnparseCharSet (buf, v.u.c);
d8d73958 805 case FcTypeLangSet:
4262e0b3 806 return FcNameUnparseLangSet (buf, v.u.l);
88c747e2
KP
807 case FcTypeFTFace:
808 return FcTrue;
24330d27
KP
809 }
810 return FcFalse;
811}
812
813static FcBool
c2e7c611 814FcNameUnparseValueList (FcStrBuf *buf,
cd2ec1a9 815 FcValueListPtr v,
ccb3e93b 816 FcChar8 *escape)
24330d27 817{
cd2ec1a9 818 while (FcValueListPtrU(v))
24330d27 819 {
9ab79bdf 820 if (!FcNameUnparseValue (buf, &FcValueListPtrU(v)->value, escape))
24330d27 821 return FcFalse;
cd2ec1a9 822 if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
ccb3e93b 823 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
24330d27
KP
824 return FcFalse;
825 }
826 return FcTrue;
827}
828
829#define FC_ESCAPE_FIXED "\\-:,"
830#define FC_ESCAPE_VARIABLE "\\=_:,"
831
832FcChar8 *
833FcNameUnparse (FcPattern *pat)
2fa3f27e
PL
834{
835 return FcNameUnparseEscaped (pat, FcTrue);
836}
837
838FcChar8 *
839FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
24330d27 840{
c2e7c611 841 FcStrBuf buf;
24330d27
KP
842 FcChar8 buf_static[8192];
843 int i;
844 FcPatternElt *e;
845 const FcObjectTypeList *l;
846 const FcObjectType *o;
847
c2e7c611 848 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
e9be9cd1 849 e = FcPatternFindElt (pat, FC_FAMILY);
24330d27
KP
850 if (e)
851 {
2fa3f27e 852 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
24330d27
KP
853 goto bail0;
854 }
e9be9cd1 855 e = FcPatternFindElt (pat, FC_SIZE);
24330d27
KP
856 if (e)
857 {
ccb3e93b 858 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
24330d27 859 goto bail0;
2fa3f27e 860 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
24330d27
KP
861 goto bail0;
862 }
863 for (l = _FcObjectTypes; l; l = l->next)
864 {
865 for (i = 0; i < l->ntypes; i++)
866 {
867 o = &l->types[i];
868 if (!strcmp (o->object, FC_FAMILY) ||
869 !strcmp (o->object, FC_SIZE) ||
870 !strcmp (o->object, FC_FILE))
871 continue;
872
e9be9cd1 873 e = FcPatternFindElt (pat, o->object);
24330d27
KP
874 if (e)
875 {
ccb3e93b 876 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
24330d27 877 goto bail0;
2fa3f27e 878 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
24330d27 879 goto bail0;
ccb3e93b 880 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
24330d27 881 goto bail0;
2fa3f27e
PL
882 if (!FcNameUnparseValueList (&buf, e->values, escape ?
883 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
24330d27
KP
884 goto bail0;
885 }
886 }
887 }
c2e7c611 888 return FcStrBufDone (&buf);
24330d27 889bail0:
c2e7c611 890 FcStrBufDestroy (&buf);
24330d27
KP
891 return 0;
892}