2 * fontconfig/src/fcname.c
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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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.
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.
37 static const FcObjectType _FcBaseObjectTypes[] = {
38 { FC_FAMILY, FcTypeString, }, /* 1 */
39 { FC_FAMILYLANG, FcTypeString, },
40 { FC_STYLE, FcTypeString, },
41 { FC_STYLELANG, FcTypeString, },
42 { FC_FULLNAME, FcTypeString, },
43 { FC_FULLNAMELANG, FcTypeString, },
44 { FC_SLANT, FcTypeInteger, },
45 { FC_WEIGHT, FcTypeInteger, },
46 { FC_WIDTH, FcTypeInteger, },
47 { FC_SIZE, FcTypeDouble, },
48 { FC_ASPECT, FcTypeDouble, },
49 { FC_PIXEL_SIZE, FcTypeDouble, },
50 { FC_SPACING, FcTypeInteger, },
51 { FC_FOUNDRY, FcTypeString, },
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_FILE, FcTypeString, },
59 { FC_INDEX, FcTypeInteger, },
60 { FC_RASTERIZER, FcTypeString, },
61 { FC_OUTLINE, FcTypeBool, },
62 { FC_SCALABLE, FcTypeBool, },
63 { FC_DPI, FcTypeDouble },
64 { FC_RGBA, FcTypeInteger, },
65 { FC_SCALE, FcTypeDouble, },
66 { FC_MINSPACE, FcTypeBool, },
67 { FC_CHAR_WIDTH, FcTypeInteger },
68 { FC_CHAR_HEIGHT, FcTypeInteger },
69 { FC_MATRIX, FcTypeMatrix },
70 { FC_CHARSET, FcTypeCharSet },
71 { FC_LANG, FcTypeLangSet },
72 { FC_FONTVERSION, FcTypeInteger },
73 { FC_CAPABILITY, FcTypeString },
74 { FC_FONTFORMAT, FcTypeString },
75 { FC_EMBOLDEN, FcTypeBool },
76 { FC_EMBEDDED_BITMAP, FcTypeBool },
77 { FC_DECORATIVE, FcTypeBool },
78 { FC_LCD_FILTER, FcTypeInteger }, /* 41 */
81 #define NUM_OBJECT_TYPES (sizeof _FcBaseObjectTypes / sizeof _FcBaseObjectTypes[0])
83 typedef struct _FcObjectTypeList FcObjectTypeList;
85 struct _FcObjectTypeList {
86 const FcObjectTypeList *next;
87 const FcObjectType *types;
91 static const FcObjectTypeList _FcBaseObjectTypesList = {
97 static const FcObjectTypeList *_FcObjectTypes = &_FcBaseObjectTypesList;
99 #define OBJECT_HASH_SIZE 31
101 typedef struct _FcObjectBucket {
102 struct _FcObjectBucket *next;
107 static FcObjectBucket *FcObjectBuckets[OBJECT_HASH_SIZE];
109 static FcObjectType *FcObjects = (FcObjectType *) _FcBaseObjectTypes;
110 static int FcObjectsNumber = NUM_OBJECT_TYPES;
111 static int FcObjectsSize = 0;
112 static FcBool FcObjectsInited;
114 static FcObjectType *
115 FcObjectInsert (const char *name, FcType type)
118 if (FcObjectsNumber >= FcObjectsSize)
120 int newsize = FcObjectsNumber * 2;
121 FcObjectType *newobjects;
124 newobjects = realloc (FcObjects, newsize * sizeof (FcObjectType));
127 newobjects = malloc (newsize * sizeof (FcObjectType));
129 memcpy (newobjects, FcObjects,
130 FcObjectsNumber * sizeof (FcObjectType));
134 FcObjects = newobjects;
135 FcObjectsSize = newsize;
137 o = &FcObjects[FcObjectsNumber];
145 FcObjectId (FcObjectType *o)
147 return o - FcObjects + 1;
150 static FcObjectType *
151 FcObjectFindByName (const char *object, FcBool insert)
153 FcChar32 hash = FcStringHash ((const FcChar8 *) object);
158 if (!FcObjectsInited)
160 for (p = &FcObjectBuckets[hash%OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
162 o = FcObjects + b->id - 1;
163 if (b->hash == hash && !strcmp (object, (o->object)))
169 * Hook it into the hash chain
171 b = malloc (sizeof(FcObjectBucket));
174 object = (const char *) FcStrCopy ((FcChar8 *) object);
179 o = FcObjectInsert (object, -1);
182 b->id = FcObjectId (o);
187 static FcObjectType *
188 FcObjectFindById (FcObject object)
190 if (1 <= object && object <= FcObjectsNumber)
191 return FcObjects + object - 1;
196 FcObjectHashInsert (const FcObjectType *object, FcBool copy)
198 FcChar32 hash = FcStringHash ((const FcChar8 *) object->object);
203 if (!FcObjectsInited)
205 for (p = &FcObjectBuckets[hash%OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
207 o = FcObjects + b->id - 1;
208 if (b->hash == hash && !strcmp (object->object, o->object))
212 * Hook it into the hash chain
214 b = malloc (sizeof(FcObjectBucket));
219 o = FcObjectInsert (object->object, object->type);
227 o = (FcObjectType *) object;
230 b->id = FcObjectId (o);
236 FcObjectHashRemove (const FcObjectType *object, FcBool cleanobj)
238 FcChar32 hash = FcStringHash ((const FcChar8 *) object->object);
243 if (!FcObjectsInited)
245 for (p = &FcObjectBuckets[hash%OBJECT_HASH_SIZE]; (b = *p); p = &(b->next))
247 o = FcObjects + b->id - 1;
248 if (b->hash == hash && !strcmp (object->object, o->object))
254 /* Clean up object array */
257 while (FcObjects[FcObjectsNumber-1].object == NULL)
266 FcNameRegisterObjectTypes (const FcObjectType *types, int ntypes)
270 for (i = 0; i < ntypes; i++)
271 if (!FcObjectHashInsert (&types[i], FcTrue))
277 FcNameUnregisterObjectTypes (const FcObjectType *types, int ntypes)
281 for (i = 0; i < ntypes; i++)
282 FcObjectHashRemove (&types[i], FcTrue);
287 FcNameGetObjectType (const char *object)
289 return FcObjectFindByName (object, FcFalse);
293 FcObjectValidType (FcObject object, FcType type)
295 FcObjectType *t = FcObjectFindById (object);
303 if (type == FcTypeDouble || type == FcTypeInteger)
307 if (type == FcTypeLangSet || type == FcTypeString)
321 FcObjectFromName (const char * name)
323 FcObjectType *o = FcObjectFindByName (name, FcTrue);
326 return FcObjectId (o);
331 FcObjectGetSet (void)
334 FcObjectSet *os = NULL;
337 os = FcObjectSetCreate ();
338 for (i = 0; i < FcObjectsNumber; i++)
339 FcObjectSetAdd (os, FcObjects[i].object);
352 FcObjectsInited = FcTrue;
353 for (i = 0; i < NUM_OBJECT_TYPES; i++)
354 if (!FcObjectHashInsert (&_FcBaseObjectTypes[i], FcFalse))
363 FcObjectBucket *b, *next;
365 for (i = 0; i < OBJECT_HASH_SIZE; i++)
367 for (b = FcObjectBuckets[i]; b; b = next)
372 FcObjectBuckets[i] = 0;
374 for (i = 0; i < FcObjectsNumber; i++)
375 if (FcObjects[i].type == -1)
376 free ((void*) FcObjects[i].object);
377 if (FcObjects != _FcBaseObjectTypes)
379 FcObjects = (FcObjectType *) _FcBaseObjectTypes;
380 FcObjectsNumber = NUM_OBJECT_TYPES;
382 FcObjectsInited = FcFalse;
386 FcObjectName (FcObject object)
388 FcObjectType *o = FcObjectFindById (object);
395 static const FcConstant _FcBaseConstants[] = {
396 { (FcChar8 *) "thin", "weight", FC_WEIGHT_THIN, },
397 { (FcChar8 *) "extralight", "weight", FC_WEIGHT_EXTRALIGHT, },
398 { (FcChar8 *) "ultralight", "weight", FC_WEIGHT_EXTRALIGHT, },
399 { (FcChar8 *) "light", "weight", FC_WEIGHT_LIGHT, },
400 { (FcChar8 *) "book", "weight", FC_WEIGHT_BOOK, },
401 { (FcChar8 *) "regular", "weight", FC_WEIGHT_REGULAR, },
402 { (FcChar8 *) "medium", "weight", FC_WEIGHT_MEDIUM, },
403 { (FcChar8 *) "demibold", "weight", FC_WEIGHT_DEMIBOLD, },
404 { (FcChar8 *) "semibold", "weight", FC_WEIGHT_DEMIBOLD, },
405 { (FcChar8 *) "bold", "weight", FC_WEIGHT_BOLD, },
406 { (FcChar8 *) "extrabold", "weight", FC_WEIGHT_EXTRABOLD, },
407 { (FcChar8 *) "ultrabold", "weight", FC_WEIGHT_EXTRABOLD, },
408 { (FcChar8 *) "black", "weight", FC_WEIGHT_BLACK, },
409 { (FcChar8 *) "heavy", "weight", FC_WEIGHT_HEAVY, },
411 { (FcChar8 *) "roman", "slant", FC_SLANT_ROMAN, },
412 { (FcChar8 *) "italic", "slant", FC_SLANT_ITALIC, },
413 { (FcChar8 *) "oblique", "slant", FC_SLANT_OBLIQUE, },
415 { (FcChar8 *) "ultracondensed", "width", FC_WIDTH_ULTRACONDENSED },
416 { (FcChar8 *) "extracondensed", "width", FC_WIDTH_EXTRACONDENSED },
417 { (FcChar8 *) "condensed", "width", FC_WIDTH_CONDENSED },
418 { (FcChar8 *) "semicondensed", "width", FC_WIDTH_SEMICONDENSED },
419 { (FcChar8 *) "normal", "width", FC_WIDTH_NORMAL },
420 { (FcChar8 *) "semiexpanded", "width", FC_WIDTH_SEMIEXPANDED },
421 { (FcChar8 *) "expanded", "width", FC_WIDTH_EXPANDED },
422 { (FcChar8 *) "extraexpanded", "width", FC_WIDTH_EXTRAEXPANDED },
423 { (FcChar8 *) "ultraexpanded", "width", FC_WIDTH_ULTRAEXPANDED },
425 { (FcChar8 *) "proportional", "spacing", FC_PROPORTIONAL, },
426 { (FcChar8 *) "dual", "spacing", FC_DUAL, },
427 { (FcChar8 *) "mono", "spacing", FC_MONO, },
428 { (FcChar8 *) "charcell", "spacing", FC_CHARCELL, },
430 { (FcChar8 *) "unknown", "rgba", FC_RGBA_UNKNOWN },
431 { (FcChar8 *) "rgb", "rgba", FC_RGBA_RGB, },
432 { (FcChar8 *) "bgr", "rgba", FC_RGBA_BGR, },
433 { (FcChar8 *) "vrgb", "rgba", FC_RGBA_VRGB },
434 { (FcChar8 *) "vbgr", "rgba", FC_RGBA_VBGR },
435 { (FcChar8 *) "none", "rgba", FC_RGBA_NONE },
437 { (FcChar8 *) "hintnone", "hintstyle", FC_HINT_NONE },
438 { (FcChar8 *) "hintslight", "hintstyle", FC_HINT_SLIGHT },
439 { (FcChar8 *) "hintmedium", "hintstyle", FC_HINT_MEDIUM },
440 { (FcChar8 *) "hintfull", "hintstyle", FC_HINT_FULL },
442 { (FcChar8 *) "antialias", "antialias", FcTrue },
443 { (FcChar8 *) "hinting", "hinting", FcTrue },
444 { (FcChar8 *) "verticallayout", "verticallayout", FcTrue },
445 { (FcChar8 *) "autohint", "autohint", FcTrue },
446 { (FcChar8 *) "globaladvance", "globaladvance", FcTrue },
447 { (FcChar8 *) "outline", "outline", FcTrue },
448 { (FcChar8 *) "scalable", "scalable", FcTrue },
449 { (FcChar8 *) "minspace", "minspace", FcTrue },
450 { (FcChar8 *) "embolden", "embolden", FcTrue },
451 { (FcChar8 *) "embeddedbitmap", "embeddedbitmap", FcTrue },
452 { (FcChar8 *) "decorative", "decorative", FcTrue },
453 { (FcChar8 *) "lcdnone", "lcdfilter", FC_LCD_NONE },
454 { (FcChar8 *) "lcddefault", "lcdfilter", FC_LCD_DEFAULT },
455 { (FcChar8 *) "lcdlight", "lcdfilter", FC_LCD_LIGHT },
456 { (FcChar8 *) "lcdlegacy", "lcdfilter", FC_LCD_LEGACY },
459 #define NUM_FC_CONSTANTS (sizeof _FcBaseConstants/sizeof _FcBaseConstants[0])
461 typedef struct _FcConstantList FcConstantList;
463 struct _FcConstantList {
464 const FcConstantList *next;
465 const FcConstant *consts;
469 static const FcConstantList _FcBaseConstantList = {
475 static const FcConstantList *_FcConstants = &_FcBaseConstantList;
478 FcNameRegisterConstants (const FcConstant *consts, int nconsts)
482 l = (FcConstantList *) malloc (sizeof (FcConstantList));
485 FcMemAlloc (FC_MEM_CONSTANT, sizeof (FcConstantList));
487 l->nconsts = nconsts;
488 l->next = _FcConstants;
494 FcNameUnregisterConstants (const FcConstant *consts, int nconsts)
496 const FcConstantList *l, **prev;
498 for (prev = &_FcConstants;
500 prev = (const FcConstantList **) &(l->next))
502 if (l->consts == consts && l->nconsts == nconsts)
505 FcMemFree (FC_MEM_CONSTANT, sizeof (FcConstantList));
514 FcNameGetConstant (FcChar8 *string)
516 const FcConstantList *l;
519 for (l = _FcConstants; l; l = l->next)
521 for (i = 0; i < l->nconsts; i++)
522 if (!FcStrCmpIgnoreCase (string, l->consts[i].name))
523 return &l->consts[i];
529 FcNameConstant (FcChar8 *string, int *result)
533 if ((c = FcNameGetConstant(string)))
542 FcNameBool (const FcChar8 *v, FcBool *result)
548 if (c0 == 't' || c0 == 'y' || c0 == '1')
553 if (c0 == 'f' || c0 == 'n' || c0 == '0')
577 FcNameConvert (FcType type, FcChar8 *string, FcMatrix *m)
584 if (!FcNameConstant (string, &v.u.i))
585 v.u.i = atoi ((char *) string);
588 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);
609 v.u.l = FcNameParseLangSet (string);
619 static const FcChar8 *
620 FcNameFindNext (const FcChar8 *cur, const char *delim, FcChar8 *save, FcChar8 *last)
632 else if (strchr (delim, c))
645 FcNameParse (const FcChar8 *name)
654 const FcObjectType *t;
658 save = malloc (strlen ((char *) name) + 1);
661 pat = FcPatternCreate ();
667 name = FcNameFindNext (name, "-,:", save, &delim);
670 if (!FcPatternAddString (pat, FC_FAMILY, save))
680 name = FcNameFindNext (name, "-,:", save, &delim);
681 d = strtod ((char *) save, (char **) &e);
684 if (!FcPatternAddDouble (pat, FC_SIZE, d))
693 name = FcNameFindNext (name, "=_:", save, &delim);
696 if (delim == '=' || delim == '_')
698 t = FcNameGetObjectType ((char *) save);
701 name = FcNameFindNext (name, ":,", save, &delim);
704 v = FcNameConvert (t->type, save, &m);
705 if (!FcPatternAdd (pat, t->object, v, FcTrue))
709 FcCharSetDestroy ((FcCharSet *) v.u.c);
712 FcLangSetDestroy ((FcLangSet *) v.u.l);
721 FcCharSetDestroy ((FcCharSet *) v.u.c);
724 FcLangSetDestroy ((FcLangSet *) v.u.l);
736 if ((c = FcNameGetConstant (save)))
738 t = FcNameGetObjectType ((char *) c->object);
742 if (!FcPatternAddInteger (pat, c->object, c->value))
746 if (!FcPatternAddBool (pat, c->object, c->value))
761 FcPatternDestroy (pat);
768 FcNameUnparseString (FcStrBuf *buf,
769 const FcChar8 *string,
770 const FcChar8 *escape)
773 while ((c = *string++))
775 if (escape && strchr ((char *) escape, (char) c))
777 if (!FcStrBufChar (buf, escape[0]))
780 if (!FcStrBufChar (buf, c))
787 FcNameUnparseValue (FcStrBuf *buf,
792 FcValue v = FcValueCanonicalize(v0);
798 sprintf ((char *) temp, "%d", v.u.i);
799 return FcNameUnparseString (buf, temp, 0);
801 sprintf ((char *) temp, "%g", v.u.d);
802 return FcNameUnparseString (buf, temp, 0);
804 return FcNameUnparseString (buf, v.u.s, escape);
806 return FcNameUnparseString (buf, v.u.b ? (FcChar8 *) "True" : (FcChar8 *) "False", 0);
808 sprintf ((char *) temp, "%g %g %g %g",
809 v.u.m->xx, v.u.m->xy, v.u.m->yx, v.u.m->yy);
810 return FcNameUnparseString (buf, temp, 0);
812 return FcNameUnparseCharSet (buf, v.u.c);
814 return FcNameUnparseLangSet (buf, v.u.l);
822 FcNameUnparseValueList (FcStrBuf *buf,
828 if (!FcNameUnparseValue (buf, &v->value, escape))
830 if ((v = FcValueListNext(v)) != NULL)
831 if (!FcNameUnparseString (buf, (FcChar8 *) ",", 0))
837 #define FC_ESCAPE_FIXED "\\-:,"
838 #define FC_ESCAPE_VARIABLE "\\=_:,"
841 FcNameUnparse (FcPattern *pat)
843 return FcNameUnparseEscaped (pat, FcTrue);
847 FcNameUnparseEscaped (FcPattern *pat, FcBool escape)
850 FcChar8 buf_static[8192];
853 const FcObjectTypeList *l;
854 const FcObjectType *o;
856 FcStrBufInit (&buf, buf_static, sizeof (buf_static));
857 e = FcPatternObjectFindElt (pat, FC_FAMILY_OBJECT);
860 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
863 e = FcPatternObjectFindElt (pat, FC_SIZE_OBJECT);
866 if (!FcNameUnparseString (&buf, (FcChar8 *) "-", 0))
868 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ? (FcChar8 *) FC_ESCAPE_FIXED : 0))
871 for (l = _FcObjectTypes; l; l = l->next)
873 for (i = 0; i < l->ntypes; i++)
876 if (!strcmp (o->object, FC_FAMILY) ||
877 !strcmp (o->object, FC_SIZE) ||
878 !strcmp (o->object, FC_FILE))
881 e = FcPatternObjectFindElt (pat, FcObjectFromName (o->object));
884 if (!FcNameUnparseString (&buf, (FcChar8 *) ":", 0))
886 if (!FcNameUnparseString (&buf, (FcChar8 *) o->object, escape ? (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
888 if (!FcNameUnparseString (&buf, (FcChar8 *) "=", 0))
890 if (!FcNameUnparseValueList (&buf, FcPatternEltValues(e), escape ?
891 (FcChar8 *) FC_ESCAPE_VARIABLE : 0))
896 return FcStrBufDone (&buf);
898 FcStrBufDestroy (&buf);
902 #include "fcaliastail.h"