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