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