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 /* The __DUMMY__ object enables callers to distinguish the error return
33 * of FcObjectToPtrLookup from FC_FAMILY's FcObjectPtr, which would
35 static const FcObjectType _FcBaseObjectTypes[] = {
36 { "__DUMMY__", FcTypeVoid, },
37 { FC_FAMILY, FcTypeString, },
38 { FC_FAMILYLANG, FcTypeString, },
39 { FC_STYLE, FcTypeString, },
40 { FC_STYLELANG, FcTypeString, },
41 { FC_FULLNAME, FcTypeString, },
42 { FC_FULLNAMELANG, FcTypeString, },
43 { FC_SLANT, FcTypeInteger, },
44 { FC_WEIGHT, FcTypeInteger, },
45 { FC_WIDTH, FcTypeInteger, },
46 { FC_SIZE, FcTypeDouble, },
47 { FC_ASPECT, FcTypeDouble, },
48 { FC_PIXEL_SIZE, FcTypeDouble, },
49 { FC_SPACING, FcTypeInteger, },
50 { FC_FOUNDRY, FcTypeString, },
51 /* { FC_CORE, FcTypeBool, }, */
52 { FC_ANTIALIAS, FcTypeBool, },
53 { FC_HINT_STYLE, FcTypeInteger, },
54 { FC_HINTING, FcTypeBool, },
55 { FC_VERTICAL_LAYOUT, FcTypeBool, },
56 { FC_AUTOHINT, FcTypeBool, },
57 { FC_GLOBAL_ADVANCE, FcTypeBool, },
58 /* { FC_XLFD, FcTypeString, }, */
59 { FC_FILE, FcTypeString, },
60 { FC_INDEX, FcTypeInteger, },
61 { FC_RASTERIZER, FcTypeString, },
62 { FC_OUTLINE, FcTypeBool, },
63 { FC_SCALABLE, FcTypeBool, },
64 { FC_DPI, FcTypeDouble },
65 { FC_RGBA, FcTypeInteger, },
66 { FC_SCALE, FcTypeDouble, },
67 { FC_MINSPACE, FcTypeBool, },
68 /* { FC_RENDER, FcTypeBool, },*/
69 { FC_CHAR_WIDTH, FcTypeInteger },
70 { FC_CHAR_HEIGHT, FcTypeInteger },
71 { FC_MATRIX, FcTypeMatrix },
72 { FC_CHARSET, FcTypeCharSet },
73 { FC_LANG, FcTypeLangSet },
74 { FC_FONTVERSION, FcTypeInteger },
75 { FC_CAPABILITY, FcTypeString },
76 { FC_FONTFORMAT, FcTypeString },
77 { FC_EMBOLDEN, FcTypeBool },
78 { FC_EMBEDDED_BITMAP, FcTypeBool },
81 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
83 static FcObjectType * _FcUserObjectNames = 0;
84 static int user_obj_alloc = 0;
85 static int next_basic_offset = NUM_OBJECT_TYPES;
87 typedef struct _FcObjectTypeList FcObjectTypeList;
89 struct _FcObjectTypeList {
90 const FcObjectTypeList *next;
91 const FcObjectType *types;
96 static const FcObjectTypeList _FcBaseObjectTypesList = {
103 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
106 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
110 l = (FcObjectTypeList *) malloc (sizeof (FcObjectTypeList));
113 FcMemAlloc (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
116 l->next = _FcObjectTypes;
117 l->basic_offset = next_basic_offset;
118 next_basic_offset += ntypes;
124 FcNameUnregisterObjectTypesFree (const FcObjectType *types, int ntypes,
127 const FcObjectTypeList *l, **prev;
129 for (prev = &_FcObjectTypes;
131 prev = (const FcObjectTypeList **) &(l->next))
133 if (l->types == types && l->ntypes == ntypes)
137 FcMemFree (FC_MEM_OBJECTTYPE, sizeof (FcObjectTypeList));
147 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
149 return FcNameUnregisterObjectTypesFree (types, ntypes, FcTrue);
153 FcNameGetObjectType (const char *object)
156 const FcObjectTypeList *l;
157 const FcObjectType *t;
159 for (l = _FcObjectTypes; l; l = l->next)
161 if (l == (FcObjectTypeList*)_FcUserObjectNames)
164 for (i = 0; i < l->ntypes; i++)
167 if (!strcmp (object, t->object))
174 #define OBJECT_HASH_SIZE 31
175 struct objectBucket {
176 struct objectBucket *next;
180 static struct objectBucket *FcObjectBuckets[OBJECT_HASH_SIZE];
182 /* Design constraint: biggest_known_ntypes must never change
183 * after any call to FcNameRegisterObjectTypes. */
184 static const FcObjectType *biggest_known_types = _FcBaseObjectTypes;
185 static FcBool allocated_biggest_known_types;
186 static int biggest_known_ntypes = NUM_OBJECT_TYPES;
187 static int biggest_known_count = 0;
188 static char * biggest_ptr;
192 FcObjectToPtrLookup (const char * object)
194 FcObjectPtr i = 0, n;
195 const FcObjectTypeList *l;
196 FcObjectType *t = _FcUserObjectNames;
198 for (l = _FcObjectTypes; l; l = l->next)
200 for (i = 0; i < l->ntypes; i++)
202 t = (FcObjectType *)&l->types[i];
203 if (!strcmp (object, t->object))
205 if (l == (FcObjectTypeList*)_FcUserObjectNames)
208 return l->basic_offset + i;
213 /* We didn't match. Look for the application's FcObjectTypeList
214 * and replace it in-place. */
215 for (l = _FcObjectTypes; l; l = l->next)
217 if (l->types == _FcUserObjectNames)
221 if (!_FcUserObjectNames ||
222 (l && l->types == _FcUserObjectNames && user_obj_alloc < l->ntypes))
224 int nt = user_obj_alloc + 4;
225 FcObjectType * t = realloc (_FcUserObjectNames,
226 nt * sizeof (FcObjectType));
229 _FcUserObjectNames = t;
233 if (l && l->types == _FcUserObjectNames)
236 FcNameUnregisterObjectTypesFree (l->types, l->ntypes, FcFalse);
241 FcNameRegisterObjectTypes (_FcUserObjectNames, n+1);
243 for (l = _FcObjectTypes; l; l = l->next)
245 if (l->types == _FcUserObjectNames)
247 t = (FcObjectType *)l->types;
255 t[n].object = object;
256 t[n].type = FcTypeVoid;
262 FcObjectToPtr (const char * name)
264 FcChar32 hash = FcStringHash ((const FcChar8 *) name);
265 struct objectBucket **p;
266 struct objectBucket *b;
269 for (p = &FcObjectBuckets[hash % OBJECT_HASH_SIZE]; (b = *p); p = &(b->next)
271 if (b->hash == hash && !strcmp (name, (char *) (b + 1)))
273 size = sizeof (struct objectBucket) + strlen (name) + 1;
274 /* workaround glibc bug which reads strlen in groups of 4 */
275 b = malloc (size + sizeof (int));
276 FcMemAlloc (FC_MEM_STATICSTR, size + sizeof(int));
281 b->id = FcObjectToPtrLookup (name);
282 strcpy ((char *) (b + 1), name);
288 FcObjectStaticNameFini (void)
291 struct objectBucket *b, *next;
294 for (i = 0; i < OBJECT_HASH_SIZE; i++)
296 for (b = FcObjectBuckets[i]; b; b = next)
299 name = (char *) (b + 1);
300 size = sizeof (struct objectBucket) + strlen (name) + 1;
301 FcMemFree (FC_MEM_STATICSTR, size);
304 FcObjectBuckets[i] = 0;
309 FcObjectPtrU (FcObjectPtr si)
311 const FcObjectTypeList *l;
316 if (si < biggest_known_ntypes)
317 return biggest_known_types[si].object;
320 for (l = _FcObjectTypes; l; l = l->next)
321 for (i = 0; i < l->ntypes; i++, j++)
323 return l->types[i].object;
326 return _FcUserObjectNames[-si].object;
330 FcObjectNeededBytes ()
333 for (i = 0; i < biggest_known_ntypes; i++)
335 const char * t = biggest_known_types[i].object;
336 num = num + strlen(t) + 1;
338 biggest_known_count = num;
339 return num + sizeof(int);
343 FcObjectNeededBytesAlign (void)
345 return fc_alignof (int) + fc_alignof (char);
349 FcObjectDistributeBytes (FcCache * metadata, void * block_ptr)
351 block_ptr = ALIGN (block_ptr, int);
352 *(int *)block_ptr = biggest_known_ntypes;
353 block_ptr = (int *) block_ptr + 1;
354 block_ptr = ALIGN (block_ptr, char);
355 biggest_ptr = block_ptr;
356 block_ptr = (char *) block_ptr + biggest_known_count;
361 FcObjectSerialize (void)
364 for (i = 0; i < biggest_known_ntypes; i++)
366 const char * t = biggest_known_types[i].object;
367 strcpy (biggest_ptr, t);
368 biggest_ptr = biggest_ptr + strlen(t) + 1;
373 FcObjectUnserialize (FcCache * metadata, void *block_ptr)
376 new_biggest = *(int *)block_ptr;
377 block_ptr = ALIGN (block_ptr, int);
378 block_ptr = (int *) block_ptr + 1;
379 if (biggest_known_ntypes < new_biggest)
382 char * bp = (char *)block_ptr;
384 FcObjectTypeList * bnl;
386 bn = malloc (sizeof (const FcObjectType) * (new_biggest + 1));
390 bnl = malloc (sizeof (FcObjectTypeList));
397 for (i = 0; i < new_biggest; i++)
399 const FcObjectType * t = FcNameGetObjectType(bp);
401 bn[i].type = t->type;
403 bn[i].type = FcTypeVoid;
405 bp = bp + strlen(bp) + 1;
408 FcNameUnregisterObjectTypesFree (biggest_known_types, biggest_known_ntypes, FcFalse);
409 if (allocated_biggest_known_types)
411 free ((FcObjectTypeList *)biggest_known_types);
414 allocated_biggest_known_types = FcTrue;
416 FcNameRegisterObjectTypes (bn, new_biggest);
417 biggest_known_ntypes = new_biggest;
418 biggest_known_types = (const FcObjectType *)bn;
420 block_ptr = ALIGN (block_ptr, char);
421 block_ptr = (char *) block_ptr + biggest_known_count;
425 static const FcConstant _FcBaseConstants[] = {
426 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
427 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
428 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
429 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
430 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
431 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
432 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
433 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
434 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
435 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
436 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
437 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
438 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
440 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
441 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
442 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
444 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
445 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
446 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
447 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
448 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
449 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
450 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
451 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
452 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
454 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
455 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
456 { (FcChar8 *) "mono", "spacing", FC_MONO, },
457 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
459 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
460 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
461 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
462 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
463 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
464 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
466 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
467 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
468 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
469 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
472 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
474 typedef struct _FcConstantList FcConstantList;
476 struct _FcConstantList {
477 const FcConstantList *next;
478 const FcConstant *consts;
482 static const FcConstantList _FcBaseConstantList = {
488 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
491 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
495 l = (FcConstantList *) malloc (sizeof (FcConstantList));
498 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
500 l->nconsts = nconsts;
501 l->next = _FcConstants;
507 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
509 const FcConstantList *l, **prev;
511 for (prev = &_FcConstants;
513 prev = (const FcConstantList **) &(l->next))
515 if (l->consts == consts && l->nconsts == nconsts)
518 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
527 FcNameGetConstant (FcChar8 *string)
529 const FcConstantList *l;
532 for (l = _FcConstants; l; l = l->next)
534 for (i = 0; i < l->nconsts; i++)
535 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
536 return &l->consts[i];
542 FcNameConstant (FcChar8 *string, int *result)
546 if ((c = FcNameGetConstant(string)))
555 FcNameBool (const FcChar8 *v, FcBool *result)
561 if (c0 == 't' || c0 == 'y' || c0 == '1')
566 if (c0 == 'f' || c0 == 'n' || c0 == '0')
590 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
597 if (!FcNameConstant (string, &v.u.i))
598 v.u.i = atoi ((char *) string);
601 v.u.s = FcStrStaticName(string);
604 if (!FcNameBool (string, &v.u.b))
608 v.u.d = strtod ((char *) string, 0);
612 sscanf ((char *) string, "%lg %lg %lg %lg", &m->xx, &m->xy, &m->yx, &m->yy);
615 v.u.c = FcNameParseCharSet (string);
618 v.u.l = FcNameParseLangSet (string);
626 static const FcChar8 *
627 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
639 else if (strchr (delim, c))
652 FcNameParse (const FcChar8 *name)
661 const FcObjectType *t;
665 save = malloc (strlen ((char *) name) + 1);
668 pat = FcPatternCreate ();
674 name = FcNameFindNext (name, "-,:", save, &delim);
677 if (!FcPatternAddString (pat, FC_FAMILY, save))
687 name = FcNameFindNext (name, "-,:", save, &delim);
688 d = strtod ((char *) save, (char **) &e);
691 if (!FcPatternAddDouble (pat, FC_SIZE, d))
700 name = FcNameFindNext (name, "=_:", save, &delim);
703 if (delim == '=' || delim == '_')
705 t = FcNameGetObjectType ((char *) save);
708 name = FcNameFindNext (name, ":,", save, &delim);
709 if (t && strcmp (t->object, _FcBaseObjectTypes[0].object))
711 v = FcNameConvert (t->type, save, &m);
712 if (!FcPatternAdd (pat, t->object, v, FcTrue))
716 FcCharSetDestroy ((FcCharSet *) v.u.c);
719 FcLangSetDestroy ((FcLangSet *) v.u.l);
728 FcCharSetDestroy ((FcCharSet *) v.u.c);
731 FcLangSetDestroy ((FcLangSet *) v.u.l);
743 if ((c = FcNameGetConstant (save)))
745 if (!FcPatternAddInteger (pat, c->object, c->value))
756 FcPatternDestroy (pat);
763 FcNameUnparseString (FcStrBuf *buf,
764 const FcChar8 *string,
765 const FcChar8 *escape)
768 while ((c = *string++))
770 if (escape && strchr ((char *) escape, (char) c))
772 if (!FcStrBufChar (buf, escape[0]))
775 if (!FcStrBufChar (buf, c))
782 FcNameUnparseValue (FcStrBuf *buf,
787 FcValue v = FcValueCanonicalize(v0);
793 sprintf ((char *) temp, "%d", v.u.i);
794 return FcNameUnparseString (buf, temp, 0);
796 sprintf ((char *) temp, "%g", v.u.d);
797 return FcNameUnparseString (buf, temp, 0);
799 return FcNameUnparseString (buf, v.u.s, escape);
801 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
803 sprintf ((char *) temp, "%g %g %g %g",
804 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
805 return FcNameUnparseString (buf, temp, 0);
807 return FcNameUnparseCharSet (buf, v.u.c);
809 return FcNameUnparseLangSet (buf, v.u.l);
817 FcNameUnparseValueList (FcStrBuf *buf,
821 while (FcValueListPtrU(v))
823 if (!FcNameUnparseValue (buf, &FcValueListPtrU(v)->value, escape))
825 if (FcValueListPtrU(v = FcValueListPtrU(v)->next))
826 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
832 #define FC_ESCAPE_FIXED "\\-:,"
833 #define FC_ESCAPE_VARIABLE "\\=_:,"
836 FcNameUnparse (FcPattern *pat)
838 return FcNameUnparseEscaped (pat, FcTrue);
842 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
845 FcChar8 buf_static[8192];
848 const FcObjectTypeList *l;
849 const FcObjectType *o;
851 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
852 e = FcPatternFindElt (pat, FC_FAMILY);
855 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
858 e = FcPatternFindElt (pat, FC_SIZE);
861 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
863 if (!FcNameUnparseValueList (&buf, e->values, escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
866 for (l = _FcObjectTypes; l; l = l->next)
868 for (i = 0; i < l->ntypes; i++)
871 if (!strcmp (o->object, FC_FAMILY) ||
872 !strcmp (o->object, FC_SIZE) ||
873 !strcmp (o->object, FC_FILE))
876 e = FcPatternFindElt (pat, o->object);
879 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
881 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
883 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
885 if (!FcNameUnparseValueList (&buf, e->values, escape ?
886 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
891 return FcStrBufDone (&buf);
893 FcStrBufDestroy (&buf);