2 * $RCSId: xc/lib/fontconfig/src/fcname.c,v 1.15 2002/09/26 00:17:28 keithp Exp $
4 * Copyright © 2000 Keith Packard
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.
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.
31 /* Please do not revoke any of these bindings. */
32 static const FcObjectType _FcBaseObjectTypes[] = {
33 { "__DUMMY__", FcTypeVoid, },
34 { FC_FAMILY, FcTypeString, },
35 { FC_FAMILYLANG, FcTypeString, },
36 { FC_STYLE, FcTypeString, },
37 { FC_STYLELANG, FcTypeString, },
38 { FC_FULLNAME, FcTypeString, },
39 { FC_FULLNAMELANG, FcTypeString, },
40 { FC_SLANT, FcTypeInteger, },
41 { FC_WEIGHT, FcTypeInteger, },
42 { FC_WIDTH, FcTypeInteger, },
43 { FC_SIZE, FcTypeDouble, },
44 { FC_ASPECT, FcTypeDouble, },
45 { FC_PIXEL_SIZE, FcTypeDouble, },
46 { FC_SPACING, FcTypeInteger, },
47 { FC_FOUNDRY, FcTypeString, },
48 /* { FC_CORE, FcTypeBool, }, */
49 { FC_ANTIALIAS, FcTypeBool, },
50 { FC_HINT_STYLE, FcTypeInteger, },
51 { FC_HINTING, FcTypeBool, },
52 { FC_VERTICAL_LAYOUT, FcTypeBool, },
53 { FC_AUTOHINT, FcTypeBool, },
54 { FC_GLOBAL_ADVANCE, FcTypeBool, },
55 /* { FC_XLFD, FcTypeString, }, */
56 { FC_FILE, FcTypeString, },
57 { FC_INDEX, FcTypeInteger, },
58 { FC_RASTERIZER, FcTypeString, },
59 { FC_OUTLINE, FcTypeBool, },
60 { FC_SCALABLE, FcTypeBool, },
61 { FC_DPI, FcTypeDouble },
62 { FC_RGBA, FcTypeInteger, },
63 { FC_SCALE, FcTypeDouble, },
64 { FC_MINSPACE, FcTypeBool, },
65 /* { FC_RENDER, FcTypeBool, },*/
66 { FC_CHAR_WIDTH, FcTypeInteger },
67 { FC_CHAR_HEIGHT, FcTypeInteger },
68 { FC_MATRIX, FcTypeMatrix },
69 { FC_CHARSET, FcTypeCharSet },
70 { FC_LANG, FcTypeLangSet },
71 { FC_FONTVERSION, FcTypeInteger },
72 { FC_CAPABILITY, FcTypeString },
73 { FC_FONTFORMAT, FcTypeString },
74 { FC_EMBOLDEN, FcTypeBool },
75 { FC_EMBEDDED_BITMAP, FcTypeBool },
78 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
80 static FcObjectType * _FcUserObjectNames = 0;
81 static int user_obj_alloc = 0;
82 static int next_basic_offset = NUM_OBJECT_TYPES;
84 typedef struct _FcObjectTypeList FcObjectTypeList;
86 struct _FcObjectTypeList {
87 const FcObjectTypeList *next;
88 const FcObjectType *types;
93 static const FcObjectTypeList _FcBaseObjectTypesList = {
99 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
102 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
106 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
109 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
112 l->next = _FcObjectTypes;
113 l->basic_offset = next_basic_offset;
114 next_basic_offset += ntypes;
120 FcNameUnregisterObjectTypesFree (const FcObjectType *types, int ntypes,
123 const FcObjectTypeList *l, **prev;
125 for (prev = &_FcObjectTypes;
127 prev = (const FcObjectTypeList **) &(l->next))
129 if (l->types == types && l->ntypes == ntypes)
133 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
143 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
145 return FcNameUnregisterObjectTypesFree (types, ntypes, FcTrue);
149 FcNameGetObjectType (const char *object)
152 const FcObjectTypeList *l;
153 const FcObjectType *t;
155 for (l = _FcObjectTypes; l; l = l->next)
157 for (i = 0; i < l->ntypes; i++)
160 if (!strcmp (object, t->object))
167 #define OBJECT_HASH_SIZE 31
168 struct objectBucket {
169 struct objectBucket *next;
173 static struct objectBucket *FcObjectBuckets[OBJECT_HASH_SIZE];
175 /* Design constraint: biggest_known_ntypes must never change
176 * after any call to FcNameRegisterObjectTypes. */
177 static const FcObjectType *biggest_known_types = _FcBaseObjectTypes;
178 static FcBool allocated_biggest_known_types;
179 static int biggest_known_ntypes = NUM_OBJECT_TYPES;
180 static int biggest_known_count = 0;
181 static char * biggest_ptr;
185 FcObjectToPtrLookup (const char * object)
187 FcObjectPtr i = 0, n;
188 const FcObjectTypeList *l;
189 FcObjectType *t = _FcUserObjectNames;
191 for (l = _FcObjectTypes; l; l = l->next)
193 for (i = 0; i < l->ntypes; i++)
195 t = (FcObjectType *)&l->types[i];
196 if (!strcmp (object, t->object))
198 if (l == (FcObjectTypeList*)_FcUserObjectNames)
201 return l->basic_offset + i;
206 /* We didn't match. Look for the correct FcObjectTypeList
207 * to replace it in-place. */
208 for (l = _FcObjectTypes; l; l = l->next)
210 if (l->types == _FcUserObjectNames)
214 if (!_FcUserObjectNames ||
215 (l && l->types == _FcUserObjectNames && user_obj_alloc < l->ntypes))
217 int nt = user_obj_alloc + 4;
218 FcObjectType * t = realloc (_FcUserObjectNames,
219 nt * sizeof (FcObjectType));
222 _FcUserObjectNames = t;
226 if (l && l->types == _FcUserObjectNames)
229 FcNameUnregisterObjectTypesFree (l->types, l->ntypes, FcFalse);
234 FcNameRegisterObjectTypes (_FcUserObjectNames, n+1);
236 for (l = _FcObjectTypes; l; l = l->next)
238 if (l->types == _FcUserObjectNames)
240 t = (FcObjectType *)l->types;
248 t[n].object = object;
249 t[n].type = FcTypeVoid;
255 FcObjectToPtr (const char * name)
257 FcChar32 hash = FcStringHash ((const FcChar8 *) name);
258 struct objectBucket **p;
259 struct objectBucket *b;
262 for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next)
264 if (b->hash == hash && !strcmp (name, (char *) (b + 1)))
266 size = sizeof (struct objectBucket) + strlen (name) + 1;
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));
274 b->id = FcObjectToPtrLookup (name);
275 strcpy ((char *) (b + 1), name);
281 FcObjectStaticNameFini (void)
284 struct objectBucket *b, *next;
287 for (i = 0; i < OBJECT_HASH_SIZE; i++)
289 for (b = FcObjectBuckets[i]; b; b = next)
292 name = (char *) (b + 1);
293 size = sizeof (struct objectBucket) + strlen (name) + 1;
294 FcMemFree (FC_MEM_STATICSTR, size);
297 FcObjectBuckets[i] = 0;
302 FcObjectPtrU (FcObjectPtr si)
304 const FcObjectTypeList *l;
309 if (si < biggest_known_ntypes)
310 return biggest_known_types[si].object;
313 for (l = _FcObjectTypes; l; l = l->next)
314 for (i = 0; i < l->ntypes; i++, j++)
316 return l->types[i].object;
319 return _FcUserObjectNames[-si].object;
323 FcObjectNeededBytes ()
326 for (i = 0; i < biggest_known_ntypes; i++)
328 const char * t = biggest_known_types[i].object;
329 num = num + strlen(t) + 1;
331 biggest_known_count = num;
332 return num + sizeof(int);
336 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
338 *(int *)block_ptr = biggest_known_ntypes;
339 block_ptr = (int *) block_ptr + 1;
340 biggest_ptr = block_ptr;
341 block_ptr = (char *) block_ptr + biggest_known_count;
349 for (i = 0; i < biggest_known_ntypes; i++)
351 const char * t = biggest_known_types[i].object;
352 strcpy (biggest_ptr, t);
353 biggest_ptr = biggest_ptr + strlen(t) + 1;
358 FcObjectUnserialize (FcCache metadata, void *block_ptr)
361 new_biggest = *(int *)block_ptr;
362 block_ptr = (int *) block_ptr + 1;
363 if (biggest_known_ntypes < new_biggest)
366 char * bp = (char *)block_ptr;
368 FcObjectTypeList * bnl;
370 bn = malloc (sizeof (const FcObjectType) * (new_biggest + 1));
374 bnl = malloc (sizeof (FcObjectTypeList));
381 for (i = 0; i < new_biggest; i++)
383 const FcObjectType * t = FcNameGetObjectType(bp);
385 bn[i].type = t->type;
387 bn[i].type = FcTypeVoid;
389 bp = bp + strlen(bp) + 1;
392 FcNameUnregisterObjectTypesFree (biggest_known_types, biggest_known_ntypes, FcFalse);
393 if (allocated_biggest_known_types)
395 free ((FcObjectTypeList *)biggest_known_types);
398 allocated_biggest_known_types = FcTrue;
400 FcNameRegisterObjectTypes (bn, new_biggest);
401 biggest_known_ntypes = new_biggest;
402 biggest_known_types = (const FcObjectType *)bn;
404 block_ptr = (char *) block_ptr + biggest_known_count;
409 FcObjectPtrCompare (const FcObjectPtr a, const FcObjectPtr b)
414 static const FcConstant _FcBaseConstants[] = {
415 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
416 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
417 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
418 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
419 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
420 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
421 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
422 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
423 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
424 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
425 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
426 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
427 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
429 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
430 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
431 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
433 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
434 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
435 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
436 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
437 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
438 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
439 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
440 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
441 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
443 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
444 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
445 { (FcChar8 *) "mono", "spacing", FC_MONO, },
446 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
448 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
449 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
450 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
451 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
452 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
453 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
455 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
456 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
457 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
458 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
461 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
463 typedef struct _FcConstantList FcConstantList;
465 struct _FcConstantList {
466 const FcConstantList *next;
467 const FcConstant *consts;
471 static const FcConstantList _FcBaseConstantList = {
477 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
480 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
484 l = (FcConstantList *) malloc (sizeof (FcConstantList));
487 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
489 l->nconsts = nconsts;
490 l->next = _FcConstants;
496 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
498 const FcConstantList *l, **prev;
500 for (prev = &_FcConstants;
502 prev = (const FcConstantList **) &(l->next))
504 if (l->consts == consts && l->nconsts == nconsts)
507 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
516 FcNameGetConstant (FcChar8 *string)
518 const FcConstantList *l;
521 for (l = _FcConstants; l; l = l->next)
523 for (i = 0; i < l->nconsts; i++)
524 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
525 return &l->consts[i];
531 FcNameConstant (FcChar8 *string, int *result)
535 if ((c = FcNameGetConstant(string)))
544 FcNameBool (const FcChar8 *v, FcBool *result)
550 if (c0 == 't' || c0 == 'y' || c0 == '1')
555 if (c0 == 'f' || c0 == 'n' || c0 == '0')
579 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
586 if (!FcNameConstant (string, &v.u.i))
587 v.u.i = atoi ((char *) string);
590 v.u.s = FcStrStaticName(string);
593 if (!FcNameBool (string, &v.u.b))
597 v.u.d = strtod ((char *) string, 0);
601 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
604 v.u.c = FcNameParseCharSet (string);
607 v.u.l = FcNameParseLangSet (string);
615 static const FcChar8 *
616 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
628 else if (strchr (delim, c))
641 FcNameParse (const FcChar8 *name)
650 const FcObjectType *t;
654 save = malloc (strlen ((char *) name) + 1);
657 pat = FcPatternCreate ();
663 name = FcNameFindNext (name, "-,:", save, &delim);
666 if (!FcPatternAddString (pat, FC_FAMILY, save))
676 name = FcNameFindNext (name, "-,:", save, &delim);
677 d = strtod ((char *) save, (char **) &e);
680 if (!FcPatternAddDouble (pat, FC_SIZE, d))
689 name = FcNameFindNext (name, "=_:", save, &delim);
692 if (delim == '=' || delim == '_')
694 t = FcNameGetObjectType ((char *) save);
697 name = FcNameFindNext (name, ":,", save, &delim);
700 v = FcNameConvert (t->type, save, &m);
701 if (!FcPatternAdd (pat, t->object, v, FcTrue))
705 FcCharSetDestroy ((FcCharSet *) v.u.c);
708 FcLangSetDestroy ((FcLangSet *) v.u.l);
717 FcCharSetDestroy ((FcCharSet *) v.u.c);
720 FcLangSetDestroy ((FcLangSet *) v.u.l);
732 if ((c = FcNameGetConstant (save)))
734 if (!FcPatternAddInteger (pat, c->object, c->value))
745 FcPatternDestroy (pat);
752 FcNameUnparseString (FcStrBuf *buf,
753 const FcChar8 *string,
754 const FcChar8 *escape)
757 while ((c = *string++))
759 if (escape && strchr ((char *) escape, (char) c))
761 if (!FcStrBufChar (buf, escape[0]))
764 if (!FcStrBufChar (buf, c))
771 FcNameUnparseValue (FcStrBuf *buf,
777 FcValue v = FcValueCanonicalize(v0);
783 sprintf ((char *) temp, "%d", v.u.i);
784 return FcNameUnparseString (buf, temp, 0);
786 sprintf ((char *) temp, "%g", v.u.d);
787 return FcNameUnparseString (buf, temp, 0);
789 return FcNameUnparseString (buf, v.u.s, escape);
791 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
793 sprintf ((char *) temp, "%g %g %g %g",
794 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
795 return FcNameUnparseString (buf, temp, 0);
797 return FcNameUnparseCharSet (buf, v.u.c);
799 return FcNameUnparseLangSet (buf, v.u.l);
807 FcNameUnparseValueList (FcStrBuf *buf,
811 while (FcValueListPtrU(v))
813 if (!FcNameUnparseValue (buf, v.bank, &FcValueListPtrU(v)->value, escape))
815 if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
816 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
822 #define FC_ESCAPE_FIXED "\\-:,"
823 #define FC_ESCAPE_VARIABLE "\\=_:,"
826 FcNameUnparse (FcPattern *pat)
828 return FcNameUnparseEscaped (pat, FcTrue);
832 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
835 FcChar8 buf_static[8192];
838 const FcObjectTypeList *l;
839 const FcObjectType *o;
841 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
842 e = FcPatternFindElt (pat, FC_FAMILY);
845 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
848 e = FcPatternFindElt (pat, FC_SIZE);
851 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
853 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
856 for (l = _FcObjectTypes; l; l = l->next)
858 for (i = 0; i < l->ntypes; i++)
861 if (!strcmp (o->object, FC_FAMILY) ||
862 !strcmp (o->object, FC_SIZE) ||
863 !strcmp (o->object, FC_FILE))
866 e = FcPatternFindElt (pat, o->object);
869 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
871 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
873 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
875 if (!FcNameUnparseValueList (&buf, e->values, escape ?
876 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
881 return FcStrBufDone (&buf);
883 FcStrBufDestroy (&buf);