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